* dbusbind.c (Fdbus__init_bus, Qdbus__init_bus, Sdbus__init_bus):
[emacs.git] / src / xdisp.c
blob836b825aafa0a4ce0801cad315ec71d34d60f595
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
310 #define INFINITY 10000000
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
335 static Lisp_Object Qfontification_functions;
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
341 /* Non-nil means don't actually do any redisplay. */
343 Lisp_Object Qinhibit_redisplay;
345 /* Names of text properties relevant for redisplay. */
347 Lisp_Object Qdisplay;
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
358 #ifdef HAVE_WINDOW_SYSTEM
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
391 /* Name of the face used to highlight trailing whitespace. */
393 static Lisp_Object Qtrailing_whitespace;
395 /* Name and number of the face used to highlight escape glyphs. */
397 static Lisp_Object Qescape_glyph;
399 /* Name and number of the face used to highlight non-breaking spaces. */
401 static Lisp_Object Qnobreak_space;
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
406 Lisp_Object Qimage;
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
419 bool noninteractive_need_newline;
421 /* Non-zero means print newline to message log before next message. */
423 static bool message_log_need_newline;
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
437 static struct text_pos this_line_start_pos;
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
442 static struct text_pos this_line_end_pos;
444 /* The vertical positions and the height of this line. */
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
453 static int this_line_start_x;
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
459 static struct text_pos this_line_min_pos;
461 /* Buffer that this_line_.* variables are referring to. */
463 static struct buffer *this_line_buffer;
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478 Lisp_Object Qmenu_bar_update_hook;
480 /* Nonzero if an overlay arrow has been displayed in this window. */
482 static bool overlay_arrow_seen;
484 /* Vector containing glyphs for an ellipsis `...'. */
486 static Lisp_Object default_invis_vector[3];
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
492 Lisp_Object echo_area_window;
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
499 static Lisp_Object Vmessage_stack;
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
504 static bool message_enable_multibyte;
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
512 int update_mode_lines;
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
521 int windows_or_buffers_changed;
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
526 static bool line_number_displayed;
528 /* The name of the *Messages* buffer, a string. */
530 static Lisp_Object Vmessages_buffer_name;
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
535 Lisp_Object echo_area_buffer[2];
537 /* The buffers referenced from echo_area_buffer. */
539 static Lisp_Object echo_buffer[2];
541 /* A vector saved used in with_area_buffer to reduce consing. */
543 static Lisp_Object Vwith_echo_area_save_vector;
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
548 static bool display_last_displayed_message_p;
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
553 static bool message_buf_print;
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
563 static bool message_cleared_p;
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
572 /* Ascent and height of the last line processed by move_it_to. */
574 static int last_height;
576 /* Non-zero if there's a help-echo in the echo area. */
578 bool help_echo_showing_p;
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
586 #define TEXT_PROP_DISTANCE_LIMIT 100
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
614 void
615 redisplay_other_windows (void)
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
621 void
622 wset_redisplay (struct window *w)
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
630 void
631 fset_redisplay (struct frame *f)
633 redisplay_other_windows ();
634 f->redisplay = true;
637 void
638 bset_redisplay (struct buffer *b)
640 int count = buffer_window_count (b);
641 if (count > 0)
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
653 void
654 bset_update_mode_line (struct buffer *b)
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
661 #ifdef GLYPH_DEBUG
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
666 bool trace_redisplay_p;
668 #endif /* GLYPH_DEBUG */
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
679 static Lisp_Object Qauto_hscroll_mode;
681 /* Buffer being redisplayed -- for redisplay_window_error. */
683 static struct buffer *displayed_buffer;
685 /* Value returned from text property handlers (see below). */
687 enum prop_handled
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
695 /* A description of text properties that redisplay is interested
696 in. */
698 struct props
700 /* The name of the property. */
701 Lisp_Object *name;
703 /* A unique index for the property. */
704 enum prop_idx idx;
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
718 /* Properties handled by iterators. */
720 static struct props it_props[] =
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
737 /* Enumeration returned by some move_it_.* functions internally. */
739 enum move_it_result
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
770 /* Similarly for the image cache. */
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
780 /* True while redisplay_internal is in progress. */
782 bool redisplaying_p;
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
795 /* Temporary variable for XTread_socket. */
797 Lisp_Object previous_help_echo_string;
799 /* Platform-independent portion of hourglass implementation. */
801 #ifdef HAVE_WINDOW_SYSTEM
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
810 #endif /* HAVE_WINDOW_SYSTEM */
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
825 #ifdef HAVE_WINDOW_SYSTEM
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
830 #endif /* HAVE_WINDOW_SYSTEM */
832 /* Function prototypes. */
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
846 static void handle_line_prefix (struct it *);
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
971 #ifdef HAVE_WINDOW_SYSTEM
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
984 #endif /* HAVE_WINDOW_SYSTEM */
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
1000 This is the height of W minus the height of a mode line, if any. */
1003 window_text_bottom_y (struct window *w)
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1020 window_box_width (struct window *w, enum glyph_row_area area)
1022 int width = w->pixel_width;
1024 if (!w->pseudo_window_p)
1026 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 width -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1038 /* With wide margins, fringes, etc. we might end up with a negative
1039 width, correct that here. */
1040 return max (0, width);
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1048 window_box_height (struct window *w)
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_PIXEL_HEIGHT (w);
1053 eassert (height >= 0);
1055 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1063 if (WINDOW_WANTS_MODELINE_P (w))
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. ANY_AREA means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1097 window_box_left_offset (struct window *w, enum glyph_row_area area)
1099 int x;
1101 if (w->pseudo_window_p)
1102 return 0;
1104 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1106 if (area == TEXT_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA));
1109 else if (area == RIGHT_MARGIN_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA)
1112 + window_box_width (w, TEXT_AREA)
1113 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1115 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1116 else if (area == LEFT_MARGIN_AREA
1117 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1118 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1120 /* Don't return more than the window's pixel width. */
1121 return min (x, w->pixel_width);
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. ANY_AREA means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1130 window_box_right_offset (struct window *w, enum glyph_row_area area)
1132 /* Don't return more than the window's pixel width. */
1133 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1134 w->pixel_width);
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. ANY_AREA means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1142 window_box_left (struct window *w, enum glyph_row_area area)
1144 struct frame *f = XFRAME (w->frame);
1145 int x;
1147 if (w->pseudo_window_p)
1148 return FRAME_INTERNAL_BORDER_WIDTH (f);
1150 x = (WINDOW_LEFT_EDGE_X (w)
1151 + window_box_left_offset (w, area));
1153 return x;
1157 /* Return the frame-relative coordinate of the right edge of display
1158 area AREA of window W. ANY_AREA means return the right edge of the
1159 whole window, to the left of the right fringe of W. */
1162 window_box_right (struct window *w, enum glyph_row_area area)
1164 return window_box_left (w, area) + window_box_width (w, area);
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines, in frame-relative coordinates. ANY_AREA means the
1169 whole window, not including the left and right fringes of
1170 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1171 coordinates of the upper-left corner of the box. Return in
1172 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1174 void
1175 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1176 int *box_y, int *box_width, int *box_height)
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1192 #ifdef HAVE_WINDOW_SYSTEM
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1201 static void
1202 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1203 int *bottom_right_x, int *bottom_right_y)
1205 window_box (w, ANY_AREA, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y);
1207 *bottom_right_x += *top_left_x;
1208 *bottom_right_y += *top_left_y;
1211 #endif /* HAVE_WINDOW_SYSTEM */
1213 /***********************************************************************
1214 Utilities
1215 ***********************************************************************/
1217 /* Return the bottom y-position of the line the iterator IT is in.
1218 This can modify IT's settings. */
1221 line_bottom_y (struct it *it)
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1226 if (line_height == 0)
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1232 move_it_by_lines (it, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1237 else
1239 struct glyph_row *row = it->glyph_row;
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1252 return line_top_y + line_height;
1255 DEFUN ("line-pixel-height", Fline_pixel_height,
1256 Sline_pixel_height, 0, 0, 0,
1257 doc: /* Return height in pixels of text line in the selected window.
1259 Value is the height in pixels of the line at point. */)
1260 (void)
1262 struct it it;
1263 struct text_pos pt;
1264 struct window *w = XWINDOW (selected_window);
1266 SET_TEXT_POS (pt, PT, PT_BYTE);
1267 start_display (&it, w, pt);
1268 it.vpos = it.current_y = 0;
1269 last_height = 0;
1270 return make_number (line_bottom_y (&it));
1273 /* Return the default pixel height of text lines in window W. The
1274 value is the canonical height of the W frame's default font, plus
1275 any extra space required by the line-spacing variable or frame
1276 parameter.
1278 Implementation note: this ignores any line-spacing text properties
1279 put on the newline characters. This is because those properties
1280 only affect the _screen_ line ending in the newline (i.e., in a
1281 continued line, only the last screen line will be affected), which
1282 means only a small number of lines in a buffer can ever use this
1283 feature. Since this function is used to compute the default pixel
1284 equivalent of text lines in a window, we can safely ignore those
1285 few lines. For the same reasons, we ignore the line-height
1286 properties. */
1288 default_line_pixel_height (struct window *w)
1290 struct frame *f = WINDOW_XFRAME (w);
1291 int height = FRAME_LINE_HEIGHT (f);
1293 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1295 struct buffer *b = XBUFFER (w->contents);
1296 Lisp_Object val = BVAR (b, extra_line_spacing);
1298 if (NILP (val))
1299 val = BVAR (&buffer_defaults, extra_line_spacing);
1300 if (!NILP (val))
1302 if (RANGED_INTEGERP (0, val, INT_MAX))
1303 height += XFASTINT (val);
1304 else if (FLOATP (val))
1306 int addon = XFLOAT_DATA (val) * height + 0.5;
1308 if (addon >= 0)
1309 height += addon;
1312 else
1313 height += f->extra_line_spacing;
1316 return height;
1319 /* Subroutine of pos_visible_p below. Extracts a display string, if
1320 any, from the display spec given as its argument. */
1321 static Lisp_Object
1322 string_from_display_spec (Lisp_Object spec)
1324 if (CONSP (spec))
1326 while (CONSP (spec))
1328 if (STRINGP (XCAR (spec)))
1329 return XCAR (spec);
1330 spec = XCDR (spec);
1333 else if (VECTORP (spec))
1335 ptrdiff_t i;
1337 for (i = 0; i < ASIZE (spec); i++)
1339 if (STRINGP (AREF (spec, i)))
1340 return AREF (spec, i);
1342 return Qnil;
1345 return spec;
1349 /* Limit insanely large values of W->hscroll on frame F to the largest
1350 value that will still prevent first_visible_x and last_visible_x of
1351 'struct it' from overflowing an int. */
1352 static int
1353 window_hscroll_limited (struct window *w, struct frame *f)
1355 ptrdiff_t window_hscroll = w->hscroll;
1356 int window_text_width = window_box_width (w, TEXT_AREA);
1357 int colwidth = FRAME_COLUMN_WIDTH (f);
1359 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1360 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1362 return window_hscroll;
1365 /* Return 1 if position CHARPOS is visible in window W.
1366 CHARPOS < 0 means return info about WINDOW_END position.
1367 If visible, set *X and *Y to pixel coordinates of top left corner.
1368 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1369 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1372 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1373 int *rtop, int *rbot, int *rowh, int *vpos)
1375 struct it it;
1376 void *itdata = bidi_shelve_cache ();
1377 struct text_pos top;
1378 int visible_p = 0;
1379 struct buffer *old_buffer = NULL;
1381 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1382 return visible_p;
1384 if (XBUFFER (w->contents) != current_buffer)
1386 old_buffer = current_buffer;
1387 set_buffer_internal_1 (XBUFFER (w->contents));
1390 SET_TEXT_POS_FROM_MARKER (top, w->start);
1391 /* Scrolling a minibuffer window via scroll bar when the echo area
1392 shows long text sometimes resets the minibuffer contents behind
1393 our backs. */
1394 if (CHARPOS (top) > ZV)
1395 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1397 /* Compute exact mode line heights. */
1398 if (WINDOW_WANTS_MODELINE_P (w))
1399 w->mode_line_height
1400 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1401 BVAR (current_buffer, mode_line_format));
1403 if (WINDOW_WANTS_HEADER_LINE_P (w))
1404 w->header_line_height
1405 = display_mode_line (w, HEADER_LINE_FACE_ID,
1406 BVAR (current_buffer, header_line_format));
1408 start_display (&it, w, top);
1409 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1410 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1412 if (charpos >= 0
1413 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1414 && IT_CHARPOS (it) >= charpos)
1415 /* When scanning backwards under bidi iteration, move_it_to
1416 stops at or _before_ CHARPOS, because it stops at or to
1417 the _right_ of the character at CHARPOS. */
1418 || (it.bidi_p && it.bidi_it.scan_dir == -1
1419 && IT_CHARPOS (it) <= charpos)))
1421 /* We have reached CHARPOS, or passed it. How the call to
1422 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1423 or covered by a display property, move_it_to stops at the end
1424 of the invisible text, to the right of CHARPOS. (ii) If
1425 CHARPOS is in a display vector, move_it_to stops on its last
1426 glyph. */
1427 int top_x = it.current_x;
1428 int top_y = it.current_y;
1429 /* Calling line_bottom_y may change it.method, it.position, etc. */
1430 enum it_method it_method = it.method;
1431 int bottom_y = (last_height = 0, line_bottom_y (&it));
1432 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1434 if (top_y < window_top_y)
1435 visible_p = bottom_y > window_top_y;
1436 else if (top_y < it.last_visible_y)
1437 visible_p = true;
1438 if (bottom_y >= it.last_visible_y
1439 && it.bidi_p && it.bidi_it.scan_dir == -1
1440 && IT_CHARPOS (it) < charpos)
1442 /* When the last line of the window is scanned backwards
1443 under bidi iteration, we could be duped into thinking
1444 that we have passed CHARPOS, when in fact move_it_to
1445 simply stopped short of CHARPOS because it reached
1446 last_visible_y. To see if that's what happened, we call
1447 move_it_to again with a slightly larger vertical limit,
1448 and see if it actually moved vertically; if it did, we
1449 didn't really reach CHARPOS, which is beyond window end. */
1450 struct it save_it = it;
1451 /* Why 10? because we don't know how many canonical lines
1452 will the height of the next line(s) be. So we guess. */
1453 int ten_more_lines = 10 * default_line_pixel_height (w);
1455 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1456 MOVE_TO_POS | MOVE_TO_Y);
1457 if (it.current_y > top_y)
1458 visible_p = 0;
1460 it = save_it;
1462 if (visible_p)
1464 if (it_method == GET_FROM_DISPLAY_VECTOR)
1466 /* We stopped on the last glyph of a display vector.
1467 Try and recompute. Hack alert! */
1468 if (charpos < 2 || top.charpos >= charpos)
1469 top_x = it.glyph_row->x;
1470 else
1472 struct it it2, it2_prev;
1473 /* The idea is to get to the previous buffer
1474 position, consume the character there, and use
1475 the pixel coordinates we get after that. But if
1476 the previous buffer position is also displayed
1477 from a display vector, we need to consume all of
1478 the glyphs from that display vector. */
1479 start_display (&it2, w, top);
1480 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1481 /* If we didn't get to CHARPOS - 1, there's some
1482 replacing display property at that position, and
1483 we stopped after it. That is exactly the place
1484 whose coordinates we want. */
1485 if (IT_CHARPOS (it2) != charpos - 1)
1486 it2_prev = it2;
1487 else
1489 /* Iterate until we get out of the display
1490 vector that displays the character at
1491 CHARPOS - 1. */
1492 do {
1493 get_next_display_element (&it2);
1494 PRODUCE_GLYPHS (&it2);
1495 it2_prev = it2;
1496 set_iterator_to_next (&it2, 1);
1497 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1498 && IT_CHARPOS (it2) < charpos);
1500 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1501 || it2_prev.current_x > it2_prev.last_visible_x)
1502 top_x = it.glyph_row->x;
1503 else
1505 top_x = it2_prev.current_x;
1506 top_y = it2_prev.current_y;
1510 else if (IT_CHARPOS (it) != charpos)
1512 Lisp_Object cpos = make_number (charpos);
1513 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1514 Lisp_Object string = string_from_display_spec (spec);
1515 struct text_pos tpos;
1516 int replacing_spec_p;
1517 bool newline_in_string
1518 = (STRINGP (string)
1519 && memchr (SDATA (string), '\n', SBYTES (string)));
1521 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1522 replacing_spec_p
1523 = (!NILP (spec)
1524 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1525 charpos, FRAME_WINDOW_P (it.f)));
1526 /* The tricky code below is needed because there's a
1527 discrepancy between move_it_to and how we set cursor
1528 when PT is at the beginning of a portion of text
1529 covered by a display property or an overlay with a
1530 display property, or the display line ends in a
1531 newline from a display string. move_it_to will stop
1532 _after_ such display strings, whereas
1533 set_cursor_from_row conspires with cursor_row_p to
1534 place the cursor on the first glyph produced from the
1535 display string. */
1537 /* We have overshoot PT because it is covered by a
1538 display property that replaces the text it covers.
1539 If the string includes embedded newlines, we are also
1540 in the wrong display line. Backtrack to the correct
1541 line, where the display property begins. */
1542 if (replacing_spec_p)
1544 Lisp_Object startpos, endpos;
1545 EMACS_INT start, end;
1546 struct it it3;
1547 int it3_moved;
1549 /* Find the first and the last buffer positions
1550 covered by the display string. */
1551 endpos =
1552 Fnext_single_char_property_change (cpos, Qdisplay,
1553 Qnil, Qnil);
1554 startpos =
1555 Fprevious_single_char_property_change (endpos, Qdisplay,
1556 Qnil, Qnil);
1557 start = XFASTINT (startpos);
1558 end = XFASTINT (endpos);
1559 /* Move to the last buffer position before the
1560 display property. */
1561 start_display (&it3, w, top);
1562 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1563 /* Move forward one more line if the position before
1564 the display string is a newline or if it is the
1565 rightmost character on a line that is
1566 continued or word-wrapped. */
1567 if (it3.method == GET_FROM_BUFFER
1568 && (it3.c == '\n'
1569 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1570 move_it_by_lines (&it3, 1);
1571 else if (move_it_in_display_line_to (&it3, -1,
1572 it3.current_x
1573 + it3.pixel_width,
1574 MOVE_TO_X)
1575 == MOVE_LINE_CONTINUED)
1577 move_it_by_lines (&it3, 1);
1578 /* When we are under word-wrap, the #$@%!
1579 move_it_by_lines moves 2 lines, so we need to
1580 fix that up. */
1581 if (it3.line_wrap == WORD_WRAP)
1582 move_it_by_lines (&it3, -1);
1585 /* Record the vertical coordinate of the display
1586 line where we wound up. */
1587 top_y = it3.current_y;
1588 if (it3.bidi_p)
1590 /* When characters are reordered for display,
1591 the character displayed to the left of the
1592 display string could be _after_ the display
1593 property in the logical order. Use the
1594 smallest vertical position of these two. */
1595 start_display (&it3, w, top);
1596 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1597 if (it3.current_y < top_y)
1598 top_y = it3.current_y;
1600 /* Move from the top of the window to the beginning
1601 of the display line where the display string
1602 begins. */
1603 start_display (&it3, w, top);
1604 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1605 /* If it3_moved stays zero after the 'while' loop
1606 below, that means we already were at a newline
1607 before the loop (e.g., the display string begins
1608 with a newline), so we don't need to (and cannot)
1609 inspect the glyphs of it3.glyph_row, because
1610 PRODUCE_GLYPHS will not produce anything for a
1611 newline, and thus it3.glyph_row stays at its
1612 stale content it got at top of the window. */
1613 it3_moved = 0;
1614 /* Finally, advance the iterator until we hit the
1615 first display element whose character position is
1616 CHARPOS, or until the first newline from the
1617 display string, which signals the end of the
1618 display line. */
1619 while (get_next_display_element (&it3))
1621 PRODUCE_GLYPHS (&it3);
1622 if (IT_CHARPOS (it3) == charpos
1623 || ITERATOR_AT_END_OF_LINE_P (&it3))
1624 break;
1625 it3_moved = 1;
1626 set_iterator_to_next (&it3, 0);
1628 top_x = it3.current_x - it3.pixel_width;
1629 /* Normally, we would exit the above loop because we
1630 found the display element whose character
1631 position is CHARPOS. For the contingency that we
1632 didn't, and stopped at the first newline from the
1633 display string, move back over the glyphs
1634 produced from the string, until we find the
1635 rightmost glyph not from the string. */
1636 if (it3_moved
1637 && newline_in_string
1638 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1640 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1641 + it3.glyph_row->used[TEXT_AREA];
1643 while (EQ ((g - 1)->object, string))
1645 --g;
1646 top_x -= g->pixel_width;
1648 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1649 + it3.glyph_row->used[TEXT_AREA]);
1654 *x = top_x;
1655 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1656 *rtop = max (0, window_top_y - top_y);
1657 *rbot = max (0, bottom_y - it.last_visible_y);
1658 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1659 - max (top_y, window_top_y)));
1660 *vpos = it.vpos;
1663 else
1665 /* We were asked to provide info about WINDOW_END. */
1666 struct it it2;
1667 void *it2data = NULL;
1669 SAVE_IT (it2, it, it2data);
1670 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1671 move_it_by_lines (&it, 1);
1672 if (charpos < IT_CHARPOS (it)
1673 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1675 visible_p = true;
1676 RESTORE_IT (&it2, &it2, it2data);
1677 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1678 *x = it2.current_x;
1679 *y = it2.current_y + it2.max_ascent - it2.ascent;
1680 *rtop = max (0, -it2.current_y);
1681 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1682 - it.last_visible_y));
1683 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1684 it.last_visible_y)
1685 - max (it2.current_y,
1686 WINDOW_HEADER_LINE_HEIGHT (w))));
1687 *vpos = it2.vpos;
1689 else
1690 bidi_unshelve_cache (it2data, 1);
1692 bidi_unshelve_cache (itdata, 0);
1694 if (old_buffer)
1695 set_buffer_internal_1 (old_buffer);
1697 if (visible_p && w->hscroll > 0)
1698 *x -=
1699 window_hscroll_limited (w, WINDOW_XFRAME (w))
1700 * WINDOW_FRAME_COLUMN_WIDTH (w);
1702 #if 0
1703 /* Debugging code. */
1704 if (visible_p)
1705 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1706 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1707 else
1708 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1709 #endif
1711 return visible_p;
1715 /* Return the next character from STR. Return in *LEN the length of
1716 the character. This is like STRING_CHAR_AND_LENGTH but never
1717 returns an invalid character. If we find one, we return a `?', but
1718 with the length of the invalid character. */
1720 static int
1721 string_char_and_length (const unsigned char *str, int *len)
1723 int c;
1725 c = STRING_CHAR_AND_LENGTH (str, *len);
1726 if (!CHAR_VALID_P (c))
1727 /* We may not change the length here because other places in Emacs
1728 don't use this function, i.e. they silently accept invalid
1729 characters. */
1730 c = '?';
1732 return c;
1737 /* Given a position POS containing a valid character and byte position
1738 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1740 static struct text_pos
1741 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1743 eassert (STRINGP (string) && nchars >= 0);
1745 if (STRING_MULTIBYTE (string))
1747 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1748 int len;
1750 while (nchars--)
1752 string_char_and_length (p, &len);
1753 p += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1758 else
1759 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1761 return pos;
1765 /* Value is the text position, i.e. character and byte position,
1766 for character position CHARPOS in STRING. */
1768 static struct text_pos
1769 string_pos (ptrdiff_t charpos, Lisp_Object string)
1771 struct text_pos pos;
1772 eassert (STRINGP (string));
1773 eassert (charpos >= 0);
1774 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1775 return pos;
1779 /* Value is a text position, i.e. character and byte position, for
1780 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1781 means recognize multibyte characters. */
1783 static struct text_pos
1784 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1786 struct text_pos pos;
1788 eassert (s != NULL);
1789 eassert (charpos >= 0);
1791 if (multibyte_p)
1793 int len;
1795 SET_TEXT_POS (pos, 0, 0);
1796 while (charpos--)
1798 string_char_and_length ((const unsigned char *) s, &len);
1799 s += len;
1800 CHARPOS (pos) += 1;
1801 BYTEPOS (pos) += len;
1804 else
1805 SET_TEXT_POS (pos, charpos, charpos);
1807 return pos;
1811 /* Value is the number of characters in C string S. MULTIBYTE_P
1812 non-zero means recognize multibyte characters. */
1814 static ptrdiff_t
1815 number_of_chars (const char *s, bool multibyte_p)
1817 ptrdiff_t nchars;
1819 if (multibyte_p)
1821 ptrdiff_t rest = strlen (s);
1822 int len;
1823 const unsigned char *p = (const unsigned char *) s;
1825 for (nchars = 0; rest > 0; ++nchars)
1827 string_char_and_length (p, &len);
1828 rest -= len, p += len;
1831 else
1832 nchars = strlen (s);
1834 return nchars;
1838 /* Compute byte position NEWPOS->bytepos corresponding to
1839 NEWPOS->charpos. POS is a known position in string STRING.
1840 NEWPOS->charpos must be >= POS.charpos. */
1842 static void
1843 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1845 eassert (STRINGP (string));
1846 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1848 if (STRING_MULTIBYTE (string))
1849 *newpos = string_pos_nchars_ahead (pos, string,
1850 CHARPOS (*newpos) - CHARPOS (pos));
1851 else
1852 BYTEPOS (*newpos) = CHARPOS (*newpos);
1855 /* EXPORT:
1856 Return an estimation of the pixel height of mode or header lines on
1857 frame F. FACE_ID specifies what line's height to estimate. */
1860 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1865 int height = FONT_HEIGHT (FRAME_FONT (f));
1867 /* This function is called so early when Emacs starts that the face
1868 cache and mode line face are not yet initialized. */
1869 if (FRAME_FACE_CACHE (f))
1871 struct face *face = FACE_FROM_ID (f, face_id);
1872 if (face)
1874 if (face->font)
1875 height = FONT_HEIGHT (face->font);
1876 if (face->box_line_width > 0)
1877 height += 2 * face->box_line_width;
1881 return height;
1883 #endif
1885 return 1;
1888 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1889 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1890 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1891 not force the value into range. */
1893 void
1894 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1895 int *x, int *y, NativeRectangle *bounds, int noclip)
1898 #ifdef HAVE_WINDOW_SYSTEM
1899 if (FRAME_WINDOW_P (f))
1901 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1902 even for negative values. */
1903 if (pix_x < 0)
1904 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1905 if (pix_y < 0)
1906 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1908 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1909 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1911 if (bounds)
1912 STORE_NATIVE_RECT (*bounds,
1913 FRAME_COL_TO_PIXEL_X (f, pix_x),
1914 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1915 FRAME_COLUMN_WIDTH (f) - 1,
1916 FRAME_LINE_HEIGHT (f) - 1);
1918 /* PXW: Should we clip pixels before converting to columns/lines? */
1919 if (!noclip)
1921 if (pix_x < 0)
1922 pix_x = 0;
1923 else if (pix_x > FRAME_TOTAL_COLS (f))
1924 pix_x = FRAME_TOTAL_COLS (f);
1926 if (pix_y < 0)
1927 pix_y = 0;
1928 else if (pix_y > FRAME_LINES (f))
1929 pix_y = FRAME_LINES (f);
1932 #endif
1934 *x = pix_x;
1935 *y = pix_y;
1939 /* Find the glyph under window-relative coordinates X/Y in window W.
1940 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1941 strings. Return in *HPOS and *VPOS the row and column number of
1942 the glyph found. Return in *AREA the glyph area containing X.
1943 Value is a pointer to the glyph found or null if X/Y is not on
1944 text, or we can't tell because W's current matrix is not up to
1945 date. */
1947 static struct glyph *
1948 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1949 int *dx, int *dy, int *area)
1951 struct glyph *glyph, *end;
1952 struct glyph_row *row = NULL;
1953 int x0, i;
1955 /* Find row containing Y. Give up if some row is not enabled. */
1956 for (i = 0; i < w->current_matrix->nrows; ++i)
1958 row = MATRIX_ROW (w->current_matrix, i);
1959 if (!row->enabled_p)
1960 return NULL;
1961 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1962 break;
1965 *vpos = i;
1966 *hpos = 0;
1968 /* Give up if Y is not in the window. */
1969 if (i == w->current_matrix->nrows)
1970 return NULL;
1972 /* Get the glyph area containing X. */
1973 if (w->pseudo_window_p)
1975 *area = TEXT_AREA;
1976 x0 = 0;
1978 else
1980 if (x < window_box_left_offset (w, TEXT_AREA))
1982 *area = LEFT_MARGIN_AREA;
1983 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1985 else if (x < window_box_right_offset (w, TEXT_AREA))
1987 *area = TEXT_AREA;
1988 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1990 else
1992 *area = RIGHT_MARGIN_AREA;
1993 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1997 /* Find glyph containing X. */
1998 glyph = row->glyphs[*area];
1999 end = glyph + row->used[*area];
2000 x -= x0;
2001 while (glyph < end && x >= glyph->pixel_width)
2003 x -= glyph->pixel_width;
2004 ++glyph;
2007 if (glyph == end)
2008 return NULL;
2010 if (dx)
2012 *dx = x;
2013 *dy = y - (row->y + row->ascent - glyph->ascent);
2016 *hpos = glyph - row->glyphs[*area];
2017 return glyph;
2020 /* Convert frame-relative x/y to coordinates relative to window W.
2021 Takes pseudo-windows into account. */
2023 static void
2024 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2026 if (w->pseudo_window_p)
2028 /* A pseudo-window is always full-width, and starts at the
2029 left edge of the frame, plus a frame border. */
2030 struct frame *f = XFRAME (w->frame);
2031 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2034 else
2036 *x -= WINDOW_LEFT_EDGE_X (w);
2037 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2041 #ifdef HAVE_WINDOW_SYSTEM
2043 /* EXPORT:
2044 Return in RECTS[] at most N clipping rectangles for glyph string S.
2045 Return the number of stored rectangles. */
2048 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2050 XRectangle r;
2052 if (n <= 0)
2053 return 0;
2055 if (s->row->full_width_p)
2057 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2058 r.x = WINDOW_LEFT_EDGE_X (s->w);
2059 if (s->row->mode_line_p)
2060 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2061 else
2062 r.width = WINDOW_PIXEL_WIDTH (s->w);
2064 /* Unless displaying a mode or menu bar line, which are always
2065 fully visible, clip to the visible part of the row. */
2066 if (s->w->pseudo_window_p)
2067 r.height = s->row->visible_height;
2068 else
2069 r.height = s->height;
2071 else
2073 /* This is a text line that may be partially visible. */
2074 r.x = window_box_left (s->w, s->area);
2075 r.width = window_box_width (s->w, s->area);
2076 r.height = s->row->visible_height;
2079 if (s->clip_head)
2080 if (r.x < s->clip_head->x)
2082 if (r.width >= s->clip_head->x - r.x)
2083 r.width -= s->clip_head->x - r.x;
2084 else
2085 r.width = 0;
2086 r.x = s->clip_head->x;
2088 if (s->clip_tail)
2089 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2091 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2092 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2093 else
2094 r.width = 0;
2097 /* If S draws overlapping rows, it's sufficient to use the top and
2098 bottom of the window for clipping because this glyph string
2099 intentionally draws over other lines. */
2100 if (s->for_overlaps)
2102 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2103 r.height = window_text_bottom_y (s->w) - r.y;
2105 /* Alas, the above simple strategy does not work for the
2106 environments with anti-aliased text: if the same text is
2107 drawn onto the same place multiple times, it gets thicker.
2108 If the overlap we are processing is for the erased cursor, we
2109 take the intersection with the rectangle of the cursor. */
2110 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2112 XRectangle rc, r_save = r;
2114 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2115 rc.y = s->w->phys_cursor.y;
2116 rc.width = s->w->phys_cursor_width;
2117 rc.height = s->w->phys_cursor_height;
2119 x_intersect_rectangles (&r_save, &rc, &r);
2122 else
2124 /* Don't use S->y for clipping because it doesn't take partially
2125 visible lines into account. For example, it can be negative for
2126 partially visible lines at the top of a window. */
2127 if (!s->row->full_width_p
2128 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2129 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2130 else
2131 r.y = max (0, s->row->y);
2134 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2136 /* If drawing the cursor, don't let glyph draw outside its
2137 advertised boundaries. Cleartype does this under some circumstances. */
2138 if (s->hl == DRAW_CURSOR)
2140 struct glyph *glyph = s->first_glyph;
2141 int height, max_y;
2143 if (s->x > r.x)
2145 r.width -= s->x - r.x;
2146 r.x = s->x;
2148 r.width = min (r.width, glyph->pixel_width);
2150 /* If r.y is below window bottom, ensure that we still see a cursor. */
2151 height = min (glyph->ascent + glyph->descent,
2152 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2153 max_y = window_text_bottom_y (s->w) - height;
2154 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2155 if (s->ybase - glyph->ascent > max_y)
2157 r.y = max_y;
2158 r.height = height;
2160 else
2162 /* Don't draw cursor glyph taller than our actual glyph. */
2163 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2164 if (height < r.height)
2166 max_y = r.y + r.height;
2167 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2168 r.height = min (max_y - r.y, height);
2173 if (s->row->clip)
2175 XRectangle r_save = r;
2177 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2178 r.width = 0;
2181 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2182 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2184 #ifdef CONVERT_FROM_XRECT
2185 CONVERT_FROM_XRECT (r, *rects);
2186 #else
2187 *rects = r;
2188 #endif
2189 return 1;
2191 else
2193 /* If we are processing overlapping and allowed to return
2194 multiple clipping rectangles, we exclude the row of the glyph
2195 string from the clipping rectangle. This is to avoid drawing
2196 the same text on the environment with anti-aliasing. */
2197 #ifdef CONVERT_FROM_XRECT
2198 XRectangle rs[2];
2199 #else
2200 XRectangle *rs = rects;
2201 #endif
2202 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2204 if (s->for_overlaps & OVERLAPS_PRED)
2206 rs[i] = r;
2207 if (r.y + r.height > row_y)
2209 if (r.y < row_y)
2210 rs[i].height = row_y - r.y;
2211 else
2212 rs[i].height = 0;
2214 i++;
2216 if (s->for_overlaps & OVERLAPS_SUCC)
2218 rs[i] = r;
2219 if (r.y < row_y + s->row->visible_height)
2221 if (r.y + r.height > row_y + s->row->visible_height)
2223 rs[i].y = row_y + s->row->visible_height;
2224 rs[i].height = r.y + r.height - rs[i].y;
2226 else
2227 rs[i].height = 0;
2229 i++;
2232 n = i;
2233 #ifdef CONVERT_FROM_XRECT
2234 for (i = 0; i < n; i++)
2235 CONVERT_FROM_XRECT (rs[i], rects[i]);
2236 #endif
2237 return n;
2241 /* EXPORT:
2242 Return in *NR the clipping rectangle for glyph string S. */
2244 void
2245 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2247 get_glyph_string_clip_rects (s, nr, 1);
2251 /* EXPORT:
2252 Return the position and height of the phys cursor in window W.
2253 Set w->phys_cursor_width to width of phys cursor.
2256 void
2257 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2258 struct glyph *glyph, int *xp, int *yp, int *heightp)
2260 struct frame *f = XFRAME (WINDOW_FRAME (w));
2261 int x, y, wd, h, h0, y0;
2263 /* Compute the width of the rectangle to draw. If on a stretch
2264 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2265 rectangle as wide as the glyph, but use a canonical character
2266 width instead. */
2267 wd = glyph->pixel_width - 1;
2268 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2269 wd++; /* Why? */
2270 #endif
2272 x = w->phys_cursor.x;
2273 if (x < 0)
2275 wd += x;
2276 x = 0;
2279 if (glyph->type == STRETCH_GLYPH
2280 && !x_stretch_cursor_p)
2281 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2282 w->phys_cursor_width = wd;
2284 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2286 /* If y is below window bottom, ensure that we still see a cursor. */
2287 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2289 h = max (h0, glyph->ascent + glyph->descent);
2290 h0 = min (h0, glyph->ascent + glyph->descent);
2292 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2293 if (y < y0)
2295 h = max (h - (y0 - y) + 1, h0);
2296 y = y0 - 1;
2298 else
2300 y0 = window_text_bottom_y (w) - h0;
2301 if (y > y0)
2303 h += y - y0;
2304 y = y0;
2308 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2309 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2310 *heightp = h;
2314 * Remember which glyph the mouse is over.
2317 void
2318 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2320 Lisp_Object window;
2321 struct window *w;
2322 struct glyph_row *r, *gr, *end_row;
2323 enum window_part part;
2324 enum glyph_row_area area;
2325 int x, y, width, height;
2327 /* Try to determine frame pixel position and size of the glyph under
2328 frame pixel coordinates X/Y on frame F. */
2330 if (window_resize_pixelwise)
2332 width = height = 1;
2333 goto virtual_glyph;
2335 else if (!f->glyphs_initialized_p
2336 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2337 NILP (window)))
2339 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2340 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2341 goto virtual_glyph;
2344 w = XWINDOW (window);
2345 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2346 height = WINDOW_FRAME_LINE_HEIGHT (w);
2348 x = window_relative_x_coord (w, part, gx);
2349 y = gy - WINDOW_TOP_EDGE_Y (w);
2351 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2352 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2354 if (w->pseudo_window_p)
2356 area = TEXT_AREA;
2357 part = ON_MODE_LINE; /* Don't adjust margin. */
2358 goto text_glyph;
2361 switch (part)
2363 case ON_LEFT_MARGIN:
2364 area = LEFT_MARGIN_AREA;
2365 goto text_glyph;
2367 case ON_RIGHT_MARGIN:
2368 area = RIGHT_MARGIN_AREA;
2369 goto text_glyph;
2371 case ON_HEADER_LINE:
2372 case ON_MODE_LINE:
2373 gr = (part == ON_HEADER_LINE
2374 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2375 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2376 gy = gr->y;
2377 area = TEXT_AREA;
2378 goto text_glyph_row_found;
2380 case ON_TEXT:
2381 area = TEXT_AREA;
2383 text_glyph:
2384 gr = 0; gy = 0;
2385 for (; r <= end_row && r->enabled_p; ++r)
2386 if (r->y + r->height > y)
2388 gr = r; gy = r->y;
2389 break;
2392 text_glyph_row_found:
2393 if (gr && gy <= y)
2395 struct glyph *g = gr->glyphs[area];
2396 struct glyph *end = g + gr->used[area];
2398 height = gr->height;
2399 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2400 if (gx + g->pixel_width > x)
2401 break;
2403 if (g < end)
2405 if (g->type == IMAGE_GLYPH)
2407 /* Don't remember when mouse is over image, as
2408 image may have hot-spots. */
2409 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2410 return;
2412 width = g->pixel_width;
2414 else
2416 /* Use nominal char spacing at end of line. */
2417 x -= gx;
2418 gx += (x / width) * width;
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2422 gx += window_box_left_offset (w, area);
2424 else
2426 /* Use nominal line height at end of window. */
2427 gx = (x / width) * width;
2428 y -= gy;
2429 gy += (y / height) * height;
2431 break;
2433 case ON_LEFT_FRINGE:
2434 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2435 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2436 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2437 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2438 goto row_glyph;
2440 case ON_RIGHT_FRINGE:
2441 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2442 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2443 : window_box_right_offset (w, TEXT_AREA));
2444 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2445 goto row_glyph;
2447 case ON_SCROLL_BAR:
2448 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2450 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2451 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2452 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2453 : 0)));
2454 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2455 goto row_glyph;
2457 case ON_RIGHT_DIVIDER:
2458 gx = WINDOW_RIGHT_PIXEL_EDGE (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2459 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2460 goto row_glyph;
2462 case ON_BOTTOM_DIVIDER:
2463 gx = 0;
2464 width = WINDOW_RIGHT_PIXEL_EDGE (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2466 row_glyph:
2467 gr = 0, gy = 0;
2468 for (; r <= end_row && r->enabled_p; ++r)
2469 if (r->y + r->height > y)
2471 gr = r; gy = r->y;
2472 break;
2475 if (gr && gy <= y)
2476 height = gr->height;
2477 else
2479 /* Use nominal line height at end of window. */
2480 y -= gy;
2481 gy += (y / height) * height;
2483 break;
2485 default:
2487 virtual_glyph:
2488 /* If there is no glyph under the mouse, then we divide the screen
2489 into a grid of the smallest glyph in the frame, and use that
2490 as our "glyph". */
2492 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2493 round down even for negative values. */
2494 if (gx < 0)
2495 gx -= width - 1;
2496 if (gy < 0)
2497 gy -= height - 1;
2499 gx = (gx / width) * width;
2500 gy = (gy / height) * height;
2502 goto store_rect;
2505 gx += WINDOW_LEFT_EDGE_X (w);
2506 gy += WINDOW_TOP_EDGE_Y (w);
2508 store_rect:
2509 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2511 /* Visible feedback for debugging. */
2512 #if 0
2513 #if HAVE_X_WINDOWS
2514 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2515 f->output_data.x->normal_gc,
2516 gx, gy, width, height);
2517 #endif
2518 #endif
2522 #endif /* HAVE_WINDOW_SYSTEM */
2524 static void
2525 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2527 eassert (w);
2528 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2529 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2530 w->window_end_vpos
2531 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2534 /***********************************************************************
2535 Lisp form evaluation
2536 ***********************************************************************/
2538 /* Error handler for safe_eval and safe_call. */
2540 static Lisp_Object
2541 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2543 add_to_log ("Error during redisplay: %S signaled %S",
2544 Flist (nargs, args), arg);
2545 return Qnil;
2548 /* Call function FUNC with the rest of NARGS - 1 arguments
2549 following. Return the result, or nil if something went
2550 wrong. Prevent redisplay during the evaluation. */
2552 Lisp_Object
2553 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2555 Lisp_Object val;
2557 if (inhibit_eval_during_redisplay)
2558 val = Qnil;
2559 else
2561 va_list ap;
2562 ptrdiff_t i;
2563 ptrdiff_t count = SPECPDL_INDEX ();
2564 struct gcpro gcpro1;
2565 Lisp_Object *args = alloca (nargs * word_size);
2567 args[0] = func;
2568 va_start (ap, func);
2569 for (i = 1; i < nargs; i++)
2570 args[i] = va_arg (ap, Lisp_Object);
2571 va_end (ap);
2573 GCPRO1 (args[0]);
2574 gcpro1.nvars = nargs;
2575 specbind (Qinhibit_redisplay, Qt);
2576 /* Use Qt to ensure debugger does not run,
2577 so there is no possibility of wanting to redisplay. */
2578 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2579 safe_eval_handler);
2580 UNGCPRO;
2581 val = unbind_to (count, val);
2584 return val;
2588 /* Call function FN with one argument ARG.
2589 Return the result, or nil if something went wrong. */
2591 Lisp_Object
2592 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2594 return safe_call (2, fn, arg);
2597 static Lisp_Object Qeval;
2599 Lisp_Object
2600 safe_eval (Lisp_Object sexpr)
2602 return safe_call1 (Qeval, sexpr);
2605 /* Call function FN with two arguments ARG1 and ARG2.
2606 Return the result, or nil if something went wrong. */
2608 Lisp_Object
2609 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2611 return safe_call (3, fn, arg1, arg2);
2616 /***********************************************************************
2617 Debugging
2618 ***********************************************************************/
2620 #if 0
2622 /* Define CHECK_IT to perform sanity checks on iterators.
2623 This is for debugging. It is too slow to do unconditionally. */
2625 static void
2626 check_it (struct it *it)
2628 if (it->method == GET_FROM_STRING)
2630 eassert (STRINGP (it->string));
2631 eassert (IT_STRING_CHARPOS (*it) >= 0);
2633 else
2635 eassert (IT_STRING_CHARPOS (*it) < 0);
2636 if (it->method == GET_FROM_BUFFER)
2638 /* Check that character and byte positions agree. */
2639 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2643 if (it->dpvec)
2644 eassert (it->current.dpvec_index >= 0);
2645 else
2646 eassert (it->current.dpvec_index < 0);
2649 #define CHECK_IT(IT) check_it ((IT))
2651 #else /* not 0 */
2653 #define CHECK_IT(IT) (void) 0
2655 #endif /* not 0 */
2658 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2660 /* Check that the window end of window W is what we expect it
2661 to be---the last row in the current matrix displaying text. */
2663 static void
2664 check_window_end (struct window *w)
2666 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2668 struct glyph_row *row;
2669 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2670 !row->enabled_p
2671 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2672 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2676 #define CHECK_WINDOW_END(W) check_window_end ((W))
2678 #else
2680 #define CHECK_WINDOW_END(W) (void) 0
2682 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2684 /***********************************************************************
2685 Iterator initialization
2686 ***********************************************************************/
2688 /* Initialize IT for displaying current_buffer in window W, starting
2689 at character position CHARPOS. CHARPOS < 0 means that no buffer
2690 position is specified which is useful when the iterator is assigned
2691 a position later. BYTEPOS is the byte position corresponding to
2692 CHARPOS.
2694 If ROW is not null, calls to produce_glyphs with IT as parameter
2695 will produce glyphs in that row.
2697 BASE_FACE_ID is the id of a base face to use. It must be one of
2698 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2699 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2700 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2702 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2704 will be initialized to use the corresponding mode line glyph row of
2705 the desired matrix of W. */
2707 void
2708 init_iterator (struct it *it, struct window *w,
2709 ptrdiff_t charpos, ptrdiff_t bytepos,
2710 struct glyph_row *row, enum face_id base_face_id)
2712 enum face_id remapped_base_face_id = base_face_id;
2714 /* Some precondition checks. */
2715 eassert (w != NULL && it != NULL);
2716 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2717 && charpos <= ZV));
2719 /* If face attributes have been changed since the last redisplay,
2720 free realized faces now because they depend on face definitions
2721 that might have changed. Don't free faces while there might be
2722 desired matrices pending which reference these faces. */
2723 if (face_change_count && !inhibit_free_realized_faces)
2725 face_change_count = 0;
2726 free_all_realized_faces (Qnil);
2729 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2730 if (! NILP (Vface_remapping_alist))
2731 remapped_base_face_id
2732 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2734 /* Use one of the mode line rows of W's desired matrix if
2735 appropriate. */
2736 if (row == NULL)
2738 if (base_face_id == MODE_LINE_FACE_ID
2739 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2740 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2741 else if (base_face_id == HEADER_LINE_FACE_ID)
2742 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2745 /* Clear IT. */
2746 memset (it, 0, sizeof *it);
2747 it->current.overlay_string_index = -1;
2748 it->current.dpvec_index = -1;
2749 it->base_face_id = remapped_base_face_id;
2750 it->string = Qnil;
2751 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2752 it->paragraph_embedding = L2R;
2753 it->bidi_it.string.lstring = Qnil;
2754 it->bidi_it.string.s = NULL;
2755 it->bidi_it.string.bufpos = 0;
2756 it->bidi_it.w = w;
2758 /* The window in which we iterate over current_buffer: */
2759 XSETWINDOW (it->window, w);
2760 it->w = w;
2761 it->f = XFRAME (w->frame);
2763 it->cmp_it.id = -1;
2765 /* Extra space between lines (on window systems only). */
2766 if (base_face_id == DEFAULT_FACE_ID
2767 && FRAME_WINDOW_P (it->f))
2769 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2770 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2771 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2772 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2773 * FRAME_LINE_HEIGHT (it->f));
2774 else if (it->f->extra_line_spacing > 0)
2775 it->extra_line_spacing = it->f->extra_line_spacing;
2776 it->max_extra_line_spacing = 0;
2779 /* If realized faces have been removed, e.g. because of face
2780 attribute changes of named faces, recompute them. When running
2781 in batch mode, the face cache of the initial frame is null. If
2782 we happen to get called, make a dummy face cache. */
2783 if (FRAME_FACE_CACHE (it->f) == NULL)
2784 init_frame_faces (it->f);
2785 if (FRAME_FACE_CACHE (it->f)->used == 0)
2786 recompute_basic_faces (it->f);
2788 /* Current value of the `slice', `space-width', and 'height' properties. */
2789 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2790 it->space_width = Qnil;
2791 it->font_height = Qnil;
2792 it->override_ascent = -1;
2794 /* Are control characters displayed as `^C'? */
2795 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2797 /* -1 means everything between a CR and the following line end
2798 is invisible. >0 means lines indented more than this value are
2799 invisible. */
2800 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2801 ? (clip_to_bounds
2802 (-1, XINT (BVAR (current_buffer, selective_display)),
2803 PTRDIFF_MAX))
2804 : (!NILP (BVAR (current_buffer, selective_display))
2805 ? -1 : 0));
2806 it->selective_display_ellipsis_p
2807 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2809 /* Display table to use. */
2810 it->dp = window_display_table (w);
2812 /* Are multibyte characters enabled in current_buffer? */
2813 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2815 /* Get the position at which the redisplay_end_trigger hook should
2816 be run, if it is to be run at all. */
2817 if (MARKERP (w->redisplay_end_trigger)
2818 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2819 it->redisplay_end_trigger_charpos
2820 = marker_position (w->redisplay_end_trigger);
2821 else if (INTEGERP (w->redisplay_end_trigger))
2822 it->redisplay_end_trigger_charpos
2823 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2824 PTRDIFF_MAX);
2826 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2828 /* Are lines in the display truncated? */
2829 if (base_face_id != DEFAULT_FACE_ID
2830 || it->w->hscroll
2831 || (! WINDOW_FULL_WIDTH_P (it->w)
2832 && ((!NILP (Vtruncate_partial_width_windows)
2833 && !INTEGERP (Vtruncate_partial_width_windows))
2834 || (INTEGERP (Vtruncate_partial_width_windows)
2835 /* PXW: Shall we do something about this? */
2836 && (WINDOW_TOTAL_COLS (it->w)
2837 < XINT (Vtruncate_partial_width_windows))))))
2838 it->line_wrap = TRUNCATE;
2839 else if (NILP (BVAR (current_buffer, truncate_lines)))
2840 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2841 ? WINDOW_WRAP : WORD_WRAP;
2842 else
2843 it->line_wrap = TRUNCATE;
2845 /* Get dimensions of truncation and continuation glyphs. These are
2846 displayed as fringe bitmaps under X, but we need them for such
2847 frames when the fringes are turned off. But leave the dimensions
2848 zero for tooltip frames, as these glyphs look ugly there and also
2849 sabotage calculations of tooltip dimensions in x-show-tip. */
2850 #ifdef HAVE_WINDOW_SYSTEM
2851 if (!(FRAME_WINDOW_P (it->f)
2852 && FRAMEP (tip_frame)
2853 && it->f == XFRAME (tip_frame)))
2854 #endif
2856 if (it->line_wrap == TRUNCATE)
2858 /* We will need the truncation glyph. */
2859 eassert (it->glyph_row == NULL);
2860 produce_special_glyphs (it, IT_TRUNCATION);
2861 it->truncation_pixel_width = it->pixel_width;
2863 else
2865 /* We will need the continuation glyph. */
2866 eassert (it->glyph_row == NULL);
2867 produce_special_glyphs (it, IT_CONTINUATION);
2868 it->continuation_pixel_width = it->pixel_width;
2872 /* Reset these values to zero because the produce_special_glyphs
2873 above has changed them. */
2874 it->pixel_width = it->ascent = it->descent = 0;
2875 it->phys_ascent = it->phys_descent = 0;
2877 /* Set this after getting the dimensions of truncation and
2878 continuation glyphs, so that we don't produce glyphs when calling
2879 produce_special_glyphs, above. */
2880 it->glyph_row = row;
2881 it->area = TEXT_AREA;
2883 /* Forget any previous info about this row being reversed. */
2884 if (it->glyph_row)
2885 it->glyph_row->reversed_p = 0;
2887 /* Get the dimensions of the display area. The display area
2888 consists of the visible window area plus a horizontally scrolled
2889 part to the left of the window. All x-values are relative to the
2890 start of this total display area. */
2891 if (base_face_id != DEFAULT_FACE_ID)
2893 /* Mode lines, menu bar in terminal frames. */
2894 it->first_visible_x = 0;
2895 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2897 else
2899 it->first_visible_x
2900 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2901 it->last_visible_x = (it->first_visible_x
2902 + window_box_width (w, TEXT_AREA));
2904 /* If we truncate lines, leave room for the truncation glyph(s) at
2905 the right margin. Otherwise, leave room for the continuation
2906 glyph(s). Done only if the window has no fringes. Since we
2907 don't know at this point whether there will be any R2L lines in
2908 the window, we reserve space for truncation/continuation glyphs
2909 even if only one of the fringes is absent. */
2910 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2911 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2913 if (it->line_wrap == TRUNCATE)
2914 it->last_visible_x -= it->truncation_pixel_width;
2915 else
2916 it->last_visible_x -= it->continuation_pixel_width;
2919 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2920 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2923 /* Leave room for a border glyph. */
2924 if (!FRAME_WINDOW_P (it->f)
2925 && !WINDOW_RIGHTMOST_P (it->w))
2926 it->last_visible_x -= 1;
2928 it->last_visible_y = window_text_bottom_y (w);
2930 /* For mode lines and alike, arrange for the first glyph having a
2931 left box line if the face specifies a box. */
2932 if (base_face_id != DEFAULT_FACE_ID)
2934 struct face *face;
2936 it->face_id = remapped_base_face_id;
2938 /* If we have a boxed mode line, make the first character appear
2939 with a left box line. */
2940 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2941 if (face && face->box != FACE_NO_BOX)
2942 it->start_of_box_run_p = true;
2945 /* If a buffer position was specified, set the iterator there,
2946 getting overlays and face properties from that position. */
2947 if (charpos >= BUF_BEG (current_buffer))
2949 it->end_charpos = ZV;
2950 eassert (charpos == BYTE_TO_CHAR (bytepos));
2951 IT_CHARPOS (*it) = charpos;
2952 IT_BYTEPOS (*it) = bytepos;
2954 /* We will rely on `reseat' to set this up properly, via
2955 handle_face_prop. */
2956 it->face_id = it->base_face_id;
2958 it->start = it->current;
2959 /* Do we need to reorder bidirectional text? Not if this is a
2960 unibyte buffer: by definition, none of the single-byte
2961 characters are strong R2L, so no reordering is needed. And
2962 bidi.c doesn't support unibyte buffers anyway. Also, don't
2963 reorder while we are loading loadup.el, since the tables of
2964 character properties needed for reordering are not yet
2965 available. */
2966 it->bidi_p =
2967 NILP (Vpurify_flag)
2968 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2969 && it->multibyte_p;
2971 /* If we are to reorder bidirectional text, init the bidi
2972 iterator. */
2973 if (it->bidi_p)
2975 /* Note the paragraph direction that this buffer wants to
2976 use. */
2977 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2978 Qleft_to_right))
2979 it->paragraph_embedding = L2R;
2980 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2981 Qright_to_left))
2982 it->paragraph_embedding = R2L;
2983 else
2984 it->paragraph_embedding = NEUTRAL_DIR;
2985 bidi_unshelve_cache (NULL, 0);
2986 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2987 &it->bidi_it);
2990 /* Compute faces etc. */
2991 reseat (it, it->current.pos, 1);
2994 CHECK_IT (it);
2998 /* Initialize IT for the display of window W with window start POS. */
3000 void
3001 start_display (struct it *it, struct window *w, struct text_pos pos)
3003 struct glyph_row *row;
3004 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3006 row = w->desired_matrix->rows + first_vpos;
3007 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3008 it->first_vpos = first_vpos;
3010 /* Don't reseat to previous visible line start if current start
3011 position is in a string or image. */
3012 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3014 int start_at_line_beg_p;
3015 int first_y = it->current_y;
3017 /* If window start is not at a line start, skip forward to POS to
3018 get the correct continuation lines width. */
3019 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3020 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3021 if (!start_at_line_beg_p)
3023 int new_x;
3025 reseat_at_previous_visible_line_start (it);
3026 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3028 new_x = it->current_x + it->pixel_width;
3030 /* If lines are continued, this line may end in the middle
3031 of a multi-glyph character (e.g. a control character
3032 displayed as \003, or in the middle of an overlay
3033 string). In this case move_it_to above will not have
3034 taken us to the start of the continuation line but to the
3035 end of the continued line. */
3036 if (it->current_x > 0
3037 && it->line_wrap != TRUNCATE /* Lines are continued. */
3038 && (/* And glyph doesn't fit on the line. */
3039 new_x > it->last_visible_x
3040 /* Or it fits exactly and we're on a window
3041 system frame. */
3042 || (new_x == it->last_visible_x
3043 && FRAME_WINDOW_P (it->f)
3044 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3045 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3046 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3048 if ((it->current.dpvec_index >= 0
3049 || it->current.overlay_string_index >= 0)
3050 /* If we are on a newline from a display vector or
3051 overlay string, then we are already at the end of
3052 a screen line; no need to go to the next line in
3053 that case, as this line is not really continued.
3054 (If we do go to the next line, C-e will not DTRT.) */
3055 && it->c != '\n')
3057 set_iterator_to_next (it, 1);
3058 move_it_in_display_line_to (it, -1, -1, 0);
3061 it->continuation_lines_width += it->current_x;
3063 /* If the character at POS is displayed via a display
3064 vector, move_it_to above stops at the final glyph of
3065 IT->dpvec. To make the caller redisplay that character
3066 again (a.k.a. start at POS), we need to reset the
3067 dpvec_index to the beginning of IT->dpvec. */
3068 else if (it->current.dpvec_index >= 0)
3069 it->current.dpvec_index = 0;
3071 /* We're starting a new display line, not affected by the
3072 height of the continued line, so clear the appropriate
3073 fields in the iterator structure. */
3074 it->max_ascent = it->max_descent = 0;
3075 it->max_phys_ascent = it->max_phys_descent = 0;
3077 it->current_y = first_y;
3078 it->vpos = 0;
3079 it->current_x = it->hpos = 0;
3085 /* Return 1 if POS is a position in ellipses displayed for invisible
3086 text. W is the window we display, for text property lookup. */
3088 static int
3089 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3091 Lisp_Object prop, window;
3092 int ellipses_p = 0;
3093 ptrdiff_t charpos = CHARPOS (pos->pos);
3095 /* If POS specifies a position in a display vector, this might
3096 be for an ellipsis displayed for invisible text. We won't
3097 get the iterator set up for delivering that ellipsis unless
3098 we make sure that it gets aware of the invisible text. */
3099 if (pos->dpvec_index >= 0
3100 && pos->overlay_string_index < 0
3101 && CHARPOS (pos->string_pos) < 0
3102 && charpos > BEGV
3103 && (XSETWINDOW (window, w),
3104 prop = Fget_char_property (make_number (charpos),
3105 Qinvisible, window),
3106 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3108 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3109 window);
3110 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3113 return ellipses_p;
3117 /* Initialize IT for stepping through current_buffer in window W,
3118 starting at position POS that includes overlay string and display
3119 vector/ control character translation position information. Value
3120 is zero if there are overlay strings with newlines at POS. */
3122 static int
3123 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3125 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3126 int i, overlay_strings_with_newlines = 0;
3128 /* If POS specifies a position in a display vector, this might
3129 be for an ellipsis displayed for invisible text. We won't
3130 get the iterator set up for delivering that ellipsis unless
3131 we make sure that it gets aware of the invisible text. */
3132 if (in_ellipses_for_invisible_text_p (pos, w))
3134 --charpos;
3135 bytepos = 0;
3138 /* Keep in mind: the call to reseat in init_iterator skips invisible
3139 text, so we might end up at a position different from POS. This
3140 is only a problem when POS is a row start after a newline and an
3141 overlay starts there with an after-string, and the overlay has an
3142 invisible property. Since we don't skip invisible text in
3143 display_line and elsewhere immediately after consuming the
3144 newline before the row start, such a POS will not be in a string,
3145 but the call to init_iterator below will move us to the
3146 after-string. */
3147 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3149 /* This only scans the current chunk -- it should scan all chunks.
3150 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3151 to 16 in 22.1 to make this a lesser problem. */
3152 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3154 const char *s = SSDATA (it->overlay_strings[i]);
3155 const char *e = s + SBYTES (it->overlay_strings[i]);
3157 while (s < e && *s != '\n')
3158 ++s;
3160 if (s < e)
3162 overlay_strings_with_newlines = 1;
3163 break;
3167 /* If position is within an overlay string, set up IT to the right
3168 overlay string. */
3169 if (pos->overlay_string_index >= 0)
3171 int relative_index;
3173 /* If the first overlay string happens to have a `display'
3174 property for an image, the iterator will be set up for that
3175 image, and we have to undo that setup first before we can
3176 correct the overlay string index. */
3177 if (it->method == GET_FROM_IMAGE)
3178 pop_it (it);
3180 /* We already have the first chunk of overlay strings in
3181 IT->overlay_strings. Load more until the one for
3182 pos->overlay_string_index is in IT->overlay_strings. */
3183 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3185 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3186 it->current.overlay_string_index = 0;
3187 while (n--)
3189 load_overlay_strings (it, 0);
3190 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3194 it->current.overlay_string_index = pos->overlay_string_index;
3195 relative_index = (it->current.overlay_string_index
3196 % OVERLAY_STRING_CHUNK_SIZE);
3197 it->string = it->overlay_strings[relative_index];
3198 eassert (STRINGP (it->string));
3199 it->current.string_pos = pos->string_pos;
3200 it->method = GET_FROM_STRING;
3201 it->end_charpos = SCHARS (it->string);
3202 /* Set up the bidi iterator for this overlay string. */
3203 if (it->bidi_p)
3205 it->bidi_it.string.lstring = it->string;
3206 it->bidi_it.string.s = NULL;
3207 it->bidi_it.string.schars = SCHARS (it->string);
3208 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3209 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3210 it->bidi_it.string.unibyte = !it->multibyte_p;
3211 it->bidi_it.w = it->w;
3212 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3213 FRAME_WINDOW_P (it->f), &it->bidi_it);
3215 /* Synchronize the state of the bidi iterator with
3216 pos->string_pos. For any string position other than
3217 zero, this will be done automagically when we resume
3218 iteration over the string and get_visually_first_element
3219 is called. But if string_pos is zero, and the string is
3220 to be reordered for display, we need to resync manually,
3221 since it could be that the iteration state recorded in
3222 pos ended at string_pos of 0 moving backwards in string. */
3223 if (CHARPOS (pos->string_pos) == 0)
3225 get_visually_first_element (it);
3226 if (IT_STRING_CHARPOS (*it) != 0)
3227 do {
3228 /* Paranoia. */
3229 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3230 bidi_move_to_visually_next (&it->bidi_it);
3231 } while (it->bidi_it.charpos != 0);
3233 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3234 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3238 if (CHARPOS (pos->string_pos) >= 0)
3240 /* Recorded position is not in an overlay string, but in another
3241 string. This can only be a string from a `display' property.
3242 IT should already be filled with that string. */
3243 it->current.string_pos = pos->string_pos;
3244 eassert (STRINGP (it->string));
3245 if (it->bidi_p)
3246 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3247 FRAME_WINDOW_P (it->f), &it->bidi_it);
3250 /* Restore position in display vector translations, control
3251 character translations or ellipses. */
3252 if (pos->dpvec_index >= 0)
3254 if (it->dpvec == NULL)
3255 get_next_display_element (it);
3256 eassert (it->dpvec && it->current.dpvec_index == 0);
3257 it->current.dpvec_index = pos->dpvec_index;
3260 CHECK_IT (it);
3261 return !overlay_strings_with_newlines;
3265 /* Initialize IT for stepping through current_buffer in window W
3266 starting at ROW->start. */
3268 static void
3269 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3271 init_from_display_pos (it, w, &row->start);
3272 it->start = row->start;
3273 it->continuation_lines_width = row->continuation_lines_width;
3274 CHECK_IT (it);
3278 /* Initialize IT for stepping through current_buffer in window W
3279 starting in the line following ROW, i.e. starting at ROW->end.
3280 Value is zero if there are overlay strings with newlines at ROW's
3281 end position. */
3283 static int
3284 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3286 int success = 0;
3288 if (init_from_display_pos (it, w, &row->end))
3290 if (row->continued_p)
3291 it->continuation_lines_width
3292 = row->continuation_lines_width + row->pixel_width;
3293 CHECK_IT (it);
3294 success = 1;
3297 return success;
3303 /***********************************************************************
3304 Text properties
3305 ***********************************************************************/
3307 /* Called when IT reaches IT->stop_charpos. Handle text property and
3308 overlay changes. Set IT->stop_charpos to the next position where
3309 to stop. */
3311 static void
3312 handle_stop (struct it *it)
3314 enum prop_handled handled;
3315 int handle_overlay_change_p;
3316 struct props *p;
3318 it->dpvec = NULL;
3319 it->current.dpvec_index = -1;
3320 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3321 it->ignore_overlay_strings_at_pos_p = 0;
3322 it->ellipsis_p = 0;
3324 /* Use face of preceding text for ellipsis (if invisible) */
3325 if (it->selective_display_ellipsis_p)
3326 it->saved_face_id = it->face_id;
3330 handled = HANDLED_NORMALLY;
3332 /* Call text property handlers. */
3333 for (p = it_props; p->handler; ++p)
3335 handled = p->handler (it);
3337 if (handled == HANDLED_RECOMPUTE_PROPS)
3338 break;
3339 else if (handled == HANDLED_RETURN)
3341 /* We still want to show before and after strings from
3342 overlays even if the actual buffer text is replaced. */
3343 if (!handle_overlay_change_p
3344 || it->sp > 1
3345 /* Don't call get_overlay_strings_1 if we already
3346 have overlay strings loaded, because doing so
3347 will load them again and push the iterator state
3348 onto the stack one more time, which is not
3349 expected by the rest of the code that processes
3350 overlay strings. */
3351 || (it->current.overlay_string_index < 0
3352 ? !get_overlay_strings_1 (it, 0, 0)
3353 : 0))
3355 if (it->ellipsis_p)
3356 setup_for_ellipsis (it, 0);
3357 /* When handling a display spec, we might load an
3358 empty string. In that case, discard it here. We
3359 used to discard it in handle_single_display_spec,
3360 but that causes get_overlay_strings_1, above, to
3361 ignore overlay strings that we must check. */
3362 if (STRINGP (it->string) && !SCHARS (it->string))
3363 pop_it (it);
3364 return;
3366 else if (STRINGP (it->string) && !SCHARS (it->string))
3367 pop_it (it);
3368 else
3370 it->ignore_overlay_strings_at_pos_p = true;
3371 it->string_from_display_prop_p = 0;
3372 it->from_disp_prop_p = 0;
3373 handle_overlay_change_p = 0;
3375 handled = HANDLED_RECOMPUTE_PROPS;
3376 break;
3378 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3379 handle_overlay_change_p = 0;
3382 if (handled != HANDLED_RECOMPUTE_PROPS)
3384 /* Don't check for overlay strings below when set to deliver
3385 characters from a display vector. */
3386 if (it->method == GET_FROM_DISPLAY_VECTOR)
3387 handle_overlay_change_p = 0;
3389 /* Handle overlay changes.
3390 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3391 if it finds overlays. */
3392 if (handle_overlay_change_p)
3393 handled = handle_overlay_change (it);
3396 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 break;
3402 while (handled == HANDLED_RECOMPUTE_PROPS);
3404 /* Determine where to stop next. */
3405 if (handled == HANDLED_NORMALLY)
3406 compute_stop_pos (it);
3410 /* Compute IT->stop_charpos from text property and overlay change
3411 information for IT's current position. */
3413 static void
3414 compute_stop_pos (struct it *it)
3416 register INTERVAL iv, next_iv;
3417 Lisp_Object object, limit, position;
3418 ptrdiff_t charpos, bytepos;
3420 if (STRINGP (it->string))
3422 /* Strings are usually short, so don't limit the search for
3423 properties. */
3424 it->stop_charpos = it->end_charpos;
3425 object = it->string;
3426 limit = Qnil;
3427 charpos = IT_STRING_CHARPOS (*it);
3428 bytepos = IT_STRING_BYTEPOS (*it);
3430 else
3432 ptrdiff_t pos;
3434 /* If end_charpos is out of range for some reason, such as a
3435 misbehaving display function, rationalize it (Bug#5984). */
3436 if (it->end_charpos > ZV)
3437 it->end_charpos = ZV;
3438 it->stop_charpos = it->end_charpos;
3440 /* If next overlay change is in front of the current stop pos
3441 (which is IT->end_charpos), stop there. Note: value of
3442 next_overlay_change is point-max if no overlay change
3443 follows. */
3444 charpos = IT_CHARPOS (*it);
3445 bytepos = IT_BYTEPOS (*it);
3446 pos = next_overlay_change (charpos);
3447 if (pos < it->stop_charpos)
3448 it->stop_charpos = pos;
3450 /* Set up variables for computing the stop position from text
3451 property changes. */
3452 XSETBUFFER (object, current_buffer);
3453 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3456 /* Get the interval containing IT's position. Value is a null
3457 interval if there isn't such an interval. */
3458 position = make_number (charpos);
3459 iv = validate_interval_range (object, &position, &position, 0);
3460 if (iv)
3462 Lisp_Object values_here[LAST_PROP_IDX];
3463 struct props *p;
3465 /* Get properties here. */
3466 for (p = it_props; p->handler; ++p)
3467 values_here[p->idx] = textget (iv->plist, *p->name);
3469 /* Look for an interval following iv that has different
3470 properties. */
3471 for (next_iv = next_interval (iv);
3472 (next_iv
3473 && (NILP (limit)
3474 || XFASTINT (limit) > next_iv->position));
3475 next_iv = next_interval (next_iv))
3477 for (p = it_props; p->handler; ++p)
3479 Lisp_Object new_value;
3481 new_value = textget (next_iv->plist, *p->name);
3482 if (!EQ (values_here[p->idx], new_value))
3483 break;
3486 if (p->handler)
3487 break;
3490 if (next_iv)
3492 if (INTEGERP (limit)
3493 && next_iv->position >= XFASTINT (limit))
3494 /* No text property change up to limit. */
3495 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3496 else
3497 /* Text properties change in next_iv. */
3498 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3502 if (it->cmp_it.id < 0)
3504 ptrdiff_t stoppos = it->end_charpos;
3506 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3507 stoppos = -1;
3508 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3509 stoppos, it->string);
3512 eassert (STRINGP (it->string)
3513 || (it->stop_charpos >= BEGV
3514 && it->stop_charpos >= IT_CHARPOS (*it)));
3518 /* Return the position of the next overlay change after POS in
3519 current_buffer. Value is point-max if no overlay change
3520 follows. This is like `next-overlay-change' but doesn't use
3521 xmalloc. */
3523 static ptrdiff_t
3524 next_overlay_change (ptrdiff_t pos)
3526 ptrdiff_t i, noverlays;
3527 ptrdiff_t endpos;
3528 Lisp_Object *overlays;
3530 /* Get all overlays at the given position. */
3531 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3533 /* If any of these overlays ends before endpos,
3534 use its ending point instead. */
3535 for (i = 0; i < noverlays; ++i)
3537 Lisp_Object oend;
3538 ptrdiff_t oendpos;
3540 oend = OVERLAY_END (overlays[i]);
3541 oendpos = OVERLAY_POSITION (oend);
3542 endpos = min (endpos, oendpos);
3545 return endpos;
3548 /* How many characters forward to search for a display property or
3549 display string. Searching too far forward makes the bidi display
3550 sluggish, especially in small windows. */
3551 #define MAX_DISP_SCAN 250
3553 /* Return the character position of a display string at or after
3554 position specified by POSITION. If no display string exists at or
3555 after POSITION, return ZV. A display string is either an overlay
3556 with `display' property whose value is a string, or a `display'
3557 text property whose value is a string. STRING is data about the
3558 string to iterate; if STRING->lstring is nil, we are iterating a
3559 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3560 on a GUI frame. DISP_PROP is set to zero if we searched
3561 MAX_DISP_SCAN characters forward without finding any display
3562 strings, non-zero otherwise. It is set to 2 if the display string
3563 uses any kind of `(space ...)' spec that will produce a stretch of
3564 white space in the text area. */
3565 ptrdiff_t
3566 compute_display_string_pos (struct text_pos *position,
3567 struct bidi_string_data *string,
3568 struct window *w,
3569 int frame_window_p, int *disp_prop)
3571 /* OBJECT = nil means current buffer. */
3572 Lisp_Object object, object1;
3573 Lisp_Object pos, spec, limpos;
3574 int string_p = (string && (STRINGP (string->lstring) || string->s));
3575 ptrdiff_t eob = string_p ? string->schars : ZV;
3576 ptrdiff_t begb = string_p ? 0 : BEGV;
3577 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3578 ptrdiff_t lim =
3579 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3580 struct text_pos tpos;
3581 int rv = 0;
3583 if (string && STRINGP (string->lstring))
3584 object1 = object = string->lstring;
3585 else if (w && !string_p)
3587 XSETWINDOW (object, w);
3588 object1 = Qnil;
3590 else
3591 object1 = object = Qnil;
3593 *disp_prop = 1;
3595 if (charpos >= eob
3596 /* We don't support display properties whose values are strings
3597 that have display string properties. */
3598 || string->from_disp_str
3599 /* C strings cannot have display properties. */
3600 || (string->s && !STRINGP (object)))
3602 *disp_prop = 0;
3603 return eob;
3606 /* If the character at CHARPOS is where the display string begins,
3607 return CHARPOS. */
3608 pos = make_number (charpos);
3609 if (STRINGP (object))
3610 bufpos = string->bufpos;
3611 else
3612 bufpos = charpos;
3613 tpos = *position;
3614 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3615 && (charpos <= begb
3616 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3617 object),
3618 spec))
3619 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3620 frame_window_p)))
3622 if (rv == 2)
3623 *disp_prop = 2;
3624 return charpos;
3627 /* Look forward for the first character with a `display' property
3628 that will replace the underlying text when displayed. */
3629 limpos = make_number (lim);
3630 do {
3631 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3632 CHARPOS (tpos) = XFASTINT (pos);
3633 if (CHARPOS (tpos) >= lim)
3635 *disp_prop = 0;
3636 break;
3638 if (STRINGP (object))
3639 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3640 else
3641 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3642 spec = Fget_char_property (pos, Qdisplay, object);
3643 if (!STRINGP (object))
3644 bufpos = CHARPOS (tpos);
3645 } while (NILP (spec)
3646 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3647 bufpos, frame_window_p)));
3648 if (rv == 2)
3649 *disp_prop = 2;
3651 return CHARPOS (tpos);
3654 /* Return the character position of the end of the display string that
3655 started at CHARPOS. If there's no display string at CHARPOS,
3656 return -1. A display string is either an overlay with `display'
3657 property whose value is a string or a `display' text property whose
3658 value is a string. */
3659 ptrdiff_t
3660 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3662 /* OBJECT = nil means current buffer. */
3663 Lisp_Object object =
3664 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3665 Lisp_Object pos = make_number (charpos);
3666 ptrdiff_t eob =
3667 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3669 if (charpos >= eob || (string->s && !STRINGP (object)))
3670 return eob;
3672 /* It could happen that the display property or overlay was removed
3673 since we found it in compute_display_string_pos above. One way
3674 this can happen is if JIT font-lock was called (through
3675 handle_fontified_prop), and jit-lock-functions remove text
3676 properties or overlays from the portion of buffer that includes
3677 CHARPOS. Muse mode is known to do that, for example. In this
3678 case, we return -1 to the caller, to signal that no display
3679 string is actually present at CHARPOS. See bidi_fetch_char for
3680 how this is handled.
3682 An alternative would be to never look for display properties past
3683 it->stop_charpos. But neither compute_display_string_pos nor
3684 bidi_fetch_char that calls it know or care where the next
3685 stop_charpos is. */
3686 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3687 return -1;
3689 /* Look forward for the first character where the `display' property
3690 changes. */
3691 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3693 return XFASTINT (pos);
3698 /***********************************************************************
3699 Fontification
3700 ***********************************************************************/
3702 /* Handle changes in the `fontified' property of the current buffer by
3703 calling hook functions from Qfontification_functions to fontify
3704 regions of text. */
3706 static enum prop_handled
3707 handle_fontified_prop (struct it *it)
3709 Lisp_Object prop, pos;
3710 enum prop_handled handled = HANDLED_NORMALLY;
3712 if (!NILP (Vmemory_full))
3713 return handled;
3715 /* Get the value of the `fontified' property at IT's current buffer
3716 position. (The `fontified' property doesn't have a special
3717 meaning in strings.) If the value is nil, call functions from
3718 Qfontification_functions. */
3719 if (!STRINGP (it->string)
3720 && it->s == NULL
3721 && !NILP (Vfontification_functions)
3722 && !NILP (Vrun_hooks)
3723 && (pos = make_number (IT_CHARPOS (*it)),
3724 prop = Fget_char_property (pos, Qfontified, Qnil),
3725 /* Ignore the special cased nil value always present at EOB since
3726 no amount of fontifying will be able to change it. */
3727 NILP (prop) && IT_CHARPOS (*it) < Z))
3729 ptrdiff_t count = SPECPDL_INDEX ();
3730 Lisp_Object val;
3731 struct buffer *obuf = current_buffer;
3732 ptrdiff_t begv = BEGV, zv = ZV;
3733 bool old_clip_changed = current_buffer->clip_changed;
3735 val = Vfontification_functions;
3736 specbind (Qfontification_functions, Qnil);
3738 eassert (it->end_charpos == ZV);
3740 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3741 safe_call1 (val, pos);
3742 else
3744 Lisp_Object fns, fn;
3745 struct gcpro gcpro1, gcpro2;
3747 fns = Qnil;
3748 GCPRO2 (val, fns);
3750 for (; CONSP (val); val = XCDR (val))
3752 fn = XCAR (val);
3754 if (EQ (fn, Qt))
3756 /* A value of t indicates this hook has a local
3757 binding; it means to run the global binding too.
3758 In a global value, t should not occur. If it
3759 does, we must ignore it to avoid an endless
3760 loop. */
3761 for (fns = Fdefault_value (Qfontification_functions);
3762 CONSP (fns);
3763 fns = XCDR (fns))
3765 fn = XCAR (fns);
3766 if (!EQ (fn, Qt))
3767 safe_call1 (fn, pos);
3770 else
3771 safe_call1 (fn, pos);
3774 UNGCPRO;
3777 unbind_to (count, Qnil);
3779 /* Fontification functions routinely call `save-restriction'.
3780 Normally, this tags clip_changed, which can confuse redisplay
3781 (see discussion in Bug#6671). Since we don't perform any
3782 special handling of fontification changes in the case where
3783 `save-restriction' isn't called, there's no point doing so in
3784 this case either. So, if the buffer's restrictions are
3785 actually left unchanged, reset clip_changed. */
3786 if (obuf == current_buffer)
3788 if (begv == BEGV && zv == ZV)
3789 current_buffer->clip_changed = old_clip_changed;
3791 /* There isn't much we can reasonably do to protect against
3792 misbehaving fontification, but here's a fig leaf. */
3793 else if (BUFFER_LIVE_P (obuf))
3794 set_buffer_internal_1 (obuf);
3796 /* The fontification code may have added/removed text.
3797 It could do even a lot worse, but let's at least protect against
3798 the most obvious case where only the text past `pos' gets changed',
3799 as is/was done in grep.el where some escapes sequences are turned
3800 into face properties (bug#7876). */
3801 it->end_charpos = ZV;
3803 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3804 something. This avoids an endless loop if they failed to
3805 fontify the text for which reason ever. */
3806 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3807 handled = HANDLED_RECOMPUTE_PROPS;
3810 return handled;
3815 /***********************************************************************
3816 Faces
3817 ***********************************************************************/
3819 /* Set up iterator IT from face properties at its current position.
3820 Called from handle_stop. */
3822 static enum prop_handled
3823 handle_face_prop (struct it *it)
3825 int new_face_id;
3826 ptrdiff_t next_stop;
3828 if (!STRINGP (it->string))
3830 new_face_id
3831 = face_at_buffer_position (it->w,
3832 IT_CHARPOS (*it),
3833 &next_stop,
3834 (IT_CHARPOS (*it)
3835 + TEXT_PROP_DISTANCE_LIMIT),
3836 0, it->base_face_id);
3838 /* Is this a start of a run of characters with box face?
3839 Caveat: this can be called for a freshly initialized
3840 iterator; face_id is -1 in this case. We know that the new
3841 face will not change until limit, i.e. if the new face has a
3842 box, all characters up to limit will have one. But, as
3843 usual, we don't know whether limit is really the end. */
3844 if (new_face_id != it->face_id)
3846 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3847 /* If it->face_id is -1, old_face below will be NULL, see
3848 the definition of FACE_FROM_ID. This will happen if this
3849 is the initial call that gets the face. */
3850 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3852 /* If the value of face_id of the iterator is -1, we have to
3853 look in front of IT's position and see whether there is a
3854 face there that's different from new_face_id. */
3855 if (!old_face && IT_CHARPOS (*it) > BEG)
3857 int prev_face_id = face_before_it_pos (it);
3859 old_face = FACE_FROM_ID (it->f, prev_face_id);
3862 /* If the new face has a box, but the old face does not,
3863 this is the start of a run of characters with box face,
3864 i.e. this character has a shadow on the left side. */
3865 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3866 && (old_face == NULL || !old_face->box));
3867 it->face_box_p = new_face->box != FACE_NO_BOX;
3870 else
3872 int base_face_id;
3873 ptrdiff_t bufpos;
3874 int i;
3875 Lisp_Object from_overlay
3876 = (it->current.overlay_string_index >= 0
3877 ? it->string_overlays[it->current.overlay_string_index
3878 % OVERLAY_STRING_CHUNK_SIZE]
3879 : Qnil);
3881 /* See if we got to this string directly or indirectly from
3882 an overlay property. That includes the before-string or
3883 after-string of an overlay, strings in display properties
3884 provided by an overlay, their text properties, etc.
3886 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3887 if (! NILP (from_overlay))
3888 for (i = it->sp - 1; i >= 0; i--)
3890 if (it->stack[i].current.overlay_string_index >= 0)
3891 from_overlay
3892 = it->string_overlays[it->stack[i].current.overlay_string_index
3893 % OVERLAY_STRING_CHUNK_SIZE];
3894 else if (! NILP (it->stack[i].from_overlay))
3895 from_overlay = it->stack[i].from_overlay;
3897 if (!NILP (from_overlay))
3898 break;
3901 if (! NILP (from_overlay))
3903 bufpos = IT_CHARPOS (*it);
3904 /* For a string from an overlay, the base face depends
3905 only on text properties and ignores overlays. */
3906 base_face_id
3907 = face_for_overlay_string (it->w,
3908 IT_CHARPOS (*it),
3909 &next_stop,
3910 (IT_CHARPOS (*it)
3911 + TEXT_PROP_DISTANCE_LIMIT),
3913 from_overlay);
3915 else
3917 bufpos = 0;
3919 /* For strings from a `display' property, use the face at
3920 IT's current buffer position as the base face to merge
3921 with, so that overlay strings appear in the same face as
3922 surrounding text, unless they specify their own faces.
3923 For strings from wrap-prefix and line-prefix properties,
3924 use the default face, possibly remapped via
3925 Vface_remapping_alist. */
3926 base_face_id = it->string_from_prefix_prop_p
3927 ? (!NILP (Vface_remapping_alist)
3928 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3929 : DEFAULT_FACE_ID)
3930 : underlying_face_id (it);
3933 new_face_id = face_at_string_position (it->w,
3934 it->string,
3935 IT_STRING_CHARPOS (*it),
3936 bufpos,
3937 &next_stop,
3938 base_face_id, 0);
3940 /* Is this a start of a run of characters with box? Caveat:
3941 this can be called for a freshly allocated iterator; face_id
3942 is -1 is this case. We know that the new face will not
3943 change until the next check pos, i.e. if the new face has a
3944 box, all characters up to that position will have a
3945 box. But, as usual, we don't know whether that position
3946 is really the end. */
3947 if (new_face_id != it->face_id)
3949 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3950 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3952 /* If new face has a box but old face hasn't, this is the
3953 start of a run of characters with box, i.e. it has a
3954 shadow on the left side. */
3955 it->start_of_box_run_p
3956 = new_face->box && (old_face == NULL || !old_face->box);
3957 it->face_box_p = new_face->box != FACE_NO_BOX;
3961 it->face_id = new_face_id;
3962 return HANDLED_NORMALLY;
3966 /* Return the ID of the face ``underlying'' IT's current position,
3967 which is in a string. If the iterator is associated with a
3968 buffer, return the face at IT's current buffer position.
3969 Otherwise, use the iterator's base_face_id. */
3971 static int
3972 underlying_face_id (struct it *it)
3974 int face_id = it->base_face_id, i;
3976 eassert (STRINGP (it->string));
3978 for (i = it->sp - 1; i >= 0; --i)
3979 if (NILP (it->stack[i].string))
3980 face_id = it->stack[i].face_id;
3982 return face_id;
3986 /* Compute the face one character before or after the current position
3987 of IT, in the visual order. BEFORE_P non-zero means get the face
3988 in front (to the left in L2R paragraphs, to the right in R2L
3989 paragraphs) of IT's screen position. Value is the ID of the face. */
3991 static int
3992 face_before_or_after_it_pos (struct it *it, int before_p)
3994 int face_id, limit;
3995 ptrdiff_t next_check_charpos;
3996 struct it it_copy;
3997 void *it_copy_data = NULL;
3999 eassert (it->s == NULL);
4001 if (STRINGP (it->string))
4003 ptrdiff_t bufpos, charpos;
4004 int base_face_id;
4006 /* No face change past the end of the string (for the case
4007 we are padding with spaces). No face change before the
4008 string start. */
4009 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4010 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4011 return it->face_id;
4013 if (!it->bidi_p)
4015 /* Set charpos to the position before or after IT's current
4016 position, in the logical order, which in the non-bidi
4017 case is the same as the visual order. */
4018 if (before_p)
4019 charpos = IT_STRING_CHARPOS (*it) - 1;
4020 else if (it->what == IT_COMPOSITION)
4021 /* For composition, we must check the character after the
4022 composition. */
4023 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4024 else
4025 charpos = IT_STRING_CHARPOS (*it) + 1;
4027 else
4029 if (before_p)
4031 /* With bidi iteration, the character before the current
4032 in the visual order cannot be found by simple
4033 iteration, because "reverse" reordering is not
4034 supported. Instead, we need to use the move_it_*
4035 family of functions. */
4036 /* Ignore face changes before the first visible
4037 character on this display line. */
4038 if (it->current_x <= it->first_visible_x)
4039 return it->face_id;
4040 SAVE_IT (it_copy, *it, it_copy_data);
4041 /* Implementation note: Since move_it_in_display_line
4042 works in the iterator geometry, and thinks the first
4043 character is always the leftmost, even in R2L lines,
4044 we don't need to distinguish between the R2L and L2R
4045 cases here. */
4046 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4047 it_copy.current_x - 1, MOVE_TO_X);
4048 charpos = IT_STRING_CHARPOS (it_copy);
4049 RESTORE_IT (it, it, it_copy_data);
4051 else
4053 /* Set charpos to the string position of the character
4054 that comes after IT's current position in the visual
4055 order. */
4056 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4058 it_copy = *it;
4059 while (n--)
4060 bidi_move_to_visually_next (&it_copy.bidi_it);
4062 charpos = it_copy.bidi_it.charpos;
4065 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4067 if (it->current.overlay_string_index >= 0)
4068 bufpos = IT_CHARPOS (*it);
4069 else
4070 bufpos = 0;
4072 base_face_id = underlying_face_id (it);
4074 /* Get the face for ASCII, or unibyte. */
4075 face_id = face_at_string_position (it->w,
4076 it->string,
4077 charpos,
4078 bufpos,
4079 &next_check_charpos,
4080 base_face_id, 0);
4082 /* Correct the face for charsets different from ASCII. Do it
4083 for the multibyte case only. The face returned above is
4084 suitable for unibyte text if IT->string is unibyte. */
4085 if (STRING_MULTIBYTE (it->string))
4087 struct text_pos pos1 = string_pos (charpos, it->string);
4088 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4089 int c, len;
4090 struct face *face = FACE_FROM_ID (it->f, face_id);
4092 c = string_char_and_length (p, &len);
4093 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4096 else
4098 struct text_pos pos;
4100 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4101 || (IT_CHARPOS (*it) <= BEGV && before_p))
4102 return it->face_id;
4104 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4105 pos = it->current.pos;
4107 if (!it->bidi_p)
4109 if (before_p)
4110 DEC_TEXT_POS (pos, it->multibyte_p);
4111 else
4113 if (it->what == IT_COMPOSITION)
4115 /* For composition, we must check the position after
4116 the composition. */
4117 pos.charpos += it->cmp_it.nchars;
4118 pos.bytepos += it->len;
4120 else
4121 INC_TEXT_POS (pos, it->multibyte_p);
4124 else
4126 if (before_p)
4128 /* With bidi iteration, the character before the current
4129 in the visual order cannot be found by simple
4130 iteration, because "reverse" reordering is not
4131 supported. Instead, we need to use the move_it_*
4132 family of functions. */
4133 /* Ignore face changes before the first visible
4134 character on this display line. */
4135 if (it->current_x <= it->first_visible_x)
4136 return it->face_id;
4137 SAVE_IT (it_copy, *it, it_copy_data);
4138 /* Implementation note: Since move_it_in_display_line
4139 works in the iterator geometry, and thinks the first
4140 character is always the leftmost, even in R2L lines,
4141 we don't need to distinguish between the R2L and L2R
4142 cases here. */
4143 move_it_in_display_line (&it_copy, ZV,
4144 it_copy.current_x - 1, MOVE_TO_X);
4145 pos = it_copy.current.pos;
4146 RESTORE_IT (it, it, it_copy_data);
4148 else
4150 /* Set charpos to the buffer position of the character
4151 that comes after IT's current position in the visual
4152 order. */
4153 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4155 it_copy = *it;
4156 while (n--)
4157 bidi_move_to_visually_next (&it_copy.bidi_it);
4159 SET_TEXT_POS (pos,
4160 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4163 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4165 /* Determine face for CHARSET_ASCII, or unibyte. */
4166 face_id = face_at_buffer_position (it->w,
4167 CHARPOS (pos),
4168 &next_check_charpos,
4169 limit, 0, -1);
4171 /* Correct the face for charsets different from ASCII. Do it
4172 for the multibyte case only. The face returned above is
4173 suitable for unibyte text if current_buffer is unibyte. */
4174 if (it->multibyte_p)
4176 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4177 struct face *face = FACE_FROM_ID (it->f, face_id);
4178 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4182 return face_id;
4187 /***********************************************************************
4188 Invisible text
4189 ***********************************************************************/
4191 /* Set up iterator IT from invisible properties at its current
4192 position. Called from handle_stop. */
4194 static enum prop_handled
4195 handle_invisible_prop (struct it *it)
4197 enum prop_handled handled = HANDLED_NORMALLY;
4198 int invis_p;
4199 Lisp_Object prop;
4201 if (STRINGP (it->string))
4203 Lisp_Object end_charpos, limit, charpos;
4205 /* Get the value of the invisible text property at the
4206 current position. Value will be nil if there is no such
4207 property. */
4208 charpos = make_number (IT_STRING_CHARPOS (*it));
4209 prop = Fget_text_property (charpos, Qinvisible, it->string);
4210 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4212 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4214 /* Record whether we have to display an ellipsis for the
4215 invisible text. */
4216 int display_ellipsis_p = (invis_p == 2);
4217 ptrdiff_t len, endpos;
4219 handled = HANDLED_RECOMPUTE_PROPS;
4221 /* Get the position at which the next visible text can be
4222 found in IT->string, if any. */
4223 endpos = len = SCHARS (it->string);
4224 XSETINT (limit, len);
4227 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4228 it->string, limit);
4229 if (INTEGERP (end_charpos))
4231 endpos = XFASTINT (end_charpos);
4232 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4233 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4234 if (invis_p == 2)
4235 display_ellipsis_p = true;
4238 while (invis_p && endpos < len);
4240 if (display_ellipsis_p)
4241 it->ellipsis_p = true;
4243 if (endpos < len)
4245 /* Text at END_CHARPOS is visible. Move IT there. */
4246 struct text_pos old;
4247 ptrdiff_t oldpos;
4249 old = it->current.string_pos;
4250 oldpos = CHARPOS (old);
4251 if (it->bidi_p)
4253 if (it->bidi_it.first_elt
4254 && it->bidi_it.charpos < SCHARS (it->string))
4255 bidi_paragraph_init (it->paragraph_embedding,
4256 &it->bidi_it, 1);
4257 /* Bidi-iterate out of the invisible text. */
4260 bidi_move_to_visually_next (&it->bidi_it);
4262 while (oldpos <= it->bidi_it.charpos
4263 && it->bidi_it.charpos < endpos);
4265 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4266 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4267 if (IT_CHARPOS (*it) >= endpos)
4268 it->prev_stop = endpos;
4270 else
4272 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4273 compute_string_pos (&it->current.string_pos, old, it->string);
4276 else
4278 /* The rest of the string is invisible. If this is an
4279 overlay string, proceed with the next overlay string
4280 or whatever comes and return a character from there. */
4281 if (it->current.overlay_string_index >= 0
4282 && !display_ellipsis_p)
4284 next_overlay_string (it);
4285 /* Don't check for overlay strings when we just
4286 finished processing them. */
4287 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4289 else
4291 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4292 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4297 else
4299 ptrdiff_t newpos, next_stop, start_charpos, tem;
4300 Lisp_Object pos, overlay;
4302 /* First of all, is there invisible text at this position? */
4303 tem = start_charpos = IT_CHARPOS (*it);
4304 pos = make_number (tem);
4305 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4306 &overlay);
4307 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4309 /* If we are on invisible text, skip over it. */
4310 if (invis_p && start_charpos < it->end_charpos)
4312 /* Record whether we have to display an ellipsis for the
4313 invisible text. */
4314 int display_ellipsis_p = invis_p == 2;
4316 handled = HANDLED_RECOMPUTE_PROPS;
4318 /* Loop skipping over invisible text. The loop is left at
4319 ZV or with IT on the first char being visible again. */
4322 /* Try to skip some invisible text. Return value is the
4323 position reached which can be equal to where we start
4324 if there is nothing invisible there. This skips both
4325 over invisible text properties and overlays with
4326 invisible property. */
4327 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4329 /* If we skipped nothing at all we weren't at invisible
4330 text in the first place. If everything to the end of
4331 the buffer was skipped, end the loop. */
4332 if (newpos == tem || newpos >= ZV)
4333 invis_p = 0;
4334 else
4336 /* We skipped some characters but not necessarily
4337 all there are. Check if we ended up on visible
4338 text. Fget_char_property returns the property of
4339 the char before the given position, i.e. if we
4340 get invis_p = 0, this means that the char at
4341 newpos is visible. */
4342 pos = make_number (newpos);
4343 prop = Fget_char_property (pos, Qinvisible, it->window);
4344 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 /* If we ended up on invisible text, proceed to
4348 skip starting with next_stop. */
4349 if (invis_p)
4350 tem = next_stop;
4352 /* If there are adjacent invisible texts, don't lose the
4353 second one's ellipsis. */
4354 if (invis_p == 2)
4355 display_ellipsis_p = true;
4357 while (invis_p);
4359 /* The position newpos is now either ZV or on visible text. */
4360 if (it->bidi_p)
4362 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4363 int on_newline
4364 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4365 int after_newline
4366 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4368 /* If the invisible text ends on a newline or on a
4369 character after a newline, we can avoid the costly,
4370 character by character, bidi iteration to NEWPOS, and
4371 instead simply reseat the iterator there. That's
4372 because all bidi reordering information is tossed at
4373 the newline. This is a big win for modes that hide
4374 complete lines, like Outline, Org, etc. */
4375 if (on_newline || after_newline)
4377 struct text_pos tpos;
4378 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4380 SET_TEXT_POS (tpos, newpos, bpos);
4381 reseat_1 (it, tpos, 0);
4382 /* If we reseat on a newline/ZV, we need to prep the
4383 bidi iterator for advancing to the next character
4384 after the newline/EOB, keeping the current paragraph
4385 direction (so that PRODUCE_GLYPHS does TRT wrt
4386 prepending/appending glyphs to a glyph row). */
4387 if (on_newline)
4389 it->bidi_it.first_elt = 0;
4390 it->bidi_it.paragraph_dir = pdir;
4391 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4392 it->bidi_it.nchars = 1;
4393 it->bidi_it.ch_len = 1;
4396 else /* Must use the slow method. */
4398 /* With bidi iteration, the region of invisible text
4399 could start and/or end in the middle of a
4400 non-base embedding level. Therefore, we need to
4401 skip invisible text using the bidi iterator,
4402 starting at IT's current position, until we find
4403 ourselves outside of the invisible text.
4404 Skipping invisible text _after_ bidi iteration
4405 avoids affecting the visual order of the
4406 displayed text when invisible properties are
4407 added or removed. */
4408 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4410 /* If we were `reseat'ed to a new paragraph,
4411 determine the paragraph base direction. We
4412 need to do it now because
4413 next_element_from_buffer may not have a
4414 chance to do it, if we are going to skip any
4415 text at the beginning, which resets the
4416 FIRST_ELT flag. */
4417 bidi_paragraph_init (it->paragraph_embedding,
4418 &it->bidi_it, 1);
4422 bidi_move_to_visually_next (&it->bidi_it);
4424 while (it->stop_charpos <= it->bidi_it.charpos
4425 && it->bidi_it.charpos < newpos);
4426 IT_CHARPOS (*it) = it->bidi_it.charpos;
4427 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4428 /* If we overstepped NEWPOS, record its position in
4429 the iterator, so that we skip invisible text if
4430 later the bidi iteration lands us in the
4431 invisible region again. */
4432 if (IT_CHARPOS (*it) >= newpos)
4433 it->prev_stop = newpos;
4436 else
4438 IT_CHARPOS (*it) = newpos;
4439 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4442 /* If there are before-strings at the start of invisible
4443 text, and the text is invisible because of a text
4444 property, arrange to show before-strings because 20.x did
4445 it that way. (If the text is invisible because of an
4446 overlay property instead of a text property, this is
4447 already handled in the overlay code.) */
4448 if (NILP (overlay)
4449 && get_overlay_strings (it, it->stop_charpos))
4451 handled = HANDLED_RECOMPUTE_PROPS;
4452 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4454 else if (display_ellipsis_p)
4456 /* Make sure that the glyphs of the ellipsis will get
4457 correct `charpos' values. If we would not update
4458 it->position here, the glyphs would belong to the
4459 last visible character _before_ the invisible
4460 text, which confuses `set_cursor_from_row'.
4462 We use the last invisible position instead of the
4463 first because this way the cursor is always drawn on
4464 the first "." of the ellipsis, whenever PT is inside
4465 the invisible text. Otherwise the cursor would be
4466 placed _after_ the ellipsis when the point is after the
4467 first invisible character. */
4468 if (!STRINGP (it->object))
4470 it->position.charpos = newpos - 1;
4471 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4473 it->ellipsis_p = true;
4474 /* Let the ellipsis display before
4475 considering any properties of the following char.
4476 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4477 handled = HANDLED_RETURN;
4482 return handled;
4486 /* Make iterator IT return `...' next.
4487 Replaces LEN characters from buffer. */
4489 static void
4490 setup_for_ellipsis (struct it *it, int len)
4492 /* Use the display table definition for `...'. Invalid glyphs
4493 will be handled by the method returning elements from dpvec. */
4494 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4496 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4497 it->dpvec = v->contents;
4498 it->dpend = v->contents + v->header.size;
4500 else
4502 /* Default `...'. */
4503 it->dpvec = default_invis_vector;
4504 it->dpend = default_invis_vector + 3;
4507 it->dpvec_char_len = len;
4508 it->current.dpvec_index = 0;
4509 it->dpvec_face_id = -1;
4511 /* Remember the current face id in case glyphs specify faces.
4512 IT's face is restored in set_iterator_to_next.
4513 saved_face_id was set to preceding char's face in handle_stop. */
4514 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4515 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4517 it->method = GET_FROM_DISPLAY_VECTOR;
4518 it->ellipsis_p = true;
4523 /***********************************************************************
4524 'display' property
4525 ***********************************************************************/
4527 /* Set up iterator IT from `display' property at its current position.
4528 Called from handle_stop.
4529 We return HANDLED_RETURN if some part of the display property
4530 overrides the display of the buffer text itself.
4531 Otherwise we return HANDLED_NORMALLY. */
4533 static enum prop_handled
4534 handle_display_prop (struct it *it)
4536 Lisp_Object propval, object, overlay;
4537 struct text_pos *position;
4538 ptrdiff_t bufpos;
4539 /* Nonzero if some property replaces the display of the text itself. */
4540 int display_replaced_p = 0;
4542 if (STRINGP (it->string))
4544 object = it->string;
4545 position = &it->current.string_pos;
4546 bufpos = CHARPOS (it->current.pos);
4548 else
4550 XSETWINDOW (object, it->w);
4551 position = &it->current.pos;
4552 bufpos = CHARPOS (*position);
4555 /* Reset those iterator values set from display property values. */
4556 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4557 it->space_width = Qnil;
4558 it->font_height = Qnil;
4559 it->voffset = 0;
4561 /* We don't support recursive `display' properties, i.e. string
4562 values that have a string `display' property, that have a string
4563 `display' property etc. */
4564 if (!it->string_from_display_prop_p)
4565 it->area = TEXT_AREA;
4567 propval = get_char_property_and_overlay (make_number (position->charpos),
4568 Qdisplay, object, &overlay);
4569 if (NILP (propval))
4570 return HANDLED_NORMALLY;
4571 /* Now OVERLAY is the overlay that gave us this property, or nil
4572 if it was a text property. */
4574 if (!STRINGP (it->string))
4575 object = it->w->contents;
4577 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4578 position, bufpos,
4579 FRAME_WINDOW_P (it->f));
4581 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4584 /* Subroutine of handle_display_prop. Returns non-zero if the display
4585 specification in SPEC is a replacing specification, i.e. it would
4586 replace the text covered by `display' property with something else,
4587 such as an image or a display string. If SPEC includes any kind or
4588 `(space ...) specification, the value is 2; this is used by
4589 compute_display_string_pos, which see.
4591 See handle_single_display_spec for documentation of arguments.
4592 frame_window_p is non-zero if the window being redisplayed is on a
4593 GUI frame; this argument is used only if IT is NULL, see below.
4595 IT can be NULL, if this is called by the bidi reordering code
4596 through compute_display_string_pos, which see. In that case, this
4597 function only examines SPEC, but does not otherwise "handle" it, in
4598 the sense that it doesn't set up members of IT from the display
4599 spec. */
4600 static int
4601 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4602 Lisp_Object overlay, struct text_pos *position,
4603 ptrdiff_t bufpos, int frame_window_p)
4605 int replacing_p = 0;
4606 int rv;
4608 if (CONSP (spec)
4609 /* Simple specifications. */
4610 && !EQ (XCAR (spec), Qimage)
4611 && !EQ (XCAR (spec), Qspace)
4612 && !EQ (XCAR (spec), Qwhen)
4613 && !EQ (XCAR (spec), Qslice)
4614 && !EQ (XCAR (spec), Qspace_width)
4615 && !EQ (XCAR (spec), Qheight)
4616 && !EQ (XCAR (spec), Qraise)
4617 /* Marginal area specifications. */
4618 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4619 && !EQ (XCAR (spec), Qleft_fringe)
4620 && !EQ (XCAR (spec), Qright_fringe)
4621 && !NILP (XCAR (spec)))
4623 for (; CONSP (spec); spec = XCDR (spec))
4625 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4637 else if (VECTORP (spec))
4639 ptrdiff_t i;
4640 for (i = 0; i < ASIZE (spec); ++i)
4641 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4642 overlay, position, bufpos,
4643 replacing_p, frame_window_p)))
4645 replacing_p = rv;
4646 /* If some text in a string is replaced, `position' no
4647 longer points to the position of `object'. */
4648 if (!it || STRINGP (object))
4649 break;
4652 else
4654 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4655 position, bufpos, 0,
4656 frame_window_p)))
4657 replacing_p = rv;
4660 return replacing_p;
4663 /* Value is the position of the end of the `display' property starting
4664 at START_POS in OBJECT. */
4666 static struct text_pos
4667 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4669 Lisp_Object end;
4670 struct text_pos end_pos;
4672 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4673 Qdisplay, object, Qnil);
4674 CHARPOS (end_pos) = XFASTINT (end);
4675 if (STRINGP (object))
4676 compute_string_pos (&end_pos, start_pos, it->string);
4677 else
4678 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4680 return end_pos;
4684 /* Set up IT from a single `display' property specification SPEC. OBJECT
4685 is the object in which the `display' property was found. *POSITION
4686 is the position in OBJECT at which the `display' property was found.
4687 BUFPOS is the buffer position of OBJECT (different from POSITION if
4688 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4689 previously saw a display specification which already replaced text
4690 display with something else, for example an image; we ignore such
4691 properties after the first one has been processed.
4693 OVERLAY is the overlay this `display' property came from,
4694 or nil if it was a text property.
4696 If SPEC is a `space' or `image' specification, and in some other
4697 cases too, set *POSITION to the position where the `display'
4698 property ends.
4700 If IT is NULL, only examine the property specification in SPEC, but
4701 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4702 is intended to be displayed in a window on a GUI frame.
4704 Value is non-zero if something was found which replaces the display
4705 of buffer or string text. */
4707 static int
4708 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4709 Lisp_Object overlay, struct text_pos *position,
4710 ptrdiff_t bufpos, int display_replaced_p,
4711 int frame_window_p)
4713 Lisp_Object form;
4714 Lisp_Object location, value;
4715 struct text_pos start_pos = *position;
4716 int valid_p;
4718 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4719 If the result is non-nil, use VALUE instead of SPEC. */
4720 form = Qt;
4721 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4723 spec = XCDR (spec);
4724 if (!CONSP (spec))
4725 return 0;
4726 form = XCAR (spec);
4727 spec = XCDR (spec);
4730 if (!NILP (form) && !EQ (form, Qt))
4732 ptrdiff_t count = SPECPDL_INDEX ();
4733 struct gcpro gcpro1;
4735 /* Bind `object' to the object having the `display' property, a
4736 buffer or string. Bind `position' to the position in the
4737 object where the property was found, and `buffer-position'
4738 to the current position in the buffer. */
4740 if (NILP (object))
4741 XSETBUFFER (object, current_buffer);
4742 specbind (Qobject, object);
4743 specbind (Qposition, make_number (CHARPOS (*position)));
4744 specbind (Qbuffer_position, make_number (bufpos));
4745 GCPRO1 (form);
4746 form = safe_eval (form);
4747 UNGCPRO;
4748 unbind_to (count, Qnil);
4751 if (NILP (form))
4752 return 0;
4754 /* Handle `(height HEIGHT)' specifications. */
4755 if (CONSP (spec)
4756 && EQ (XCAR (spec), Qheight)
4757 && CONSP (XCDR (spec)))
4759 if (it)
4761 if (!FRAME_WINDOW_P (it->f))
4762 return 0;
4764 it->font_height = XCAR (XCDR (spec));
4765 if (!NILP (it->font_height))
4767 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4768 int new_height = -1;
4770 if (CONSP (it->font_height)
4771 && (EQ (XCAR (it->font_height), Qplus)
4772 || EQ (XCAR (it->font_height), Qminus))
4773 && CONSP (XCDR (it->font_height))
4774 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4776 /* `(+ N)' or `(- N)' where N is an integer. */
4777 int steps = XINT (XCAR (XCDR (it->font_height)));
4778 if (EQ (XCAR (it->font_height), Qplus))
4779 steps = - steps;
4780 it->face_id = smaller_face (it->f, it->face_id, steps);
4782 else if (FUNCTIONP (it->font_height))
4784 /* Call function with current height as argument.
4785 Value is the new height. */
4786 Lisp_Object height;
4787 height = safe_call1 (it->font_height,
4788 face->lface[LFACE_HEIGHT_INDEX]);
4789 if (NUMBERP (height))
4790 new_height = XFLOATINT (height);
4792 else if (NUMBERP (it->font_height))
4794 /* Value is a multiple of the canonical char height. */
4795 struct face *f;
4797 f = FACE_FROM_ID (it->f,
4798 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4799 new_height = (XFLOATINT (it->font_height)
4800 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4802 else
4804 /* Evaluate IT->font_height with `height' bound to the
4805 current specified height to get the new height. */
4806 ptrdiff_t count = SPECPDL_INDEX ();
4808 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4809 value = safe_eval (it->font_height);
4810 unbind_to (count, Qnil);
4812 if (NUMBERP (value))
4813 new_height = XFLOATINT (value);
4816 if (new_height > 0)
4817 it->face_id = face_with_height (it->f, it->face_id, new_height);
4821 return 0;
4824 /* Handle `(space-width WIDTH)'. */
4825 if (CONSP (spec)
4826 && EQ (XCAR (spec), Qspace_width)
4827 && CONSP (XCDR (spec)))
4829 if (it)
4831 if (!FRAME_WINDOW_P (it->f))
4832 return 0;
4834 value = XCAR (XCDR (spec));
4835 if (NUMBERP (value) && XFLOATINT (value) > 0)
4836 it->space_width = value;
4839 return 0;
4842 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4843 if (CONSP (spec)
4844 && EQ (XCAR (spec), Qslice))
4846 Lisp_Object tem;
4848 if (it)
4850 if (!FRAME_WINDOW_P (it->f))
4851 return 0;
4853 if (tem = XCDR (spec), CONSP (tem))
4855 it->slice.x = XCAR (tem);
4856 if (tem = XCDR (tem), CONSP (tem))
4858 it->slice.y = XCAR (tem);
4859 if (tem = XCDR (tem), CONSP (tem))
4861 it->slice.width = XCAR (tem);
4862 if (tem = XCDR (tem), CONSP (tem))
4863 it->slice.height = XCAR (tem);
4869 return 0;
4872 /* Handle `(raise FACTOR)'. */
4873 if (CONSP (spec)
4874 && EQ (XCAR (spec), Qraise)
4875 && CONSP (XCDR (spec)))
4877 if (it)
4879 if (!FRAME_WINDOW_P (it->f))
4880 return 0;
4882 #ifdef HAVE_WINDOW_SYSTEM
4883 value = XCAR (XCDR (spec));
4884 if (NUMBERP (value))
4886 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4887 it->voffset = - (XFLOATINT (value)
4888 * (FONT_HEIGHT (face->font)));
4890 #endif /* HAVE_WINDOW_SYSTEM */
4893 return 0;
4896 /* Don't handle the other kinds of display specifications
4897 inside a string that we got from a `display' property. */
4898 if (it && it->string_from_display_prop_p)
4899 return 0;
4901 /* Characters having this form of property are not displayed, so
4902 we have to find the end of the property. */
4903 if (it)
4905 start_pos = *position;
4906 *position = display_prop_end (it, object, start_pos);
4908 value = Qnil;
4910 /* Stop the scan at that end position--we assume that all
4911 text properties change there. */
4912 if (it)
4913 it->stop_charpos = position->charpos;
4915 /* Handle `(left-fringe BITMAP [FACE])'
4916 and `(right-fringe BITMAP [FACE])'. */
4917 if (CONSP (spec)
4918 && (EQ (XCAR (spec), Qleft_fringe)
4919 || EQ (XCAR (spec), Qright_fringe))
4920 && CONSP (XCDR (spec)))
4922 int fringe_bitmap;
4924 if (it)
4926 if (!FRAME_WINDOW_P (it->f))
4927 /* If we return here, POSITION has been advanced
4928 across the text with this property. */
4930 /* Synchronize the bidi iterator with POSITION. This is
4931 needed because we are not going to push the iterator
4932 on behalf of this display property, so there will be
4933 no pop_it call to do this synchronization for us. */
4934 if (it->bidi_p)
4936 it->position = *position;
4937 iterate_out_of_display_property (it);
4938 *position = it->position;
4940 return 1;
4943 else if (!frame_window_p)
4944 return 1;
4946 #ifdef HAVE_WINDOW_SYSTEM
4947 value = XCAR (XCDR (spec));
4948 if (!SYMBOLP (value)
4949 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4950 /* If we return here, POSITION has been advanced
4951 across the text with this property. */
4953 if (it && it->bidi_p)
4955 it->position = *position;
4956 iterate_out_of_display_property (it);
4957 *position = it->position;
4959 return 1;
4962 if (it)
4964 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4966 if (CONSP (XCDR (XCDR (spec))))
4968 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4969 int face_id2 = lookup_derived_face (it->f, face_name,
4970 FRINGE_FACE_ID, 0);
4971 if (face_id2 >= 0)
4972 face_id = face_id2;
4975 /* Save current settings of IT so that we can restore them
4976 when we are finished with the glyph property value. */
4977 push_it (it, position);
4979 it->area = TEXT_AREA;
4980 it->what = IT_IMAGE;
4981 it->image_id = -1; /* no image */
4982 it->position = start_pos;
4983 it->object = NILP (object) ? it->w->contents : object;
4984 it->method = GET_FROM_IMAGE;
4985 it->from_overlay = Qnil;
4986 it->face_id = face_id;
4987 it->from_disp_prop_p = true;
4989 /* Say that we haven't consumed the characters with
4990 `display' property yet. The call to pop_it in
4991 set_iterator_to_next will clean this up. */
4992 *position = start_pos;
4994 if (EQ (XCAR (spec), Qleft_fringe))
4996 it->left_user_fringe_bitmap = fringe_bitmap;
4997 it->left_user_fringe_face_id = face_id;
4999 else
5001 it->right_user_fringe_bitmap = fringe_bitmap;
5002 it->right_user_fringe_face_id = face_id;
5005 #endif /* HAVE_WINDOW_SYSTEM */
5006 return 1;
5009 /* Prepare to handle `((margin left-margin) ...)',
5010 `((margin right-margin) ...)' and `((margin nil) ...)'
5011 prefixes for display specifications. */
5012 location = Qunbound;
5013 if (CONSP (spec) && CONSP (XCAR (spec)))
5015 Lisp_Object tem;
5017 value = XCDR (spec);
5018 if (CONSP (value))
5019 value = XCAR (value);
5021 tem = XCAR (spec);
5022 if (EQ (XCAR (tem), Qmargin)
5023 && (tem = XCDR (tem),
5024 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5025 (NILP (tem)
5026 || EQ (tem, Qleft_margin)
5027 || EQ (tem, Qright_margin))))
5028 location = tem;
5031 if (EQ (location, Qunbound))
5033 location = Qnil;
5034 value = spec;
5037 /* After this point, VALUE is the property after any
5038 margin prefix has been stripped. It must be a string,
5039 an image specification, or `(space ...)'.
5041 LOCATION specifies where to display: `left-margin',
5042 `right-margin' or nil. */
5044 valid_p = (STRINGP (value)
5045 #ifdef HAVE_WINDOW_SYSTEM
5046 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5047 && valid_image_p (value))
5048 #endif /* not HAVE_WINDOW_SYSTEM */
5049 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5051 if (valid_p && !display_replaced_p)
5053 int retval = 1;
5055 if (!it)
5057 /* Callers need to know whether the display spec is any kind
5058 of `(space ...)' spec that is about to affect text-area
5059 display. */
5060 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5061 retval = 2;
5062 return retval;
5065 /* Save current settings of IT so that we can restore them
5066 when we are finished with the glyph property value. */
5067 push_it (it, position);
5068 it->from_overlay = overlay;
5069 it->from_disp_prop_p = true;
5071 if (NILP (location))
5072 it->area = TEXT_AREA;
5073 else if (EQ (location, Qleft_margin))
5074 it->area = LEFT_MARGIN_AREA;
5075 else
5076 it->area = RIGHT_MARGIN_AREA;
5078 if (STRINGP (value))
5080 it->string = value;
5081 it->multibyte_p = STRING_MULTIBYTE (it->string);
5082 it->current.overlay_string_index = -1;
5083 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5084 it->end_charpos = it->string_nchars = SCHARS (it->string);
5085 it->method = GET_FROM_STRING;
5086 it->stop_charpos = 0;
5087 it->prev_stop = 0;
5088 it->base_level_stop = 0;
5089 it->string_from_display_prop_p = true;
5090 /* Say that we haven't consumed the characters with
5091 `display' property yet. The call to pop_it in
5092 set_iterator_to_next will clean this up. */
5093 if (BUFFERP (object))
5094 *position = start_pos;
5096 /* Force paragraph direction to be that of the parent
5097 object. If the parent object's paragraph direction is
5098 not yet determined, default to L2R. */
5099 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5100 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5101 else
5102 it->paragraph_embedding = L2R;
5104 /* Set up the bidi iterator for this display string. */
5105 if (it->bidi_p)
5107 it->bidi_it.string.lstring = it->string;
5108 it->bidi_it.string.s = NULL;
5109 it->bidi_it.string.schars = it->end_charpos;
5110 it->bidi_it.string.bufpos = bufpos;
5111 it->bidi_it.string.from_disp_str = 1;
5112 it->bidi_it.string.unibyte = !it->multibyte_p;
5113 it->bidi_it.w = it->w;
5114 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5117 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5119 it->method = GET_FROM_STRETCH;
5120 it->object = value;
5121 *position = it->position = start_pos;
5122 retval = 1 + (it->area == TEXT_AREA);
5124 #ifdef HAVE_WINDOW_SYSTEM
5125 else
5127 it->what = IT_IMAGE;
5128 it->image_id = lookup_image (it->f, value);
5129 it->position = start_pos;
5130 it->object = NILP (object) ? it->w->contents : object;
5131 it->method = GET_FROM_IMAGE;
5133 /* Say that we haven't consumed the characters with
5134 `display' property yet. The call to pop_it in
5135 set_iterator_to_next will clean this up. */
5136 *position = start_pos;
5138 #endif /* HAVE_WINDOW_SYSTEM */
5140 return retval;
5143 /* Invalid property or property not supported. Restore
5144 POSITION to what it was before. */
5145 *position = start_pos;
5146 return 0;
5149 /* Check if PROP is a display property value whose text should be
5150 treated as intangible. OVERLAY is the overlay from which PROP
5151 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5152 specify the buffer position covered by PROP. */
5155 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5156 ptrdiff_t charpos, ptrdiff_t bytepos)
5158 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5159 struct text_pos position;
5161 SET_TEXT_POS (position, charpos, bytepos);
5162 return handle_display_spec (NULL, prop, Qnil, overlay,
5163 &position, charpos, frame_window_p);
5167 /* Return 1 if PROP is a display sub-property value containing STRING.
5169 Implementation note: this and the following function are really
5170 special cases of handle_display_spec and
5171 handle_single_display_spec, and should ideally use the same code.
5172 Until they do, these two pairs must be consistent and must be
5173 modified in sync. */
5175 static int
5176 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5178 if (EQ (string, prop))
5179 return 1;
5181 /* Skip over `when FORM'. */
5182 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5184 prop = XCDR (prop);
5185 if (!CONSP (prop))
5186 return 0;
5187 /* Actually, the condition following `when' should be eval'ed,
5188 like handle_single_display_spec does, and we should return
5189 zero if it evaluates to nil. However, this function is
5190 called only when the buffer was already displayed and some
5191 glyph in the glyph matrix was found to come from a display
5192 string. Therefore, the condition was already evaluated, and
5193 the result was non-nil, otherwise the display string wouldn't
5194 have been displayed and we would have never been called for
5195 this property. Thus, we can skip the evaluation and assume
5196 its result is non-nil. */
5197 prop = XCDR (prop);
5200 if (CONSP (prop))
5201 /* Skip over `margin LOCATION'. */
5202 if (EQ (XCAR (prop), Qmargin))
5204 prop = XCDR (prop);
5205 if (!CONSP (prop))
5206 return 0;
5208 prop = XCDR (prop);
5209 if (!CONSP (prop))
5210 return 0;
5213 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5217 /* Return 1 if STRING appears in the `display' property PROP. */
5219 static int
5220 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5222 if (CONSP (prop)
5223 && !EQ (XCAR (prop), Qwhen)
5224 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5226 /* A list of sub-properties. */
5227 while (CONSP (prop))
5229 if (single_display_spec_string_p (XCAR (prop), string))
5230 return 1;
5231 prop = XCDR (prop);
5234 else if (VECTORP (prop))
5236 /* A vector of sub-properties. */
5237 ptrdiff_t i;
5238 for (i = 0; i < ASIZE (prop); ++i)
5239 if (single_display_spec_string_p (AREF (prop, i), string))
5240 return 1;
5242 else
5243 return single_display_spec_string_p (prop, string);
5245 return 0;
5248 /* Look for STRING in overlays and text properties in the current
5249 buffer, between character positions FROM and TO (excluding TO).
5250 BACK_P non-zero means look back (in this case, TO is supposed to be
5251 less than FROM).
5252 Value is the first character position where STRING was found, or
5253 zero if it wasn't found before hitting TO.
5255 This function may only use code that doesn't eval because it is
5256 called asynchronously from note_mouse_highlight. */
5258 static ptrdiff_t
5259 string_buffer_position_lim (Lisp_Object string,
5260 ptrdiff_t from, ptrdiff_t to, int back_p)
5262 Lisp_Object limit, prop, pos;
5263 int found = 0;
5265 pos = make_number (max (from, BEGV));
5267 if (!back_p) /* looking forward */
5269 limit = make_number (min (to, ZV));
5270 while (!found && !EQ (pos, limit))
5272 prop = Fget_char_property (pos, Qdisplay, Qnil);
5273 if (!NILP (prop) && display_prop_string_p (prop, string))
5274 found = 1;
5275 else
5276 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5277 limit);
5280 else /* looking back */
5282 limit = make_number (max (to, BEGV));
5283 while (!found && !EQ (pos, limit))
5285 prop = Fget_char_property (pos, Qdisplay, Qnil);
5286 if (!NILP (prop) && display_prop_string_p (prop, string))
5287 found = 1;
5288 else
5289 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5290 limit);
5294 return found ? XINT (pos) : 0;
5297 /* Determine which buffer position in current buffer STRING comes from.
5298 AROUND_CHARPOS is an approximate position where it could come from.
5299 Value is the buffer position or 0 if it couldn't be determined.
5301 This function is necessary because we don't record buffer positions
5302 in glyphs generated from strings (to keep struct glyph small).
5303 This function may only use code that doesn't eval because it is
5304 called asynchronously from note_mouse_highlight. */
5306 static ptrdiff_t
5307 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5309 const int MAX_DISTANCE = 1000;
5310 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5311 around_charpos + MAX_DISTANCE,
5314 if (!found)
5315 found = string_buffer_position_lim (string, around_charpos,
5316 around_charpos - MAX_DISTANCE, 1);
5317 return found;
5322 /***********************************************************************
5323 `composition' property
5324 ***********************************************************************/
5326 /* Set up iterator IT from `composition' property at its current
5327 position. Called from handle_stop. */
5329 static enum prop_handled
5330 handle_composition_prop (struct it *it)
5332 Lisp_Object prop, string;
5333 ptrdiff_t pos, pos_byte, start, end;
5335 if (STRINGP (it->string))
5337 unsigned char *s;
5339 pos = IT_STRING_CHARPOS (*it);
5340 pos_byte = IT_STRING_BYTEPOS (*it);
5341 string = it->string;
5342 s = SDATA (string) + pos_byte;
5343 it->c = STRING_CHAR (s);
5345 else
5347 pos = IT_CHARPOS (*it);
5348 pos_byte = IT_BYTEPOS (*it);
5349 string = Qnil;
5350 it->c = FETCH_CHAR (pos_byte);
5353 /* If there's a valid composition and point is not inside of the
5354 composition (in the case that the composition is from the current
5355 buffer), draw a glyph composed from the composition components. */
5356 if (find_composition (pos, -1, &start, &end, &prop, string)
5357 && composition_valid_p (start, end, prop)
5358 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5360 if (start < pos)
5361 /* As we can't handle this situation (perhaps font-lock added
5362 a new composition), we just return here hoping that next
5363 redisplay will detect this composition much earlier. */
5364 return HANDLED_NORMALLY;
5365 if (start != pos)
5367 if (STRINGP (it->string))
5368 pos_byte = string_char_to_byte (it->string, start);
5369 else
5370 pos_byte = CHAR_TO_BYTE (start);
5372 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5373 prop, string);
5375 if (it->cmp_it.id >= 0)
5377 it->cmp_it.ch = -1;
5378 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5379 it->cmp_it.nglyphs = -1;
5383 return HANDLED_NORMALLY;
5388 /***********************************************************************
5389 Overlay strings
5390 ***********************************************************************/
5392 /* The following structure is used to record overlay strings for
5393 later sorting in load_overlay_strings. */
5395 struct overlay_entry
5397 Lisp_Object overlay;
5398 Lisp_Object string;
5399 EMACS_INT priority;
5400 int after_string_p;
5404 /* Set up iterator IT from overlay strings at its current position.
5405 Called from handle_stop. */
5407 static enum prop_handled
5408 handle_overlay_change (struct it *it)
5410 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5411 return HANDLED_RECOMPUTE_PROPS;
5412 else
5413 return HANDLED_NORMALLY;
5417 /* Set up the next overlay string for delivery by IT, if there is an
5418 overlay string to deliver. Called by set_iterator_to_next when the
5419 end of the current overlay string is reached. If there are more
5420 overlay strings to display, IT->string and
5421 IT->current.overlay_string_index are set appropriately here.
5422 Otherwise IT->string is set to nil. */
5424 static void
5425 next_overlay_string (struct it *it)
5427 ++it->current.overlay_string_index;
5428 if (it->current.overlay_string_index == it->n_overlay_strings)
5430 /* No more overlay strings. Restore IT's settings to what
5431 they were before overlay strings were processed, and
5432 continue to deliver from current_buffer. */
5434 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5435 pop_it (it);
5436 eassert (it->sp > 0
5437 || (NILP (it->string)
5438 && it->method == GET_FROM_BUFFER
5439 && it->stop_charpos >= BEGV
5440 && it->stop_charpos <= it->end_charpos));
5441 it->current.overlay_string_index = -1;
5442 it->n_overlay_strings = 0;
5443 it->overlay_strings_charpos = -1;
5444 /* If there's an empty display string on the stack, pop the
5445 stack, to resync the bidi iterator with IT's position. Such
5446 empty strings are pushed onto the stack in
5447 get_overlay_strings_1. */
5448 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5449 pop_it (it);
5451 /* If we're at the end of the buffer, record that we have
5452 processed the overlay strings there already, so that
5453 next_element_from_buffer doesn't try it again. */
5454 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5455 it->overlay_strings_at_end_processed_p = true;
5457 else
5459 /* There are more overlay strings to process. If
5460 IT->current.overlay_string_index has advanced to a position
5461 where we must load IT->overlay_strings with more strings, do
5462 it. We must load at the IT->overlay_strings_charpos where
5463 IT->n_overlay_strings was originally computed; when invisible
5464 text is present, this might not be IT_CHARPOS (Bug#7016). */
5465 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5467 if (it->current.overlay_string_index && i == 0)
5468 load_overlay_strings (it, it->overlay_strings_charpos);
5470 /* Initialize IT to deliver display elements from the overlay
5471 string. */
5472 it->string = it->overlay_strings[i];
5473 it->multibyte_p = STRING_MULTIBYTE (it->string);
5474 SET_TEXT_POS (it->current.string_pos, 0, 0);
5475 it->method = GET_FROM_STRING;
5476 it->stop_charpos = 0;
5477 it->end_charpos = SCHARS (it->string);
5478 if (it->cmp_it.stop_pos >= 0)
5479 it->cmp_it.stop_pos = 0;
5480 it->prev_stop = 0;
5481 it->base_level_stop = 0;
5483 /* Set up the bidi iterator for this overlay string. */
5484 if (it->bidi_p)
5486 it->bidi_it.string.lstring = it->string;
5487 it->bidi_it.string.s = NULL;
5488 it->bidi_it.string.schars = SCHARS (it->string);
5489 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5490 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5491 it->bidi_it.string.unibyte = !it->multibyte_p;
5492 it->bidi_it.w = it->w;
5493 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5497 CHECK_IT (it);
5501 /* Compare two overlay_entry structures E1 and E2. Used as a
5502 comparison function for qsort in load_overlay_strings. Overlay
5503 strings for the same position are sorted so that
5505 1. All after-strings come in front of before-strings, except
5506 when they come from the same overlay.
5508 2. Within after-strings, strings are sorted so that overlay strings
5509 from overlays with higher priorities come first.
5511 2. Within before-strings, strings are sorted so that overlay
5512 strings from overlays with higher priorities come last.
5514 Value is analogous to strcmp. */
5517 static int
5518 compare_overlay_entries (const void *e1, const void *e2)
5520 struct overlay_entry const *entry1 = e1;
5521 struct overlay_entry const *entry2 = e2;
5522 int result;
5524 if (entry1->after_string_p != entry2->after_string_p)
5526 /* Let after-strings appear in front of before-strings if
5527 they come from different overlays. */
5528 if (EQ (entry1->overlay, entry2->overlay))
5529 result = entry1->after_string_p ? 1 : -1;
5530 else
5531 result = entry1->after_string_p ? -1 : 1;
5533 else if (entry1->priority != entry2->priority)
5535 if (entry1->after_string_p)
5536 /* After-strings sorted in order of decreasing priority. */
5537 result = entry2->priority < entry1->priority ? -1 : 1;
5538 else
5539 /* Before-strings sorted in order of increasing priority. */
5540 result = entry1->priority < entry2->priority ? -1 : 1;
5542 else
5543 result = 0;
5545 return result;
5549 /* Load the vector IT->overlay_strings with overlay strings from IT's
5550 current buffer position, or from CHARPOS if that is > 0. Set
5551 IT->n_overlays to the total number of overlay strings found.
5553 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5554 a time. On entry into load_overlay_strings,
5555 IT->current.overlay_string_index gives the number of overlay
5556 strings that have already been loaded by previous calls to this
5557 function.
5559 IT->add_overlay_start contains an additional overlay start
5560 position to consider for taking overlay strings from, if non-zero.
5561 This position comes into play when the overlay has an `invisible'
5562 property, and both before and after-strings. When we've skipped to
5563 the end of the overlay, because of its `invisible' property, we
5564 nevertheless want its before-string to appear.
5565 IT->add_overlay_start will contain the overlay start position
5566 in this case.
5568 Overlay strings are sorted so that after-string strings come in
5569 front of before-string strings. Within before and after-strings,
5570 strings are sorted by overlay priority. See also function
5571 compare_overlay_entries. */
5573 static void
5574 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5576 Lisp_Object overlay, window, str, invisible;
5577 struct Lisp_Overlay *ov;
5578 ptrdiff_t start, end;
5579 ptrdiff_t size = 20;
5580 ptrdiff_t n = 0, i, j;
5581 int invis_p;
5582 struct overlay_entry *entries = alloca (size * sizeof *entries);
5583 USE_SAFE_ALLOCA;
5585 if (charpos <= 0)
5586 charpos = IT_CHARPOS (*it);
5588 /* Append the overlay string STRING of overlay OVERLAY to vector
5589 `entries' which has size `size' and currently contains `n'
5590 elements. AFTER_P non-zero means STRING is an after-string of
5591 OVERLAY. */
5592 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5593 do \
5595 Lisp_Object priority; \
5597 if (n == size) \
5599 struct overlay_entry *old = entries; \
5600 SAFE_NALLOCA (entries, 2, size); \
5601 memcpy (entries, old, size * sizeof *entries); \
5602 size *= 2; \
5605 entries[n].string = (STRING); \
5606 entries[n].overlay = (OVERLAY); \
5607 priority = Foverlay_get ((OVERLAY), Qpriority); \
5608 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5609 entries[n].after_string_p = (AFTER_P); \
5610 ++n; \
5612 while (0)
5614 /* Process overlay before the overlay center. */
5615 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5617 XSETMISC (overlay, ov);
5618 eassert (OVERLAYP (overlay));
5619 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5620 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5622 if (end < charpos)
5623 break;
5625 /* Skip this overlay if it doesn't start or end at IT's current
5626 position. */
5627 if (end != charpos && start != charpos)
5628 continue;
5630 /* Skip this overlay if it doesn't apply to IT->w. */
5631 window = Foverlay_get (overlay, Qwindow);
5632 if (WINDOWP (window) && XWINDOW (window) != it->w)
5633 continue;
5635 /* If the text ``under'' the overlay is invisible, both before-
5636 and after-strings from this overlay are visible; start and
5637 end position are indistinguishable. */
5638 invisible = Foverlay_get (overlay, Qinvisible);
5639 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5641 /* If overlay has a non-empty before-string, record it. */
5642 if ((start == charpos || (end == charpos && invis_p))
5643 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5644 && SCHARS (str))
5645 RECORD_OVERLAY_STRING (overlay, str, 0);
5647 /* If overlay has a non-empty after-string, record it. */
5648 if ((end == charpos || (start == charpos && invis_p))
5649 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5650 && SCHARS (str))
5651 RECORD_OVERLAY_STRING (overlay, str, 1);
5654 /* Process overlays after the overlay center. */
5655 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5657 XSETMISC (overlay, ov);
5658 eassert (OVERLAYP (overlay));
5659 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5660 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5662 if (start > charpos)
5663 break;
5665 /* Skip this overlay if it doesn't start or end at IT's current
5666 position. */
5667 if (end != charpos && start != charpos)
5668 continue;
5670 /* Skip this overlay if it doesn't apply to IT->w. */
5671 window = Foverlay_get (overlay, Qwindow);
5672 if (WINDOWP (window) && XWINDOW (window) != it->w)
5673 continue;
5675 /* If the text ``under'' the overlay is invisible, it has a zero
5676 dimension, and both before- and after-strings apply. */
5677 invisible = Foverlay_get (overlay, Qinvisible);
5678 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5680 /* If overlay has a non-empty before-string, record it. */
5681 if ((start == charpos || (end == charpos && invis_p))
5682 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5683 && SCHARS (str))
5684 RECORD_OVERLAY_STRING (overlay, str, 0);
5686 /* If overlay has a non-empty after-string, record it. */
5687 if ((end == charpos || (start == charpos && invis_p))
5688 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5689 && SCHARS (str))
5690 RECORD_OVERLAY_STRING (overlay, str, 1);
5693 #undef RECORD_OVERLAY_STRING
5695 /* Sort entries. */
5696 if (n > 1)
5697 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5699 /* Record number of overlay strings, and where we computed it. */
5700 it->n_overlay_strings = n;
5701 it->overlay_strings_charpos = charpos;
5703 /* IT->current.overlay_string_index is the number of overlay strings
5704 that have already been consumed by IT. Copy some of the
5705 remaining overlay strings to IT->overlay_strings. */
5706 i = 0;
5707 j = it->current.overlay_string_index;
5708 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5710 it->overlay_strings[i] = entries[j].string;
5711 it->string_overlays[i++] = entries[j++].overlay;
5714 CHECK_IT (it);
5715 SAFE_FREE ();
5719 /* Get the first chunk of overlay strings at IT's current buffer
5720 position, or at CHARPOS if that is > 0. Value is non-zero if at
5721 least one overlay string was found. */
5723 static int
5724 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5726 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5727 process. This fills IT->overlay_strings with strings, and sets
5728 IT->n_overlay_strings to the total number of strings to process.
5729 IT->pos.overlay_string_index has to be set temporarily to zero
5730 because load_overlay_strings needs this; it must be set to -1
5731 when no overlay strings are found because a zero value would
5732 indicate a position in the first overlay string. */
5733 it->current.overlay_string_index = 0;
5734 load_overlay_strings (it, charpos);
5736 /* If we found overlay strings, set up IT to deliver display
5737 elements from the first one. Otherwise set up IT to deliver
5738 from current_buffer. */
5739 if (it->n_overlay_strings)
5741 /* Make sure we know settings in current_buffer, so that we can
5742 restore meaningful values when we're done with the overlay
5743 strings. */
5744 if (compute_stop_p)
5745 compute_stop_pos (it);
5746 eassert (it->face_id >= 0);
5748 /* Save IT's settings. They are restored after all overlay
5749 strings have been processed. */
5750 eassert (!compute_stop_p || it->sp == 0);
5752 /* When called from handle_stop, there might be an empty display
5753 string loaded. In that case, don't bother saving it. But
5754 don't use this optimization with the bidi iterator, since we
5755 need the corresponding pop_it call to resync the bidi
5756 iterator's position with IT's position, after we are done
5757 with the overlay strings. (The corresponding call to pop_it
5758 in case of an empty display string is in
5759 next_overlay_string.) */
5760 if (!(!it->bidi_p
5761 && STRINGP (it->string) && !SCHARS (it->string)))
5762 push_it (it, NULL);
5764 /* Set up IT to deliver display elements from the first overlay
5765 string. */
5766 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5767 it->string = it->overlay_strings[0];
5768 it->from_overlay = Qnil;
5769 it->stop_charpos = 0;
5770 eassert (STRINGP (it->string));
5771 it->end_charpos = SCHARS (it->string);
5772 it->prev_stop = 0;
5773 it->base_level_stop = 0;
5774 it->multibyte_p = STRING_MULTIBYTE (it->string);
5775 it->method = GET_FROM_STRING;
5776 it->from_disp_prop_p = 0;
5778 /* Force paragraph direction to be that of the parent
5779 buffer. */
5780 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5781 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5782 else
5783 it->paragraph_embedding = L2R;
5785 /* Set up the bidi iterator for this overlay string. */
5786 if (it->bidi_p)
5788 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5790 it->bidi_it.string.lstring = it->string;
5791 it->bidi_it.string.s = NULL;
5792 it->bidi_it.string.schars = SCHARS (it->string);
5793 it->bidi_it.string.bufpos = pos;
5794 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5795 it->bidi_it.string.unibyte = !it->multibyte_p;
5796 it->bidi_it.w = it->w;
5797 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5799 return 1;
5802 it->current.overlay_string_index = -1;
5803 return 0;
5806 static int
5807 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5809 it->string = Qnil;
5810 it->method = GET_FROM_BUFFER;
5812 (void) get_overlay_strings_1 (it, charpos, 1);
5814 CHECK_IT (it);
5816 /* Value is non-zero if we found at least one overlay string. */
5817 return STRINGP (it->string);
5822 /***********************************************************************
5823 Saving and restoring state
5824 ***********************************************************************/
5826 /* Save current settings of IT on IT->stack. Called, for example,
5827 before setting up IT for an overlay string, to be able to restore
5828 IT's settings to what they were after the overlay string has been
5829 processed. If POSITION is non-NULL, it is the position to save on
5830 the stack instead of IT->position. */
5832 static void
5833 push_it (struct it *it, struct text_pos *position)
5835 struct iterator_stack_entry *p;
5837 eassert (it->sp < IT_STACK_SIZE);
5838 p = it->stack + it->sp;
5840 p->stop_charpos = it->stop_charpos;
5841 p->prev_stop = it->prev_stop;
5842 p->base_level_stop = it->base_level_stop;
5843 p->cmp_it = it->cmp_it;
5844 eassert (it->face_id >= 0);
5845 p->face_id = it->face_id;
5846 p->string = it->string;
5847 p->method = it->method;
5848 p->from_overlay = it->from_overlay;
5849 switch (p->method)
5851 case GET_FROM_IMAGE:
5852 p->u.image.object = it->object;
5853 p->u.image.image_id = it->image_id;
5854 p->u.image.slice = it->slice;
5855 break;
5856 case GET_FROM_STRETCH:
5857 p->u.stretch.object = it->object;
5858 break;
5860 p->position = position ? *position : it->position;
5861 p->current = it->current;
5862 p->end_charpos = it->end_charpos;
5863 p->string_nchars = it->string_nchars;
5864 p->area = it->area;
5865 p->multibyte_p = it->multibyte_p;
5866 p->avoid_cursor_p = it->avoid_cursor_p;
5867 p->space_width = it->space_width;
5868 p->font_height = it->font_height;
5869 p->voffset = it->voffset;
5870 p->string_from_display_prop_p = it->string_from_display_prop_p;
5871 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5872 p->display_ellipsis_p = 0;
5873 p->line_wrap = it->line_wrap;
5874 p->bidi_p = it->bidi_p;
5875 p->paragraph_embedding = it->paragraph_embedding;
5876 p->from_disp_prop_p = it->from_disp_prop_p;
5877 ++it->sp;
5879 /* Save the state of the bidi iterator as well. */
5880 if (it->bidi_p)
5881 bidi_push_it (&it->bidi_it);
5884 static void
5885 iterate_out_of_display_property (struct it *it)
5887 int buffer_p = !STRINGP (it->string);
5888 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5889 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5891 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5893 /* Maybe initialize paragraph direction. If we are at the beginning
5894 of a new paragraph, next_element_from_buffer may not have a
5895 chance to do that. */
5896 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5897 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5898 /* prev_stop can be zero, so check against BEGV as well. */
5899 while (it->bidi_it.charpos >= bob
5900 && it->prev_stop <= it->bidi_it.charpos
5901 && it->bidi_it.charpos < CHARPOS (it->position)
5902 && it->bidi_it.charpos < eob)
5903 bidi_move_to_visually_next (&it->bidi_it);
5904 /* Record the stop_pos we just crossed, for when we cross it
5905 back, maybe. */
5906 if (it->bidi_it.charpos > CHARPOS (it->position))
5907 it->prev_stop = CHARPOS (it->position);
5908 /* If we ended up not where pop_it put us, resync IT's
5909 positional members with the bidi iterator. */
5910 if (it->bidi_it.charpos != CHARPOS (it->position))
5911 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5912 if (buffer_p)
5913 it->current.pos = it->position;
5914 else
5915 it->current.string_pos = it->position;
5918 /* Restore IT's settings from IT->stack. Called, for example, when no
5919 more overlay strings must be processed, and we return to delivering
5920 display elements from a buffer, or when the end of a string from a
5921 `display' property is reached and we return to delivering display
5922 elements from an overlay string, or from a buffer. */
5924 static void
5925 pop_it (struct it *it)
5927 struct iterator_stack_entry *p;
5928 int from_display_prop = it->from_disp_prop_p;
5930 eassert (it->sp > 0);
5931 --it->sp;
5932 p = it->stack + it->sp;
5933 it->stop_charpos = p->stop_charpos;
5934 it->prev_stop = p->prev_stop;
5935 it->base_level_stop = p->base_level_stop;
5936 it->cmp_it = p->cmp_it;
5937 it->face_id = p->face_id;
5938 it->current = p->current;
5939 it->position = p->position;
5940 it->string = p->string;
5941 it->from_overlay = p->from_overlay;
5942 if (NILP (it->string))
5943 SET_TEXT_POS (it->current.string_pos, -1, -1);
5944 it->method = p->method;
5945 switch (it->method)
5947 case GET_FROM_IMAGE:
5948 it->image_id = p->u.image.image_id;
5949 it->object = p->u.image.object;
5950 it->slice = p->u.image.slice;
5951 break;
5952 case GET_FROM_STRETCH:
5953 it->object = p->u.stretch.object;
5954 break;
5955 case GET_FROM_BUFFER:
5956 it->object = it->w->contents;
5957 break;
5958 case GET_FROM_STRING:
5959 it->object = it->string;
5960 break;
5961 case GET_FROM_DISPLAY_VECTOR:
5962 if (it->s)
5963 it->method = GET_FROM_C_STRING;
5964 else if (STRINGP (it->string))
5965 it->method = GET_FROM_STRING;
5966 else
5968 it->method = GET_FROM_BUFFER;
5969 it->object = it->w->contents;
5972 it->end_charpos = p->end_charpos;
5973 it->string_nchars = p->string_nchars;
5974 it->area = p->area;
5975 it->multibyte_p = p->multibyte_p;
5976 it->avoid_cursor_p = p->avoid_cursor_p;
5977 it->space_width = p->space_width;
5978 it->font_height = p->font_height;
5979 it->voffset = p->voffset;
5980 it->string_from_display_prop_p = p->string_from_display_prop_p;
5981 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5982 it->line_wrap = p->line_wrap;
5983 it->bidi_p = p->bidi_p;
5984 it->paragraph_embedding = p->paragraph_embedding;
5985 it->from_disp_prop_p = p->from_disp_prop_p;
5986 if (it->bidi_p)
5988 bidi_pop_it (&it->bidi_it);
5989 /* Bidi-iterate until we get out of the portion of text, if any,
5990 covered by a `display' text property or by an overlay with
5991 `display' property. (We cannot just jump there, because the
5992 internal coherency of the bidi iterator state can not be
5993 preserved across such jumps.) We also must determine the
5994 paragraph base direction if the overlay we just processed is
5995 at the beginning of a new paragraph. */
5996 if (from_display_prop
5997 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5998 iterate_out_of_display_property (it);
6000 eassert ((BUFFERP (it->object)
6001 && IT_CHARPOS (*it) == it->bidi_it.charpos
6002 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6003 || (STRINGP (it->object)
6004 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6005 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6006 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6012 /***********************************************************************
6013 Moving over lines
6014 ***********************************************************************/
6016 /* Set IT's current position to the previous line start. */
6018 static void
6019 back_to_previous_line_start (struct it *it)
6021 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6023 DEC_BOTH (cp, bp);
6024 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6028 /* Move IT to the next line start.
6030 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6031 we skipped over part of the text (as opposed to moving the iterator
6032 continuously over the text). Otherwise, don't change the value
6033 of *SKIPPED_P.
6035 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6036 iterator on the newline, if it was found.
6038 Newlines may come from buffer text, overlay strings, or strings
6039 displayed via the `display' property. That's the reason we can't
6040 simply use find_newline_no_quit.
6042 Note that this function may not skip over invisible text that is so
6043 because of text properties and immediately follows a newline. If
6044 it would, function reseat_at_next_visible_line_start, when called
6045 from set_iterator_to_next, would effectively make invisible
6046 characters following a newline part of the wrong glyph row, which
6047 leads to wrong cursor motion. */
6049 static int
6050 forward_to_next_line_start (struct it *it, int *skipped_p,
6051 struct bidi_it *bidi_it_prev)
6053 ptrdiff_t old_selective;
6054 int newline_found_p, n;
6055 const int MAX_NEWLINE_DISTANCE = 500;
6057 /* If already on a newline, just consume it to avoid unintended
6058 skipping over invisible text below. */
6059 if (it->what == IT_CHARACTER
6060 && it->c == '\n'
6061 && CHARPOS (it->position) == IT_CHARPOS (*it))
6063 if (it->bidi_p && bidi_it_prev)
6064 *bidi_it_prev = it->bidi_it;
6065 set_iterator_to_next (it, 0);
6066 it->c = 0;
6067 return 1;
6070 /* Don't handle selective display in the following. It's (a)
6071 unnecessary because it's done by the caller, and (b) leads to an
6072 infinite recursion because next_element_from_ellipsis indirectly
6073 calls this function. */
6074 old_selective = it->selective;
6075 it->selective = 0;
6077 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6078 from buffer text. */
6079 for (n = newline_found_p = 0;
6080 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6081 n += STRINGP (it->string) ? 0 : 1)
6083 if (!get_next_display_element (it))
6084 return 0;
6085 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6086 if (newline_found_p && it->bidi_p && bidi_it_prev)
6087 *bidi_it_prev = it->bidi_it;
6088 set_iterator_to_next (it, 0);
6091 /* If we didn't find a newline near enough, see if we can use a
6092 short-cut. */
6093 if (!newline_found_p)
6095 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6096 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6097 1, &bytepos);
6098 Lisp_Object pos;
6100 eassert (!STRINGP (it->string));
6102 /* If there isn't any `display' property in sight, and no
6103 overlays, we can just use the position of the newline in
6104 buffer text. */
6105 if (it->stop_charpos >= limit
6106 || ((pos = Fnext_single_property_change (make_number (start),
6107 Qdisplay, Qnil,
6108 make_number (limit)),
6109 NILP (pos))
6110 && next_overlay_change (start) == ZV))
6112 if (!it->bidi_p)
6114 IT_CHARPOS (*it) = limit;
6115 IT_BYTEPOS (*it) = bytepos;
6117 else
6119 struct bidi_it bprev;
6121 /* Help bidi.c avoid expensive searches for display
6122 properties and overlays, by telling it that there are
6123 none up to `limit'. */
6124 if (it->bidi_it.disp_pos < limit)
6126 it->bidi_it.disp_pos = limit;
6127 it->bidi_it.disp_prop = 0;
6129 do {
6130 bprev = it->bidi_it;
6131 bidi_move_to_visually_next (&it->bidi_it);
6132 } while (it->bidi_it.charpos != limit);
6133 IT_CHARPOS (*it) = limit;
6134 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6135 if (bidi_it_prev)
6136 *bidi_it_prev = bprev;
6138 *skipped_p = newline_found_p = true;
6140 else
6142 while (get_next_display_element (it)
6143 && !newline_found_p)
6145 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
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);
6153 it->selective = old_selective;
6154 return newline_found_p;
6158 /* Set IT's current position to the previous visible line start. Skip
6159 invisible text that is so either due to text properties or due to
6160 selective display. Caution: this does not change IT->current_x and
6161 IT->hpos. */
6163 static void
6164 back_to_previous_visible_line_start (struct it *it)
6166 while (IT_CHARPOS (*it) > BEGV)
6168 back_to_previous_line_start (it);
6170 if (IT_CHARPOS (*it) <= BEGV)
6171 break;
6173 /* If selective > 0, then lines indented more than its value are
6174 invisible. */
6175 if (it->selective > 0
6176 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6177 it->selective))
6178 continue;
6180 /* Check the newline before point for invisibility. */
6182 Lisp_Object prop;
6183 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6184 Qinvisible, it->window);
6185 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6186 continue;
6189 if (IT_CHARPOS (*it) <= BEGV)
6190 break;
6193 struct it it2;
6194 void *it2data = NULL;
6195 ptrdiff_t pos;
6196 ptrdiff_t beg, end;
6197 Lisp_Object val, overlay;
6199 SAVE_IT (it2, *it, it2data);
6201 /* If newline is part of a composition, continue from start of composition */
6202 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6203 && beg < IT_CHARPOS (*it))
6204 goto replaced;
6206 /* If newline is replaced by a display property, find start of overlay
6207 or interval and continue search from that point. */
6208 pos = --IT_CHARPOS (it2);
6209 --IT_BYTEPOS (it2);
6210 it2.sp = 0;
6211 bidi_unshelve_cache (NULL, 0);
6212 it2.string_from_display_prop_p = 0;
6213 it2.from_disp_prop_p = 0;
6214 if (handle_display_prop (&it2) == HANDLED_RETURN
6215 && !NILP (val = get_char_property_and_overlay
6216 (make_number (pos), Qdisplay, Qnil, &overlay))
6217 && (OVERLAYP (overlay)
6218 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6219 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6221 RESTORE_IT (it, it, it2data);
6222 goto replaced;
6225 /* Newline is not replaced by anything -- so we are done. */
6226 RESTORE_IT (it, it, it2data);
6227 break;
6229 replaced:
6230 if (beg < BEGV)
6231 beg = BEGV;
6232 IT_CHARPOS (*it) = beg;
6233 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6237 it->continuation_lines_width = 0;
6239 eassert (IT_CHARPOS (*it) >= BEGV);
6240 eassert (IT_CHARPOS (*it) == BEGV
6241 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6242 CHECK_IT (it);
6246 /* Reseat iterator IT at the previous visible line start. Skip
6247 invisible text that is so either due to text properties or due to
6248 selective display. At the end, update IT's overlay information,
6249 face information etc. */
6251 void
6252 reseat_at_previous_visible_line_start (struct it *it)
6254 back_to_previous_visible_line_start (it);
6255 reseat (it, it->current.pos, 1);
6256 CHECK_IT (it);
6260 /* Reseat iterator IT on the next visible line start in the current
6261 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6262 preceding the line start. Skip over invisible text that is so
6263 because of selective display. Compute faces, overlays etc at the
6264 new position. Note that this function does not skip over text that
6265 is invisible because of text properties. */
6267 static void
6268 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6270 int newline_found_p, skipped_p = 0;
6271 struct bidi_it bidi_it_prev;
6273 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6275 /* Skip over lines that are invisible because they are indented
6276 more than the value of IT->selective. */
6277 if (it->selective > 0)
6278 while (IT_CHARPOS (*it) < ZV
6279 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6280 it->selective))
6282 eassert (IT_BYTEPOS (*it) == BEGV
6283 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6284 newline_found_p =
6285 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6288 /* Position on the newline if that's what's requested. */
6289 if (on_newline_p && newline_found_p)
6291 if (STRINGP (it->string))
6293 if (IT_STRING_CHARPOS (*it) > 0)
6295 if (!it->bidi_p)
6297 --IT_STRING_CHARPOS (*it);
6298 --IT_STRING_BYTEPOS (*it);
6300 else
6302 /* We need to restore the bidi iterator to the state
6303 it had on the newline, and resync the IT's
6304 position with that. */
6305 it->bidi_it = bidi_it_prev;
6306 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6307 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6311 else if (IT_CHARPOS (*it) > BEGV)
6313 if (!it->bidi_p)
6315 --IT_CHARPOS (*it);
6316 --IT_BYTEPOS (*it);
6318 else
6320 /* We need to restore the bidi iterator to the state it
6321 had on the newline and resync IT with that. */
6322 it->bidi_it = bidi_it_prev;
6323 IT_CHARPOS (*it) = it->bidi_it.charpos;
6324 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6326 reseat (it, it->current.pos, 0);
6329 else if (skipped_p)
6330 reseat (it, it->current.pos, 0);
6332 CHECK_IT (it);
6337 /***********************************************************************
6338 Changing an iterator's position
6339 ***********************************************************************/
6341 /* Change IT's current position to POS in current_buffer. If FORCE_P
6342 is non-zero, always check for text properties at the new position.
6343 Otherwise, text properties are only looked up if POS >=
6344 IT->check_charpos of a property. */
6346 static void
6347 reseat (struct it *it, struct text_pos pos, int force_p)
6349 ptrdiff_t original_pos = IT_CHARPOS (*it);
6351 reseat_1 (it, pos, 0);
6353 /* Determine where to check text properties. Avoid doing it
6354 where possible because text property lookup is very expensive. */
6355 if (force_p
6356 || CHARPOS (pos) > it->stop_charpos
6357 || CHARPOS (pos) < original_pos)
6359 if (it->bidi_p)
6361 /* For bidi iteration, we need to prime prev_stop and
6362 base_level_stop with our best estimations. */
6363 /* Implementation note: Of course, POS is not necessarily a
6364 stop position, so assigning prev_pos to it is a lie; we
6365 should have called compute_stop_backwards. However, if
6366 the current buffer does not include any R2L characters,
6367 that call would be a waste of cycles, because the
6368 iterator will never move back, and thus never cross this
6369 "fake" stop position. So we delay that backward search
6370 until the time we really need it, in next_element_from_buffer. */
6371 if (CHARPOS (pos) != it->prev_stop)
6372 it->prev_stop = CHARPOS (pos);
6373 if (CHARPOS (pos) < it->base_level_stop)
6374 it->base_level_stop = 0; /* meaning it's unknown */
6375 handle_stop (it);
6377 else
6379 handle_stop (it);
6380 it->prev_stop = it->base_level_stop = 0;
6385 CHECK_IT (it);
6389 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6390 IT->stop_pos to POS, also. */
6392 static void
6393 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6395 /* Don't call this function when scanning a C string. */
6396 eassert (it->s == NULL);
6398 /* POS must be a reasonable value. */
6399 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6401 it->current.pos = it->position = pos;
6402 it->end_charpos = ZV;
6403 it->dpvec = NULL;
6404 it->current.dpvec_index = -1;
6405 it->current.overlay_string_index = -1;
6406 IT_STRING_CHARPOS (*it) = -1;
6407 IT_STRING_BYTEPOS (*it) = -1;
6408 it->string = Qnil;
6409 it->method = GET_FROM_BUFFER;
6410 it->object = it->w->contents;
6411 it->area = TEXT_AREA;
6412 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6413 it->sp = 0;
6414 it->string_from_display_prop_p = 0;
6415 it->string_from_prefix_prop_p = 0;
6417 it->from_disp_prop_p = 0;
6418 it->face_before_selective_p = 0;
6419 if (it->bidi_p)
6421 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6422 &it->bidi_it);
6423 bidi_unshelve_cache (NULL, 0);
6424 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6425 it->bidi_it.string.s = NULL;
6426 it->bidi_it.string.lstring = Qnil;
6427 it->bidi_it.string.bufpos = 0;
6428 it->bidi_it.string.from_disp_str = 0;
6429 it->bidi_it.string.unibyte = 0;
6430 it->bidi_it.w = it->w;
6433 if (set_stop_p)
6435 it->stop_charpos = CHARPOS (pos);
6436 it->base_level_stop = CHARPOS (pos);
6438 /* This make the information stored in it->cmp_it invalidate. */
6439 it->cmp_it.id = -1;
6443 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6444 If S is non-null, it is a C string to iterate over. Otherwise,
6445 STRING gives a Lisp string to iterate over.
6447 If PRECISION > 0, don't return more then PRECISION number of
6448 characters from the string.
6450 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6451 characters have been returned. FIELD_WIDTH < 0 means an infinite
6452 field width.
6454 MULTIBYTE = 0 means disable processing of multibyte characters,
6455 MULTIBYTE > 0 means enable it,
6456 MULTIBYTE < 0 means use IT->multibyte_p.
6458 IT must be initialized via a prior call to init_iterator before
6459 calling this function. */
6461 static void
6462 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6463 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6464 int multibyte)
6466 /* No text property checks performed by default, but see below. */
6467 it->stop_charpos = -1;
6469 /* Set iterator position and end position. */
6470 memset (&it->current, 0, sizeof it->current);
6471 it->current.overlay_string_index = -1;
6472 it->current.dpvec_index = -1;
6473 eassert (charpos >= 0);
6475 /* If STRING is specified, use its multibyteness, otherwise use the
6476 setting of MULTIBYTE, if specified. */
6477 if (multibyte >= 0)
6478 it->multibyte_p = multibyte > 0;
6480 /* Bidirectional reordering of strings is controlled by the default
6481 value of bidi-display-reordering. Don't try to reorder while
6482 loading loadup.el, as the necessary character property tables are
6483 not yet available. */
6484 it->bidi_p =
6485 NILP (Vpurify_flag)
6486 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6488 if (s == NULL)
6490 eassert (STRINGP (string));
6491 it->string = string;
6492 it->s = NULL;
6493 it->end_charpos = it->string_nchars = SCHARS (string);
6494 it->method = GET_FROM_STRING;
6495 it->current.string_pos = string_pos (charpos, string);
6497 if (it->bidi_p)
6499 it->bidi_it.string.lstring = string;
6500 it->bidi_it.string.s = NULL;
6501 it->bidi_it.string.schars = it->end_charpos;
6502 it->bidi_it.string.bufpos = 0;
6503 it->bidi_it.string.from_disp_str = 0;
6504 it->bidi_it.string.unibyte = !it->multibyte_p;
6505 it->bidi_it.w = it->w;
6506 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6507 FRAME_WINDOW_P (it->f), &it->bidi_it);
6510 else
6512 it->s = (const unsigned char *) s;
6513 it->string = Qnil;
6515 /* Note that we use IT->current.pos, not it->current.string_pos,
6516 for displaying C strings. */
6517 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6518 if (it->multibyte_p)
6520 it->current.pos = c_string_pos (charpos, s, 1);
6521 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6523 else
6525 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6526 it->end_charpos = it->string_nchars = strlen (s);
6529 if (it->bidi_p)
6531 it->bidi_it.string.lstring = Qnil;
6532 it->bidi_it.string.s = (const unsigned char *) s;
6533 it->bidi_it.string.schars = it->end_charpos;
6534 it->bidi_it.string.bufpos = 0;
6535 it->bidi_it.string.from_disp_str = 0;
6536 it->bidi_it.string.unibyte = !it->multibyte_p;
6537 it->bidi_it.w = it->w;
6538 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6539 &it->bidi_it);
6541 it->method = GET_FROM_C_STRING;
6544 /* PRECISION > 0 means don't return more than PRECISION characters
6545 from the string. */
6546 if (precision > 0 && it->end_charpos - charpos > precision)
6548 it->end_charpos = it->string_nchars = charpos + precision;
6549 if (it->bidi_p)
6550 it->bidi_it.string.schars = it->end_charpos;
6553 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6554 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6555 FIELD_WIDTH < 0 means infinite field width. This is useful for
6556 padding with `-' at the end of a mode line. */
6557 if (field_width < 0)
6558 field_width = INFINITY;
6559 /* Implementation note: We deliberately don't enlarge
6560 it->bidi_it.string.schars here to fit it->end_charpos, because
6561 the bidi iterator cannot produce characters out of thin air. */
6562 if (field_width > it->end_charpos - charpos)
6563 it->end_charpos = charpos + field_width;
6565 /* Use the standard display table for displaying strings. */
6566 if (DISP_TABLE_P (Vstandard_display_table))
6567 it->dp = XCHAR_TABLE (Vstandard_display_table);
6569 it->stop_charpos = charpos;
6570 it->prev_stop = charpos;
6571 it->base_level_stop = 0;
6572 if (it->bidi_p)
6574 it->bidi_it.first_elt = 1;
6575 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6576 it->bidi_it.disp_pos = -1;
6578 if (s == NULL && it->multibyte_p)
6580 ptrdiff_t endpos = SCHARS (it->string);
6581 if (endpos > it->end_charpos)
6582 endpos = it->end_charpos;
6583 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6584 it->string);
6586 CHECK_IT (it);
6591 /***********************************************************************
6592 Iteration
6593 ***********************************************************************/
6595 /* Map enum it_method value to corresponding next_element_from_* function. */
6597 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6599 next_element_from_buffer,
6600 next_element_from_display_vector,
6601 next_element_from_string,
6602 next_element_from_c_string,
6603 next_element_from_image,
6604 next_element_from_stretch
6607 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6610 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6611 (possibly with the following characters). */
6613 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6614 ((IT)->cmp_it.id >= 0 \
6615 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6616 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6617 END_CHARPOS, (IT)->w, \
6618 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6619 (IT)->string)))
6622 /* Lookup the char-table Vglyphless_char_display for character C (-1
6623 if we want information for no-font case), and return the display
6624 method symbol. By side-effect, update it->what and
6625 it->glyphless_method. This function is called from
6626 get_next_display_element for each character element, and from
6627 x_produce_glyphs when no suitable font was found. */
6629 Lisp_Object
6630 lookup_glyphless_char_display (int c, struct it *it)
6632 Lisp_Object glyphless_method = Qnil;
6634 if (CHAR_TABLE_P (Vglyphless_char_display)
6635 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6637 if (c >= 0)
6639 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6640 if (CONSP (glyphless_method))
6641 glyphless_method = FRAME_WINDOW_P (it->f)
6642 ? XCAR (glyphless_method)
6643 : XCDR (glyphless_method);
6645 else
6646 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6649 retry:
6650 if (NILP (glyphless_method))
6652 if (c >= 0)
6653 /* The default is to display the character by a proper font. */
6654 return Qnil;
6655 /* The default for the no-font case is to display an empty box. */
6656 glyphless_method = Qempty_box;
6658 if (EQ (glyphless_method, Qzero_width))
6660 if (c >= 0)
6661 return glyphless_method;
6662 /* This method can't be used for the no-font case. */
6663 glyphless_method = Qempty_box;
6665 if (EQ (glyphless_method, Qthin_space))
6666 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6667 else if (EQ (glyphless_method, Qempty_box))
6668 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6669 else if (EQ (glyphless_method, Qhex_code))
6670 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6671 else if (STRINGP (glyphless_method))
6672 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6673 else
6675 /* Invalid value. We use the default method. */
6676 glyphless_method = Qnil;
6677 goto retry;
6679 it->what = IT_GLYPHLESS;
6680 return glyphless_method;
6683 /* Merge escape glyph face and cache the result. */
6685 static struct frame *last_escape_glyph_frame = NULL;
6686 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6687 static int last_escape_glyph_merged_face_id = 0;
6689 static int
6690 merge_escape_glyph_face (struct it *it)
6692 int face_id;
6694 if (it->f == last_escape_glyph_frame
6695 && it->face_id == last_escape_glyph_face_id)
6696 face_id = last_escape_glyph_merged_face_id;
6697 else
6699 /* Merge the `escape-glyph' face into the current face. */
6700 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6701 last_escape_glyph_frame = it->f;
6702 last_escape_glyph_face_id = it->face_id;
6703 last_escape_glyph_merged_face_id = face_id;
6705 return face_id;
6708 /* Likewise for glyphless glyph face. */
6710 static struct frame *last_glyphless_glyph_frame = NULL;
6711 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6712 static int last_glyphless_glyph_merged_face_id = 0;
6715 merge_glyphless_glyph_face (struct it *it)
6717 int face_id;
6719 if (it->f == last_glyphless_glyph_frame
6720 && it->face_id == last_glyphless_glyph_face_id)
6721 face_id = last_glyphless_glyph_merged_face_id;
6722 else
6724 /* Merge the `glyphless-char' face into the current face. */
6725 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6726 last_glyphless_glyph_frame = it->f;
6727 last_glyphless_glyph_face_id = it->face_id;
6728 last_glyphless_glyph_merged_face_id = face_id;
6730 return face_id;
6733 /* Load IT's display element fields with information about the next
6734 display element from the current position of IT. Value is zero if
6735 end of buffer (or C string) is reached. */
6737 static int
6738 get_next_display_element (struct it *it)
6740 /* Non-zero means that we found a display element. Zero means that
6741 we hit the end of what we iterate over. Performance note: the
6742 function pointer `method' used here turns out to be faster than
6743 using a sequence of if-statements. */
6744 int success_p;
6746 get_next:
6747 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6749 if (it->what == IT_CHARACTER)
6751 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6752 and only if (a) the resolved directionality of that character
6753 is R..." */
6754 /* FIXME: Do we need an exception for characters from display
6755 tables? */
6756 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6757 it->c = bidi_mirror_char (it->c);
6758 /* Map via display table or translate control characters.
6759 IT->c, IT->len etc. have been set to the next character by
6760 the function call above. If we have a display table, and it
6761 contains an entry for IT->c, translate it. Don't do this if
6762 IT->c itself comes from a display table, otherwise we could
6763 end up in an infinite recursion. (An alternative could be to
6764 count the recursion depth of this function and signal an
6765 error when a certain maximum depth is reached.) Is it worth
6766 it? */
6767 if (success_p && it->dpvec == NULL)
6769 Lisp_Object dv;
6770 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6771 int nonascii_space_p = 0;
6772 int nonascii_hyphen_p = 0;
6773 int c = it->c; /* This is the character to display. */
6775 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6777 eassert (SINGLE_BYTE_CHAR_P (c));
6778 if (unibyte_display_via_language_environment)
6780 c = DECODE_CHAR (unibyte, c);
6781 if (c < 0)
6782 c = BYTE8_TO_CHAR (it->c);
6784 else
6785 c = BYTE8_TO_CHAR (it->c);
6788 if (it->dp
6789 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6790 VECTORP (dv)))
6792 struct Lisp_Vector *v = XVECTOR (dv);
6794 /* Return the first character from the display table
6795 entry, if not empty. If empty, don't display the
6796 current character. */
6797 if (v->header.size)
6799 it->dpvec_char_len = it->len;
6800 it->dpvec = v->contents;
6801 it->dpend = v->contents + v->header.size;
6802 it->current.dpvec_index = 0;
6803 it->dpvec_face_id = -1;
6804 it->saved_face_id = it->face_id;
6805 it->method = GET_FROM_DISPLAY_VECTOR;
6806 it->ellipsis_p = 0;
6808 else
6810 set_iterator_to_next (it, 0);
6812 goto get_next;
6815 if (! NILP (lookup_glyphless_char_display (c, it)))
6817 if (it->what == IT_GLYPHLESS)
6818 goto done;
6819 /* Don't display this character. */
6820 set_iterator_to_next (it, 0);
6821 goto get_next;
6824 /* If `nobreak-char-display' is non-nil, we display
6825 non-ASCII spaces and hyphens specially. */
6826 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6828 if (c == 0xA0)
6829 nonascii_space_p = true;
6830 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6831 nonascii_hyphen_p = true;
6834 /* Translate control characters into `\003' or `^C' form.
6835 Control characters coming from a display table entry are
6836 currently not translated because we use IT->dpvec to hold
6837 the translation. This could easily be changed but I
6838 don't believe that it is worth doing.
6840 The characters handled by `nobreak-char-display' must be
6841 translated too.
6843 Non-printable characters and raw-byte characters are also
6844 translated to octal form. */
6845 if (((c < ' ' || c == 127) /* ASCII control chars. */
6846 ? (it->area != TEXT_AREA
6847 /* In mode line, treat \n, \t like other crl chars. */
6848 || (c != '\t'
6849 && it->glyph_row
6850 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6851 || (c != '\n' && c != '\t'))
6852 : (nonascii_space_p
6853 || nonascii_hyphen_p
6854 || CHAR_BYTE8_P (c)
6855 || ! CHAR_PRINTABLE_P (c))))
6857 /* C is a control character, non-ASCII space/hyphen,
6858 raw-byte, or a non-printable character which must be
6859 displayed either as '\003' or as `^C' where the '\\'
6860 and '^' can be defined in the display table. Fill
6861 IT->ctl_chars with glyphs for what we have to
6862 display. Then, set IT->dpvec to these glyphs. */
6863 Lisp_Object gc;
6864 int ctl_len;
6865 int face_id;
6866 int lface_id = 0;
6867 int escape_glyph;
6869 /* Handle control characters with ^. */
6871 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6873 int g;
6875 g = '^'; /* default glyph for Control */
6876 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6877 if (it->dp
6878 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6880 g = GLYPH_CODE_CHAR (gc);
6881 lface_id = GLYPH_CODE_FACE (gc);
6884 face_id = (lface_id
6885 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6886 : merge_escape_glyph_face (it));
6888 XSETINT (it->ctl_chars[0], g);
6889 XSETINT (it->ctl_chars[1], c ^ 0100);
6890 ctl_len = 2;
6891 goto display_control;
6894 /* Handle non-ascii space in the mode where it only gets
6895 highlighting. */
6897 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6899 /* Merge `nobreak-space' into the current face. */
6900 face_id = merge_faces (it->f, Qnobreak_space, 0,
6901 it->face_id);
6902 XSETINT (it->ctl_chars[0], ' ');
6903 ctl_len = 1;
6904 goto display_control;
6907 /* Handle sequences that start with the "escape glyph". */
6909 /* the default escape glyph is \. */
6910 escape_glyph = '\\';
6912 if (it->dp
6913 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6915 escape_glyph = GLYPH_CODE_CHAR (gc);
6916 lface_id = GLYPH_CODE_FACE (gc);
6919 face_id = (lface_id
6920 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6921 : merge_escape_glyph_face (it));
6923 /* Draw non-ASCII hyphen with just highlighting: */
6925 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6927 XSETINT (it->ctl_chars[0], '-');
6928 ctl_len = 1;
6929 goto display_control;
6932 /* Draw non-ASCII space/hyphen with escape glyph: */
6934 if (nonascii_space_p || nonascii_hyphen_p)
6936 XSETINT (it->ctl_chars[0], escape_glyph);
6937 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6938 ctl_len = 2;
6939 goto display_control;
6943 char str[10];
6944 int len, i;
6946 if (CHAR_BYTE8_P (c))
6947 /* Display \200 instead of \17777600. */
6948 c = CHAR_TO_BYTE8 (c);
6949 len = sprintf (str, "%03o", c);
6951 XSETINT (it->ctl_chars[0], escape_glyph);
6952 for (i = 0; i < len; i++)
6953 XSETINT (it->ctl_chars[i + 1], str[i]);
6954 ctl_len = len + 1;
6957 display_control:
6958 /* Set up IT->dpvec and return first character from it. */
6959 it->dpvec_char_len = it->len;
6960 it->dpvec = it->ctl_chars;
6961 it->dpend = it->dpvec + ctl_len;
6962 it->current.dpvec_index = 0;
6963 it->dpvec_face_id = face_id;
6964 it->saved_face_id = it->face_id;
6965 it->method = GET_FROM_DISPLAY_VECTOR;
6966 it->ellipsis_p = 0;
6967 goto get_next;
6969 it->char_to_display = c;
6971 else if (success_p)
6973 it->char_to_display = it->c;
6977 #ifdef HAVE_WINDOW_SYSTEM
6978 /* Adjust face id for a multibyte character. There are no multibyte
6979 character in unibyte text. */
6980 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6981 && it->multibyte_p
6982 && success_p
6983 && FRAME_WINDOW_P (it->f))
6985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6987 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6989 /* Automatic composition with glyph-string. */
6990 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6992 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6994 else
6996 ptrdiff_t pos = (it->s ? -1
6997 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6998 : IT_CHARPOS (*it));
6999 int c;
7001 if (it->what == IT_CHARACTER)
7002 c = it->char_to_display;
7003 else
7005 struct composition *cmp = composition_table[it->cmp_it.id];
7006 int i;
7008 c = ' ';
7009 for (i = 0; i < cmp->glyph_len; i++)
7010 /* TAB in a composition means display glyphs with
7011 padding space on the left or right. */
7012 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7013 break;
7015 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7018 #endif /* HAVE_WINDOW_SYSTEM */
7020 done:
7021 /* Is this character the last one of a run of characters with
7022 box? If yes, set IT->end_of_box_run_p to 1. */
7023 if (it->face_box_p
7024 && it->s == NULL)
7026 if (it->method == GET_FROM_STRING && it->sp)
7028 int face_id = underlying_face_id (it);
7029 struct face *face = FACE_FROM_ID (it->f, face_id);
7031 if (face)
7033 if (face->box == FACE_NO_BOX)
7035 /* If the box comes from face properties in a
7036 display string, check faces in that string. */
7037 int string_face_id = face_after_it_pos (it);
7038 it->end_of_box_run_p
7039 = (FACE_FROM_ID (it->f, string_face_id)->box
7040 == FACE_NO_BOX);
7042 /* Otherwise, the box comes from the underlying face.
7043 If this is the last string character displayed, check
7044 the next buffer location. */
7045 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7046 && (it->current.overlay_string_index
7047 == it->n_overlay_strings - 1))
7049 ptrdiff_t ignore;
7050 int next_face_id;
7051 struct text_pos pos = it->current.pos;
7052 INC_TEXT_POS (pos, it->multibyte_p);
7054 next_face_id = face_at_buffer_position
7055 (it->w, CHARPOS (pos), &ignore,
7056 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7057 -1);
7058 it->end_of_box_run_p
7059 = (FACE_FROM_ID (it->f, next_face_id)->box
7060 == FACE_NO_BOX);
7064 /* next_element_from_display_vector sets this flag according to
7065 faces of the display vector glyphs, see there. */
7066 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7068 int face_id = face_after_it_pos (it);
7069 it->end_of_box_run_p
7070 = (face_id != it->face_id
7071 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7074 /* If we reached the end of the object we've been iterating (e.g., a
7075 display string or an overlay string), and there's something on
7076 IT->stack, proceed with what's on the stack. It doesn't make
7077 sense to return zero if there's unprocessed stuff on the stack,
7078 because otherwise that stuff will never be displayed. */
7079 if (!success_p && it->sp > 0)
7081 set_iterator_to_next (it, 0);
7082 success_p = get_next_display_element (it);
7085 /* Value is 0 if end of buffer or string reached. */
7086 return success_p;
7090 /* Move IT to the next display element.
7092 RESEAT_P non-zero means if called on a newline in buffer text,
7093 skip to the next visible line start.
7095 Functions get_next_display_element and set_iterator_to_next are
7096 separate because I find this arrangement easier to handle than a
7097 get_next_display_element function that also increments IT's
7098 position. The way it is we can first look at an iterator's current
7099 display element, decide whether it fits on a line, and if it does,
7100 increment the iterator position. The other way around we probably
7101 would either need a flag indicating whether the iterator has to be
7102 incremented the next time, or we would have to implement a
7103 decrement position function which would not be easy to write. */
7105 void
7106 set_iterator_to_next (struct it *it, int reseat_p)
7108 /* Reset flags indicating start and end of a sequence of characters
7109 with box. Reset them at the start of this function because
7110 moving the iterator to a new position might set them. */
7111 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7113 switch (it->method)
7115 case GET_FROM_BUFFER:
7116 /* The current display element of IT is a character from
7117 current_buffer. Advance in the buffer, and maybe skip over
7118 invisible lines that are so because of selective display. */
7119 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7120 reseat_at_next_visible_line_start (it, 0);
7121 else if (it->cmp_it.id >= 0)
7123 /* We are currently getting glyphs from a composition. */
7124 int i;
7126 if (! it->bidi_p)
7128 IT_CHARPOS (*it) += it->cmp_it.nchars;
7129 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7130 if (it->cmp_it.to < it->cmp_it.nglyphs)
7132 it->cmp_it.from = it->cmp_it.to;
7134 else
7136 it->cmp_it.id = -1;
7137 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7138 IT_BYTEPOS (*it),
7139 it->end_charpos, Qnil);
7142 else if (! it->cmp_it.reversed_p)
7144 /* Composition created while scanning forward. */
7145 /* Update IT's char/byte positions to point to the first
7146 character of the next grapheme cluster, or to the
7147 character visually after the current composition. */
7148 for (i = 0; i < it->cmp_it.nchars; i++)
7149 bidi_move_to_visually_next (&it->bidi_it);
7150 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7151 IT_CHARPOS (*it) = it->bidi_it.charpos;
7153 if (it->cmp_it.to < it->cmp_it.nglyphs)
7155 /* Proceed to the next grapheme cluster. */
7156 it->cmp_it.from = it->cmp_it.to;
7158 else
7160 /* No more grapheme clusters in this composition.
7161 Find the next stop position. */
7162 ptrdiff_t stop = it->end_charpos;
7163 if (it->bidi_it.scan_dir < 0)
7164 /* Now we are scanning backward and don't know
7165 where to stop. */
7166 stop = -1;
7167 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7168 IT_BYTEPOS (*it), stop, Qnil);
7171 else
7173 /* Composition created while scanning backward. */
7174 /* Update IT's char/byte positions to point to the last
7175 character of the previous grapheme cluster, or the
7176 character visually after the current composition. */
7177 for (i = 0; i < it->cmp_it.nchars; i++)
7178 bidi_move_to_visually_next (&it->bidi_it);
7179 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7180 IT_CHARPOS (*it) = it->bidi_it.charpos;
7181 if (it->cmp_it.from > 0)
7183 /* Proceed to the previous grapheme cluster. */
7184 it->cmp_it.to = it->cmp_it.from;
7186 else
7188 /* No more grapheme clusters in this composition.
7189 Find the next stop position. */
7190 ptrdiff_t stop = it->end_charpos;
7191 if (it->bidi_it.scan_dir < 0)
7192 /* Now we are scanning backward and don't know
7193 where to stop. */
7194 stop = -1;
7195 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7196 IT_BYTEPOS (*it), stop, Qnil);
7200 else
7202 eassert (it->len != 0);
7204 if (!it->bidi_p)
7206 IT_BYTEPOS (*it) += it->len;
7207 IT_CHARPOS (*it) += 1;
7209 else
7211 int prev_scan_dir = it->bidi_it.scan_dir;
7212 /* If this is a new paragraph, determine its base
7213 direction (a.k.a. its base embedding level). */
7214 if (it->bidi_it.new_paragraph)
7215 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7216 bidi_move_to_visually_next (&it->bidi_it);
7217 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7218 IT_CHARPOS (*it) = it->bidi_it.charpos;
7219 if (prev_scan_dir != it->bidi_it.scan_dir)
7221 /* As the scan direction was changed, we must
7222 re-compute the stop position for composition. */
7223 ptrdiff_t stop = it->end_charpos;
7224 if (it->bidi_it.scan_dir < 0)
7225 stop = -1;
7226 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7227 IT_BYTEPOS (*it), stop, Qnil);
7230 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7232 break;
7234 case GET_FROM_C_STRING:
7235 /* Current display element of IT is from a C string. */
7236 if (!it->bidi_p
7237 /* If the string position is beyond string's end, it means
7238 next_element_from_c_string is padding the string with
7239 blanks, in which case we bypass the bidi iterator,
7240 because it cannot deal with such virtual characters. */
7241 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7243 IT_BYTEPOS (*it) += it->len;
7244 IT_CHARPOS (*it) += 1;
7246 else
7248 bidi_move_to_visually_next (&it->bidi_it);
7249 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7250 IT_CHARPOS (*it) = it->bidi_it.charpos;
7252 break;
7254 case GET_FROM_DISPLAY_VECTOR:
7255 /* Current display element of IT is from a display table entry.
7256 Advance in the display table definition. Reset it to null if
7257 end reached, and continue with characters from buffers/
7258 strings. */
7259 ++it->current.dpvec_index;
7261 /* Restore face of the iterator to what they were before the
7262 display vector entry (these entries may contain faces). */
7263 it->face_id = it->saved_face_id;
7265 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7267 int recheck_faces = it->ellipsis_p;
7269 if (it->s)
7270 it->method = GET_FROM_C_STRING;
7271 else if (STRINGP (it->string))
7272 it->method = GET_FROM_STRING;
7273 else
7275 it->method = GET_FROM_BUFFER;
7276 it->object = it->w->contents;
7279 it->dpvec = NULL;
7280 it->current.dpvec_index = -1;
7282 /* Skip over characters which were displayed via IT->dpvec. */
7283 if (it->dpvec_char_len < 0)
7284 reseat_at_next_visible_line_start (it, 1);
7285 else if (it->dpvec_char_len > 0)
7287 if (it->method == GET_FROM_STRING
7288 && it->current.overlay_string_index >= 0
7289 && it->n_overlay_strings > 0)
7290 it->ignore_overlay_strings_at_pos_p = true;
7291 it->len = it->dpvec_char_len;
7292 set_iterator_to_next (it, reseat_p);
7295 /* Maybe recheck faces after display vector. */
7296 if (recheck_faces)
7297 it->stop_charpos = IT_CHARPOS (*it);
7299 break;
7301 case GET_FROM_STRING:
7302 /* Current display element is a character from a Lisp string. */
7303 eassert (it->s == NULL && STRINGP (it->string));
7304 /* Don't advance past string end. These conditions are true
7305 when set_iterator_to_next is called at the end of
7306 get_next_display_element, in which case the Lisp string is
7307 already exhausted, and all we want is pop the iterator
7308 stack. */
7309 if (it->current.overlay_string_index >= 0)
7311 /* This is an overlay string, so there's no padding with
7312 spaces, and the number of characters in the string is
7313 where the string ends. */
7314 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7315 goto consider_string_end;
7317 else
7319 /* Not an overlay string. There could be padding, so test
7320 against it->end_charpos. */
7321 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7322 goto consider_string_end;
7324 if (it->cmp_it.id >= 0)
7326 int i;
7328 if (! it->bidi_p)
7330 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7331 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7332 if (it->cmp_it.to < it->cmp_it.nglyphs)
7333 it->cmp_it.from = it->cmp_it.to;
7334 else
7336 it->cmp_it.id = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it),
7340 it->end_charpos, it->string);
7343 else if (! it->cmp_it.reversed_p)
7345 for (i = 0; i < it->cmp_it.nchars; i++)
7346 bidi_move_to_visually_next (&it->bidi_it);
7347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.to < it->cmp_it.nglyphs)
7351 it->cmp_it.from = it->cmp_it.to;
7352 else
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7363 else
7365 for (i = 0; i < it->cmp_it.nchars; i++)
7366 bidi_move_to_visually_next (&it->bidi_it);
7367 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7368 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7369 if (it->cmp_it.from > 0)
7370 it->cmp_it.to = it->cmp_it.from;
7371 else
7373 ptrdiff_t stop = it->end_charpos;
7374 if (it->bidi_it.scan_dir < 0)
7375 stop = -1;
7376 composition_compute_stop_pos (&it->cmp_it,
7377 IT_STRING_CHARPOS (*it),
7378 IT_STRING_BYTEPOS (*it), stop,
7379 it->string);
7383 else
7385 if (!it->bidi_p
7386 /* If the string position is beyond string's end, it
7387 means next_element_from_string is padding the string
7388 with blanks, in which case we bypass the bidi
7389 iterator, because it cannot deal with such virtual
7390 characters. */
7391 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7393 IT_STRING_BYTEPOS (*it) += it->len;
7394 IT_STRING_CHARPOS (*it) += 1;
7396 else
7398 int prev_scan_dir = it->bidi_it.scan_dir;
7400 bidi_move_to_visually_next (&it->bidi_it);
7401 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7402 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7403 if (prev_scan_dir != it->bidi_it.scan_dir)
7405 ptrdiff_t stop = it->end_charpos;
7407 if (it->bidi_it.scan_dir < 0)
7408 stop = -1;
7409 composition_compute_stop_pos (&it->cmp_it,
7410 IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it), stop,
7412 it->string);
7417 consider_string_end:
7419 if (it->current.overlay_string_index >= 0)
7421 /* IT->string is an overlay string. Advance to the
7422 next, if there is one. */
7423 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7425 it->ellipsis_p = 0;
7426 next_overlay_string (it);
7427 if (it->ellipsis_p)
7428 setup_for_ellipsis (it, 0);
7431 else
7433 /* IT->string is not an overlay string. If we reached
7434 its end, and there is something on IT->stack, proceed
7435 with what is on the stack. This can be either another
7436 string, this time an overlay string, or a buffer. */
7437 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7438 && it->sp > 0)
7440 pop_it (it);
7441 if (it->method == GET_FROM_STRING)
7442 goto consider_string_end;
7445 break;
7447 case GET_FROM_IMAGE:
7448 case GET_FROM_STRETCH:
7449 /* The position etc with which we have to proceed are on
7450 the stack. The position may be at the end of a string,
7451 if the `display' property takes up the whole string. */
7452 eassert (it->sp > 0);
7453 pop_it (it);
7454 if (it->method == GET_FROM_STRING)
7455 goto consider_string_end;
7456 break;
7458 default:
7459 /* There are no other methods defined, so this should be a bug. */
7460 emacs_abort ();
7463 eassert (it->method != GET_FROM_STRING
7464 || (STRINGP (it->string)
7465 && IT_STRING_CHARPOS (*it) >= 0));
7468 /* Load IT's display element fields with information about the next
7469 display element which comes from a display table entry or from the
7470 result of translating a control character to one of the forms `^C'
7471 or `\003'.
7473 IT->dpvec holds the glyphs to return as characters.
7474 IT->saved_face_id holds the face id before the display vector--it
7475 is restored into IT->face_id in set_iterator_to_next. */
7477 static int
7478 next_element_from_display_vector (struct it *it)
7480 Lisp_Object gc;
7481 int prev_face_id = it->face_id;
7482 int next_face_id;
7484 /* Precondition. */
7485 eassert (it->dpvec && it->current.dpvec_index >= 0);
7487 it->face_id = it->saved_face_id;
7489 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7490 That seemed totally bogus - so I changed it... */
7491 gc = it->dpvec[it->current.dpvec_index];
7493 if (GLYPH_CODE_P (gc))
7495 struct face *this_face, *prev_face, *next_face;
7497 it->c = GLYPH_CODE_CHAR (gc);
7498 it->len = CHAR_BYTES (it->c);
7500 /* The entry may contain a face id to use. Such a face id is
7501 the id of a Lisp face, not a realized face. A face id of
7502 zero means no face is specified. */
7503 if (it->dpvec_face_id >= 0)
7504 it->face_id = it->dpvec_face_id;
7505 else
7507 int lface_id = GLYPH_CODE_FACE (gc);
7508 if (lface_id > 0)
7509 it->face_id = merge_faces (it->f, Qt, lface_id,
7510 it->saved_face_id);
7513 /* Glyphs in the display vector could have the box face, so we
7514 need to set the related flags in the iterator, as
7515 appropriate. */
7516 this_face = FACE_FROM_ID (it->f, it->face_id);
7517 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7519 /* Is this character the first character of a box-face run? */
7520 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7521 && (!prev_face
7522 || prev_face->box == FACE_NO_BOX));
7524 /* For the last character of the box-face run, we need to look
7525 either at the next glyph from the display vector, or at the
7526 face we saw before the display vector. */
7527 next_face_id = it->saved_face_id;
7528 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7530 if (it->dpvec_face_id >= 0)
7531 next_face_id = it->dpvec_face_id;
7532 else
7534 int lface_id =
7535 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7537 if (lface_id > 0)
7538 next_face_id = merge_faces (it->f, Qt, lface_id,
7539 it->saved_face_id);
7542 next_face = FACE_FROM_ID (it->f, next_face_id);
7543 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7544 && (!next_face
7545 || next_face->box == FACE_NO_BOX));
7546 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7548 else
7549 /* Display table entry is invalid. Return a space. */
7550 it->c = ' ', it->len = 1;
7552 /* Don't change position and object of the iterator here. They are
7553 still the values of the character that had this display table
7554 entry or was translated, and that's what we want. */
7555 it->what = IT_CHARACTER;
7556 return 1;
7559 /* Get the first element of string/buffer in the visual order, after
7560 being reseated to a new position in a string or a buffer. */
7561 static void
7562 get_visually_first_element (struct it *it)
7564 int string_p = STRINGP (it->string) || it->s;
7565 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7566 ptrdiff_t bob = (string_p ? 0 : BEGV);
7568 if (STRINGP (it->string))
7570 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7571 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7573 else
7575 it->bidi_it.charpos = IT_CHARPOS (*it);
7576 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7579 if (it->bidi_it.charpos == eob)
7581 /* Nothing to do, but reset the FIRST_ELT flag, like
7582 bidi_paragraph_init does, because we are not going to
7583 call it. */
7584 it->bidi_it.first_elt = 0;
7586 else if (it->bidi_it.charpos == bob
7587 || (!string_p
7588 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7589 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7591 /* If we are at the beginning of a line/string, we can produce
7592 the next element right away. */
7593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7594 bidi_move_to_visually_next (&it->bidi_it);
7596 else
7598 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7600 /* We need to prime the bidi iterator starting at the line's or
7601 string's beginning, before we will be able to produce the
7602 next element. */
7603 if (string_p)
7604 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7605 else
7606 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7607 IT_BYTEPOS (*it), -1,
7608 &it->bidi_it.bytepos);
7609 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7612 /* Now return to buffer/string position where we were asked
7613 to get the next display element, and produce that. */
7614 bidi_move_to_visually_next (&it->bidi_it);
7616 while (it->bidi_it.bytepos != orig_bytepos
7617 && it->bidi_it.charpos < eob);
7620 /* Adjust IT's position information to where we ended up. */
7621 if (STRINGP (it->string))
7623 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7624 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7626 else
7628 IT_CHARPOS (*it) = it->bidi_it.charpos;
7629 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7632 if (STRINGP (it->string) || !it->s)
7634 ptrdiff_t stop, charpos, bytepos;
7636 if (STRINGP (it->string))
7638 eassert (!it->s);
7639 stop = SCHARS (it->string);
7640 if (stop > it->end_charpos)
7641 stop = it->end_charpos;
7642 charpos = IT_STRING_CHARPOS (*it);
7643 bytepos = IT_STRING_BYTEPOS (*it);
7645 else
7647 stop = it->end_charpos;
7648 charpos = IT_CHARPOS (*it);
7649 bytepos = IT_BYTEPOS (*it);
7651 if (it->bidi_it.scan_dir < 0)
7652 stop = -1;
7653 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7654 it->string);
7658 /* Load IT with the next display element from Lisp string IT->string.
7659 IT->current.string_pos is the current position within the string.
7660 If IT->current.overlay_string_index >= 0, the Lisp string is an
7661 overlay string. */
7663 static int
7664 next_element_from_string (struct it *it)
7666 struct text_pos position;
7668 eassert (STRINGP (it->string));
7669 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7670 eassert (IT_STRING_CHARPOS (*it) >= 0);
7671 position = it->current.string_pos;
7673 /* With bidi reordering, the character to display might not be the
7674 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7675 that we were reseat()ed to a new string, whose paragraph
7676 direction is not known. */
7677 if (it->bidi_p && it->bidi_it.first_elt)
7679 get_visually_first_element (it);
7680 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7683 /* Time to check for invisible text? */
7684 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7686 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7688 if (!(!it->bidi_p
7689 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7690 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7692 /* With bidi non-linear iteration, we could find
7693 ourselves far beyond the last computed stop_charpos,
7694 with several other stop positions in between that we
7695 missed. Scan them all now, in buffer's logical
7696 order, until we find and handle the last stop_charpos
7697 that precedes our current position. */
7698 handle_stop_backwards (it, it->stop_charpos);
7699 return GET_NEXT_DISPLAY_ELEMENT (it);
7701 else
7703 if (it->bidi_p)
7705 /* Take note of the stop position we just moved
7706 across, for when we will move back across it. */
7707 it->prev_stop = it->stop_charpos;
7708 /* If we are at base paragraph embedding level, take
7709 note of the last stop position seen at this
7710 level. */
7711 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7712 it->base_level_stop = it->stop_charpos;
7714 handle_stop (it);
7716 /* Since a handler may have changed IT->method, we must
7717 recurse here. */
7718 return GET_NEXT_DISPLAY_ELEMENT (it);
7721 else if (it->bidi_p
7722 /* If we are before prev_stop, we may have overstepped
7723 on our way backwards a stop_pos, and if so, we need
7724 to handle that stop_pos. */
7725 && IT_STRING_CHARPOS (*it) < it->prev_stop
7726 /* We can sometimes back up for reasons that have nothing
7727 to do with bidi reordering. E.g., compositions. The
7728 code below is only needed when we are above the base
7729 embedding level, so test for that explicitly. */
7730 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7732 /* If we lost track of base_level_stop, we have no better
7733 place for handle_stop_backwards to start from than string
7734 beginning. This happens, e.g., when we were reseated to
7735 the previous screenful of text by vertical-motion. */
7736 if (it->base_level_stop <= 0
7737 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7738 it->base_level_stop = 0;
7739 handle_stop_backwards (it, it->base_level_stop);
7740 return GET_NEXT_DISPLAY_ELEMENT (it);
7744 if (it->current.overlay_string_index >= 0)
7746 /* Get the next character from an overlay string. In overlay
7747 strings, there is no field width or padding with spaces to
7748 do. */
7749 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7751 it->what = IT_EOB;
7752 return 0;
7754 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7755 IT_STRING_BYTEPOS (*it),
7756 it->bidi_it.scan_dir < 0
7757 ? -1
7758 : SCHARS (it->string))
7759 && next_element_from_composition (it))
7761 return 1;
7763 else if (STRING_MULTIBYTE (it->string))
7765 const unsigned char *s = (SDATA (it->string)
7766 + IT_STRING_BYTEPOS (*it));
7767 it->c = string_char_and_length (s, &it->len);
7769 else
7771 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7772 it->len = 1;
7775 else
7777 /* Get the next character from a Lisp string that is not an
7778 overlay string. Such strings come from the mode line, for
7779 example. We may have to pad with spaces, or truncate the
7780 string. See also next_element_from_c_string. */
7781 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7783 it->what = IT_EOB;
7784 return 0;
7786 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7788 /* Pad with spaces. */
7789 it->c = ' ', it->len = 1;
7790 CHARPOS (position) = BYTEPOS (position) = -1;
7792 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7793 IT_STRING_BYTEPOS (*it),
7794 it->bidi_it.scan_dir < 0
7795 ? -1
7796 : it->string_nchars)
7797 && next_element_from_composition (it))
7799 return 1;
7801 else if (STRING_MULTIBYTE (it->string))
7803 const unsigned char *s = (SDATA (it->string)
7804 + IT_STRING_BYTEPOS (*it));
7805 it->c = string_char_and_length (s, &it->len);
7807 else
7809 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7810 it->len = 1;
7814 /* Record what we have and where it came from. */
7815 it->what = IT_CHARACTER;
7816 it->object = it->string;
7817 it->position = position;
7818 return 1;
7822 /* Load IT with next display element from C string IT->s.
7823 IT->string_nchars is the maximum number of characters to return
7824 from the string. IT->end_charpos may be greater than
7825 IT->string_nchars when this function is called, in which case we
7826 may have to return padding spaces. Value is zero if end of string
7827 reached, including padding spaces. */
7829 static int
7830 next_element_from_c_string (struct it *it)
7832 bool success_p = true;
7834 eassert (it->s);
7835 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7836 it->what = IT_CHARACTER;
7837 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7838 it->object = Qnil;
7840 /* With bidi reordering, the character to display might not be the
7841 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7842 we were reseated to a new string, whose paragraph direction is
7843 not known. */
7844 if (it->bidi_p && it->bidi_it.first_elt)
7845 get_visually_first_element (it);
7847 /* IT's position can be greater than IT->string_nchars in case a
7848 field width or precision has been specified when the iterator was
7849 initialized. */
7850 if (IT_CHARPOS (*it) >= it->end_charpos)
7852 /* End of the game. */
7853 it->what = IT_EOB;
7854 success_p = 0;
7856 else if (IT_CHARPOS (*it) >= it->string_nchars)
7858 /* Pad with spaces. */
7859 it->c = ' ', it->len = 1;
7860 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7862 else if (it->multibyte_p)
7863 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7864 else
7865 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7867 return success_p;
7871 /* Set up IT to return characters from an ellipsis, if appropriate.
7872 The definition of the ellipsis glyphs may come from a display table
7873 entry. This function fills IT with the first glyph from the
7874 ellipsis if an ellipsis is to be displayed. */
7876 static int
7877 next_element_from_ellipsis (struct it *it)
7879 if (it->selective_display_ellipsis_p)
7880 setup_for_ellipsis (it, it->len);
7881 else
7883 /* The face at the current position may be different from the
7884 face we find after the invisible text. Remember what it
7885 was in IT->saved_face_id, and signal that it's there by
7886 setting face_before_selective_p. */
7887 it->saved_face_id = it->face_id;
7888 it->method = GET_FROM_BUFFER;
7889 it->object = it->w->contents;
7890 reseat_at_next_visible_line_start (it, 1);
7891 it->face_before_selective_p = true;
7894 return GET_NEXT_DISPLAY_ELEMENT (it);
7898 /* Deliver an image display element. The iterator IT is already
7899 filled with image information (done in handle_display_prop). Value
7900 is always 1. */
7903 static int
7904 next_element_from_image (struct it *it)
7906 it->what = IT_IMAGE;
7907 it->ignore_overlay_strings_at_pos_p = 0;
7908 return 1;
7912 /* Fill iterator IT with next display element from a stretch glyph
7913 property. IT->object is the value of the text property. Value is
7914 always 1. */
7916 static int
7917 next_element_from_stretch (struct it *it)
7919 it->what = IT_STRETCH;
7920 return 1;
7923 /* Scan backwards from IT's current position until we find a stop
7924 position, or until BEGV. This is called when we find ourself
7925 before both the last known prev_stop and base_level_stop while
7926 reordering bidirectional text. */
7928 static void
7929 compute_stop_pos_backwards (struct it *it)
7931 const int SCAN_BACK_LIMIT = 1000;
7932 struct text_pos pos;
7933 struct display_pos save_current = it->current;
7934 struct text_pos save_position = it->position;
7935 ptrdiff_t charpos = IT_CHARPOS (*it);
7936 ptrdiff_t where_we_are = charpos;
7937 ptrdiff_t save_stop_pos = it->stop_charpos;
7938 ptrdiff_t save_end_pos = it->end_charpos;
7940 eassert (NILP (it->string) && !it->s);
7941 eassert (it->bidi_p);
7942 it->bidi_p = 0;
7945 it->end_charpos = min (charpos + 1, ZV);
7946 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7947 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7948 reseat_1 (it, pos, 0);
7949 compute_stop_pos (it);
7950 /* We must advance forward, right? */
7951 if (it->stop_charpos <= charpos)
7952 emacs_abort ();
7954 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7956 if (it->stop_charpos <= where_we_are)
7957 it->prev_stop = it->stop_charpos;
7958 else
7959 it->prev_stop = BEGV;
7960 it->bidi_p = true;
7961 it->current = save_current;
7962 it->position = save_position;
7963 it->stop_charpos = save_stop_pos;
7964 it->end_charpos = save_end_pos;
7967 /* Scan forward from CHARPOS in the current buffer/string, until we
7968 find a stop position > current IT's position. Then handle the stop
7969 position before that. This is called when we bump into a stop
7970 position while reordering bidirectional text. CHARPOS should be
7971 the last previously processed stop_pos (or BEGV/0, if none were
7972 processed yet) whose position is less that IT's current
7973 position. */
7975 static void
7976 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7978 int bufp = !STRINGP (it->string);
7979 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7980 struct display_pos save_current = it->current;
7981 struct text_pos save_position = it->position;
7982 struct text_pos pos1;
7983 ptrdiff_t next_stop;
7985 /* Scan in strict logical order. */
7986 eassert (it->bidi_p);
7987 it->bidi_p = 0;
7990 it->prev_stop = charpos;
7991 if (bufp)
7993 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7994 reseat_1 (it, pos1, 0);
7996 else
7997 it->current.string_pos = string_pos (charpos, it->string);
7998 compute_stop_pos (it);
7999 /* We must advance forward, right? */
8000 if (it->stop_charpos <= it->prev_stop)
8001 emacs_abort ();
8002 charpos = it->stop_charpos;
8004 while (charpos <= where_we_are);
8006 it->bidi_p = true;
8007 it->current = save_current;
8008 it->position = save_position;
8009 next_stop = it->stop_charpos;
8010 it->stop_charpos = it->prev_stop;
8011 handle_stop (it);
8012 it->stop_charpos = next_stop;
8015 /* Load IT with the next display element from current_buffer. Value
8016 is zero if end of buffer reached. IT->stop_charpos is the next
8017 position at which to stop and check for text properties or buffer
8018 end. */
8020 static int
8021 next_element_from_buffer (struct it *it)
8023 bool success_p = true;
8025 eassert (IT_CHARPOS (*it) >= BEGV);
8026 eassert (NILP (it->string) && !it->s);
8027 eassert (!it->bidi_p
8028 || (EQ (it->bidi_it.string.lstring, Qnil)
8029 && it->bidi_it.string.s == NULL));
8031 /* With bidi reordering, the character to display might not be the
8032 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8033 we were reseat()ed to a new buffer position, which is potentially
8034 a different paragraph. */
8035 if (it->bidi_p && it->bidi_it.first_elt)
8037 get_visually_first_element (it);
8038 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8041 if (IT_CHARPOS (*it) >= it->stop_charpos)
8043 if (IT_CHARPOS (*it) >= it->end_charpos)
8045 int overlay_strings_follow_p;
8047 /* End of the game, except when overlay strings follow that
8048 haven't been returned yet. */
8049 if (it->overlay_strings_at_end_processed_p)
8050 overlay_strings_follow_p = 0;
8051 else
8053 it->overlay_strings_at_end_processed_p = true;
8054 overlay_strings_follow_p = get_overlay_strings (it, 0);
8057 if (overlay_strings_follow_p)
8058 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8059 else
8061 it->what = IT_EOB;
8062 it->position = it->current.pos;
8063 success_p = 0;
8066 else if (!(!it->bidi_p
8067 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8068 || IT_CHARPOS (*it) == it->stop_charpos))
8070 /* With bidi non-linear iteration, we could find ourselves
8071 far beyond the last computed stop_charpos, with several
8072 other stop positions in between that we missed. Scan
8073 them all now, in buffer's logical order, until we find
8074 and handle the last stop_charpos that precedes our
8075 current position. */
8076 handle_stop_backwards (it, it->stop_charpos);
8077 return GET_NEXT_DISPLAY_ELEMENT (it);
8079 else
8081 if (it->bidi_p)
8083 /* Take note of the stop position we just moved across,
8084 for when we will move back across it. */
8085 it->prev_stop = it->stop_charpos;
8086 /* If we are at base paragraph embedding level, take
8087 note of the last stop position seen at this
8088 level. */
8089 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8090 it->base_level_stop = it->stop_charpos;
8092 handle_stop (it);
8093 return GET_NEXT_DISPLAY_ELEMENT (it);
8096 else if (it->bidi_p
8097 /* If we are before prev_stop, we may have overstepped on
8098 our way backwards a stop_pos, and if so, we need to
8099 handle that stop_pos. */
8100 && IT_CHARPOS (*it) < it->prev_stop
8101 /* We can sometimes back up for reasons that have nothing
8102 to do with bidi reordering. E.g., compositions. The
8103 code below is only needed when we are above the base
8104 embedding level, so test for that explicitly. */
8105 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8107 if (it->base_level_stop <= 0
8108 || IT_CHARPOS (*it) < it->base_level_stop)
8110 /* If we lost track of base_level_stop, we need to find
8111 prev_stop by looking backwards. This happens, e.g., when
8112 we were reseated to the previous screenful of text by
8113 vertical-motion. */
8114 it->base_level_stop = BEGV;
8115 compute_stop_pos_backwards (it);
8116 handle_stop_backwards (it, it->prev_stop);
8118 else
8119 handle_stop_backwards (it, it->base_level_stop);
8120 return GET_NEXT_DISPLAY_ELEMENT (it);
8122 else
8124 /* No face changes, overlays etc. in sight, so just return a
8125 character from current_buffer. */
8126 unsigned char *p;
8127 ptrdiff_t stop;
8129 /* Maybe run the redisplay end trigger hook. Performance note:
8130 This doesn't seem to cost measurable time. */
8131 if (it->redisplay_end_trigger_charpos
8132 && it->glyph_row
8133 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8134 run_redisplay_end_trigger_hook (it);
8136 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8137 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8138 stop)
8139 && next_element_from_composition (it))
8141 return 1;
8144 /* Get the next character, maybe multibyte. */
8145 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8146 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8147 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8148 else
8149 it->c = *p, it->len = 1;
8151 /* Record what we have and where it came from. */
8152 it->what = IT_CHARACTER;
8153 it->object = it->w->contents;
8154 it->position = it->current.pos;
8156 /* Normally we return the character found above, except when we
8157 really want to return an ellipsis for selective display. */
8158 if (it->selective)
8160 if (it->c == '\n')
8162 /* A value of selective > 0 means hide lines indented more
8163 than that number of columns. */
8164 if (it->selective > 0
8165 && IT_CHARPOS (*it) + 1 < ZV
8166 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8167 IT_BYTEPOS (*it) + 1,
8168 it->selective))
8170 success_p = next_element_from_ellipsis (it);
8171 it->dpvec_char_len = -1;
8174 else if (it->c == '\r' && it->selective == -1)
8176 /* A value of selective == -1 means that everything from the
8177 CR to the end of the line is invisible, with maybe an
8178 ellipsis displayed for it. */
8179 success_p = next_element_from_ellipsis (it);
8180 it->dpvec_char_len = -1;
8185 /* Value is zero if end of buffer reached. */
8186 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8187 return success_p;
8191 /* Run the redisplay end trigger hook for IT. */
8193 static void
8194 run_redisplay_end_trigger_hook (struct it *it)
8196 Lisp_Object args[3];
8198 /* IT->glyph_row should be non-null, i.e. we should be actually
8199 displaying something, or otherwise we should not run the hook. */
8200 eassert (it->glyph_row);
8202 /* Set up hook arguments. */
8203 args[0] = Qredisplay_end_trigger_functions;
8204 args[1] = it->window;
8205 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8206 it->redisplay_end_trigger_charpos = 0;
8208 /* Since we are *trying* to run these functions, don't try to run
8209 them again, even if they get an error. */
8210 wset_redisplay_end_trigger (it->w, Qnil);
8211 Frun_hook_with_args (3, args);
8213 /* Notice if it changed the face of the character we are on. */
8214 handle_face_prop (it);
8218 /* Deliver a composition display element. Unlike the other
8219 next_element_from_XXX, this function is not registered in the array
8220 get_next_element[]. It is called from next_element_from_buffer and
8221 next_element_from_string when necessary. */
8223 static int
8224 next_element_from_composition (struct it *it)
8226 it->what = IT_COMPOSITION;
8227 it->len = it->cmp_it.nbytes;
8228 if (STRINGP (it->string))
8230 if (it->c < 0)
8232 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8233 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8234 return 0;
8236 it->position = it->current.string_pos;
8237 it->object = it->string;
8238 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8239 IT_STRING_BYTEPOS (*it), it->string);
8241 else
8243 if (it->c < 0)
8245 IT_CHARPOS (*it) += it->cmp_it.nchars;
8246 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8247 if (it->bidi_p)
8249 if (it->bidi_it.new_paragraph)
8250 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8251 /* Resync the bidi iterator with IT's new position.
8252 FIXME: this doesn't support bidirectional text. */
8253 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8254 bidi_move_to_visually_next (&it->bidi_it);
8256 return 0;
8258 it->position = it->current.pos;
8259 it->object = it->w->contents;
8260 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8261 IT_BYTEPOS (*it), Qnil);
8263 return 1;
8268 /***********************************************************************
8269 Moving an iterator without producing glyphs
8270 ***********************************************************************/
8272 /* Check if iterator is at a position corresponding to a valid buffer
8273 position after some move_it_ call. */
8275 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8276 ((it)->method == GET_FROM_STRING \
8277 ? IT_STRING_CHARPOS (*it) == 0 \
8278 : 1)
8281 /* Move iterator IT to a specified buffer or X position within one
8282 line on the display without producing glyphs.
8284 OP should be a bit mask including some or all of these bits:
8285 MOVE_TO_X: Stop upon reaching x-position TO_X.
8286 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8287 Regardless of OP's value, stop upon reaching the end of the display line.
8289 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8290 This means, in particular, that TO_X includes window's horizontal
8291 scroll amount.
8293 The return value has several possible values that
8294 say what condition caused the scan to stop:
8296 MOVE_POS_MATCH_OR_ZV
8297 - when TO_POS or ZV was reached.
8299 MOVE_X_REACHED
8300 -when TO_X was reached before TO_POS or ZV were reached.
8302 MOVE_LINE_CONTINUED
8303 - when we reached the end of the display area and the line must
8304 be continued.
8306 MOVE_LINE_TRUNCATED
8307 - when we reached the end of the display area and the line is
8308 truncated.
8310 MOVE_NEWLINE_OR_CR
8311 - when we stopped at a line end, i.e. a newline or a CR and selective
8312 display is on. */
8314 static enum move_it_result
8315 move_it_in_display_line_to (struct it *it,
8316 ptrdiff_t to_charpos, int to_x,
8317 enum move_operation_enum op)
8319 enum move_it_result result = MOVE_UNDEFINED;
8320 struct glyph_row *saved_glyph_row;
8321 struct it wrap_it, atpos_it, atx_it, ppos_it;
8322 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8323 void *ppos_data = NULL;
8324 int may_wrap = 0;
8325 enum it_method prev_method = it->method;
8326 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8327 int saw_smaller_pos = prev_pos < to_charpos;
8329 /* Don't produce glyphs in produce_glyphs. */
8330 saved_glyph_row = it->glyph_row;
8331 it->glyph_row = NULL;
8333 /* Use wrap_it to save a copy of IT wherever a word wrap could
8334 occur. Use atpos_it to save a copy of IT at the desired buffer
8335 position, if found, so that we can scan ahead and check if the
8336 word later overshoots the window edge. Use atx_it similarly, for
8337 pixel positions. */
8338 wrap_it.sp = -1;
8339 atpos_it.sp = -1;
8340 atx_it.sp = -1;
8342 /* Use ppos_it under bidi reordering to save a copy of IT for the
8343 initial position. We restore that position in IT when we have
8344 scanned the entire display line without finding a match for
8345 TO_CHARPOS and all the character positions are greater than
8346 TO_CHARPOS. We then restart the scan from the initial position,
8347 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8348 the closest to TO_CHARPOS. */
8349 if (it->bidi_p)
8351 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8353 SAVE_IT (ppos_it, *it, ppos_data);
8354 closest_pos = IT_CHARPOS (*it);
8356 else
8357 closest_pos = ZV;
8360 #define BUFFER_POS_REACHED_P() \
8361 ((op & MOVE_TO_POS) != 0 \
8362 && BUFFERP (it->object) \
8363 && (IT_CHARPOS (*it) == to_charpos \
8364 || ((!it->bidi_p \
8365 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8366 && IT_CHARPOS (*it) > to_charpos) \
8367 || (it->what == IT_COMPOSITION \
8368 && ((IT_CHARPOS (*it) > to_charpos \
8369 && to_charpos >= it->cmp_it.charpos) \
8370 || (IT_CHARPOS (*it) < to_charpos \
8371 && to_charpos <= it->cmp_it.charpos)))) \
8372 && (it->method == GET_FROM_BUFFER \
8373 || (it->method == GET_FROM_DISPLAY_VECTOR \
8374 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8376 /* If there's a line-/wrap-prefix, handle it. */
8377 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8378 && it->current_y < it->last_visible_y)
8379 handle_line_prefix (it);
8381 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8382 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8384 while (1)
8386 int x, i, ascent = 0, descent = 0;
8388 /* Utility macro to reset an iterator with x, ascent, and descent. */
8389 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8390 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8391 (IT)->max_descent = descent)
8393 /* Stop if we move beyond TO_CHARPOS (after an image or a
8394 display string or stretch glyph). */
8395 if ((op & MOVE_TO_POS) != 0
8396 && BUFFERP (it->object)
8397 && it->method == GET_FROM_BUFFER
8398 && (((!it->bidi_p
8399 /* When the iterator is at base embedding level, we
8400 are guaranteed that characters are delivered for
8401 display in strictly increasing order of their
8402 buffer positions. */
8403 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8404 && IT_CHARPOS (*it) > to_charpos)
8405 || (it->bidi_p
8406 && (prev_method == GET_FROM_IMAGE
8407 || prev_method == GET_FROM_STRETCH
8408 || prev_method == GET_FROM_STRING)
8409 /* Passed TO_CHARPOS from left to right. */
8410 && ((prev_pos < to_charpos
8411 && IT_CHARPOS (*it) > to_charpos)
8412 /* Passed TO_CHARPOS from right to left. */
8413 || (prev_pos > to_charpos
8414 && IT_CHARPOS (*it) < to_charpos)))))
8416 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8421 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8422 /* If wrap_it is valid, the current position might be in a
8423 word that is wrapped. So, save the iterator in
8424 atpos_it and continue to see if wrapping happens. */
8425 SAVE_IT (atpos_it, *it, atpos_data);
8428 /* Stop when ZV reached.
8429 We used to stop here when TO_CHARPOS reached as well, but that is
8430 too soon if this glyph does not fit on this line. So we handle it
8431 explicitly below. */
8432 if (!get_next_display_element (it))
8434 result = MOVE_POS_MATCH_OR_ZV;
8435 break;
8438 if (it->line_wrap == TRUNCATE)
8440 if (BUFFER_POS_REACHED_P ())
8442 result = MOVE_POS_MATCH_OR_ZV;
8443 break;
8446 else
8448 if (it->line_wrap == WORD_WRAP)
8450 if (IT_DISPLAYING_WHITESPACE (it))
8451 may_wrap = 1;
8452 else if (may_wrap)
8454 /* We have reached a glyph that follows one or more
8455 whitespace characters. If the position is
8456 already found, we are done. */
8457 if (atpos_it.sp >= 0)
8459 RESTORE_IT (it, &atpos_it, atpos_data);
8460 result = MOVE_POS_MATCH_OR_ZV;
8461 goto done;
8463 if (atx_it.sp >= 0)
8465 RESTORE_IT (it, &atx_it, atx_data);
8466 result = MOVE_X_REACHED;
8467 goto done;
8469 /* Otherwise, we can wrap here. */
8470 SAVE_IT (wrap_it, *it, wrap_data);
8471 may_wrap = 0;
8476 /* Remember the line height for the current line, in case
8477 the next element doesn't fit on the line. */
8478 ascent = it->max_ascent;
8479 descent = it->max_descent;
8481 /* The call to produce_glyphs will get the metrics of the
8482 display element IT is loaded with. Record the x-position
8483 before this display element, in case it doesn't fit on the
8484 line. */
8485 x = it->current_x;
8487 PRODUCE_GLYPHS (it);
8489 if (it->area != TEXT_AREA)
8491 prev_method = it->method;
8492 if (it->method == GET_FROM_BUFFER)
8493 prev_pos = IT_CHARPOS (*it);
8494 set_iterator_to_next (it, 1);
8495 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8496 SET_TEXT_POS (this_line_min_pos,
8497 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8498 if (it->bidi_p
8499 && (op & MOVE_TO_POS)
8500 && IT_CHARPOS (*it) > to_charpos
8501 && IT_CHARPOS (*it) < closest_pos)
8502 closest_pos = IT_CHARPOS (*it);
8503 continue;
8506 /* The number of glyphs we get back in IT->nglyphs will normally
8507 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8508 character on a terminal frame, or (iii) a line end. For the
8509 second case, IT->nglyphs - 1 padding glyphs will be present.
8510 (On X frames, there is only one glyph produced for a
8511 composite character.)
8513 The behavior implemented below means, for continuation lines,
8514 that as many spaces of a TAB as fit on the current line are
8515 displayed there. For terminal frames, as many glyphs of a
8516 multi-glyph character are displayed in the current line, too.
8517 This is what the old redisplay code did, and we keep it that
8518 way. Under X, the whole shape of a complex character must
8519 fit on the line or it will be completely displayed in the
8520 next line.
8522 Note that both for tabs and padding glyphs, all glyphs have
8523 the same width. */
8524 if (it->nglyphs)
8526 /* More than one glyph or glyph doesn't fit on line. All
8527 glyphs have the same width. */
8528 int single_glyph_width = it->pixel_width / it->nglyphs;
8529 int new_x;
8530 int x_before_this_char = x;
8531 int hpos_before_this_char = it->hpos;
8533 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8535 new_x = x + single_glyph_width;
8537 /* We want to leave anything reaching TO_X to the caller. */
8538 if ((op & MOVE_TO_X) && new_x > to_x)
8540 if (BUFFER_POS_REACHED_P ())
8542 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8543 goto buffer_pos_reached;
8544 if (atpos_it.sp < 0)
8546 SAVE_IT (atpos_it, *it, atpos_data);
8547 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8550 else
8552 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8554 it->current_x = x;
8555 result = MOVE_X_REACHED;
8556 break;
8558 if (atx_it.sp < 0)
8560 SAVE_IT (atx_it, *it, atx_data);
8561 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8566 if (/* Lines are continued. */
8567 it->line_wrap != TRUNCATE
8568 && (/* And glyph doesn't fit on the line. */
8569 new_x > it->last_visible_x
8570 /* Or it fits exactly and we're on a window
8571 system frame. */
8572 || (new_x == it->last_visible_x
8573 && FRAME_WINDOW_P (it->f)
8574 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8575 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8576 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8578 if (/* IT->hpos == 0 means the very first glyph
8579 doesn't fit on the line, e.g. a wide image. */
8580 it->hpos == 0
8581 || (new_x == it->last_visible_x
8582 && FRAME_WINDOW_P (it->f)))
8584 ++it->hpos;
8585 it->current_x = new_x;
8587 /* The character's last glyph just barely fits
8588 in this row. */
8589 if (i == it->nglyphs - 1)
8591 /* If this is the destination position,
8592 return a position *before* it in this row,
8593 now that we know it fits in this row. */
8594 if (BUFFER_POS_REACHED_P ())
8596 if (it->line_wrap != WORD_WRAP
8597 || wrap_it.sp < 0)
8599 it->hpos = hpos_before_this_char;
8600 it->current_x = x_before_this_char;
8601 result = MOVE_POS_MATCH_OR_ZV;
8602 break;
8604 if (it->line_wrap == WORD_WRAP
8605 && atpos_it.sp < 0)
8607 SAVE_IT (atpos_it, *it, atpos_data);
8608 atpos_it.current_x = x_before_this_char;
8609 atpos_it.hpos = hpos_before_this_char;
8613 prev_method = it->method;
8614 if (it->method == GET_FROM_BUFFER)
8615 prev_pos = IT_CHARPOS (*it);
8616 set_iterator_to_next (it, 1);
8617 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8618 SET_TEXT_POS (this_line_min_pos,
8619 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8620 /* On graphical terminals, newlines may
8621 "overflow" into the fringe if
8622 overflow-newline-into-fringe is non-nil.
8623 On text terminals, and on graphical
8624 terminals with no right margin, newlines
8625 may overflow into the last glyph on the
8626 display line.*/
8627 if (!FRAME_WINDOW_P (it->f)
8628 || ((it->bidi_p
8629 && it->bidi_it.paragraph_dir == R2L)
8630 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8631 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8632 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8634 if (!get_next_display_element (it))
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8639 if (BUFFER_POS_REACHED_P ())
8641 if (ITERATOR_AT_END_OF_LINE_P (it))
8642 result = MOVE_POS_MATCH_OR_ZV;
8643 else
8644 result = MOVE_LINE_CONTINUED;
8645 break;
8647 if (ITERATOR_AT_END_OF_LINE_P (it)
8648 && (it->line_wrap != WORD_WRAP
8649 || wrap_it.sp < 0))
8651 result = MOVE_NEWLINE_OR_CR;
8652 break;
8657 else
8658 IT_RESET_X_ASCENT_DESCENT (it);
8660 if (wrap_it.sp >= 0)
8662 RESTORE_IT (it, &wrap_it, wrap_data);
8663 atpos_it.sp = -1;
8664 atx_it.sp = -1;
8667 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8668 IT_CHARPOS (*it)));
8669 result = MOVE_LINE_CONTINUED;
8670 break;
8673 if (BUFFER_POS_REACHED_P ())
8675 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8676 goto buffer_pos_reached;
8677 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8679 SAVE_IT (atpos_it, *it, atpos_data);
8680 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8684 if (new_x > it->first_visible_x)
8686 /* Glyph is visible. Increment number of glyphs that
8687 would be displayed. */
8688 ++it->hpos;
8692 if (result != MOVE_UNDEFINED)
8693 break;
8695 else if (BUFFER_POS_REACHED_P ())
8697 buffer_pos_reached:
8698 IT_RESET_X_ASCENT_DESCENT (it);
8699 result = MOVE_POS_MATCH_OR_ZV;
8700 break;
8702 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8704 /* Stop when TO_X specified and reached. This check is
8705 necessary here because of lines consisting of a line end,
8706 only. The line end will not produce any glyphs and we
8707 would never get MOVE_X_REACHED. */
8708 eassert (it->nglyphs == 0);
8709 result = MOVE_X_REACHED;
8710 break;
8713 /* Is this a line end? If yes, we're done. */
8714 if (ITERATOR_AT_END_OF_LINE_P (it))
8716 /* If we are past TO_CHARPOS, but never saw any character
8717 positions smaller than TO_CHARPOS, return
8718 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8719 did. */
8720 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8722 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8724 if (closest_pos < ZV)
8726 RESTORE_IT (it, &ppos_it, ppos_data);
8727 move_it_in_display_line_to (it, closest_pos, -1,
8728 MOVE_TO_POS);
8729 result = MOVE_POS_MATCH_OR_ZV;
8731 else
8732 goto buffer_pos_reached;
8734 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8735 && IT_CHARPOS (*it) > to_charpos)
8736 goto buffer_pos_reached;
8737 else
8738 result = MOVE_NEWLINE_OR_CR;
8740 else
8741 result = MOVE_NEWLINE_OR_CR;
8742 break;
8745 prev_method = it->method;
8746 if (it->method == GET_FROM_BUFFER)
8747 prev_pos = IT_CHARPOS (*it);
8748 /* The current display element has been consumed. Advance
8749 to the next. */
8750 set_iterator_to_next (it, 1);
8751 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8752 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8753 if (IT_CHARPOS (*it) < to_charpos)
8754 saw_smaller_pos = 1;
8755 if (it->bidi_p
8756 && (op & MOVE_TO_POS)
8757 && IT_CHARPOS (*it) >= to_charpos
8758 && IT_CHARPOS (*it) < closest_pos)
8759 closest_pos = IT_CHARPOS (*it);
8761 /* Stop if lines are truncated and IT's current x-position is
8762 past the right edge of the window now. */
8763 if (it->line_wrap == TRUNCATE
8764 && it->current_x >= it->last_visible_x)
8766 if (!FRAME_WINDOW_P (it->f)
8767 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8768 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8769 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8770 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8772 int at_eob_p = 0;
8774 if ((at_eob_p = !get_next_display_element (it))
8775 || BUFFER_POS_REACHED_P ()
8776 /* If we are past TO_CHARPOS, but never saw any
8777 character positions smaller than TO_CHARPOS,
8778 return MOVE_POS_MATCH_OR_ZV, like the
8779 unidirectional display did. */
8780 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8781 && !saw_smaller_pos
8782 && IT_CHARPOS (*it) > to_charpos))
8784 if (it->bidi_p
8785 && !BUFFER_POS_REACHED_P ()
8786 && !at_eob_p && closest_pos < ZV)
8788 RESTORE_IT (it, &ppos_it, ppos_data);
8789 move_it_in_display_line_to (it, closest_pos, -1,
8790 MOVE_TO_POS);
8792 result = MOVE_POS_MATCH_OR_ZV;
8793 break;
8795 if (ITERATOR_AT_END_OF_LINE_P (it))
8797 result = MOVE_NEWLINE_OR_CR;
8798 break;
8801 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8802 && !saw_smaller_pos
8803 && IT_CHARPOS (*it) > to_charpos)
8805 if (closest_pos < ZV)
8807 RESTORE_IT (it, &ppos_it, ppos_data);
8808 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8810 result = MOVE_POS_MATCH_OR_ZV;
8811 break;
8813 result = MOVE_LINE_TRUNCATED;
8814 break;
8816 #undef IT_RESET_X_ASCENT_DESCENT
8819 #undef BUFFER_POS_REACHED_P
8821 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8822 restore the saved iterator. */
8823 if (atpos_it.sp >= 0)
8824 RESTORE_IT (it, &atpos_it, atpos_data);
8825 else if (atx_it.sp >= 0)
8826 RESTORE_IT (it, &atx_it, atx_data);
8828 done:
8830 if (atpos_data)
8831 bidi_unshelve_cache (atpos_data, 1);
8832 if (atx_data)
8833 bidi_unshelve_cache (atx_data, 1);
8834 if (wrap_data)
8835 bidi_unshelve_cache (wrap_data, 1);
8836 if (ppos_data)
8837 bidi_unshelve_cache (ppos_data, 1);
8839 /* Restore the iterator settings altered at the beginning of this
8840 function. */
8841 it->glyph_row = saved_glyph_row;
8842 return result;
8845 /* For external use. */
8846 void
8847 move_it_in_display_line (struct it *it,
8848 ptrdiff_t to_charpos, int to_x,
8849 enum move_operation_enum op)
8851 if (it->line_wrap == WORD_WRAP
8852 && (op & MOVE_TO_X))
8854 struct it save_it;
8855 void *save_data = NULL;
8856 int skip;
8858 SAVE_IT (save_it, *it, save_data);
8859 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8860 /* When word-wrap is on, TO_X may lie past the end
8861 of a wrapped line. Then it->current is the
8862 character on the next line, so backtrack to the
8863 space before the wrap point. */
8864 if (skip == MOVE_LINE_CONTINUED)
8866 int prev_x = max (it->current_x - 1, 0);
8867 RESTORE_IT (it, &save_it, save_data);
8868 move_it_in_display_line_to
8869 (it, -1, prev_x, MOVE_TO_X);
8871 else
8872 bidi_unshelve_cache (save_data, 1);
8874 else
8875 move_it_in_display_line_to (it, to_charpos, to_x, op);
8879 /* Move IT forward until it satisfies one or more of the criteria in
8880 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8882 OP is a bit-mask that specifies where to stop, and in particular,
8883 which of those four position arguments makes a difference. See the
8884 description of enum move_operation_enum.
8886 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8887 screen line, this function will set IT to the next position that is
8888 displayed to the right of TO_CHARPOS on the screen.
8890 Return the maximum pixel length of any line scanned but never more
8891 than it.last_visible_x. */
8894 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8896 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8897 int line_height, line_start_x = 0, reached = 0;
8898 int max_current_x = 0;
8899 void *backup_data = NULL;
8901 for (;;)
8903 if (op & MOVE_TO_VPOS)
8905 /* If no TO_CHARPOS and no TO_X specified, stop at the
8906 start of the line TO_VPOS. */
8907 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8909 if (it->vpos == to_vpos)
8911 reached = 1;
8912 break;
8914 else
8915 skip = move_it_in_display_line_to (it, -1, -1, 0);
8917 else
8919 /* TO_VPOS >= 0 means stop at TO_X in the line at
8920 TO_VPOS, or at TO_POS, whichever comes first. */
8921 if (it->vpos == to_vpos)
8923 reached = 2;
8924 break;
8927 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8929 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8931 reached = 3;
8932 break;
8934 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8936 /* We have reached TO_X but not in the line we want. */
8937 skip = move_it_in_display_line_to (it, to_charpos,
8938 -1, MOVE_TO_POS);
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8941 reached = 4;
8942 break;
8947 else if (op & MOVE_TO_Y)
8949 struct it it_backup;
8951 if (it->line_wrap == WORD_WRAP)
8952 SAVE_IT (it_backup, *it, backup_data);
8954 /* TO_Y specified means stop at TO_X in the line containing
8955 TO_Y---or at TO_CHARPOS if this is reached first. The
8956 problem is that we can't really tell whether the line
8957 contains TO_Y before we have completely scanned it, and
8958 this may skip past TO_X. What we do is to first scan to
8959 TO_X.
8961 If TO_X is not specified, use a TO_X of zero. The reason
8962 is to make the outcome of this function more predictable.
8963 If we didn't use TO_X == 0, we would stop at the end of
8964 the line which is probably not what a caller would expect
8965 to happen. */
8966 skip = move_it_in_display_line_to
8967 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8968 (MOVE_TO_X | (op & MOVE_TO_POS)));
8970 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8971 if (skip == MOVE_POS_MATCH_OR_ZV)
8972 reached = 5;
8973 else if (skip == MOVE_X_REACHED)
8975 /* If TO_X was reached, we want to know whether TO_Y is
8976 in the line. We know this is the case if the already
8977 scanned glyphs make the line tall enough. Otherwise,
8978 we must check by scanning the rest of the line. */
8979 line_height = it->max_ascent + it->max_descent;
8980 if (to_y >= it->current_y
8981 && to_y < it->current_y + line_height)
8983 reached = 6;
8984 break;
8986 SAVE_IT (it_backup, *it, backup_data);
8987 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8988 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8989 op & MOVE_TO_POS);
8990 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8991 line_height = it->max_ascent + it->max_descent;
8992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8994 if (to_y >= it->current_y
8995 && to_y < it->current_y + line_height)
8997 /* If TO_Y is in this line and TO_X was reached
8998 above, we scanned too far. We have to restore
8999 IT's settings to the ones before skipping. But
9000 keep the more accurate values of max_ascent and
9001 max_descent we've found while skipping the rest
9002 of the line, for the sake of callers, such as
9003 pos_visible_p, that need to know the line
9004 height. */
9005 int max_ascent = it->max_ascent;
9006 int max_descent = it->max_descent;
9008 RESTORE_IT (it, &it_backup, backup_data);
9009 it->max_ascent = max_ascent;
9010 it->max_descent = max_descent;
9011 reached = 6;
9013 else
9015 skip = skip2;
9016 if (skip == MOVE_POS_MATCH_OR_ZV)
9017 reached = 7;
9020 else
9022 /* Check whether TO_Y is in this line. */
9023 line_height = it->max_ascent + it->max_descent;
9024 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9026 if (to_y >= it->current_y
9027 && to_y < it->current_y + line_height)
9029 if (to_y > it->current_y)
9030 max_current_x = max (it->current_x, max_current_x);
9032 /* When word-wrap is on, TO_X may lie past the end
9033 of a wrapped line. Then it->current is the
9034 character on the next line, so backtrack to the
9035 space before the wrap point. */
9036 if (skip == MOVE_LINE_CONTINUED
9037 && it->line_wrap == WORD_WRAP)
9039 int prev_x = max (it->current_x - 1, 0);
9040 RESTORE_IT (it, &it_backup, backup_data);
9041 skip = move_it_in_display_line_to
9042 (it, -1, prev_x, MOVE_TO_X);
9045 reached = 6;
9049 if (reached)
9051 max_current_x = max (it->current_x, max_current_x);
9052 break;
9055 else if (BUFFERP (it->object)
9056 && (it->method == GET_FROM_BUFFER
9057 || it->method == GET_FROM_STRETCH)
9058 && IT_CHARPOS (*it) >= to_charpos
9059 /* Under bidi iteration, a call to set_iterator_to_next
9060 can scan far beyond to_charpos if the initial
9061 portion of the next line needs to be reordered. In
9062 that case, give move_it_in_display_line_to another
9063 chance below. */
9064 && !(it->bidi_p
9065 && it->bidi_it.scan_dir == -1))
9066 skip = MOVE_POS_MATCH_OR_ZV;
9067 else
9068 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9070 switch (skip)
9072 case MOVE_POS_MATCH_OR_ZV:
9073 max_current_x = max (it->current_x, max_current_x);
9074 reached = 8;
9075 goto out;
9077 case MOVE_NEWLINE_OR_CR:
9078 max_current_x = max (it->current_x, max_current_x);
9079 set_iterator_to_next (it, 1);
9080 it->continuation_lines_width = 0;
9081 break;
9083 case MOVE_LINE_TRUNCATED:
9084 max_current_x = it->last_visible_x;
9085 it->continuation_lines_width = 0;
9086 reseat_at_next_visible_line_start (it, 0);
9087 if ((op & MOVE_TO_POS) != 0
9088 && IT_CHARPOS (*it) > to_charpos)
9090 reached = 9;
9091 goto out;
9093 break;
9095 case MOVE_LINE_CONTINUED:
9096 max_current_x = it->last_visible_x;
9097 /* For continued lines ending in a tab, some of the glyphs
9098 associated with the tab are displayed on the current
9099 line. Since it->current_x does not include these glyphs,
9100 we use it->last_visible_x instead. */
9101 if (it->c == '\t')
9103 it->continuation_lines_width += it->last_visible_x;
9104 /* When moving by vpos, ensure that the iterator really
9105 advances to the next line (bug#847, bug#969). Fixme:
9106 do we need to do this in other circumstances? */
9107 if (it->current_x != it->last_visible_x
9108 && (op & MOVE_TO_VPOS)
9109 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9111 line_start_x = it->current_x + it->pixel_width
9112 - it->last_visible_x;
9113 set_iterator_to_next (it, 0);
9116 else
9117 it->continuation_lines_width += it->current_x;
9118 break;
9120 default:
9121 emacs_abort ();
9124 /* Reset/increment for the next run. */
9125 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9126 it->current_x = line_start_x;
9127 line_start_x = 0;
9128 it->hpos = 0;
9129 it->current_y += it->max_ascent + it->max_descent;
9130 ++it->vpos;
9131 last_height = it->max_ascent + it->max_descent;
9132 it->max_ascent = it->max_descent = 0;
9135 out:
9137 /* On text terminals, we may stop at the end of a line in the middle
9138 of a multi-character glyph. If the glyph itself is continued,
9139 i.e. it is actually displayed on the next line, don't treat this
9140 stopping point as valid; move to the next line instead (unless
9141 that brings us offscreen). */
9142 if (!FRAME_WINDOW_P (it->f)
9143 && op & MOVE_TO_POS
9144 && IT_CHARPOS (*it) == to_charpos
9145 && it->what == IT_CHARACTER
9146 && it->nglyphs > 1
9147 && it->line_wrap == WINDOW_WRAP
9148 && it->current_x == it->last_visible_x - 1
9149 && it->c != '\n'
9150 && it->c != '\t'
9151 && it->vpos < it->w->window_end_vpos)
9153 it->continuation_lines_width += it->current_x;
9154 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9155 it->current_y += it->max_ascent + it->max_descent;
9156 ++it->vpos;
9157 last_height = it->max_ascent + it->max_descent;
9160 if (backup_data)
9161 bidi_unshelve_cache (backup_data, 1);
9163 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9165 return max_current_x;
9169 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9171 If DY > 0, move IT backward at least that many pixels. DY = 0
9172 means move IT backward to the preceding line start or BEGV. This
9173 function may move over more than DY pixels if IT->current_y - DY
9174 ends up in the middle of a line; in this case IT->current_y will be
9175 set to the top of the line moved to. */
9177 void
9178 move_it_vertically_backward (struct it *it, int dy)
9180 int nlines, h;
9181 struct it it2, it3;
9182 void *it2data = NULL, *it3data = NULL;
9183 ptrdiff_t start_pos;
9184 int nchars_per_row
9185 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9186 ptrdiff_t pos_limit;
9188 move_further_back:
9189 eassert (dy >= 0);
9191 start_pos = IT_CHARPOS (*it);
9193 /* Estimate how many newlines we must move back. */
9194 nlines = max (1, dy / default_line_pixel_height (it->w));
9195 if (it->line_wrap == TRUNCATE)
9196 pos_limit = BEGV;
9197 else
9198 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9200 /* Set the iterator's position that many lines back. But don't go
9201 back more than NLINES full screen lines -- this wins a day with
9202 buffers which have very long lines. */
9203 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9204 back_to_previous_visible_line_start (it);
9206 /* Reseat the iterator here. When moving backward, we don't want
9207 reseat to skip forward over invisible text, set up the iterator
9208 to deliver from overlay strings at the new position etc. So,
9209 use reseat_1 here. */
9210 reseat_1 (it, it->current.pos, 1);
9212 /* We are now surely at a line start. */
9213 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9214 reordering is in effect. */
9215 it->continuation_lines_width = 0;
9217 /* Move forward and see what y-distance we moved. First move to the
9218 start of the next line so that we get its height. We need this
9219 height to be able to tell whether we reached the specified
9220 y-distance. */
9221 SAVE_IT (it2, *it, it2data);
9222 it2.max_ascent = it2.max_descent = 0;
9225 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9226 MOVE_TO_POS | MOVE_TO_VPOS);
9228 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9229 /* If we are in a display string which starts at START_POS,
9230 and that display string includes a newline, and we are
9231 right after that newline (i.e. at the beginning of a
9232 display line), exit the loop, because otherwise we will
9233 infloop, since move_it_to will see that it is already at
9234 START_POS and will not move. */
9235 || (it2.method == GET_FROM_STRING
9236 && IT_CHARPOS (it2) == start_pos
9237 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9238 eassert (IT_CHARPOS (*it) >= BEGV);
9239 SAVE_IT (it3, it2, it3data);
9241 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9242 eassert (IT_CHARPOS (*it) >= BEGV);
9243 /* H is the actual vertical distance from the position in *IT
9244 and the starting position. */
9245 h = it2.current_y - it->current_y;
9246 /* NLINES is the distance in number of lines. */
9247 nlines = it2.vpos - it->vpos;
9249 /* Correct IT's y and vpos position
9250 so that they are relative to the starting point. */
9251 it->vpos -= nlines;
9252 it->current_y -= h;
9254 if (dy == 0)
9256 /* DY == 0 means move to the start of the screen line. The
9257 value of nlines is > 0 if continuation lines were involved,
9258 or if the original IT position was at start of a line. */
9259 RESTORE_IT (it, it, it2data);
9260 if (nlines > 0)
9261 move_it_by_lines (it, nlines);
9262 /* The above code moves us to some position NLINES down,
9263 usually to its first glyph (leftmost in an L2R line), but
9264 that's not necessarily the start of the line, under bidi
9265 reordering. We want to get to the character position
9266 that is immediately after the newline of the previous
9267 line. */
9268 if (it->bidi_p
9269 && !it->continuation_lines_width
9270 && !STRINGP (it->string)
9271 && IT_CHARPOS (*it) > BEGV
9272 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9274 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9276 DEC_BOTH (cp, bp);
9277 cp = find_newline_no_quit (cp, bp, -1, NULL);
9278 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9280 bidi_unshelve_cache (it3data, 1);
9282 else
9284 /* The y-position we try to reach, relative to *IT.
9285 Note that H has been subtracted in front of the if-statement. */
9286 int target_y = it->current_y + h - dy;
9287 int y0 = it3.current_y;
9288 int y1;
9289 int line_height;
9291 RESTORE_IT (&it3, &it3, it3data);
9292 y1 = line_bottom_y (&it3);
9293 line_height = y1 - y0;
9294 RESTORE_IT (it, it, it2data);
9295 /* If we did not reach target_y, try to move further backward if
9296 we can. If we moved too far backward, try to move forward. */
9297 if (target_y < it->current_y
9298 /* This is heuristic. In a window that's 3 lines high, with
9299 a line height of 13 pixels each, recentering with point
9300 on the bottom line will try to move -39/2 = 19 pixels
9301 backward. Try to avoid moving into the first line. */
9302 && (it->current_y - target_y
9303 > min (window_box_height (it->w), line_height * 2 / 3))
9304 && IT_CHARPOS (*it) > BEGV)
9306 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9307 target_y - it->current_y));
9308 dy = it->current_y - target_y;
9309 goto move_further_back;
9311 else if (target_y >= it->current_y + line_height
9312 && IT_CHARPOS (*it) < ZV)
9314 /* Should move forward by at least one line, maybe more.
9316 Note: Calling move_it_by_lines can be expensive on
9317 terminal frames, where compute_motion is used (via
9318 vmotion) to do the job, when there are very long lines
9319 and truncate-lines is nil. That's the reason for
9320 treating terminal frames specially here. */
9322 if (!FRAME_WINDOW_P (it->f))
9323 move_it_vertically (it, target_y - (it->current_y + line_height));
9324 else
9328 move_it_by_lines (it, 1);
9330 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9337 /* Move IT by a specified amount of pixel lines DY. DY negative means
9338 move backwards. DY = 0 means move to start of screen line. At the
9339 end, IT will be on the start of a screen line. */
9341 void
9342 move_it_vertically (struct it *it, int dy)
9344 if (dy <= 0)
9345 move_it_vertically_backward (it, -dy);
9346 else
9348 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9349 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9350 MOVE_TO_POS | MOVE_TO_Y);
9351 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9353 /* If buffer ends in ZV without a newline, move to the start of
9354 the line to satisfy the post-condition. */
9355 if (IT_CHARPOS (*it) == ZV
9356 && ZV > BEGV
9357 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9358 move_it_by_lines (it, 0);
9363 /* Move iterator IT past the end of the text line it is in. */
9365 void
9366 move_it_past_eol (struct it *it)
9368 enum move_it_result rc;
9370 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9371 if (rc == MOVE_NEWLINE_OR_CR)
9372 set_iterator_to_next (it, 0);
9376 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9377 negative means move up. DVPOS == 0 means move to the start of the
9378 screen line.
9380 Optimization idea: If we would know that IT->f doesn't use
9381 a face with proportional font, we could be faster for
9382 truncate-lines nil. */
9384 void
9385 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9388 /* The commented-out optimization uses vmotion on terminals. This
9389 gives bad results, because elements like it->what, on which
9390 callers such as pos_visible_p rely, aren't updated. */
9391 /* struct position pos;
9392 if (!FRAME_WINDOW_P (it->f))
9394 struct text_pos textpos;
9396 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9397 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9398 reseat (it, textpos, 1);
9399 it->vpos += pos.vpos;
9400 it->current_y += pos.vpos;
9402 else */
9404 if (dvpos == 0)
9406 /* DVPOS == 0 means move to the start of the screen line. */
9407 move_it_vertically_backward (it, 0);
9408 /* Let next call to line_bottom_y calculate real line height. */
9409 last_height = 0;
9411 else if (dvpos > 0)
9413 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9414 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9416 /* Only move to the next buffer position if we ended up in a
9417 string from display property, not in an overlay string
9418 (before-string or after-string). That is because the
9419 latter don't conceal the underlying buffer position, so
9420 we can ask to move the iterator to the exact position we
9421 are interested in. Note that, even if we are already at
9422 IT_CHARPOS (*it), the call below is not a no-op, as it
9423 will detect that we are at the end of the string, pop the
9424 iterator, and compute it->current_x and it->hpos
9425 correctly. */
9426 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9427 -1, -1, -1, MOVE_TO_POS);
9430 else
9432 struct it it2;
9433 void *it2data = NULL;
9434 ptrdiff_t start_charpos, i;
9435 int nchars_per_row
9436 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9437 ptrdiff_t pos_limit;
9439 /* Start at the beginning of the screen line containing IT's
9440 position. This may actually move vertically backwards,
9441 in case of overlays, so adjust dvpos accordingly. */
9442 dvpos += it->vpos;
9443 move_it_vertically_backward (it, 0);
9444 dvpos -= it->vpos;
9446 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9447 screen lines, and reseat the iterator there. */
9448 start_charpos = IT_CHARPOS (*it);
9449 if (it->line_wrap == TRUNCATE)
9450 pos_limit = BEGV;
9451 else
9452 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9453 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9454 back_to_previous_visible_line_start (it);
9455 reseat (it, it->current.pos, 1);
9457 /* Move further back if we end up in a string or an image. */
9458 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9460 /* First try to move to start of display line. */
9461 dvpos += it->vpos;
9462 move_it_vertically_backward (it, 0);
9463 dvpos -= it->vpos;
9464 if (IT_POS_VALID_AFTER_MOVE_P (it))
9465 break;
9466 /* If start of line is still in string or image,
9467 move further back. */
9468 back_to_previous_visible_line_start (it);
9469 reseat (it, it->current.pos, 1);
9470 dvpos--;
9473 it->current_x = it->hpos = 0;
9475 /* Above call may have moved too far if continuation lines
9476 are involved. Scan forward and see if it did. */
9477 SAVE_IT (it2, *it, it2data);
9478 it2.vpos = it2.current_y = 0;
9479 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9480 it->vpos -= it2.vpos;
9481 it->current_y -= it2.current_y;
9482 it->current_x = it->hpos = 0;
9484 /* If we moved too far back, move IT some lines forward. */
9485 if (it2.vpos > -dvpos)
9487 int delta = it2.vpos + dvpos;
9489 RESTORE_IT (&it2, &it2, it2data);
9490 SAVE_IT (it2, *it, it2data);
9491 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9492 /* Move back again if we got too far ahead. */
9493 if (IT_CHARPOS (*it) >= start_charpos)
9494 RESTORE_IT (it, &it2, it2data);
9495 else
9496 bidi_unshelve_cache (it2data, 1);
9498 else
9499 RESTORE_IT (it, it, it2data);
9503 /* Return true if IT points into the middle of a display vector. */
9505 bool
9506 in_display_vector_p (struct it *it)
9508 return (it->method == GET_FROM_DISPLAY_VECTOR
9509 && it->current.dpvec_index > 0
9510 && it->dpvec + it->current.dpvec_index != it->dpend);
9513 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9514 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9515 WINDOW must be a live window and defaults to the selected one. The
9516 return value is a cons of the maximum pixel-width of any text line and
9517 the maximum pixel-height of all text lines.
9519 The optional argument FROM, if non-nil, specifies the first text
9520 position and defaults to the minimum accessible position of the buffer.
9521 If FROM is t, use the minimum accessible position that is not a newline
9522 character. TO, if non-nil, specifies the last text position and
9523 defaults to the maximum accessible position of the buffer. If TO is t,
9524 use the maximum accessible position that is not a newline character.
9526 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9527 width that can be returned. X_LIMIT nil or omitted, means to use the
9528 pixel-width of WINDOW's body; use this if you do not intend to change
9529 the width of WINDOW. Use the maximum width WINDOW may assume if you
9530 intend to change WINDOW's width.
9532 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9533 height that can be returned. Text lines whose y-coordinate is beyond
9534 Y_LIMIT are ignored. Since calculating the text height of a large
9535 buffer can take some time, it makes sense to specify this argument if
9536 the size of the buffer is unknown.
9538 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9539 include the height of the mode- or header-line of WINDOW in the return
9540 value. If it is either the symbol `mode-line' or `header-line', include
9541 only the height of that line, if present, in the return value. If t,
9542 include the height of both, if present, in the return value. */)
9543 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9544 Lisp_Object mode_and_header_line)
9546 struct window *w = decode_live_window (window);
9547 Lisp_Object buf;
9548 struct buffer *b;
9549 struct it it;
9550 struct buffer *old_buffer = NULL;
9551 ptrdiff_t start, end, pos;
9552 struct text_pos startp;
9553 void *itdata = NULL;
9554 int c, max_y = -1, x = 0, y = 0;
9556 buf = w->contents;
9557 CHECK_BUFFER (buf);
9558 b = XBUFFER (buf);
9560 if (b != current_buffer)
9562 old_buffer = current_buffer;
9563 set_buffer_internal (b);
9566 if (NILP (from))
9567 start = BEGV;
9568 else if (EQ (from, Qt))
9570 start = pos = BEGV;
9571 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9572 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9573 start = pos;
9574 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9575 start = pos;
9577 else
9579 CHECK_NUMBER_COERCE_MARKER (from);
9580 start = min (max (XINT (from), BEGV), ZV);
9583 if (NILP (to))
9584 end = ZV;
9585 else if (EQ (to, Qt))
9587 end = pos = ZV;
9588 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9589 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9590 end = pos;
9591 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9592 end = pos;
9594 else
9596 CHECK_NUMBER_COERCE_MARKER (to);
9597 end = max (start, min (XINT (to), ZV));
9600 if (!NILP (y_limit))
9602 CHECK_NUMBER (y_limit);
9603 max_y = min (XINT (y_limit), INT_MAX);
9606 itdata = bidi_shelve_cache ();
9607 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9608 start_display (&it, w, startp);
9610 if (NILP (x_limit))
9611 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9612 else
9614 CHECK_NUMBER (x_limit);
9615 it.last_visible_x = min (XINT (x_limit), INFINITY);
9616 /* Actually, we never want move_it_to stop at to_x. But to make
9617 sure that move_it_in_display_line_to always moves far enough,
9618 we set it to INT_MAX and specify MOVE_TO_X. */
9619 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9620 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9623 y = it.current_y + it.max_ascent + it.max_descent;
9625 if (!EQ (mode_and_header_line, Qheader_line)
9626 && !EQ (mode_and_header_line, Qt))
9627 /* Do not count the header-line which was counted automatically by
9628 start_display. */
9629 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9631 if (EQ (mode_and_header_line, Qmode_line)
9632 || EQ (mode_and_header_line, Qt))
9633 /* Do count the mode-line which is not included automatically by
9634 start_display. */
9635 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9637 bidi_unshelve_cache (itdata, 0);
9639 if (old_buffer)
9640 set_buffer_internal (old_buffer);
9642 return Fcons (make_number (x), make_number (y));
9645 /***********************************************************************
9646 Messages
9647 ***********************************************************************/
9650 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9651 to *Messages*. */
9653 void
9654 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9656 Lisp_Object args[3];
9657 Lisp_Object msg, fmt;
9658 char *buffer;
9659 ptrdiff_t len;
9660 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9661 USE_SAFE_ALLOCA;
9663 fmt = msg = Qnil;
9664 GCPRO4 (fmt, msg, arg1, arg2);
9666 args[0] = fmt = build_string (format);
9667 args[1] = arg1;
9668 args[2] = arg2;
9669 msg = Fformat (3, args);
9671 len = SBYTES (msg) + 1;
9672 buffer = SAFE_ALLOCA (len);
9673 memcpy (buffer, SDATA (msg), len);
9675 message_dolog (buffer, len - 1, 1, 0);
9676 SAFE_FREE ();
9678 UNGCPRO;
9682 /* Output a newline in the *Messages* buffer if "needs" one. */
9684 void
9685 message_log_maybe_newline (void)
9687 if (message_log_need_newline)
9688 message_dolog ("", 0, 1, 0);
9692 /* Add a string M of length NBYTES to the message log, optionally
9693 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9694 true, means interpret the contents of M as multibyte. This
9695 function calls low-level routines in order to bypass text property
9696 hooks, etc. which might not be safe to run.
9698 This may GC (insert may run before/after change hooks),
9699 so the buffer M must NOT point to a Lisp string. */
9701 void
9702 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9704 const unsigned char *msg = (const unsigned char *) m;
9706 if (!NILP (Vmemory_full))
9707 return;
9709 if (!NILP (Vmessage_log_max))
9711 struct buffer *oldbuf;
9712 Lisp_Object oldpoint, oldbegv, oldzv;
9713 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9714 ptrdiff_t point_at_end = 0;
9715 ptrdiff_t zv_at_end = 0;
9716 Lisp_Object old_deactivate_mark;
9717 struct gcpro gcpro1;
9719 old_deactivate_mark = Vdeactivate_mark;
9720 oldbuf = current_buffer;
9722 /* Ensure the Messages buffer exists, and switch to it.
9723 If we created it, set the major-mode. */
9725 int newbuffer = 0;
9726 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9728 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9730 if (newbuffer
9731 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9732 call0 (intern ("messages-buffer-mode"));
9735 bset_undo_list (current_buffer, Qt);
9736 bset_cache_long_scans (current_buffer, Qnil);
9738 oldpoint = message_dolog_marker1;
9739 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9740 oldbegv = message_dolog_marker2;
9741 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9742 oldzv = message_dolog_marker3;
9743 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9744 GCPRO1 (old_deactivate_mark);
9746 if (PT == Z)
9747 point_at_end = 1;
9748 if (ZV == Z)
9749 zv_at_end = 1;
9751 BEGV = BEG;
9752 BEGV_BYTE = BEG_BYTE;
9753 ZV = Z;
9754 ZV_BYTE = Z_BYTE;
9755 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9757 /* Insert the string--maybe converting multibyte to single byte
9758 or vice versa, so that all the text fits the buffer. */
9759 if (multibyte
9760 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9762 ptrdiff_t i;
9763 int c, char_bytes;
9764 char work[1];
9766 /* Convert a multibyte string to single-byte
9767 for the *Message* buffer. */
9768 for (i = 0; i < nbytes; i += char_bytes)
9770 c = string_char_and_length (msg + i, &char_bytes);
9771 work[0] = (ASCII_CHAR_P (c)
9773 : multibyte_char_to_unibyte (c));
9774 insert_1_both (work, 1, 1, 1, 0, 0);
9777 else if (! multibyte
9778 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9780 ptrdiff_t i;
9781 int c, char_bytes;
9782 unsigned char str[MAX_MULTIBYTE_LENGTH];
9783 /* Convert a single-byte string to multibyte
9784 for the *Message* buffer. */
9785 for (i = 0; i < nbytes; i++)
9787 c = msg[i];
9788 MAKE_CHAR_MULTIBYTE (c);
9789 char_bytes = CHAR_STRING (c, str);
9790 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9793 else if (nbytes)
9794 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9796 if (nlflag)
9798 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9799 printmax_t dups;
9801 insert_1_both ("\n", 1, 1, 1, 0, 0);
9803 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9804 this_bol = PT;
9805 this_bol_byte = PT_BYTE;
9807 /* See if this line duplicates the previous one.
9808 If so, combine duplicates. */
9809 if (this_bol > BEG)
9811 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9812 prev_bol = PT;
9813 prev_bol_byte = PT_BYTE;
9815 dups = message_log_check_duplicate (prev_bol_byte,
9816 this_bol_byte);
9817 if (dups)
9819 del_range_both (prev_bol, prev_bol_byte,
9820 this_bol, this_bol_byte, 0);
9821 if (dups > 1)
9823 char dupstr[sizeof " [ times]"
9824 + INT_STRLEN_BOUND (printmax_t)];
9826 /* If you change this format, don't forget to also
9827 change message_log_check_duplicate. */
9828 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9829 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9830 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9835 /* If we have more than the desired maximum number of lines
9836 in the *Messages* buffer now, delete the oldest ones.
9837 This is safe because we don't have undo in this buffer. */
9839 if (NATNUMP (Vmessage_log_max))
9841 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9842 -XFASTINT (Vmessage_log_max) - 1, 0);
9843 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9846 BEGV = marker_position (oldbegv);
9847 BEGV_BYTE = marker_byte_position (oldbegv);
9849 if (zv_at_end)
9851 ZV = Z;
9852 ZV_BYTE = Z_BYTE;
9854 else
9856 ZV = marker_position (oldzv);
9857 ZV_BYTE = marker_byte_position (oldzv);
9860 if (point_at_end)
9861 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9862 else
9863 /* We can't do Fgoto_char (oldpoint) because it will run some
9864 Lisp code. */
9865 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9866 marker_byte_position (oldpoint));
9868 UNGCPRO;
9869 unchain_marker (XMARKER (oldpoint));
9870 unchain_marker (XMARKER (oldbegv));
9871 unchain_marker (XMARKER (oldzv));
9873 /* We called insert_1_both above with its 5th argument (PREPARE)
9874 zero, which prevents insert_1_both from calling
9875 prepare_to_modify_buffer, which in turns prevents us from
9876 incrementing windows_or_buffers_changed even if *Messages* is
9877 shown in some window. So we must manually set
9878 windows_or_buffers_changed here to make up for that. */
9879 windows_or_buffers_changed = old_windows_or_buffers_changed;
9880 bset_redisplay (current_buffer);
9882 set_buffer_internal (oldbuf);
9884 message_log_need_newline = !nlflag;
9885 Vdeactivate_mark = old_deactivate_mark;
9890 /* We are at the end of the buffer after just having inserted a newline.
9891 (Note: We depend on the fact we won't be crossing the gap.)
9892 Check to see if the most recent message looks a lot like the previous one.
9893 Return 0 if different, 1 if the new one should just replace it, or a
9894 value N > 1 if we should also append " [N times]". */
9896 static intmax_t
9897 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9899 ptrdiff_t i;
9900 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9901 int seen_dots = 0;
9902 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9903 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9905 for (i = 0; i < len; i++)
9907 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9908 seen_dots = 1;
9909 if (p1[i] != p2[i])
9910 return seen_dots;
9912 p1 += len;
9913 if (*p1 == '\n')
9914 return 2;
9915 if (*p1++ == ' ' && *p1++ == '[')
9917 char *pend;
9918 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9919 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9920 return n + 1;
9922 return 0;
9926 /* Display an echo area message M with a specified length of NBYTES
9927 bytes. The string may include null characters. If M is not a
9928 string, clear out any existing message, and let the mini-buffer
9929 text show through.
9931 This function cancels echoing. */
9933 void
9934 message3 (Lisp_Object m)
9936 struct gcpro gcpro1;
9938 GCPRO1 (m);
9939 clear_message (true, true);
9940 cancel_echoing ();
9942 /* First flush out any partial line written with print. */
9943 message_log_maybe_newline ();
9944 if (STRINGP (m))
9946 ptrdiff_t nbytes = SBYTES (m);
9947 bool multibyte = STRING_MULTIBYTE (m);
9948 USE_SAFE_ALLOCA;
9949 char *buffer = SAFE_ALLOCA (nbytes);
9950 memcpy (buffer, SDATA (m), nbytes);
9951 message_dolog (buffer, nbytes, 1, multibyte);
9952 SAFE_FREE ();
9954 message3_nolog (m);
9956 UNGCPRO;
9960 /* The non-logging version of message3.
9961 This does not cancel echoing, because it is used for echoing.
9962 Perhaps we need to make a separate function for echoing
9963 and make this cancel echoing. */
9965 void
9966 message3_nolog (Lisp_Object m)
9968 struct frame *sf = SELECTED_FRAME ();
9970 if (FRAME_INITIAL_P (sf))
9972 if (noninteractive_need_newline)
9973 putc ('\n', stderr);
9974 noninteractive_need_newline = 0;
9975 if (STRINGP (m))
9977 Lisp_Object s = ENCODE_SYSTEM (m);
9979 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9981 if (cursor_in_echo_area == 0)
9982 fprintf (stderr, "\n");
9983 fflush (stderr);
9985 /* Error messages get reported properly by cmd_error, so this must be just an
9986 informative message; if the frame hasn't really been initialized yet, just
9987 toss it. */
9988 else if (INTERACTIVE && sf->glyphs_initialized_p)
9990 /* Get the frame containing the mini-buffer
9991 that the selected frame is using. */
9992 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9993 Lisp_Object frame = XWINDOW (mini_window)->frame;
9994 struct frame *f = XFRAME (frame);
9996 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9997 Fmake_frame_visible (frame);
9999 if (STRINGP (m) && SCHARS (m) > 0)
10001 set_message (m);
10002 if (minibuffer_auto_raise)
10003 Fraise_frame (frame);
10004 /* Assume we are not echoing.
10005 (If we are, echo_now will override this.) */
10006 echo_message_buffer = Qnil;
10008 else
10009 clear_message (true, true);
10011 do_pending_window_change (0);
10012 echo_area_display (1);
10013 do_pending_window_change (0);
10014 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10015 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10020 /* Display a null-terminated echo area message M. If M is 0, clear
10021 out any existing message, and let the mini-buffer text show through.
10023 The buffer M must continue to exist until after the echo area gets
10024 cleared or some other message gets displayed there. Do not pass
10025 text that is stored in a Lisp string. Do not pass text in a buffer
10026 that was alloca'd. */
10028 void
10029 message1 (const char *m)
10031 message3 (m ? build_unibyte_string (m) : Qnil);
10035 /* The non-logging counterpart of message1. */
10037 void
10038 message1_nolog (const char *m)
10040 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10043 /* Display a message M which contains a single %s
10044 which gets replaced with STRING. */
10046 void
10047 message_with_string (const char *m, Lisp_Object string, int log)
10049 CHECK_STRING (string);
10051 if (noninteractive)
10053 if (m)
10055 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10056 String whose data pointer might be passed to us in M. So
10057 we use a local copy. */
10058 char *fmt = xstrdup (m);
10060 if (noninteractive_need_newline)
10061 putc ('\n', stderr);
10062 noninteractive_need_newline = 0;
10063 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10064 if (!cursor_in_echo_area)
10065 fprintf (stderr, "\n");
10066 fflush (stderr);
10067 xfree (fmt);
10070 else if (INTERACTIVE)
10072 /* The frame whose minibuffer we're going to display the message on.
10073 It may be larger than the selected frame, so we need
10074 to use its buffer, not the selected frame's buffer. */
10075 Lisp_Object mini_window;
10076 struct frame *f, *sf = SELECTED_FRAME ();
10078 /* Get the frame containing the minibuffer
10079 that the selected frame is using. */
10080 mini_window = FRAME_MINIBUF_WINDOW (sf);
10081 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10083 /* Error messages get reported properly by cmd_error, so this must be
10084 just an informative message; if the frame hasn't really been
10085 initialized yet, just toss it. */
10086 if (f->glyphs_initialized_p)
10088 Lisp_Object args[2], msg;
10089 struct gcpro gcpro1, gcpro2;
10091 args[0] = build_string (m);
10092 args[1] = msg = string;
10093 GCPRO2 (args[0], msg);
10094 gcpro1.nvars = 2;
10096 msg = Fformat (2, args);
10098 if (log)
10099 message3 (msg);
10100 else
10101 message3_nolog (msg);
10103 UNGCPRO;
10105 /* Print should start at the beginning of the message
10106 buffer next time. */
10107 message_buf_print = 0;
10113 /* Dump an informative message to the minibuf. If M is 0, clear out
10114 any existing message, and let the mini-buffer text show through. */
10116 static void
10117 vmessage (const char *m, va_list ap)
10119 if (noninteractive)
10121 if (m)
10123 if (noninteractive_need_newline)
10124 putc ('\n', stderr);
10125 noninteractive_need_newline = 0;
10126 vfprintf (stderr, m, ap);
10127 if (cursor_in_echo_area == 0)
10128 fprintf (stderr, "\n");
10129 fflush (stderr);
10132 else if (INTERACTIVE)
10134 /* The frame whose mini-buffer we're going to display the message
10135 on. It may be larger than the selected frame, so we need to
10136 use its buffer, not the selected frame's buffer. */
10137 Lisp_Object mini_window;
10138 struct frame *f, *sf = SELECTED_FRAME ();
10140 /* Get the frame containing the mini-buffer
10141 that the selected frame is using. */
10142 mini_window = FRAME_MINIBUF_WINDOW (sf);
10143 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10145 /* Error messages get reported properly by cmd_error, so this must be
10146 just an informative message; if the frame hasn't really been
10147 initialized yet, just toss it. */
10148 if (f->glyphs_initialized_p)
10150 if (m)
10152 ptrdiff_t len;
10153 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10154 char *message_buf = alloca (maxsize + 1);
10156 len = doprnt (message_buf, maxsize, m, 0, ap);
10158 message3 (make_string (message_buf, len));
10160 else
10161 message1 (0);
10163 /* Print should start at the beginning of the message
10164 buffer next time. */
10165 message_buf_print = 0;
10170 void
10171 message (const char *m, ...)
10173 va_list ap;
10174 va_start (ap, m);
10175 vmessage (m, ap);
10176 va_end (ap);
10180 #if 0
10181 /* The non-logging version of message. */
10183 void
10184 message_nolog (const char *m, ...)
10186 Lisp_Object old_log_max;
10187 va_list ap;
10188 va_start (ap, m);
10189 old_log_max = Vmessage_log_max;
10190 Vmessage_log_max = Qnil;
10191 vmessage (m, ap);
10192 Vmessage_log_max = old_log_max;
10193 va_end (ap);
10195 #endif
10198 /* Display the current message in the current mini-buffer. This is
10199 only called from error handlers in process.c, and is not time
10200 critical. */
10202 void
10203 update_echo_area (void)
10205 if (!NILP (echo_area_buffer[0]))
10207 Lisp_Object string;
10208 string = Fcurrent_message ();
10209 message3 (string);
10214 /* Make sure echo area buffers in `echo_buffers' are live.
10215 If they aren't, make new ones. */
10217 static void
10218 ensure_echo_area_buffers (void)
10220 int i;
10222 for (i = 0; i < 2; ++i)
10223 if (!BUFFERP (echo_buffer[i])
10224 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10226 char name[30];
10227 Lisp_Object old_buffer;
10228 int j;
10230 old_buffer = echo_buffer[i];
10231 echo_buffer[i] = Fget_buffer_create
10232 (make_formatted_string (name, " *Echo Area %d*", i));
10233 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10234 /* to force word wrap in echo area -
10235 it was decided to postpone this*/
10236 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10238 for (j = 0; j < 2; ++j)
10239 if (EQ (old_buffer, echo_area_buffer[j]))
10240 echo_area_buffer[j] = echo_buffer[i];
10245 /* Call FN with args A1..A2 with either the current or last displayed
10246 echo_area_buffer as current buffer.
10248 WHICH zero means use the current message buffer
10249 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10250 from echo_buffer[] and clear it.
10252 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10253 suitable buffer from echo_buffer[] and clear it.
10255 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10256 that the current message becomes the last displayed one, make
10257 choose a suitable buffer for echo_area_buffer[0], and clear it.
10259 Value is what FN returns. */
10261 static int
10262 with_echo_area_buffer (struct window *w, int which,
10263 int (*fn) (ptrdiff_t, Lisp_Object),
10264 ptrdiff_t a1, Lisp_Object a2)
10266 Lisp_Object buffer;
10267 int this_one, the_other, clear_buffer_p, rc;
10268 ptrdiff_t count = SPECPDL_INDEX ();
10270 /* If buffers aren't live, make new ones. */
10271 ensure_echo_area_buffers ();
10273 clear_buffer_p = 0;
10275 if (which == 0)
10276 this_one = 0, the_other = 1;
10277 else if (which > 0)
10278 this_one = 1, the_other = 0;
10279 else
10281 this_one = 0, the_other = 1;
10282 clear_buffer_p = true;
10284 /* We need a fresh one in case the current echo buffer equals
10285 the one containing the last displayed echo area message. */
10286 if (!NILP (echo_area_buffer[this_one])
10287 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10288 echo_area_buffer[this_one] = Qnil;
10291 /* Choose a suitable buffer from echo_buffer[] is we don't
10292 have one. */
10293 if (NILP (echo_area_buffer[this_one]))
10295 echo_area_buffer[this_one]
10296 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10297 ? echo_buffer[the_other]
10298 : echo_buffer[this_one]);
10299 clear_buffer_p = true;
10302 buffer = echo_area_buffer[this_one];
10304 /* Don't get confused by reusing the buffer used for echoing
10305 for a different purpose. */
10306 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10307 cancel_echoing ();
10309 record_unwind_protect (unwind_with_echo_area_buffer,
10310 with_echo_area_buffer_unwind_data (w));
10312 /* Make the echo area buffer current. Note that for display
10313 purposes, it is not necessary that the displayed window's buffer
10314 == current_buffer, except for text property lookup. So, let's
10315 only set that buffer temporarily here without doing a full
10316 Fset_window_buffer. We must also change w->pointm, though,
10317 because otherwise an assertions in unshow_buffer fails, and Emacs
10318 aborts. */
10319 set_buffer_internal_1 (XBUFFER (buffer));
10320 if (w)
10322 wset_buffer (w, buffer);
10323 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10326 bset_undo_list (current_buffer, Qt);
10327 bset_read_only (current_buffer, Qnil);
10328 specbind (Qinhibit_read_only, Qt);
10329 specbind (Qinhibit_modification_hooks, Qt);
10331 if (clear_buffer_p && Z > BEG)
10332 del_range (BEG, Z);
10334 eassert (BEGV >= BEG);
10335 eassert (ZV <= Z && ZV >= BEGV);
10337 rc = fn (a1, a2);
10339 eassert (BEGV >= BEG);
10340 eassert (ZV <= Z && ZV >= BEGV);
10342 unbind_to (count, Qnil);
10343 return rc;
10347 /* Save state that should be preserved around the call to the function
10348 FN called in with_echo_area_buffer. */
10350 static Lisp_Object
10351 with_echo_area_buffer_unwind_data (struct window *w)
10353 int i = 0;
10354 Lisp_Object vector, tmp;
10356 /* Reduce consing by keeping one vector in
10357 Vwith_echo_area_save_vector. */
10358 vector = Vwith_echo_area_save_vector;
10359 Vwith_echo_area_save_vector = Qnil;
10361 if (NILP (vector))
10362 vector = Fmake_vector (make_number (9), Qnil);
10364 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10365 ASET (vector, i, Vdeactivate_mark); ++i;
10366 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10368 if (w)
10370 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10371 ASET (vector, i, w->contents); ++i;
10372 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10373 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10374 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10375 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10377 else
10379 int end = i + 6;
10380 for (; i < end; ++i)
10381 ASET (vector, i, Qnil);
10384 eassert (i == ASIZE (vector));
10385 return vector;
10389 /* Restore global state from VECTOR which was created by
10390 with_echo_area_buffer_unwind_data. */
10392 static void
10393 unwind_with_echo_area_buffer (Lisp_Object vector)
10395 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10396 Vdeactivate_mark = AREF (vector, 1);
10397 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10399 if (WINDOWP (AREF (vector, 3)))
10401 struct window *w;
10402 Lisp_Object buffer;
10404 w = XWINDOW (AREF (vector, 3));
10405 buffer = AREF (vector, 4);
10407 wset_buffer (w, buffer);
10408 set_marker_both (w->pointm, buffer,
10409 XFASTINT (AREF (vector, 5)),
10410 XFASTINT (AREF (vector, 6)));
10411 set_marker_both (w->start, buffer,
10412 XFASTINT (AREF (vector, 7)),
10413 XFASTINT (AREF (vector, 8)));
10416 Vwith_echo_area_save_vector = vector;
10420 /* Set up the echo area for use by print functions. MULTIBYTE_P
10421 non-zero means we will print multibyte. */
10423 void
10424 setup_echo_area_for_printing (int multibyte_p)
10426 /* If we can't find an echo area any more, exit. */
10427 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10428 Fkill_emacs (Qnil);
10430 ensure_echo_area_buffers ();
10432 if (!message_buf_print)
10434 /* A message has been output since the last time we printed.
10435 Choose a fresh echo area buffer. */
10436 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10437 echo_area_buffer[0] = echo_buffer[1];
10438 else
10439 echo_area_buffer[0] = echo_buffer[0];
10441 /* Switch to that buffer and clear it. */
10442 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10443 bset_truncate_lines (current_buffer, Qnil);
10445 if (Z > BEG)
10447 ptrdiff_t count = SPECPDL_INDEX ();
10448 specbind (Qinhibit_read_only, Qt);
10449 /* Note that undo recording is always disabled. */
10450 del_range (BEG, Z);
10451 unbind_to (count, Qnil);
10453 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10455 /* Set up the buffer for the multibyteness we need. */
10456 if (multibyte_p
10457 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10458 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10460 /* Raise the frame containing the echo area. */
10461 if (minibuffer_auto_raise)
10463 struct frame *sf = SELECTED_FRAME ();
10464 Lisp_Object mini_window;
10465 mini_window = FRAME_MINIBUF_WINDOW (sf);
10466 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10469 message_log_maybe_newline ();
10470 message_buf_print = 1;
10472 else
10474 if (NILP (echo_area_buffer[0]))
10476 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10477 echo_area_buffer[0] = echo_buffer[1];
10478 else
10479 echo_area_buffer[0] = echo_buffer[0];
10482 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10484 /* Someone switched buffers between print requests. */
10485 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10486 bset_truncate_lines (current_buffer, Qnil);
10492 /* Display an echo area message in window W. Value is non-zero if W's
10493 height is changed. If display_last_displayed_message_p is
10494 non-zero, display the message that was last displayed, otherwise
10495 display the current message. */
10497 static int
10498 display_echo_area (struct window *w)
10500 int i, no_message_p, window_height_changed_p;
10502 /* Temporarily disable garbage collections while displaying the echo
10503 area. This is done because a GC can print a message itself.
10504 That message would modify the echo area buffer's contents while a
10505 redisplay of the buffer is going on, and seriously confuse
10506 redisplay. */
10507 ptrdiff_t count = inhibit_garbage_collection ();
10509 /* If there is no message, we must call display_echo_area_1
10510 nevertheless because it resizes the window. But we will have to
10511 reset the echo_area_buffer in question to nil at the end because
10512 with_echo_area_buffer will sets it to an empty buffer. */
10513 i = display_last_displayed_message_p ? 1 : 0;
10514 no_message_p = NILP (echo_area_buffer[i]);
10516 window_height_changed_p
10517 = with_echo_area_buffer (w, display_last_displayed_message_p,
10518 display_echo_area_1,
10519 (intptr_t) w, Qnil);
10521 if (no_message_p)
10522 echo_area_buffer[i] = Qnil;
10524 unbind_to (count, Qnil);
10525 return window_height_changed_p;
10529 /* Helper for display_echo_area. Display the current buffer which
10530 contains the current echo area message in window W, a mini-window,
10531 a pointer to which is passed in A1. A2..A4 are currently not used.
10532 Change the height of W so that all of the message is displayed.
10533 Value is non-zero if height of W was changed. */
10535 static int
10536 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10538 intptr_t i1 = a1;
10539 struct window *w = (struct window *) i1;
10540 Lisp_Object window;
10541 struct text_pos start;
10542 int window_height_changed_p = 0;
10544 /* Do this before displaying, so that we have a large enough glyph
10545 matrix for the display. If we can't get enough space for the
10546 whole text, display the last N lines. That works by setting w->start. */
10547 window_height_changed_p = resize_mini_window (w, 0);
10549 /* Use the starting position chosen by resize_mini_window. */
10550 SET_TEXT_POS_FROM_MARKER (start, w->start);
10552 /* Display. */
10553 clear_glyph_matrix (w->desired_matrix);
10554 XSETWINDOW (window, w);
10555 try_window (window, start, 0);
10557 return window_height_changed_p;
10561 /* Resize the echo area window to exactly the size needed for the
10562 currently displayed message, if there is one. If a mini-buffer
10563 is active, don't shrink it. */
10565 void
10566 resize_echo_area_exactly (void)
10568 if (BUFFERP (echo_area_buffer[0])
10569 && WINDOWP (echo_area_window))
10571 struct window *w = XWINDOW (echo_area_window);
10572 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10573 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10574 (intptr_t) w, resize_exactly);
10575 if (resized_p)
10577 windows_or_buffers_changed = 42;
10578 update_mode_lines = 30;
10579 redisplay_internal ();
10585 /* Callback function for with_echo_area_buffer, when used from
10586 resize_echo_area_exactly. A1 contains a pointer to the window to
10587 resize, EXACTLY non-nil means resize the mini-window exactly to the
10588 size of the text displayed. A3 and A4 are not used. Value is what
10589 resize_mini_window returns. */
10591 static int
10592 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10594 intptr_t i1 = a1;
10595 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10599 /* Resize mini-window W to fit the size of its contents. EXACT_P
10600 means size the window exactly to the size needed. Otherwise, it's
10601 only enlarged until W's buffer is empty.
10603 Set W->start to the right place to begin display. If the whole
10604 contents fit, start at the beginning. Otherwise, start so as
10605 to make the end of the contents appear. This is particularly
10606 important for y-or-n-p, but seems desirable generally.
10608 Value is non-zero if the window height has been changed. */
10611 resize_mini_window (struct window *w, int exact_p)
10613 struct frame *f = XFRAME (w->frame);
10614 int window_height_changed_p = 0;
10616 eassert (MINI_WINDOW_P (w));
10618 /* By default, start display at the beginning. */
10619 set_marker_both (w->start, w->contents,
10620 BUF_BEGV (XBUFFER (w->contents)),
10621 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10623 /* Don't resize windows while redisplaying a window; it would
10624 confuse redisplay functions when the size of the window they are
10625 displaying changes from under them. Such a resizing can happen,
10626 for instance, when which-func prints a long message while
10627 we are running fontification-functions. We're running these
10628 functions with safe_call which binds inhibit-redisplay to t. */
10629 if (!NILP (Vinhibit_redisplay))
10630 return 0;
10632 /* Nil means don't try to resize. */
10633 if (NILP (Vresize_mini_windows)
10634 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10635 return 0;
10637 if (!FRAME_MINIBUF_ONLY_P (f))
10639 struct it it;
10640 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10641 + WINDOW_PIXEL_HEIGHT (w));
10642 int unit = FRAME_LINE_HEIGHT (f);
10643 int height, max_height;
10644 struct text_pos start;
10645 struct buffer *old_current_buffer = NULL;
10647 if (current_buffer != XBUFFER (w->contents))
10649 old_current_buffer = current_buffer;
10650 set_buffer_internal (XBUFFER (w->contents));
10653 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10655 /* Compute the max. number of lines specified by the user. */
10656 if (FLOATP (Vmax_mini_window_height))
10657 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10658 else if (INTEGERP (Vmax_mini_window_height))
10659 max_height = XINT (Vmax_mini_window_height) * unit;
10660 else
10661 max_height = total_height / 4;
10663 /* Correct that max. height if it's bogus. */
10664 max_height = clip_to_bounds (unit, max_height, total_height);
10666 /* Find out the height of the text in the window. */
10667 if (it.line_wrap == TRUNCATE)
10668 height = unit;
10669 else
10671 last_height = 0;
10672 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10673 if (it.max_ascent == 0 && it.max_descent == 0)
10674 height = it.current_y + last_height;
10675 else
10676 height = it.current_y + it.max_ascent + it.max_descent;
10677 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10680 /* Compute a suitable window start. */
10681 if (height > max_height)
10683 height = (max_height / unit) * unit;
10684 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10685 move_it_vertically_backward (&it, height - unit);
10686 start = it.current.pos;
10688 else
10689 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10690 SET_MARKER_FROM_TEXT_POS (w->start, start);
10692 if (EQ (Vresize_mini_windows, Qgrow_only))
10694 /* Let it grow only, until we display an empty message, in which
10695 case the window shrinks again. */
10696 if (height > WINDOW_PIXEL_HEIGHT (w))
10698 int old_height = WINDOW_PIXEL_HEIGHT (w);
10700 FRAME_WINDOWS_FROZEN (f) = 1;
10701 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10702 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10704 else if (height < WINDOW_PIXEL_HEIGHT (w)
10705 && (exact_p || BEGV == ZV))
10707 int old_height = WINDOW_PIXEL_HEIGHT (w);
10709 FRAME_WINDOWS_FROZEN (f) = 0;
10710 shrink_mini_window (w, 1);
10711 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10714 else
10716 /* Always resize to exact size needed. */
10717 if (height > WINDOW_PIXEL_HEIGHT (w))
10719 int old_height = WINDOW_PIXEL_HEIGHT (w);
10721 FRAME_WINDOWS_FROZEN (f) = 1;
10722 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10723 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10725 else if (height < WINDOW_PIXEL_HEIGHT (w))
10727 int old_height = WINDOW_PIXEL_HEIGHT (w);
10729 FRAME_WINDOWS_FROZEN (f) = 0;
10730 shrink_mini_window (w, 1);
10732 if (height)
10734 FRAME_WINDOWS_FROZEN (f) = 1;
10735 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10738 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10742 if (old_current_buffer)
10743 set_buffer_internal (old_current_buffer);
10746 return window_height_changed_p;
10750 /* Value is the current message, a string, or nil if there is no
10751 current message. */
10753 Lisp_Object
10754 current_message (void)
10756 Lisp_Object msg;
10758 if (!BUFFERP (echo_area_buffer[0]))
10759 msg = Qnil;
10760 else
10762 with_echo_area_buffer (0, 0, current_message_1,
10763 (intptr_t) &msg, Qnil);
10764 if (NILP (msg))
10765 echo_area_buffer[0] = Qnil;
10768 return msg;
10772 static int
10773 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10775 intptr_t i1 = a1;
10776 Lisp_Object *msg = (Lisp_Object *) i1;
10778 if (Z > BEG)
10779 *msg = make_buffer_string (BEG, Z, 1);
10780 else
10781 *msg = Qnil;
10782 return 0;
10786 /* Push the current message on Vmessage_stack for later restoration
10787 by restore_message. Value is non-zero if the current message isn't
10788 empty. This is a relatively infrequent operation, so it's not
10789 worth optimizing. */
10791 bool
10792 push_message (void)
10794 Lisp_Object msg = current_message ();
10795 Vmessage_stack = Fcons (msg, Vmessage_stack);
10796 return STRINGP (msg);
10800 /* Restore message display from the top of Vmessage_stack. */
10802 void
10803 restore_message (void)
10805 eassert (CONSP (Vmessage_stack));
10806 message3_nolog (XCAR (Vmessage_stack));
10810 /* Handler for unwind-protect calling pop_message. */
10812 void
10813 pop_message_unwind (void)
10815 /* Pop the top-most entry off Vmessage_stack. */
10816 eassert (CONSP (Vmessage_stack));
10817 Vmessage_stack = XCDR (Vmessage_stack);
10821 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10822 exits. If the stack is not empty, we have a missing pop_message
10823 somewhere. */
10825 void
10826 check_message_stack (void)
10828 if (!NILP (Vmessage_stack))
10829 emacs_abort ();
10833 /* Truncate to NCHARS what will be displayed in the echo area the next
10834 time we display it---but don't redisplay it now. */
10836 void
10837 truncate_echo_area (ptrdiff_t nchars)
10839 if (nchars == 0)
10840 echo_area_buffer[0] = Qnil;
10841 else if (!noninteractive
10842 && INTERACTIVE
10843 && !NILP (echo_area_buffer[0]))
10845 struct frame *sf = SELECTED_FRAME ();
10846 /* Error messages get reported properly by cmd_error, so this must be
10847 just an informative message; if the frame hasn't really been
10848 initialized yet, just toss it. */
10849 if (sf->glyphs_initialized_p)
10850 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10855 /* Helper function for truncate_echo_area. Truncate the current
10856 message to at most NCHARS characters. */
10858 static int
10859 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10861 if (BEG + nchars < Z)
10862 del_range (BEG + nchars, Z);
10863 if (Z == BEG)
10864 echo_area_buffer[0] = Qnil;
10865 return 0;
10868 /* Set the current message to STRING. */
10870 static void
10871 set_message (Lisp_Object string)
10873 eassert (STRINGP (string));
10875 message_enable_multibyte = STRING_MULTIBYTE (string);
10877 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10878 message_buf_print = 0;
10879 help_echo_showing_p = 0;
10881 if (STRINGP (Vdebug_on_message)
10882 && STRINGP (string)
10883 && fast_string_match (Vdebug_on_message, string) >= 0)
10884 call_debugger (list2 (Qerror, string));
10888 /* Helper function for set_message. First argument is ignored and second
10889 argument has the same meaning as for set_message.
10890 This function is called with the echo area buffer being current. */
10892 static int
10893 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10895 eassert (STRINGP (string));
10897 /* Change multibyteness of the echo buffer appropriately. */
10898 if (message_enable_multibyte
10899 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10900 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10902 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10903 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10904 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10906 /* Insert new message at BEG. */
10907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10909 /* This function takes care of single/multibyte conversion.
10910 We just have to ensure that the echo area buffer has the right
10911 setting of enable_multibyte_characters. */
10912 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10914 return 0;
10918 /* Clear messages. CURRENT_P non-zero means clear the current
10919 message. LAST_DISPLAYED_P non-zero means clear the message
10920 last displayed. */
10922 void
10923 clear_message (bool current_p, bool last_displayed_p)
10925 if (current_p)
10927 echo_area_buffer[0] = Qnil;
10928 message_cleared_p = true;
10931 if (last_displayed_p)
10932 echo_area_buffer[1] = Qnil;
10934 message_buf_print = 0;
10937 /* Clear garbaged frames.
10939 This function is used where the old redisplay called
10940 redraw_garbaged_frames which in turn called redraw_frame which in
10941 turn called clear_frame. The call to clear_frame was a source of
10942 flickering. I believe a clear_frame is not necessary. It should
10943 suffice in the new redisplay to invalidate all current matrices,
10944 and ensure a complete redisplay of all windows. */
10946 static void
10947 clear_garbaged_frames (void)
10949 if (frame_garbaged)
10951 Lisp_Object tail, frame;
10953 FOR_EACH_FRAME (tail, frame)
10955 struct frame *f = XFRAME (frame);
10957 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10959 if (f->resized_p)
10960 redraw_frame (f);
10961 else
10962 clear_current_matrices (f);
10963 fset_redisplay (f);
10964 f->garbaged = false;
10965 f->resized_p = false;
10969 frame_garbaged = false;
10974 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10975 is non-zero update selected_frame. Value is non-zero if the
10976 mini-windows height has been changed. */
10978 static int
10979 echo_area_display (int update_frame_p)
10981 Lisp_Object mini_window;
10982 struct window *w;
10983 struct frame *f;
10984 int window_height_changed_p = 0;
10985 struct frame *sf = SELECTED_FRAME ();
10987 mini_window = FRAME_MINIBUF_WINDOW (sf);
10988 w = XWINDOW (mini_window);
10989 f = XFRAME (WINDOW_FRAME (w));
10991 /* Don't display if frame is invisible or not yet initialized. */
10992 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10993 return 0;
10995 #ifdef HAVE_WINDOW_SYSTEM
10996 /* When Emacs starts, selected_frame may be the initial terminal
10997 frame. If we let this through, a message would be displayed on
10998 the terminal. */
10999 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11000 return 0;
11001 #endif /* HAVE_WINDOW_SYSTEM */
11003 /* Redraw garbaged frames. */
11004 clear_garbaged_frames ();
11006 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11008 echo_area_window = mini_window;
11009 window_height_changed_p = display_echo_area (w);
11010 w->must_be_updated_p = true;
11012 /* Update the display, unless called from redisplay_internal.
11013 Also don't update the screen during redisplay itself. The
11014 update will happen at the end of redisplay, and an update
11015 here could cause confusion. */
11016 if (update_frame_p && !redisplaying_p)
11018 int n = 0;
11020 /* If the display update has been interrupted by pending
11021 input, update mode lines in the frame. Due to the
11022 pending input, it might have been that redisplay hasn't
11023 been called, so that mode lines above the echo area are
11024 garbaged. This looks odd, so we prevent it here. */
11025 if (!display_completed)
11026 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11028 if (window_height_changed_p
11029 /* Don't do this if Emacs is shutting down. Redisplay
11030 needs to run hooks. */
11031 && !NILP (Vrun_hooks))
11033 /* Must update other windows. Likewise as in other
11034 cases, don't let this update be interrupted by
11035 pending input. */
11036 ptrdiff_t count = SPECPDL_INDEX ();
11037 specbind (Qredisplay_dont_pause, Qt);
11038 windows_or_buffers_changed = 44;
11039 redisplay_internal ();
11040 unbind_to (count, Qnil);
11042 else if (FRAME_WINDOW_P (f) && n == 0)
11044 /* Window configuration is the same as before.
11045 Can do with a display update of the echo area,
11046 unless we displayed some mode lines. */
11047 update_single_window (w, 1);
11048 flush_frame (f);
11050 else
11051 update_frame (f, 1, 1);
11053 /* If cursor is in the echo area, make sure that the next
11054 redisplay displays the minibuffer, so that the cursor will
11055 be replaced with what the minibuffer wants. */
11056 if (cursor_in_echo_area)
11057 wset_redisplay (XWINDOW (mini_window));
11060 else if (!EQ (mini_window, selected_window))
11061 wset_redisplay (XWINDOW (mini_window));
11063 /* Last displayed message is now the current message. */
11064 echo_area_buffer[1] = echo_area_buffer[0];
11065 /* Inform read_char that we're not echoing. */
11066 echo_message_buffer = Qnil;
11068 /* Prevent redisplay optimization in redisplay_internal by resetting
11069 this_line_start_pos. This is done because the mini-buffer now
11070 displays the message instead of its buffer text. */
11071 if (EQ (mini_window, selected_window))
11072 CHARPOS (this_line_start_pos) = 0;
11074 return window_height_changed_p;
11077 /* Nonzero if W's buffer was changed but not saved. */
11079 static int
11080 window_buffer_changed (struct window *w)
11082 struct buffer *b = XBUFFER (w->contents);
11084 eassert (BUFFER_LIVE_P (b));
11086 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11089 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11091 static int
11092 mode_line_update_needed (struct window *w)
11094 return (w->column_number_displayed != -1
11095 && !(PT == w->last_point && !window_outdated (w))
11096 && (w->column_number_displayed != current_column ()));
11099 /* Nonzero if window start of W is frozen and may not be changed during
11100 redisplay. */
11102 static bool
11103 window_frozen_p (struct window *w)
11105 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11107 Lisp_Object window;
11109 XSETWINDOW (window, w);
11110 if (MINI_WINDOW_P (w))
11111 return 0;
11112 else if (EQ (window, selected_window))
11113 return 0;
11114 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11115 && EQ (window, Vminibuf_scroll_window))
11116 /* This special window can't be frozen too. */
11117 return 0;
11118 else
11119 return 1;
11121 return 0;
11124 /***********************************************************************
11125 Mode Lines and Frame Titles
11126 ***********************************************************************/
11128 /* A buffer for constructing non-propertized mode-line strings and
11129 frame titles in it; allocated from the heap in init_xdisp and
11130 resized as needed in store_mode_line_noprop_char. */
11132 static char *mode_line_noprop_buf;
11134 /* The buffer's end, and a current output position in it. */
11136 static char *mode_line_noprop_buf_end;
11137 static char *mode_line_noprop_ptr;
11139 #define MODE_LINE_NOPROP_LEN(start) \
11140 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11142 static enum {
11143 MODE_LINE_DISPLAY = 0,
11144 MODE_LINE_TITLE,
11145 MODE_LINE_NOPROP,
11146 MODE_LINE_STRING
11147 } mode_line_target;
11149 /* Alist that caches the results of :propertize.
11150 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11151 static Lisp_Object mode_line_proptrans_alist;
11153 /* List of strings making up the mode-line. */
11154 static Lisp_Object mode_line_string_list;
11156 /* Base face property when building propertized mode line string. */
11157 static Lisp_Object mode_line_string_face;
11158 static Lisp_Object mode_line_string_face_prop;
11161 /* Unwind data for mode line strings */
11163 static Lisp_Object Vmode_line_unwind_vector;
11165 static Lisp_Object
11166 format_mode_line_unwind_data (struct frame *target_frame,
11167 struct buffer *obuf,
11168 Lisp_Object owin,
11169 int save_proptrans)
11171 Lisp_Object vector, tmp;
11173 /* Reduce consing by keeping one vector in
11174 Vwith_echo_area_save_vector. */
11175 vector = Vmode_line_unwind_vector;
11176 Vmode_line_unwind_vector = Qnil;
11178 if (NILP (vector))
11179 vector = Fmake_vector (make_number (10), Qnil);
11181 ASET (vector, 0, make_number (mode_line_target));
11182 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11183 ASET (vector, 2, mode_line_string_list);
11184 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11185 ASET (vector, 4, mode_line_string_face);
11186 ASET (vector, 5, mode_line_string_face_prop);
11188 if (obuf)
11189 XSETBUFFER (tmp, obuf);
11190 else
11191 tmp = Qnil;
11192 ASET (vector, 6, tmp);
11193 ASET (vector, 7, owin);
11194 if (target_frame)
11196 /* Similarly to `with-selected-window', if the operation selects
11197 a window on another frame, we must restore that frame's
11198 selected window, and (for a tty) the top-frame. */
11199 ASET (vector, 8, target_frame->selected_window);
11200 if (FRAME_TERMCAP_P (target_frame))
11201 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11204 return vector;
11207 static void
11208 unwind_format_mode_line (Lisp_Object vector)
11210 Lisp_Object old_window = AREF (vector, 7);
11211 Lisp_Object target_frame_window = AREF (vector, 8);
11212 Lisp_Object old_top_frame = AREF (vector, 9);
11214 mode_line_target = XINT (AREF (vector, 0));
11215 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11216 mode_line_string_list = AREF (vector, 2);
11217 if (! EQ (AREF (vector, 3), Qt))
11218 mode_line_proptrans_alist = AREF (vector, 3);
11219 mode_line_string_face = AREF (vector, 4);
11220 mode_line_string_face_prop = AREF (vector, 5);
11222 /* Select window before buffer, since it may change the buffer. */
11223 if (!NILP (old_window))
11225 /* If the operation that we are unwinding had selected a window
11226 on a different frame, reset its frame-selected-window. For a
11227 text terminal, reset its top-frame if necessary. */
11228 if (!NILP (target_frame_window))
11230 Lisp_Object frame
11231 = WINDOW_FRAME (XWINDOW (target_frame_window));
11233 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11234 Fselect_window (target_frame_window, Qt);
11236 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11237 Fselect_frame (old_top_frame, Qt);
11240 Fselect_window (old_window, Qt);
11243 if (!NILP (AREF (vector, 6)))
11245 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11246 ASET (vector, 6, Qnil);
11249 Vmode_line_unwind_vector = vector;
11253 /* Store a single character C for the frame title in mode_line_noprop_buf.
11254 Re-allocate mode_line_noprop_buf if necessary. */
11256 static void
11257 store_mode_line_noprop_char (char c)
11259 /* If output position has reached the end of the allocated buffer,
11260 increase the buffer's size. */
11261 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11263 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11264 ptrdiff_t size = len;
11265 mode_line_noprop_buf =
11266 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11267 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11268 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11271 *mode_line_noprop_ptr++ = c;
11275 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11276 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11277 characters that yield more columns than PRECISION; PRECISION <= 0
11278 means copy the whole string. Pad with spaces until FIELD_WIDTH
11279 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11280 pad. Called from display_mode_element when it is used to build a
11281 frame title. */
11283 static int
11284 store_mode_line_noprop (const char *string, int field_width, int precision)
11286 const unsigned char *str = (const unsigned char *) string;
11287 int n = 0;
11288 ptrdiff_t dummy, nbytes;
11290 /* Copy at most PRECISION chars from STR. */
11291 nbytes = strlen (string);
11292 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11293 while (nbytes--)
11294 store_mode_line_noprop_char (*str++);
11296 /* Fill up with spaces until FIELD_WIDTH reached. */
11297 while (field_width > 0
11298 && n < field_width)
11300 store_mode_line_noprop_char (' ');
11301 ++n;
11304 return n;
11307 /***********************************************************************
11308 Frame Titles
11309 ***********************************************************************/
11311 #ifdef HAVE_WINDOW_SYSTEM
11313 /* Set the title of FRAME, if it has changed. The title format is
11314 Vicon_title_format if FRAME is iconified, otherwise it is
11315 frame_title_format. */
11317 static void
11318 x_consider_frame_title (Lisp_Object frame)
11320 struct frame *f = XFRAME (frame);
11322 if (FRAME_WINDOW_P (f)
11323 || FRAME_MINIBUF_ONLY_P (f)
11324 || f->explicit_name)
11326 /* Do we have more than one visible frame on this X display? */
11327 Lisp_Object tail, other_frame, fmt;
11328 ptrdiff_t title_start;
11329 char *title;
11330 ptrdiff_t len;
11331 struct it it;
11332 ptrdiff_t count = SPECPDL_INDEX ();
11334 FOR_EACH_FRAME (tail, other_frame)
11336 struct frame *tf = XFRAME (other_frame);
11338 if (tf != f
11339 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11340 && !FRAME_MINIBUF_ONLY_P (tf)
11341 && !EQ (other_frame, tip_frame)
11342 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11343 break;
11346 /* Set global variable indicating that multiple frames exist. */
11347 multiple_frames = CONSP (tail);
11349 /* Switch to the buffer of selected window of the frame. Set up
11350 mode_line_target so that display_mode_element will output into
11351 mode_line_noprop_buf; then display the title. */
11352 record_unwind_protect (unwind_format_mode_line,
11353 format_mode_line_unwind_data
11354 (f, current_buffer, selected_window, 0));
11356 Fselect_window (f->selected_window, Qt);
11357 set_buffer_internal_1
11358 (XBUFFER (XWINDOW (f->selected_window)->contents));
11359 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11361 mode_line_target = MODE_LINE_TITLE;
11362 title_start = MODE_LINE_NOPROP_LEN (0);
11363 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11364 NULL, DEFAULT_FACE_ID);
11365 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11366 len = MODE_LINE_NOPROP_LEN (title_start);
11367 title = mode_line_noprop_buf + title_start;
11368 unbind_to (count, Qnil);
11370 /* Set the title only if it's changed. This avoids consing in
11371 the common case where it hasn't. (If it turns out that we've
11372 already wasted too much time by walking through the list with
11373 display_mode_element, then we might need to optimize at a
11374 higher level than this.) */
11375 if (! STRINGP (f->name)
11376 || SBYTES (f->name) != len
11377 || memcmp (title, SDATA (f->name), len) != 0)
11378 x_implicitly_set_name (f, make_string (title, len), Qnil);
11382 #endif /* not HAVE_WINDOW_SYSTEM */
11385 /***********************************************************************
11386 Menu Bars
11387 ***********************************************************************/
11389 /* Non-zero if we will not redisplay all visible windows. */
11390 #define REDISPLAY_SOME_P() \
11391 ((windows_or_buffers_changed == 0 \
11392 || windows_or_buffers_changed == REDISPLAY_SOME) \
11393 && (update_mode_lines == 0 \
11394 || update_mode_lines == REDISPLAY_SOME))
11396 /* Prepare for redisplay by updating menu-bar item lists when
11397 appropriate. This can call eval. */
11399 static void
11400 prepare_menu_bars (void)
11402 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11403 bool some_windows = REDISPLAY_SOME_P ();
11404 struct gcpro gcpro1, gcpro2;
11405 Lisp_Object tooltip_frame;
11407 #ifdef HAVE_WINDOW_SYSTEM
11408 tooltip_frame = tip_frame;
11409 #else
11410 tooltip_frame = Qnil;
11411 #endif
11413 if (FUNCTIONP (Vpre_redisplay_function))
11415 Lisp_Object windows = all_windows ? Qt : Qnil;
11416 if (all_windows && some_windows)
11418 Lisp_Object ws = window_list ();
11419 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11421 Lisp_Object this = XCAR (ws);
11422 struct window *w = XWINDOW (this);
11423 if (w->redisplay
11424 || XFRAME (w->frame)->redisplay
11425 || XBUFFER (w->contents)->text->redisplay)
11427 windows = Fcons (this, windows);
11431 safe_call1 (Vpre_redisplay_function, windows);
11434 /* Update all frame titles based on their buffer names, etc. We do
11435 this before the menu bars so that the buffer-menu will show the
11436 up-to-date frame titles. */
11437 #ifdef HAVE_WINDOW_SYSTEM
11438 if (all_windows)
11440 Lisp_Object tail, frame;
11442 FOR_EACH_FRAME (tail, frame)
11444 struct frame *f = XFRAME (frame);
11445 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11446 if (some_windows
11447 && !f->redisplay
11448 && !w->redisplay
11449 && !XBUFFER (w->contents)->text->redisplay)
11450 continue;
11452 if (!EQ (frame, tooltip_frame)
11453 && (FRAME_ICONIFIED_P (f)
11454 || FRAME_VISIBLE_P (f) == 1
11455 /* Exclude TTY frames that are obscured because they
11456 are not the top frame on their console. This is
11457 because x_consider_frame_title actually switches
11458 to the frame, which for TTY frames means it is
11459 marked as garbaged, and will be completely
11460 redrawn on the next redisplay cycle. This causes
11461 TTY frames to be completely redrawn, when there
11462 are more than one of them, even though nothing
11463 should be changed on display. */
11464 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11465 x_consider_frame_title (frame);
11468 #endif /* HAVE_WINDOW_SYSTEM */
11470 /* Update the menu bar item lists, if appropriate. This has to be
11471 done before any actual redisplay or generation of display lines. */
11473 if (all_windows)
11475 Lisp_Object tail, frame;
11476 ptrdiff_t count = SPECPDL_INDEX ();
11477 /* 1 means that update_menu_bar has run its hooks
11478 so any further calls to update_menu_bar shouldn't do so again. */
11479 int menu_bar_hooks_run = 0;
11481 record_unwind_save_match_data ();
11483 FOR_EACH_FRAME (tail, frame)
11485 struct frame *f = XFRAME (frame);
11486 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11488 /* Ignore tooltip frame. */
11489 if (EQ (frame, tooltip_frame))
11490 continue;
11492 if (some_windows
11493 && !f->redisplay
11494 && !w->redisplay
11495 && !XBUFFER (w->contents)->text->redisplay)
11496 continue;
11498 /* If a window on this frame changed size, report that to
11499 the user and clear the size-change flag. */
11500 if (FRAME_WINDOW_SIZES_CHANGED (f))
11502 Lisp_Object functions;
11504 /* Clear flag first in case we get an error below. */
11505 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11506 functions = Vwindow_size_change_functions;
11507 GCPRO2 (tail, functions);
11509 while (CONSP (functions))
11511 if (!EQ (XCAR (functions), Qt))
11512 call1 (XCAR (functions), frame);
11513 functions = XCDR (functions);
11515 UNGCPRO;
11518 GCPRO1 (tail);
11519 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11520 #ifdef HAVE_WINDOW_SYSTEM
11521 update_tool_bar (f, 0);
11522 #endif
11523 #ifdef HAVE_NS
11524 if (windows_or_buffers_changed
11525 && FRAME_NS_P (f))
11526 ns_set_doc_edited
11527 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11528 #endif
11529 UNGCPRO;
11532 unbind_to (count, Qnil);
11534 else
11536 struct frame *sf = SELECTED_FRAME ();
11537 update_menu_bar (sf, 1, 0);
11538 #ifdef HAVE_WINDOW_SYSTEM
11539 update_tool_bar (sf, 1);
11540 #endif
11545 /* Update the menu bar item list for frame F. This has to be done
11546 before we start to fill in any display lines, because it can call
11547 eval.
11549 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11551 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11552 already ran the menu bar hooks for this redisplay, so there
11553 is no need to run them again. The return value is the
11554 updated value of this flag, to pass to the next call. */
11556 static int
11557 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11559 Lisp_Object window;
11560 register struct window *w;
11562 /* If called recursively during a menu update, do nothing. This can
11563 happen when, for instance, an activate-menubar-hook causes a
11564 redisplay. */
11565 if (inhibit_menubar_update)
11566 return hooks_run;
11568 window = FRAME_SELECTED_WINDOW (f);
11569 w = XWINDOW (window);
11571 if (FRAME_WINDOW_P (f)
11573 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11574 || defined (HAVE_NS) || defined (USE_GTK)
11575 FRAME_EXTERNAL_MENU_BAR (f)
11576 #else
11577 FRAME_MENU_BAR_LINES (f) > 0
11578 #endif
11579 : FRAME_MENU_BAR_LINES (f) > 0)
11581 /* If the user has switched buffers or windows, we need to
11582 recompute to reflect the new bindings. But we'll
11583 recompute when update_mode_lines is set too; that means
11584 that people can use force-mode-line-update to request
11585 that the menu bar be recomputed. The adverse effect on
11586 the rest of the redisplay algorithm is about the same as
11587 windows_or_buffers_changed anyway. */
11588 if (windows_or_buffers_changed
11589 /* This used to test w->update_mode_line, but we believe
11590 there is no need to recompute the menu in that case. */
11591 || update_mode_lines
11592 || window_buffer_changed (w))
11594 struct buffer *prev = current_buffer;
11595 ptrdiff_t count = SPECPDL_INDEX ();
11597 specbind (Qinhibit_menubar_update, Qt);
11599 set_buffer_internal_1 (XBUFFER (w->contents));
11600 if (save_match_data)
11601 record_unwind_save_match_data ();
11602 if (NILP (Voverriding_local_map_menu_flag))
11604 specbind (Qoverriding_terminal_local_map, Qnil);
11605 specbind (Qoverriding_local_map, Qnil);
11608 if (!hooks_run)
11610 /* Run the Lucid hook. */
11611 safe_run_hooks (Qactivate_menubar_hook);
11613 /* If it has changed current-menubar from previous value,
11614 really recompute the menu-bar from the value. */
11615 if (! NILP (Vlucid_menu_bar_dirty_flag))
11616 call0 (Qrecompute_lucid_menubar);
11618 safe_run_hooks (Qmenu_bar_update_hook);
11620 hooks_run = 1;
11623 XSETFRAME (Vmenu_updating_frame, f);
11624 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11626 /* Redisplay the menu bar in case we changed it. */
11627 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11628 || defined (HAVE_NS) || defined (USE_GTK)
11629 if (FRAME_WINDOW_P (f))
11631 #if defined (HAVE_NS)
11632 /* All frames on Mac OS share the same menubar. So only
11633 the selected frame should be allowed to set it. */
11634 if (f == SELECTED_FRAME ())
11635 #endif
11636 set_frame_menubar (f, 0, 0);
11638 else
11639 /* On a terminal screen, the menu bar is an ordinary screen
11640 line, and this makes it get updated. */
11641 w->update_mode_line = 1;
11642 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11643 /* In the non-toolkit version, the menu bar is an ordinary screen
11644 line, and this makes it get updated. */
11645 w->update_mode_line = 1;
11646 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11648 unbind_to (count, Qnil);
11649 set_buffer_internal_1 (prev);
11653 return hooks_run;
11656 /***********************************************************************
11657 Tool-bars
11658 ***********************************************************************/
11660 #ifdef HAVE_WINDOW_SYSTEM
11662 /* Tool-bar item index of the item on which a mouse button was pressed
11663 or -1. */
11665 int last_tool_bar_item;
11667 /* Select `frame' temporarily without running all the code in
11668 do_switch_frame.
11669 FIXME: Maybe do_switch_frame should be trimmed down similarly
11670 when `norecord' is set. */
11671 static void
11672 fast_set_selected_frame (Lisp_Object frame)
11674 if (!EQ (selected_frame, frame))
11676 selected_frame = frame;
11677 selected_window = XFRAME (frame)->selected_window;
11681 /* Update the tool-bar item list for frame F. This has to be done
11682 before we start to fill in any display lines. Called from
11683 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11684 and restore it here. */
11686 static void
11687 update_tool_bar (struct frame *f, int save_match_data)
11689 #if defined (USE_GTK) || defined (HAVE_NS)
11690 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11691 #else
11692 int do_update = (WINDOWP (f->tool_bar_window)
11693 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11694 #endif
11696 if (do_update)
11698 Lisp_Object window;
11699 struct window *w;
11701 window = FRAME_SELECTED_WINDOW (f);
11702 w = XWINDOW (window);
11704 /* If the user has switched buffers or windows, we need to
11705 recompute to reflect the new bindings. But we'll
11706 recompute when update_mode_lines is set too; that means
11707 that people can use force-mode-line-update to request
11708 that the menu bar be recomputed. The adverse effect on
11709 the rest of the redisplay algorithm is about the same as
11710 windows_or_buffers_changed anyway. */
11711 if (windows_or_buffers_changed
11712 || w->update_mode_line
11713 || update_mode_lines
11714 || window_buffer_changed (w))
11716 struct buffer *prev = current_buffer;
11717 ptrdiff_t count = SPECPDL_INDEX ();
11718 Lisp_Object frame, new_tool_bar;
11719 int new_n_tool_bar;
11720 struct gcpro gcpro1;
11722 /* Set current_buffer to the buffer of the selected
11723 window of the frame, so that we get the right local
11724 keymaps. */
11725 set_buffer_internal_1 (XBUFFER (w->contents));
11727 /* Save match data, if we must. */
11728 if (save_match_data)
11729 record_unwind_save_match_data ();
11731 /* Make sure that we don't accidentally use bogus keymaps. */
11732 if (NILP (Voverriding_local_map_menu_flag))
11734 specbind (Qoverriding_terminal_local_map, Qnil);
11735 specbind (Qoverriding_local_map, Qnil);
11738 GCPRO1 (new_tool_bar);
11740 /* We must temporarily set the selected frame to this frame
11741 before calling tool_bar_items, because the calculation of
11742 the tool-bar keymap uses the selected frame (see
11743 `tool-bar-make-keymap' in tool-bar.el). */
11744 eassert (EQ (selected_window,
11745 /* Since we only explicitly preserve selected_frame,
11746 check that selected_window would be redundant. */
11747 XFRAME (selected_frame)->selected_window));
11748 record_unwind_protect (fast_set_selected_frame, selected_frame);
11749 XSETFRAME (frame, f);
11750 fast_set_selected_frame (frame);
11752 /* Build desired tool-bar items from keymaps. */
11753 new_tool_bar
11754 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11755 &new_n_tool_bar);
11757 /* Redisplay the tool-bar if we changed it. */
11758 if (new_n_tool_bar != f->n_tool_bar_items
11759 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11761 /* Redisplay that happens asynchronously due to an expose event
11762 may access f->tool_bar_items. Make sure we update both
11763 variables within BLOCK_INPUT so no such event interrupts. */
11764 block_input ();
11765 fset_tool_bar_items (f, new_tool_bar);
11766 f->n_tool_bar_items = new_n_tool_bar;
11767 w->update_mode_line = 1;
11768 unblock_input ();
11771 UNGCPRO;
11773 unbind_to (count, Qnil);
11774 set_buffer_internal_1 (prev);
11779 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11781 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11782 F's desired tool-bar contents. F->tool_bar_items must have
11783 been set up previously by calling prepare_menu_bars. */
11785 static void
11786 build_desired_tool_bar_string (struct frame *f)
11788 int i, size, size_needed;
11789 struct gcpro gcpro1, gcpro2, gcpro3;
11790 Lisp_Object image, plist, props;
11792 image = plist = props = Qnil;
11793 GCPRO3 (image, plist, props);
11795 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11796 Otherwise, make a new string. */
11798 /* The size of the string we might be able to reuse. */
11799 size = (STRINGP (f->desired_tool_bar_string)
11800 ? SCHARS (f->desired_tool_bar_string)
11801 : 0);
11803 /* We need one space in the string for each image. */
11804 size_needed = f->n_tool_bar_items;
11806 /* Reuse f->desired_tool_bar_string, if possible. */
11807 if (size < size_needed || NILP (f->desired_tool_bar_string))
11808 fset_desired_tool_bar_string
11809 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11810 else
11812 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11813 Fremove_text_properties (make_number (0), make_number (size),
11814 props, f->desired_tool_bar_string);
11817 /* Put a `display' property on the string for the images to display,
11818 put a `menu_item' property on tool-bar items with a value that
11819 is the index of the item in F's tool-bar item vector. */
11820 for (i = 0; i < f->n_tool_bar_items; ++i)
11822 #define PROP(IDX) \
11823 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11825 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11826 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11827 int hmargin, vmargin, relief, idx, end;
11829 /* If image is a vector, choose the image according to the
11830 button state. */
11831 image = PROP (TOOL_BAR_ITEM_IMAGES);
11832 if (VECTORP (image))
11834 if (enabled_p)
11835 idx = (selected_p
11836 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11837 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11838 else
11839 idx = (selected_p
11840 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11841 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11843 eassert (ASIZE (image) >= idx);
11844 image = AREF (image, idx);
11846 else
11847 idx = -1;
11849 /* Ignore invalid image specifications. */
11850 if (!valid_image_p (image))
11851 continue;
11853 /* Display the tool-bar button pressed, or depressed. */
11854 plist = Fcopy_sequence (XCDR (image));
11856 /* Compute margin and relief to draw. */
11857 relief = (tool_bar_button_relief >= 0
11858 ? tool_bar_button_relief
11859 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11860 hmargin = vmargin = relief;
11862 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11863 INT_MAX - max (hmargin, vmargin)))
11865 hmargin += XFASTINT (Vtool_bar_button_margin);
11866 vmargin += XFASTINT (Vtool_bar_button_margin);
11868 else if (CONSP (Vtool_bar_button_margin))
11870 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11871 INT_MAX - hmargin))
11872 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11874 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11875 INT_MAX - vmargin))
11876 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11879 if (auto_raise_tool_bar_buttons_p)
11881 /* Add a `:relief' property to the image spec if the item is
11882 selected. */
11883 if (selected_p)
11885 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11886 hmargin -= relief;
11887 vmargin -= relief;
11890 else
11892 /* If image is selected, display it pressed, i.e. with a
11893 negative relief. If it's not selected, display it with a
11894 raised relief. */
11895 plist = Fplist_put (plist, QCrelief,
11896 (selected_p
11897 ? make_number (-relief)
11898 : make_number (relief)));
11899 hmargin -= relief;
11900 vmargin -= relief;
11903 /* Put a margin around the image. */
11904 if (hmargin || vmargin)
11906 if (hmargin == vmargin)
11907 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11908 else
11909 plist = Fplist_put (plist, QCmargin,
11910 Fcons (make_number (hmargin),
11911 make_number (vmargin)));
11914 /* If button is not enabled, and we don't have special images
11915 for the disabled state, make the image appear disabled by
11916 applying an appropriate algorithm to it. */
11917 if (!enabled_p && idx < 0)
11918 plist = Fplist_put (plist, QCconversion, Qdisabled);
11920 /* Put a `display' text property on the string for the image to
11921 display. Put a `menu-item' property on the string that gives
11922 the start of this item's properties in the tool-bar items
11923 vector. */
11924 image = Fcons (Qimage, plist);
11925 props = list4 (Qdisplay, image,
11926 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11928 /* Let the last image hide all remaining spaces in the tool bar
11929 string. The string can be longer than needed when we reuse a
11930 previous string. */
11931 if (i + 1 == f->n_tool_bar_items)
11932 end = SCHARS (f->desired_tool_bar_string);
11933 else
11934 end = i + 1;
11935 Fadd_text_properties (make_number (i), make_number (end),
11936 props, f->desired_tool_bar_string);
11937 #undef PROP
11940 UNGCPRO;
11944 /* Display one line of the tool-bar of frame IT->f.
11946 HEIGHT specifies the desired height of the tool-bar line.
11947 If the actual height of the glyph row is less than HEIGHT, the
11948 row's height is increased to HEIGHT, and the icons are centered
11949 vertically in the new height.
11951 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11952 count a final empty row in case the tool-bar width exactly matches
11953 the window width.
11956 static void
11957 display_tool_bar_line (struct it *it, int height)
11959 struct glyph_row *row = it->glyph_row;
11960 int max_x = it->last_visible_x;
11961 struct glyph *last;
11963 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11964 clear_glyph_row (row);
11965 row->enabled_p = true;
11966 row->y = it->current_y;
11968 /* Note that this isn't made use of if the face hasn't a box,
11969 so there's no need to check the face here. */
11970 it->start_of_box_run_p = 1;
11972 while (it->current_x < max_x)
11974 int x, n_glyphs_before, i, nglyphs;
11975 struct it it_before;
11977 /* Get the next display element. */
11978 if (!get_next_display_element (it))
11980 /* Don't count empty row if we are counting needed tool-bar lines. */
11981 if (height < 0 && !it->hpos)
11982 return;
11983 break;
11986 /* Produce glyphs. */
11987 n_glyphs_before = row->used[TEXT_AREA];
11988 it_before = *it;
11990 PRODUCE_GLYPHS (it);
11992 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11993 i = 0;
11994 x = it_before.current_x;
11995 while (i < nglyphs)
11997 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11999 if (x + glyph->pixel_width > max_x)
12001 /* Glyph doesn't fit on line. Backtrack. */
12002 row->used[TEXT_AREA] = n_glyphs_before;
12003 *it = it_before;
12004 /* If this is the only glyph on this line, it will never fit on the
12005 tool-bar, so skip it. But ensure there is at least one glyph,
12006 so we don't accidentally disable the tool-bar. */
12007 if (n_glyphs_before == 0
12008 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12009 break;
12010 goto out;
12013 ++it->hpos;
12014 x += glyph->pixel_width;
12015 ++i;
12018 /* Stop at line end. */
12019 if (ITERATOR_AT_END_OF_LINE_P (it))
12020 break;
12022 set_iterator_to_next (it, 1);
12025 out:;
12027 row->displays_text_p = row->used[TEXT_AREA] != 0;
12029 /* Use default face for the border below the tool bar.
12031 FIXME: When auto-resize-tool-bars is grow-only, there is
12032 no additional border below the possibly empty tool-bar lines.
12033 So to make the extra empty lines look "normal", we have to
12034 use the tool-bar face for the border too. */
12035 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12036 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12037 it->face_id = DEFAULT_FACE_ID;
12039 extend_face_to_end_of_line (it);
12040 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12041 last->right_box_line_p = 1;
12042 if (last == row->glyphs[TEXT_AREA])
12043 last->left_box_line_p = 1;
12045 /* Make line the desired height and center it vertically. */
12046 if ((height -= it->max_ascent + it->max_descent) > 0)
12048 /* Don't add more than one line height. */
12049 height %= FRAME_LINE_HEIGHT (it->f);
12050 it->max_ascent += height / 2;
12051 it->max_descent += (height + 1) / 2;
12054 compute_line_metrics (it);
12056 /* If line is empty, make it occupy the rest of the tool-bar. */
12057 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12059 row->height = row->phys_height = it->last_visible_y - row->y;
12060 row->visible_height = row->height;
12061 row->ascent = row->phys_ascent = 0;
12062 row->extra_line_spacing = 0;
12065 row->full_width_p = 1;
12066 row->continued_p = 0;
12067 row->truncated_on_left_p = 0;
12068 row->truncated_on_right_p = 0;
12070 it->current_x = it->hpos = 0;
12071 it->current_y += row->height;
12072 ++it->vpos;
12073 ++it->glyph_row;
12077 /* Max tool-bar height. Basically, this is what makes all other windows
12078 disappear when the frame gets too small. Rethink this! */
12080 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12081 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12083 /* Value is the number of pixels needed to make all tool-bar items of
12084 frame F visible. The actual number of glyph rows needed is
12085 returned in *N_ROWS if non-NULL. */
12087 static int
12088 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12090 struct window *w = XWINDOW (f->tool_bar_window);
12091 struct it it;
12092 /* tool_bar_height is called from redisplay_tool_bar after building
12093 the desired matrix, so use (unused) mode-line row as temporary row to
12094 avoid destroying the first tool-bar row. */
12095 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12097 /* Initialize an iterator for iteration over
12098 F->desired_tool_bar_string in the tool-bar window of frame F. */
12099 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12100 it.first_visible_x = 0;
12101 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12102 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12103 it.paragraph_embedding = L2R;
12105 while (!ITERATOR_AT_END_P (&it))
12107 clear_glyph_row (temp_row);
12108 it.glyph_row = temp_row;
12109 display_tool_bar_line (&it, -1);
12111 clear_glyph_row (temp_row);
12113 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12114 if (n_rows)
12115 *n_rows = it.vpos > 0 ? it.vpos : -1;
12117 if (pixelwise)
12118 return it.current_y;
12119 else
12120 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12123 #endif /* !USE_GTK && !HAVE_NS */
12125 #if defined USE_GTK || defined HAVE_NS
12126 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12127 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12128 #endif
12130 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12131 0, 2, 0,
12132 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12133 If FRAME is nil or omitted, use the selected frame. Optional argument
12134 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12135 (Lisp_Object frame, Lisp_Object pixelwise)
12137 int height = 0;
12139 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12140 struct frame *f = decode_any_frame (frame);
12142 if (WINDOWP (f->tool_bar_window)
12143 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12145 update_tool_bar (f, 1);
12146 if (f->n_tool_bar_items)
12148 build_desired_tool_bar_string (f);
12149 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12152 #endif
12154 return make_number (height);
12158 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12159 height should be changed. */
12161 static int
12162 redisplay_tool_bar (struct frame *f)
12164 #if defined (USE_GTK) || defined (HAVE_NS)
12166 if (FRAME_EXTERNAL_TOOL_BAR (f))
12167 update_frame_tool_bar (f);
12168 return 0;
12170 #else /* !USE_GTK && !HAVE_NS */
12172 struct window *w;
12173 struct it it;
12174 struct glyph_row *row;
12176 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12177 do anything. This means you must start with tool-bar-lines
12178 non-zero to get the auto-sizing effect. Or in other words, you
12179 can turn off tool-bars by specifying tool-bar-lines zero. */
12180 if (!WINDOWP (f->tool_bar_window)
12181 || (w = XWINDOW (f->tool_bar_window),
12182 WINDOW_PIXEL_HEIGHT (w) == 0))
12183 return 0;
12185 /* Set up an iterator for the tool-bar window. */
12186 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12187 it.first_visible_x = 0;
12188 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12189 row = it.glyph_row;
12191 /* Build a string that represents the contents of the tool-bar. */
12192 build_desired_tool_bar_string (f);
12193 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12194 /* FIXME: This should be controlled by a user option. But it
12195 doesn't make sense to have an R2L tool bar if the menu bar cannot
12196 be drawn also R2L, and making the menu bar R2L is tricky due
12197 toolkit-specific code that implements it. If an R2L tool bar is
12198 ever supported, display_tool_bar_line should also be augmented to
12199 call unproduce_glyphs like display_line and display_string
12200 do. */
12201 it.paragraph_embedding = L2R;
12203 if (f->n_tool_bar_rows == 0)
12205 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12207 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12209 Lisp_Object frame;
12210 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12211 / FRAME_LINE_HEIGHT (f));
12213 XSETFRAME (frame, f);
12214 Fmodify_frame_parameters (frame,
12215 list1 (Fcons (Qtool_bar_lines,
12216 make_number (new_lines))));
12217 /* Always do that now. */
12218 clear_glyph_matrix (w->desired_matrix);
12219 f->fonts_changed = 1;
12220 return 1;
12224 /* Display as many lines as needed to display all tool-bar items. */
12226 if (f->n_tool_bar_rows > 0)
12228 int border, rows, height, extra;
12230 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12231 border = XINT (Vtool_bar_border);
12232 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12233 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12234 else if (EQ (Vtool_bar_border, Qborder_width))
12235 border = f->border_width;
12236 else
12237 border = 0;
12238 if (border < 0)
12239 border = 0;
12241 rows = f->n_tool_bar_rows;
12242 height = max (1, (it.last_visible_y - border) / rows);
12243 extra = it.last_visible_y - border - height * rows;
12245 while (it.current_y < it.last_visible_y)
12247 int h = 0;
12248 if (extra > 0 && rows-- > 0)
12250 h = (extra + rows - 1) / rows;
12251 extra -= h;
12253 display_tool_bar_line (&it, height + h);
12256 else
12258 while (it.current_y < it.last_visible_y)
12259 display_tool_bar_line (&it, 0);
12262 /* It doesn't make much sense to try scrolling in the tool-bar
12263 window, so don't do it. */
12264 w->desired_matrix->no_scrolling_p = 1;
12265 w->must_be_updated_p = 1;
12267 if (!NILP (Vauto_resize_tool_bars))
12269 /* Do we really allow the toolbar to occupy the whole frame? */
12270 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12271 int change_height_p = 0;
12273 /* If we couldn't display everything, change the tool-bar's
12274 height if there is room for more. */
12275 if (IT_STRING_CHARPOS (it) < it.end_charpos
12276 && it.current_y < max_tool_bar_height)
12277 change_height_p = 1;
12279 /* We subtract 1 because display_tool_bar_line advances the
12280 glyph_row pointer before returning to its caller. We want to
12281 examine the last glyph row produced by
12282 display_tool_bar_line. */
12283 row = it.glyph_row - 1;
12285 /* If there are blank lines at the end, except for a partially
12286 visible blank line at the end that is smaller than
12287 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12288 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12289 && row->height >= FRAME_LINE_HEIGHT (f))
12290 change_height_p = 1;
12292 /* If row displays tool-bar items, but is partially visible,
12293 change the tool-bar's height. */
12294 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12295 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12296 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12297 change_height_p = 1;
12299 /* Resize windows as needed by changing the `tool-bar-lines'
12300 frame parameter. */
12301 if (change_height_p)
12303 Lisp_Object frame;
12304 int nrows;
12305 int new_height = tool_bar_height (f, &nrows, 1);
12307 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12308 && !f->minimize_tool_bar_window_p)
12309 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12310 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12311 f->minimize_tool_bar_window_p = 0;
12313 if (change_height_p)
12315 /* Current size of the tool-bar window in canonical line
12316 units. */
12317 int old_lines = WINDOW_TOTAL_LINES (w);
12318 /* Required size of the tool-bar window in canonical
12319 line units. */
12320 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12321 / FRAME_LINE_HEIGHT (f));
12322 /* Maximum size of the tool-bar window in canonical line
12323 units that this frame can allow. */
12324 int max_lines =
12325 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12327 /* Don't try to change the tool-bar window size and set
12328 the fonts_changed flag unless really necessary. That
12329 flag causes redisplay to give up and retry
12330 redisplaying the frame from scratch, so setting it
12331 unnecessarily can lead to nasty redisplay loops. */
12332 if (new_lines <= max_lines
12333 && eabs (new_lines - old_lines) >= 1)
12335 XSETFRAME (frame, f);
12336 Fmodify_frame_parameters (frame,
12337 list1 (Fcons (Qtool_bar_lines,
12338 make_number (new_lines))));
12339 clear_glyph_matrix (w->desired_matrix);
12340 f->n_tool_bar_rows = nrows;
12341 f->fonts_changed = 1;
12342 return 1;
12348 f->minimize_tool_bar_window_p = 0;
12349 return 0;
12351 #endif /* USE_GTK || HAVE_NS */
12354 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12356 /* Get information about the tool-bar item which is displayed in GLYPH
12357 on frame F. Return in *PROP_IDX the index where tool-bar item
12358 properties start in F->tool_bar_items. Value is zero if
12359 GLYPH doesn't display a tool-bar item. */
12361 static int
12362 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12364 Lisp_Object prop;
12365 int success_p;
12366 int charpos;
12368 /* This function can be called asynchronously, which means we must
12369 exclude any possibility that Fget_text_property signals an
12370 error. */
12371 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12372 charpos = max (0, charpos);
12374 /* Get the text property `menu-item' at pos. The value of that
12375 property is the start index of this item's properties in
12376 F->tool_bar_items. */
12377 prop = Fget_text_property (make_number (charpos),
12378 Qmenu_item, f->current_tool_bar_string);
12379 if (INTEGERP (prop))
12381 *prop_idx = XINT (prop);
12382 success_p = 1;
12384 else
12385 success_p = 0;
12387 return success_p;
12391 /* Get information about the tool-bar item at position X/Y on frame F.
12392 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12393 the current matrix of the tool-bar window of F, or NULL if not
12394 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12395 item in F->tool_bar_items. Value is
12397 -1 if X/Y is not on a tool-bar item
12398 0 if X/Y is on the same item that was highlighted before.
12399 1 otherwise. */
12401 static int
12402 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12403 int *hpos, int *vpos, int *prop_idx)
12405 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12406 struct window *w = XWINDOW (f->tool_bar_window);
12407 int area;
12409 /* Find the glyph under X/Y. */
12410 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12411 if (*glyph == NULL)
12412 return -1;
12414 /* Get the start of this tool-bar item's properties in
12415 f->tool_bar_items. */
12416 if (!tool_bar_item_info (f, *glyph, prop_idx))
12417 return -1;
12419 /* Is mouse on the highlighted item? */
12420 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12421 && *vpos >= hlinfo->mouse_face_beg_row
12422 && *vpos <= hlinfo->mouse_face_end_row
12423 && (*vpos > hlinfo->mouse_face_beg_row
12424 || *hpos >= hlinfo->mouse_face_beg_col)
12425 && (*vpos < hlinfo->mouse_face_end_row
12426 || *hpos < hlinfo->mouse_face_end_col
12427 || hlinfo->mouse_face_past_end))
12428 return 0;
12430 return 1;
12434 /* EXPORT:
12435 Handle mouse button event on the tool-bar of frame F, at
12436 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12437 0 for button release. MODIFIERS is event modifiers for button
12438 release. */
12440 void
12441 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12442 int modifiers)
12444 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12445 struct window *w = XWINDOW (f->tool_bar_window);
12446 int hpos, vpos, prop_idx;
12447 struct glyph *glyph;
12448 Lisp_Object enabled_p;
12449 int ts;
12451 /* If not on the highlighted tool-bar item, and mouse-highlight is
12452 non-nil, return. This is so we generate the tool-bar button
12453 click only when the mouse button is released on the same item as
12454 where it was pressed. However, when mouse-highlight is disabled,
12455 generate the click when the button is released regardless of the
12456 highlight, since tool-bar items are not highlighted in that
12457 case. */
12458 frame_to_window_pixel_xy (w, &x, &y);
12459 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12460 if (ts == -1
12461 || (ts != 0 && !NILP (Vmouse_highlight)))
12462 return;
12464 /* When mouse-highlight is off, generate the click for the item
12465 where the button was pressed, disregarding where it was
12466 released. */
12467 if (NILP (Vmouse_highlight) && !down_p)
12468 prop_idx = last_tool_bar_item;
12470 /* If item is disabled, do nothing. */
12471 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12472 if (NILP (enabled_p))
12473 return;
12475 if (down_p)
12477 /* Show item in pressed state. */
12478 if (!NILP (Vmouse_highlight))
12479 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12480 last_tool_bar_item = prop_idx;
12482 else
12484 Lisp_Object key, frame;
12485 struct input_event event;
12486 EVENT_INIT (event);
12488 /* Show item in released state. */
12489 if (!NILP (Vmouse_highlight))
12490 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12492 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12494 XSETFRAME (frame, f);
12495 event.kind = TOOL_BAR_EVENT;
12496 event.frame_or_window = frame;
12497 event.arg = frame;
12498 kbd_buffer_store_event (&event);
12500 event.kind = TOOL_BAR_EVENT;
12501 event.frame_or_window = frame;
12502 event.arg = key;
12503 event.modifiers = modifiers;
12504 kbd_buffer_store_event (&event);
12505 last_tool_bar_item = -1;
12510 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12511 tool-bar window-relative coordinates X/Y. Called from
12512 note_mouse_highlight. */
12514 static void
12515 note_tool_bar_highlight (struct frame *f, int x, int y)
12517 Lisp_Object window = f->tool_bar_window;
12518 struct window *w = XWINDOW (window);
12519 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12520 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12521 int hpos, vpos;
12522 struct glyph *glyph;
12523 struct glyph_row *row;
12524 int i;
12525 Lisp_Object enabled_p;
12526 int prop_idx;
12527 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12528 int mouse_down_p, rc;
12530 /* Function note_mouse_highlight is called with negative X/Y
12531 values when mouse moves outside of the frame. */
12532 if (x <= 0 || y <= 0)
12534 clear_mouse_face (hlinfo);
12535 return;
12538 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12539 if (rc < 0)
12541 /* Not on tool-bar item. */
12542 clear_mouse_face (hlinfo);
12543 return;
12545 else if (rc == 0)
12546 /* On same tool-bar item as before. */
12547 goto set_help_echo;
12549 clear_mouse_face (hlinfo);
12551 /* Mouse is down, but on different tool-bar item? */
12552 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12553 && f == dpyinfo->last_mouse_frame);
12555 if (mouse_down_p
12556 && last_tool_bar_item != prop_idx)
12557 return;
12559 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12561 /* If tool-bar item is not enabled, don't highlight it. */
12562 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12563 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12565 /* Compute the x-position of the glyph. In front and past the
12566 image is a space. We include this in the highlighted area. */
12567 row = MATRIX_ROW (w->current_matrix, vpos);
12568 for (i = x = 0; i < hpos; ++i)
12569 x += row->glyphs[TEXT_AREA][i].pixel_width;
12571 /* Record this as the current active region. */
12572 hlinfo->mouse_face_beg_col = hpos;
12573 hlinfo->mouse_face_beg_row = vpos;
12574 hlinfo->mouse_face_beg_x = x;
12575 hlinfo->mouse_face_past_end = 0;
12577 hlinfo->mouse_face_end_col = hpos + 1;
12578 hlinfo->mouse_face_end_row = vpos;
12579 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12580 hlinfo->mouse_face_window = window;
12581 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12583 /* Display it as active. */
12584 show_mouse_face (hlinfo, draw);
12587 set_help_echo:
12589 /* Set help_echo_string to a help string to display for this tool-bar item.
12590 XTread_socket does the rest. */
12591 help_echo_object = help_echo_window = Qnil;
12592 help_echo_pos = -1;
12593 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12594 if (NILP (help_echo_string))
12595 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12598 #endif /* !USE_GTK && !HAVE_NS */
12600 #endif /* HAVE_WINDOW_SYSTEM */
12604 /************************************************************************
12605 Horizontal scrolling
12606 ************************************************************************/
12608 static int hscroll_window_tree (Lisp_Object);
12609 static int hscroll_windows (Lisp_Object);
12611 /* For all leaf windows in the window tree rooted at WINDOW, set their
12612 hscroll value so that PT is (i) visible in the window, and (ii) so
12613 that it is not within a certain margin at the window's left and
12614 right border. Value is non-zero if any window's hscroll has been
12615 changed. */
12617 static int
12618 hscroll_window_tree (Lisp_Object window)
12620 int hscrolled_p = 0;
12621 int hscroll_relative_p = FLOATP (Vhscroll_step);
12622 int hscroll_step_abs = 0;
12623 double hscroll_step_rel = 0;
12625 if (hscroll_relative_p)
12627 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12628 if (hscroll_step_rel < 0)
12630 hscroll_relative_p = 0;
12631 hscroll_step_abs = 0;
12634 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12636 hscroll_step_abs = XINT (Vhscroll_step);
12637 if (hscroll_step_abs < 0)
12638 hscroll_step_abs = 0;
12640 else
12641 hscroll_step_abs = 0;
12643 while (WINDOWP (window))
12645 struct window *w = XWINDOW (window);
12647 if (WINDOWP (w->contents))
12648 hscrolled_p |= hscroll_window_tree (w->contents);
12649 else if (w->cursor.vpos >= 0)
12651 int h_margin;
12652 int text_area_width;
12653 struct glyph_row *cursor_row;
12654 struct glyph_row *bottom_row;
12655 int row_r2l_p;
12657 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12658 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12659 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12660 else
12661 cursor_row = bottom_row - 1;
12663 if (!cursor_row->enabled_p)
12665 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12666 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12667 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12668 else
12669 cursor_row = bottom_row - 1;
12671 row_r2l_p = cursor_row->reversed_p;
12673 text_area_width = window_box_width (w, TEXT_AREA);
12675 /* Scroll when cursor is inside this scroll margin. */
12676 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12678 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12679 /* For left-to-right rows, hscroll when cursor is either
12680 (i) inside the right hscroll margin, or (ii) if it is
12681 inside the left margin and the window is already
12682 hscrolled. */
12683 && ((!row_r2l_p
12684 && ((w->hscroll
12685 && w->cursor.x <= h_margin)
12686 || (cursor_row->enabled_p
12687 && cursor_row->truncated_on_right_p
12688 && (w->cursor.x >= text_area_width - h_margin))))
12689 /* For right-to-left rows, the logic is similar,
12690 except that rules for scrolling to left and right
12691 are reversed. E.g., if cursor.x <= h_margin, we
12692 need to hscroll "to the right" unconditionally,
12693 and that will scroll the screen to the left so as
12694 to reveal the next portion of the row. */
12695 || (row_r2l_p
12696 && ((cursor_row->enabled_p
12697 /* FIXME: It is confusing to set the
12698 truncated_on_right_p flag when R2L rows
12699 are actually truncated on the left. */
12700 && cursor_row->truncated_on_right_p
12701 && w->cursor.x <= h_margin)
12702 || (w->hscroll
12703 && (w->cursor.x >= text_area_width - h_margin))))))
12705 struct it it;
12706 ptrdiff_t hscroll;
12707 struct buffer *saved_current_buffer;
12708 ptrdiff_t pt;
12709 int wanted_x;
12711 /* Find point in a display of infinite width. */
12712 saved_current_buffer = current_buffer;
12713 current_buffer = XBUFFER (w->contents);
12715 if (w == XWINDOW (selected_window))
12716 pt = PT;
12717 else
12718 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12720 /* Move iterator to pt starting at cursor_row->start in
12721 a line with infinite width. */
12722 init_to_row_start (&it, w, cursor_row);
12723 it.last_visible_x = INFINITY;
12724 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12725 current_buffer = saved_current_buffer;
12727 /* Position cursor in window. */
12728 if (!hscroll_relative_p && hscroll_step_abs == 0)
12729 hscroll = max (0, (it.current_x
12730 - (ITERATOR_AT_END_OF_LINE_P (&it)
12731 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12732 : (text_area_width / 2))))
12733 / FRAME_COLUMN_WIDTH (it.f);
12734 else if ((!row_r2l_p
12735 && w->cursor.x >= text_area_width - h_margin)
12736 || (row_r2l_p && w->cursor.x <= h_margin))
12738 if (hscroll_relative_p)
12739 wanted_x = text_area_width * (1 - hscroll_step_rel)
12740 - h_margin;
12741 else
12742 wanted_x = text_area_width
12743 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12744 - h_margin;
12745 hscroll
12746 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12748 else
12750 if (hscroll_relative_p)
12751 wanted_x = text_area_width * hscroll_step_rel
12752 + h_margin;
12753 else
12754 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12755 + h_margin;
12756 hscroll
12757 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12759 hscroll = max (hscroll, w->min_hscroll);
12761 /* Don't prevent redisplay optimizations if hscroll
12762 hasn't changed, as it will unnecessarily slow down
12763 redisplay. */
12764 if (w->hscroll != hscroll)
12766 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12767 w->hscroll = hscroll;
12768 hscrolled_p = 1;
12773 window = w->next;
12776 /* Value is non-zero if hscroll of any leaf window has been changed. */
12777 return hscrolled_p;
12781 /* Set hscroll so that cursor is visible and not inside horizontal
12782 scroll margins for all windows in the tree rooted at WINDOW. See
12783 also hscroll_window_tree above. Value is non-zero if any window's
12784 hscroll has been changed. If it has, desired matrices on the frame
12785 of WINDOW are cleared. */
12787 static int
12788 hscroll_windows (Lisp_Object window)
12790 int hscrolled_p = hscroll_window_tree (window);
12791 if (hscrolled_p)
12792 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12793 return hscrolled_p;
12798 /************************************************************************
12799 Redisplay
12800 ************************************************************************/
12802 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12803 to a non-zero value. This is sometimes handy to have in a debugger
12804 session. */
12806 #ifdef GLYPH_DEBUG
12808 /* First and last unchanged row for try_window_id. */
12810 static int debug_first_unchanged_at_end_vpos;
12811 static int debug_last_unchanged_at_beg_vpos;
12813 /* Delta vpos and y. */
12815 static int debug_dvpos, debug_dy;
12817 /* Delta in characters and bytes for try_window_id. */
12819 static ptrdiff_t debug_delta, debug_delta_bytes;
12821 /* Values of window_end_pos and window_end_vpos at the end of
12822 try_window_id. */
12824 static ptrdiff_t debug_end_vpos;
12826 /* Append a string to W->desired_matrix->method. FMT is a printf
12827 format string. If trace_redisplay_p is true also printf the
12828 resulting string to stderr. */
12830 static void debug_method_add (struct window *, char const *, ...)
12831 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12833 static void
12834 debug_method_add (struct window *w, char const *fmt, ...)
12836 void *ptr = w;
12837 char *method = w->desired_matrix->method;
12838 int len = strlen (method);
12839 int size = sizeof w->desired_matrix->method;
12840 int remaining = size - len - 1;
12841 va_list ap;
12843 if (len && remaining)
12845 method[len] = '|';
12846 --remaining, ++len;
12849 va_start (ap, fmt);
12850 vsnprintf (method + len, remaining + 1, fmt, ap);
12851 va_end (ap);
12853 if (trace_redisplay_p)
12854 fprintf (stderr, "%p (%s): %s\n",
12855 ptr,
12856 ((BUFFERP (w->contents)
12857 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12858 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12859 : "no buffer"),
12860 method + len);
12863 #endif /* GLYPH_DEBUG */
12866 /* Value is non-zero if all changes in window W, which displays
12867 current_buffer, are in the text between START and END. START is a
12868 buffer position, END is given as a distance from Z. Used in
12869 redisplay_internal for display optimization. */
12871 static int
12872 text_outside_line_unchanged_p (struct window *w,
12873 ptrdiff_t start, ptrdiff_t end)
12875 int unchanged_p = 1;
12877 /* If text or overlays have changed, see where. */
12878 if (window_outdated (w))
12880 /* Gap in the line? */
12881 if (GPT < start || Z - GPT < end)
12882 unchanged_p = 0;
12884 /* Changes start in front of the line, or end after it? */
12885 if (unchanged_p
12886 && (BEG_UNCHANGED < start - 1
12887 || END_UNCHANGED < end))
12888 unchanged_p = 0;
12890 /* If selective display, can't optimize if changes start at the
12891 beginning of the line. */
12892 if (unchanged_p
12893 && INTEGERP (BVAR (current_buffer, selective_display))
12894 && XINT (BVAR (current_buffer, selective_display)) > 0
12895 && (BEG_UNCHANGED < start || GPT <= start))
12896 unchanged_p = 0;
12898 /* If there are overlays at the start or end of the line, these
12899 may have overlay strings with newlines in them. A change at
12900 START, for instance, may actually concern the display of such
12901 overlay strings as well, and they are displayed on different
12902 lines. So, quickly rule out this case. (For the future, it
12903 might be desirable to implement something more telling than
12904 just BEG/END_UNCHANGED.) */
12905 if (unchanged_p)
12907 if (BEG + BEG_UNCHANGED == start
12908 && overlay_touches_p (start))
12909 unchanged_p = 0;
12910 if (END_UNCHANGED == end
12911 && overlay_touches_p (Z - end))
12912 unchanged_p = 0;
12915 /* Under bidi reordering, adding or deleting a character in the
12916 beginning of a paragraph, before the first strong directional
12917 character, can change the base direction of the paragraph (unless
12918 the buffer specifies a fixed paragraph direction), which will
12919 require to redisplay the whole paragraph. It might be worthwhile
12920 to find the paragraph limits and widen the range of redisplayed
12921 lines to that, but for now just give up this optimization. */
12922 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12923 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12924 unchanged_p = 0;
12927 return unchanged_p;
12931 /* Do a frame update, taking possible shortcuts into account. This is
12932 the main external entry point for redisplay.
12934 If the last redisplay displayed an echo area message and that message
12935 is no longer requested, we clear the echo area or bring back the
12936 mini-buffer if that is in use. */
12938 void
12939 redisplay (void)
12941 redisplay_internal ();
12945 static Lisp_Object
12946 overlay_arrow_string_or_property (Lisp_Object var)
12948 Lisp_Object val;
12950 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12951 return val;
12953 return Voverlay_arrow_string;
12956 /* Return 1 if there are any overlay-arrows in current_buffer. */
12957 static int
12958 overlay_arrow_in_current_buffer_p (void)
12960 Lisp_Object vlist;
12962 for (vlist = Voverlay_arrow_variable_list;
12963 CONSP (vlist);
12964 vlist = XCDR (vlist))
12966 Lisp_Object var = XCAR (vlist);
12967 Lisp_Object val;
12969 if (!SYMBOLP (var))
12970 continue;
12971 val = find_symbol_value (var);
12972 if (MARKERP (val)
12973 && current_buffer == XMARKER (val)->buffer)
12974 return 1;
12976 return 0;
12980 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12981 has changed. */
12983 static int
12984 overlay_arrows_changed_p (void)
12986 Lisp_Object vlist;
12988 for (vlist = Voverlay_arrow_variable_list;
12989 CONSP (vlist);
12990 vlist = XCDR (vlist))
12992 Lisp_Object var = XCAR (vlist);
12993 Lisp_Object val, pstr;
12995 if (!SYMBOLP (var))
12996 continue;
12997 val = find_symbol_value (var);
12998 if (!MARKERP (val))
12999 continue;
13000 if (! EQ (COERCE_MARKER (val),
13001 Fget (var, Qlast_arrow_position))
13002 || ! (pstr = overlay_arrow_string_or_property (var),
13003 EQ (pstr, Fget (var, Qlast_arrow_string))))
13004 return 1;
13006 return 0;
13009 /* Mark overlay arrows to be updated on next redisplay. */
13011 static void
13012 update_overlay_arrows (int up_to_date)
13014 Lisp_Object vlist;
13016 for (vlist = Voverlay_arrow_variable_list;
13017 CONSP (vlist);
13018 vlist = XCDR (vlist))
13020 Lisp_Object var = XCAR (vlist);
13022 if (!SYMBOLP (var))
13023 continue;
13025 if (up_to_date > 0)
13027 Lisp_Object val = find_symbol_value (var);
13028 Fput (var, Qlast_arrow_position,
13029 COERCE_MARKER (val));
13030 Fput (var, Qlast_arrow_string,
13031 overlay_arrow_string_or_property (var));
13033 else if (up_to_date < 0
13034 || !NILP (Fget (var, Qlast_arrow_position)))
13036 Fput (var, Qlast_arrow_position, Qt);
13037 Fput (var, Qlast_arrow_string, Qt);
13043 /* Return overlay arrow string to display at row.
13044 Return integer (bitmap number) for arrow bitmap in left fringe.
13045 Return nil if no overlay arrow. */
13047 static Lisp_Object
13048 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13050 Lisp_Object vlist;
13052 for (vlist = Voverlay_arrow_variable_list;
13053 CONSP (vlist);
13054 vlist = XCDR (vlist))
13056 Lisp_Object var = XCAR (vlist);
13057 Lisp_Object val;
13059 if (!SYMBOLP (var))
13060 continue;
13062 val = find_symbol_value (var);
13064 if (MARKERP (val)
13065 && current_buffer == XMARKER (val)->buffer
13066 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13068 if (FRAME_WINDOW_P (it->f)
13069 /* FIXME: if ROW->reversed_p is set, this should test
13070 the right fringe, not the left one. */
13071 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13073 #ifdef HAVE_WINDOW_SYSTEM
13074 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13076 int fringe_bitmap;
13077 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13078 return make_number (fringe_bitmap);
13080 #endif
13081 return make_number (-1); /* Use default arrow bitmap. */
13083 return overlay_arrow_string_or_property (var);
13087 return Qnil;
13090 /* Return 1 if point moved out of or into a composition. Otherwise
13091 return 0. PREV_BUF and PREV_PT are the last point buffer and
13092 position. BUF and PT are the current point buffer and position. */
13094 static int
13095 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13096 struct buffer *buf, ptrdiff_t pt)
13098 ptrdiff_t start, end;
13099 Lisp_Object prop;
13100 Lisp_Object buffer;
13102 XSETBUFFER (buffer, buf);
13103 /* Check a composition at the last point if point moved within the
13104 same buffer. */
13105 if (prev_buf == buf)
13107 if (prev_pt == pt)
13108 /* Point didn't move. */
13109 return 0;
13111 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13112 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13113 && composition_valid_p (start, end, prop)
13114 && start < prev_pt && end > prev_pt)
13115 /* The last point was within the composition. Return 1 iff
13116 point moved out of the composition. */
13117 return (pt <= start || pt >= end);
13120 /* Check a composition at the current point. */
13121 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13122 && find_composition (pt, -1, &start, &end, &prop, buffer)
13123 && composition_valid_p (start, end, prop)
13124 && start < pt && end > pt);
13127 /* Reconsider the clip changes of buffer which is displayed in W. */
13129 static void
13130 reconsider_clip_changes (struct window *w)
13132 struct buffer *b = XBUFFER (w->contents);
13134 if (b->clip_changed
13135 && w->window_end_valid
13136 && w->current_matrix->buffer == b
13137 && w->current_matrix->zv == BUF_ZV (b)
13138 && w->current_matrix->begv == BUF_BEGV (b))
13139 b->clip_changed = 0;
13141 /* If display wasn't paused, and W is not a tool bar window, see if
13142 point has been moved into or out of a composition. In that case,
13143 we set b->clip_changed to 1 to force updating the screen. If
13144 b->clip_changed has already been set to 1, we can skip this
13145 check. */
13146 if (!b->clip_changed && w->window_end_valid)
13148 ptrdiff_t pt = (w == XWINDOW (selected_window)
13149 ? PT : marker_position (w->pointm));
13151 if ((w->current_matrix->buffer != b || pt != w->last_point)
13152 && check_point_in_composition (w->current_matrix->buffer,
13153 w->last_point, b, pt))
13154 b->clip_changed = 1;
13158 static void
13159 propagate_buffer_redisplay (void)
13160 { /* Resetting b->text->redisplay is problematic!
13161 We can't just reset it in the case that some window that displays
13162 it has not been redisplayed; and such a window can stay
13163 unredisplayed for a long time if it's currently invisible.
13164 But we do want to reset it at the end of redisplay otherwise
13165 its displayed windows will keep being redisplayed over and over
13166 again.
13167 So we copy all b->text->redisplay flags up to their windows here,
13168 such that mark_window_display_accurate can safely reset
13169 b->text->redisplay. */
13170 Lisp_Object ws = window_list ();
13171 for (; CONSP (ws); ws = XCDR (ws))
13173 struct window *thisw = XWINDOW (XCAR (ws));
13174 struct buffer *thisb = XBUFFER (thisw->contents);
13175 if (thisb->text->redisplay)
13176 thisw->redisplay = true;
13180 #define STOP_POLLING \
13181 do { if (! polling_stopped_here) stop_polling (); \
13182 polling_stopped_here = 1; } while (0)
13184 #define RESUME_POLLING \
13185 do { if (polling_stopped_here) start_polling (); \
13186 polling_stopped_here = 0; } while (0)
13189 /* Perhaps in the future avoid recentering windows if it
13190 is not necessary; currently that causes some problems. */
13192 static void
13193 redisplay_internal (void)
13195 struct window *w = XWINDOW (selected_window);
13196 struct window *sw;
13197 struct frame *fr;
13198 int pending;
13199 bool must_finish = 0, match_p;
13200 struct text_pos tlbufpos, tlendpos;
13201 int number_of_visible_frames;
13202 ptrdiff_t count;
13203 struct frame *sf;
13204 int polling_stopped_here = 0;
13205 Lisp_Object tail, frame;
13207 /* True means redisplay has to consider all windows on all
13208 frames. False, only selected_window is considered. */
13209 bool consider_all_windows_p;
13211 /* True means redisplay has to redisplay the miniwindow. */
13212 bool update_miniwindow_p = false;
13214 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13216 /* No redisplay if running in batch mode or frame is not yet fully
13217 initialized, or redisplay is explicitly turned off by setting
13218 Vinhibit_redisplay. */
13219 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13220 || !NILP (Vinhibit_redisplay))
13221 return;
13223 /* Don't examine these until after testing Vinhibit_redisplay.
13224 When Emacs is shutting down, perhaps because its connection to
13225 X has dropped, we should not look at them at all. */
13226 fr = XFRAME (w->frame);
13227 sf = SELECTED_FRAME ();
13229 if (!fr->glyphs_initialized_p)
13230 return;
13232 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13233 if (popup_activated ())
13234 return;
13235 #endif
13237 /* I don't think this happens but let's be paranoid. */
13238 if (redisplaying_p)
13239 return;
13241 /* Record a function that clears redisplaying_p
13242 when we leave this function. */
13243 count = SPECPDL_INDEX ();
13244 record_unwind_protect_void (unwind_redisplay);
13245 redisplaying_p = 1;
13246 specbind (Qinhibit_free_realized_faces, Qnil);
13248 /* Record this function, so it appears on the profiler's backtraces. */
13249 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13251 FOR_EACH_FRAME (tail, frame)
13252 XFRAME (frame)->already_hscrolled_p = 0;
13254 retry:
13255 /* Remember the currently selected window. */
13256 sw = w;
13258 pending = 0;
13259 last_escape_glyph_frame = NULL;
13260 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13261 last_glyphless_glyph_frame = NULL;
13262 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13264 /* If face_change_count is non-zero, init_iterator will free all
13265 realized faces, which includes the faces referenced from current
13266 matrices. So, we can't reuse current matrices in this case. */
13267 if (face_change_count)
13268 windows_or_buffers_changed = 47;
13270 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13271 && FRAME_TTY (sf)->previous_frame != sf)
13273 /* Since frames on a single ASCII terminal share the same
13274 display area, displaying a different frame means redisplay
13275 the whole thing. */
13276 SET_FRAME_GARBAGED (sf);
13277 #ifndef DOS_NT
13278 set_tty_color_mode (FRAME_TTY (sf), sf);
13279 #endif
13280 FRAME_TTY (sf)->previous_frame = sf;
13283 /* Set the visible flags for all frames. Do this before checking for
13284 resized or garbaged frames; they want to know if their frames are
13285 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13286 number_of_visible_frames = 0;
13288 FOR_EACH_FRAME (tail, frame)
13290 struct frame *f = XFRAME (frame);
13292 if (FRAME_VISIBLE_P (f))
13294 ++number_of_visible_frames;
13295 /* Adjust matrices for visible frames only. */
13296 if (f->fonts_changed)
13298 adjust_frame_glyphs (f);
13299 f->fonts_changed = 0;
13301 /* If cursor type has been changed on the frame
13302 other than selected, consider all frames. */
13303 if (f != sf && f->cursor_type_changed)
13304 update_mode_lines = 31;
13306 clear_desired_matrices (f);
13309 /* Notice any pending interrupt request to change frame size. */
13310 do_pending_window_change (1);
13312 /* do_pending_window_change could change the selected_window due to
13313 frame resizing which makes the selected window too small. */
13314 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13315 sw = w;
13317 /* Clear frames marked as garbaged. */
13318 clear_garbaged_frames ();
13320 /* Build menubar and tool-bar items. */
13321 if (NILP (Vmemory_full))
13322 prepare_menu_bars ();
13324 reconsider_clip_changes (w);
13326 /* In most cases selected window displays current buffer. */
13327 match_p = XBUFFER (w->contents) == current_buffer;
13328 if (match_p)
13330 /* Detect case that we need to write or remove a star in the mode line. */
13331 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13332 w->update_mode_line = 1;
13334 if (mode_line_update_needed (w))
13335 w->update_mode_line = 1;
13338 /* Normally the message* functions will have already displayed and
13339 updated the echo area, but the frame may have been trashed, or
13340 the update may have been preempted, so display the echo area
13341 again here. Checking message_cleared_p captures the case that
13342 the echo area should be cleared. */
13343 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13344 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13345 || (message_cleared_p
13346 && minibuf_level == 0
13347 /* If the mini-window is currently selected, this means the
13348 echo-area doesn't show through. */
13349 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13351 int window_height_changed_p = echo_area_display (0);
13353 if (message_cleared_p)
13354 update_miniwindow_p = true;
13356 must_finish = 1;
13358 /* If we don't display the current message, don't clear the
13359 message_cleared_p flag, because, if we did, we wouldn't clear
13360 the echo area in the next redisplay which doesn't preserve
13361 the echo area. */
13362 if (!display_last_displayed_message_p)
13363 message_cleared_p = 0;
13365 if (window_height_changed_p)
13367 windows_or_buffers_changed = 50;
13369 /* If window configuration was changed, frames may have been
13370 marked garbaged. Clear them or we will experience
13371 surprises wrt scrolling. */
13372 clear_garbaged_frames ();
13375 else if (EQ (selected_window, minibuf_window)
13376 && (current_buffer->clip_changed || window_outdated (w))
13377 && resize_mini_window (w, 0))
13379 /* Resized active mini-window to fit the size of what it is
13380 showing if its contents might have changed. */
13381 must_finish = 1;
13383 /* If window configuration was changed, frames may have been
13384 marked garbaged. Clear them or we will experience
13385 surprises wrt scrolling. */
13386 clear_garbaged_frames ();
13389 if (windows_or_buffers_changed && !update_mode_lines)
13390 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13391 only the windows's contents needs to be refreshed, or whether the
13392 mode-lines also need a refresh. */
13393 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13394 ? REDISPLAY_SOME : 32);
13396 /* If specs for an arrow have changed, do thorough redisplay
13397 to ensure we remove any arrow that should no longer exist. */
13398 if (overlay_arrows_changed_p ())
13399 /* Apparently, this is the only case where we update other windows,
13400 without updating other mode-lines. */
13401 windows_or_buffers_changed = 49;
13403 consider_all_windows_p = (update_mode_lines
13404 || windows_or_buffers_changed);
13406 #define AINC(a,i) \
13407 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13408 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13410 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13411 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13413 /* Optimize the case that only the line containing the cursor in the
13414 selected window has changed. Variables starting with this_ are
13415 set in display_line and record information about the line
13416 containing the cursor. */
13417 tlbufpos = this_line_start_pos;
13418 tlendpos = this_line_end_pos;
13419 if (!consider_all_windows_p
13420 && CHARPOS (tlbufpos) > 0
13421 && !w->update_mode_line
13422 && !current_buffer->clip_changed
13423 && !current_buffer->prevent_redisplay_optimizations_p
13424 && FRAME_VISIBLE_P (XFRAME (w->frame))
13425 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13426 && !XFRAME (w->frame)->cursor_type_changed
13427 /* Make sure recorded data applies to current buffer, etc. */
13428 && this_line_buffer == current_buffer
13429 && match_p
13430 && !w->force_start
13431 && !w->optional_new_start
13432 /* Point must be on the line that we have info recorded about. */
13433 && PT >= CHARPOS (tlbufpos)
13434 && PT <= Z - CHARPOS (tlendpos)
13435 /* All text outside that line, including its final newline,
13436 must be unchanged. */
13437 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13438 CHARPOS (tlendpos)))
13440 if (CHARPOS (tlbufpos) > BEGV
13441 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13442 && (CHARPOS (tlbufpos) == ZV
13443 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13444 /* Former continuation line has disappeared by becoming empty. */
13445 goto cancel;
13446 else if (window_outdated (w) || MINI_WINDOW_P (w))
13448 /* We have to handle the case of continuation around a
13449 wide-column character (see the comment in indent.c around
13450 line 1340).
13452 For instance, in the following case:
13454 -------- Insert --------
13455 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13456 J_I_ ==> J_I_ `^^' are cursors.
13457 ^^ ^^
13458 -------- --------
13460 As we have to redraw the line above, we cannot use this
13461 optimization. */
13463 struct it it;
13464 int line_height_before = this_line_pixel_height;
13466 /* Note that start_display will handle the case that the
13467 line starting at tlbufpos is a continuation line. */
13468 start_display (&it, w, tlbufpos);
13470 /* Implementation note: It this still necessary? */
13471 if (it.current_x != this_line_start_x)
13472 goto cancel;
13474 TRACE ((stderr, "trying display optimization 1\n"));
13475 w->cursor.vpos = -1;
13476 overlay_arrow_seen = 0;
13477 it.vpos = this_line_vpos;
13478 it.current_y = this_line_y;
13479 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13480 display_line (&it);
13482 /* If line contains point, is not continued,
13483 and ends at same distance from eob as before, we win. */
13484 if (w->cursor.vpos >= 0
13485 /* Line is not continued, otherwise this_line_start_pos
13486 would have been set to 0 in display_line. */
13487 && CHARPOS (this_line_start_pos)
13488 /* Line ends as before. */
13489 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13490 /* Line has same height as before. Otherwise other lines
13491 would have to be shifted up or down. */
13492 && this_line_pixel_height == line_height_before)
13494 /* If this is not the window's last line, we must adjust
13495 the charstarts of the lines below. */
13496 if (it.current_y < it.last_visible_y)
13498 struct glyph_row *row
13499 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13500 ptrdiff_t delta, delta_bytes;
13502 /* We used to distinguish between two cases here,
13503 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13504 when the line ends in a newline or the end of the
13505 buffer's accessible portion. But both cases did
13506 the same, so they were collapsed. */
13507 delta = (Z
13508 - CHARPOS (tlendpos)
13509 - MATRIX_ROW_START_CHARPOS (row));
13510 delta_bytes = (Z_BYTE
13511 - BYTEPOS (tlendpos)
13512 - MATRIX_ROW_START_BYTEPOS (row));
13514 increment_matrix_positions (w->current_matrix,
13515 this_line_vpos + 1,
13516 w->current_matrix->nrows,
13517 delta, delta_bytes);
13520 /* If this row displays text now but previously didn't,
13521 or vice versa, w->window_end_vpos may have to be
13522 adjusted. */
13523 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13525 if (w->window_end_vpos < this_line_vpos)
13526 w->window_end_vpos = this_line_vpos;
13528 else if (w->window_end_vpos == this_line_vpos
13529 && this_line_vpos > 0)
13530 w->window_end_vpos = this_line_vpos - 1;
13531 w->window_end_valid = 0;
13533 /* Update hint: No need to try to scroll in update_window. */
13534 w->desired_matrix->no_scrolling_p = 1;
13536 #ifdef GLYPH_DEBUG
13537 *w->desired_matrix->method = 0;
13538 debug_method_add (w, "optimization 1");
13539 #endif
13540 #ifdef HAVE_WINDOW_SYSTEM
13541 update_window_fringes (w, 0);
13542 #endif
13543 goto update;
13545 else
13546 goto cancel;
13548 else if (/* Cursor position hasn't changed. */
13549 PT == w->last_point
13550 /* Make sure the cursor was last displayed
13551 in this window. Otherwise we have to reposition it. */
13553 /* PXW: Must be converted to pixels, probably. */
13554 && 0 <= w->cursor.vpos
13555 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13557 if (!must_finish)
13559 do_pending_window_change (1);
13560 /* If selected_window changed, redisplay again. */
13561 if (WINDOWP (selected_window)
13562 && (w = XWINDOW (selected_window)) != sw)
13563 goto retry;
13565 /* We used to always goto end_of_redisplay here, but this
13566 isn't enough if we have a blinking cursor. */
13567 if (w->cursor_off_p == w->last_cursor_off_p)
13568 goto end_of_redisplay;
13570 goto update;
13572 /* If highlighting the region, or if the cursor is in the echo area,
13573 then we can't just move the cursor. */
13574 else if (NILP (Vshow_trailing_whitespace)
13575 && !cursor_in_echo_area)
13577 struct it it;
13578 struct glyph_row *row;
13580 /* Skip from tlbufpos to PT and see where it is. Note that
13581 PT may be in invisible text. If so, we will end at the
13582 next visible position. */
13583 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13584 NULL, DEFAULT_FACE_ID);
13585 it.current_x = this_line_start_x;
13586 it.current_y = this_line_y;
13587 it.vpos = this_line_vpos;
13589 /* The call to move_it_to stops in front of PT, but
13590 moves over before-strings. */
13591 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13593 if (it.vpos == this_line_vpos
13594 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13595 row->enabled_p))
13597 eassert (this_line_vpos == it.vpos);
13598 eassert (this_line_y == it.current_y);
13599 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13600 #ifdef GLYPH_DEBUG
13601 *w->desired_matrix->method = 0;
13602 debug_method_add (w, "optimization 3");
13603 #endif
13604 goto update;
13606 else
13607 goto cancel;
13610 cancel:
13611 /* Text changed drastically or point moved off of line. */
13612 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13615 CHARPOS (this_line_start_pos) = 0;
13616 ++clear_face_cache_count;
13617 #ifdef HAVE_WINDOW_SYSTEM
13618 ++clear_image_cache_count;
13619 #endif
13621 /* Build desired matrices, and update the display. If
13622 consider_all_windows_p is non-zero, do it for all windows on all
13623 frames. Otherwise do it for selected_window, only. */
13625 if (consider_all_windows_p)
13627 FOR_EACH_FRAME (tail, frame)
13628 XFRAME (frame)->updated_p = 0;
13630 propagate_buffer_redisplay ();
13632 FOR_EACH_FRAME (tail, frame)
13634 struct frame *f = XFRAME (frame);
13636 /* We don't have to do anything for unselected terminal
13637 frames. */
13638 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13639 && !EQ (FRAME_TTY (f)->top_frame, frame))
13640 continue;
13642 retry_frame:
13644 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13646 bool gcscrollbars
13647 /* Only GC scrollbars when we redisplay the whole frame. */
13648 = f->redisplay || !REDISPLAY_SOME_P ();
13649 /* Mark all the scroll bars to be removed; we'll redeem
13650 the ones we want when we redisplay their windows. */
13651 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13652 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13654 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13655 redisplay_windows (FRAME_ROOT_WINDOW (f));
13656 /* Remember that the invisible frames need to be redisplayed next
13657 time they're visible. */
13658 else if (!REDISPLAY_SOME_P ())
13659 f->redisplay = true;
13661 /* The X error handler may have deleted that frame. */
13662 if (!FRAME_LIVE_P (f))
13663 continue;
13665 /* Any scroll bars which redisplay_windows should have
13666 nuked should now go away. */
13667 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13668 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13670 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13672 /* If fonts changed on visible frame, display again. */
13673 if (f->fonts_changed)
13675 adjust_frame_glyphs (f);
13676 f->fonts_changed = 0;
13677 goto retry_frame;
13680 /* See if we have to hscroll. */
13681 if (!f->already_hscrolled_p)
13683 f->already_hscrolled_p = 1;
13684 if (hscroll_windows (f->root_window))
13685 goto retry_frame;
13688 /* Prevent various kinds of signals during display
13689 update. stdio is not robust about handling
13690 signals, which can cause an apparent I/O error. */
13691 if (interrupt_input)
13692 unrequest_sigio ();
13693 STOP_POLLING;
13695 pending |= update_frame (f, 0, 0);
13696 f->cursor_type_changed = 0;
13697 f->updated_p = 1;
13702 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13704 if (!pending)
13706 /* Do the mark_window_display_accurate after all windows have
13707 been redisplayed because this call resets flags in buffers
13708 which are needed for proper redisplay. */
13709 FOR_EACH_FRAME (tail, frame)
13711 struct frame *f = XFRAME (frame);
13712 if (f->updated_p)
13714 f->redisplay = false;
13715 mark_window_display_accurate (f->root_window, 1);
13716 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13717 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13722 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13724 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13725 struct frame *mini_frame;
13727 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13728 /* Use list_of_error, not Qerror, so that
13729 we catch only errors and don't run the debugger. */
13730 internal_condition_case_1 (redisplay_window_1, selected_window,
13731 list_of_error,
13732 redisplay_window_error);
13733 if (update_miniwindow_p)
13734 internal_condition_case_1 (redisplay_window_1, mini_window,
13735 list_of_error,
13736 redisplay_window_error);
13738 /* Compare desired and current matrices, perform output. */
13740 update:
13741 /* If fonts changed, display again. */
13742 if (sf->fonts_changed)
13743 goto retry;
13745 /* Prevent various kinds of signals during display update.
13746 stdio is not robust about handling signals,
13747 which can cause an apparent I/O error. */
13748 if (interrupt_input)
13749 unrequest_sigio ();
13750 STOP_POLLING;
13752 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13754 if (hscroll_windows (selected_window))
13755 goto retry;
13757 XWINDOW (selected_window)->must_be_updated_p = true;
13758 pending = update_frame (sf, 0, 0);
13759 sf->cursor_type_changed = 0;
13762 /* We may have called echo_area_display at the top of this
13763 function. If the echo area is on another frame, that may
13764 have put text on a frame other than the selected one, so the
13765 above call to update_frame would not have caught it. Catch
13766 it here. */
13767 mini_window = FRAME_MINIBUF_WINDOW (sf);
13768 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13770 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13772 XWINDOW (mini_window)->must_be_updated_p = true;
13773 pending |= update_frame (mini_frame, 0, 0);
13774 mini_frame->cursor_type_changed = 0;
13775 if (!pending && hscroll_windows (mini_window))
13776 goto retry;
13780 /* If display was paused because of pending input, make sure we do a
13781 thorough update the next time. */
13782 if (pending)
13784 /* Prevent the optimization at the beginning of
13785 redisplay_internal that tries a single-line update of the
13786 line containing the cursor in the selected window. */
13787 CHARPOS (this_line_start_pos) = 0;
13789 /* Let the overlay arrow be updated the next time. */
13790 update_overlay_arrows (0);
13792 /* If we pause after scrolling, some rows in the current
13793 matrices of some windows are not valid. */
13794 if (!WINDOW_FULL_WIDTH_P (w)
13795 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13796 update_mode_lines = 36;
13798 else
13800 if (!consider_all_windows_p)
13802 /* This has already been done above if
13803 consider_all_windows_p is set. */
13804 if (XBUFFER (w->contents)->text->redisplay
13805 && buffer_window_count (XBUFFER (w->contents)) > 1)
13806 /* This can happen if b->text->redisplay was set during
13807 jit-lock. */
13808 propagate_buffer_redisplay ();
13809 mark_window_display_accurate_1 (w, 1);
13811 /* Say overlay arrows are up to date. */
13812 update_overlay_arrows (1);
13814 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13815 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13818 update_mode_lines = 0;
13819 windows_or_buffers_changed = 0;
13822 /* Start SIGIO interrupts coming again. Having them off during the
13823 code above makes it less likely one will discard output, but not
13824 impossible, since there might be stuff in the system buffer here.
13825 But it is much hairier to try to do anything about that. */
13826 if (interrupt_input)
13827 request_sigio ();
13828 RESUME_POLLING;
13830 /* If a frame has become visible which was not before, redisplay
13831 again, so that we display it. Expose events for such a frame
13832 (which it gets when becoming visible) don't call the parts of
13833 redisplay constructing glyphs, so simply exposing a frame won't
13834 display anything in this case. So, we have to display these
13835 frames here explicitly. */
13836 if (!pending)
13838 int new_count = 0;
13840 FOR_EACH_FRAME (tail, frame)
13842 if (XFRAME (frame)->visible)
13843 new_count++;
13846 if (new_count != number_of_visible_frames)
13847 windows_or_buffers_changed = 52;
13850 /* Change frame size now if a change is pending. */
13851 do_pending_window_change (1);
13853 /* If we just did a pending size change, or have additional
13854 visible frames, or selected_window changed, redisplay again. */
13855 if ((windows_or_buffers_changed && !pending)
13856 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13857 goto retry;
13859 /* Clear the face and image caches.
13861 We used to do this only if consider_all_windows_p. But the cache
13862 needs to be cleared if a timer creates images in the current
13863 buffer (e.g. the test case in Bug#6230). */
13865 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13867 clear_face_cache (0);
13868 clear_face_cache_count = 0;
13871 #ifdef HAVE_WINDOW_SYSTEM
13872 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13874 clear_image_caches (Qnil);
13875 clear_image_cache_count = 0;
13877 #endif /* HAVE_WINDOW_SYSTEM */
13879 end_of_redisplay:
13880 if (interrupt_input && interrupts_deferred)
13881 request_sigio ();
13883 unbind_to (count, Qnil);
13884 RESUME_POLLING;
13888 /* Redisplay, but leave alone any recent echo area message unless
13889 another message has been requested in its place.
13891 This is useful in situations where you need to redisplay but no
13892 user action has occurred, making it inappropriate for the message
13893 area to be cleared. See tracking_off and
13894 wait_reading_process_output for examples of these situations.
13896 FROM_WHERE is an integer saying from where this function was
13897 called. This is useful for debugging. */
13899 void
13900 redisplay_preserve_echo_area (int from_where)
13902 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13904 if (!NILP (echo_area_buffer[1]))
13906 /* We have a previously displayed message, but no current
13907 message. Redisplay the previous message. */
13908 display_last_displayed_message_p = 1;
13909 redisplay_internal ();
13910 display_last_displayed_message_p = 0;
13912 else
13913 redisplay_internal ();
13915 flush_frame (SELECTED_FRAME ());
13919 /* Function registered with record_unwind_protect in redisplay_internal. */
13921 static void
13922 unwind_redisplay (void)
13924 redisplaying_p = 0;
13928 /* Mark the display of leaf window W as accurate or inaccurate.
13929 If ACCURATE_P is non-zero mark display of W as accurate. If
13930 ACCURATE_P is zero, arrange for W to be redisplayed the next
13931 time redisplay_internal is called. */
13933 static void
13934 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13936 struct buffer *b = XBUFFER (w->contents);
13938 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13939 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13940 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13942 if (accurate_p)
13944 b->clip_changed = false;
13945 b->prevent_redisplay_optimizations_p = false;
13946 eassert (buffer_window_count (b) > 0);
13947 /* Resetting b->text->redisplay is problematic!
13948 In order to make it safer to do it here, redisplay_internal must
13949 have copied all b->text->redisplay to their respective windows. */
13950 b->text->redisplay = false;
13952 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13953 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13954 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13955 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13957 w->current_matrix->buffer = b;
13958 w->current_matrix->begv = BUF_BEGV (b);
13959 w->current_matrix->zv = BUF_ZV (b);
13961 w->last_cursor_vpos = w->cursor.vpos;
13962 w->last_cursor_off_p = w->cursor_off_p;
13964 if (w == XWINDOW (selected_window))
13965 w->last_point = BUF_PT (b);
13966 else
13967 w->last_point = marker_position (w->pointm);
13969 w->window_end_valid = true;
13970 w->update_mode_line = false;
13973 w->redisplay = !accurate_p;
13977 /* Mark the display of windows in the window tree rooted at WINDOW as
13978 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13979 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13980 be redisplayed the next time redisplay_internal is called. */
13982 void
13983 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13985 struct window *w;
13987 for (; !NILP (window); window = w->next)
13989 w = XWINDOW (window);
13990 if (WINDOWP (w->contents))
13991 mark_window_display_accurate (w->contents, accurate_p);
13992 else
13993 mark_window_display_accurate_1 (w, accurate_p);
13996 if (accurate_p)
13997 update_overlay_arrows (1);
13998 else
13999 /* Force a thorough redisplay the next time by setting
14000 last_arrow_position and last_arrow_string to t, which is
14001 unequal to any useful value of Voverlay_arrow_... */
14002 update_overlay_arrows (-1);
14006 /* Return value in display table DP (Lisp_Char_Table *) for character
14007 C. Since a display table doesn't have any parent, we don't have to
14008 follow parent. Do not call this function directly but use the
14009 macro DISP_CHAR_VECTOR. */
14011 Lisp_Object
14012 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14014 Lisp_Object val;
14016 if (ASCII_CHAR_P (c))
14018 val = dp->ascii;
14019 if (SUB_CHAR_TABLE_P (val))
14020 val = XSUB_CHAR_TABLE (val)->contents[c];
14022 else
14024 Lisp_Object table;
14026 XSETCHAR_TABLE (table, dp);
14027 val = char_table_ref (table, c);
14029 if (NILP (val))
14030 val = dp->defalt;
14031 return val;
14036 /***********************************************************************
14037 Window Redisplay
14038 ***********************************************************************/
14040 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14042 static void
14043 redisplay_windows (Lisp_Object window)
14045 while (!NILP (window))
14047 struct window *w = XWINDOW (window);
14049 if (WINDOWP (w->contents))
14050 redisplay_windows (w->contents);
14051 else if (BUFFERP (w->contents))
14053 displayed_buffer = XBUFFER (w->contents);
14054 /* Use list_of_error, not Qerror, so that
14055 we catch only errors and don't run the debugger. */
14056 internal_condition_case_1 (redisplay_window_0, window,
14057 list_of_error,
14058 redisplay_window_error);
14061 window = w->next;
14065 static Lisp_Object
14066 redisplay_window_error (Lisp_Object ignore)
14068 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14069 return Qnil;
14072 static Lisp_Object
14073 redisplay_window_0 (Lisp_Object window)
14075 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14076 redisplay_window (window, false);
14077 return Qnil;
14080 static Lisp_Object
14081 redisplay_window_1 (Lisp_Object window)
14083 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14084 redisplay_window (window, true);
14085 return Qnil;
14089 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14090 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14091 which positions recorded in ROW differ from current buffer
14092 positions.
14094 Return 0 if cursor is not on this row, 1 otherwise. */
14096 static int
14097 set_cursor_from_row (struct window *w, struct glyph_row *row,
14098 struct glyph_matrix *matrix,
14099 ptrdiff_t delta, ptrdiff_t delta_bytes,
14100 int dy, int dvpos)
14102 struct glyph *glyph = row->glyphs[TEXT_AREA];
14103 struct glyph *end = glyph + row->used[TEXT_AREA];
14104 struct glyph *cursor = NULL;
14105 /* The last known character position in row. */
14106 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14107 int x = row->x;
14108 ptrdiff_t pt_old = PT - delta;
14109 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14110 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14111 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14112 /* A glyph beyond the edge of TEXT_AREA which we should never
14113 touch. */
14114 struct glyph *glyphs_end = end;
14115 /* Non-zero means we've found a match for cursor position, but that
14116 glyph has the avoid_cursor_p flag set. */
14117 int match_with_avoid_cursor = 0;
14118 /* Non-zero means we've seen at least one glyph that came from a
14119 display string. */
14120 int string_seen = 0;
14121 /* Largest and smallest buffer positions seen so far during scan of
14122 glyph row. */
14123 ptrdiff_t bpos_max = pos_before;
14124 ptrdiff_t bpos_min = pos_after;
14125 /* Last buffer position covered by an overlay string with an integer
14126 `cursor' property. */
14127 ptrdiff_t bpos_covered = 0;
14128 /* Non-zero means the display string on which to display the cursor
14129 comes from a text property, not from an overlay. */
14130 int string_from_text_prop = 0;
14132 /* Don't even try doing anything if called for a mode-line or
14133 header-line row, since the rest of the code isn't prepared to
14134 deal with such calamities. */
14135 eassert (!row->mode_line_p);
14136 if (row->mode_line_p)
14137 return 0;
14139 /* Skip over glyphs not having an object at the start and the end of
14140 the row. These are special glyphs like truncation marks on
14141 terminal frames. */
14142 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14144 if (!row->reversed_p)
14146 while (glyph < end
14147 && INTEGERP (glyph->object)
14148 && glyph->charpos < 0)
14150 x += glyph->pixel_width;
14151 ++glyph;
14153 while (end > glyph
14154 && INTEGERP ((end - 1)->object)
14155 /* CHARPOS is zero for blanks and stretch glyphs
14156 inserted by extend_face_to_end_of_line. */
14157 && (end - 1)->charpos <= 0)
14158 --end;
14159 glyph_before = glyph - 1;
14160 glyph_after = end;
14162 else
14164 struct glyph *g;
14166 /* If the glyph row is reversed, we need to process it from back
14167 to front, so swap the edge pointers. */
14168 glyphs_end = end = glyph - 1;
14169 glyph += row->used[TEXT_AREA] - 1;
14171 while (glyph > end + 1
14172 && INTEGERP (glyph->object)
14173 && glyph->charpos < 0)
14175 --glyph;
14176 x -= glyph->pixel_width;
14178 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14179 --glyph;
14180 /* By default, in reversed rows we put the cursor on the
14181 rightmost (first in the reading order) glyph. */
14182 for (g = end + 1; g < glyph; g++)
14183 x += g->pixel_width;
14184 while (end < glyph
14185 && INTEGERP ((end + 1)->object)
14186 && (end + 1)->charpos <= 0)
14187 ++end;
14188 glyph_before = glyph + 1;
14189 glyph_after = end;
14192 else if (row->reversed_p)
14194 /* In R2L rows that don't display text, put the cursor on the
14195 rightmost glyph. Case in point: an empty last line that is
14196 part of an R2L paragraph. */
14197 cursor = end - 1;
14198 /* Avoid placing the cursor on the last glyph of the row, where
14199 on terminal frames we hold the vertical border between
14200 adjacent windows. */
14201 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14202 && !WINDOW_RIGHTMOST_P (w)
14203 && cursor == row->glyphs[LAST_AREA] - 1)
14204 cursor--;
14205 x = -1; /* will be computed below, at label compute_x */
14208 /* Step 1: Try to find the glyph whose character position
14209 corresponds to point. If that's not possible, find 2 glyphs
14210 whose character positions are the closest to point, one before
14211 point, the other after it. */
14212 if (!row->reversed_p)
14213 while (/* not marched to end of glyph row */
14214 glyph < end
14215 /* glyph was not inserted by redisplay for internal purposes */
14216 && !INTEGERP (glyph->object))
14218 if (BUFFERP (glyph->object))
14220 ptrdiff_t dpos = glyph->charpos - pt_old;
14222 if (glyph->charpos > bpos_max)
14223 bpos_max = glyph->charpos;
14224 if (glyph->charpos < bpos_min)
14225 bpos_min = glyph->charpos;
14226 if (!glyph->avoid_cursor_p)
14228 /* If we hit point, we've found the glyph on which to
14229 display the cursor. */
14230 if (dpos == 0)
14232 match_with_avoid_cursor = 0;
14233 break;
14235 /* See if we've found a better approximation to
14236 POS_BEFORE or to POS_AFTER. */
14237 if (0 > dpos && dpos > pos_before - pt_old)
14239 pos_before = glyph->charpos;
14240 glyph_before = glyph;
14242 else if (0 < dpos && dpos < pos_after - pt_old)
14244 pos_after = glyph->charpos;
14245 glyph_after = glyph;
14248 else if (dpos == 0)
14249 match_with_avoid_cursor = 1;
14251 else if (STRINGP (glyph->object))
14253 Lisp_Object chprop;
14254 ptrdiff_t glyph_pos = glyph->charpos;
14256 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14257 glyph->object);
14258 if (!NILP (chprop))
14260 /* If the string came from a `display' text property,
14261 look up the buffer position of that property and
14262 use that position to update bpos_max, as if we
14263 actually saw such a position in one of the row's
14264 glyphs. This helps with supporting integer values
14265 of `cursor' property on the display string in
14266 situations where most or all of the row's buffer
14267 text is completely covered by display properties,
14268 so that no glyph with valid buffer positions is
14269 ever seen in the row. */
14270 ptrdiff_t prop_pos =
14271 string_buffer_position_lim (glyph->object, pos_before,
14272 pos_after, 0);
14274 if (prop_pos >= pos_before)
14275 bpos_max = prop_pos - 1;
14277 if (INTEGERP (chprop))
14279 bpos_covered = bpos_max + XINT (chprop);
14280 /* If the `cursor' property covers buffer positions up
14281 to and including point, we should display cursor on
14282 this glyph. Note that, if a `cursor' property on one
14283 of the string's characters has an integer value, we
14284 will break out of the loop below _before_ we get to
14285 the position match above. IOW, integer values of
14286 the `cursor' property override the "exact match for
14287 point" strategy of positioning the cursor. */
14288 /* Implementation note: bpos_max == pt_old when, e.g.,
14289 we are in an empty line, where bpos_max is set to
14290 MATRIX_ROW_START_CHARPOS, see above. */
14291 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14293 cursor = glyph;
14294 break;
14298 string_seen = 1;
14300 x += glyph->pixel_width;
14301 ++glyph;
14303 else if (glyph > end) /* row is reversed */
14304 while (!INTEGERP (glyph->object))
14306 if (BUFFERP (glyph->object))
14308 ptrdiff_t dpos = glyph->charpos - pt_old;
14310 if (glyph->charpos > bpos_max)
14311 bpos_max = glyph->charpos;
14312 if (glyph->charpos < bpos_min)
14313 bpos_min = glyph->charpos;
14314 if (!glyph->avoid_cursor_p)
14316 if (dpos == 0)
14318 match_with_avoid_cursor = 0;
14319 break;
14321 if (0 > dpos && dpos > pos_before - pt_old)
14323 pos_before = glyph->charpos;
14324 glyph_before = glyph;
14326 else if (0 < dpos && dpos < pos_after - pt_old)
14328 pos_after = glyph->charpos;
14329 glyph_after = glyph;
14332 else if (dpos == 0)
14333 match_with_avoid_cursor = 1;
14335 else if (STRINGP (glyph->object))
14337 Lisp_Object chprop;
14338 ptrdiff_t glyph_pos = glyph->charpos;
14340 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14341 glyph->object);
14342 if (!NILP (chprop))
14344 ptrdiff_t prop_pos =
14345 string_buffer_position_lim (glyph->object, pos_before,
14346 pos_after, 0);
14348 if (prop_pos >= pos_before)
14349 bpos_max = prop_pos - 1;
14351 if (INTEGERP (chprop))
14353 bpos_covered = bpos_max + XINT (chprop);
14354 /* If the `cursor' property covers buffer positions up
14355 to and including point, we should display cursor on
14356 this glyph. */
14357 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14359 cursor = glyph;
14360 break;
14363 string_seen = 1;
14365 --glyph;
14366 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14368 x--; /* can't use any pixel_width */
14369 break;
14371 x -= glyph->pixel_width;
14374 /* Step 2: If we didn't find an exact match for point, we need to
14375 look for a proper place to put the cursor among glyphs between
14376 GLYPH_BEFORE and GLYPH_AFTER. */
14377 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14378 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14379 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14381 /* An empty line has a single glyph whose OBJECT is zero and
14382 whose CHARPOS is the position of a newline on that line.
14383 Note that on a TTY, there are more glyphs after that, which
14384 were produced by extend_face_to_end_of_line, but their
14385 CHARPOS is zero or negative. */
14386 int empty_line_p =
14387 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14388 && INTEGERP (glyph->object) && glyph->charpos > 0
14389 /* On a TTY, continued and truncated rows also have a glyph at
14390 their end whose OBJECT is zero and whose CHARPOS is
14391 positive (the continuation and truncation glyphs), but such
14392 rows are obviously not "empty". */
14393 && !(row->continued_p || row->truncated_on_right_p);
14395 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14397 ptrdiff_t ellipsis_pos;
14399 /* Scan back over the ellipsis glyphs. */
14400 if (!row->reversed_p)
14402 ellipsis_pos = (glyph - 1)->charpos;
14403 while (glyph > row->glyphs[TEXT_AREA]
14404 && (glyph - 1)->charpos == ellipsis_pos)
14405 glyph--, x -= glyph->pixel_width;
14406 /* That loop always goes one position too far, including
14407 the glyph before the ellipsis. So scan forward over
14408 that one. */
14409 x += glyph->pixel_width;
14410 glyph++;
14412 else /* row is reversed */
14414 ellipsis_pos = (glyph + 1)->charpos;
14415 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14416 && (glyph + 1)->charpos == ellipsis_pos)
14417 glyph++, x += glyph->pixel_width;
14418 x -= glyph->pixel_width;
14419 glyph--;
14422 else if (match_with_avoid_cursor)
14424 cursor = glyph_after;
14425 x = -1;
14427 else if (string_seen)
14429 int incr = row->reversed_p ? -1 : +1;
14431 /* Need to find the glyph that came out of a string which is
14432 present at point. That glyph is somewhere between
14433 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14434 positioned between POS_BEFORE and POS_AFTER in the
14435 buffer. */
14436 struct glyph *start, *stop;
14437 ptrdiff_t pos = pos_before;
14439 x = -1;
14441 /* If the row ends in a newline from a display string,
14442 reordering could have moved the glyphs belonging to the
14443 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14444 in this case we extend the search to the last glyph in
14445 the row that was not inserted by redisplay. */
14446 if (row->ends_in_newline_from_string_p)
14448 glyph_after = end;
14449 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14452 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14453 correspond to POS_BEFORE and POS_AFTER, respectively. We
14454 need START and STOP in the order that corresponds to the
14455 row's direction as given by its reversed_p flag. If the
14456 directionality of characters between POS_BEFORE and
14457 POS_AFTER is the opposite of the row's base direction,
14458 these characters will have been reordered for display,
14459 and we need to reverse START and STOP. */
14460 if (!row->reversed_p)
14462 start = min (glyph_before, glyph_after);
14463 stop = max (glyph_before, glyph_after);
14465 else
14467 start = max (glyph_before, glyph_after);
14468 stop = min (glyph_before, glyph_after);
14470 for (glyph = start + incr;
14471 row->reversed_p ? glyph > stop : glyph < stop; )
14474 /* Any glyphs that come from the buffer are here because
14475 of bidi reordering. Skip them, and only pay
14476 attention to glyphs that came from some string. */
14477 if (STRINGP (glyph->object))
14479 Lisp_Object str;
14480 ptrdiff_t tem;
14481 /* If the display property covers the newline, we
14482 need to search for it one position farther. */
14483 ptrdiff_t lim = pos_after
14484 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14486 string_from_text_prop = 0;
14487 str = glyph->object;
14488 tem = string_buffer_position_lim (str, pos, lim, 0);
14489 if (tem == 0 /* from overlay */
14490 || pos <= tem)
14492 /* If the string from which this glyph came is
14493 found in the buffer at point, or at position
14494 that is closer to point than pos_after, then
14495 we've found the glyph we've been looking for.
14496 If it comes from an overlay (tem == 0), and
14497 it has the `cursor' property on one of its
14498 glyphs, record that glyph as a candidate for
14499 displaying the cursor. (As in the
14500 unidirectional version, we will display the
14501 cursor on the last candidate we find.) */
14502 if (tem == 0
14503 || tem == pt_old
14504 || (tem - pt_old > 0 && tem < pos_after))
14506 /* The glyphs from this string could have
14507 been reordered. Find the one with the
14508 smallest string position. Or there could
14509 be a character in the string with the
14510 `cursor' property, which means display
14511 cursor on that character's glyph. */
14512 ptrdiff_t strpos = glyph->charpos;
14514 if (tem)
14516 cursor = glyph;
14517 string_from_text_prop = 1;
14519 for ( ;
14520 (row->reversed_p ? glyph > stop : glyph < stop)
14521 && EQ (glyph->object, str);
14522 glyph += incr)
14524 Lisp_Object cprop;
14525 ptrdiff_t gpos = glyph->charpos;
14527 cprop = Fget_char_property (make_number (gpos),
14528 Qcursor,
14529 glyph->object);
14530 if (!NILP (cprop))
14532 cursor = glyph;
14533 break;
14535 if (tem && glyph->charpos < strpos)
14537 strpos = glyph->charpos;
14538 cursor = glyph;
14542 if (tem == pt_old
14543 || (tem - pt_old > 0 && tem < pos_after))
14544 goto compute_x;
14546 if (tem)
14547 pos = tem + 1; /* don't find previous instances */
14549 /* This string is not what we want; skip all of the
14550 glyphs that came from it. */
14551 while ((row->reversed_p ? glyph > stop : glyph < stop)
14552 && EQ (glyph->object, str))
14553 glyph += incr;
14555 else
14556 glyph += incr;
14559 /* If we reached the end of the line, and END was from a string,
14560 the cursor is not on this line. */
14561 if (cursor == NULL
14562 && (row->reversed_p ? glyph <= end : glyph >= end)
14563 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14564 && STRINGP (end->object)
14565 && row->continued_p)
14566 return 0;
14568 /* A truncated row may not include PT among its character positions.
14569 Setting the cursor inside the scroll margin will trigger
14570 recalculation of hscroll in hscroll_window_tree. But if a
14571 display string covers point, defer to the string-handling
14572 code below to figure this out. */
14573 else if (row->truncated_on_left_p && pt_old < bpos_min)
14575 cursor = glyph_before;
14576 x = -1;
14578 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14579 /* Zero-width characters produce no glyphs. */
14580 || (!empty_line_p
14581 && (row->reversed_p
14582 ? glyph_after > glyphs_end
14583 : glyph_after < glyphs_end)))
14585 cursor = glyph_after;
14586 x = -1;
14590 compute_x:
14591 if (cursor != NULL)
14592 glyph = cursor;
14593 else if (glyph == glyphs_end
14594 && pos_before == pos_after
14595 && STRINGP ((row->reversed_p
14596 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14597 : row->glyphs[TEXT_AREA])->object))
14599 /* If all the glyphs of this row came from strings, put the
14600 cursor on the first glyph of the row. This avoids having the
14601 cursor outside of the text area in this very rare and hard
14602 use case. */
14603 glyph =
14604 row->reversed_p
14605 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14606 : row->glyphs[TEXT_AREA];
14608 if (x < 0)
14610 struct glyph *g;
14612 /* Need to compute x that corresponds to GLYPH. */
14613 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14615 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14616 emacs_abort ();
14617 x += g->pixel_width;
14621 /* ROW could be part of a continued line, which, under bidi
14622 reordering, might have other rows whose start and end charpos
14623 occlude point. Only set w->cursor if we found a better
14624 approximation to the cursor position than we have from previously
14625 examined candidate rows belonging to the same continued line. */
14626 if (/* We already have a candidate row. */
14627 w->cursor.vpos >= 0
14628 /* That candidate is not the row we are processing. */
14629 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14630 /* Make sure cursor.vpos specifies a row whose start and end
14631 charpos occlude point, and it is valid candidate for being a
14632 cursor-row. This is because some callers of this function
14633 leave cursor.vpos at the row where the cursor was displayed
14634 during the last redisplay cycle. */
14635 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14636 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14637 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14639 struct glyph *g1
14640 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14642 /* Don't consider glyphs that are outside TEXT_AREA. */
14643 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14644 return 0;
14645 /* Keep the candidate whose buffer position is the closest to
14646 point or has the `cursor' property. */
14647 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14648 w->cursor.hpos >= 0
14649 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14650 && ((BUFFERP (g1->object)
14651 && (g1->charpos == pt_old /* An exact match always wins. */
14652 || (BUFFERP (glyph->object)
14653 && eabs (g1->charpos - pt_old)
14654 < eabs (glyph->charpos - pt_old))))
14655 /* Previous candidate is a glyph from a string that has
14656 a non-nil `cursor' property. */
14657 || (STRINGP (g1->object)
14658 && (!NILP (Fget_char_property (make_number (g1->charpos),
14659 Qcursor, g1->object))
14660 /* Previous candidate is from the same display
14661 string as this one, and the display string
14662 came from a text property. */
14663 || (EQ (g1->object, glyph->object)
14664 && string_from_text_prop)
14665 /* this candidate is from newline and its
14666 position is not an exact match */
14667 || (INTEGERP (glyph->object)
14668 && glyph->charpos != pt_old)))))
14669 return 0;
14670 /* If this candidate gives an exact match, use that. */
14671 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14672 /* If this candidate is a glyph created for the
14673 terminating newline of a line, and point is on that
14674 newline, it wins because it's an exact match. */
14675 || (!row->continued_p
14676 && INTEGERP (glyph->object)
14677 && glyph->charpos == 0
14678 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14679 /* Otherwise, keep the candidate that comes from a row
14680 spanning less buffer positions. This may win when one or
14681 both candidate positions are on glyphs that came from
14682 display strings, for which we cannot compare buffer
14683 positions. */
14684 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14685 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14686 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14687 return 0;
14689 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14690 w->cursor.x = x;
14691 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14692 w->cursor.y = row->y + dy;
14694 if (w == XWINDOW (selected_window))
14696 if (!row->continued_p
14697 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14698 && row->x == 0)
14700 this_line_buffer = XBUFFER (w->contents);
14702 CHARPOS (this_line_start_pos)
14703 = MATRIX_ROW_START_CHARPOS (row) + delta;
14704 BYTEPOS (this_line_start_pos)
14705 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14707 CHARPOS (this_line_end_pos)
14708 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14709 BYTEPOS (this_line_end_pos)
14710 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14712 this_line_y = w->cursor.y;
14713 this_line_pixel_height = row->height;
14714 this_line_vpos = w->cursor.vpos;
14715 this_line_start_x = row->x;
14717 else
14718 CHARPOS (this_line_start_pos) = 0;
14721 return 1;
14725 /* Run window scroll functions, if any, for WINDOW with new window
14726 start STARTP. Sets the window start of WINDOW to that position.
14728 We assume that the window's buffer is really current. */
14730 static struct text_pos
14731 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14733 struct window *w = XWINDOW (window);
14734 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14736 eassert (current_buffer == XBUFFER (w->contents));
14738 if (!NILP (Vwindow_scroll_functions))
14740 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14741 make_number (CHARPOS (startp)));
14742 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14743 /* In case the hook functions switch buffers. */
14744 set_buffer_internal (XBUFFER (w->contents));
14747 return startp;
14751 /* Make sure the line containing the cursor is fully visible.
14752 A value of 1 means there is nothing to be done.
14753 (Either the line is fully visible, or it cannot be made so,
14754 or we cannot tell.)
14756 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14757 is higher than window.
14759 A value of 0 means the caller should do scrolling
14760 as if point had gone off the screen. */
14762 static int
14763 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14765 struct glyph_matrix *matrix;
14766 struct glyph_row *row;
14767 int window_height;
14769 if (!make_cursor_line_fully_visible_p)
14770 return 1;
14772 /* It's not always possible to find the cursor, e.g, when a window
14773 is full of overlay strings. Don't do anything in that case. */
14774 if (w->cursor.vpos < 0)
14775 return 1;
14777 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14778 row = MATRIX_ROW (matrix, w->cursor.vpos);
14780 /* If the cursor row is not partially visible, there's nothing to do. */
14781 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14782 return 1;
14784 /* If the row the cursor is in is taller than the window's height,
14785 it's not clear what to do, so do nothing. */
14786 window_height = window_box_height (w);
14787 if (row->height >= window_height)
14789 if (!force_p || MINI_WINDOW_P (w)
14790 || w->vscroll || w->cursor.vpos == 0)
14791 return 1;
14793 return 0;
14797 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14798 non-zero means only WINDOW is redisplayed in redisplay_internal.
14799 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14800 in redisplay_window to bring a partially visible line into view in
14801 the case that only the cursor has moved.
14803 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14804 last screen line's vertical height extends past the end of the screen.
14806 Value is
14808 1 if scrolling succeeded
14810 0 if scrolling didn't find point.
14812 -1 if new fonts have been loaded so that we must interrupt
14813 redisplay, adjust glyph matrices, and try again. */
14815 enum
14817 SCROLLING_SUCCESS,
14818 SCROLLING_FAILED,
14819 SCROLLING_NEED_LARGER_MATRICES
14822 /* If scroll-conservatively is more than this, never recenter.
14824 If you change this, don't forget to update the doc string of
14825 `scroll-conservatively' and the Emacs manual. */
14826 #define SCROLL_LIMIT 100
14828 static int
14829 try_scrolling (Lisp_Object window, int just_this_one_p,
14830 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14831 int temp_scroll_step, int last_line_misfit)
14833 struct window *w = XWINDOW (window);
14834 struct frame *f = XFRAME (w->frame);
14835 struct text_pos pos, startp;
14836 struct it it;
14837 int this_scroll_margin, scroll_max, rc, height;
14838 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14839 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14840 Lisp_Object aggressive;
14841 /* We will never try scrolling more than this number of lines. */
14842 int scroll_limit = SCROLL_LIMIT;
14843 int frame_line_height = default_line_pixel_height (w);
14844 int window_total_lines
14845 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14847 #ifdef GLYPH_DEBUG
14848 debug_method_add (w, "try_scrolling");
14849 #endif
14851 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14853 /* Compute scroll margin height in pixels. We scroll when point is
14854 within this distance from the top or bottom of the window. */
14855 if (scroll_margin > 0)
14856 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14857 * frame_line_height;
14858 else
14859 this_scroll_margin = 0;
14861 /* Force arg_scroll_conservatively to have a reasonable value, to
14862 avoid scrolling too far away with slow move_it_* functions. Note
14863 that the user can supply scroll-conservatively equal to
14864 `most-positive-fixnum', which can be larger than INT_MAX. */
14865 if (arg_scroll_conservatively > scroll_limit)
14867 arg_scroll_conservatively = scroll_limit + 1;
14868 scroll_max = scroll_limit * frame_line_height;
14870 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14871 /* Compute how much we should try to scroll maximally to bring
14872 point into view. */
14873 scroll_max = (max (scroll_step,
14874 max (arg_scroll_conservatively, temp_scroll_step))
14875 * frame_line_height);
14876 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14877 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14878 /* We're trying to scroll because of aggressive scrolling but no
14879 scroll_step is set. Choose an arbitrary one. */
14880 scroll_max = 10 * frame_line_height;
14881 else
14882 scroll_max = 0;
14884 too_near_end:
14886 /* Decide whether to scroll down. */
14887 if (PT > CHARPOS (startp))
14889 int scroll_margin_y;
14891 /* Compute the pixel ypos of the scroll margin, then move IT to
14892 either that ypos or PT, whichever comes first. */
14893 start_display (&it, w, startp);
14894 scroll_margin_y = it.last_visible_y - this_scroll_margin
14895 - frame_line_height * extra_scroll_margin_lines;
14896 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14897 (MOVE_TO_POS | MOVE_TO_Y));
14899 if (PT > CHARPOS (it.current.pos))
14901 int y0 = line_bottom_y (&it);
14902 /* Compute how many pixels below window bottom to stop searching
14903 for PT. This avoids costly search for PT that is far away if
14904 the user limited scrolling by a small number of lines, but
14905 always finds PT if scroll_conservatively is set to a large
14906 number, such as most-positive-fixnum. */
14907 int slack = max (scroll_max, 10 * frame_line_height);
14908 int y_to_move = it.last_visible_y + slack;
14910 /* Compute the distance from the scroll margin to PT or to
14911 the scroll limit, whichever comes first. This should
14912 include the height of the cursor line, to make that line
14913 fully visible. */
14914 move_it_to (&it, PT, -1, y_to_move,
14915 -1, MOVE_TO_POS | MOVE_TO_Y);
14916 dy = line_bottom_y (&it) - y0;
14918 if (dy > scroll_max)
14919 return SCROLLING_FAILED;
14921 if (dy > 0)
14922 scroll_down_p = 1;
14926 if (scroll_down_p)
14928 /* Point is in or below the bottom scroll margin, so move the
14929 window start down. If scrolling conservatively, move it just
14930 enough down to make point visible. If scroll_step is set,
14931 move it down by scroll_step. */
14932 if (arg_scroll_conservatively)
14933 amount_to_scroll
14934 = min (max (dy, frame_line_height),
14935 frame_line_height * arg_scroll_conservatively);
14936 else if (scroll_step || temp_scroll_step)
14937 amount_to_scroll = scroll_max;
14938 else
14940 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14941 height = WINDOW_BOX_TEXT_HEIGHT (w);
14942 if (NUMBERP (aggressive))
14944 double float_amount = XFLOATINT (aggressive) * height;
14945 int aggressive_scroll = float_amount;
14946 if (aggressive_scroll == 0 && float_amount > 0)
14947 aggressive_scroll = 1;
14948 /* Don't let point enter the scroll margin near top of
14949 the window. This could happen if the value of
14950 scroll_up_aggressively is too large and there are
14951 non-zero margins, because scroll_up_aggressively
14952 means put point that fraction of window height
14953 _from_the_bottom_margin_. */
14954 if (aggressive_scroll + 2*this_scroll_margin > height)
14955 aggressive_scroll = height - 2*this_scroll_margin;
14956 amount_to_scroll = dy + aggressive_scroll;
14960 if (amount_to_scroll <= 0)
14961 return SCROLLING_FAILED;
14963 start_display (&it, w, startp);
14964 if (arg_scroll_conservatively <= scroll_limit)
14965 move_it_vertically (&it, amount_to_scroll);
14966 else
14968 /* Extra precision for users who set scroll-conservatively
14969 to a large number: make sure the amount we scroll
14970 the window start is never less than amount_to_scroll,
14971 which was computed as distance from window bottom to
14972 point. This matters when lines at window top and lines
14973 below window bottom have different height. */
14974 struct it it1;
14975 void *it1data = NULL;
14976 /* We use a temporary it1 because line_bottom_y can modify
14977 its argument, if it moves one line down; see there. */
14978 int start_y;
14980 SAVE_IT (it1, it, it1data);
14981 start_y = line_bottom_y (&it1);
14982 do {
14983 RESTORE_IT (&it, &it, it1data);
14984 move_it_by_lines (&it, 1);
14985 SAVE_IT (it1, it, it1data);
14986 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14989 /* If STARTP is unchanged, move it down another screen line. */
14990 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14991 move_it_by_lines (&it, 1);
14992 startp = it.current.pos;
14994 else
14996 struct text_pos scroll_margin_pos = startp;
14997 int y_offset = 0;
14999 /* See if point is inside the scroll margin at the top of the
15000 window. */
15001 if (this_scroll_margin)
15003 int y_start;
15005 start_display (&it, w, startp);
15006 y_start = it.current_y;
15007 move_it_vertically (&it, this_scroll_margin);
15008 scroll_margin_pos = it.current.pos;
15009 /* If we didn't move enough before hitting ZV, request
15010 additional amount of scroll, to move point out of the
15011 scroll margin. */
15012 if (IT_CHARPOS (it) == ZV
15013 && it.current_y - y_start < this_scroll_margin)
15014 y_offset = this_scroll_margin - (it.current_y - y_start);
15017 if (PT < CHARPOS (scroll_margin_pos))
15019 /* Point is in the scroll margin at the top of the window or
15020 above what is displayed in the window. */
15021 int y0, y_to_move;
15023 /* Compute the vertical distance from PT to the scroll
15024 margin position. Move as far as scroll_max allows, or
15025 one screenful, or 10 screen lines, whichever is largest.
15026 Give up if distance is greater than scroll_max or if we
15027 didn't reach the scroll margin position. */
15028 SET_TEXT_POS (pos, PT, PT_BYTE);
15029 start_display (&it, w, pos);
15030 y0 = it.current_y;
15031 y_to_move = max (it.last_visible_y,
15032 max (scroll_max, 10 * frame_line_height));
15033 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15034 y_to_move, -1,
15035 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15036 dy = it.current_y - y0;
15037 if (dy > scroll_max
15038 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15039 return SCROLLING_FAILED;
15041 /* Additional scroll for when ZV was too close to point. */
15042 dy += y_offset;
15044 /* Compute new window start. */
15045 start_display (&it, w, startp);
15047 if (arg_scroll_conservatively)
15048 amount_to_scroll = max (dy, frame_line_height *
15049 max (scroll_step, temp_scroll_step));
15050 else if (scroll_step || temp_scroll_step)
15051 amount_to_scroll = scroll_max;
15052 else
15054 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15055 height = WINDOW_BOX_TEXT_HEIGHT (w);
15056 if (NUMBERP (aggressive))
15058 double float_amount = XFLOATINT (aggressive) * height;
15059 int aggressive_scroll = float_amount;
15060 if (aggressive_scroll == 0 && float_amount > 0)
15061 aggressive_scroll = 1;
15062 /* Don't let point enter the scroll margin near
15063 bottom of the window, if the value of
15064 scroll_down_aggressively happens to be too
15065 large. */
15066 if (aggressive_scroll + 2*this_scroll_margin > height)
15067 aggressive_scroll = height - 2*this_scroll_margin;
15068 amount_to_scroll = dy + aggressive_scroll;
15072 if (amount_to_scroll <= 0)
15073 return SCROLLING_FAILED;
15075 move_it_vertically_backward (&it, amount_to_scroll);
15076 startp = it.current.pos;
15080 /* Run window scroll functions. */
15081 startp = run_window_scroll_functions (window, startp);
15083 /* Display the window. Give up if new fonts are loaded, or if point
15084 doesn't appear. */
15085 if (!try_window (window, startp, 0))
15086 rc = SCROLLING_NEED_LARGER_MATRICES;
15087 else if (w->cursor.vpos < 0)
15089 clear_glyph_matrix (w->desired_matrix);
15090 rc = SCROLLING_FAILED;
15092 else
15094 /* Maybe forget recorded base line for line number display. */
15095 if (!just_this_one_p
15096 || current_buffer->clip_changed
15097 || BEG_UNCHANGED < CHARPOS (startp))
15098 w->base_line_number = 0;
15100 /* If cursor ends up on a partially visible line,
15101 treat that as being off the bottom of the screen. */
15102 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15103 /* It's possible that the cursor is on the first line of the
15104 buffer, which is partially obscured due to a vscroll
15105 (Bug#7537). In that case, avoid looping forever. */
15106 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15108 clear_glyph_matrix (w->desired_matrix);
15109 ++extra_scroll_margin_lines;
15110 goto too_near_end;
15112 rc = SCROLLING_SUCCESS;
15115 return rc;
15119 /* Compute a suitable window start for window W if display of W starts
15120 on a continuation line. Value is non-zero if a new window start
15121 was computed.
15123 The new window start will be computed, based on W's width, starting
15124 from the start of the continued line. It is the start of the
15125 screen line with the minimum distance from the old start W->start. */
15127 static int
15128 compute_window_start_on_continuation_line (struct window *w)
15130 struct text_pos pos, start_pos;
15131 int window_start_changed_p = 0;
15133 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15135 /* If window start is on a continuation line... Window start may be
15136 < BEGV in case there's invisible text at the start of the
15137 buffer (M-x rmail, for example). */
15138 if (CHARPOS (start_pos) > BEGV
15139 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15141 struct it it;
15142 struct glyph_row *row;
15144 /* Handle the case that the window start is out of range. */
15145 if (CHARPOS (start_pos) < BEGV)
15146 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15147 else if (CHARPOS (start_pos) > ZV)
15148 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15150 /* Find the start of the continued line. This should be fast
15151 because find_newline is fast (newline cache). */
15152 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15153 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15154 row, DEFAULT_FACE_ID);
15155 reseat_at_previous_visible_line_start (&it);
15157 /* If the line start is "too far" away from the window start,
15158 say it takes too much time to compute a new window start. */
15159 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15160 /* PXW: Do we need upper bounds here? */
15161 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15163 int min_distance, distance;
15165 /* Move forward by display lines to find the new window
15166 start. If window width was enlarged, the new start can
15167 be expected to be > the old start. If window width was
15168 decreased, the new window start will be < the old start.
15169 So, we're looking for the display line start with the
15170 minimum distance from the old window start. */
15171 pos = it.current.pos;
15172 min_distance = INFINITY;
15173 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15174 distance < min_distance)
15176 min_distance = distance;
15177 pos = it.current.pos;
15178 if (it.line_wrap == WORD_WRAP)
15180 /* Under WORD_WRAP, move_it_by_lines is likely to
15181 overshoot and stop not at the first, but the
15182 second character from the left margin. So in
15183 that case, we need a more tight control on the X
15184 coordinate of the iterator than move_it_by_lines
15185 promises in its contract. The method is to first
15186 go to the last (rightmost) visible character of a
15187 line, then move to the leftmost character on the
15188 next line in a separate call. */
15189 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15190 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15191 move_it_to (&it, ZV, 0,
15192 it.current_y + it.max_ascent + it.max_descent, -1,
15193 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15195 else
15196 move_it_by_lines (&it, 1);
15199 /* Set the window start there. */
15200 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15201 window_start_changed_p = 1;
15205 return window_start_changed_p;
15209 /* Try cursor movement in case text has not changed in window WINDOW,
15210 with window start STARTP. Value is
15212 CURSOR_MOVEMENT_SUCCESS if successful
15214 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15216 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15217 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15218 we want to scroll as if scroll-step were set to 1. See the code.
15220 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15221 which case we have to abort this redisplay, and adjust matrices
15222 first. */
15224 enum
15226 CURSOR_MOVEMENT_SUCCESS,
15227 CURSOR_MOVEMENT_CANNOT_BE_USED,
15228 CURSOR_MOVEMENT_MUST_SCROLL,
15229 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15232 static int
15233 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15235 struct window *w = XWINDOW (window);
15236 struct frame *f = XFRAME (w->frame);
15237 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15239 #ifdef GLYPH_DEBUG
15240 if (inhibit_try_cursor_movement)
15241 return rc;
15242 #endif
15244 /* Previously, there was a check for Lisp integer in the
15245 if-statement below. Now, this field is converted to
15246 ptrdiff_t, thus zero means invalid position in a buffer. */
15247 eassert (w->last_point > 0);
15248 /* Likewise there was a check whether window_end_vpos is nil or larger
15249 than the window. Now window_end_vpos is int and so never nil, but
15250 let's leave eassert to check whether it fits in the window. */
15251 eassert (w->window_end_vpos < w->current_matrix->nrows);
15253 /* Handle case where text has not changed, only point, and it has
15254 not moved off the frame. */
15255 if (/* Point may be in this window. */
15256 PT >= CHARPOS (startp)
15257 /* Selective display hasn't changed. */
15258 && !current_buffer->clip_changed
15259 /* Function force-mode-line-update is used to force a thorough
15260 redisplay. It sets either windows_or_buffers_changed or
15261 update_mode_lines. So don't take a shortcut here for these
15262 cases. */
15263 && !update_mode_lines
15264 && !windows_or_buffers_changed
15265 && !f->cursor_type_changed
15266 && NILP (Vshow_trailing_whitespace)
15267 /* This code is not used for mini-buffer for the sake of the case
15268 of redisplaying to replace an echo area message; since in
15269 that case the mini-buffer contents per se are usually
15270 unchanged. This code is of no real use in the mini-buffer
15271 since the handling of this_line_start_pos, etc., in redisplay
15272 handles the same cases. */
15273 && !EQ (window, minibuf_window)
15274 && (FRAME_WINDOW_P (f)
15275 || !overlay_arrow_in_current_buffer_p ()))
15277 int this_scroll_margin, top_scroll_margin;
15278 struct glyph_row *row = NULL;
15279 int frame_line_height = default_line_pixel_height (w);
15280 int window_total_lines
15281 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15283 #ifdef GLYPH_DEBUG
15284 debug_method_add (w, "cursor movement");
15285 #endif
15287 /* Scroll if point within this distance from the top or bottom
15288 of the window. This is a pixel value. */
15289 if (scroll_margin > 0)
15291 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15292 this_scroll_margin *= frame_line_height;
15294 else
15295 this_scroll_margin = 0;
15297 top_scroll_margin = this_scroll_margin;
15298 if (WINDOW_WANTS_HEADER_LINE_P (w))
15299 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15301 /* Start with the row the cursor was displayed during the last
15302 not paused redisplay. Give up if that row is not valid. */
15303 if (w->last_cursor_vpos < 0
15304 || w->last_cursor_vpos >= w->current_matrix->nrows)
15305 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15306 else
15308 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15309 if (row->mode_line_p)
15310 ++row;
15311 if (!row->enabled_p)
15312 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15315 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15317 int scroll_p = 0, must_scroll = 0;
15318 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15320 if (PT > w->last_point)
15322 /* Point has moved forward. */
15323 while (MATRIX_ROW_END_CHARPOS (row) < PT
15324 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15326 eassert (row->enabled_p);
15327 ++row;
15330 /* If the end position of a row equals the start
15331 position of the next row, and PT is at that position,
15332 we would rather display cursor in the next line. */
15333 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15334 && MATRIX_ROW_END_CHARPOS (row) == PT
15335 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15336 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15337 && !cursor_row_p (row))
15338 ++row;
15340 /* If within the scroll margin, scroll. Note that
15341 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15342 the next line would be drawn, and that
15343 this_scroll_margin can be zero. */
15344 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15345 || PT > MATRIX_ROW_END_CHARPOS (row)
15346 /* Line is completely visible last line in window
15347 and PT is to be set in the next line. */
15348 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15349 && PT == MATRIX_ROW_END_CHARPOS (row)
15350 && !row->ends_at_zv_p
15351 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15352 scroll_p = 1;
15354 else if (PT < w->last_point)
15356 /* Cursor has to be moved backward. Note that PT >=
15357 CHARPOS (startp) because of the outer if-statement. */
15358 while (!row->mode_line_p
15359 && (MATRIX_ROW_START_CHARPOS (row) > PT
15360 || (MATRIX_ROW_START_CHARPOS (row) == PT
15361 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15362 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15363 row > w->current_matrix->rows
15364 && (row-1)->ends_in_newline_from_string_p))))
15365 && (row->y > top_scroll_margin
15366 || CHARPOS (startp) == BEGV))
15368 eassert (row->enabled_p);
15369 --row;
15372 /* Consider the following case: Window starts at BEGV,
15373 there is invisible, intangible text at BEGV, so that
15374 display starts at some point START > BEGV. It can
15375 happen that we are called with PT somewhere between
15376 BEGV and START. Try to handle that case. */
15377 if (row < w->current_matrix->rows
15378 || row->mode_line_p)
15380 row = w->current_matrix->rows;
15381 if (row->mode_line_p)
15382 ++row;
15385 /* Due to newlines in overlay strings, we may have to
15386 skip forward over overlay strings. */
15387 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15388 && MATRIX_ROW_END_CHARPOS (row) == PT
15389 && !cursor_row_p (row))
15390 ++row;
15392 /* If within the scroll margin, scroll. */
15393 if (row->y < top_scroll_margin
15394 && CHARPOS (startp) != BEGV)
15395 scroll_p = 1;
15397 else
15399 /* Cursor did not move. So don't scroll even if cursor line
15400 is partially visible, as it was so before. */
15401 rc = CURSOR_MOVEMENT_SUCCESS;
15404 if (PT < MATRIX_ROW_START_CHARPOS (row)
15405 || PT > MATRIX_ROW_END_CHARPOS (row))
15407 /* if PT is not in the glyph row, give up. */
15408 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15409 must_scroll = 1;
15411 else if (rc != CURSOR_MOVEMENT_SUCCESS
15412 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15414 struct glyph_row *row1;
15416 /* If rows are bidi-reordered and point moved, back up
15417 until we find a row that does not belong to a
15418 continuation line. This is because we must consider
15419 all rows of a continued line as candidates for the
15420 new cursor positioning, since row start and end
15421 positions change non-linearly with vertical position
15422 in such rows. */
15423 /* FIXME: Revisit this when glyph ``spilling'' in
15424 continuation lines' rows is implemented for
15425 bidi-reordered rows. */
15426 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15427 MATRIX_ROW_CONTINUATION_LINE_P (row);
15428 --row)
15430 /* If we hit the beginning of the displayed portion
15431 without finding the first row of a continued
15432 line, give up. */
15433 if (row <= row1)
15435 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15436 break;
15438 eassert (row->enabled_p);
15441 if (must_scroll)
15443 else if (rc != CURSOR_MOVEMENT_SUCCESS
15444 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15445 /* Make sure this isn't a header line by any chance, since
15446 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15447 && !row->mode_line_p
15448 && make_cursor_line_fully_visible_p)
15450 if (PT == MATRIX_ROW_END_CHARPOS (row)
15451 && !row->ends_at_zv_p
15452 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15453 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15454 else if (row->height > window_box_height (w))
15456 /* If we end up in a partially visible line, let's
15457 make it fully visible, except when it's taller
15458 than the window, in which case we can't do much
15459 about it. */
15460 *scroll_step = 1;
15461 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15463 else
15465 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15466 if (!cursor_row_fully_visible_p (w, 0, 1))
15467 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15468 else
15469 rc = CURSOR_MOVEMENT_SUCCESS;
15472 else if (scroll_p)
15473 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15474 else if (rc != CURSOR_MOVEMENT_SUCCESS
15475 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15477 /* With bidi-reordered rows, there could be more than
15478 one candidate row whose start and end positions
15479 occlude point. We need to let set_cursor_from_row
15480 find the best candidate. */
15481 /* FIXME: Revisit this when glyph ``spilling'' in
15482 continuation lines' rows is implemented for
15483 bidi-reordered rows. */
15484 int rv = 0;
15488 int at_zv_p = 0, exact_match_p = 0;
15490 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15491 && PT <= MATRIX_ROW_END_CHARPOS (row)
15492 && cursor_row_p (row))
15493 rv |= set_cursor_from_row (w, row, w->current_matrix,
15494 0, 0, 0, 0);
15495 /* As soon as we've found the exact match for point,
15496 or the first suitable row whose ends_at_zv_p flag
15497 is set, we are done. */
15498 if (rv)
15500 at_zv_p = MATRIX_ROW (w->current_matrix,
15501 w->cursor.vpos)->ends_at_zv_p;
15502 if (!at_zv_p
15503 && w->cursor.hpos >= 0
15504 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15505 w->cursor.vpos))
15507 struct glyph_row *candidate =
15508 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15509 struct glyph *g =
15510 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15511 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15513 exact_match_p =
15514 (BUFFERP (g->object) && g->charpos == PT)
15515 || (INTEGERP (g->object)
15516 && (g->charpos == PT
15517 || (g->charpos == 0 && endpos - 1 == PT)));
15519 if (at_zv_p || exact_match_p)
15521 rc = CURSOR_MOVEMENT_SUCCESS;
15522 break;
15525 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15526 break;
15527 ++row;
15529 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15530 || row->continued_p)
15531 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15532 || (MATRIX_ROW_START_CHARPOS (row) == PT
15533 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15534 /* If we didn't find any candidate rows, or exited the
15535 loop before all the candidates were examined, signal
15536 to the caller that this method failed. */
15537 if (rc != CURSOR_MOVEMENT_SUCCESS
15538 && !(rv
15539 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15540 && !row->continued_p))
15541 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15542 else if (rv)
15543 rc = CURSOR_MOVEMENT_SUCCESS;
15545 else
15549 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15551 rc = CURSOR_MOVEMENT_SUCCESS;
15552 break;
15554 ++row;
15556 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15557 && MATRIX_ROW_START_CHARPOS (row) == PT
15558 && cursor_row_p (row));
15563 return rc;
15566 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15567 static
15568 #endif
15569 void
15570 set_vertical_scroll_bar (struct window *w)
15572 ptrdiff_t start, end, whole;
15574 /* Calculate the start and end positions for the current window.
15575 At some point, it would be nice to choose between scrollbars
15576 which reflect the whole buffer size, with special markers
15577 indicating narrowing, and scrollbars which reflect only the
15578 visible region.
15580 Note that mini-buffers sometimes aren't displaying any text. */
15581 if (!MINI_WINDOW_P (w)
15582 || (w == XWINDOW (minibuf_window)
15583 && NILP (echo_area_buffer[0])))
15585 struct buffer *buf = XBUFFER (w->contents);
15586 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15587 start = marker_position (w->start) - BUF_BEGV (buf);
15588 /* I don't think this is guaranteed to be right. For the
15589 moment, we'll pretend it is. */
15590 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15592 if (end < start)
15593 end = start;
15594 if (whole < (end - start))
15595 whole = end - start;
15597 else
15598 start = end = whole = 0;
15600 /* Indicate what this scroll bar ought to be displaying now. */
15601 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15602 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15603 (w, end - start, whole, start);
15607 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15608 selected_window is redisplayed.
15610 We can return without actually redisplaying the window if fonts has been
15611 changed on window's frame. In that case, redisplay_internal will retry. */
15613 static void
15614 redisplay_window (Lisp_Object window, bool just_this_one_p)
15616 struct window *w = XWINDOW (window);
15617 struct frame *f = XFRAME (w->frame);
15618 struct buffer *buffer = XBUFFER (w->contents);
15619 struct buffer *old = current_buffer;
15620 struct text_pos lpoint, opoint, startp;
15621 int update_mode_line;
15622 int tem;
15623 struct it it;
15624 /* Record it now because it's overwritten. */
15625 bool current_matrix_up_to_date_p = false;
15626 bool used_current_matrix_p = false;
15627 /* This is less strict than current_matrix_up_to_date_p.
15628 It indicates that the buffer contents and narrowing are unchanged. */
15629 bool buffer_unchanged_p = false;
15630 int temp_scroll_step = 0;
15631 ptrdiff_t count = SPECPDL_INDEX ();
15632 int rc;
15633 int centering_position = -1;
15634 int last_line_misfit = 0;
15635 ptrdiff_t beg_unchanged, end_unchanged;
15636 int frame_line_height;
15638 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15639 opoint = lpoint;
15641 #ifdef GLYPH_DEBUG
15642 *w->desired_matrix->method = 0;
15643 #endif
15645 if (!just_this_one_p
15646 && REDISPLAY_SOME_P ()
15647 && !w->redisplay
15648 && !f->redisplay
15649 && !buffer->text->redisplay
15650 && BUF_PT (buffer) == w->last_point)
15651 return;
15653 /* Make sure that both W's markers are valid. */
15654 eassert (XMARKER (w->start)->buffer == buffer);
15655 eassert (XMARKER (w->pointm)->buffer == buffer);
15657 restart:
15658 reconsider_clip_changes (w);
15659 frame_line_height = default_line_pixel_height (w);
15661 /* Has the mode line to be updated? */
15662 update_mode_line = (w->update_mode_line
15663 || update_mode_lines
15664 || buffer->clip_changed
15665 || buffer->prevent_redisplay_optimizations_p);
15667 if (!just_this_one_p)
15668 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15669 cleverly elsewhere. */
15670 w->must_be_updated_p = true;
15672 if (MINI_WINDOW_P (w))
15674 if (w == XWINDOW (echo_area_window)
15675 && !NILP (echo_area_buffer[0]))
15677 if (update_mode_line)
15678 /* We may have to update a tty frame's menu bar or a
15679 tool-bar. Example `M-x C-h C-h C-g'. */
15680 goto finish_menu_bars;
15681 else
15682 /* We've already displayed the echo area glyphs in this window. */
15683 goto finish_scroll_bars;
15685 else if ((w != XWINDOW (minibuf_window)
15686 || minibuf_level == 0)
15687 /* When buffer is nonempty, redisplay window normally. */
15688 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15689 /* Quail displays non-mini buffers in minibuffer window.
15690 In that case, redisplay the window normally. */
15691 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15693 /* W is a mini-buffer window, but it's not active, so clear
15694 it. */
15695 int yb = window_text_bottom_y (w);
15696 struct glyph_row *row;
15697 int y;
15699 for (y = 0, row = w->desired_matrix->rows;
15700 y < yb;
15701 y += row->height, ++row)
15702 blank_row (w, row, y);
15703 goto finish_scroll_bars;
15706 clear_glyph_matrix (w->desired_matrix);
15709 /* Otherwise set up data on this window; select its buffer and point
15710 value. */
15711 /* Really select the buffer, for the sake of buffer-local
15712 variables. */
15713 set_buffer_internal_1 (XBUFFER (w->contents));
15715 current_matrix_up_to_date_p
15716 = (w->window_end_valid
15717 && !current_buffer->clip_changed
15718 && !current_buffer->prevent_redisplay_optimizations_p
15719 && !window_outdated (w));
15721 /* Run the window-bottom-change-functions
15722 if it is possible that the text on the screen has changed
15723 (either due to modification of the text, or any other reason). */
15724 if (!current_matrix_up_to_date_p
15725 && !NILP (Vwindow_text_change_functions))
15727 safe_run_hooks (Qwindow_text_change_functions);
15728 goto restart;
15731 beg_unchanged = BEG_UNCHANGED;
15732 end_unchanged = END_UNCHANGED;
15734 SET_TEXT_POS (opoint, PT, PT_BYTE);
15736 specbind (Qinhibit_point_motion_hooks, Qt);
15738 buffer_unchanged_p
15739 = (w->window_end_valid
15740 && !current_buffer->clip_changed
15741 && !window_outdated (w));
15743 /* When windows_or_buffers_changed is non-zero, we can't rely
15744 on the window end being valid, so set it to zero there. */
15745 if (windows_or_buffers_changed)
15747 /* If window starts on a continuation line, maybe adjust the
15748 window start in case the window's width changed. */
15749 if (XMARKER (w->start)->buffer == current_buffer)
15750 compute_window_start_on_continuation_line (w);
15752 w->window_end_valid = false;
15753 /* If so, we also can't rely on current matrix
15754 and should not fool try_cursor_movement below. */
15755 current_matrix_up_to_date_p = false;
15758 /* Some sanity checks. */
15759 CHECK_WINDOW_END (w);
15760 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15761 emacs_abort ();
15762 if (BYTEPOS (opoint) < CHARPOS (opoint))
15763 emacs_abort ();
15765 if (mode_line_update_needed (w))
15766 update_mode_line = 1;
15768 /* Point refers normally to the selected window. For any other
15769 window, set up appropriate value. */
15770 if (!EQ (window, selected_window))
15772 ptrdiff_t new_pt = marker_position (w->pointm);
15773 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15774 if (new_pt < BEGV)
15776 new_pt = BEGV;
15777 new_pt_byte = BEGV_BYTE;
15778 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15780 else if (new_pt > (ZV - 1))
15782 new_pt = ZV;
15783 new_pt_byte = ZV_BYTE;
15784 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15787 /* We don't use SET_PT so that the point-motion hooks don't run. */
15788 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15791 /* If any of the character widths specified in the display table
15792 have changed, invalidate the width run cache. It's true that
15793 this may be a bit late to catch such changes, but the rest of
15794 redisplay goes (non-fatally) haywire when the display table is
15795 changed, so why should we worry about doing any better? */
15796 if (current_buffer->width_run_cache
15797 || (current_buffer->base_buffer
15798 && current_buffer->base_buffer->width_run_cache))
15800 struct Lisp_Char_Table *disptab = buffer_display_table ();
15802 if (! disptab_matches_widthtab
15803 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15805 struct buffer *buf = current_buffer;
15807 if (buf->base_buffer)
15808 buf = buf->base_buffer;
15809 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15810 recompute_width_table (current_buffer, disptab);
15814 /* If window-start is screwed up, choose a new one. */
15815 if (XMARKER (w->start)->buffer != current_buffer)
15816 goto recenter;
15818 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15820 /* If someone specified a new starting point but did not insist,
15821 check whether it can be used. */
15822 if (w->optional_new_start
15823 && CHARPOS (startp) >= BEGV
15824 && CHARPOS (startp) <= ZV)
15826 w->optional_new_start = 0;
15827 start_display (&it, w, startp);
15828 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15829 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15830 if (IT_CHARPOS (it) == PT)
15831 w->force_start = 1;
15832 /* IT may overshoot PT if text at PT is invisible. */
15833 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15834 w->force_start = 1;
15837 force_start:
15839 /* Handle case where place to start displaying has been specified,
15840 unless the specified location is outside the accessible range. */
15841 if (w->force_start || window_frozen_p (w))
15843 /* We set this later on if we have to adjust point. */
15844 int new_vpos = -1;
15846 w->force_start = 0;
15847 w->vscroll = 0;
15848 w->window_end_valid = 0;
15850 /* Forget any recorded base line for line number display. */
15851 if (!buffer_unchanged_p)
15852 w->base_line_number = 0;
15854 /* Redisplay the mode line. Select the buffer properly for that.
15855 Also, run the hook window-scroll-functions
15856 because we have scrolled. */
15857 /* Note, we do this after clearing force_start because
15858 if there's an error, it is better to forget about force_start
15859 than to get into an infinite loop calling the hook functions
15860 and having them get more errors. */
15861 if (!update_mode_line
15862 || ! NILP (Vwindow_scroll_functions))
15864 update_mode_line = 1;
15865 w->update_mode_line = 1;
15866 startp = run_window_scroll_functions (window, startp);
15869 if (CHARPOS (startp) < BEGV)
15870 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15871 else if (CHARPOS (startp) > ZV)
15872 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15874 /* Redisplay, then check if cursor has been set during the
15875 redisplay. Give up if new fonts were loaded. */
15876 /* We used to issue a CHECK_MARGINS argument to try_window here,
15877 but this causes scrolling to fail when point begins inside
15878 the scroll margin (bug#148) -- cyd */
15879 if (!try_window (window, startp, 0))
15881 w->force_start = 1;
15882 clear_glyph_matrix (w->desired_matrix);
15883 goto need_larger_matrices;
15886 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15888 /* If point does not appear, try to move point so it does
15889 appear. The desired matrix has been built above, so we
15890 can use it here. */
15891 new_vpos = window_box_height (w) / 2;
15894 if (!cursor_row_fully_visible_p (w, 0, 0))
15896 /* Point does appear, but on a line partly visible at end of window.
15897 Move it back to a fully-visible line. */
15898 new_vpos = window_box_height (w);
15900 else if (w->cursor.vpos >= 0)
15902 /* Some people insist on not letting point enter the scroll
15903 margin, even though this part handles windows that didn't
15904 scroll at all. */
15905 int window_total_lines
15906 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15907 int margin = min (scroll_margin, window_total_lines / 4);
15908 int pixel_margin = margin * frame_line_height;
15909 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15911 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15912 below, which finds the row to move point to, advances by
15913 the Y coordinate of the _next_ row, see the definition of
15914 MATRIX_ROW_BOTTOM_Y. */
15915 if (w->cursor.vpos < margin + header_line)
15917 w->cursor.vpos = -1;
15918 clear_glyph_matrix (w->desired_matrix);
15919 goto try_to_scroll;
15921 else
15923 int window_height = window_box_height (w);
15925 if (header_line)
15926 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15927 if (w->cursor.y >= window_height - pixel_margin)
15929 w->cursor.vpos = -1;
15930 clear_glyph_matrix (w->desired_matrix);
15931 goto try_to_scroll;
15936 /* If we need to move point for either of the above reasons,
15937 now actually do it. */
15938 if (new_vpos >= 0)
15940 struct glyph_row *row;
15942 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15943 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15944 ++row;
15946 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15947 MATRIX_ROW_START_BYTEPOS (row));
15949 if (w != XWINDOW (selected_window))
15950 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15951 else if (current_buffer == old)
15952 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15954 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15956 /* If we are highlighting the region, then we just changed
15957 the region, so redisplay to show it. */
15958 /* FIXME: We need to (re)run pre-redisplay-function! */
15959 /* if (markpos_of_region () >= 0)
15961 clear_glyph_matrix (w->desired_matrix);
15962 if (!try_window (window, startp, 0))
15963 goto need_larger_matrices;
15968 #ifdef GLYPH_DEBUG
15969 debug_method_add (w, "forced window start");
15970 #endif
15971 goto done;
15974 /* Handle case where text has not changed, only point, and it has
15975 not moved off the frame, and we are not retrying after hscroll.
15976 (current_matrix_up_to_date_p is nonzero when retrying.) */
15977 if (current_matrix_up_to_date_p
15978 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15979 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15981 switch (rc)
15983 case CURSOR_MOVEMENT_SUCCESS:
15984 used_current_matrix_p = 1;
15985 goto done;
15987 case CURSOR_MOVEMENT_MUST_SCROLL:
15988 goto try_to_scroll;
15990 default:
15991 emacs_abort ();
15994 /* If current starting point was originally the beginning of a line
15995 but no longer is, find a new starting point. */
15996 else if (w->start_at_line_beg
15997 && !(CHARPOS (startp) <= BEGV
15998 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16000 #ifdef GLYPH_DEBUG
16001 debug_method_add (w, "recenter 1");
16002 #endif
16003 goto recenter;
16006 /* Try scrolling with try_window_id. Value is > 0 if update has
16007 been done, it is -1 if we know that the same window start will
16008 not work. It is 0 if unsuccessful for some other reason. */
16009 else if ((tem = try_window_id (w)) != 0)
16011 #ifdef GLYPH_DEBUG
16012 debug_method_add (w, "try_window_id %d", tem);
16013 #endif
16015 if (f->fonts_changed)
16016 goto need_larger_matrices;
16017 if (tem > 0)
16018 goto done;
16020 /* Otherwise try_window_id has returned -1 which means that we
16021 don't want the alternative below this comment to execute. */
16023 else if (CHARPOS (startp) >= BEGV
16024 && CHARPOS (startp) <= ZV
16025 && PT >= CHARPOS (startp)
16026 && (CHARPOS (startp) < ZV
16027 /* Avoid starting at end of buffer. */
16028 || CHARPOS (startp) == BEGV
16029 || !window_outdated (w)))
16031 int d1, d2, d3, d4, d5, d6;
16033 /* If first window line is a continuation line, and window start
16034 is inside the modified region, but the first change is before
16035 current window start, we must select a new window start.
16037 However, if this is the result of a down-mouse event (e.g. by
16038 extending the mouse-drag-overlay), we don't want to select a
16039 new window start, since that would change the position under
16040 the mouse, resulting in an unwanted mouse-movement rather
16041 than a simple mouse-click. */
16042 if (!w->start_at_line_beg
16043 && NILP (do_mouse_tracking)
16044 && CHARPOS (startp) > BEGV
16045 && CHARPOS (startp) > BEG + beg_unchanged
16046 && CHARPOS (startp) <= Z - end_unchanged
16047 /* Even if w->start_at_line_beg is nil, a new window may
16048 start at a line_beg, since that's how set_buffer_window
16049 sets it. So, we need to check the return value of
16050 compute_window_start_on_continuation_line. (See also
16051 bug#197). */
16052 && XMARKER (w->start)->buffer == current_buffer
16053 && compute_window_start_on_continuation_line (w)
16054 /* It doesn't make sense to force the window start like we
16055 do at label force_start if it is already known that point
16056 will not be visible in the resulting window, because
16057 doing so will move point from its correct position
16058 instead of scrolling the window to bring point into view.
16059 See bug#9324. */
16060 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16062 w->force_start = 1;
16063 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16064 goto force_start;
16067 #ifdef GLYPH_DEBUG
16068 debug_method_add (w, "same window start");
16069 #endif
16071 /* Try to redisplay starting at same place as before.
16072 If point has not moved off frame, accept the results. */
16073 if (!current_matrix_up_to_date_p
16074 /* Don't use try_window_reusing_current_matrix in this case
16075 because a window scroll function can have changed the
16076 buffer. */
16077 || !NILP (Vwindow_scroll_functions)
16078 || MINI_WINDOW_P (w)
16079 || !(used_current_matrix_p
16080 = try_window_reusing_current_matrix (w)))
16082 IF_DEBUG (debug_method_add (w, "1"));
16083 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16084 /* -1 means we need to scroll.
16085 0 means we need new matrices, but fonts_changed
16086 is set in that case, so we will detect it below. */
16087 goto try_to_scroll;
16090 if (f->fonts_changed)
16091 goto need_larger_matrices;
16093 if (w->cursor.vpos >= 0)
16095 if (!just_this_one_p
16096 || current_buffer->clip_changed
16097 || BEG_UNCHANGED < CHARPOS (startp))
16098 /* Forget any recorded base line for line number display. */
16099 w->base_line_number = 0;
16101 if (!cursor_row_fully_visible_p (w, 1, 0))
16103 clear_glyph_matrix (w->desired_matrix);
16104 last_line_misfit = 1;
16106 /* Drop through and scroll. */
16107 else
16108 goto done;
16110 else
16111 clear_glyph_matrix (w->desired_matrix);
16114 try_to_scroll:
16116 /* Redisplay the mode line. Select the buffer properly for that. */
16117 if (!update_mode_line)
16119 update_mode_line = 1;
16120 w->update_mode_line = 1;
16123 /* Try to scroll by specified few lines. */
16124 if ((scroll_conservatively
16125 || emacs_scroll_step
16126 || temp_scroll_step
16127 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16128 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16129 && CHARPOS (startp) >= BEGV
16130 && CHARPOS (startp) <= ZV)
16132 /* The function returns -1 if new fonts were loaded, 1 if
16133 successful, 0 if not successful. */
16134 int ss = try_scrolling (window, just_this_one_p,
16135 scroll_conservatively,
16136 emacs_scroll_step,
16137 temp_scroll_step, last_line_misfit);
16138 switch (ss)
16140 case SCROLLING_SUCCESS:
16141 goto done;
16143 case SCROLLING_NEED_LARGER_MATRICES:
16144 goto need_larger_matrices;
16146 case SCROLLING_FAILED:
16147 break;
16149 default:
16150 emacs_abort ();
16154 /* Finally, just choose a place to start which positions point
16155 according to user preferences. */
16157 recenter:
16159 #ifdef GLYPH_DEBUG
16160 debug_method_add (w, "recenter");
16161 #endif
16163 /* Forget any previously recorded base line for line number display. */
16164 if (!buffer_unchanged_p)
16165 w->base_line_number = 0;
16167 /* Determine the window start relative to point. */
16168 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16169 it.current_y = it.last_visible_y;
16170 if (centering_position < 0)
16172 int window_total_lines
16173 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16174 int margin =
16175 scroll_margin > 0
16176 ? min (scroll_margin, window_total_lines / 4)
16177 : 0;
16178 ptrdiff_t margin_pos = CHARPOS (startp);
16179 Lisp_Object aggressive;
16180 int scrolling_up;
16182 /* If there is a scroll margin at the top of the window, find
16183 its character position. */
16184 if (margin
16185 /* Cannot call start_display if startp is not in the
16186 accessible region of the buffer. This can happen when we
16187 have just switched to a different buffer and/or changed
16188 its restriction. In that case, startp is initialized to
16189 the character position 1 (BEGV) because we did not yet
16190 have chance to display the buffer even once. */
16191 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16193 struct it it1;
16194 void *it1data = NULL;
16196 SAVE_IT (it1, it, it1data);
16197 start_display (&it1, w, startp);
16198 move_it_vertically (&it1, margin * frame_line_height);
16199 margin_pos = IT_CHARPOS (it1);
16200 RESTORE_IT (&it, &it, it1data);
16202 scrolling_up = PT > margin_pos;
16203 aggressive =
16204 scrolling_up
16205 ? BVAR (current_buffer, scroll_up_aggressively)
16206 : BVAR (current_buffer, scroll_down_aggressively);
16208 if (!MINI_WINDOW_P (w)
16209 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16211 int pt_offset = 0;
16213 /* Setting scroll-conservatively overrides
16214 scroll-*-aggressively. */
16215 if (!scroll_conservatively && NUMBERP (aggressive))
16217 double float_amount = XFLOATINT (aggressive);
16219 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16220 if (pt_offset == 0 && float_amount > 0)
16221 pt_offset = 1;
16222 if (pt_offset && margin > 0)
16223 margin -= 1;
16225 /* Compute how much to move the window start backward from
16226 point so that point will be displayed where the user
16227 wants it. */
16228 if (scrolling_up)
16230 centering_position = it.last_visible_y;
16231 if (pt_offset)
16232 centering_position -= pt_offset;
16233 centering_position -=
16234 frame_line_height * (1 + margin + (last_line_misfit != 0))
16235 + WINDOW_HEADER_LINE_HEIGHT (w);
16236 /* Don't let point enter the scroll margin near top of
16237 the window. */
16238 if (centering_position < margin * frame_line_height)
16239 centering_position = margin * frame_line_height;
16241 else
16242 centering_position = margin * frame_line_height + pt_offset;
16244 else
16245 /* Set the window start half the height of the window backward
16246 from point. */
16247 centering_position = window_box_height (w) / 2;
16249 move_it_vertically_backward (&it, centering_position);
16251 eassert (IT_CHARPOS (it) >= BEGV);
16253 /* The function move_it_vertically_backward may move over more
16254 than the specified y-distance. If it->w is small, e.g. a
16255 mini-buffer window, we may end up in front of the window's
16256 display area. Start displaying at the start of the line
16257 containing PT in this case. */
16258 if (it.current_y <= 0)
16260 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16261 move_it_vertically_backward (&it, 0);
16262 it.current_y = 0;
16265 it.current_x = it.hpos = 0;
16267 /* Set the window start position here explicitly, to avoid an
16268 infinite loop in case the functions in window-scroll-functions
16269 get errors. */
16270 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16272 /* Run scroll hooks. */
16273 startp = run_window_scroll_functions (window, it.current.pos);
16275 /* Redisplay the window. */
16276 if (!current_matrix_up_to_date_p
16277 || windows_or_buffers_changed
16278 || f->cursor_type_changed
16279 /* Don't use try_window_reusing_current_matrix in this case
16280 because it can have changed the buffer. */
16281 || !NILP (Vwindow_scroll_functions)
16282 || !just_this_one_p
16283 || MINI_WINDOW_P (w)
16284 || !(used_current_matrix_p
16285 = try_window_reusing_current_matrix (w)))
16286 try_window (window, startp, 0);
16288 /* If new fonts have been loaded (due to fontsets), give up. We
16289 have to start a new redisplay since we need to re-adjust glyph
16290 matrices. */
16291 if (f->fonts_changed)
16292 goto need_larger_matrices;
16294 /* If cursor did not appear assume that the middle of the window is
16295 in the first line of the window. Do it again with the next line.
16296 (Imagine a window of height 100, displaying two lines of height
16297 60. Moving back 50 from it->last_visible_y will end in the first
16298 line.) */
16299 if (w->cursor.vpos < 0)
16301 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16303 clear_glyph_matrix (w->desired_matrix);
16304 move_it_by_lines (&it, 1);
16305 try_window (window, it.current.pos, 0);
16307 else if (PT < IT_CHARPOS (it))
16309 clear_glyph_matrix (w->desired_matrix);
16310 move_it_by_lines (&it, -1);
16311 try_window (window, it.current.pos, 0);
16313 else
16315 /* Not much we can do about it. */
16319 /* Consider the following case: Window starts at BEGV, there is
16320 invisible, intangible text at BEGV, so that display starts at
16321 some point START > BEGV. It can happen that we are called with
16322 PT somewhere between BEGV and START. Try to handle that case. */
16323 if (w->cursor.vpos < 0)
16325 struct glyph_row *row = w->current_matrix->rows;
16326 if (row->mode_line_p)
16327 ++row;
16328 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16331 if (!cursor_row_fully_visible_p (w, 0, 0))
16333 /* If vscroll is enabled, disable it and try again. */
16334 if (w->vscroll)
16336 w->vscroll = 0;
16337 clear_glyph_matrix (w->desired_matrix);
16338 goto recenter;
16341 /* Users who set scroll-conservatively to a large number want
16342 point just above/below the scroll margin. If we ended up
16343 with point's row partially visible, move the window start to
16344 make that row fully visible and out of the margin. */
16345 if (scroll_conservatively > SCROLL_LIMIT)
16347 int window_total_lines
16348 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16349 int margin =
16350 scroll_margin > 0
16351 ? min (scroll_margin, window_total_lines / 4)
16352 : 0;
16353 int move_down = w->cursor.vpos >= window_total_lines / 2;
16355 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16356 clear_glyph_matrix (w->desired_matrix);
16357 if (1 == try_window (window, it.current.pos,
16358 TRY_WINDOW_CHECK_MARGINS))
16359 goto done;
16362 /* If centering point failed to make the whole line visible,
16363 put point at the top instead. That has to make the whole line
16364 visible, if it can be done. */
16365 if (centering_position == 0)
16366 goto done;
16368 clear_glyph_matrix (w->desired_matrix);
16369 centering_position = 0;
16370 goto recenter;
16373 done:
16375 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16376 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16377 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16379 /* Display the mode line, if we must. */
16380 if ((update_mode_line
16381 /* If window not full width, must redo its mode line
16382 if (a) the window to its side is being redone and
16383 (b) we do a frame-based redisplay. This is a consequence
16384 of how inverted lines are drawn in frame-based redisplay. */
16385 || (!just_this_one_p
16386 && !FRAME_WINDOW_P (f)
16387 && !WINDOW_FULL_WIDTH_P (w))
16388 /* Line number to display. */
16389 || w->base_line_pos > 0
16390 /* Column number is displayed and different from the one displayed. */
16391 || (w->column_number_displayed != -1
16392 && (w->column_number_displayed != current_column ())))
16393 /* This means that the window has a mode line. */
16394 && (WINDOW_WANTS_MODELINE_P (w)
16395 || WINDOW_WANTS_HEADER_LINE_P (w)))
16398 display_mode_lines (w);
16400 /* If mode line height has changed, arrange for a thorough
16401 immediate redisplay using the correct mode line height. */
16402 if (WINDOW_WANTS_MODELINE_P (w)
16403 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16405 f->fonts_changed = 1;
16406 w->mode_line_height = -1;
16407 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16408 = DESIRED_MODE_LINE_HEIGHT (w);
16411 /* If header line height has changed, arrange for a thorough
16412 immediate redisplay using the correct header line height. */
16413 if (WINDOW_WANTS_HEADER_LINE_P (w)
16414 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16416 f->fonts_changed = 1;
16417 w->header_line_height = -1;
16418 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16419 = DESIRED_HEADER_LINE_HEIGHT (w);
16422 if (f->fonts_changed)
16423 goto need_larger_matrices;
16426 if (!line_number_displayed && w->base_line_pos != -1)
16428 w->base_line_pos = 0;
16429 w->base_line_number = 0;
16432 finish_menu_bars:
16434 /* When we reach a frame's selected window, redo the frame's menu bar. */
16435 if (update_mode_line
16436 && EQ (FRAME_SELECTED_WINDOW (f), window))
16438 int redisplay_menu_p = 0;
16440 if (FRAME_WINDOW_P (f))
16442 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16443 || defined (HAVE_NS) || defined (USE_GTK)
16444 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16445 #else
16446 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16447 #endif
16449 else
16450 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16452 if (redisplay_menu_p)
16453 display_menu_bar (w);
16455 #ifdef HAVE_WINDOW_SYSTEM
16456 if (FRAME_WINDOW_P (f))
16458 #if defined (USE_GTK) || defined (HAVE_NS)
16459 if (FRAME_EXTERNAL_TOOL_BAR (f))
16460 redisplay_tool_bar (f);
16461 #else
16462 if (WINDOWP (f->tool_bar_window)
16463 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16464 || !NILP (Vauto_resize_tool_bars))
16465 && redisplay_tool_bar (f))
16466 ignore_mouse_drag_p = 1;
16467 #endif
16469 #endif
16472 #ifdef HAVE_WINDOW_SYSTEM
16473 if (FRAME_WINDOW_P (f)
16474 && update_window_fringes (w, (just_this_one_p
16475 || (!used_current_matrix_p && !overlay_arrow_seen)
16476 || w->pseudo_window_p)))
16478 update_begin (f);
16479 block_input ();
16480 if (draw_window_fringes (w, 1))
16482 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16483 x_draw_right_divider (w);
16484 else
16485 x_draw_vertical_border (w);
16487 unblock_input ();
16488 update_end (f);
16491 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16492 x_draw_bottom_divider (w);
16493 #endif /* HAVE_WINDOW_SYSTEM */
16495 /* We go to this label, with fonts_changed set, if it is
16496 necessary to try again using larger glyph matrices.
16497 We have to redeem the scroll bar even in this case,
16498 because the loop in redisplay_internal expects that. */
16499 need_larger_matrices:
16501 finish_scroll_bars:
16503 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16505 /* Set the thumb's position and size. */
16506 set_vertical_scroll_bar (w);
16508 /* Note that we actually used the scroll bar attached to this
16509 window, so it shouldn't be deleted at the end of redisplay. */
16510 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16511 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16514 /* Restore current_buffer and value of point in it. The window
16515 update may have changed the buffer, so first make sure `opoint'
16516 is still valid (Bug#6177). */
16517 if (CHARPOS (opoint) < BEGV)
16518 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16519 else if (CHARPOS (opoint) > ZV)
16520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16521 else
16522 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16524 set_buffer_internal_1 (old);
16525 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16526 shorter. This can be caused by log truncation in *Messages*. */
16527 if (CHARPOS (lpoint) <= ZV)
16528 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16530 unbind_to (count, Qnil);
16534 /* Build the complete desired matrix of WINDOW with a window start
16535 buffer position POS.
16537 Value is 1 if successful. It is zero if fonts were loaded during
16538 redisplay which makes re-adjusting glyph matrices necessary, and -1
16539 if point would appear in the scroll margins.
16540 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16541 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16542 set in FLAGS.) */
16545 try_window (Lisp_Object window, struct text_pos pos, int flags)
16547 struct window *w = XWINDOW (window);
16548 struct it it;
16549 struct glyph_row *last_text_row = NULL;
16550 struct frame *f = XFRAME (w->frame);
16551 int frame_line_height = default_line_pixel_height (w);
16553 /* Make POS the new window start. */
16554 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16556 /* Mark cursor position as unknown. No overlay arrow seen. */
16557 w->cursor.vpos = -1;
16558 overlay_arrow_seen = 0;
16560 /* Initialize iterator and info to start at POS. */
16561 start_display (&it, w, pos);
16563 /* Display all lines of W. */
16564 while (it.current_y < it.last_visible_y)
16566 if (display_line (&it))
16567 last_text_row = it.glyph_row - 1;
16568 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16569 return 0;
16572 /* Don't let the cursor end in the scroll margins. */
16573 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16574 && !MINI_WINDOW_P (w))
16576 int this_scroll_margin;
16577 int window_total_lines
16578 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16580 if (scroll_margin > 0)
16582 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16583 this_scroll_margin *= frame_line_height;
16585 else
16586 this_scroll_margin = 0;
16588 if ((w->cursor.y >= 0 /* not vscrolled */
16589 && w->cursor.y < this_scroll_margin
16590 && CHARPOS (pos) > BEGV
16591 && IT_CHARPOS (it) < ZV)
16592 /* rms: considering make_cursor_line_fully_visible_p here
16593 seems to give wrong results. We don't want to recenter
16594 when the last line is partly visible, we want to allow
16595 that case to be handled in the usual way. */
16596 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16598 w->cursor.vpos = -1;
16599 clear_glyph_matrix (w->desired_matrix);
16600 return -1;
16604 /* If bottom moved off end of frame, change mode line percentage. */
16605 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16606 w->update_mode_line = 1;
16608 /* Set window_end_pos to the offset of the last character displayed
16609 on the window from the end of current_buffer. Set
16610 window_end_vpos to its row number. */
16611 if (last_text_row)
16613 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16614 adjust_window_ends (w, last_text_row, 0);
16615 eassert
16616 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16617 w->window_end_vpos)));
16619 else
16621 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16622 w->window_end_pos = Z - ZV;
16623 w->window_end_vpos = 0;
16626 /* But that is not valid info until redisplay finishes. */
16627 w->window_end_valid = 0;
16628 return 1;
16633 /************************************************************************
16634 Window redisplay reusing current matrix when buffer has not changed
16635 ************************************************************************/
16637 /* Try redisplay of window W showing an unchanged buffer with a
16638 different window start than the last time it was displayed by
16639 reusing its current matrix. Value is non-zero if successful.
16640 W->start is the new window start. */
16642 static int
16643 try_window_reusing_current_matrix (struct window *w)
16645 struct frame *f = XFRAME (w->frame);
16646 struct glyph_row *bottom_row;
16647 struct it it;
16648 struct run run;
16649 struct text_pos start, new_start;
16650 int nrows_scrolled, i;
16651 struct glyph_row *last_text_row;
16652 struct glyph_row *last_reused_text_row;
16653 struct glyph_row *start_row;
16654 int start_vpos, min_y, max_y;
16656 #ifdef GLYPH_DEBUG
16657 if (inhibit_try_window_reusing)
16658 return 0;
16659 #endif
16661 if (/* This function doesn't handle terminal frames. */
16662 !FRAME_WINDOW_P (f)
16663 /* Don't try to reuse the display if windows have been split
16664 or such. */
16665 || windows_or_buffers_changed
16666 || f->cursor_type_changed)
16667 return 0;
16669 /* Can't do this if showing trailing whitespace. */
16670 if (!NILP (Vshow_trailing_whitespace))
16671 return 0;
16673 /* If top-line visibility has changed, give up. */
16674 if (WINDOW_WANTS_HEADER_LINE_P (w)
16675 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16676 return 0;
16678 /* Give up if old or new display is scrolled vertically. We could
16679 make this function handle this, but right now it doesn't. */
16680 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16681 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16682 return 0;
16684 /* The variable new_start now holds the new window start. The old
16685 start `start' can be determined from the current matrix. */
16686 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16687 start = start_row->minpos;
16688 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16690 /* Clear the desired matrix for the display below. */
16691 clear_glyph_matrix (w->desired_matrix);
16693 if (CHARPOS (new_start) <= CHARPOS (start))
16695 /* Don't use this method if the display starts with an ellipsis
16696 displayed for invisible text. It's not easy to handle that case
16697 below, and it's certainly not worth the effort since this is
16698 not a frequent case. */
16699 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16700 return 0;
16702 IF_DEBUG (debug_method_add (w, "twu1"));
16704 /* Display up to a row that can be reused. The variable
16705 last_text_row is set to the last row displayed that displays
16706 text. Note that it.vpos == 0 if or if not there is a
16707 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16708 start_display (&it, w, new_start);
16709 w->cursor.vpos = -1;
16710 last_text_row = last_reused_text_row = NULL;
16712 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16714 /* If we have reached into the characters in the START row,
16715 that means the line boundaries have changed. So we
16716 can't start copying with the row START. Maybe it will
16717 work to start copying with the following row. */
16718 while (IT_CHARPOS (it) > CHARPOS (start))
16720 /* Advance to the next row as the "start". */
16721 start_row++;
16722 start = start_row->minpos;
16723 /* If there are no more rows to try, or just one, give up. */
16724 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16725 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16726 || CHARPOS (start) == ZV)
16728 clear_glyph_matrix (w->desired_matrix);
16729 return 0;
16732 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16734 /* If we have reached alignment, we can copy the rest of the
16735 rows. */
16736 if (IT_CHARPOS (it) == CHARPOS (start)
16737 /* Don't accept "alignment" inside a display vector,
16738 since start_row could have started in the middle of
16739 that same display vector (thus their character
16740 positions match), and we have no way of telling if
16741 that is the case. */
16742 && it.current.dpvec_index < 0)
16743 break;
16745 if (display_line (&it))
16746 last_text_row = it.glyph_row - 1;
16750 /* A value of current_y < last_visible_y means that we stopped
16751 at the previous window start, which in turn means that we
16752 have at least one reusable row. */
16753 if (it.current_y < it.last_visible_y)
16755 struct glyph_row *row;
16757 /* IT.vpos always starts from 0; it counts text lines. */
16758 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16760 /* Find PT if not already found in the lines displayed. */
16761 if (w->cursor.vpos < 0)
16763 int dy = it.current_y - start_row->y;
16765 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16766 row = row_containing_pos (w, PT, row, NULL, dy);
16767 if (row)
16768 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16769 dy, nrows_scrolled);
16770 else
16772 clear_glyph_matrix (w->desired_matrix);
16773 return 0;
16777 /* Scroll the display. Do it before the current matrix is
16778 changed. The problem here is that update has not yet
16779 run, i.e. part of the current matrix is not up to date.
16780 scroll_run_hook will clear the cursor, and use the
16781 current matrix to get the height of the row the cursor is
16782 in. */
16783 run.current_y = start_row->y;
16784 run.desired_y = it.current_y;
16785 run.height = it.last_visible_y - it.current_y;
16787 if (run.height > 0 && run.current_y != run.desired_y)
16789 update_begin (f);
16790 FRAME_RIF (f)->update_window_begin_hook (w);
16791 FRAME_RIF (f)->clear_window_mouse_face (w);
16792 FRAME_RIF (f)->scroll_run_hook (w, &run);
16793 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16794 update_end (f);
16797 /* Shift current matrix down by nrows_scrolled lines. */
16798 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16799 rotate_matrix (w->current_matrix,
16800 start_vpos,
16801 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16802 nrows_scrolled);
16804 /* Disable lines that must be updated. */
16805 for (i = 0; i < nrows_scrolled; ++i)
16806 (start_row + i)->enabled_p = false;
16808 /* Re-compute Y positions. */
16809 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16810 max_y = it.last_visible_y;
16811 for (row = start_row + nrows_scrolled;
16812 row < bottom_row;
16813 ++row)
16815 row->y = it.current_y;
16816 row->visible_height = row->height;
16818 if (row->y < min_y)
16819 row->visible_height -= min_y - row->y;
16820 if (row->y + row->height > max_y)
16821 row->visible_height -= row->y + row->height - max_y;
16822 if (row->fringe_bitmap_periodic_p)
16823 row->redraw_fringe_bitmaps_p = 1;
16825 it.current_y += row->height;
16827 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16828 last_reused_text_row = row;
16829 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16830 break;
16833 /* Disable lines in the current matrix which are now
16834 below the window. */
16835 for (++row; row < bottom_row; ++row)
16836 row->enabled_p = row->mode_line_p = 0;
16839 /* Update window_end_pos etc.; last_reused_text_row is the last
16840 reused row from the current matrix containing text, if any.
16841 The value of last_text_row is the last displayed line
16842 containing text. */
16843 if (last_reused_text_row)
16844 adjust_window_ends (w, last_reused_text_row, 1);
16845 else if (last_text_row)
16846 adjust_window_ends (w, last_text_row, 0);
16847 else
16849 /* This window must be completely empty. */
16850 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16851 w->window_end_pos = Z - ZV;
16852 w->window_end_vpos = 0;
16854 w->window_end_valid = 0;
16856 /* Update hint: don't try scrolling again in update_window. */
16857 w->desired_matrix->no_scrolling_p = 1;
16859 #ifdef GLYPH_DEBUG
16860 debug_method_add (w, "try_window_reusing_current_matrix 1");
16861 #endif
16862 return 1;
16864 else if (CHARPOS (new_start) > CHARPOS (start))
16866 struct glyph_row *pt_row, *row;
16867 struct glyph_row *first_reusable_row;
16868 struct glyph_row *first_row_to_display;
16869 int dy;
16870 int yb = window_text_bottom_y (w);
16872 /* Find the row starting at new_start, if there is one. Don't
16873 reuse a partially visible line at the end. */
16874 first_reusable_row = start_row;
16875 while (first_reusable_row->enabled_p
16876 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16877 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16878 < CHARPOS (new_start)))
16879 ++first_reusable_row;
16881 /* Give up if there is no row to reuse. */
16882 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16883 || !first_reusable_row->enabled_p
16884 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16885 != CHARPOS (new_start)))
16886 return 0;
16888 /* We can reuse fully visible rows beginning with
16889 first_reusable_row to the end of the window. Set
16890 first_row_to_display to the first row that cannot be reused.
16891 Set pt_row to the row containing point, if there is any. */
16892 pt_row = NULL;
16893 for (first_row_to_display = first_reusable_row;
16894 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16895 ++first_row_to_display)
16897 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16898 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16899 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16900 && first_row_to_display->ends_at_zv_p
16901 && pt_row == NULL)))
16902 pt_row = first_row_to_display;
16905 /* Start displaying at the start of first_row_to_display. */
16906 eassert (first_row_to_display->y < yb);
16907 init_to_row_start (&it, w, first_row_to_display);
16909 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16910 - start_vpos);
16911 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16912 - nrows_scrolled);
16913 it.current_y = (first_row_to_display->y - first_reusable_row->y
16914 + WINDOW_HEADER_LINE_HEIGHT (w));
16916 /* Display lines beginning with first_row_to_display in the
16917 desired matrix. Set last_text_row to the last row displayed
16918 that displays text. */
16919 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16920 if (pt_row == NULL)
16921 w->cursor.vpos = -1;
16922 last_text_row = NULL;
16923 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16924 if (display_line (&it))
16925 last_text_row = it.glyph_row - 1;
16927 /* If point is in a reused row, adjust y and vpos of the cursor
16928 position. */
16929 if (pt_row)
16931 w->cursor.vpos -= nrows_scrolled;
16932 w->cursor.y -= first_reusable_row->y - start_row->y;
16935 /* Give up if point isn't in a row displayed or reused. (This
16936 also handles the case where w->cursor.vpos < nrows_scrolled
16937 after the calls to display_line, which can happen with scroll
16938 margins. See bug#1295.) */
16939 if (w->cursor.vpos < 0)
16941 clear_glyph_matrix (w->desired_matrix);
16942 return 0;
16945 /* Scroll the display. */
16946 run.current_y = first_reusable_row->y;
16947 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16948 run.height = it.last_visible_y - run.current_y;
16949 dy = run.current_y - run.desired_y;
16951 if (run.height)
16953 update_begin (f);
16954 FRAME_RIF (f)->update_window_begin_hook (w);
16955 FRAME_RIF (f)->clear_window_mouse_face (w);
16956 FRAME_RIF (f)->scroll_run_hook (w, &run);
16957 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16958 update_end (f);
16961 /* Adjust Y positions of reused rows. */
16962 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16963 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16964 max_y = it.last_visible_y;
16965 for (row = first_reusable_row; row < first_row_to_display; ++row)
16967 row->y -= dy;
16968 row->visible_height = row->height;
16969 if (row->y < min_y)
16970 row->visible_height -= min_y - row->y;
16971 if (row->y + row->height > max_y)
16972 row->visible_height -= row->y + row->height - max_y;
16973 if (row->fringe_bitmap_periodic_p)
16974 row->redraw_fringe_bitmaps_p = 1;
16977 /* Scroll the current matrix. */
16978 eassert (nrows_scrolled > 0);
16979 rotate_matrix (w->current_matrix,
16980 start_vpos,
16981 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16982 -nrows_scrolled);
16984 /* Disable rows not reused. */
16985 for (row -= nrows_scrolled; row < bottom_row; ++row)
16986 row->enabled_p = false;
16988 /* Point may have moved to a different line, so we cannot assume that
16989 the previous cursor position is valid; locate the correct row. */
16990 if (pt_row)
16992 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16993 row < bottom_row
16994 && PT >= MATRIX_ROW_END_CHARPOS (row)
16995 && !row->ends_at_zv_p;
16996 row++)
16998 w->cursor.vpos++;
16999 w->cursor.y = row->y;
17001 if (row < bottom_row)
17003 /* Can't simply scan the row for point with
17004 bidi-reordered glyph rows. Let set_cursor_from_row
17005 figure out where to put the cursor, and if it fails,
17006 give up. */
17007 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17009 if (!set_cursor_from_row (w, row, w->current_matrix,
17010 0, 0, 0, 0))
17012 clear_glyph_matrix (w->desired_matrix);
17013 return 0;
17016 else
17018 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17019 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17021 for (; glyph < end
17022 && (!BUFFERP (glyph->object)
17023 || glyph->charpos < PT);
17024 glyph++)
17026 w->cursor.hpos++;
17027 w->cursor.x += glyph->pixel_width;
17033 /* Adjust window end. A null value of last_text_row means that
17034 the window end is in reused rows which in turn means that
17035 only its vpos can have changed. */
17036 if (last_text_row)
17037 adjust_window_ends (w, last_text_row, 0);
17038 else
17039 w->window_end_vpos -= nrows_scrolled;
17041 w->window_end_valid = 0;
17042 w->desired_matrix->no_scrolling_p = 1;
17044 #ifdef GLYPH_DEBUG
17045 debug_method_add (w, "try_window_reusing_current_matrix 2");
17046 #endif
17047 return 1;
17050 return 0;
17055 /************************************************************************
17056 Window redisplay reusing current matrix when buffer has changed
17057 ************************************************************************/
17059 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17060 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17061 ptrdiff_t *, ptrdiff_t *);
17062 static struct glyph_row *
17063 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17064 struct glyph_row *);
17067 /* Return the last row in MATRIX displaying text. If row START is
17068 non-null, start searching with that row. IT gives the dimensions
17069 of the display. Value is null if matrix is empty; otherwise it is
17070 a pointer to the row found. */
17072 static struct glyph_row *
17073 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17074 struct glyph_row *start)
17076 struct glyph_row *row, *row_found;
17078 /* Set row_found to the last row in IT->w's current matrix
17079 displaying text. The loop looks funny but think of partially
17080 visible lines. */
17081 row_found = NULL;
17082 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17083 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17085 eassert (row->enabled_p);
17086 row_found = row;
17087 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17088 break;
17089 ++row;
17092 return row_found;
17096 /* Return the last row in the current matrix of W that is not affected
17097 by changes at the start of current_buffer that occurred since W's
17098 current matrix was built. Value is null if no such row exists.
17100 BEG_UNCHANGED us the number of characters unchanged at the start of
17101 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17102 first changed character in current_buffer. Characters at positions <
17103 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17104 when the current matrix was built. */
17106 static struct glyph_row *
17107 find_last_unchanged_at_beg_row (struct window *w)
17109 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17110 struct glyph_row *row;
17111 struct glyph_row *row_found = NULL;
17112 int yb = window_text_bottom_y (w);
17114 /* Find the last row displaying unchanged text. */
17115 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17116 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17117 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17118 ++row)
17120 if (/* If row ends before first_changed_pos, it is unchanged,
17121 except in some case. */
17122 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17123 /* When row ends in ZV and we write at ZV it is not
17124 unchanged. */
17125 && !row->ends_at_zv_p
17126 /* When first_changed_pos is the end of a continued line,
17127 row is not unchanged because it may be no longer
17128 continued. */
17129 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17130 && (row->continued_p
17131 || row->exact_window_width_line_p))
17132 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17133 needs to be recomputed, so don't consider this row as
17134 unchanged. This happens when the last line was
17135 bidi-reordered and was killed immediately before this
17136 redisplay cycle. In that case, ROW->end stores the
17137 buffer position of the first visual-order character of
17138 the killed text, which is now beyond ZV. */
17139 && CHARPOS (row->end.pos) <= ZV)
17140 row_found = row;
17142 /* Stop if last visible row. */
17143 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17144 break;
17147 return row_found;
17151 /* Find the first glyph row in the current matrix of W that is not
17152 affected by changes at the end of current_buffer since the
17153 time W's current matrix was built.
17155 Return in *DELTA the number of chars by which buffer positions in
17156 unchanged text at the end of current_buffer must be adjusted.
17158 Return in *DELTA_BYTES the corresponding number of bytes.
17160 Value is null if no such row exists, i.e. all rows are affected by
17161 changes. */
17163 static struct glyph_row *
17164 find_first_unchanged_at_end_row (struct window *w,
17165 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17167 struct glyph_row *row;
17168 struct glyph_row *row_found = NULL;
17170 *delta = *delta_bytes = 0;
17172 /* Display must not have been paused, otherwise the current matrix
17173 is not up to date. */
17174 eassert (w->window_end_valid);
17176 /* A value of window_end_pos >= END_UNCHANGED means that the window
17177 end is in the range of changed text. If so, there is no
17178 unchanged row at the end of W's current matrix. */
17179 if (w->window_end_pos >= END_UNCHANGED)
17180 return NULL;
17182 /* Set row to the last row in W's current matrix displaying text. */
17183 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17185 /* If matrix is entirely empty, no unchanged row exists. */
17186 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17188 /* The value of row is the last glyph row in the matrix having a
17189 meaningful buffer position in it. The end position of row
17190 corresponds to window_end_pos. This allows us to translate
17191 buffer positions in the current matrix to current buffer
17192 positions for characters not in changed text. */
17193 ptrdiff_t Z_old =
17194 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17195 ptrdiff_t Z_BYTE_old =
17196 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17197 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17198 struct glyph_row *first_text_row
17199 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17201 *delta = Z - Z_old;
17202 *delta_bytes = Z_BYTE - Z_BYTE_old;
17204 /* Set last_unchanged_pos to the buffer position of the last
17205 character in the buffer that has not been changed. Z is the
17206 index + 1 of the last character in current_buffer, i.e. by
17207 subtracting END_UNCHANGED we get the index of the last
17208 unchanged character, and we have to add BEG to get its buffer
17209 position. */
17210 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17211 last_unchanged_pos_old = last_unchanged_pos - *delta;
17213 /* Search backward from ROW for a row displaying a line that
17214 starts at a minimum position >= last_unchanged_pos_old. */
17215 for (; row > first_text_row; --row)
17217 /* This used to abort, but it can happen.
17218 It is ok to just stop the search instead here. KFS. */
17219 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17220 break;
17222 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17223 row_found = row;
17227 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17229 return row_found;
17233 /* Make sure that glyph rows in the current matrix of window W
17234 reference the same glyph memory as corresponding rows in the
17235 frame's frame matrix. This function is called after scrolling W's
17236 current matrix on a terminal frame in try_window_id and
17237 try_window_reusing_current_matrix. */
17239 static void
17240 sync_frame_with_window_matrix_rows (struct window *w)
17242 struct frame *f = XFRAME (w->frame);
17243 struct glyph_row *window_row, *window_row_end, *frame_row;
17245 /* Preconditions: W must be a leaf window and full-width. Its frame
17246 must have a frame matrix. */
17247 eassert (BUFFERP (w->contents));
17248 eassert (WINDOW_FULL_WIDTH_P (w));
17249 eassert (!FRAME_WINDOW_P (f));
17251 /* If W is a full-width window, glyph pointers in W's current matrix
17252 have, by definition, to be the same as glyph pointers in the
17253 corresponding frame matrix. Note that frame matrices have no
17254 marginal areas (see build_frame_matrix). */
17255 window_row = w->current_matrix->rows;
17256 window_row_end = window_row + w->current_matrix->nrows;
17257 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17258 while (window_row < window_row_end)
17260 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17261 struct glyph *end = window_row->glyphs[LAST_AREA];
17263 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17264 frame_row->glyphs[TEXT_AREA] = start;
17265 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17266 frame_row->glyphs[LAST_AREA] = end;
17268 /* Disable frame rows whose corresponding window rows have
17269 been disabled in try_window_id. */
17270 if (!window_row->enabled_p)
17271 frame_row->enabled_p = false;
17273 ++window_row, ++frame_row;
17278 /* Find the glyph row in window W containing CHARPOS. Consider all
17279 rows between START and END (not inclusive). END null means search
17280 all rows to the end of the display area of W. Value is the row
17281 containing CHARPOS or null. */
17283 struct glyph_row *
17284 row_containing_pos (struct window *w, ptrdiff_t charpos,
17285 struct glyph_row *start, struct glyph_row *end, int dy)
17287 struct glyph_row *row = start;
17288 struct glyph_row *best_row = NULL;
17289 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17290 int last_y;
17292 /* If we happen to start on a header-line, skip that. */
17293 if (row->mode_line_p)
17294 ++row;
17296 if ((end && row >= end) || !row->enabled_p)
17297 return NULL;
17299 last_y = window_text_bottom_y (w) - dy;
17301 while (1)
17303 /* Give up if we have gone too far. */
17304 if (end && row >= end)
17305 return NULL;
17306 /* This formerly returned if they were equal.
17307 I think that both quantities are of a "last plus one" type;
17308 if so, when they are equal, the row is within the screen. -- rms. */
17309 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17310 return NULL;
17312 /* If it is in this row, return this row. */
17313 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17314 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17315 /* The end position of a row equals the start
17316 position of the next row. If CHARPOS is there, we
17317 would rather consider it displayed in the next
17318 line, except when this line ends in ZV. */
17319 && !row_for_charpos_p (row, charpos)))
17320 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17322 struct glyph *g;
17324 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17325 || (!best_row && !row->continued_p))
17326 return row;
17327 /* In bidi-reordered rows, there could be several rows whose
17328 edges surround CHARPOS, all of these rows belonging to
17329 the same continued line. We need to find the row which
17330 fits CHARPOS the best. */
17331 for (g = row->glyphs[TEXT_AREA];
17332 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17333 g++)
17335 if (!STRINGP (g->object))
17337 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17339 mindif = eabs (g->charpos - charpos);
17340 best_row = row;
17341 /* Exact match always wins. */
17342 if (mindif == 0)
17343 return best_row;
17348 else if (best_row && !row->continued_p)
17349 return best_row;
17350 ++row;
17355 /* Try to redisplay window W by reusing its existing display. W's
17356 current matrix must be up to date when this function is called,
17357 i.e. window_end_valid must be nonzero.
17359 Value is
17361 >= 1 if successful, i.e. display has been updated
17362 specifically:
17363 1 means the changes were in front of a newline that precedes
17364 the window start, and the whole current matrix was reused
17365 2 means the changes were after the last position displayed
17366 in the window, and the whole current matrix was reused
17367 3 means portions of the current matrix were reused, while
17368 some of the screen lines were redrawn
17369 -1 if redisplay with same window start is known not to succeed
17370 0 if otherwise unsuccessful
17372 The following steps are performed:
17374 1. Find the last row in the current matrix of W that is not
17375 affected by changes at the start of current_buffer. If no such row
17376 is found, give up.
17378 2. Find the first row in W's current matrix that is not affected by
17379 changes at the end of current_buffer. Maybe there is no such row.
17381 3. Display lines beginning with the row + 1 found in step 1 to the
17382 row found in step 2 or, if step 2 didn't find a row, to the end of
17383 the window.
17385 4. If cursor is not known to appear on the window, give up.
17387 5. If display stopped at the row found in step 2, scroll the
17388 display and current matrix as needed.
17390 6. Maybe display some lines at the end of W, if we must. This can
17391 happen under various circumstances, like a partially visible line
17392 becoming fully visible, or because newly displayed lines are displayed
17393 in smaller font sizes.
17395 7. Update W's window end information. */
17397 static int
17398 try_window_id (struct window *w)
17400 struct frame *f = XFRAME (w->frame);
17401 struct glyph_matrix *current_matrix = w->current_matrix;
17402 struct glyph_matrix *desired_matrix = w->desired_matrix;
17403 struct glyph_row *last_unchanged_at_beg_row;
17404 struct glyph_row *first_unchanged_at_end_row;
17405 struct glyph_row *row;
17406 struct glyph_row *bottom_row;
17407 int bottom_vpos;
17408 struct it it;
17409 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17410 int dvpos, dy;
17411 struct text_pos start_pos;
17412 struct run run;
17413 int first_unchanged_at_end_vpos = 0;
17414 struct glyph_row *last_text_row, *last_text_row_at_end;
17415 struct text_pos start;
17416 ptrdiff_t first_changed_charpos, last_changed_charpos;
17418 #ifdef GLYPH_DEBUG
17419 if (inhibit_try_window_id)
17420 return 0;
17421 #endif
17423 /* This is handy for debugging. */
17424 #if 0
17425 #define GIVE_UP(X) \
17426 do { \
17427 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17428 return 0; \
17429 } while (0)
17430 #else
17431 #define GIVE_UP(X) return 0
17432 #endif
17434 SET_TEXT_POS_FROM_MARKER (start, w->start);
17436 /* Don't use this for mini-windows because these can show
17437 messages and mini-buffers, and we don't handle that here. */
17438 if (MINI_WINDOW_P (w))
17439 GIVE_UP (1);
17441 /* This flag is used to prevent redisplay optimizations. */
17442 if (windows_or_buffers_changed || f->cursor_type_changed)
17443 GIVE_UP (2);
17445 /* This function's optimizations cannot be used if overlays have
17446 changed in the buffer displayed by the window, so give up if they
17447 have. */
17448 if (w->last_overlay_modified != OVERLAY_MODIFF)
17449 GIVE_UP (21);
17451 /* Verify that narrowing has not changed.
17452 Also verify that we were not told to prevent redisplay optimizations.
17453 It would be nice to further
17454 reduce the number of cases where this prevents try_window_id. */
17455 if (current_buffer->clip_changed
17456 || current_buffer->prevent_redisplay_optimizations_p)
17457 GIVE_UP (3);
17459 /* Window must either use window-based redisplay or be full width. */
17460 if (!FRAME_WINDOW_P (f)
17461 && (!FRAME_LINE_INS_DEL_OK (f)
17462 || !WINDOW_FULL_WIDTH_P (w)))
17463 GIVE_UP (4);
17465 /* Give up if point is known NOT to appear in W. */
17466 if (PT < CHARPOS (start))
17467 GIVE_UP (5);
17469 /* Another way to prevent redisplay optimizations. */
17470 if (w->last_modified == 0)
17471 GIVE_UP (6);
17473 /* Verify that window is not hscrolled. */
17474 if (w->hscroll != 0)
17475 GIVE_UP (7);
17477 /* Verify that display wasn't paused. */
17478 if (!w->window_end_valid)
17479 GIVE_UP (8);
17481 /* Likewise if highlighting trailing whitespace. */
17482 if (!NILP (Vshow_trailing_whitespace))
17483 GIVE_UP (11);
17485 /* Can't use this if overlay arrow position and/or string have
17486 changed. */
17487 if (overlay_arrows_changed_p ())
17488 GIVE_UP (12);
17490 /* When word-wrap is on, adding a space to the first word of a
17491 wrapped line can change the wrap position, altering the line
17492 above it. It might be worthwhile to handle this more
17493 intelligently, but for now just redisplay from scratch. */
17494 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17495 GIVE_UP (21);
17497 /* Under bidi reordering, adding or deleting a character in the
17498 beginning of a paragraph, before the first strong directional
17499 character, can change the base direction of the paragraph (unless
17500 the buffer specifies a fixed paragraph direction), which will
17501 require to redisplay the whole paragraph. It might be worthwhile
17502 to find the paragraph limits and widen the range of redisplayed
17503 lines to that, but for now just give up this optimization and
17504 redisplay from scratch. */
17505 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17506 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17507 GIVE_UP (22);
17509 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17510 only if buffer has really changed. The reason is that the gap is
17511 initially at Z for freshly visited files. The code below would
17512 set end_unchanged to 0 in that case. */
17513 if (MODIFF > SAVE_MODIFF
17514 /* This seems to happen sometimes after saving a buffer. */
17515 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17517 if (GPT - BEG < BEG_UNCHANGED)
17518 BEG_UNCHANGED = GPT - BEG;
17519 if (Z - GPT < END_UNCHANGED)
17520 END_UNCHANGED = Z - GPT;
17523 /* The position of the first and last character that has been changed. */
17524 first_changed_charpos = BEG + BEG_UNCHANGED;
17525 last_changed_charpos = Z - END_UNCHANGED;
17527 /* If window starts after a line end, and the last change is in
17528 front of that newline, then changes don't affect the display.
17529 This case happens with stealth-fontification. Note that although
17530 the display is unchanged, glyph positions in the matrix have to
17531 be adjusted, of course. */
17532 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17533 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17534 && ((last_changed_charpos < CHARPOS (start)
17535 && CHARPOS (start) == BEGV)
17536 || (last_changed_charpos < CHARPOS (start) - 1
17537 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17539 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17540 struct glyph_row *r0;
17542 /* Compute how many chars/bytes have been added to or removed
17543 from the buffer. */
17544 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17545 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17546 Z_delta = Z - Z_old;
17547 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17549 /* Give up if PT is not in the window. Note that it already has
17550 been checked at the start of try_window_id that PT is not in
17551 front of the window start. */
17552 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17553 GIVE_UP (13);
17555 /* If window start is unchanged, we can reuse the whole matrix
17556 as is, after adjusting glyph positions. No need to compute
17557 the window end again, since its offset from Z hasn't changed. */
17558 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17559 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17560 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17561 /* PT must not be in a partially visible line. */
17562 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17563 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17565 /* Adjust positions in the glyph matrix. */
17566 if (Z_delta || Z_delta_bytes)
17568 struct glyph_row *r1
17569 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17570 increment_matrix_positions (w->current_matrix,
17571 MATRIX_ROW_VPOS (r0, current_matrix),
17572 MATRIX_ROW_VPOS (r1, current_matrix),
17573 Z_delta, Z_delta_bytes);
17576 /* Set the cursor. */
17577 row = row_containing_pos (w, PT, r0, NULL, 0);
17578 if (row)
17579 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17580 return 1;
17584 /* Handle the case that changes are all below what is displayed in
17585 the window, and that PT is in the window. This shortcut cannot
17586 be taken if ZV is visible in the window, and text has been added
17587 there that is visible in the window. */
17588 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17589 /* ZV is not visible in the window, or there are no
17590 changes at ZV, actually. */
17591 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17592 || first_changed_charpos == last_changed_charpos))
17594 struct glyph_row *r0;
17596 /* Give up if PT is not in the window. Note that it already has
17597 been checked at the start of try_window_id that PT is not in
17598 front of the window start. */
17599 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17600 GIVE_UP (14);
17602 /* If window start is unchanged, we can reuse the whole matrix
17603 as is, without changing glyph positions since no text has
17604 been added/removed in front of the window end. */
17605 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17606 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17607 /* PT must not be in a partially visible line. */
17608 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17609 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17611 /* We have to compute the window end anew since text
17612 could have been added/removed after it. */
17613 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17614 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17616 /* Set the cursor. */
17617 row = row_containing_pos (w, PT, r0, NULL, 0);
17618 if (row)
17619 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17620 return 2;
17624 /* Give up if window start is in the changed area.
17626 The condition used to read
17628 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17630 but why that was tested escapes me at the moment. */
17631 if (CHARPOS (start) >= first_changed_charpos
17632 && CHARPOS (start) <= last_changed_charpos)
17633 GIVE_UP (15);
17635 /* Check that window start agrees with the start of the first glyph
17636 row in its current matrix. Check this after we know the window
17637 start is not in changed text, otherwise positions would not be
17638 comparable. */
17639 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17640 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17641 GIVE_UP (16);
17643 /* Give up if the window ends in strings. Overlay strings
17644 at the end are difficult to handle, so don't try. */
17645 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17646 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17647 GIVE_UP (20);
17649 /* Compute the position at which we have to start displaying new
17650 lines. Some of the lines at the top of the window might be
17651 reusable because they are not displaying changed text. Find the
17652 last row in W's current matrix not affected by changes at the
17653 start of current_buffer. Value is null if changes start in the
17654 first line of window. */
17655 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17656 if (last_unchanged_at_beg_row)
17658 /* Avoid starting to display in the middle of a character, a TAB
17659 for instance. This is easier than to set up the iterator
17660 exactly, and it's not a frequent case, so the additional
17661 effort wouldn't really pay off. */
17662 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17663 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17664 && last_unchanged_at_beg_row > w->current_matrix->rows)
17665 --last_unchanged_at_beg_row;
17667 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17668 GIVE_UP (17);
17670 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17671 GIVE_UP (18);
17672 start_pos = it.current.pos;
17674 /* Start displaying new lines in the desired matrix at the same
17675 vpos we would use in the current matrix, i.e. below
17676 last_unchanged_at_beg_row. */
17677 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17678 current_matrix);
17679 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17680 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17682 eassert (it.hpos == 0 && it.current_x == 0);
17684 else
17686 /* There are no reusable lines at the start of the window.
17687 Start displaying in the first text line. */
17688 start_display (&it, w, start);
17689 it.vpos = it.first_vpos;
17690 start_pos = it.current.pos;
17693 /* Find the first row that is not affected by changes at the end of
17694 the buffer. Value will be null if there is no unchanged row, in
17695 which case we must redisplay to the end of the window. delta
17696 will be set to the value by which buffer positions beginning with
17697 first_unchanged_at_end_row have to be adjusted due to text
17698 changes. */
17699 first_unchanged_at_end_row
17700 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17701 IF_DEBUG (debug_delta = delta);
17702 IF_DEBUG (debug_delta_bytes = delta_bytes);
17704 /* Set stop_pos to the buffer position up to which we will have to
17705 display new lines. If first_unchanged_at_end_row != NULL, this
17706 is the buffer position of the start of the line displayed in that
17707 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17708 that we don't stop at a buffer position. */
17709 stop_pos = 0;
17710 if (first_unchanged_at_end_row)
17712 eassert (last_unchanged_at_beg_row == NULL
17713 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17715 /* If this is a continuation line, move forward to the next one
17716 that isn't. Changes in lines above affect this line.
17717 Caution: this may move first_unchanged_at_end_row to a row
17718 not displaying text. */
17719 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17720 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17721 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17722 < it.last_visible_y))
17723 ++first_unchanged_at_end_row;
17725 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17726 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17727 >= it.last_visible_y))
17728 first_unchanged_at_end_row = NULL;
17729 else
17731 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17732 + delta);
17733 first_unchanged_at_end_vpos
17734 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17735 eassert (stop_pos >= Z - END_UNCHANGED);
17738 else if (last_unchanged_at_beg_row == NULL)
17739 GIVE_UP (19);
17742 #ifdef GLYPH_DEBUG
17744 /* Either there is no unchanged row at the end, or the one we have
17745 now displays text. This is a necessary condition for the window
17746 end pos calculation at the end of this function. */
17747 eassert (first_unchanged_at_end_row == NULL
17748 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17750 debug_last_unchanged_at_beg_vpos
17751 = (last_unchanged_at_beg_row
17752 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17753 : -1);
17754 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17756 #endif /* GLYPH_DEBUG */
17759 /* Display new lines. Set last_text_row to the last new line
17760 displayed which has text on it, i.e. might end up as being the
17761 line where the window_end_vpos is. */
17762 w->cursor.vpos = -1;
17763 last_text_row = NULL;
17764 overlay_arrow_seen = 0;
17765 while (it.current_y < it.last_visible_y
17766 && !f->fonts_changed
17767 && (first_unchanged_at_end_row == NULL
17768 || IT_CHARPOS (it) < stop_pos))
17770 if (display_line (&it))
17771 last_text_row = it.glyph_row - 1;
17774 if (f->fonts_changed)
17775 return -1;
17778 /* Compute differences in buffer positions, y-positions etc. for
17779 lines reused at the bottom of the window. Compute what we can
17780 scroll. */
17781 if (first_unchanged_at_end_row
17782 /* No lines reused because we displayed everything up to the
17783 bottom of the window. */
17784 && it.current_y < it.last_visible_y)
17786 dvpos = (it.vpos
17787 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17788 current_matrix));
17789 dy = it.current_y - first_unchanged_at_end_row->y;
17790 run.current_y = first_unchanged_at_end_row->y;
17791 run.desired_y = run.current_y + dy;
17792 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17794 else
17796 delta = delta_bytes = dvpos = dy
17797 = run.current_y = run.desired_y = run.height = 0;
17798 first_unchanged_at_end_row = NULL;
17800 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17803 /* Find the cursor if not already found. We have to decide whether
17804 PT will appear on this window (it sometimes doesn't, but this is
17805 not a very frequent case.) This decision has to be made before
17806 the current matrix is altered. A value of cursor.vpos < 0 means
17807 that PT is either in one of the lines beginning at
17808 first_unchanged_at_end_row or below the window. Don't care for
17809 lines that might be displayed later at the window end; as
17810 mentioned, this is not a frequent case. */
17811 if (w->cursor.vpos < 0)
17813 /* Cursor in unchanged rows at the top? */
17814 if (PT < CHARPOS (start_pos)
17815 && last_unchanged_at_beg_row)
17817 row = row_containing_pos (w, PT,
17818 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17819 last_unchanged_at_beg_row + 1, 0);
17820 if (row)
17821 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17824 /* Start from first_unchanged_at_end_row looking for PT. */
17825 else if (first_unchanged_at_end_row)
17827 row = row_containing_pos (w, PT - delta,
17828 first_unchanged_at_end_row, NULL, 0);
17829 if (row)
17830 set_cursor_from_row (w, row, w->current_matrix, delta,
17831 delta_bytes, dy, dvpos);
17834 /* Give up if cursor was not found. */
17835 if (w->cursor.vpos < 0)
17837 clear_glyph_matrix (w->desired_matrix);
17838 return -1;
17842 /* Don't let the cursor end in the scroll margins. */
17844 int this_scroll_margin, cursor_height;
17845 int frame_line_height = default_line_pixel_height (w);
17846 int window_total_lines
17847 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17849 this_scroll_margin =
17850 max (0, min (scroll_margin, window_total_lines / 4));
17851 this_scroll_margin *= frame_line_height;
17852 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17854 if ((w->cursor.y < this_scroll_margin
17855 && CHARPOS (start) > BEGV)
17856 /* Old redisplay didn't take scroll margin into account at the bottom,
17857 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17858 || (w->cursor.y + (make_cursor_line_fully_visible_p
17859 ? cursor_height + this_scroll_margin
17860 : 1)) > it.last_visible_y)
17862 w->cursor.vpos = -1;
17863 clear_glyph_matrix (w->desired_matrix);
17864 return -1;
17868 /* Scroll the display. Do it before changing the current matrix so
17869 that xterm.c doesn't get confused about where the cursor glyph is
17870 found. */
17871 if (dy && run.height)
17873 update_begin (f);
17875 if (FRAME_WINDOW_P (f))
17877 FRAME_RIF (f)->update_window_begin_hook (w);
17878 FRAME_RIF (f)->clear_window_mouse_face (w);
17879 FRAME_RIF (f)->scroll_run_hook (w, &run);
17880 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17882 else
17884 /* Terminal frame. In this case, dvpos gives the number of
17885 lines to scroll by; dvpos < 0 means scroll up. */
17886 int from_vpos
17887 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17888 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17889 int end = (WINDOW_TOP_EDGE_LINE (w)
17890 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17891 + window_internal_height (w));
17893 #if defined (HAVE_GPM) || defined (MSDOS)
17894 x_clear_window_mouse_face (w);
17895 #endif
17896 /* Perform the operation on the screen. */
17897 if (dvpos > 0)
17899 /* Scroll last_unchanged_at_beg_row to the end of the
17900 window down dvpos lines. */
17901 set_terminal_window (f, end);
17903 /* On dumb terminals delete dvpos lines at the end
17904 before inserting dvpos empty lines. */
17905 if (!FRAME_SCROLL_REGION_OK (f))
17906 ins_del_lines (f, end - dvpos, -dvpos);
17908 /* Insert dvpos empty lines in front of
17909 last_unchanged_at_beg_row. */
17910 ins_del_lines (f, from, dvpos);
17912 else if (dvpos < 0)
17914 /* Scroll up last_unchanged_at_beg_vpos to the end of
17915 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17916 set_terminal_window (f, end);
17918 /* Delete dvpos lines in front of
17919 last_unchanged_at_beg_vpos. ins_del_lines will set
17920 the cursor to the given vpos and emit |dvpos| delete
17921 line sequences. */
17922 ins_del_lines (f, from + dvpos, dvpos);
17924 /* On a dumb terminal insert dvpos empty lines at the
17925 end. */
17926 if (!FRAME_SCROLL_REGION_OK (f))
17927 ins_del_lines (f, end + dvpos, -dvpos);
17930 set_terminal_window (f, 0);
17933 update_end (f);
17936 /* Shift reused rows of the current matrix to the right position.
17937 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17938 text. */
17939 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17940 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17941 if (dvpos < 0)
17943 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17944 bottom_vpos, dvpos);
17945 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17946 bottom_vpos);
17948 else if (dvpos > 0)
17950 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17951 bottom_vpos, dvpos);
17952 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17953 first_unchanged_at_end_vpos + dvpos);
17956 /* For frame-based redisplay, make sure that current frame and window
17957 matrix are in sync with respect to glyph memory. */
17958 if (!FRAME_WINDOW_P (f))
17959 sync_frame_with_window_matrix_rows (w);
17961 /* Adjust buffer positions in reused rows. */
17962 if (delta || delta_bytes)
17963 increment_matrix_positions (current_matrix,
17964 first_unchanged_at_end_vpos + dvpos,
17965 bottom_vpos, delta, delta_bytes);
17967 /* Adjust Y positions. */
17968 if (dy)
17969 shift_glyph_matrix (w, current_matrix,
17970 first_unchanged_at_end_vpos + dvpos,
17971 bottom_vpos, dy);
17973 if (first_unchanged_at_end_row)
17975 first_unchanged_at_end_row += dvpos;
17976 if (first_unchanged_at_end_row->y >= it.last_visible_y
17977 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17978 first_unchanged_at_end_row = NULL;
17981 /* If scrolling up, there may be some lines to display at the end of
17982 the window. */
17983 last_text_row_at_end = NULL;
17984 if (dy < 0)
17986 /* Scrolling up can leave for example a partially visible line
17987 at the end of the window to be redisplayed. */
17988 /* Set last_row to the glyph row in the current matrix where the
17989 window end line is found. It has been moved up or down in
17990 the matrix by dvpos. */
17991 int last_vpos = w->window_end_vpos + dvpos;
17992 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17994 /* If last_row is the window end line, it should display text. */
17995 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17997 /* If window end line was partially visible before, begin
17998 displaying at that line. Otherwise begin displaying with the
17999 line following it. */
18000 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18002 init_to_row_start (&it, w, last_row);
18003 it.vpos = last_vpos;
18004 it.current_y = last_row->y;
18006 else
18008 init_to_row_end (&it, w, last_row);
18009 it.vpos = 1 + last_vpos;
18010 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18011 ++last_row;
18014 /* We may start in a continuation line. If so, we have to
18015 get the right continuation_lines_width and current_x. */
18016 it.continuation_lines_width = last_row->continuation_lines_width;
18017 it.hpos = it.current_x = 0;
18019 /* Display the rest of the lines at the window end. */
18020 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18021 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18023 /* Is it always sure that the display agrees with lines in
18024 the current matrix? I don't think so, so we mark rows
18025 displayed invalid in the current matrix by setting their
18026 enabled_p flag to zero. */
18027 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18028 if (display_line (&it))
18029 last_text_row_at_end = it.glyph_row - 1;
18033 /* Update window_end_pos and window_end_vpos. */
18034 if (first_unchanged_at_end_row && !last_text_row_at_end)
18036 /* Window end line if one of the preserved rows from the current
18037 matrix. Set row to the last row displaying text in current
18038 matrix starting at first_unchanged_at_end_row, after
18039 scrolling. */
18040 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18041 row = find_last_row_displaying_text (w->current_matrix, &it,
18042 first_unchanged_at_end_row);
18043 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18044 adjust_window_ends (w, row, 1);
18045 eassert (w->window_end_bytepos >= 0);
18046 IF_DEBUG (debug_method_add (w, "A"));
18048 else if (last_text_row_at_end)
18050 adjust_window_ends (w, last_text_row_at_end, 0);
18051 eassert (w->window_end_bytepos >= 0);
18052 IF_DEBUG (debug_method_add (w, "B"));
18054 else if (last_text_row)
18056 /* We have displayed either to the end of the window or at the
18057 end of the window, i.e. the last row with text is to be found
18058 in the desired matrix. */
18059 adjust_window_ends (w, last_text_row, 0);
18060 eassert (w->window_end_bytepos >= 0);
18062 else if (first_unchanged_at_end_row == NULL
18063 && last_text_row == NULL
18064 && last_text_row_at_end == NULL)
18066 /* Displayed to end of window, but no line containing text was
18067 displayed. Lines were deleted at the end of the window. */
18068 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18069 int vpos = w->window_end_vpos;
18070 struct glyph_row *current_row = current_matrix->rows + vpos;
18071 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18073 for (row = NULL;
18074 row == NULL && vpos >= first_vpos;
18075 --vpos, --current_row, --desired_row)
18077 if (desired_row->enabled_p)
18079 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18080 row = desired_row;
18082 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18083 row = current_row;
18086 eassert (row != NULL);
18087 w->window_end_vpos = vpos + 1;
18088 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18089 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18090 eassert (w->window_end_bytepos >= 0);
18091 IF_DEBUG (debug_method_add (w, "C"));
18093 else
18094 emacs_abort ();
18096 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18097 debug_end_vpos = w->window_end_vpos));
18099 /* Record that display has not been completed. */
18100 w->window_end_valid = 0;
18101 w->desired_matrix->no_scrolling_p = 1;
18102 return 3;
18104 #undef GIVE_UP
18109 /***********************************************************************
18110 More debugging support
18111 ***********************************************************************/
18113 #ifdef GLYPH_DEBUG
18115 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18116 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18117 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18120 /* Dump the contents of glyph matrix MATRIX on stderr.
18122 GLYPHS 0 means don't show glyph contents.
18123 GLYPHS 1 means show glyphs in short form
18124 GLYPHS > 1 means show glyphs in long form. */
18126 void
18127 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18129 int i;
18130 for (i = 0; i < matrix->nrows; ++i)
18131 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18135 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18136 the glyph row and area where the glyph comes from. */
18138 void
18139 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18141 if (glyph->type == CHAR_GLYPH
18142 || glyph->type == GLYPHLESS_GLYPH)
18144 fprintf (stderr,
18145 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18146 glyph - row->glyphs[TEXT_AREA],
18147 (glyph->type == CHAR_GLYPH
18148 ? 'C'
18149 : 'G'),
18150 glyph->charpos,
18151 (BUFFERP (glyph->object)
18152 ? 'B'
18153 : (STRINGP (glyph->object)
18154 ? 'S'
18155 : (INTEGERP (glyph->object)
18156 ? '0'
18157 : '-'))),
18158 glyph->pixel_width,
18159 glyph->u.ch,
18160 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18161 ? glyph->u.ch
18162 : '.'),
18163 glyph->face_id,
18164 glyph->left_box_line_p,
18165 glyph->right_box_line_p);
18167 else if (glyph->type == STRETCH_GLYPH)
18169 fprintf (stderr,
18170 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18171 glyph - row->glyphs[TEXT_AREA],
18172 'S',
18173 glyph->charpos,
18174 (BUFFERP (glyph->object)
18175 ? 'B'
18176 : (STRINGP (glyph->object)
18177 ? 'S'
18178 : (INTEGERP (glyph->object)
18179 ? '0'
18180 : '-'))),
18181 glyph->pixel_width,
18183 ' ',
18184 glyph->face_id,
18185 glyph->left_box_line_p,
18186 glyph->right_box_line_p);
18188 else if (glyph->type == IMAGE_GLYPH)
18190 fprintf (stderr,
18191 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18192 glyph - row->glyphs[TEXT_AREA],
18193 'I',
18194 glyph->charpos,
18195 (BUFFERP (glyph->object)
18196 ? 'B'
18197 : (STRINGP (glyph->object)
18198 ? 'S'
18199 : (INTEGERP (glyph->object)
18200 ? '0'
18201 : '-'))),
18202 glyph->pixel_width,
18203 glyph->u.img_id,
18204 '.',
18205 glyph->face_id,
18206 glyph->left_box_line_p,
18207 glyph->right_box_line_p);
18209 else if (glyph->type == COMPOSITE_GLYPH)
18211 fprintf (stderr,
18212 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18213 glyph - row->glyphs[TEXT_AREA],
18214 '+',
18215 glyph->charpos,
18216 (BUFFERP (glyph->object)
18217 ? 'B'
18218 : (STRINGP (glyph->object)
18219 ? 'S'
18220 : (INTEGERP (glyph->object)
18221 ? '0'
18222 : '-'))),
18223 glyph->pixel_width,
18224 glyph->u.cmp.id);
18225 if (glyph->u.cmp.automatic)
18226 fprintf (stderr,
18227 "[%d-%d]",
18228 glyph->slice.cmp.from, glyph->slice.cmp.to);
18229 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18230 glyph->face_id,
18231 glyph->left_box_line_p,
18232 glyph->right_box_line_p);
18237 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18238 GLYPHS 0 means don't show glyph contents.
18239 GLYPHS 1 means show glyphs in short form
18240 GLYPHS > 1 means show glyphs in long form. */
18242 void
18243 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18245 if (glyphs != 1)
18247 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18248 fprintf (stderr, "==============================================================================\n");
18250 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18251 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18252 vpos,
18253 MATRIX_ROW_START_CHARPOS (row),
18254 MATRIX_ROW_END_CHARPOS (row),
18255 row->used[TEXT_AREA],
18256 row->contains_overlapping_glyphs_p,
18257 row->enabled_p,
18258 row->truncated_on_left_p,
18259 row->truncated_on_right_p,
18260 row->continued_p,
18261 MATRIX_ROW_CONTINUATION_LINE_P (row),
18262 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18263 row->ends_at_zv_p,
18264 row->fill_line_p,
18265 row->ends_in_middle_of_char_p,
18266 row->starts_in_middle_of_char_p,
18267 row->mouse_face_p,
18268 row->x,
18269 row->y,
18270 row->pixel_width,
18271 row->height,
18272 row->visible_height,
18273 row->ascent,
18274 row->phys_ascent);
18275 /* The next 3 lines should align to "Start" in the header. */
18276 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18277 row->end.overlay_string_index,
18278 row->continuation_lines_width);
18279 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18280 CHARPOS (row->start.string_pos),
18281 CHARPOS (row->end.string_pos));
18282 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18283 row->end.dpvec_index);
18286 if (glyphs > 1)
18288 int area;
18290 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18292 struct glyph *glyph = row->glyphs[area];
18293 struct glyph *glyph_end = glyph + row->used[area];
18295 /* Glyph for a line end in text. */
18296 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18297 ++glyph_end;
18299 if (glyph < glyph_end)
18300 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18302 for (; glyph < glyph_end; ++glyph)
18303 dump_glyph (row, glyph, area);
18306 else if (glyphs == 1)
18308 int area;
18310 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18312 char *s = alloca (row->used[area] + 4);
18313 int i;
18315 for (i = 0; i < row->used[area]; ++i)
18317 struct glyph *glyph = row->glyphs[area] + i;
18318 if (i == row->used[area] - 1
18319 && area == TEXT_AREA
18320 && INTEGERP (glyph->object)
18321 && glyph->type == CHAR_GLYPH
18322 && glyph->u.ch == ' ')
18324 strcpy (&s[i], "[\\n]");
18325 i += 4;
18327 else if (glyph->type == CHAR_GLYPH
18328 && glyph->u.ch < 0x80
18329 && glyph->u.ch >= ' ')
18330 s[i] = glyph->u.ch;
18331 else
18332 s[i] = '.';
18335 s[i] = '\0';
18336 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18342 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18343 Sdump_glyph_matrix, 0, 1, "p",
18344 doc: /* Dump the current matrix of the selected window to stderr.
18345 Shows contents of glyph row structures. With non-nil
18346 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18347 glyphs in short form, otherwise show glyphs in long form. */)
18348 (Lisp_Object glyphs)
18350 struct window *w = XWINDOW (selected_window);
18351 struct buffer *buffer = XBUFFER (w->contents);
18353 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18354 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18355 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18356 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18357 fprintf (stderr, "=============================================\n");
18358 dump_glyph_matrix (w->current_matrix,
18359 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18360 return Qnil;
18364 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18365 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18366 (void)
18368 struct frame *f = XFRAME (selected_frame);
18369 dump_glyph_matrix (f->current_matrix, 1);
18370 return Qnil;
18374 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18375 doc: /* Dump glyph row ROW to stderr.
18376 GLYPH 0 means don't dump glyphs.
18377 GLYPH 1 means dump glyphs in short form.
18378 GLYPH > 1 or omitted means dump glyphs in long form. */)
18379 (Lisp_Object row, Lisp_Object glyphs)
18381 struct glyph_matrix *matrix;
18382 EMACS_INT vpos;
18384 CHECK_NUMBER (row);
18385 matrix = XWINDOW (selected_window)->current_matrix;
18386 vpos = XINT (row);
18387 if (vpos >= 0 && vpos < matrix->nrows)
18388 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18389 vpos,
18390 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18391 return Qnil;
18395 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18396 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18397 GLYPH 0 means don't dump glyphs.
18398 GLYPH 1 means dump glyphs in short form.
18399 GLYPH > 1 or omitted means dump glyphs in long form.
18401 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18402 do nothing. */)
18403 (Lisp_Object row, Lisp_Object glyphs)
18405 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18406 struct frame *sf = SELECTED_FRAME ();
18407 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18408 EMACS_INT vpos;
18410 CHECK_NUMBER (row);
18411 vpos = XINT (row);
18412 if (vpos >= 0 && vpos < m->nrows)
18413 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18414 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18415 #endif
18416 return Qnil;
18420 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18421 doc: /* Toggle tracing of redisplay.
18422 With ARG, turn tracing on if and only if ARG is positive. */)
18423 (Lisp_Object arg)
18425 if (NILP (arg))
18426 trace_redisplay_p = !trace_redisplay_p;
18427 else
18429 arg = Fprefix_numeric_value (arg);
18430 trace_redisplay_p = XINT (arg) > 0;
18433 return Qnil;
18437 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18438 doc: /* Like `format', but print result to stderr.
18439 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18440 (ptrdiff_t nargs, Lisp_Object *args)
18442 Lisp_Object s = Fformat (nargs, args);
18443 fprintf (stderr, "%s", SDATA (s));
18444 return Qnil;
18447 #endif /* GLYPH_DEBUG */
18451 /***********************************************************************
18452 Building Desired Matrix Rows
18453 ***********************************************************************/
18455 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18456 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18458 static struct glyph_row *
18459 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18461 struct frame *f = XFRAME (WINDOW_FRAME (w));
18462 struct buffer *buffer = XBUFFER (w->contents);
18463 struct buffer *old = current_buffer;
18464 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18465 int arrow_len = SCHARS (overlay_arrow_string);
18466 const unsigned char *arrow_end = arrow_string + arrow_len;
18467 const unsigned char *p;
18468 struct it it;
18469 bool multibyte_p;
18470 int n_glyphs_before;
18472 set_buffer_temp (buffer);
18473 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18474 it.glyph_row->used[TEXT_AREA] = 0;
18475 SET_TEXT_POS (it.position, 0, 0);
18477 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18478 p = arrow_string;
18479 while (p < arrow_end)
18481 Lisp_Object face, ilisp;
18483 /* Get the next character. */
18484 if (multibyte_p)
18485 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18486 else
18488 it.c = it.char_to_display = *p, it.len = 1;
18489 if (! ASCII_CHAR_P (it.c))
18490 it.char_to_display = BYTE8_TO_CHAR (it.c);
18492 p += it.len;
18494 /* Get its face. */
18495 ilisp = make_number (p - arrow_string);
18496 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18497 it.face_id = compute_char_face (f, it.char_to_display, face);
18499 /* Compute its width, get its glyphs. */
18500 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18501 SET_TEXT_POS (it.position, -1, -1);
18502 PRODUCE_GLYPHS (&it);
18504 /* If this character doesn't fit any more in the line, we have
18505 to remove some glyphs. */
18506 if (it.current_x > it.last_visible_x)
18508 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18509 break;
18513 set_buffer_temp (old);
18514 return it.glyph_row;
18518 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18519 glyphs to insert is determined by produce_special_glyphs. */
18521 static void
18522 insert_left_trunc_glyphs (struct it *it)
18524 struct it truncate_it;
18525 struct glyph *from, *end, *to, *toend;
18527 eassert (!FRAME_WINDOW_P (it->f)
18528 || (!it->glyph_row->reversed_p
18529 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18530 || (it->glyph_row->reversed_p
18531 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18533 /* Get the truncation glyphs. */
18534 truncate_it = *it;
18535 truncate_it.current_x = 0;
18536 truncate_it.face_id = DEFAULT_FACE_ID;
18537 truncate_it.glyph_row = &scratch_glyph_row;
18538 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18539 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18540 truncate_it.object = make_number (0);
18541 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18543 /* Overwrite glyphs from IT with truncation glyphs. */
18544 if (!it->glyph_row->reversed_p)
18546 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18548 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18549 end = from + tused;
18550 to = it->glyph_row->glyphs[TEXT_AREA];
18551 toend = to + it->glyph_row->used[TEXT_AREA];
18552 if (FRAME_WINDOW_P (it->f))
18554 /* On GUI frames, when variable-size fonts are displayed,
18555 the truncation glyphs may need more pixels than the row's
18556 glyphs they overwrite. We overwrite more glyphs to free
18557 enough screen real estate, and enlarge the stretch glyph
18558 on the right (see display_line), if there is one, to
18559 preserve the screen position of the truncation glyphs on
18560 the right. */
18561 int w = 0;
18562 struct glyph *g = to;
18563 short used;
18565 /* The first glyph could be partially visible, in which case
18566 it->glyph_row->x will be negative. But we want the left
18567 truncation glyphs to be aligned at the left margin of the
18568 window, so we override the x coordinate at which the row
18569 will begin. */
18570 it->glyph_row->x = 0;
18571 while (g < toend && w < it->truncation_pixel_width)
18573 w += g->pixel_width;
18574 ++g;
18576 if (g - to - tused > 0)
18578 memmove (to + tused, g, (toend - g) * sizeof(*g));
18579 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18581 used = it->glyph_row->used[TEXT_AREA];
18582 if (it->glyph_row->truncated_on_right_p
18583 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18584 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18585 == STRETCH_GLYPH)
18587 int extra = w - it->truncation_pixel_width;
18589 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18593 while (from < end)
18594 *to++ = *from++;
18596 /* There may be padding glyphs left over. Overwrite them too. */
18597 if (!FRAME_WINDOW_P (it->f))
18599 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18601 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18602 while (from < end)
18603 *to++ = *from++;
18607 if (to > toend)
18608 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18610 else
18612 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18614 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18615 that back to front. */
18616 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18617 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18618 toend = it->glyph_row->glyphs[TEXT_AREA];
18619 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18620 if (FRAME_WINDOW_P (it->f))
18622 int w = 0;
18623 struct glyph *g = to;
18625 while (g >= toend && w < it->truncation_pixel_width)
18627 w += g->pixel_width;
18628 --g;
18630 if (to - g - tused > 0)
18631 to = g + tused;
18632 if (it->glyph_row->truncated_on_right_p
18633 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18634 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18636 int extra = w - it->truncation_pixel_width;
18638 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18642 while (from >= end && to >= toend)
18643 *to-- = *from--;
18644 if (!FRAME_WINDOW_P (it->f))
18646 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18648 from =
18649 truncate_it.glyph_row->glyphs[TEXT_AREA]
18650 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18651 while (from >= end && to >= toend)
18652 *to-- = *from--;
18655 if (from >= end)
18657 /* Need to free some room before prepending additional
18658 glyphs. */
18659 int move_by = from - end + 1;
18660 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18661 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18663 for ( ; g >= g0; g--)
18664 g[move_by] = *g;
18665 while (from >= end)
18666 *to-- = *from--;
18667 it->glyph_row->used[TEXT_AREA] += move_by;
18672 /* Compute the hash code for ROW. */
18673 unsigned
18674 row_hash (struct glyph_row *row)
18676 int area, k;
18677 unsigned hashval = 0;
18679 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18680 for (k = 0; k < row->used[area]; ++k)
18681 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18682 + row->glyphs[area][k].u.val
18683 + row->glyphs[area][k].face_id
18684 + row->glyphs[area][k].padding_p
18685 + (row->glyphs[area][k].type << 2));
18687 return hashval;
18690 /* Compute the pixel height and width of IT->glyph_row.
18692 Most of the time, ascent and height of a display line will be equal
18693 to the max_ascent and max_height values of the display iterator
18694 structure. This is not the case if
18696 1. We hit ZV without displaying anything. In this case, max_ascent
18697 and max_height will be zero.
18699 2. We have some glyphs that don't contribute to the line height.
18700 (The glyph row flag contributes_to_line_height_p is for future
18701 pixmap extensions).
18703 The first case is easily covered by using default values because in
18704 these cases, the line height does not really matter, except that it
18705 must not be zero. */
18707 static void
18708 compute_line_metrics (struct it *it)
18710 struct glyph_row *row = it->glyph_row;
18712 if (FRAME_WINDOW_P (it->f))
18714 int i, min_y, max_y;
18716 /* The line may consist of one space only, that was added to
18717 place the cursor on it. If so, the row's height hasn't been
18718 computed yet. */
18719 if (row->height == 0)
18721 if (it->max_ascent + it->max_descent == 0)
18722 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18723 row->ascent = it->max_ascent;
18724 row->height = it->max_ascent + it->max_descent;
18725 row->phys_ascent = it->max_phys_ascent;
18726 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18727 row->extra_line_spacing = it->max_extra_line_spacing;
18730 /* Compute the width of this line. */
18731 row->pixel_width = row->x;
18732 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18733 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18735 eassert (row->pixel_width >= 0);
18736 eassert (row->ascent >= 0 && row->height > 0);
18738 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18739 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18741 /* If first line's physical ascent is larger than its logical
18742 ascent, use the physical ascent, and make the row taller.
18743 This makes accented characters fully visible. */
18744 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18745 && row->phys_ascent > row->ascent)
18747 row->height += row->phys_ascent - row->ascent;
18748 row->ascent = row->phys_ascent;
18751 /* Compute how much of the line is visible. */
18752 row->visible_height = row->height;
18754 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18755 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18757 if (row->y < min_y)
18758 row->visible_height -= min_y - row->y;
18759 if (row->y + row->height > max_y)
18760 row->visible_height -= row->y + row->height - max_y;
18762 else
18764 row->pixel_width = row->used[TEXT_AREA];
18765 if (row->continued_p)
18766 row->pixel_width -= it->continuation_pixel_width;
18767 else if (row->truncated_on_right_p)
18768 row->pixel_width -= it->truncation_pixel_width;
18769 row->ascent = row->phys_ascent = 0;
18770 row->height = row->phys_height = row->visible_height = 1;
18771 row->extra_line_spacing = 0;
18774 /* Compute a hash code for this row. */
18775 row->hash = row_hash (row);
18777 it->max_ascent = it->max_descent = 0;
18778 it->max_phys_ascent = it->max_phys_descent = 0;
18782 /* Append one space to the glyph row of iterator IT if doing a
18783 window-based redisplay. The space has the same face as
18784 IT->face_id. Value is non-zero if a space was added.
18786 This function is called to make sure that there is always one glyph
18787 at the end of a glyph row that the cursor can be set on under
18788 window-systems. (If there weren't such a glyph we would not know
18789 how wide and tall a box cursor should be displayed).
18791 At the same time this space let's a nicely handle clearing to the
18792 end of the line if the row ends in italic text. */
18794 static int
18795 append_space_for_newline (struct it *it, int default_face_p)
18797 if (FRAME_WINDOW_P (it->f))
18799 int n = it->glyph_row->used[TEXT_AREA];
18801 if (it->glyph_row->glyphs[TEXT_AREA] + n
18802 < it->glyph_row->glyphs[1 + TEXT_AREA])
18804 /* Save some values that must not be changed.
18805 Must save IT->c and IT->len because otherwise
18806 ITERATOR_AT_END_P wouldn't work anymore after
18807 append_space_for_newline has been called. */
18808 enum display_element_type saved_what = it->what;
18809 int saved_c = it->c, saved_len = it->len;
18810 int saved_char_to_display = it->char_to_display;
18811 int saved_x = it->current_x;
18812 int saved_face_id = it->face_id;
18813 int saved_box_end = it->end_of_box_run_p;
18814 struct text_pos saved_pos;
18815 Lisp_Object saved_object;
18816 struct face *face;
18818 saved_object = it->object;
18819 saved_pos = it->position;
18821 it->what = IT_CHARACTER;
18822 memset (&it->position, 0, sizeof it->position);
18823 it->object = make_number (0);
18824 it->c = it->char_to_display = ' ';
18825 it->len = 1;
18827 /* If the default face was remapped, be sure to use the
18828 remapped face for the appended newline. */
18829 if (default_face_p)
18830 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18831 else if (it->face_before_selective_p)
18832 it->face_id = it->saved_face_id;
18833 face = FACE_FROM_ID (it->f, it->face_id);
18834 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18835 /* In R2L rows, we will prepend a stretch glyph that will
18836 have the end_of_box_run_p flag set for it, so there's no
18837 need for the appended newline glyph to have that flag
18838 set. */
18839 if (it->glyph_row->reversed_p
18840 /* But if the appended newline glyph goes all the way to
18841 the end of the row, there will be no stretch glyph,
18842 so leave the box flag set. */
18843 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18844 it->end_of_box_run_p = 0;
18846 PRODUCE_GLYPHS (it);
18848 it->override_ascent = -1;
18849 it->constrain_row_ascent_descent_p = 0;
18850 it->current_x = saved_x;
18851 it->object = saved_object;
18852 it->position = saved_pos;
18853 it->what = saved_what;
18854 it->face_id = saved_face_id;
18855 it->len = saved_len;
18856 it->c = saved_c;
18857 it->char_to_display = saved_char_to_display;
18858 it->end_of_box_run_p = saved_box_end;
18859 return 1;
18863 return 0;
18867 /* Extend the face of the last glyph in the text area of IT->glyph_row
18868 to the end of the display line. Called from display_line. If the
18869 glyph row is empty, add a space glyph to it so that we know the
18870 face to draw. Set the glyph row flag fill_line_p. If the glyph
18871 row is R2L, prepend a stretch glyph to cover the empty space to the
18872 left of the leftmost glyph. */
18874 static void
18875 extend_face_to_end_of_line (struct it *it)
18877 struct face *face, *default_face;
18878 struct frame *f = it->f;
18880 /* If line is already filled, do nothing. Non window-system frames
18881 get a grace of one more ``pixel'' because their characters are
18882 1-``pixel'' wide, so they hit the equality too early. This grace
18883 is needed only for R2L rows that are not continued, to produce
18884 one extra blank where we could display the cursor. */
18885 if ((it->current_x >= it->last_visible_x
18886 + (!FRAME_WINDOW_P (f)
18887 && it->glyph_row->reversed_p
18888 && !it->glyph_row->continued_p))
18889 /* If the window has display margins, we will need to extend
18890 their face even if the text area is filled. */
18891 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18892 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18893 return;
18895 /* The default face, possibly remapped. */
18896 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18898 /* Face extension extends the background and box of IT->face_id
18899 to the end of the line. If the background equals the background
18900 of the frame, we don't have to do anything. */
18901 if (it->face_before_selective_p)
18902 face = FACE_FROM_ID (f, it->saved_face_id);
18903 else
18904 face = FACE_FROM_ID (f, it->face_id);
18906 if (FRAME_WINDOW_P (f)
18907 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18908 && face->box == FACE_NO_BOX
18909 && face->background == FRAME_BACKGROUND_PIXEL (f)
18910 #ifdef HAVE_WINDOW_SYSTEM
18911 && !face->stipple
18912 #endif
18913 && !it->glyph_row->reversed_p)
18914 return;
18916 /* Set the glyph row flag indicating that the face of the last glyph
18917 in the text area has to be drawn to the end of the text area. */
18918 it->glyph_row->fill_line_p = 1;
18920 /* If current character of IT is not ASCII, make sure we have the
18921 ASCII face. This will be automatically undone the next time
18922 get_next_display_element returns a multibyte character. Note
18923 that the character will always be single byte in unibyte
18924 text. */
18925 if (!ASCII_CHAR_P (it->c))
18927 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18930 if (FRAME_WINDOW_P (f))
18932 /* If the row is empty, add a space with the current face of IT,
18933 so that we know which face to draw. */
18934 if (it->glyph_row->used[TEXT_AREA] == 0)
18936 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18937 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18938 it->glyph_row->used[TEXT_AREA] = 1;
18940 /* Mode line and the header line don't have margins, and
18941 likewise the frame's tool-bar window, if there is any. */
18942 if (!(it->glyph_row->mode_line_p
18943 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18944 || (WINDOWP (f->tool_bar_window)
18945 && it->w == XWINDOW (f->tool_bar_window))
18946 #endif
18949 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18950 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18952 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18953 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18954 default_face->id;
18955 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18957 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18958 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18960 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18961 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18962 default_face->id;
18963 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18966 #ifdef HAVE_WINDOW_SYSTEM
18967 if (it->glyph_row->reversed_p)
18969 /* Prepend a stretch glyph to the row, such that the
18970 rightmost glyph will be drawn flushed all the way to the
18971 right margin of the window. The stretch glyph that will
18972 occupy the empty space, if any, to the left of the
18973 glyphs. */
18974 struct font *font = face->font ? face->font : FRAME_FONT (f);
18975 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18976 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18977 struct glyph *g;
18978 int row_width, stretch_ascent, stretch_width;
18979 struct text_pos saved_pos;
18980 int saved_face_id, saved_avoid_cursor, saved_box_start;
18982 for (row_width = 0, g = row_start; g < row_end; g++)
18983 row_width += g->pixel_width;
18984 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18985 if (stretch_width > 0)
18987 stretch_ascent =
18988 (((it->ascent + it->descent)
18989 * FONT_BASE (font)) / FONT_HEIGHT (font));
18990 saved_pos = it->position;
18991 memset (&it->position, 0, sizeof it->position);
18992 saved_avoid_cursor = it->avoid_cursor_p;
18993 it->avoid_cursor_p = 1;
18994 saved_face_id = it->face_id;
18995 saved_box_start = it->start_of_box_run_p;
18996 /* The last row's stretch glyph should get the default
18997 face, to avoid painting the rest of the window with
18998 the region face, if the region ends at ZV. */
18999 if (it->glyph_row->ends_at_zv_p)
19000 it->face_id = default_face->id;
19001 else
19002 it->face_id = face->id;
19003 it->start_of_box_run_p = 0;
19004 append_stretch_glyph (it, make_number (0), stretch_width,
19005 it->ascent + it->descent, stretch_ascent);
19006 it->position = saved_pos;
19007 it->avoid_cursor_p = saved_avoid_cursor;
19008 it->face_id = saved_face_id;
19009 it->start_of_box_run_p = saved_box_start;
19012 #endif /* HAVE_WINDOW_SYSTEM */
19014 else
19016 /* Save some values that must not be changed. */
19017 int saved_x = it->current_x;
19018 struct text_pos saved_pos;
19019 Lisp_Object saved_object;
19020 enum display_element_type saved_what = it->what;
19021 int saved_face_id = it->face_id;
19023 saved_object = it->object;
19024 saved_pos = it->position;
19026 it->what = IT_CHARACTER;
19027 memset (&it->position, 0, sizeof it->position);
19028 it->object = make_number (0);
19029 it->c = it->char_to_display = ' ';
19030 it->len = 1;
19032 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19033 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19034 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19035 && !it->glyph_row->mode_line_p
19036 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19038 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19039 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19041 for (it->current_x = 0; g < e; g++)
19042 it->current_x += g->pixel_width;
19044 it->area = LEFT_MARGIN_AREA;
19045 it->face_id = default_face->id;
19046 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19047 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19049 PRODUCE_GLYPHS (it);
19050 /* term.c:produce_glyphs advances it->current_x only for
19051 TEXT_AREA. */
19052 it->current_x += it->pixel_width;
19055 it->current_x = saved_x;
19056 it->area = TEXT_AREA;
19059 /* The last row's blank glyphs should get the default face, to
19060 avoid painting the rest of the window with the region face,
19061 if the region ends at ZV. */
19062 if (it->glyph_row->ends_at_zv_p)
19063 it->face_id = default_face->id;
19064 else
19065 it->face_id = face->id;
19066 PRODUCE_GLYPHS (it);
19068 while (it->current_x <= it->last_visible_x)
19069 PRODUCE_GLYPHS (it);
19071 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19072 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19073 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19074 && !it->glyph_row->mode_line_p
19075 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19077 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19078 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19080 for ( ; g < e; g++)
19081 it->current_x += g->pixel_width;
19083 it->area = RIGHT_MARGIN_AREA;
19084 it->face_id = default_face->id;
19085 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19086 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19088 PRODUCE_GLYPHS (it);
19089 it->current_x += it->pixel_width;
19092 it->area = TEXT_AREA;
19095 /* Don't count these blanks really. It would let us insert a left
19096 truncation glyph below and make us set the cursor on them, maybe. */
19097 it->current_x = saved_x;
19098 it->object = saved_object;
19099 it->position = saved_pos;
19100 it->what = saved_what;
19101 it->face_id = saved_face_id;
19106 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19107 trailing whitespace. */
19109 static int
19110 trailing_whitespace_p (ptrdiff_t charpos)
19112 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19113 int c = 0;
19115 while (bytepos < ZV_BYTE
19116 && (c = FETCH_CHAR (bytepos),
19117 c == ' ' || c == '\t'))
19118 ++bytepos;
19120 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19122 if (bytepos != PT_BYTE)
19123 return 1;
19125 return 0;
19129 /* Highlight trailing whitespace, if any, in ROW. */
19131 static void
19132 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19134 int used = row->used[TEXT_AREA];
19136 if (used)
19138 struct glyph *start = row->glyphs[TEXT_AREA];
19139 struct glyph *glyph = start + used - 1;
19141 if (row->reversed_p)
19143 /* Right-to-left rows need to be processed in the opposite
19144 direction, so swap the edge pointers. */
19145 glyph = start;
19146 start = row->glyphs[TEXT_AREA] + used - 1;
19149 /* Skip over glyphs inserted to display the cursor at the
19150 end of a line, for extending the face of the last glyph
19151 to the end of the line on terminals, and for truncation
19152 and continuation glyphs. */
19153 if (!row->reversed_p)
19155 while (glyph >= start
19156 && glyph->type == CHAR_GLYPH
19157 && INTEGERP (glyph->object))
19158 --glyph;
19160 else
19162 while (glyph <= start
19163 && glyph->type == CHAR_GLYPH
19164 && INTEGERP (glyph->object))
19165 ++glyph;
19168 /* If last glyph is a space or stretch, and it's trailing
19169 whitespace, set the face of all trailing whitespace glyphs in
19170 IT->glyph_row to `trailing-whitespace'. */
19171 if ((row->reversed_p ? glyph <= start : glyph >= start)
19172 && BUFFERP (glyph->object)
19173 && (glyph->type == STRETCH_GLYPH
19174 || (glyph->type == CHAR_GLYPH
19175 && glyph->u.ch == ' '))
19176 && trailing_whitespace_p (glyph->charpos))
19178 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19179 if (face_id < 0)
19180 return;
19182 if (!row->reversed_p)
19184 while (glyph >= start
19185 && BUFFERP (glyph->object)
19186 && (glyph->type == STRETCH_GLYPH
19187 || (glyph->type == CHAR_GLYPH
19188 && glyph->u.ch == ' ')))
19189 (glyph--)->face_id = face_id;
19191 else
19193 while (glyph <= start
19194 && BUFFERP (glyph->object)
19195 && (glyph->type == STRETCH_GLYPH
19196 || (glyph->type == CHAR_GLYPH
19197 && glyph->u.ch == ' ')))
19198 (glyph++)->face_id = face_id;
19205 /* Value is non-zero if glyph row ROW should be
19206 considered to hold the buffer position CHARPOS. */
19208 static int
19209 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19211 int result = 1;
19213 if (charpos == CHARPOS (row->end.pos)
19214 || charpos == MATRIX_ROW_END_CHARPOS (row))
19216 /* Suppose the row ends on a string.
19217 Unless the row is continued, that means it ends on a newline
19218 in the string. If it's anything other than a display string
19219 (e.g., a before-string from an overlay), we don't want the
19220 cursor there. (This heuristic seems to give the optimal
19221 behavior for the various types of multi-line strings.)
19222 One exception: if the string has `cursor' property on one of
19223 its characters, we _do_ want the cursor there. */
19224 if (CHARPOS (row->end.string_pos) >= 0)
19226 if (row->continued_p)
19227 result = 1;
19228 else
19230 /* Check for `display' property. */
19231 struct glyph *beg = row->glyphs[TEXT_AREA];
19232 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19233 struct glyph *glyph;
19235 result = 0;
19236 for (glyph = end; glyph >= beg; --glyph)
19237 if (STRINGP (glyph->object))
19239 Lisp_Object prop
19240 = Fget_char_property (make_number (charpos),
19241 Qdisplay, Qnil);
19242 result =
19243 (!NILP (prop)
19244 && display_prop_string_p (prop, glyph->object));
19245 /* If there's a `cursor' property on one of the
19246 string's characters, this row is a cursor row,
19247 even though this is not a display string. */
19248 if (!result)
19250 Lisp_Object s = glyph->object;
19252 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19254 ptrdiff_t gpos = glyph->charpos;
19256 if (!NILP (Fget_char_property (make_number (gpos),
19257 Qcursor, s)))
19259 result = 1;
19260 break;
19264 break;
19268 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19270 /* If the row ends in middle of a real character,
19271 and the line is continued, we want the cursor here.
19272 That's because CHARPOS (ROW->end.pos) would equal
19273 PT if PT is before the character. */
19274 if (!row->ends_in_ellipsis_p)
19275 result = row->continued_p;
19276 else
19277 /* If the row ends in an ellipsis, then
19278 CHARPOS (ROW->end.pos) will equal point after the
19279 invisible text. We want that position to be displayed
19280 after the ellipsis. */
19281 result = 0;
19283 /* If the row ends at ZV, display the cursor at the end of that
19284 row instead of at the start of the row below. */
19285 else if (row->ends_at_zv_p)
19286 result = 1;
19287 else
19288 result = 0;
19291 return result;
19294 /* Value is non-zero if glyph row ROW should be
19295 used to hold the cursor. */
19297 static int
19298 cursor_row_p (struct glyph_row *row)
19300 return row_for_charpos_p (row, PT);
19305 /* Push the property PROP so that it will be rendered at the current
19306 position in IT. Return 1 if PROP was successfully pushed, 0
19307 otherwise. Called from handle_line_prefix to handle the
19308 `line-prefix' and `wrap-prefix' properties. */
19310 static int
19311 push_prefix_prop (struct it *it, Lisp_Object prop)
19313 struct text_pos pos =
19314 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19316 eassert (it->method == GET_FROM_BUFFER
19317 || it->method == GET_FROM_DISPLAY_VECTOR
19318 || it->method == GET_FROM_STRING);
19320 /* We need to save the current buffer/string position, so it will be
19321 restored by pop_it, because iterate_out_of_display_property
19322 depends on that being set correctly, but some situations leave
19323 it->position not yet set when this function is called. */
19324 push_it (it, &pos);
19326 if (STRINGP (prop))
19328 if (SCHARS (prop) == 0)
19330 pop_it (it);
19331 return 0;
19334 it->string = prop;
19335 it->string_from_prefix_prop_p = 1;
19336 it->multibyte_p = STRING_MULTIBYTE (it->string);
19337 it->current.overlay_string_index = -1;
19338 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19339 it->end_charpos = it->string_nchars = SCHARS (it->string);
19340 it->method = GET_FROM_STRING;
19341 it->stop_charpos = 0;
19342 it->prev_stop = 0;
19343 it->base_level_stop = 0;
19345 /* Force paragraph direction to be that of the parent
19346 buffer/string. */
19347 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19348 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19349 else
19350 it->paragraph_embedding = L2R;
19352 /* Set up the bidi iterator for this display string. */
19353 if (it->bidi_p)
19355 it->bidi_it.string.lstring = it->string;
19356 it->bidi_it.string.s = NULL;
19357 it->bidi_it.string.schars = it->end_charpos;
19358 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19359 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19360 it->bidi_it.string.unibyte = !it->multibyte_p;
19361 it->bidi_it.w = it->w;
19362 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19365 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19367 it->method = GET_FROM_STRETCH;
19368 it->object = prop;
19370 #ifdef HAVE_WINDOW_SYSTEM
19371 else if (IMAGEP (prop))
19373 it->what = IT_IMAGE;
19374 it->image_id = lookup_image (it->f, prop);
19375 it->method = GET_FROM_IMAGE;
19377 #endif /* HAVE_WINDOW_SYSTEM */
19378 else
19380 pop_it (it); /* bogus display property, give up */
19381 return 0;
19384 return 1;
19387 /* Return the character-property PROP at the current position in IT. */
19389 static Lisp_Object
19390 get_it_property (struct it *it, Lisp_Object prop)
19392 Lisp_Object position, object = it->object;
19394 if (STRINGP (object))
19395 position = make_number (IT_STRING_CHARPOS (*it));
19396 else if (BUFFERP (object))
19398 position = make_number (IT_CHARPOS (*it));
19399 object = it->window;
19401 else
19402 return Qnil;
19404 return Fget_char_property (position, prop, object);
19407 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19409 static void
19410 handle_line_prefix (struct it *it)
19412 Lisp_Object prefix;
19414 if (it->continuation_lines_width > 0)
19416 prefix = get_it_property (it, Qwrap_prefix);
19417 if (NILP (prefix))
19418 prefix = Vwrap_prefix;
19420 else
19422 prefix = get_it_property (it, Qline_prefix);
19423 if (NILP (prefix))
19424 prefix = Vline_prefix;
19426 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19428 /* If the prefix is wider than the window, and we try to wrap
19429 it, it would acquire its own wrap prefix, and so on till the
19430 iterator stack overflows. So, don't wrap the prefix. */
19431 it->line_wrap = TRUNCATE;
19432 it->avoid_cursor_p = 1;
19438 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19439 only for R2L lines from display_line and display_string, when they
19440 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19441 the line/string needs to be continued on the next glyph row. */
19442 static void
19443 unproduce_glyphs (struct it *it, int n)
19445 struct glyph *glyph, *end;
19447 eassert (it->glyph_row);
19448 eassert (it->glyph_row->reversed_p);
19449 eassert (it->area == TEXT_AREA);
19450 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19452 if (n > it->glyph_row->used[TEXT_AREA])
19453 n = it->glyph_row->used[TEXT_AREA];
19454 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19455 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19456 for ( ; glyph < end; glyph++)
19457 glyph[-n] = *glyph;
19460 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19461 and ROW->maxpos. */
19462 static void
19463 find_row_edges (struct it *it, struct glyph_row *row,
19464 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19465 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19467 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19468 lines' rows is implemented for bidi-reordered rows. */
19470 /* ROW->minpos is the value of min_pos, the minimal buffer position
19471 we have in ROW, or ROW->start.pos if that is smaller. */
19472 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19473 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19474 else
19475 /* We didn't find buffer positions smaller than ROW->start, or
19476 didn't find _any_ valid buffer positions in any of the glyphs,
19477 so we must trust the iterator's computed positions. */
19478 row->minpos = row->start.pos;
19479 if (max_pos <= 0)
19481 max_pos = CHARPOS (it->current.pos);
19482 max_bpos = BYTEPOS (it->current.pos);
19485 /* Here are the various use-cases for ending the row, and the
19486 corresponding values for ROW->maxpos:
19488 Line ends in a newline from buffer eol_pos + 1
19489 Line is continued from buffer max_pos + 1
19490 Line is truncated on right it->current.pos
19491 Line ends in a newline from string max_pos + 1(*)
19492 (*) + 1 only when line ends in a forward scan
19493 Line is continued from string max_pos
19494 Line is continued from display vector max_pos
19495 Line is entirely from a string min_pos == max_pos
19496 Line is entirely from a display vector min_pos == max_pos
19497 Line that ends at ZV ZV
19499 If you discover other use-cases, please add them here as
19500 appropriate. */
19501 if (row->ends_at_zv_p)
19502 row->maxpos = it->current.pos;
19503 else if (row->used[TEXT_AREA])
19505 int seen_this_string = 0;
19506 struct glyph_row *r1 = row - 1;
19508 /* Did we see the same display string on the previous row? */
19509 if (STRINGP (it->object)
19510 /* this is not the first row */
19511 && row > it->w->desired_matrix->rows
19512 /* previous row is not the header line */
19513 && !r1->mode_line_p
19514 /* previous row also ends in a newline from a string */
19515 && r1->ends_in_newline_from_string_p)
19517 struct glyph *start, *end;
19519 /* Search for the last glyph of the previous row that came
19520 from buffer or string. Depending on whether the row is
19521 L2R or R2L, we need to process it front to back or the
19522 other way round. */
19523 if (!r1->reversed_p)
19525 start = r1->glyphs[TEXT_AREA];
19526 end = start + r1->used[TEXT_AREA];
19527 /* Glyphs inserted by redisplay have an integer (zero)
19528 as their object. */
19529 while (end > start
19530 && INTEGERP ((end - 1)->object)
19531 && (end - 1)->charpos <= 0)
19532 --end;
19533 if (end > start)
19535 if (EQ ((end - 1)->object, it->object))
19536 seen_this_string = 1;
19538 else
19539 /* If all the glyphs of the previous row were inserted
19540 by redisplay, it means the previous row was
19541 produced from a single newline, which is only
19542 possible if that newline came from the same string
19543 as the one which produced this ROW. */
19544 seen_this_string = 1;
19546 else
19548 end = r1->glyphs[TEXT_AREA] - 1;
19549 start = end + r1->used[TEXT_AREA];
19550 while (end < start
19551 && INTEGERP ((end + 1)->object)
19552 && (end + 1)->charpos <= 0)
19553 ++end;
19554 if (end < start)
19556 if (EQ ((end + 1)->object, it->object))
19557 seen_this_string = 1;
19559 else
19560 seen_this_string = 1;
19563 /* Take note of each display string that covers a newline only
19564 once, the first time we see it. This is for when a display
19565 string includes more than one newline in it. */
19566 if (row->ends_in_newline_from_string_p && !seen_this_string)
19568 /* If we were scanning the buffer forward when we displayed
19569 the string, we want to account for at least one buffer
19570 position that belongs to this row (position covered by
19571 the display string), so that cursor positioning will
19572 consider this row as a candidate when point is at the end
19573 of the visual line represented by this row. This is not
19574 required when scanning back, because max_pos will already
19575 have a much larger value. */
19576 if (CHARPOS (row->end.pos) > max_pos)
19577 INC_BOTH (max_pos, max_bpos);
19578 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19580 else if (CHARPOS (it->eol_pos) > 0)
19581 SET_TEXT_POS (row->maxpos,
19582 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19583 else if (row->continued_p)
19585 /* If max_pos is different from IT's current position, it
19586 means IT->method does not belong to the display element
19587 at max_pos. However, it also means that the display
19588 element at max_pos was displayed in its entirety on this
19589 line, which is equivalent to saying that the next line
19590 starts at the next buffer position. */
19591 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19592 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19593 else
19595 INC_BOTH (max_pos, max_bpos);
19596 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19599 else if (row->truncated_on_right_p)
19600 /* display_line already called reseat_at_next_visible_line_start,
19601 which puts the iterator at the beginning of the next line, in
19602 the logical order. */
19603 row->maxpos = it->current.pos;
19604 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19605 /* A line that is entirely from a string/image/stretch... */
19606 row->maxpos = row->minpos;
19607 else
19608 emacs_abort ();
19610 else
19611 row->maxpos = it->current.pos;
19614 /* Construct the glyph row IT->glyph_row in the desired matrix of
19615 IT->w from text at the current position of IT. See dispextern.h
19616 for an overview of struct it. Value is non-zero if
19617 IT->glyph_row displays text, as opposed to a line displaying ZV
19618 only. */
19620 static int
19621 display_line (struct it *it)
19623 struct glyph_row *row = it->glyph_row;
19624 Lisp_Object overlay_arrow_string;
19625 struct it wrap_it;
19626 void *wrap_data = NULL;
19627 int may_wrap = 0, wrap_x IF_LINT (= 0);
19628 int wrap_row_used = -1;
19629 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19630 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19631 int wrap_row_extra_line_spacing IF_LINT (= 0);
19632 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19633 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19634 int cvpos;
19635 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19636 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19638 /* We always start displaying at hpos zero even if hscrolled. */
19639 eassert (it->hpos == 0 && it->current_x == 0);
19641 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19642 >= it->w->desired_matrix->nrows)
19644 it->w->nrows_scale_factor++;
19645 it->f->fonts_changed = 1;
19646 return 0;
19649 /* Clear the result glyph row and enable it. */
19650 prepare_desired_row (row);
19652 row->y = it->current_y;
19653 row->start = it->start;
19654 row->continuation_lines_width = it->continuation_lines_width;
19655 row->displays_text_p = 1;
19656 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19657 it->starts_in_middle_of_char_p = 0;
19659 /* Arrange the overlays nicely for our purposes. Usually, we call
19660 display_line on only one line at a time, in which case this
19661 can't really hurt too much, or we call it on lines which appear
19662 one after another in the buffer, in which case all calls to
19663 recenter_overlay_lists but the first will be pretty cheap. */
19664 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19666 /* Move over display elements that are not visible because we are
19667 hscrolled. This may stop at an x-position < IT->first_visible_x
19668 if the first glyph is partially visible or if we hit a line end. */
19669 if (it->current_x < it->first_visible_x)
19671 enum move_it_result move_result;
19673 this_line_min_pos = row->start.pos;
19674 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19675 MOVE_TO_POS | MOVE_TO_X);
19676 /* If we are under a large hscroll, move_it_in_display_line_to
19677 could hit the end of the line without reaching
19678 it->first_visible_x. Pretend that we did reach it. This is
19679 especially important on a TTY, where we will call
19680 extend_face_to_end_of_line, which needs to know how many
19681 blank glyphs to produce. */
19682 if (it->current_x < it->first_visible_x
19683 && (move_result == MOVE_NEWLINE_OR_CR
19684 || move_result == MOVE_POS_MATCH_OR_ZV))
19685 it->current_x = it->first_visible_x;
19687 /* Record the smallest positions seen while we moved over
19688 display elements that are not visible. This is needed by
19689 redisplay_internal for optimizing the case where the cursor
19690 stays inside the same line. The rest of this function only
19691 considers positions that are actually displayed, so
19692 RECORD_MAX_MIN_POS will not otherwise record positions that
19693 are hscrolled to the left of the left edge of the window. */
19694 min_pos = CHARPOS (this_line_min_pos);
19695 min_bpos = BYTEPOS (this_line_min_pos);
19697 else
19699 /* We only do this when not calling `move_it_in_display_line_to'
19700 above, because move_it_in_display_line_to calls
19701 handle_line_prefix itself. */
19702 handle_line_prefix (it);
19705 /* Get the initial row height. This is either the height of the
19706 text hscrolled, if there is any, or zero. */
19707 row->ascent = it->max_ascent;
19708 row->height = it->max_ascent + it->max_descent;
19709 row->phys_ascent = it->max_phys_ascent;
19710 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19711 row->extra_line_spacing = it->max_extra_line_spacing;
19713 /* Utility macro to record max and min buffer positions seen until now. */
19714 #define RECORD_MAX_MIN_POS(IT) \
19715 do \
19717 int composition_p = !STRINGP ((IT)->string) \
19718 && ((IT)->what == IT_COMPOSITION); \
19719 ptrdiff_t current_pos = \
19720 composition_p ? (IT)->cmp_it.charpos \
19721 : IT_CHARPOS (*(IT)); \
19722 ptrdiff_t current_bpos = \
19723 composition_p ? CHAR_TO_BYTE (current_pos) \
19724 : IT_BYTEPOS (*(IT)); \
19725 if (current_pos < min_pos) \
19727 min_pos = current_pos; \
19728 min_bpos = current_bpos; \
19730 if (IT_CHARPOS (*it) > max_pos) \
19732 max_pos = IT_CHARPOS (*it); \
19733 max_bpos = IT_BYTEPOS (*it); \
19736 while (0)
19738 /* Loop generating characters. The loop is left with IT on the next
19739 character to display. */
19740 while (1)
19742 int n_glyphs_before, hpos_before, x_before;
19743 int x, nglyphs;
19744 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19746 /* Retrieve the next thing to display. Value is zero if end of
19747 buffer reached. */
19748 if (!get_next_display_element (it))
19750 /* Maybe add a space at the end of this line that is used to
19751 display the cursor there under X. Set the charpos of the
19752 first glyph of blank lines not corresponding to any text
19753 to -1. */
19754 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19755 row->exact_window_width_line_p = 1;
19756 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19757 || row->used[TEXT_AREA] == 0)
19759 row->glyphs[TEXT_AREA]->charpos = -1;
19760 row->displays_text_p = 0;
19762 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19763 && (!MINI_WINDOW_P (it->w)
19764 || (minibuf_level && EQ (it->window, minibuf_window))))
19765 row->indicate_empty_line_p = 1;
19768 it->continuation_lines_width = 0;
19769 row->ends_at_zv_p = 1;
19770 /* A row that displays right-to-left text must always have
19771 its last face extended all the way to the end of line,
19772 even if this row ends in ZV, because we still write to
19773 the screen left to right. We also need to extend the
19774 last face if the default face is remapped to some
19775 different face, otherwise the functions that clear
19776 portions of the screen will clear with the default face's
19777 background color. */
19778 if (row->reversed_p
19779 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19780 extend_face_to_end_of_line (it);
19781 break;
19784 /* Now, get the metrics of what we want to display. This also
19785 generates glyphs in `row' (which is IT->glyph_row). */
19786 n_glyphs_before = row->used[TEXT_AREA];
19787 x = it->current_x;
19789 /* Remember the line height so far in case the next element doesn't
19790 fit on the line. */
19791 if (it->line_wrap != TRUNCATE)
19793 ascent = it->max_ascent;
19794 descent = it->max_descent;
19795 phys_ascent = it->max_phys_ascent;
19796 phys_descent = it->max_phys_descent;
19798 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19800 if (IT_DISPLAYING_WHITESPACE (it))
19801 may_wrap = 1;
19802 else if (may_wrap)
19804 SAVE_IT (wrap_it, *it, wrap_data);
19805 wrap_x = x;
19806 wrap_row_used = row->used[TEXT_AREA];
19807 wrap_row_ascent = row->ascent;
19808 wrap_row_height = row->height;
19809 wrap_row_phys_ascent = row->phys_ascent;
19810 wrap_row_phys_height = row->phys_height;
19811 wrap_row_extra_line_spacing = row->extra_line_spacing;
19812 wrap_row_min_pos = min_pos;
19813 wrap_row_min_bpos = min_bpos;
19814 wrap_row_max_pos = max_pos;
19815 wrap_row_max_bpos = max_bpos;
19816 may_wrap = 0;
19821 PRODUCE_GLYPHS (it);
19823 /* If this display element was in marginal areas, continue with
19824 the next one. */
19825 if (it->area != TEXT_AREA)
19827 row->ascent = max (row->ascent, it->max_ascent);
19828 row->height = max (row->height, it->max_ascent + it->max_descent);
19829 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19830 row->phys_height = max (row->phys_height,
19831 it->max_phys_ascent + it->max_phys_descent);
19832 row->extra_line_spacing = max (row->extra_line_spacing,
19833 it->max_extra_line_spacing);
19834 set_iterator_to_next (it, 1);
19835 continue;
19838 /* Does the display element fit on the line? If we truncate
19839 lines, we should draw past the right edge of the window. If
19840 we don't truncate, we want to stop so that we can display the
19841 continuation glyph before the right margin. If lines are
19842 continued, there are two possible strategies for characters
19843 resulting in more than 1 glyph (e.g. tabs): Display as many
19844 glyphs as possible in this line and leave the rest for the
19845 continuation line, or display the whole element in the next
19846 line. Original redisplay did the former, so we do it also. */
19847 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19848 hpos_before = it->hpos;
19849 x_before = x;
19851 if (/* Not a newline. */
19852 nglyphs > 0
19853 /* Glyphs produced fit entirely in the line. */
19854 && it->current_x < it->last_visible_x)
19856 it->hpos += nglyphs;
19857 row->ascent = max (row->ascent, it->max_ascent);
19858 row->height = max (row->height, it->max_ascent + it->max_descent);
19859 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19860 row->phys_height = max (row->phys_height,
19861 it->max_phys_ascent + it->max_phys_descent);
19862 row->extra_line_spacing = max (row->extra_line_spacing,
19863 it->max_extra_line_spacing);
19864 if (it->current_x - it->pixel_width < it->first_visible_x)
19865 row->x = x - it->first_visible_x;
19866 /* Record the maximum and minimum buffer positions seen so
19867 far in glyphs that will be displayed by this row. */
19868 if (it->bidi_p)
19869 RECORD_MAX_MIN_POS (it);
19871 else
19873 int i, new_x;
19874 struct glyph *glyph;
19876 for (i = 0; i < nglyphs; ++i, x = new_x)
19878 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19879 new_x = x + glyph->pixel_width;
19881 if (/* Lines are continued. */
19882 it->line_wrap != TRUNCATE
19883 && (/* Glyph doesn't fit on the line. */
19884 new_x > it->last_visible_x
19885 /* Or it fits exactly on a window system frame. */
19886 || (new_x == it->last_visible_x
19887 && FRAME_WINDOW_P (it->f)
19888 && (row->reversed_p
19889 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19890 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19892 /* End of a continued line. */
19894 if (it->hpos == 0
19895 || (new_x == it->last_visible_x
19896 && FRAME_WINDOW_P (it->f)
19897 && (row->reversed_p
19898 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19899 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19901 /* Current glyph is the only one on the line or
19902 fits exactly on the line. We must continue
19903 the line because we can't draw the cursor
19904 after the glyph. */
19905 row->continued_p = 1;
19906 it->current_x = new_x;
19907 it->continuation_lines_width += new_x;
19908 ++it->hpos;
19909 if (i == nglyphs - 1)
19911 /* If line-wrap is on, check if a previous
19912 wrap point was found. */
19913 if (wrap_row_used > 0
19914 /* Even if there is a previous wrap
19915 point, continue the line here as
19916 usual, if (i) the previous character
19917 was a space or tab AND (ii) the
19918 current character is not. */
19919 && (!may_wrap
19920 || IT_DISPLAYING_WHITESPACE (it)))
19921 goto back_to_wrap;
19923 /* Record the maximum and minimum buffer
19924 positions seen so far in glyphs that will be
19925 displayed by this row. */
19926 if (it->bidi_p)
19927 RECORD_MAX_MIN_POS (it);
19928 set_iterator_to_next (it, 1);
19929 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19931 if (!get_next_display_element (it))
19933 row->exact_window_width_line_p = 1;
19934 it->continuation_lines_width = 0;
19935 row->continued_p = 0;
19936 row->ends_at_zv_p = 1;
19938 else if (ITERATOR_AT_END_OF_LINE_P (it))
19940 row->continued_p = 0;
19941 row->exact_window_width_line_p = 1;
19945 else if (it->bidi_p)
19946 RECORD_MAX_MIN_POS (it);
19947 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19948 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19949 extend_face_to_end_of_line (it);
19951 else if (CHAR_GLYPH_PADDING_P (*glyph)
19952 && !FRAME_WINDOW_P (it->f))
19954 /* A padding glyph that doesn't fit on this line.
19955 This means the whole character doesn't fit
19956 on the line. */
19957 if (row->reversed_p)
19958 unproduce_glyphs (it, row->used[TEXT_AREA]
19959 - n_glyphs_before);
19960 row->used[TEXT_AREA] = n_glyphs_before;
19962 /* Fill the rest of the row with continuation
19963 glyphs like in 20.x. */
19964 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19965 < row->glyphs[1 + TEXT_AREA])
19966 produce_special_glyphs (it, IT_CONTINUATION);
19968 row->continued_p = 1;
19969 it->current_x = x_before;
19970 it->continuation_lines_width += x_before;
19972 /* Restore the height to what it was before the
19973 element not fitting on the line. */
19974 it->max_ascent = ascent;
19975 it->max_descent = descent;
19976 it->max_phys_ascent = phys_ascent;
19977 it->max_phys_descent = phys_descent;
19978 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19979 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19980 extend_face_to_end_of_line (it);
19982 else if (wrap_row_used > 0)
19984 back_to_wrap:
19985 if (row->reversed_p)
19986 unproduce_glyphs (it,
19987 row->used[TEXT_AREA] - wrap_row_used);
19988 RESTORE_IT (it, &wrap_it, wrap_data);
19989 it->continuation_lines_width += wrap_x;
19990 row->used[TEXT_AREA] = wrap_row_used;
19991 row->ascent = wrap_row_ascent;
19992 row->height = wrap_row_height;
19993 row->phys_ascent = wrap_row_phys_ascent;
19994 row->phys_height = wrap_row_phys_height;
19995 row->extra_line_spacing = wrap_row_extra_line_spacing;
19996 min_pos = wrap_row_min_pos;
19997 min_bpos = wrap_row_min_bpos;
19998 max_pos = wrap_row_max_pos;
19999 max_bpos = wrap_row_max_bpos;
20000 row->continued_p = 1;
20001 row->ends_at_zv_p = 0;
20002 row->exact_window_width_line_p = 0;
20003 it->continuation_lines_width += x;
20005 /* Make sure that a non-default face is extended
20006 up to the right margin of the window. */
20007 extend_face_to_end_of_line (it);
20009 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20011 /* A TAB that extends past the right edge of the
20012 window. This produces a single glyph on
20013 window system frames. We leave the glyph in
20014 this row and let it fill the row, but don't
20015 consume the TAB. */
20016 if ((row->reversed_p
20017 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20018 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20019 produce_special_glyphs (it, IT_CONTINUATION);
20020 it->continuation_lines_width += it->last_visible_x;
20021 row->ends_in_middle_of_char_p = 1;
20022 row->continued_p = 1;
20023 glyph->pixel_width = it->last_visible_x - x;
20024 it->starts_in_middle_of_char_p = 1;
20025 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20026 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20027 extend_face_to_end_of_line (it);
20029 else
20031 /* Something other than a TAB that draws past
20032 the right edge of the window. Restore
20033 positions to values before the element. */
20034 if (row->reversed_p)
20035 unproduce_glyphs (it, row->used[TEXT_AREA]
20036 - (n_glyphs_before + i));
20037 row->used[TEXT_AREA] = n_glyphs_before + i;
20039 /* Display continuation glyphs. */
20040 it->current_x = x_before;
20041 it->continuation_lines_width += x;
20042 if (!FRAME_WINDOW_P (it->f)
20043 || (row->reversed_p
20044 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20045 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20046 produce_special_glyphs (it, IT_CONTINUATION);
20047 row->continued_p = 1;
20049 extend_face_to_end_of_line (it);
20051 if (nglyphs > 1 && i > 0)
20053 row->ends_in_middle_of_char_p = 1;
20054 it->starts_in_middle_of_char_p = 1;
20057 /* Restore the height to what it was before the
20058 element not fitting on the line. */
20059 it->max_ascent = ascent;
20060 it->max_descent = descent;
20061 it->max_phys_ascent = phys_ascent;
20062 it->max_phys_descent = phys_descent;
20065 break;
20067 else if (new_x > it->first_visible_x)
20069 /* Increment number of glyphs actually displayed. */
20070 ++it->hpos;
20072 /* Record the maximum and minimum buffer positions
20073 seen so far in glyphs that will be displayed by
20074 this row. */
20075 if (it->bidi_p)
20076 RECORD_MAX_MIN_POS (it);
20078 if (x < it->first_visible_x)
20079 /* Glyph is partially visible, i.e. row starts at
20080 negative X position. */
20081 row->x = x - it->first_visible_x;
20083 else
20085 /* Glyph is completely off the left margin of the
20086 window. This should not happen because of the
20087 move_it_in_display_line at the start of this
20088 function, unless the text display area of the
20089 window is empty. */
20090 eassert (it->first_visible_x <= it->last_visible_x);
20093 /* Even if this display element produced no glyphs at all,
20094 we want to record its position. */
20095 if (it->bidi_p && nglyphs == 0)
20096 RECORD_MAX_MIN_POS (it);
20098 row->ascent = max (row->ascent, it->max_ascent);
20099 row->height = max (row->height, it->max_ascent + it->max_descent);
20100 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20101 row->phys_height = max (row->phys_height,
20102 it->max_phys_ascent + it->max_phys_descent);
20103 row->extra_line_spacing = max (row->extra_line_spacing,
20104 it->max_extra_line_spacing);
20106 /* End of this display line if row is continued. */
20107 if (row->continued_p || row->ends_at_zv_p)
20108 break;
20111 at_end_of_line:
20112 /* Is this a line end? If yes, we're also done, after making
20113 sure that a non-default face is extended up to the right
20114 margin of the window. */
20115 if (ITERATOR_AT_END_OF_LINE_P (it))
20117 int used_before = row->used[TEXT_AREA];
20119 row->ends_in_newline_from_string_p = STRINGP (it->object);
20121 /* Add a space at the end of the line that is used to
20122 display the cursor there. */
20123 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20124 append_space_for_newline (it, 0);
20126 /* Extend the face to the end of the line. */
20127 extend_face_to_end_of_line (it);
20129 /* Make sure we have the position. */
20130 if (used_before == 0)
20131 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20133 /* Record the position of the newline, for use in
20134 find_row_edges. */
20135 it->eol_pos = it->current.pos;
20137 /* Consume the line end. This skips over invisible lines. */
20138 set_iterator_to_next (it, 1);
20139 it->continuation_lines_width = 0;
20140 break;
20143 /* Proceed with next display element. Note that this skips
20144 over lines invisible because of selective display. */
20145 set_iterator_to_next (it, 1);
20147 /* If we truncate lines, we are done when the last displayed
20148 glyphs reach past the right margin of the window. */
20149 if (it->line_wrap == TRUNCATE
20150 && ((FRAME_WINDOW_P (it->f)
20151 /* Images are preprocessed in produce_image_glyph such
20152 that they are cropped at the right edge of the
20153 window, so an image glyph will always end exactly at
20154 last_visible_x, even if there's no right fringe. */
20155 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20156 ? (it->current_x >= it->last_visible_x)
20157 : (it->current_x > it->last_visible_x)))
20159 /* Maybe add truncation glyphs. */
20160 if (!FRAME_WINDOW_P (it->f)
20161 || (row->reversed_p
20162 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20163 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20165 int i, n;
20167 if (!row->reversed_p)
20169 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20170 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20171 break;
20173 else
20175 for (i = 0; i < row->used[TEXT_AREA]; i++)
20176 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20177 break;
20178 /* Remove any padding glyphs at the front of ROW, to
20179 make room for the truncation glyphs we will be
20180 adding below. The loop below always inserts at
20181 least one truncation glyph, so also remove the
20182 last glyph added to ROW. */
20183 unproduce_glyphs (it, i + 1);
20184 /* Adjust i for the loop below. */
20185 i = row->used[TEXT_AREA] - (i + 1);
20188 /* produce_special_glyphs overwrites the last glyph, so
20189 we don't want that if we want to keep that last
20190 glyph, which means it's an image. */
20191 if (it->current_x > it->last_visible_x)
20193 it->current_x = x_before;
20194 if (!FRAME_WINDOW_P (it->f))
20196 for (n = row->used[TEXT_AREA]; i < n; ++i)
20198 row->used[TEXT_AREA] = i;
20199 produce_special_glyphs (it, IT_TRUNCATION);
20202 else
20204 row->used[TEXT_AREA] = i;
20205 produce_special_glyphs (it, IT_TRUNCATION);
20207 it->hpos = hpos_before;
20210 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20212 /* Don't truncate if we can overflow newline into fringe. */
20213 if (!get_next_display_element (it))
20215 it->continuation_lines_width = 0;
20216 row->ends_at_zv_p = 1;
20217 row->exact_window_width_line_p = 1;
20218 break;
20220 if (ITERATOR_AT_END_OF_LINE_P (it))
20222 row->exact_window_width_line_p = 1;
20223 goto at_end_of_line;
20225 it->current_x = x_before;
20226 it->hpos = hpos_before;
20229 row->truncated_on_right_p = 1;
20230 it->continuation_lines_width = 0;
20231 reseat_at_next_visible_line_start (it, 0);
20232 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20233 break;
20237 if (wrap_data)
20238 bidi_unshelve_cache (wrap_data, 1);
20240 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20241 at the left window margin. */
20242 if (it->first_visible_x
20243 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20245 if (!FRAME_WINDOW_P (it->f)
20246 || (((row->reversed_p
20247 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20248 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20249 /* Don't let insert_left_trunc_glyphs overwrite the
20250 first glyph of the row if it is an image. */
20251 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20252 insert_left_trunc_glyphs (it);
20253 row->truncated_on_left_p = 1;
20256 /* Remember the position at which this line ends.
20258 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20259 cannot be before the call to find_row_edges below, since that is
20260 where these positions are determined. */
20261 row->end = it->current;
20262 if (!it->bidi_p)
20264 row->minpos = row->start.pos;
20265 row->maxpos = row->end.pos;
20267 else
20269 /* ROW->minpos and ROW->maxpos must be the smallest and
20270 `1 + the largest' buffer positions in ROW. But if ROW was
20271 bidi-reordered, these two positions can be anywhere in the
20272 row, so we must determine them now. */
20273 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20276 /* If the start of this line is the overlay arrow-position, then
20277 mark this glyph row as the one containing the overlay arrow.
20278 This is clearly a mess with variable size fonts. It would be
20279 better to let it be displayed like cursors under X. */
20280 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20281 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20282 !NILP (overlay_arrow_string)))
20284 /* Overlay arrow in window redisplay is a fringe bitmap. */
20285 if (STRINGP (overlay_arrow_string))
20287 struct glyph_row *arrow_row
20288 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20289 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20290 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20291 struct glyph *p = row->glyphs[TEXT_AREA];
20292 struct glyph *p2, *end;
20294 /* Copy the arrow glyphs. */
20295 while (glyph < arrow_end)
20296 *p++ = *glyph++;
20298 /* Throw away padding glyphs. */
20299 p2 = p;
20300 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20301 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20302 ++p2;
20303 if (p2 > p)
20305 while (p2 < end)
20306 *p++ = *p2++;
20307 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20310 else
20312 eassert (INTEGERP (overlay_arrow_string));
20313 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20315 overlay_arrow_seen = 1;
20318 /* Highlight trailing whitespace. */
20319 if (!NILP (Vshow_trailing_whitespace))
20320 highlight_trailing_whitespace (it->f, it->glyph_row);
20322 /* Compute pixel dimensions of this line. */
20323 compute_line_metrics (it);
20325 /* Implementation note: No changes in the glyphs of ROW or in their
20326 faces can be done past this point, because compute_line_metrics
20327 computes ROW's hash value and stores it within the glyph_row
20328 structure. */
20330 /* Record whether this row ends inside an ellipsis. */
20331 row->ends_in_ellipsis_p
20332 = (it->method == GET_FROM_DISPLAY_VECTOR
20333 && it->ellipsis_p);
20335 /* Save fringe bitmaps in this row. */
20336 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20337 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20338 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20339 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20341 it->left_user_fringe_bitmap = 0;
20342 it->left_user_fringe_face_id = 0;
20343 it->right_user_fringe_bitmap = 0;
20344 it->right_user_fringe_face_id = 0;
20346 /* Maybe set the cursor. */
20347 cvpos = it->w->cursor.vpos;
20348 if ((cvpos < 0
20349 /* In bidi-reordered rows, keep checking for proper cursor
20350 position even if one has been found already, because buffer
20351 positions in such rows change non-linearly with ROW->VPOS,
20352 when a line is continued. One exception: when we are at ZV,
20353 display cursor on the first suitable glyph row, since all
20354 the empty rows after that also have their position set to ZV. */
20355 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20356 lines' rows is implemented for bidi-reordered rows. */
20357 || (it->bidi_p
20358 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20359 && PT >= MATRIX_ROW_START_CHARPOS (row)
20360 && PT <= MATRIX_ROW_END_CHARPOS (row)
20361 && cursor_row_p (row))
20362 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20364 /* Prepare for the next line. This line starts horizontally at (X
20365 HPOS) = (0 0). Vertical positions are incremented. As a
20366 convenience for the caller, IT->glyph_row is set to the next
20367 row to be used. */
20368 it->current_x = it->hpos = 0;
20369 it->current_y += row->height;
20370 SET_TEXT_POS (it->eol_pos, 0, 0);
20371 ++it->vpos;
20372 ++it->glyph_row;
20373 /* The next row should by default use the same value of the
20374 reversed_p flag as this one. set_iterator_to_next decides when
20375 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20376 the flag accordingly. */
20377 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20378 it->glyph_row->reversed_p = row->reversed_p;
20379 it->start = row->end;
20380 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20382 #undef RECORD_MAX_MIN_POS
20385 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20386 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20387 doc: /* Return paragraph direction at point in BUFFER.
20388 Value is either `left-to-right' or `right-to-left'.
20389 If BUFFER is omitted or nil, it defaults to the current buffer.
20391 Paragraph direction determines how the text in the paragraph is displayed.
20392 In left-to-right paragraphs, text begins at the left margin of the window
20393 and the reading direction is generally left to right. In right-to-left
20394 paragraphs, text begins at the right margin and is read from right to left.
20396 See also `bidi-paragraph-direction'. */)
20397 (Lisp_Object buffer)
20399 struct buffer *buf = current_buffer;
20400 struct buffer *old = buf;
20402 if (! NILP (buffer))
20404 CHECK_BUFFER (buffer);
20405 buf = XBUFFER (buffer);
20408 if (NILP (BVAR (buf, bidi_display_reordering))
20409 || NILP (BVAR (buf, enable_multibyte_characters))
20410 /* When we are loading loadup.el, the character property tables
20411 needed for bidi iteration are not yet available. */
20412 || !NILP (Vpurify_flag))
20413 return Qleft_to_right;
20414 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20415 return BVAR (buf, bidi_paragraph_direction);
20416 else
20418 /* Determine the direction from buffer text. We could try to
20419 use current_matrix if it is up to date, but this seems fast
20420 enough as it is. */
20421 struct bidi_it itb;
20422 ptrdiff_t pos = BUF_PT (buf);
20423 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20424 int c;
20425 void *itb_data = bidi_shelve_cache ();
20427 set_buffer_temp (buf);
20428 /* bidi_paragraph_init finds the base direction of the paragraph
20429 by searching forward from paragraph start. We need the base
20430 direction of the current or _previous_ paragraph, so we need
20431 to make sure we are within that paragraph. To that end, find
20432 the previous non-empty line. */
20433 if (pos >= ZV && pos > BEGV)
20434 DEC_BOTH (pos, bytepos);
20435 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20436 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20438 while ((c = FETCH_BYTE (bytepos)) == '\n'
20439 || c == ' ' || c == '\t' || c == '\f')
20441 if (bytepos <= BEGV_BYTE)
20442 break;
20443 bytepos--;
20444 pos--;
20446 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20447 bytepos--;
20449 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20450 itb.paragraph_dir = NEUTRAL_DIR;
20451 itb.string.s = NULL;
20452 itb.string.lstring = Qnil;
20453 itb.string.bufpos = 0;
20454 itb.string.from_disp_str = 0;
20455 itb.string.unibyte = 0;
20456 /* We have no window to use here for ignoring window-specific
20457 overlays. Using NULL for window pointer will cause
20458 compute_display_string_pos to use the current buffer. */
20459 itb.w = NULL;
20460 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20461 bidi_unshelve_cache (itb_data, 0);
20462 set_buffer_temp (old);
20463 switch (itb.paragraph_dir)
20465 case L2R:
20466 return Qleft_to_right;
20467 break;
20468 case R2L:
20469 return Qright_to_left;
20470 break;
20471 default:
20472 emacs_abort ();
20477 DEFUN ("move-point-visually", Fmove_point_visually,
20478 Smove_point_visually, 1, 1, 0,
20479 doc: /* Move point in the visual order in the specified DIRECTION.
20480 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20481 left.
20483 Value is the new character position of point. */)
20484 (Lisp_Object direction)
20486 struct window *w = XWINDOW (selected_window);
20487 struct buffer *b = XBUFFER (w->contents);
20488 struct glyph_row *row;
20489 int dir;
20490 Lisp_Object paragraph_dir;
20492 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20493 (!(ROW)->continued_p \
20494 && INTEGERP ((GLYPH)->object) \
20495 && (GLYPH)->type == CHAR_GLYPH \
20496 && (GLYPH)->u.ch == ' ' \
20497 && (GLYPH)->charpos >= 0 \
20498 && !(GLYPH)->avoid_cursor_p)
20500 CHECK_NUMBER (direction);
20501 dir = XINT (direction);
20502 if (dir > 0)
20503 dir = 1;
20504 else
20505 dir = -1;
20507 /* If current matrix is up-to-date, we can use the information
20508 recorded in the glyphs, at least as long as the goal is on the
20509 screen. */
20510 if (w->window_end_valid
20511 && !windows_or_buffers_changed
20512 && b
20513 && !b->clip_changed
20514 && !b->prevent_redisplay_optimizations_p
20515 && !window_outdated (w)
20516 && w->cursor.vpos >= 0
20517 && w->cursor.vpos < w->current_matrix->nrows
20518 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20520 struct glyph *g = row->glyphs[TEXT_AREA];
20521 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20522 struct glyph *gpt = g + w->cursor.hpos;
20524 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20526 if (BUFFERP (g->object) && g->charpos != PT)
20528 SET_PT (g->charpos);
20529 w->cursor.vpos = -1;
20530 return make_number (PT);
20532 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20534 ptrdiff_t new_pos;
20536 if (BUFFERP (gpt->object))
20538 new_pos = PT;
20539 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20540 new_pos += (row->reversed_p ? -dir : dir);
20541 else
20542 new_pos -= (row->reversed_p ? -dir : dir);;
20544 else if (BUFFERP (g->object))
20545 new_pos = g->charpos;
20546 else
20547 break;
20548 SET_PT (new_pos);
20549 w->cursor.vpos = -1;
20550 return make_number (PT);
20552 else if (ROW_GLYPH_NEWLINE_P (row, g))
20554 /* Glyphs inserted at the end of a non-empty line for
20555 positioning the cursor have zero charpos, so we must
20556 deduce the value of point by other means. */
20557 if (g->charpos > 0)
20558 SET_PT (g->charpos);
20559 else if (row->ends_at_zv_p && PT != ZV)
20560 SET_PT (ZV);
20561 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20562 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20563 else
20564 break;
20565 w->cursor.vpos = -1;
20566 return make_number (PT);
20569 if (g == e || INTEGERP (g->object))
20571 if (row->truncated_on_left_p || row->truncated_on_right_p)
20572 goto simulate_display;
20573 if (!row->reversed_p)
20574 row += dir;
20575 else
20576 row -= dir;
20577 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20578 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20579 goto simulate_display;
20581 if (dir > 0)
20583 if (row->reversed_p && !row->continued_p)
20585 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20586 w->cursor.vpos = -1;
20587 return make_number (PT);
20589 g = row->glyphs[TEXT_AREA];
20590 e = g + row->used[TEXT_AREA];
20591 for ( ; g < e; g++)
20593 if (BUFFERP (g->object)
20594 /* Empty lines have only one glyph, which stands
20595 for the newline, and whose charpos is the
20596 buffer position of the newline. */
20597 || ROW_GLYPH_NEWLINE_P (row, g)
20598 /* When the buffer ends in a newline, the line at
20599 EOB also has one glyph, but its charpos is -1. */
20600 || (row->ends_at_zv_p
20601 && !row->reversed_p
20602 && INTEGERP (g->object)
20603 && g->type == CHAR_GLYPH
20604 && g->u.ch == ' '))
20606 if (g->charpos > 0)
20607 SET_PT (g->charpos);
20608 else if (!row->reversed_p
20609 && row->ends_at_zv_p
20610 && PT != ZV)
20611 SET_PT (ZV);
20612 else
20613 continue;
20614 w->cursor.vpos = -1;
20615 return make_number (PT);
20619 else
20621 if (!row->reversed_p && !row->continued_p)
20623 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20624 w->cursor.vpos = -1;
20625 return make_number (PT);
20627 e = row->glyphs[TEXT_AREA];
20628 g = e + row->used[TEXT_AREA] - 1;
20629 for ( ; g >= e; g--)
20631 if (BUFFERP (g->object)
20632 || (ROW_GLYPH_NEWLINE_P (row, g)
20633 && g->charpos > 0)
20634 /* Empty R2L lines on GUI frames have the buffer
20635 position of the newline stored in the stretch
20636 glyph. */
20637 || g->type == STRETCH_GLYPH
20638 || (row->ends_at_zv_p
20639 && row->reversed_p
20640 && INTEGERP (g->object)
20641 && g->type == CHAR_GLYPH
20642 && g->u.ch == ' '))
20644 if (g->charpos > 0)
20645 SET_PT (g->charpos);
20646 else if (row->reversed_p
20647 && row->ends_at_zv_p
20648 && PT != ZV)
20649 SET_PT (ZV);
20650 else
20651 continue;
20652 w->cursor.vpos = -1;
20653 return make_number (PT);
20660 simulate_display:
20662 /* If we wind up here, we failed to move by using the glyphs, so we
20663 need to simulate display instead. */
20665 if (b)
20666 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20667 else
20668 paragraph_dir = Qleft_to_right;
20669 if (EQ (paragraph_dir, Qright_to_left))
20670 dir = -dir;
20671 if (PT <= BEGV && dir < 0)
20672 xsignal0 (Qbeginning_of_buffer);
20673 else if (PT >= ZV && dir > 0)
20674 xsignal0 (Qend_of_buffer);
20675 else
20677 struct text_pos pt;
20678 struct it it;
20679 int pt_x, target_x, pixel_width, pt_vpos;
20680 bool at_eol_p;
20681 bool overshoot_expected = false;
20682 bool target_is_eol_p = false;
20684 /* Setup the arena. */
20685 SET_TEXT_POS (pt, PT, PT_BYTE);
20686 start_display (&it, w, pt);
20688 if (it.cmp_it.id < 0
20689 && it.method == GET_FROM_STRING
20690 && it.area == TEXT_AREA
20691 && it.string_from_display_prop_p
20692 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20693 overshoot_expected = true;
20695 /* Find the X coordinate of point. We start from the beginning
20696 of this or previous line to make sure we are before point in
20697 the logical order (since the move_it_* functions can only
20698 move forward). */
20699 reseat:
20700 reseat_at_previous_visible_line_start (&it);
20701 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20702 if (IT_CHARPOS (it) != PT)
20704 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20705 -1, -1, -1, MOVE_TO_POS);
20706 /* If we missed point because the character there is
20707 displayed out of a display vector that has more than one
20708 glyph, retry expecting overshoot. */
20709 if (it.method == GET_FROM_DISPLAY_VECTOR
20710 && it.current.dpvec_index > 0
20711 && !overshoot_expected)
20713 overshoot_expected = true;
20714 goto reseat;
20716 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20717 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20719 pt_x = it.current_x;
20720 pt_vpos = it.vpos;
20721 if (dir > 0 || overshoot_expected)
20723 struct glyph_row *row = it.glyph_row;
20725 /* When point is at beginning of line, we don't have
20726 information about the glyph there loaded into struct
20727 it. Calling get_next_display_element fixes that. */
20728 if (pt_x == 0)
20729 get_next_display_element (&it);
20730 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20731 it.glyph_row = NULL;
20732 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20733 it.glyph_row = row;
20734 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20735 it, lest it will become out of sync with it's buffer
20736 position. */
20737 it.current_x = pt_x;
20739 else
20740 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20741 pixel_width = it.pixel_width;
20742 if (overshoot_expected && at_eol_p)
20743 pixel_width = 0;
20744 else if (pixel_width <= 0)
20745 pixel_width = 1;
20747 /* If there's a display string (or something similar) at point,
20748 we are actually at the glyph to the left of point, so we need
20749 to correct the X coordinate. */
20750 if (overshoot_expected)
20752 if (it.bidi_p)
20753 pt_x += pixel_width * it.bidi_it.scan_dir;
20754 else
20755 pt_x += pixel_width;
20758 /* Compute target X coordinate, either to the left or to the
20759 right of point. On TTY frames, all characters have the same
20760 pixel width of 1, so we can use that. On GUI frames we don't
20761 have an easy way of getting at the pixel width of the
20762 character to the left of point, so we use a different method
20763 of getting to that place. */
20764 if (dir > 0)
20765 target_x = pt_x + pixel_width;
20766 else
20767 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20769 /* Target X coordinate could be one line above or below the line
20770 of point, in which case we need to adjust the target X
20771 coordinate. Also, if moving to the left, we need to begin at
20772 the left edge of the point's screen line. */
20773 if (dir < 0)
20775 if (pt_x > 0)
20777 start_display (&it, w, pt);
20778 reseat_at_previous_visible_line_start (&it);
20779 it.current_x = it.current_y = it.hpos = 0;
20780 if (pt_vpos != 0)
20781 move_it_by_lines (&it, pt_vpos);
20783 else
20785 move_it_by_lines (&it, -1);
20786 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20787 target_is_eol_p = true;
20790 else
20792 if (at_eol_p
20793 || (target_x >= it.last_visible_x
20794 && it.line_wrap != TRUNCATE))
20796 if (pt_x > 0)
20797 move_it_by_lines (&it, 0);
20798 move_it_by_lines (&it, 1);
20799 target_x = 0;
20803 /* Move to the target X coordinate. */
20804 #ifdef HAVE_WINDOW_SYSTEM
20805 /* On GUI frames, as we don't know the X coordinate of the
20806 character to the left of point, moving point to the left
20807 requires walking, one grapheme cluster at a time, until we
20808 find ourself at a place immediately to the left of the
20809 character at point. */
20810 if (FRAME_WINDOW_P (it.f) && dir < 0)
20812 struct text_pos new_pos;
20813 enum move_it_result rc = MOVE_X_REACHED;
20815 if (it.current_x == 0)
20816 get_next_display_element (&it);
20817 if (it.what == IT_COMPOSITION)
20819 new_pos.charpos = it.cmp_it.charpos;
20820 new_pos.bytepos = -1;
20822 else
20823 new_pos = it.current.pos;
20825 while (it.current_x + it.pixel_width <= target_x
20826 && rc == MOVE_X_REACHED)
20828 int new_x = it.current_x + it.pixel_width;
20830 /* For composed characters, we want the position of the
20831 first character in the grapheme cluster (usually, the
20832 composition's base character), whereas it.current
20833 might give us the position of the _last_ one, e.g. if
20834 the composition is rendered in reverse due to bidi
20835 reordering. */
20836 if (it.what == IT_COMPOSITION)
20838 new_pos.charpos = it.cmp_it.charpos;
20839 new_pos.bytepos = -1;
20841 else
20842 new_pos = it.current.pos;
20843 if (new_x == it.current_x)
20844 new_x++;
20845 rc = move_it_in_display_line_to (&it, ZV, new_x,
20846 MOVE_TO_POS | MOVE_TO_X);
20847 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20848 break;
20850 /* The previous position we saw in the loop is the one we
20851 want. */
20852 if (new_pos.bytepos == -1)
20853 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20854 it.current.pos = new_pos;
20856 else
20857 #endif
20858 if (it.current_x != target_x)
20859 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20861 /* When lines are truncated, the above loop will stop at the
20862 window edge. But we want to get to the end of line, even if
20863 it is beyond the window edge; automatic hscroll will then
20864 scroll the window to show point as appropriate. */
20865 if (target_is_eol_p && it.line_wrap == TRUNCATE
20866 && get_next_display_element (&it))
20868 struct text_pos new_pos = it.current.pos;
20870 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20872 set_iterator_to_next (&it, 0);
20873 if (it.method == GET_FROM_BUFFER)
20874 new_pos = it.current.pos;
20875 if (!get_next_display_element (&it))
20876 break;
20879 it.current.pos = new_pos;
20882 /* If we ended up in a display string that covers point, move to
20883 buffer position to the right in the visual order. */
20884 if (dir > 0)
20886 while (IT_CHARPOS (it) == PT)
20888 set_iterator_to_next (&it, 0);
20889 if (!get_next_display_element (&it))
20890 break;
20894 /* Move point to that position. */
20895 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20898 return make_number (PT);
20900 #undef ROW_GLYPH_NEWLINE_P
20904 /***********************************************************************
20905 Menu Bar
20906 ***********************************************************************/
20908 /* Redisplay the menu bar in the frame for window W.
20910 The menu bar of X frames that don't have X toolkit support is
20911 displayed in a special window W->frame->menu_bar_window.
20913 The menu bar of terminal frames is treated specially as far as
20914 glyph matrices are concerned. Menu bar lines are not part of
20915 windows, so the update is done directly on the frame matrix rows
20916 for the menu bar. */
20918 static void
20919 display_menu_bar (struct window *w)
20921 struct frame *f = XFRAME (WINDOW_FRAME (w));
20922 struct it it;
20923 Lisp_Object items;
20924 int i;
20926 /* Don't do all this for graphical frames. */
20927 #ifdef HAVE_NTGUI
20928 if (FRAME_W32_P (f))
20929 return;
20930 #endif
20931 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20932 if (FRAME_X_P (f))
20933 return;
20934 #endif
20936 #ifdef HAVE_NS
20937 if (FRAME_NS_P (f))
20938 return;
20939 #endif /* HAVE_NS */
20941 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20942 eassert (!FRAME_WINDOW_P (f));
20943 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20944 it.first_visible_x = 0;
20945 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20946 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20947 if (FRAME_WINDOW_P (f))
20949 /* Menu bar lines are displayed in the desired matrix of the
20950 dummy window menu_bar_window. */
20951 struct window *menu_w;
20952 menu_w = XWINDOW (f->menu_bar_window);
20953 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20954 MENU_FACE_ID);
20955 it.first_visible_x = 0;
20956 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20958 else
20959 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20961 /* This is a TTY frame, i.e. character hpos/vpos are used as
20962 pixel x/y. */
20963 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20964 MENU_FACE_ID);
20965 it.first_visible_x = 0;
20966 it.last_visible_x = FRAME_COLS (f);
20969 /* FIXME: This should be controlled by a user option. See the
20970 comments in redisplay_tool_bar and display_mode_line about
20971 this. */
20972 it.paragraph_embedding = L2R;
20974 /* Clear all rows of the menu bar. */
20975 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20977 struct glyph_row *row = it.glyph_row + i;
20978 clear_glyph_row (row);
20979 row->enabled_p = true;
20980 row->full_width_p = 1;
20983 /* Display all items of the menu bar. */
20984 items = FRAME_MENU_BAR_ITEMS (it.f);
20985 for (i = 0; i < ASIZE (items); i += 4)
20987 Lisp_Object string;
20989 /* Stop at nil string. */
20990 string = AREF (items, i + 1);
20991 if (NILP (string))
20992 break;
20994 /* Remember where item was displayed. */
20995 ASET (items, i + 3, make_number (it.hpos));
20997 /* Display the item, pad with one space. */
20998 if (it.current_x < it.last_visible_x)
20999 display_string (NULL, string, Qnil, 0, 0, &it,
21000 SCHARS (string) + 1, 0, 0, -1);
21003 /* Fill out the line with spaces. */
21004 if (it.current_x < it.last_visible_x)
21005 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21007 /* Compute the total height of the lines. */
21008 compute_line_metrics (&it);
21011 /* Deep copy of a glyph row, including the glyphs. */
21012 static void
21013 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21015 struct glyph *pointers[1 + LAST_AREA];
21016 int to_used = to->used[TEXT_AREA];
21018 /* Save glyph pointers of TO. */
21019 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21021 /* Do a structure assignment. */
21022 *to = *from;
21024 /* Restore original glyph pointers of TO. */
21025 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21027 /* Copy the glyphs. */
21028 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21029 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21031 /* If we filled only part of the TO row, fill the rest with
21032 space_glyph (which will display as empty space). */
21033 if (to_used > from->used[TEXT_AREA])
21034 fill_up_frame_row_with_spaces (to, to_used);
21037 /* Display one menu item on a TTY, by overwriting the glyphs in the
21038 frame F's desired glyph matrix with glyphs produced from the menu
21039 item text. Called from term.c to display TTY drop-down menus one
21040 item at a time.
21042 ITEM_TEXT is the menu item text as a C string.
21044 FACE_ID is the face ID to be used for this menu item. FACE_ID
21045 could specify one of 3 faces: a face for an enabled item, a face
21046 for a disabled item, or a face for a selected item.
21048 X and Y are coordinates of the first glyph in the frame's desired
21049 matrix to be overwritten by the menu item. Since this is a TTY, Y
21050 is the zero-based number of the glyph row and X is the zero-based
21051 glyph number in the row, starting from left, where to start
21052 displaying the item.
21054 SUBMENU non-zero means this menu item drops down a submenu, which
21055 should be indicated by displaying a proper visual cue after the
21056 item text. */
21058 void
21059 display_tty_menu_item (const char *item_text, int width, int face_id,
21060 int x, int y, int submenu)
21062 struct it it;
21063 struct frame *f = SELECTED_FRAME ();
21064 struct window *w = XWINDOW (f->selected_window);
21065 int saved_used, saved_truncated, saved_width, saved_reversed;
21066 struct glyph_row *row;
21067 size_t item_len = strlen (item_text);
21069 eassert (FRAME_TERMCAP_P (f));
21071 /* Don't write beyond the matrix's last row. This can happen for
21072 TTY screens that are not high enough to show the entire menu.
21073 (This is actually a bit of defensive programming, as
21074 tty_menu_display already limits the number of menu items to one
21075 less than the number of screen lines.) */
21076 if (y >= f->desired_matrix->nrows)
21077 return;
21079 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21080 it.first_visible_x = 0;
21081 it.last_visible_x = FRAME_COLS (f) - 1;
21082 row = it.glyph_row;
21083 /* Start with the row contents from the current matrix. */
21084 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21085 saved_width = row->full_width_p;
21086 row->full_width_p = 1;
21087 saved_reversed = row->reversed_p;
21088 row->reversed_p = 0;
21089 row->enabled_p = true;
21091 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21092 desired face. */
21093 eassert (x < f->desired_matrix->matrix_w);
21094 it.current_x = it.hpos = x;
21095 it.current_y = it.vpos = y;
21096 saved_used = row->used[TEXT_AREA];
21097 saved_truncated = row->truncated_on_right_p;
21098 row->used[TEXT_AREA] = x;
21099 it.face_id = face_id;
21100 it.line_wrap = TRUNCATE;
21102 /* FIXME: This should be controlled by a user option. See the
21103 comments in redisplay_tool_bar and display_mode_line about this.
21104 Also, if paragraph_embedding could ever be R2L, changes will be
21105 needed to avoid shifting to the right the row characters in
21106 term.c:append_glyph. */
21107 it.paragraph_embedding = L2R;
21109 /* Pad with a space on the left. */
21110 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21111 width--;
21112 /* Display the menu item, pad with spaces to WIDTH. */
21113 if (submenu)
21115 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21116 item_len, 0, FRAME_COLS (f) - 1, -1);
21117 width -= item_len;
21118 /* Indicate with " >" that there's a submenu. */
21119 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21120 FRAME_COLS (f) - 1, -1);
21122 else
21123 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21124 width, 0, FRAME_COLS (f) - 1, -1);
21126 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21127 row->truncated_on_right_p = saved_truncated;
21128 row->hash = row_hash (row);
21129 row->full_width_p = saved_width;
21130 row->reversed_p = saved_reversed;
21133 /***********************************************************************
21134 Mode Line
21135 ***********************************************************************/
21137 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21138 FORCE is non-zero, redisplay mode lines unconditionally.
21139 Otherwise, redisplay only mode lines that are garbaged. Value is
21140 the number of windows whose mode lines were redisplayed. */
21142 static int
21143 redisplay_mode_lines (Lisp_Object window, bool force)
21145 int nwindows = 0;
21147 while (!NILP (window))
21149 struct window *w = XWINDOW (window);
21151 if (WINDOWP (w->contents))
21152 nwindows += redisplay_mode_lines (w->contents, force);
21153 else if (force
21154 || FRAME_GARBAGED_P (XFRAME (w->frame))
21155 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21157 struct text_pos lpoint;
21158 struct buffer *old = current_buffer;
21160 /* Set the window's buffer for the mode line display. */
21161 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21162 set_buffer_internal_1 (XBUFFER (w->contents));
21164 /* Point refers normally to the selected window. For any
21165 other window, set up appropriate value. */
21166 if (!EQ (window, selected_window))
21168 struct text_pos pt;
21170 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21171 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21174 /* Display mode lines. */
21175 clear_glyph_matrix (w->desired_matrix);
21176 if (display_mode_lines (w))
21177 ++nwindows;
21179 /* Restore old settings. */
21180 set_buffer_internal_1 (old);
21181 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21184 window = w->next;
21187 return nwindows;
21191 /* Display the mode and/or header line of window W. Value is the
21192 sum number of mode lines and header lines displayed. */
21194 static int
21195 display_mode_lines (struct window *w)
21197 Lisp_Object old_selected_window = selected_window;
21198 Lisp_Object old_selected_frame = selected_frame;
21199 Lisp_Object new_frame = w->frame;
21200 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21201 int n = 0;
21203 selected_frame = new_frame;
21204 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21205 or window's point, then we'd need select_window_1 here as well. */
21206 XSETWINDOW (selected_window, w);
21207 XFRAME (new_frame)->selected_window = selected_window;
21209 /* These will be set while the mode line specs are processed. */
21210 line_number_displayed = 0;
21211 w->column_number_displayed = -1;
21213 if (WINDOW_WANTS_MODELINE_P (w))
21215 struct window *sel_w = XWINDOW (old_selected_window);
21217 /* Select mode line face based on the real selected window. */
21218 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21219 BVAR (current_buffer, mode_line_format));
21220 ++n;
21223 if (WINDOW_WANTS_HEADER_LINE_P (w))
21225 display_mode_line (w, HEADER_LINE_FACE_ID,
21226 BVAR (current_buffer, header_line_format));
21227 ++n;
21230 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21231 selected_frame = old_selected_frame;
21232 selected_window = old_selected_window;
21233 if (n > 0)
21234 w->must_be_updated_p = true;
21235 return n;
21239 /* Display mode or header line of window W. FACE_ID specifies which
21240 line to display; it is either MODE_LINE_FACE_ID or
21241 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21242 display. Value is the pixel height of the mode/header line
21243 displayed. */
21245 static int
21246 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21248 struct it it;
21249 struct face *face;
21250 ptrdiff_t count = SPECPDL_INDEX ();
21252 init_iterator (&it, w, -1, -1, NULL, face_id);
21253 /* Don't extend on a previously drawn mode-line.
21254 This may happen if called from pos_visible_p. */
21255 it.glyph_row->enabled_p = false;
21256 prepare_desired_row (it.glyph_row);
21258 it.glyph_row->mode_line_p = 1;
21260 /* FIXME: This should be controlled by a user option. But
21261 supporting such an option is not trivial, since the mode line is
21262 made up of many separate strings. */
21263 it.paragraph_embedding = L2R;
21265 record_unwind_protect (unwind_format_mode_line,
21266 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21268 mode_line_target = MODE_LINE_DISPLAY;
21270 /* Temporarily make frame's keyboard the current kboard so that
21271 kboard-local variables in the mode_line_format will get the right
21272 values. */
21273 push_kboard (FRAME_KBOARD (it.f));
21274 record_unwind_save_match_data ();
21275 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21276 pop_kboard ();
21278 unbind_to (count, Qnil);
21280 /* Fill up with spaces. */
21281 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21283 compute_line_metrics (&it);
21284 it.glyph_row->full_width_p = 1;
21285 it.glyph_row->continued_p = 0;
21286 it.glyph_row->truncated_on_left_p = 0;
21287 it.glyph_row->truncated_on_right_p = 0;
21289 /* Make a 3D mode-line have a shadow at its right end. */
21290 face = FACE_FROM_ID (it.f, face_id);
21291 extend_face_to_end_of_line (&it);
21292 if (face->box != FACE_NO_BOX)
21294 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21295 + it.glyph_row->used[TEXT_AREA] - 1);
21296 last->right_box_line_p = 1;
21299 return it.glyph_row->height;
21302 /* Move element ELT in LIST to the front of LIST.
21303 Return the updated list. */
21305 static Lisp_Object
21306 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21308 register Lisp_Object tail, prev;
21309 register Lisp_Object tem;
21311 tail = list;
21312 prev = Qnil;
21313 while (CONSP (tail))
21315 tem = XCAR (tail);
21317 if (EQ (elt, tem))
21319 /* Splice out the link TAIL. */
21320 if (NILP (prev))
21321 list = XCDR (tail);
21322 else
21323 Fsetcdr (prev, XCDR (tail));
21325 /* Now make it the first. */
21326 Fsetcdr (tail, list);
21327 return tail;
21329 else
21330 prev = tail;
21331 tail = XCDR (tail);
21332 QUIT;
21335 /* Not found--return unchanged LIST. */
21336 return list;
21339 /* Contribute ELT to the mode line for window IT->w. How it
21340 translates into text depends on its data type.
21342 IT describes the display environment in which we display, as usual.
21344 DEPTH is the depth in recursion. It is used to prevent
21345 infinite recursion here.
21347 FIELD_WIDTH is the number of characters the display of ELT should
21348 occupy in the mode line, and PRECISION is the maximum number of
21349 characters to display from ELT's representation. See
21350 display_string for details.
21352 Returns the hpos of the end of the text generated by ELT.
21354 PROPS is a property list to add to any string we encounter.
21356 If RISKY is nonzero, remove (disregard) any properties in any string
21357 we encounter, and ignore :eval and :propertize.
21359 The global variable `mode_line_target' determines whether the
21360 output is passed to `store_mode_line_noprop',
21361 `store_mode_line_string', or `display_string'. */
21363 static int
21364 display_mode_element (struct it *it, int depth, int field_width, int precision,
21365 Lisp_Object elt, Lisp_Object props, int risky)
21367 int n = 0, field, prec;
21368 int literal = 0;
21370 tail_recurse:
21371 if (depth > 100)
21372 elt = build_string ("*too-deep*");
21374 depth++;
21376 switch (XTYPE (elt))
21378 case Lisp_String:
21380 /* A string: output it and check for %-constructs within it. */
21381 unsigned char c;
21382 ptrdiff_t offset = 0;
21384 if (SCHARS (elt) > 0
21385 && (!NILP (props) || risky))
21387 Lisp_Object oprops, aelt;
21388 oprops = Ftext_properties_at (make_number (0), elt);
21390 /* If the starting string's properties are not what
21391 we want, translate the string. Also, if the string
21392 is risky, do that anyway. */
21394 if (NILP (Fequal (props, oprops)) || risky)
21396 /* If the starting string has properties,
21397 merge the specified ones onto the existing ones. */
21398 if (! NILP (oprops) && !risky)
21400 Lisp_Object tem;
21402 oprops = Fcopy_sequence (oprops);
21403 tem = props;
21404 while (CONSP (tem))
21406 oprops = Fplist_put (oprops, XCAR (tem),
21407 XCAR (XCDR (tem)));
21408 tem = XCDR (XCDR (tem));
21410 props = oprops;
21413 aelt = Fassoc (elt, mode_line_proptrans_alist);
21414 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21416 /* AELT is what we want. Move it to the front
21417 without consing. */
21418 elt = XCAR (aelt);
21419 mode_line_proptrans_alist
21420 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21422 else
21424 Lisp_Object tem;
21426 /* If AELT has the wrong props, it is useless.
21427 so get rid of it. */
21428 if (! NILP (aelt))
21429 mode_line_proptrans_alist
21430 = Fdelq (aelt, mode_line_proptrans_alist);
21432 elt = Fcopy_sequence (elt);
21433 Fset_text_properties (make_number (0), Flength (elt),
21434 props, elt);
21435 /* Add this item to mode_line_proptrans_alist. */
21436 mode_line_proptrans_alist
21437 = Fcons (Fcons (elt, props),
21438 mode_line_proptrans_alist);
21439 /* Truncate mode_line_proptrans_alist
21440 to at most 50 elements. */
21441 tem = Fnthcdr (make_number (50),
21442 mode_line_proptrans_alist);
21443 if (! NILP (tem))
21444 XSETCDR (tem, Qnil);
21449 offset = 0;
21451 if (literal)
21453 prec = precision - n;
21454 switch (mode_line_target)
21456 case MODE_LINE_NOPROP:
21457 case MODE_LINE_TITLE:
21458 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21459 break;
21460 case MODE_LINE_STRING:
21461 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21462 break;
21463 case MODE_LINE_DISPLAY:
21464 n += display_string (NULL, elt, Qnil, 0, 0, it,
21465 0, prec, 0, STRING_MULTIBYTE (elt));
21466 break;
21469 break;
21472 /* Handle the non-literal case. */
21474 while ((precision <= 0 || n < precision)
21475 && SREF (elt, offset) != 0
21476 && (mode_line_target != MODE_LINE_DISPLAY
21477 || it->current_x < it->last_visible_x))
21479 ptrdiff_t last_offset = offset;
21481 /* Advance to end of string or next format specifier. */
21482 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21485 if (offset - 1 != last_offset)
21487 ptrdiff_t nchars, nbytes;
21489 /* Output to end of string or up to '%'. Field width
21490 is length of string. Don't output more than
21491 PRECISION allows us. */
21492 offset--;
21494 prec = c_string_width (SDATA (elt) + last_offset,
21495 offset - last_offset, precision - n,
21496 &nchars, &nbytes);
21498 switch (mode_line_target)
21500 case MODE_LINE_NOPROP:
21501 case MODE_LINE_TITLE:
21502 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21503 break;
21504 case MODE_LINE_STRING:
21506 ptrdiff_t bytepos = last_offset;
21507 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21508 ptrdiff_t endpos = (precision <= 0
21509 ? string_byte_to_char (elt, offset)
21510 : charpos + nchars);
21512 n += store_mode_line_string (NULL,
21513 Fsubstring (elt, make_number (charpos),
21514 make_number (endpos)),
21515 0, 0, 0, Qnil);
21517 break;
21518 case MODE_LINE_DISPLAY:
21520 ptrdiff_t bytepos = last_offset;
21521 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21523 if (precision <= 0)
21524 nchars = string_byte_to_char (elt, offset) - charpos;
21525 n += display_string (NULL, elt, Qnil, 0, charpos,
21526 it, 0, nchars, 0,
21527 STRING_MULTIBYTE (elt));
21529 break;
21532 else /* c == '%' */
21534 ptrdiff_t percent_position = offset;
21536 /* Get the specified minimum width. Zero means
21537 don't pad. */
21538 field = 0;
21539 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21540 field = field * 10 + c - '0';
21542 /* Don't pad beyond the total padding allowed. */
21543 if (field_width - n > 0 && field > field_width - n)
21544 field = field_width - n;
21546 /* Note that either PRECISION <= 0 or N < PRECISION. */
21547 prec = precision - n;
21549 if (c == 'M')
21550 n += display_mode_element (it, depth, field, prec,
21551 Vglobal_mode_string, props,
21552 risky);
21553 else if (c != 0)
21555 bool multibyte;
21556 ptrdiff_t bytepos, charpos;
21557 const char *spec;
21558 Lisp_Object string;
21560 bytepos = percent_position;
21561 charpos = (STRING_MULTIBYTE (elt)
21562 ? string_byte_to_char (elt, bytepos)
21563 : bytepos);
21564 spec = decode_mode_spec (it->w, c, field, &string);
21565 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21567 switch (mode_line_target)
21569 case MODE_LINE_NOPROP:
21570 case MODE_LINE_TITLE:
21571 n += store_mode_line_noprop (spec, field, prec);
21572 break;
21573 case MODE_LINE_STRING:
21575 Lisp_Object tem = build_string (spec);
21576 props = Ftext_properties_at (make_number (charpos), elt);
21577 /* Should only keep face property in props */
21578 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21580 break;
21581 case MODE_LINE_DISPLAY:
21583 int nglyphs_before, nwritten;
21585 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21586 nwritten = display_string (spec, string, elt,
21587 charpos, 0, it,
21588 field, prec, 0,
21589 multibyte);
21591 /* Assign to the glyphs written above the
21592 string where the `%x' came from, position
21593 of the `%'. */
21594 if (nwritten > 0)
21596 struct glyph *glyph
21597 = (it->glyph_row->glyphs[TEXT_AREA]
21598 + nglyphs_before);
21599 int i;
21601 for (i = 0; i < nwritten; ++i)
21603 glyph[i].object = elt;
21604 glyph[i].charpos = charpos;
21607 n += nwritten;
21610 break;
21613 else /* c == 0 */
21614 break;
21618 break;
21620 case Lisp_Symbol:
21621 /* A symbol: process the value of the symbol recursively
21622 as if it appeared here directly. Avoid error if symbol void.
21623 Special case: if value of symbol is a string, output the string
21624 literally. */
21626 register Lisp_Object tem;
21628 /* If the variable is not marked as risky to set
21629 then its contents are risky to use. */
21630 if (NILP (Fget (elt, Qrisky_local_variable)))
21631 risky = 1;
21633 tem = Fboundp (elt);
21634 if (!NILP (tem))
21636 tem = Fsymbol_value (elt);
21637 /* If value is a string, output that string literally:
21638 don't check for % within it. */
21639 if (STRINGP (tem))
21640 literal = 1;
21642 if (!EQ (tem, elt))
21644 /* Give up right away for nil or t. */
21645 elt = tem;
21646 goto tail_recurse;
21650 break;
21652 case Lisp_Cons:
21654 register Lisp_Object car, tem;
21656 /* A cons cell: five distinct cases.
21657 If first element is :eval or :propertize, do something special.
21658 If first element is a string or a cons, process all the elements
21659 and effectively concatenate them.
21660 If first element is a negative number, truncate displaying cdr to
21661 at most that many characters. If positive, pad (with spaces)
21662 to at least that many characters.
21663 If first element is a symbol, process the cadr or caddr recursively
21664 according to whether the symbol's value is non-nil or nil. */
21665 car = XCAR (elt);
21666 if (EQ (car, QCeval))
21668 /* An element of the form (:eval FORM) means evaluate FORM
21669 and use the result as mode line elements. */
21671 if (risky)
21672 break;
21674 if (CONSP (XCDR (elt)))
21676 Lisp_Object spec;
21677 spec = safe_eval (XCAR (XCDR (elt)));
21678 n += display_mode_element (it, depth, field_width - n,
21679 precision - n, spec, props,
21680 risky);
21683 else if (EQ (car, QCpropertize))
21685 /* An element of the form (:propertize ELT PROPS...)
21686 means display ELT but applying properties PROPS. */
21688 if (risky)
21689 break;
21691 if (CONSP (XCDR (elt)))
21692 n += display_mode_element (it, depth, field_width - n,
21693 precision - n, XCAR (XCDR (elt)),
21694 XCDR (XCDR (elt)), risky);
21696 else if (SYMBOLP (car))
21698 tem = Fboundp (car);
21699 elt = XCDR (elt);
21700 if (!CONSP (elt))
21701 goto invalid;
21702 /* elt is now the cdr, and we know it is a cons cell.
21703 Use its car if CAR has a non-nil value. */
21704 if (!NILP (tem))
21706 tem = Fsymbol_value (car);
21707 if (!NILP (tem))
21709 elt = XCAR (elt);
21710 goto tail_recurse;
21713 /* Symbol's value is nil (or symbol is unbound)
21714 Get the cddr of the original list
21715 and if possible find the caddr and use that. */
21716 elt = XCDR (elt);
21717 if (NILP (elt))
21718 break;
21719 else if (!CONSP (elt))
21720 goto invalid;
21721 elt = XCAR (elt);
21722 goto tail_recurse;
21724 else if (INTEGERP (car))
21726 register int lim = XINT (car);
21727 elt = XCDR (elt);
21728 if (lim < 0)
21730 /* Negative int means reduce maximum width. */
21731 if (precision <= 0)
21732 precision = -lim;
21733 else
21734 precision = min (precision, -lim);
21736 else if (lim > 0)
21738 /* Padding specified. Don't let it be more than
21739 current maximum. */
21740 if (precision > 0)
21741 lim = min (precision, lim);
21743 /* If that's more padding than already wanted, queue it.
21744 But don't reduce padding already specified even if
21745 that is beyond the current truncation point. */
21746 field_width = max (lim, field_width);
21748 goto tail_recurse;
21750 else if (STRINGP (car) || CONSP (car))
21752 Lisp_Object halftail = elt;
21753 int len = 0;
21755 while (CONSP (elt)
21756 && (precision <= 0 || n < precision))
21758 n += display_mode_element (it, depth,
21759 /* Do padding only after the last
21760 element in the list. */
21761 (! CONSP (XCDR (elt))
21762 ? field_width - n
21763 : 0),
21764 precision - n, XCAR (elt),
21765 props, risky);
21766 elt = XCDR (elt);
21767 len++;
21768 if ((len & 1) == 0)
21769 halftail = XCDR (halftail);
21770 /* Check for cycle. */
21771 if (EQ (halftail, elt))
21772 break;
21776 break;
21778 default:
21779 invalid:
21780 elt = build_string ("*invalid*");
21781 goto tail_recurse;
21784 /* Pad to FIELD_WIDTH. */
21785 if (field_width > 0 && n < field_width)
21787 switch (mode_line_target)
21789 case MODE_LINE_NOPROP:
21790 case MODE_LINE_TITLE:
21791 n += store_mode_line_noprop ("", field_width - n, 0);
21792 break;
21793 case MODE_LINE_STRING:
21794 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21795 break;
21796 case MODE_LINE_DISPLAY:
21797 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21798 0, 0, 0);
21799 break;
21803 return n;
21806 /* Store a mode-line string element in mode_line_string_list.
21808 If STRING is non-null, display that C string. Otherwise, the Lisp
21809 string LISP_STRING is displayed.
21811 FIELD_WIDTH is the minimum number of output glyphs to produce.
21812 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21813 with spaces. FIELD_WIDTH <= 0 means don't pad.
21815 PRECISION is the maximum number of characters to output from
21816 STRING. PRECISION <= 0 means don't truncate the string.
21818 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21819 properties to the string.
21821 PROPS are the properties to add to the string.
21822 The mode_line_string_face face property is always added to the string.
21825 static int
21826 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21827 int field_width, int precision, Lisp_Object props)
21829 ptrdiff_t len;
21830 int n = 0;
21832 if (string != NULL)
21834 len = strlen (string);
21835 if (precision > 0 && len > precision)
21836 len = precision;
21837 lisp_string = make_string (string, len);
21838 if (NILP (props))
21839 props = mode_line_string_face_prop;
21840 else if (!NILP (mode_line_string_face))
21842 Lisp_Object face = Fplist_get (props, Qface);
21843 props = Fcopy_sequence (props);
21844 if (NILP (face))
21845 face = mode_line_string_face;
21846 else
21847 face = list2 (face, mode_line_string_face);
21848 props = Fplist_put (props, Qface, face);
21850 Fadd_text_properties (make_number (0), make_number (len),
21851 props, lisp_string);
21853 else
21855 len = XFASTINT (Flength (lisp_string));
21856 if (precision > 0 && len > precision)
21858 len = precision;
21859 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21860 precision = -1;
21862 if (!NILP (mode_line_string_face))
21864 Lisp_Object face;
21865 if (NILP (props))
21866 props = Ftext_properties_at (make_number (0), lisp_string);
21867 face = Fplist_get (props, Qface);
21868 if (NILP (face))
21869 face = mode_line_string_face;
21870 else
21871 face = list2 (face, mode_line_string_face);
21872 props = list2 (Qface, face);
21873 if (copy_string)
21874 lisp_string = Fcopy_sequence (lisp_string);
21876 if (!NILP (props))
21877 Fadd_text_properties (make_number (0), make_number (len),
21878 props, lisp_string);
21881 if (len > 0)
21883 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21884 n += len;
21887 if (field_width > len)
21889 field_width -= len;
21890 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21891 if (!NILP (props))
21892 Fadd_text_properties (make_number (0), make_number (field_width),
21893 props, lisp_string);
21894 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21895 n += field_width;
21898 return n;
21902 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21903 1, 4, 0,
21904 doc: /* Format a string out of a mode line format specification.
21905 First arg FORMAT specifies the mode line format (see `mode-line-format'
21906 for details) to use.
21908 By default, the format is evaluated for the currently selected window.
21910 Optional second arg FACE specifies the face property to put on all
21911 characters for which no face is specified. The value nil means the
21912 default face. The value t means whatever face the window's mode line
21913 currently uses (either `mode-line' or `mode-line-inactive',
21914 depending on whether the window is the selected window or not).
21915 An integer value means the value string has no text
21916 properties.
21918 Optional third and fourth args WINDOW and BUFFER specify the window
21919 and buffer to use as the context for the formatting (defaults
21920 are the selected window and the WINDOW's buffer). */)
21921 (Lisp_Object format, Lisp_Object face,
21922 Lisp_Object window, Lisp_Object buffer)
21924 struct it it;
21925 int len;
21926 struct window *w;
21927 struct buffer *old_buffer = NULL;
21928 int face_id;
21929 int no_props = INTEGERP (face);
21930 ptrdiff_t count = SPECPDL_INDEX ();
21931 Lisp_Object str;
21932 int string_start = 0;
21934 w = decode_any_window (window);
21935 XSETWINDOW (window, w);
21937 if (NILP (buffer))
21938 buffer = w->contents;
21939 CHECK_BUFFER (buffer);
21941 /* Make formatting the modeline a non-op when noninteractive, otherwise
21942 there will be problems later caused by a partially initialized frame. */
21943 if (NILP (format) || noninteractive)
21944 return empty_unibyte_string;
21946 if (no_props)
21947 face = Qnil;
21949 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21950 : EQ (face, Qt) ? (EQ (window, selected_window)
21951 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21952 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21953 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21954 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21955 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21956 : DEFAULT_FACE_ID;
21958 old_buffer = current_buffer;
21960 /* Save things including mode_line_proptrans_alist,
21961 and set that to nil so that we don't alter the outer value. */
21962 record_unwind_protect (unwind_format_mode_line,
21963 format_mode_line_unwind_data
21964 (XFRAME (WINDOW_FRAME (w)),
21965 old_buffer, selected_window, 1));
21966 mode_line_proptrans_alist = Qnil;
21968 Fselect_window (window, Qt);
21969 set_buffer_internal_1 (XBUFFER (buffer));
21971 init_iterator (&it, w, -1, -1, NULL, face_id);
21973 if (no_props)
21975 mode_line_target = MODE_LINE_NOPROP;
21976 mode_line_string_face_prop = Qnil;
21977 mode_line_string_list = Qnil;
21978 string_start = MODE_LINE_NOPROP_LEN (0);
21980 else
21982 mode_line_target = MODE_LINE_STRING;
21983 mode_line_string_list = Qnil;
21984 mode_line_string_face = face;
21985 mode_line_string_face_prop
21986 = NILP (face) ? Qnil : list2 (Qface, face);
21989 push_kboard (FRAME_KBOARD (it.f));
21990 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21991 pop_kboard ();
21993 if (no_props)
21995 len = MODE_LINE_NOPROP_LEN (string_start);
21996 str = make_string (mode_line_noprop_buf + string_start, len);
21998 else
22000 mode_line_string_list = Fnreverse (mode_line_string_list);
22001 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22002 empty_unibyte_string);
22005 unbind_to (count, Qnil);
22006 return str;
22009 /* Write a null-terminated, right justified decimal representation of
22010 the positive integer D to BUF using a minimal field width WIDTH. */
22012 static void
22013 pint2str (register char *buf, register int width, register ptrdiff_t d)
22015 register char *p = buf;
22017 if (d <= 0)
22018 *p++ = '0';
22019 else
22021 while (d > 0)
22023 *p++ = d % 10 + '0';
22024 d /= 10;
22028 for (width -= (int) (p - buf); width > 0; --width)
22029 *p++ = ' ';
22030 *p-- = '\0';
22031 while (p > buf)
22033 d = *buf;
22034 *buf++ = *p;
22035 *p-- = d;
22039 /* Write a null-terminated, right justified decimal and "human
22040 readable" representation of the nonnegative integer D to BUF using
22041 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22043 static const char power_letter[] =
22045 0, /* no letter */
22046 'k', /* kilo */
22047 'M', /* mega */
22048 'G', /* giga */
22049 'T', /* tera */
22050 'P', /* peta */
22051 'E', /* exa */
22052 'Z', /* zetta */
22053 'Y' /* yotta */
22056 static void
22057 pint2hrstr (char *buf, int width, ptrdiff_t d)
22059 /* We aim to represent the nonnegative integer D as
22060 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22061 ptrdiff_t quotient = d;
22062 int remainder = 0;
22063 /* -1 means: do not use TENTHS. */
22064 int tenths = -1;
22065 int exponent = 0;
22067 /* Length of QUOTIENT.TENTHS as a string. */
22068 int length;
22070 char * psuffix;
22071 char * p;
22073 if (quotient >= 1000)
22075 /* Scale to the appropriate EXPONENT. */
22078 remainder = quotient % 1000;
22079 quotient /= 1000;
22080 exponent++;
22082 while (quotient >= 1000);
22084 /* Round to nearest and decide whether to use TENTHS or not. */
22085 if (quotient <= 9)
22087 tenths = remainder / 100;
22088 if (remainder % 100 >= 50)
22090 if (tenths < 9)
22091 tenths++;
22092 else
22094 quotient++;
22095 if (quotient == 10)
22096 tenths = -1;
22097 else
22098 tenths = 0;
22102 else
22103 if (remainder >= 500)
22105 if (quotient < 999)
22106 quotient++;
22107 else
22109 quotient = 1;
22110 exponent++;
22111 tenths = 0;
22116 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22117 if (tenths == -1 && quotient <= 99)
22118 if (quotient <= 9)
22119 length = 1;
22120 else
22121 length = 2;
22122 else
22123 length = 3;
22124 p = psuffix = buf + max (width, length);
22126 /* Print EXPONENT. */
22127 *psuffix++ = power_letter[exponent];
22128 *psuffix = '\0';
22130 /* Print TENTHS. */
22131 if (tenths >= 0)
22133 *--p = '0' + tenths;
22134 *--p = '.';
22137 /* Print QUOTIENT. */
22140 int digit = quotient % 10;
22141 *--p = '0' + digit;
22143 while ((quotient /= 10) != 0);
22145 /* Print leading spaces. */
22146 while (buf < p)
22147 *--p = ' ';
22150 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22151 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22152 type of CODING_SYSTEM. Return updated pointer into BUF. */
22154 static unsigned char invalid_eol_type[] = "(*invalid*)";
22156 static char *
22157 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22159 Lisp_Object val;
22160 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22161 const unsigned char *eol_str;
22162 int eol_str_len;
22163 /* The EOL conversion we are using. */
22164 Lisp_Object eoltype;
22166 val = CODING_SYSTEM_SPEC (coding_system);
22167 eoltype = Qnil;
22169 if (!VECTORP (val)) /* Not yet decided. */
22171 *buf++ = multibyte ? '-' : ' ';
22172 if (eol_flag)
22173 eoltype = eol_mnemonic_undecided;
22174 /* Don't mention EOL conversion if it isn't decided. */
22176 else
22178 Lisp_Object attrs;
22179 Lisp_Object eolvalue;
22181 attrs = AREF (val, 0);
22182 eolvalue = AREF (val, 2);
22184 *buf++ = multibyte
22185 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22186 : ' ';
22188 if (eol_flag)
22190 /* The EOL conversion that is normal on this system. */
22192 if (NILP (eolvalue)) /* Not yet decided. */
22193 eoltype = eol_mnemonic_undecided;
22194 else if (VECTORP (eolvalue)) /* Not yet decided. */
22195 eoltype = eol_mnemonic_undecided;
22196 else /* eolvalue is Qunix, Qdos, or Qmac. */
22197 eoltype = (EQ (eolvalue, Qunix)
22198 ? eol_mnemonic_unix
22199 : (EQ (eolvalue, Qdos) == 1
22200 ? eol_mnemonic_dos : eol_mnemonic_mac));
22204 if (eol_flag)
22206 /* Mention the EOL conversion if it is not the usual one. */
22207 if (STRINGP (eoltype))
22209 eol_str = SDATA (eoltype);
22210 eol_str_len = SBYTES (eoltype);
22212 else if (CHARACTERP (eoltype))
22214 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22215 int c = XFASTINT (eoltype);
22216 eol_str_len = CHAR_STRING (c, tmp);
22217 eol_str = tmp;
22219 else
22221 eol_str = invalid_eol_type;
22222 eol_str_len = sizeof (invalid_eol_type) - 1;
22224 memcpy (buf, eol_str, eol_str_len);
22225 buf += eol_str_len;
22228 return buf;
22231 /* Return a string for the output of a mode line %-spec for window W,
22232 generated by character C. FIELD_WIDTH > 0 means pad the string
22233 returned with spaces to that value. Return a Lisp string in
22234 *STRING if the resulting string is taken from that Lisp string.
22236 Note we operate on the current buffer for most purposes. */
22238 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22240 static const char *
22241 decode_mode_spec (struct window *w, register int c, int field_width,
22242 Lisp_Object *string)
22244 Lisp_Object obj;
22245 struct frame *f = XFRAME (WINDOW_FRAME (w));
22246 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22247 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22248 produce strings from numerical values, so limit preposterously
22249 large values of FIELD_WIDTH to avoid overrunning the buffer's
22250 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22251 bytes plus the terminating null. */
22252 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22253 struct buffer *b = current_buffer;
22255 obj = Qnil;
22256 *string = Qnil;
22258 switch (c)
22260 case '*':
22261 if (!NILP (BVAR (b, read_only)))
22262 return "%";
22263 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22264 return "*";
22265 return "-";
22267 case '+':
22268 /* This differs from %* only for a modified read-only buffer. */
22269 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22270 return "*";
22271 if (!NILP (BVAR (b, read_only)))
22272 return "%";
22273 return "-";
22275 case '&':
22276 /* This differs from %* in ignoring read-only-ness. */
22277 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22278 return "*";
22279 return "-";
22281 case '%':
22282 return "%";
22284 case '[':
22286 int i;
22287 char *p;
22289 if (command_loop_level > 5)
22290 return "[[[... ";
22291 p = decode_mode_spec_buf;
22292 for (i = 0; i < command_loop_level; i++)
22293 *p++ = '[';
22294 *p = 0;
22295 return decode_mode_spec_buf;
22298 case ']':
22300 int i;
22301 char *p;
22303 if (command_loop_level > 5)
22304 return " ...]]]";
22305 p = decode_mode_spec_buf;
22306 for (i = 0; i < command_loop_level; i++)
22307 *p++ = ']';
22308 *p = 0;
22309 return decode_mode_spec_buf;
22312 case '-':
22314 register int i;
22316 /* Let lots_of_dashes be a string of infinite length. */
22317 if (mode_line_target == MODE_LINE_NOPROP
22318 || mode_line_target == MODE_LINE_STRING)
22319 return "--";
22320 if (field_width <= 0
22321 || field_width > sizeof (lots_of_dashes))
22323 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22324 decode_mode_spec_buf[i] = '-';
22325 decode_mode_spec_buf[i] = '\0';
22326 return decode_mode_spec_buf;
22328 else
22329 return lots_of_dashes;
22332 case 'b':
22333 obj = BVAR (b, name);
22334 break;
22336 case 'c':
22337 /* %c and %l are ignored in `frame-title-format'.
22338 (In redisplay_internal, the frame title is drawn _before_ the
22339 windows are updated, so the stuff which depends on actual
22340 window contents (such as %l) may fail to render properly, or
22341 even crash emacs.) */
22342 if (mode_line_target == MODE_LINE_TITLE)
22343 return "";
22344 else
22346 ptrdiff_t col = current_column ();
22347 w->column_number_displayed = col;
22348 pint2str (decode_mode_spec_buf, width, col);
22349 return decode_mode_spec_buf;
22352 case 'e':
22353 #ifndef SYSTEM_MALLOC
22355 if (NILP (Vmemory_full))
22356 return "";
22357 else
22358 return "!MEM FULL! ";
22360 #else
22361 return "";
22362 #endif
22364 case 'F':
22365 /* %F displays the frame name. */
22366 if (!NILP (f->title))
22367 return SSDATA (f->title);
22368 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22369 return SSDATA (f->name);
22370 return "Emacs";
22372 case 'f':
22373 obj = BVAR (b, filename);
22374 break;
22376 case 'i':
22378 ptrdiff_t size = ZV - BEGV;
22379 pint2str (decode_mode_spec_buf, width, size);
22380 return decode_mode_spec_buf;
22383 case 'I':
22385 ptrdiff_t size = ZV - BEGV;
22386 pint2hrstr (decode_mode_spec_buf, width, size);
22387 return decode_mode_spec_buf;
22390 case 'l':
22392 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22393 ptrdiff_t topline, nlines, height;
22394 ptrdiff_t junk;
22396 /* %c and %l are ignored in `frame-title-format'. */
22397 if (mode_line_target == MODE_LINE_TITLE)
22398 return "";
22400 startpos = marker_position (w->start);
22401 startpos_byte = marker_byte_position (w->start);
22402 height = WINDOW_TOTAL_LINES (w);
22404 /* If we decided that this buffer isn't suitable for line numbers,
22405 don't forget that too fast. */
22406 if (w->base_line_pos == -1)
22407 goto no_value;
22409 /* If the buffer is very big, don't waste time. */
22410 if (INTEGERP (Vline_number_display_limit)
22411 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22413 w->base_line_pos = 0;
22414 w->base_line_number = 0;
22415 goto no_value;
22418 if (w->base_line_number > 0
22419 && w->base_line_pos > 0
22420 && w->base_line_pos <= startpos)
22422 line = w->base_line_number;
22423 linepos = w->base_line_pos;
22424 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22426 else
22428 line = 1;
22429 linepos = BUF_BEGV (b);
22430 linepos_byte = BUF_BEGV_BYTE (b);
22433 /* Count lines from base line to window start position. */
22434 nlines = display_count_lines (linepos_byte,
22435 startpos_byte,
22436 startpos, &junk);
22438 topline = nlines + line;
22440 /* Determine a new base line, if the old one is too close
22441 or too far away, or if we did not have one.
22442 "Too close" means it's plausible a scroll-down would
22443 go back past it. */
22444 if (startpos == BUF_BEGV (b))
22446 w->base_line_number = topline;
22447 w->base_line_pos = BUF_BEGV (b);
22449 else if (nlines < height + 25 || nlines > height * 3 + 50
22450 || linepos == BUF_BEGV (b))
22452 ptrdiff_t limit = BUF_BEGV (b);
22453 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22454 ptrdiff_t position;
22455 ptrdiff_t distance =
22456 (height * 2 + 30) * line_number_display_limit_width;
22458 if (startpos - distance > limit)
22460 limit = startpos - distance;
22461 limit_byte = CHAR_TO_BYTE (limit);
22464 nlines = display_count_lines (startpos_byte,
22465 limit_byte,
22466 - (height * 2 + 30),
22467 &position);
22468 /* If we couldn't find the lines we wanted within
22469 line_number_display_limit_width chars per line,
22470 give up on line numbers for this window. */
22471 if (position == limit_byte && limit == startpos - distance)
22473 w->base_line_pos = -1;
22474 w->base_line_number = 0;
22475 goto no_value;
22478 w->base_line_number = topline - nlines;
22479 w->base_line_pos = BYTE_TO_CHAR (position);
22482 /* Now count lines from the start pos to point. */
22483 nlines = display_count_lines (startpos_byte,
22484 PT_BYTE, PT, &junk);
22486 /* Record that we did display the line number. */
22487 line_number_displayed = 1;
22489 /* Make the string to show. */
22490 pint2str (decode_mode_spec_buf, width, topline + nlines);
22491 return decode_mode_spec_buf;
22492 no_value:
22494 char* p = decode_mode_spec_buf;
22495 int pad = width - 2;
22496 while (pad-- > 0)
22497 *p++ = ' ';
22498 *p++ = '?';
22499 *p++ = '?';
22500 *p = '\0';
22501 return decode_mode_spec_buf;
22504 break;
22506 case 'm':
22507 obj = BVAR (b, mode_name);
22508 break;
22510 case 'n':
22511 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22512 return " Narrow";
22513 break;
22515 case 'p':
22517 ptrdiff_t pos = marker_position (w->start);
22518 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22520 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22522 if (pos <= BUF_BEGV (b))
22523 return "All";
22524 else
22525 return "Bottom";
22527 else if (pos <= BUF_BEGV (b))
22528 return "Top";
22529 else
22531 if (total > 1000000)
22532 /* Do it differently for a large value, to avoid overflow. */
22533 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22534 else
22535 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22536 /* We can't normally display a 3-digit number,
22537 so get us a 2-digit number that is close. */
22538 if (total == 100)
22539 total = 99;
22540 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22541 return decode_mode_spec_buf;
22545 /* Display percentage of size above the bottom of the screen. */
22546 case 'P':
22548 ptrdiff_t toppos = marker_position (w->start);
22549 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22550 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22552 if (botpos >= BUF_ZV (b))
22554 if (toppos <= BUF_BEGV (b))
22555 return "All";
22556 else
22557 return "Bottom";
22559 else
22561 if (total > 1000000)
22562 /* Do it differently for a large value, to avoid overflow. */
22563 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22564 else
22565 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22566 /* We can't normally display a 3-digit number,
22567 so get us a 2-digit number that is close. */
22568 if (total == 100)
22569 total = 99;
22570 if (toppos <= BUF_BEGV (b))
22571 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22572 else
22573 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22574 return decode_mode_spec_buf;
22578 case 's':
22579 /* status of process */
22580 obj = Fget_buffer_process (Fcurrent_buffer ());
22581 if (NILP (obj))
22582 return "no process";
22583 #ifndef MSDOS
22584 obj = Fsymbol_name (Fprocess_status (obj));
22585 #endif
22586 break;
22588 case '@':
22590 ptrdiff_t count = inhibit_garbage_collection ();
22591 Lisp_Object val = call1 (intern ("file-remote-p"),
22592 BVAR (current_buffer, directory));
22593 unbind_to (count, Qnil);
22595 if (NILP (val))
22596 return "-";
22597 else
22598 return "@";
22601 case 'z':
22602 /* coding-system (not including end-of-line format) */
22603 case 'Z':
22604 /* coding-system (including end-of-line type) */
22606 int eol_flag = (c == 'Z');
22607 char *p = decode_mode_spec_buf;
22609 if (! FRAME_WINDOW_P (f))
22611 /* No need to mention EOL here--the terminal never needs
22612 to do EOL conversion. */
22613 p = decode_mode_spec_coding (CODING_ID_NAME
22614 (FRAME_KEYBOARD_CODING (f)->id),
22615 p, 0);
22616 p = decode_mode_spec_coding (CODING_ID_NAME
22617 (FRAME_TERMINAL_CODING (f)->id),
22618 p, 0);
22620 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22621 p, eol_flag);
22623 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22624 #ifdef subprocesses
22625 obj = Fget_buffer_process (Fcurrent_buffer ());
22626 if (PROCESSP (obj))
22628 p = decode_mode_spec_coding
22629 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22630 p = decode_mode_spec_coding
22631 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22633 #endif /* subprocesses */
22634 #endif /* 0 */
22635 *p = 0;
22636 return decode_mode_spec_buf;
22640 if (STRINGP (obj))
22642 *string = obj;
22643 return SSDATA (obj);
22645 else
22646 return "";
22650 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22651 means count lines back from START_BYTE. But don't go beyond
22652 LIMIT_BYTE. Return the number of lines thus found (always
22653 nonnegative).
22655 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22656 either the position COUNT lines after/before START_BYTE, if we
22657 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22658 COUNT lines. */
22660 static ptrdiff_t
22661 display_count_lines (ptrdiff_t start_byte,
22662 ptrdiff_t limit_byte, ptrdiff_t count,
22663 ptrdiff_t *byte_pos_ptr)
22665 register unsigned char *cursor;
22666 unsigned char *base;
22668 register ptrdiff_t ceiling;
22669 register unsigned char *ceiling_addr;
22670 ptrdiff_t orig_count = count;
22672 /* If we are not in selective display mode,
22673 check only for newlines. */
22674 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22675 && !INTEGERP (BVAR (current_buffer, selective_display)));
22677 if (count > 0)
22679 while (start_byte < limit_byte)
22681 ceiling = BUFFER_CEILING_OF (start_byte);
22682 ceiling = min (limit_byte - 1, ceiling);
22683 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22684 base = (cursor = BYTE_POS_ADDR (start_byte));
22688 if (selective_display)
22690 while (*cursor != '\n' && *cursor != 015
22691 && ++cursor != ceiling_addr)
22692 continue;
22693 if (cursor == ceiling_addr)
22694 break;
22696 else
22698 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22699 if (! cursor)
22700 break;
22703 cursor++;
22705 if (--count == 0)
22707 start_byte += cursor - base;
22708 *byte_pos_ptr = start_byte;
22709 return orig_count;
22712 while (cursor < ceiling_addr);
22714 start_byte += ceiling_addr - base;
22717 else
22719 while (start_byte > limit_byte)
22721 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22722 ceiling = max (limit_byte, ceiling);
22723 ceiling_addr = BYTE_POS_ADDR (ceiling);
22724 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22725 while (1)
22727 if (selective_display)
22729 while (--cursor >= ceiling_addr
22730 && *cursor != '\n' && *cursor != 015)
22731 continue;
22732 if (cursor < ceiling_addr)
22733 break;
22735 else
22737 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22738 if (! cursor)
22739 break;
22742 if (++count == 0)
22744 start_byte += cursor - base + 1;
22745 *byte_pos_ptr = start_byte;
22746 /* When scanning backwards, we should
22747 not count the newline posterior to which we stop. */
22748 return - orig_count - 1;
22751 start_byte += ceiling_addr - base;
22755 *byte_pos_ptr = limit_byte;
22757 if (count < 0)
22758 return - orig_count + count;
22759 return orig_count - count;
22765 /***********************************************************************
22766 Displaying strings
22767 ***********************************************************************/
22769 /* Display a NUL-terminated string, starting with index START.
22771 If STRING is non-null, display that C string. Otherwise, the Lisp
22772 string LISP_STRING is displayed. There's a case that STRING is
22773 non-null and LISP_STRING is not nil. It means STRING is a string
22774 data of LISP_STRING. In that case, we display LISP_STRING while
22775 ignoring its text properties.
22777 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22778 FACE_STRING. Display STRING or LISP_STRING with the face at
22779 FACE_STRING_POS in FACE_STRING:
22781 Display the string in the environment given by IT, but use the
22782 standard display table, temporarily.
22784 FIELD_WIDTH is the minimum number of output glyphs to produce.
22785 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22786 with spaces. If STRING has more characters, more than FIELD_WIDTH
22787 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22789 PRECISION is the maximum number of characters to output from
22790 STRING. PRECISION < 0 means don't truncate the string.
22792 This is roughly equivalent to printf format specifiers:
22794 FIELD_WIDTH PRECISION PRINTF
22795 ----------------------------------------
22796 -1 -1 %s
22797 -1 10 %.10s
22798 10 -1 %10s
22799 20 10 %20.10s
22801 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22802 display them, and < 0 means obey the current buffer's value of
22803 enable_multibyte_characters.
22805 Value is the number of columns displayed. */
22807 static int
22808 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22809 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22810 int field_width, int precision, int max_x, int multibyte)
22812 int hpos_at_start = it->hpos;
22813 int saved_face_id = it->face_id;
22814 struct glyph_row *row = it->glyph_row;
22815 ptrdiff_t it_charpos;
22817 /* Initialize the iterator IT for iteration over STRING beginning
22818 with index START. */
22819 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22820 precision, field_width, multibyte);
22821 if (string && STRINGP (lisp_string))
22822 /* LISP_STRING is the one returned by decode_mode_spec. We should
22823 ignore its text properties. */
22824 it->stop_charpos = it->end_charpos;
22826 /* If displaying STRING, set up the face of the iterator from
22827 FACE_STRING, if that's given. */
22828 if (STRINGP (face_string))
22830 ptrdiff_t endptr;
22831 struct face *face;
22833 it->face_id
22834 = face_at_string_position (it->w, face_string, face_string_pos,
22835 0, &endptr, it->base_face_id, 0);
22836 face = FACE_FROM_ID (it->f, it->face_id);
22837 it->face_box_p = face->box != FACE_NO_BOX;
22840 /* Set max_x to the maximum allowed X position. Don't let it go
22841 beyond the right edge of the window. */
22842 if (max_x <= 0)
22843 max_x = it->last_visible_x;
22844 else
22845 max_x = min (max_x, it->last_visible_x);
22847 /* Skip over display elements that are not visible. because IT->w is
22848 hscrolled. */
22849 if (it->current_x < it->first_visible_x)
22850 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22851 MOVE_TO_POS | MOVE_TO_X);
22853 row->ascent = it->max_ascent;
22854 row->height = it->max_ascent + it->max_descent;
22855 row->phys_ascent = it->max_phys_ascent;
22856 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22857 row->extra_line_spacing = it->max_extra_line_spacing;
22859 if (STRINGP (it->string))
22860 it_charpos = IT_STRING_CHARPOS (*it);
22861 else
22862 it_charpos = IT_CHARPOS (*it);
22864 /* This condition is for the case that we are called with current_x
22865 past last_visible_x. */
22866 while (it->current_x < max_x)
22868 int x_before, x, n_glyphs_before, i, nglyphs;
22870 /* Get the next display element. */
22871 if (!get_next_display_element (it))
22872 break;
22874 /* Produce glyphs. */
22875 x_before = it->current_x;
22876 n_glyphs_before = row->used[TEXT_AREA];
22877 PRODUCE_GLYPHS (it);
22879 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22880 i = 0;
22881 x = x_before;
22882 while (i < nglyphs)
22884 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22886 if (it->line_wrap != TRUNCATE
22887 && x + glyph->pixel_width > max_x)
22889 /* End of continued line or max_x reached. */
22890 if (CHAR_GLYPH_PADDING_P (*glyph))
22892 /* A wide character is unbreakable. */
22893 if (row->reversed_p)
22894 unproduce_glyphs (it, row->used[TEXT_AREA]
22895 - n_glyphs_before);
22896 row->used[TEXT_AREA] = n_glyphs_before;
22897 it->current_x = x_before;
22899 else
22901 if (row->reversed_p)
22902 unproduce_glyphs (it, row->used[TEXT_AREA]
22903 - (n_glyphs_before + i));
22904 row->used[TEXT_AREA] = n_glyphs_before + i;
22905 it->current_x = x;
22907 break;
22909 else if (x + glyph->pixel_width >= it->first_visible_x)
22911 /* Glyph is at least partially visible. */
22912 ++it->hpos;
22913 if (x < it->first_visible_x)
22914 row->x = x - it->first_visible_x;
22916 else
22918 /* Glyph is off the left margin of the display area.
22919 Should not happen. */
22920 emacs_abort ();
22923 row->ascent = max (row->ascent, it->max_ascent);
22924 row->height = max (row->height, it->max_ascent + it->max_descent);
22925 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22926 row->phys_height = max (row->phys_height,
22927 it->max_phys_ascent + it->max_phys_descent);
22928 row->extra_line_spacing = max (row->extra_line_spacing,
22929 it->max_extra_line_spacing);
22930 x += glyph->pixel_width;
22931 ++i;
22934 /* Stop if max_x reached. */
22935 if (i < nglyphs)
22936 break;
22938 /* Stop at line ends. */
22939 if (ITERATOR_AT_END_OF_LINE_P (it))
22941 it->continuation_lines_width = 0;
22942 break;
22945 set_iterator_to_next (it, 1);
22946 if (STRINGP (it->string))
22947 it_charpos = IT_STRING_CHARPOS (*it);
22948 else
22949 it_charpos = IT_CHARPOS (*it);
22951 /* Stop if truncating at the right edge. */
22952 if (it->line_wrap == TRUNCATE
22953 && it->current_x >= it->last_visible_x)
22955 /* Add truncation mark, but don't do it if the line is
22956 truncated at a padding space. */
22957 if (it_charpos < it->string_nchars)
22959 if (!FRAME_WINDOW_P (it->f))
22961 int ii, n;
22963 if (it->current_x > it->last_visible_x)
22965 if (!row->reversed_p)
22967 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22968 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22969 break;
22971 else
22973 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22974 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22975 break;
22976 unproduce_glyphs (it, ii + 1);
22977 ii = row->used[TEXT_AREA] - (ii + 1);
22979 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22981 row->used[TEXT_AREA] = ii;
22982 produce_special_glyphs (it, IT_TRUNCATION);
22985 produce_special_glyphs (it, IT_TRUNCATION);
22987 row->truncated_on_right_p = 1;
22989 break;
22993 /* Maybe insert a truncation at the left. */
22994 if (it->first_visible_x
22995 && it_charpos > 0)
22997 if (!FRAME_WINDOW_P (it->f)
22998 || (row->reversed_p
22999 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23000 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23001 insert_left_trunc_glyphs (it);
23002 row->truncated_on_left_p = 1;
23005 it->face_id = saved_face_id;
23007 /* Value is number of columns displayed. */
23008 return it->hpos - hpos_at_start;
23013 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23014 appears as an element of LIST or as the car of an element of LIST.
23015 If PROPVAL is a list, compare each element against LIST in that
23016 way, and return 1/2 if any element of PROPVAL is found in LIST.
23017 Otherwise return 0. This function cannot quit.
23018 The return value is 2 if the text is invisible but with an ellipsis
23019 and 1 if it's invisible and without an ellipsis. */
23022 invisible_p (register Lisp_Object propval, Lisp_Object list)
23024 register Lisp_Object tail, proptail;
23026 for (tail = list; CONSP (tail); tail = XCDR (tail))
23028 register Lisp_Object tem;
23029 tem = XCAR (tail);
23030 if (EQ (propval, tem))
23031 return 1;
23032 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23033 return NILP (XCDR (tem)) ? 1 : 2;
23036 if (CONSP (propval))
23038 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23040 Lisp_Object propelt;
23041 propelt = XCAR (proptail);
23042 for (tail = list; CONSP (tail); tail = XCDR (tail))
23044 register Lisp_Object tem;
23045 tem = XCAR (tail);
23046 if (EQ (propelt, tem))
23047 return 1;
23048 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23049 return NILP (XCDR (tem)) ? 1 : 2;
23054 return 0;
23057 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23058 doc: /* Non-nil if the property makes the text invisible.
23059 POS-OR-PROP can be a marker or number, in which case it is taken to be
23060 a position in the current buffer and the value of the `invisible' property
23061 is checked; or it can be some other value, which is then presumed to be the
23062 value of the `invisible' property of the text of interest.
23063 The non-nil value returned can be t for truly invisible text or something
23064 else if the text is replaced by an ellipsis. */)
23065 (Lisp_Object pos_or_prop)
23067 Lisp_Object prop
23068 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23069 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23070 : pos_or_prop);
23071 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23072 return (invis == 0 ? Qnil
23073 : invis == 1 ? Qt
23074 : make_number (invis));
23077 /* Calculate a width or height in pixels from a specification using
23078 the following elements:
23080 SPEC ::=
23081 NUM - a (fractional) multiple of the default font width/height
23082 (NUM) - specifies exactly NUM pixels
23083 UNIT - a fixed number of pixels, see below.
23084 ELEMENT - size of a display element in pixels, see below.
23085 (NUM . SPEC) - equals NUM * SPEC
23086 (+ SPEC SPEC ...) - add pixel values
23087 (- SPEC SPEC ...) - subtract pixel values
23088 (- SPEC) - negate pixel value
23090 NUM ::=
23091 INT or FLOAT - a number constant
23092 SYMBOL - use symbol's (buffer local) variable binding.
23094 UNIT ::=
23095 in - pixels per inch *)
23096 mm - pixels per 1/1000 meter *)
23097 cm - pixels per 1/100 meter *)
23098 width - width of current font in pixels.
23099 height - height of current font in pixels.
23101 *) using the ratio(s) defined in display-pixels-per-inch.
23103 ELEMENT ::=
23105 left-fringe - left fringe width in pixels
23106 right-fringe - right fringe width in pixels
23108 left-margin - left margin width in pixels
23109 right-margin - right margin width in pixels
23111 scroll-bar - scroll-bar area width in pixels
23113 Examples:
23115 Pixels corresponding to 5 inches:
23116 (5 . in)
23118 Total width of non-text areas on left side of window (if scroll-bar is on left):
23119 '(space :width (+ left-fringe left-margin scroll-bar))
23121 Align to first text column (in header line):
23122 '(space :align-to 0)
23124 Align to middle of text area minus half the width of variable `my-image'
23125 containing a loaded image:
23126 '(space :align-to (0.5 . (- text my-image)))
23128 Width of left margin minus width of 1 character in the default font:
23129 '(space :width (- left-margin 1))
23131 Width of left margin minus width of 2 characters in the current font:
23132 '(space :width (- left-margin (2 . width)))
23134 Center 1 character over left-margin (in header line):
23135 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23137 Different ways to express width of left fringe plus left margin minus one pixel:
23138 '(space :width (- (+ left-fringe left-margin) (1)))
23139 '(space :width (+ left-fringe left-margin (- (1))))
23140 '(space :width (+ left-fringe left-margin (-1)))
23144 static int
23145 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23146 struct font *font, int width_p, int *align_to)
23148 double pixels;
23150 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23151 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23153 if (NILP (prop))
23154 return OK_PIXELS (0);
23156 eassert (FRAME_LIVE_P (it->f));
23158 if (SYMBOLP (prop))
23160 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23162 char *unit = SSDATA (SYMBOL_NAME (prop));
23164 if (unit[0] == 'i' && unit[1] == 'n')
23165 pixels = 1.0;
23166 else if (unit[0] == 'm' && unit[1] == 'm')
23167 pixels = 25.4;
23168 else if (unit[0] == 'c' && unit[1] == 'm')
23169 pixels = 2.54;
23170 else
23171 pixels = 0;
23172 if (pixels > 0)
23174 double ppi = (width_p ? FRAME_RES_X (it->f)
23175 : FRAME_RES_Y (it->f));
23177 if (ppi > 0)
23178 return OK_PIXELS (ppi / pixels);
23179 return 0;
23183 #ifdef HAVE_WINDOW_SYSTEM
23184 if (EQ (prop, Qheight))
23185 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23186 if (EQ (prop, Qwidth))
23187 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23188 #else
23189 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23190 return OK_PIXELS (1);
23191 #endif
23193 if (EQ (prop, Qtext))
23194 return OK_PIXELS (width_p
23195 ? window_box_width (it->w, TEXT_AREA)
23196 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23198 if (align_to && *align_to < 0)
23200 *res = 0;
23201 if (EQ (prop, Qleft))
23202 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23203 if (EQ (prop, Qright))
23204 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23205 if (EQ (prop, Qcenter))
23206 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23207 + window_box_width (it->w, TEXT_AREA) / 2);
23208 if (EQ (prop, Qleft_fringe))
23209 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23210 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23211 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23212 if (EQ (prop, Qright_fringe))
23213 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23214 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23215 : window_box_right_offset (it->w, TEXT_AREA));
23216 if (EQ (prop, Qleft_margin))
23217 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23218 if (EQ (prop, Qright_margin))
23219 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23220 if (EQ (prop, Qscroll_bar))
23221 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23223 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23224 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23225 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23226 : 0)));
23228 else
23230 if (EQ (prop, Qleft_fringe))
23231 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23232 if (EQ (prop, Qright_fringe))
23233 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23234 if (EQ (prop, Qleft_margin))
23235 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23236 if (EQ (prop, Qright_margin))
23237 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23238 if (EQ (prop, Qscroll_bar))
23239 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23242 prop = buffer_local_value_1 (prop, it->w->contents);
23243 if (EQ (prop, Qunbound))
23244 prop = Qnil;
23247 if (INTEGERP (prop) || FLOATP (prop))
23249 int base_unit = (width_p
23250 ? FRAME_COLUMN_WIDTH (it->f)
23251 : FRAME_LINE_HEIGHT (it->f));
23252 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23255 if (CONSP (prop))
23257 Lisp_Object car = XCAR (prop);
23258 Lisp_Object cdr = XCDR (prop);
23260 if (SYMBOLP (car))
23262 #ifdef HAVE_WINDOW_SYSTEM
23263 if (FRAME_WINDOW_P (it->f)
23264 && valid_image_p (prop))
23266 ptrdiff_t id = lookup_image (it->f, prop);
23267 struct image *img = IMAGE_FROM_ID (it->f, id);
23269 return OK_PIXELS (width_p ? img->width : img->height);
23271 #endif
23272 if (EQ (car, Qplus) || EQ (car, Qminus))
23274 int first = 1;
23275 double px;
23277 pixels = 0;
23278 while (CONSP (cdr))
23280 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23281 font, width_p, align_to))
23282 return 0;
23283 if (first)
23284 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23285 else
23286 pixels += px;
23287 cdr = XCDR (cdr);
23289 if (EQ (car, Qminus))
23290 pixels = -pixels;
23291 return OK_PIXELS (pixels);
23294 car = buffer_local_value_1 (car, it->w->contents);
23295 if (EQ (car, Qunbound))
23296 car = Qnil;
23299 if (INTEGERP (car) || FLOATP (car))
23301 double fact;
23302 pixels = XFLOATINT (car);
23303 if (NILP (cdr))
23304 return OK_PIXELS (pixels);
23305 if (calc_pixel_width_or_height (&fact, it, cdr,
23306 font, width_p, align_to))
23307 return OK_PIXELS (pixels * fact);
23308 return 0;
23311 return 0;
23314 return 0;
23318 /***********************************************************************
23319 Glyph Display
23320 ***********************************************************************/
23322 #ifdef HAVE_WINDOW_SYSTEM
23324 #ifdef GLYPH_DEBUG
23326 void
23327 dump_glyph_string (struct glyph_string *s)
23329 fprintf (stderr, "glyph string\n");
23330 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23331 s->x, s->y, s->width, s->height);
23332 fprintf (stderr, " ybase = %d\n", s->ybase);
23333 fprintf (stderr, " hl = %d\n", s->hl);
23334 fprintf (stderr, " left overhang = %d, right = %d\n",
23335 s->left_overhang, s->right_overhang);
23336 fprintf (stderr, " nchars = %d\n", s->nchars);
23337 fprintf (stderr, " extends to end of line = %d\n",
23338 s->extends_to_end_of_line_p);
23339 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23340 fprintf (stderr, " bg width = %d\n", s->background_width);
23343 #endif /* GLYPH_DEBUG */
23345 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23346 of XChar2b structures for S; it can't be allocated in
23347 init_glyph_string because it must be allocated via `alloca'. W
23348 is the window on which S is drawn. ROW and AREA are the glyph row
23349 and area within the row from which S is constructed. START is the
23350 index of the first glyph structure covered by S. HL is a
23351 face-override for drawing S. */
23353 #ifdef HAVE_NTGUI
23354 #define OPTIONAL_HDC(hdc) HDC hdc,
23355 #define DECLARE_HDC(hdc) HDC hdc;
23356 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23357 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23358 #endif
23360 #ifndef OPTIONAL_HDC
23361 #define OPTIONAL_HDC(hdc)
23362 #define DECLARE_HDC(hdc)
23363 #define ALLOCATE_HDC(hdc, f)
23364 #define RELEASE_HDC(hdc, f)
23365 #endif
23367 static void
23368 init_glyph_string (struct glyph_string *s,
23369 OPTIONAL_HDC (hdc)
23370 XChar2b *char2b, struct window *w, struct glyph_row *row,
23371 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23373 memset (s, 0, sizeof *s);
23374 s->w = w;
23375 s->f = XFRAME (w->frame);
23376 #ifdef HAVE_NTGUI
23377 s->hdc = hdc;
23378 #endif
23379 s->display = FRAME_X_DISPLAY (s->f);
23380 s->window = FRAME_X_WINDOW (s->f);
23381 s->char2b = char2b;
23382 s->hl = hl;
23383 s->row = row;
23384 s->area = area;
23385 s->first_glyph = row->glyphs[area] + start;
23386 s->height = row->height;
23387 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23388 s->ybase = s->y + row->ascent;
23392 /* Append the list of glyph strings with head H and tail T to the list
23393 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23395 static void
23396 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23397 struct glyph_string *h, struct glyph_string *t)
23399 if (h)
23401 if (*head)
23402 (*tail)->next = h;
23403 else
23404 *head = h;
23405 h->prev = *tail;
23406 *tail = t;
23411 /* Prepend the list of glyph strings with head H and tail T to the
23412 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23413 result. */
23415 static void
23416 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23417 struct glyph_string *h, struct glyph_string *t)
23419 if (h)
23421 if (*head)
23422 (*head)->prev = t;
23423 else
23424 *tail = t;
23425 t->next = *head;
23426 *head = h;
23431 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23432 Set *HEAD and *TAIL to the resulting list. */
23434 static void
23435 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23436 struct glyph_string *s)
23438 s->next = s->prev = NULL;
23439 append_glyph_string_lists (head, tail, s, s);
23443 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23444 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23445 make sure that X resources for the face returned are allocated.
23446 Value is a pointer to a realized face that is ready for display if
23447 DISPLAY_P is non-zero. */
23449 static struct face *
23450 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23451 XChar2b *char2b, int display_p)
23453 struct face *face = FACE_FROM_ID (f, face_id);
23454 unsigned code = 0;
23456 if (face->font)
23458 code = face->font->driver->encode_char (face->font, c);
23460 if (code == FONT_INVALID_CODE)
23461 code = 0;
23463 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23465 /* Make sure X resources of the face are allocated. */
23466 #ifdef HAVE_X_WINDOWS
23467 if (display_p)
23468 #endif
23470 eassert (face != NULL);
23471 PREPARE_FACE_FOR_DISPLAY (f, face);
23474 return face;
23478 /* Get face and two-byte form of character glyph GLYPH on frame F.
23479 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23480 a pointer to a realized face that is ready for display. */
23482 static struct face *
23483 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23484 XChar2b *char2b, int *two_byte_p)
23486 struct face *face;
23487 unsigned code = 0;
23489 eassert (glyph->type == CHAR_GLYPH);
23490 face = FACE_FROM_ID (f, glyph->face_id);
23492 /* Make sure X resources of the face are allocated. */
23493 eassert (face != NULL);
23494 PREPARE_FACE_FOR_DISPLAY (f, face);
23496 if (two_byte_p)
23497 *two_byte_p = 0;
23499 if (face->font)
23501 if (CHAR_BYTE8_P (glyph->u.ch))
23502 code = CHAR_TO_BYTE8 (glyph->u.ch);
23503 else
23504 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23506 if (code == FONT_INVALID_CODE)
23507 code = 0;
23510 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23511 return face;
23515 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23516 Return 1 if FONT has a glyph for C, otherwise return 0. */
23518 static int
23519 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23521 unsigned code;
23523 if (CHAR_BYTE8_P (c))
23524 code = CHAR_TO_BYTE8 (c);
23525 else
23526 code = font->driver->encode_char (font, c);
23528 if (code == FONT_INVALID_CODE)
23529 return 0;
23530 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23531 return 1;
23535 /* Fill glyph string S with composition components specified by S->cmp.
23537 BASE_FACE is the base face of the composition.
23538 S->cmp_from is the index of the first component for S.
23540 OVERLAPS non-zero means S should draw the foreground only, and use
23541 its physical height for clipping. See also draw_glyphs.
23543 Value is the index of a component not in S. */
23545 static int
23546 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23547 int overlaps)
23549 int i;
23550 /* For all glyphs of this composition, starting at the offset
23551 S->cmp_from, until we reach the end of the definition or encounter a
23552 glyph that requires the different face, add it to S. */
23553 struct face *face;
23555 eassert (s);
23557 s->for_overlaps = overlaps;
23558 s->face = NULL;
23559 s->font = NULL;
23560 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23562 int c = COMPOSITION_GLYPH (s->cmp, i);
23564 /* TAB in a composition means display glyphs with padding space
23565 on the left or right. */
23566 if (c != '\t')
23568 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23569 -1, Qnil);
23571 face = get_char_face_and_encoding (s->f, c, face_id,
23572 s->char2b + i, 1);
23573 if (face)
23575 if (! s->face)
23577 s->face = face;
23578 s->font = s->face->font;
23580 else if (s->face != face)
23581 break;
23584 ++s->nchars;
23586 s->cmp_to = i;
23588 if (s->face == NULL)
23590 s->face = base_face->ascii_face;
23591 s->font = s->face->font;
23594 /* All glyph strings for the same composition has the same width,
23595 i.e. the width set for the first component of the composition. */
23596 s->width = s->first_glyph->pixel_width;
23598 /* If the specified font could not be loaded, use the frame's
23599 default font, but record the fact that we couldn't load it in
23600 the glyph string so that we can draw rectangles for the
23601 characters of the glyph string. */
23602 if (s->font == NULL)
23604 s->font_not_found_p = 1;
23605 s->font = FRAME_FONT (s->f);
23608 /* Adjust base line for subscript/superscript text. */
23609 s->ybase += s->first_glyph->voffset;
23611 /* This glyph string must always be drawn with 16-bit functions. */
23612 s->two_byte_p = 1;
23614 return s->cmp_to;
23617 static int
23618 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23619 int start, int end, int overlaps)
23621 struct glyph *glyph, *last;
23622 Lisp_Object lgstring;
23623 int i;
23625 s->for_overlaps = overlaps;
23626 glyph = s->row->glyphs[s->area] + start;
23627 last = s->row->glyphs[s->area] + end;
23628 s->cmp_id = glyph->u.cmp.id;
23629 s->cmp_from = glyph->slice.cmp.from;
23630 s->cmp_to = glyph->slice.cmp.to + 1;
23631 s->face = FACE_FROM_ID (s->f, face_id);
23632 lgstring = composition_gstring_from_id (s->cmp_id);
23633 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23634 glyph++;
23635 while (glyph < last
23636 && glyph->u.cmp.automatic
23637 && glyph->u.cmp.id == s->cmp_id
23638 && s->cmp_to == glyph->slice.cmp.from)
23639 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23641 for (i = s->cmp_from; i < s->cmp_to; i++)
23643 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23644 unsigned code = LGLYPH_CODE (lglyph);
23646 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23648 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23649 return glyph - s->row->glyphs[s->area];
23653 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23654 See the comment of fill_glyph_string for arguments.
23655 Value is the index of the first glyph not in S. */
23658 static int
23659 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23660 int start, int end, int overlaps)
23662 struct glyph *glyph, *last;
23663 int voffset;
23665 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23666 s->for_overlaps = overlaps;
23667 glyph = s->row->glyphs[s->area] + start;
23668 last = s->row->glyphs[s->area] + end;
23669 voffset = glyph->voffset;
23670 s->face = FACE_FROM_ID (s->f, face_id);
23671 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23672 s->nchars = 1;
23673 s->width = glyph->pixel_width;
23674 glyph++;
23675 while (glyph < last
23676 && glyph->type == GLYPHLESS_GLYPH
23677 && glyph->voffset == voffset
23678 && glyph->face_id == face_id)
23680 s->nchars++;
23681 s->width += glyph->pixel_width;
23682 glyph++;
23684 s->ybase += voffset;
23685 return glyph - s->row->glyphs[s->area];
23689 /* Fill glyph string S from a sequence of character glyphs.
23691 FACE_ID is the face id of the string. START is the index of the
23692 first glyph to consider, END is the index of the last + 1.
23693 OVERLAPS non-zero means S should draw the foreground only, and use
23694 its physical height for clipping. See also draw_glyphs.
23696 Value is the index of the first glyph not in S. */
23698 static int
23699 fill_glyph_string (struct glyph_string *s, int face_id,
23700 int start, int end, int overlaps)
23702 struct glyph *glyph, *last;
23703 int voffset;
23704 int glyph_not_available_p;
23706 eassert (s->f == XFRAME (s->w->frame));
23707 eassert (s->nchars == 0);
23708 eassert (start >= 0 && end > start);
23710 s->for_overlaps = overlaps;
23711 glyph = s->row->glyphs[s->area] + start;
23712 last = s->row->glyphs[s->area] + end;
23713 voffset = glyph->voffset;
23714 s->padding_p = glyph->padding_p;
23715 glyph_not_available_p = glyph->glyph_not_available_p;
23717 while (glyph < last
23718 && glyph->type == CHAR_GLYPH
23719 && glyph->voffset == voffset
23720 /* Same face id implies same font, nowadays. */
23721 && glyph->face_id == face_id
23722 && glyph->glyph_not_available_p == glyph_not_available_p)
23724 int two_byte_p;
23726 s->face = get_glyph_face_and_encoding (s->f, glyph,
23727 s->char2b + s->nchars,
23728 &two_byte_p);
23729 s->two_byte_p = two_byte_p;
23730 ++s->nchars;
23731 eassert (s->nchars <= end - start);
23732 s->width += glyph->pixel_width;
23733 if (glyph++->padding_p != s->padding_p)
23734 break;
23737 s->font = s->face->font;
23739 /* If the specified font could not be loaded, use the frame's font,
23740 but record the fact that we couldn't load it in
23741 S->font_not_found_p so that we can draw rectangles for the
23742 characters of the glyph string. */
23743 if (s->font == NULL || glyph_not_available_p)
23745 s->font_not_found_p = 1;
23746 s->font = FRAME_FONT (s->f);
23749 /* Adjust base line for subscript/superscript text. */
23750 s->ybase += voffset;
23752 eassert (s->face && s->face->gc);
23753 return glyph - s->row->glyphs[s->area];
23757 /* Fill glyph string S from image glyph S->first_glyph. */
23759 static void
23760 fill_image_glyph_string (struct glyph_string *s)
23762 eassert (s->first_glyph->type == IMAGE_GLYPH);
23763 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23764 eassert (s->img);
23765 s->slice = s->first_glyph->slice.img;
23766 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23767 s->font = s->face->font;
23768 s->width = s->first_glyph->pixel_width;
23770 /* Adjust base line for subscript/superscript text. */
23771 s->ybase += s->first_glyph->voffset;
23775 /* Fill glyph string S from a sequence of stretch glyphs.
23777 START is the index of the first glyph to consider,
23778 END is the index of the last + 1.
23780 Value is the index of the first glyph not in S. */
23782 static int
23783 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23785 struct glyph *glyph, *last;
23786 int voffset, face_id;
23788 eassert (s->first_glyph->type == STRETCH_GLYPH);
23790 glyph = s->row->glyphs[s->area] + start;
23791 last = s->row->glyphs[s->area] + end;
23792 face_id = glyph->face_id;
23793 s->face = FACE_FROM_ID (s->f, face_id);
23794 s->font = s->face->font;
23795 s->width = glyph->pixel_width;
23796 s->nchars = 1;
23797 voffset = glyph->voffset;
23799 for (++glyph;
23800 (glyph < last
23801 && glyph->type == STRETCH_GLYPH
23802 && glyph->voffset == voffset
23803 && glyph->face_id == face_id);
23804 ++glyph)
23805 s->width += glyph->pixel_width;
23807 /* Adjust base line for subscript/superscript text. */
23808 s->ybase += voffset;
23810 /* The case that face->gc == 0 is handled when drawing the glyph
23811 string by calling PREPARE_FACE_FOR_DISPLAY. */
23812 eassert (s->face);
23813 return glyph - s->row->glyphs[s->area];
23816 static struct font_metrics *
23817 get_per_char_metric (struct font *font, XChar2b *char2b)
23819 static struct font_metrics metrics;
23820 unsigned code;
23822 if (! font)
23823 return NULL;
23824 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23825 if (code == FONT_INVALID_CODE)
23826 return NULL;
23827 font->driver->text_extents (font, &code, 1, &metrics);
23828 return &metrics;
23831 /* EXPORT for RIF:
23832 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23833 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23834 assumed to be zero. */
23836 void
23837 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23839 *left = *right = 0;
23841 if (glyph->type == CHAR_GLYPH)
23843 struct face *face;
23844 XChar2b char2b;
23845 struct font_metrics *pcm;
23847 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23848 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23850 if (pcm->rbearing > pcm->width)
23851 *right = pcm->rbearing - pcm->width;
23852 if (pcm->lbearing < 0)
23853 *left = -pcm->lbearing;
23856 else if (glyph->type == COMPOSITE_GLYPH)
23858 if (! glyph->u.cmp.automatic)
23860 struct composition *cmp = composition_table[glyph->u.cmp.id];
23862 if (cmp->rbearing > cmp->pixel_width)
23863 *right = cmp->rbearing - cmp->pixel_width;
23864 if (cmp->lbearing < 0)
23865 *left = - cmp->lbearing;
23867 else
23869 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23870 struct font_metrics metrics;
23872 composition_gstring_width (gstring, glyph->slice.cmp.from,
23873 glyph->slice.cmp.to + 1, &metrics);
23874 if (metrics.rbearing > metrics.width)
23875 *right = metrics.rbearing - metrics.width;
23876 if (metrics.lbearing < 0)
23877 *left = - metrics.lbearing;
23883 /* Return the index of the first glyph preceding glyph string S that
23884 is overwritten by S because of S's left overhang. Value is -1
23885 if no glyphs are overwritten. */
23887 static int
23888 left_overwritten (struct glyph_string *s)
23890 int k;
23892 if (s->left_overhang)
23894 int x = 0, i;
23895 struct glyph *glyphs = s->row->glyphs[s->area];
23896 int first = s->first_glyph - glyphs;
23898 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23899 x -= glyphs[i].pixel_width;
23901 k = i + 1;
23903 else
23904 k = -1;
23906 return k;
23910 /* Return the index of the first glyph preceding glyph string S that
23911 is overwriting S because of its right overhang. Value is -1 if no
23912 glyph in front of S overwrites S. */
23914 static int
23915 left_overwriting (struct glyph_string *s)
23917 int i, k, x;
23918 struct glyph *glyphs = s->row->glyphs[s->area];
23919 int first = s->first_glyph - glyphs;
23921 k = -1;
23922 x = 0;
23923 for (i = first - 1; i >= 0; --i)
23925 int left, right;
23926 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23927 if (x + right > 0)
23928 k = i;
23929 x -= glyphs[i].pixel_width;
23932 return k;
23936 /* Return the index of the last glyph following glyph string S that is
23937 overwritten by S because of S's right overhang. Value is -1 if
23938 no such glyph is found. */
23940 static int
23941 right_overwritten (struct glyph_string *s)
23943 int k = -1;
23945 if (s->right_overhang)
23947 int x = 0, i;
23948 struct glyph *glyphs = s->row->glyphs[s->area];
23949 int first = (s->first_glyph - glyphs
23950 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23951 int end = s->row->used[s->area];
23953 for (i = first; i < end && s->right_overhang > x; ++i)
23954 x += glyphs[i].pixel_width;
23956 k = i;
23959 return k;
23963 /* Return the index of the last glyph following glyph string S that
23964 overwrites S because of its left overhang. Value is negative
23965 if no such glyph is found. */
23967 static int
23968 right_overwriting (struct glyph_string *s)
23970 int i, k, x;
23971 int end = s->row->used[s->area];
23972 struct glyph *glyphs = s->row->glyphs[s->area];
23973 int first = (s->first_glyph - glyphs
23974 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23976 k = -1;
23977 x = 0;
23978 for (i = first; i < end; ++i)
23980 int left, right;
23981 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23982 if (x - left < 0)
23983 k = i;
23984 x += glyphs[i].pixel_width;
23987 return k;
23991 /* Set background width of glyph string S. START is the index of the
23992 first glyph following S. LAST_X is the right-most x-position + 1
23993 in the drawing area. */
23995 static void
23996 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23998 /* If the face of this glyph string has to be drawn to the end of
23999 the drawing area, set S->extends_to_end_of_line_p. */
24001 if (start == s->row->used[s->area]
24002 && ((s->row->fill_line_p
24003 && (s->hl == DRAW_NORMAL_TEXT
24004 || s->hl == DRAW_IMAGE_RAISED
24005 || s->hl == DRAW_IMAGE_SUNKEN))
24006 || s->hl == DRAW_MOUSE_FACE))
24007 s->extends_to_end_of_line_p = 1;
24009 /* If S extends its face to the end of the line, set its
24010 background_width to the distance to the right edge of the drawing
24011 area. */
24012 if (s->extends_to_end_of_line_p)
24013 s->background_width = last_x - s->x + 1;
24014 else
24015 s->background_width = s->width;
24019 /* Compute overhangs and x-positions for glyph string S and its
24020 predecessors, or successors. X is the starting x-position for S.
24021 BACKWARD_P non-zero means process predecessors. */
24023 static void
24024 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24026 if (backward_p)
24028 while (s)
24030 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24031 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24032 x -= s->width;
24033 s->x = x;
24034 s = s->prev;
24037 else
24039 while (s)
24041 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24042 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24043 s->x = x;
24044 x += s->width;
24045 s = s->next;
24052 /* The following macros are only called from draw_glyphs below.
24053 They reference the following parameters of that function directly:
24054 `w', `row', `area', and `overlap_p'
24055 as well as the following local variables:
24056 `s', `f', and `hdc' (in W32) */
24058 #ifdef HAVE_NTGUI
24059 /* On W32, silently add local `hdc' variable to argument list of
24060 init_glyph_string. */
24061 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24062 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24063 #else
24064 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24065 init_glyph_string (s, char2b, w, row, area, start, hl)
24066 #endif
24068 /* Add a glyph string for a stretch glyph to the list of strings
24069 between HEAD and TAIL. START is the index of the stretch glyph in
24070 row area AREA of glyph row ROW. END is the index of the last glyph
24071 in that glyph row area. X is the current output position assigned
24072 to the new glyph string constructed. HL overrides that face of the
24073 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24074 is the right-most x-position of the drawing area. */
24076 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24077 and below -- keep them on one line. */
24078 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24079 do \
24081 s = alloca (sizeof *s); \
24082 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24083 START = fill_stretch_glyph_string (s, START, END); \
24084 append_glyph_string (&HEAD, &TAIL, s); \
24085 s->x = (X); \
24087 while (0)
24090 /* Add a glyph string for an image glyph to the list of strings
24091 between HEAD and TAIL. START is the index of the image glyph in
24092 row area AREA of glyph row ROW. END is the index of the last glyph
24093 in that glyph row area. X is the current output position assigned
24094 to the new glyph string constructed. HL overrides that face of the
24095 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24096 is the right-most x-position of the drawing area. */
24098 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24099 do \
24101 s = alloca (sizeof *s); \
24102 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24103 fill_image_glyph_string (s); \
24104 append_glyph_string (&HEAD, &TAIL, s); \
24105 ++START; \
24106 s->x = (X); \
24108 while (0)
24111 /* Add a glyph string for a sequence of character glyphs to the list
24112 of strings between HEAD and TAIL. START is the index of the first
24113 glyph in row area AREA of glyph row ROW that is part of the new
24114 glyph string. END is the index of the last glyph in that glyph row
24115 area. X is the current output position assigned to the new glyph
24116 string constructed. HL overrides that face of the glyph; e.g. it
24117 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24118 right-most x-position of the drawing area. */
24120 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24121 do \
24123 int face_id; \
24124 XChar2b *char2b; \
24126 face_id = (row)->glyphs[area][START].face_id; \
24128 s = alloca (sizeof *s); \
24129 char2b = alloca ((END - START) * sizeof *char2b); \
24130 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24131 append_glyph_string (&HEAD, &TAIL, s); \
24132 s->x = (X); \
24133 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24135 while (0)
24138 /* Add a glyph string for a composite sequence to the list of strings
24139 between HEAD and TAIL. START is the index of the first glyph in
24140 row area AREA of glyph row ROW that is part of the new glyph
24141 string. END is the index of the last glyph in that glyph row area.
24142 X is the current output position assigned to the new glyph string
24143 constructed. HL overrides that face of the glyph; e.g. it is
24144 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24145 x-position of the drawing area. */
24147 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24148 do { \
24149 int face_id = (row)->glyphs[area][START].face_id; \
24150 struct face *base_face = FACE_FROM_ID (f, face_id); \
24151 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24152 struct composition *cmp = composition_table[cmp_id]; \
24153 XChar2b *char2b; \
24154 struct glyph_string *first_s = NULL; \
24155 int n; \
24157 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24159 /* Make glyph_strings for each glyph sequence that is drawable by \
24160 the same face, and append them to HEAD/TAIL. */ \
24161 for (n = 0; n < cmp->glyph_len;) \
24163 s = alloca (sizeof *s); \
24164 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24165 append_glyph_string (&(HEAD), &(TAIL), s); \
24166 s->cmp = cmp; \
24167 s->cmp_from = n; \
24168 s->x = (X); \
24169 if (n == 0) \
24170 first_s = s; \
24171 n = fill_composite_glyph_string (s, base_face, overlaps); \
24174 ++START; \
24175 s = first_s; \
24176 } while (0)
24179 /* Add a glyph string for a glyph-string sequence to the list of strings
24180 between HEAD and TAIL. */
24182 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24183 do { \
24184 int face_id; \
24185 XChar2b *char2b; \
24186 Lisp_Object gstring; \
24188 face_id = (row)->glyphs[area][START].face_id; \
24189 gstring = (composition_gstring_from_id \
24190 ((row)->glyphs[area][START].u.cmp.id)); \
24191 s = alloca (sizeof *s); \
24192 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24193 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24194 append_glyph_string (&(HEAD), &(TAIL), s); \
24195 s->x = (X); \
24196 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24197 } while (0)
24200 /* Add a glyph string for a sequence of glyphless character's glyphs
24201 to the list of strings between HEAD and TAIL. The meanings of
24202 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24204 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24205 do \
24207 int face_id; \
24209 face_id = (row)->glyphs[area][START].face_id; \
24211 s = alloca (sizeof *s); \
24212 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24213 append_glyph_string (&HEAD, &TAIL, s); \
24214 s->x = (X); \
24215 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24216 overlaps); \
24218 while (0)
24221 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24222 of AREA of glyph row ROW on window W between indices START and END.
24223 HL overrides the face for drawing glyph strings, e.g. it is
24224 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24225 x-positions of the drawing area.
24227 This is an ugly monster macro construct because we must use alloca
24228 to allocate glyph strings (because draw_glyphs can be called
24229 asynchronously). */
24231 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24232 do \
24234 HEAD = TAIL = NULL; \
24235 while (START < END) \
24237 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24238 switch (first_glyph->type) \
24240 case CHAR_GLYPH: \
24241 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24242 HL, X, LAST_X); \
24243 break; \
24245 case COMPOSITE_GLYPH: \
24246 if (first_glyph->u.cmp.automatic) \
24247 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24248 HL, X, LAST_X); \
24249 else \
24250 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24251 HL, X, LAST_X); \
24252 break; \
24254 case STRETCH_GLYPH: \
24255 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24256 HL, X, LAST_X); \
24257 break; \
24259 case IMAGE_GLYPH: \
24260 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24261 HL, X, LAST_X); \
24262 break; \
24264 case GLYPHLESS_GLYPH: \
24265 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24266 HL, X, LAST_X); \
24267 break; \
24269 default: \
24270 emacs_abort (); \
24273 if (s) \
24275 set_glyph_string_background_width (s, START, LAST_X); \
24276 (X) += s->width; \
24279 } while (0)
24282 /* Draw glyphs between START and END in AREA of ROW on window W,
24283 starting at x-position X. X is relative to AREA in W. HL is a
24284 face-override with the following meaning:
24286 DRAW_NORMAL_TEXT draw normally
24287 DRAW_CURSOR draw in cursor face
24288 DRAW_MOUSE_FACE draw in mouse face.
24289 DRAW_INVERSE_VIDEO draw in mode line face
24290 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24291 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24293 If OVERLAPS is non-zero, draw only the foreground of characters and
24294 clip to the physical height of ROW. Non-zero value also defines
24295 the overlapping part to be drawn:
24297 OVERLAPS_PRED overlap with preceding rows
24298 OVERLAPS_SUCC overlap with succeeding rows
24299 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24300 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24302 Value is the x-position reached, relative to AREA of W. */
24304 static int
24305 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24306 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24307 enum draw_glyphs_face hl, int overlaps)
24309 struct glyph_string *head, *tail;
24310 struct glyph_string *s;
24311 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24312 int i, j, x_reached, last_x, area_left = 0;
24313 struct frame *f = XFRAME (WINDOW_FRAME (w));
24314 DECLARE_HDC (hdc);
24316 ALLOCATE_HDC (hdc, f);
24318 /* Let's rather be paranoid than getting a SEGV. */
24319 end = min (end, row->used[area]);
24320 start = clip_to_bounds (0, start, end);
24322 /* Translate X to frame coordinates. Set last_x to the right
24323 end of the drawing area. */
24324 if (row->full_width_p)
24326 /* X is relative to the left edge of W, without scroll bars
24327 or fringes. */
24328 area_left = WINDOW_LEFT_EDGE_X (w);
24329 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24330 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24332 else
24334 area_left = window_box_left (w, area);
24335 last_x = area_left + window_box_width (w, area);
24337 x += area_left;
24339 /* Build a doubly-linked list of glyph_string structures between
24340 head and tail from what we have to draw. Note that the macro
24341 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24342 the reason we use a separate variable `i'. */
24343 i = start;
24344 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24345 if (tail)
24346 x_reached = tail->x + tail->background_width;
24347 else
24348 x_reached = x;
24350 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24351 the row, redraw some glyphs in front or following the glyph
24352 strings built above. */
24353 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24355 struct glyph_string *h, *t;
24356 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24357 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24358 int check_mouse_face = 0;
24359 int dummy_x = 0;
24361 /* If mouse highlighting is on, we may need to draw adjacent
24362 glyphs using mouse-face highlighting. */
24363 if (area == TEXT_AREA && row->mouse_face_p
24364 && hlinfo->mouse_face_beg_row >= 0
24365 && hlinfo->mouse_face_end_row >= 0)
24367 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24369 if (row_vpos >= hlinfo->mouse_face_beg_row
24370 && row_vpos <= hlinfo->mouse_face_end_row)
24372 check_mouse_face = 1;
24373 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24374 ? hlinfo->mouse_face_beg_col : 0;
24375 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24376 ? hlinfo->mouse_face_end_col
24377 : row->used[TEXT_AREA];
24381 /* Compute overhangs for all glyph strings. */
24382 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24383 for (s = head; s; s = s->next)
24384 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24386 /* Prepend glyph strings for glyphs in front of the first glyph
24387 string that are overwritten because of the first glyph
24388 string's left overhang. The background of all strings
24389 prepended must be drawn because the first glyph string
24390 draws over it. */
24391 i = left_overwritten (head);
24392 if (i >= 0)
24394 enum draw_glyphs_face overlap_hl;
24396 /* If this row contains mouse highlighting, attempt to draw
24397 the overlapped glyphs with the correct highlight. This
24398 code fails if the overlap encompasses more than one glyph
24399 and mouse-highlight spans only some of these glyphs.
24400 However, making it work perfectly involves a lot more
24401 code, and I don't know if the pathological case occurs in
24402 practice, so we'll stick to this for now. --- cyd */
24403 if (check_mouse_face
24404 && mouse_beg_col < start && mouse_end_col > i)
24405 overlap_hl = DRAW_MOUSE_FACE;
24406 else
24407 overlap_hl = DRAW_NORMAL_TEXT;
24409 j = i;
24410 BUILD_GLYPH_STRINGS (j, start, h, t,
24411 overlap_hl, dummy_x, last_x);
24412 start = i;
24413 compute_overhangs_and_x (t, head->x, 1);
24414 prepend_glyph_string_lists (&head, &tail, h, t);
24415 clip_head = head;
24418 /* Prepend glyph strings for glyphs in front of the first glyph
24419 string that overwrite that glyph string because of their
24420 right overhang. For these strings, only the foreground must
24421 be drawn, because it draws over the glyph string at `head'.
24422 The background must not be drawn because this would overwrite
24423 right overhangs of preceding glyphs for which no glyph
24424 strings exist. */
24425 i = left_overwriting (head);
24426 if (i >= 0)
24428 enum draw_glyphs_face overlap_hl;
24430 if (check_mouse_face
24431 && mouse_beg_col < start && mouse_end_col > i)
24432 overlap_hl = DRAW_MOUSE_FACE;
24433 else
24434 overlap_hl = DRAW_NORMAL_TEXT;
24436 clip_head = head;
24437 BUILD_GLYPH_STRINGS (i, start, h, t,
24438 overlap_hl, dummy_x, last_x);
24439 for (s = h; s; s = s->next)
24440 s->background_filled_p = 1;
24441 compute_overhangs_and_x (t, head->x, 1);
24442 prepend_glyph_string_lists (&head, &tail, h, t);
24445 /* Append glyphs strings for glyphs following the last glyph
24446 string tail that are overwritten by tail. The background of
24447 these strings has to be drawn because tail's foreground draws
24448 over it. */
24449 i = right_overwritten (tail);
24450 if (i >= 0)
24452 enum draw_glyphs_face overlap_hl;
24454 if (check_mouse_face
24455 && mouse_beg_col < i && mouse_end_col > end)
24456 overlap_hl = DRAW_MOUSE_FACE;
24457 else
24458 overlap_hl = DRAW_NORMAL_TEXT;
24460 BUILD_GLYPH_STRINGS (end, i, h, t,
24461 overlap_hl, x, last_x);
24462 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24463 we don't have `end = i;' here. */
24464 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24465 append_glyph_string_lists (&head, &tail, h, t);
24466 clip_tail = tail;
24469 /* Append glyph strings for glyphs following the last glyph
24470 string tail that overwrite tail. The foreground of such
24471 glyphs has to be drawn because it writes into the background
24472 of tail. The background must not be drawn because it could
24473 paint over the foreground of following glyphs. */
24474 i = right_overwriting (tail);
24475 if (i >= 0)
24477 enum draw_glyphs_face overlap_hl;
24478 if (check_mouse_face
24479 && mouse_beg_col < i && mouse_end_col > end)
24480 overlap_hl = DRAW_MOUSE_FACE;
24481 else
24482 overlap_hl = DRAW_NORMAL_TEXT;
24484 clip_tail = tail;
24485 i++; /* We must include the Ith glyph. */
24486 BUILD_GLYPH_STRINGS (end, i, h, t,
24487 overlap_hl, x, last_x);
24488 for (s = h; s; s = s->next)
24489 s->background_filled_p = 1;
24490 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24491 append_glyph_string_lists (&head, &tail, h, t);
24493 if (clip_head || clip_tail)
24494 for (s = head; s; s = s->next)
24496 s->clip_head = clip_head;
24497 s->clip_tail = clip_tail;
24501 /* Draw all strings. */
24502 for (s = head; s; s = s->next)
24503 FRAME_RIF (f)->draw_glyph_string (s);
24505 #ifndef HAVE_NS
24506 /* When focus a sole frame and move horizontally, this sets on_p to 0
24507 causing a failure to erase prev cursor position. */
24508 if (area == TEXT_AREA
24509 && !row->full_width_p
24510 /* When drawing overlapping rows, only the glyph strings'
24511 foreground is drawn, which doesn't erase a cursor
24512 completely. */
24513 && !overlaps)
24515 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24516 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24517 : (tail ? tail->x + tail->background_width : x));
24518 x0 -= area_left;
24519 x1 -= area_left;
24521 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24522 row->y, MATRIX_ROW_BOTTOM_Y (row));
24524 #endif
24526 /* Value is the x-position up to which drawn, relative to AREA of W.
24527 This doesn't include parts drawn because of overhangs. */
24528 if (row->full_width_p)
24529 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24530 else
24531 x_reached -= area_left;
24533 RELEASE_HDC (hdc, f);
24535 return x_reached;
24538 /* Expand row matrix if too narrow. Don't expand if area
24539 is not present. */
24541 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24543 if (!it->f->fonts_changed \
24544 && (it->glyph_row->glyphs[area] \
24545 < it->glyph_row->glyphs[area + 1])) \
24547 it->w->ncols_scale_factor++; \
24548 it->f->fonts_changed = 1; \
24552 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24553 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24555 static void
24556 append_glyph (struct it *it)
24558 struct glyph *glyph;
24559 enum glyph_row_area area = it->area;
24561 eassert (it->glyph_row);
24562 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24564 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24565 if (glyph < it->glyph_row->glyphs[area + 1])
24567 /* If the glyph row is reversed, we need to prepend the glyph
24568 rather than append it. */
24569 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24571 struct glyph *g;
24573 /* Make room for the additional glyph. */
24574 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24575 g[1] = *g;
24576 glyph = it->glyph_row->glyphs[area];
24578 glyph->charpos = CHARPOS (it->position);
24579 glyph->object = it->object;
24580 if (it->pixel_width > 0)
24582 glyph->pixel_width = it->pixel_width;
24583 glyph->padding_p = 0;
24585 else
24587 /* Assure at least 1-pixel width. Otherwise, cursor can't
24588 be displayed correctly. */
24589 glyph->pixel_width = 1;
24590 glyph->padding_p = 1;
24592 glyph->ascent = it->ascent;
24593 glyph->descent = it->descent;
24594 glyph->voffset = it->voffset;
24595 glyph->type = CHAR_GLYPH;
24596 glyph->avoid_cursor_p = it->avoid_cursor_p;
24597 glyph->multibyte_p = it->multibyte_p;
24598 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24600 /* In R2L rows, the left and the right box edges need to be
24601 drawn in reverse direction. */
24602 glyph->right_box_line_p = it->start_of_box_run_p;
24603 glyph->left_box_line_p = it->end_of_box_run_p;
24605 else
24607 glyph->left_box_line_p = it->start_of_box_run_p;
24608 glyph->right_box_line_p = it->end_of_box_run_p;
24610 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24611 || it->phys_descent > it->descent);
24612 glyph->glyph_not_available_p = it->glyph_not_available_p;
24613 glyph->face_id = it->face_id;
24614 glyph->u.ch = it->char_to_display;
24615 glyph->slice.img = null_glyph_slice;
24616 glyph->font_type = FONT_TYPE_UNKNOWN;
24617 if (it->bidi_p)
24619 glyph->resolved_level = it->bidi_it.resolved_level;
24620 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24621 emacs_abort ();
24622 glyph->bidi_type = it->bidi_it.type;
24624 else
24626 glyph->resolved_level = 0;
24627 glyph->bidi_type = UNKNOWN_BT;
24629 ++it->glyph_row->used[area];
24631 else
24632 IT_EXPAND_MATRIX_WIDTH (it, area);
24635 /* Store one glyph for the composition IT->cmp_it.id in
24636 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24637 non-null. */
24639 static void
24640 append_composite_glyph (struct it *it)
24642 struct glyph *glyph;
24643 enum glyph_row_area area = it->area;
24645 eassert (it->glyph_row);
24647 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24648 if (glyph < it->glyph_row->glyphs[area + 1])
24650 /* If the glyph row is reversed, we need to prepend the glyph
24651 rather than append it. */
24652 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24654 struct glyph *g;
24656 /* Make room for the new glyph. */
24657 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24658 g[1] = *g;
24659 glyph = it->glyph_row->glyphs[it->area];
24661 glyph->charpos = it->cmp_it.charpos;
24662 glyph->object = it->object;
24663 glyph->pixel_width = it->pixel_width;
24664 glyph->ascent = it->ascent;
24665 glyph->descent = it->descent;
24666 glyph->voffset = it->voffset;
24667 glyph->type = COMPOSITE_GLYPH;
24668 if (it->cmp_it.ch < 0)
24670 glyph->u.cmp.automatic = 0;
24671 glyph->u.cmp.id = it->cmp_it.id;
24672 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24674 else
24676 glyph->u.cmp.automatic = 1;
24677 glyph->u.cmp.id = it->cmp_it.id;
24678 glyph->slice.cmp.from = it->cmp_it.from;
24679 glyph->slice.cmp.to = it->cmp_it.to - 1;
24681 glyph->avoid_cursor_p = it->avoid_cursor_p;
24682 glyph->multibyte_p = it->multibyte_p;
24683 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24685 /* In R2L rows, the left and the right box edges need to be
24686 drawn in reverse direction. */
24687 glyph->right_box_line_p = it->start_of_box_run_p;
24688 glyph->left_box_line_p = it->end_of_box_run_p;
24690 else
24692 glyph->left_box_line_p = it->start_of_box_run_p;
24693 glyph->right_box_line_p = it->end_of_box_run_p;
24695 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24696 || it->phys_descent > it->descent);
24697 glyph->padding_p = 0;
24698 glyph->glyph_not_available_p = 0;
24699 glyph->face_id = it->face_id;
24700 glyph->font_type = FONT_TYPE_UNKNOWN;
24701 if (it->bidi_p)
24703 glyph->resolved_level = it->bidi_it.resolved_level;
24704 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24705 emacs_abort ();
24706 glyph->bidi_type = it->bidi_it.type;
24708 ++it->glyph_row->used[area];
24710 else
24711 IT_EXPAND_MATRIX_WIDTH (it, area);
24715 /* Change IT->ascent and IT->height according to the setting of
24716 IT->voffset. */
24718 static void
24719 take_vertical_position_into_account (struct it *it)
24721 if (it->voffset)
24723 if (it->voffset < 0)
24724 /* Increase the ascent so that we can display the text higher
24725 in the line. */
24726 it->ascent -= it->voffset;
24727 else
24728 /* Increase the descent so that we can display the text lower
24729 in the line. */
24730 it->descent += it->voffset;
24735 /* Produce glyphs/get display metrics for the image IT is loaded with.
24736 See the description of struct display_iterator in dispextern.h for
24737 an overview of struct display_iterator. */
24739 static void
24740 produce_image_glyph (struct it *it)
24742 struct image *img;
24743 struct face *face;
24744 int glyph_ascent, crop;
24745 struct glyph_slice slice;
24747 eassert (it->what == IT_IMAGE);
24749 face = FACE_FROM_ID (it->f, it->face_id);
24750 eassert (face);
24751 /* Make sure X resources of the face is loaded. */
24752 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24754 if (it->image_id < 0)
24756 /* Fringe bitmap. */
24757 it->ascent = it->phys_ascent = 0;
24758 it->descent = it->phys_descent = 0;
24759 it->pixel_width = 0;
24760 it->nglyphs = 0;
24761 return;
24764 img = IMAGE_FROM_ID (it->f, it->image_id);
24765 eassert (img);
24766 /* Make sure X resources of the image is loaded. */
24767 prepare_image_for_display (it->f, img);
24769 slice.x = slice.y = 0;
24770 slice.width = img->width;
24771 slice.height = img->height;
24773 if (INTEGERP (it->slice.x))
24774 slice.x = XINT (it->slice.x);
24775 else if (FLOATP (it->slice.x))
24776 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24778 if (INTEGERP (it->slice.y))
24779 slice.y = XINT (it->slice.y);
24780 else if (FLOATP (it->slice.y))
24781 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24783 if (INTEGERP (it->slice.width))
24784 slice.width = XINT (it->slice.width);
24785 else if (FLOATP (it->slice.width))
24786 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24788 if (INTEGERP (it->slice.height))
24789 slice.height = XINT (it->slice.height);
24790 else if (FLOATP (it->slice.height))
24791 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24793 if (slice.x >= img->width)
24794 slice.x = img->width;
24795 if (slice.y >= img->height)
24796 slice.y = img->height;
24797 if (slice.x + slice.width >= img->width)
24798 slice.width = img->width - slice.x;
24799 if (slice.y + slice.height > img->height)
24800 slice.height = img->height - slice.y;
24802 if (slice.width == 0 || slice.height == 0)
24803 return;
24805 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24807 it->descent = slice.height - glyph_ascent;
24808 if (slice.y == 0)
24809 it->descent += img->vmargin;
24810 if (slice.y + slice.height == img->height)
24811 it->descent += img->vmargin;
24812 it->phys_descent = it->descent;
24814 it->pixel_width = slice.width;
24815 if (slice.x == 0)
24816 it->pixel_width += img->hmargin;
24817 if (slice.x + slice.width == img->width)
24818 it->pixel_width += img->hmargin;
24820 /* It's quite possible for images to have an ascent greater than
24821 their height, so don't get confused in that case. */
24822 if (it->descent < 0)
24823 it->descent = 0;
24825 it->nglyphs = 1;
24827 if (face->box != FACE_NO_BOX)
24829 if (face->box_line_width > 0)
24831 if (slice.y == 0)
24832 it->ascent += face->box_line_width;
24833 if (slice.y + slice.height == img->height)
24834 it->descent += face->box_line_width;
24837 if (it->start_of_box_run_p && slice.x == 0)
24838 it->pixel_width += eabs (face->box_line_width);
24839 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24840 it->pixel_width += eabs (face->box_line_width);
24843 take_vertical_position_into_account (it);
24845 /* Automatically crop wide image glyphs at right edge so we can
24846 draw the cursor on same display row. */
24847 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24848 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24850 it->pixel_width -= crop;
24851 slice.width -= crop;
24854 if (it->glyph_row)
24856 struct glyph *glyph;
24857 enum glyph_row_area area = it->area;
24859 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24860 if (glyph < it->glyph_row->glyphs[area + 1])
24862 glyph->charpos = CHARPOS (it->position);
24863 glyph->object = it->object;
24864 glyph->pixel_width = it->pixel_width;
24865 glyph->ascent = glyph_ascent;
24866 glyph->descent = it->descent;
24867 glyph->voffset = it->voffset;
24868 glyph->type = IMAGE_GLYPH;
24869 glyph->avoid_cursor_p = it->avoid_cursor_p;
24870 glyph->multibyte_p = it->multibyte_p;
24871 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24873 /* In R2L rows, the left and the right box edges need to be
24874 drawn in reverse direction. */
24875 glyph->right_box_line_p = it->start_of_box_run_p;
24876 glyph->left_box_line_p = it->end_of_box_run_p;
24878 else
24880 glyph->left_box_line_p = it->start_of_box_run_p;
24881 glyph->right_box_line_p = it->end_of_box_run_p;
24883 glyph->overlaps_vertically_p = 0;
24884 glyph->padding_p = 0;
24885 glyph->glyph_not_available_p = 0;
24886 glyph->face_id = it->face_id;
24887 glyph->u.img_id = img->id;
24888 glyph->slice.img = slice;
24889 glyph->font_type = FONT_TYPE_UNKNOWN;
24890 if (it->bidi_p)
24892 glyph->resolved_level = it->bidi_it.resolved_level;
24893 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24894 emacs_abort ();
24895 glyph->bidi_type = it->bidi_it.type;
24897 ++it->glyph_row->used[area];
24899 else
24900 IT_EXPAND_MATRIX_WIDTH (it, area);
24905 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24906 of the glyph, WIDTH and HEIGHT are the width and height of the
24907 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24909 static void
24910 append_stretch_glyph (struct it *it, Lisp_Object object,
24911 int width, int height, int ascent)
24913 struct glyph *glyph;
24914 enum glyph_row_area area = it->area;
24916 eassert (ascent >= 0 && ascent <= height);
24918 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24919 if (glyph < it->glyph_row->glyphs[area + 1])
24921 /* If the glyph row is reversed, we need to prepend the glyph
24922 rather than append it. */
24923 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24925 struct glyph *g;
24927 /* Make room for the additional glyph. */
24928 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24929 g[1] = *g;
24930 glyph = it->glyph_row->glyphs[area];
24932 glyph->charpos = CHARPOS (it->position);
24933 glyph->object = object;
24934 glyph->pixel_width = width;
24935 glyph->ascent = ascent;
24936 glyph->descent = height - ascent;
24937 glyph->voffset = it->voffset;
24938 glyph->type = STRETCH_GLYPH;
24939 glyph->avoid_cursor_p = it->avoid_cursor_p;
24940 glyph->multibyte_p = it->multibyte_p;
24941 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24943 /* In R2L rows, the left and the right box edges need to be
24944 drawn in reverse direction. */
24945 glyph->right_box_line_p = it->start_of_box_run_p;
24946 glyph->left_box_line_p = it->end_of_box_run_p;
24948 else
24950 glyph->left_box_line_p = it->start_of_box_run_p;
24951 glyph->right_box_line_p = it->end_of_box_run_p;
24953 glyph->overlaps_vertically_p = 0;
24954 glyph->padding_p = 0;
24955 glyph->glyph_not_available_p = 0;
24956 glyph->face_id = it->face_id;
24957 glyph->u.stretch.ascent = ascent;
24958 glyph->u.stretch.height = height;
24959 glyph->slice.img = null_glyph_slice;
24960 glyph->font_type = FONT_TYPE_UNKNOWN;
24961 if (it->bidi_p)
24963 glyph->resolved_level = it->bidi_it.resolved_level;
24964 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24965 emacs_abort ();
24966 glyph->bidi_type = it->bidi_it.type;
24968 else
24970 glyph->resolved_level = 0;
24971 glyph->bidi_type = UNKNOWN_BT;
24973 ++it->glyph_row->used[area];
24975 else
24976 IT_EXPAND_MATRIX_WIDTH (it, area);
24979 #endif /* HAVE_WINDOW_SYSTEM */
24981 /* Produce a stretch glyph for iterator IT. IT->object is the value
24982 of the glyph property displayed. The value must be a list
24983 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24984 being recognized:
24986 1. `:width WIDTH' specifies that the space should be WIDTH *
24987 canonical char width wide. WIDTH may be an integer or floating
24988 point number.
24990 2. `:relative-width FACTOR' specifies that the width of the stretch
24991 should be computed from the width of the first character having the
24992 `glyph' property, and should be FACTOR times that width.
24994 3. `:align-to HPOS' specifies that the space should be wide enough
24995 to reach HPOS, a value in canonical character units.
24997 Exactly one of the above pairs must be present.
24999 4. `:height HEIGHT' specifies that the height of the stretch produced
25000 should be HEIGHT, measured in canonical character units.
25002 5. `:relative-height FACTOR' specifies that the height of the
25003 stretch should be FACTOR times the height of the characters having
25004 the glyph property.
25006 Either none or exactly one of 4 or 5 must be present.
25008 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25009 of the stretch should be used for the ascent of the stretch.
25010 ASCENT must be in the range 0 <= ASCENT <= 100. */
25012 void
25013 produce_stretch_glyph (struct it *it)
25015 /* (space :width WIDTH :height HEIGHT ...) */
25016 Lisp_Object prop, plist;
25017 int width = 0, height = 0, align_to = -1;
25018 int zero_width_ok_p = 0;
25019 double tem;
25020 struct font *font = NULL;
25022 #ifdef HAVE_WINDOW_SYSTEM
25023 int ascent = 0;
25024 int zero_height_ok_p = 0;
25026 if (FRAME_WINDOW_P (it->f))
25028 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25029 font = face->font ? face->font : FRAME_FONT (it->f);
25030 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25032 #endif
25034 /* List should start with `space'. */
25035 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25036 plist = XCDR (it->object);
25038 /* Compute the width of the stretch. */
25039 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25040 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25042 /* Absolute width `:width WIDTH' specified and valid. */
25043 zero_width_ok_p = 1;
25044 width = (int)tem;
25046 #ifdef HAVE_WINDOW_SYSTEM
25047 else if (FRAME_WINDOW_P (it->f)
25048 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25050 /* Relative width `:relative-width FACTOR' specified and valid.
25051 Compute the width of the characters having the `glyph'
25052 property. */
25053 struct it it2;
25054 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25056 it2 = *it;
25057 if (it->multibyte_p)
25058 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25059 else
25061 it2.c = it2.char_to_display = *p, it2.len = 1;
25062 if (! ASCII_CHAR_P (it2.c))
25063 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25066 it2.glyph_row = NULL;
25067 it2.what = IT_CHARACTER;
25068 x_produce_glyphs (&it2);
25069 width = NUMVAL (prop) * it2.pixel_width;
25071 #endif /* HAVE_WINDOW_SYSTEM */
25072 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25073 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25075 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25076 align_to = (align_to < 0
25078 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25079 else if (align_to < 0)
25080 align_to = window_box_left_offset (it->w, TEXT_AREA);
25081 width = max (0, (int)tem + align_to - it->current_x);
25082 zero_width_ok_p = 1;
25084 else
25085 /* Nothing specified -> width defaults to canonical char width. */
25086 width = FRAME_COLUMN_WIDTH (it->f);
25088 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25089 width = 1;
25091 #ifdef HAVE_WINDOW_SYSTEM
25092 /* Compute height. */
25093 if (FRAME_WINDOW_P (it->f))
25095 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25096 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25098 height = (int)tem;
25099 zero_height_ok_p = 1;
25101 else if (prop = Fplist_get (plist, QCrelative_height),
25102 NUMVAL (prop) > 0)
25103 height = FONT_HEIGHT (font) * NUMVAL (prop);
25104 else
25105 height = FONT_HEIGHT (font);
25107 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25108 height = 1;
25110 /* Compute percentage of height used for ascent. If
25111 `:ascent ASCENT' is present and valid, use that. Otherwise,
25112 derive the ascent from the font in use. */
25113 if (prop = Fplist_get (plist, QCascent),
25114 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25115 ascent = height * NUMVAL (prop) / 100.0;
25116 else if (!NILP (prop)
25117 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25118 ascent = min (max (0, (int)tem), height);
25119 else
25120 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25122 else
25123 #endif /* HAVE_WINDOW_SYSTEM */
25124 height = 1;
25126 if (width > 0 && it->line_wrap != TRUNCATE
25127 && it->current_x + width > it->last_visible_x)
25129 width = it->last_visible_x - it->current_x;
25130 #ifdef HAVE_WINDOW_SYSTEM
25131 /* Subtract one more pixel from the stretch width, but only on
25132 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25133 width -= FRAME_WINDOW_P (it->f);
25134 #endif
25137 if (width > 0 && height > 0 && it->glyph_row)
25139 Lisp_Object o_object = it->object;
25140 Lisp_Object object = it->stack[it->sp - 1].string;
25141 int n = width;
25143 if (!STRINGP (object))
25144 object = it->w->contents;
25145 #ifdef HAVE_WINDOW_SYSTEM
25146 if (FRAME_WINDOW_P (it->f))
25147 append_stretch_glyph (it, object, width, height, ascent);
25148 else
25149 #endif
25151 it->object = object;
25152 it->char_to_display = ' ';
25153 it->pixel_width = it->len = 1;
25154 while (n--)
25155 tty_append_glyph (it);
25156 it->object = o_object;
25160 it->pixel_width = width;
25161 #ifdef HAVE_WINDOW_SYSTEM
25162 if (FRAME_WINDOW_P (it->f))
25164 it->ascent = it->phys_ascent = ascent;
25165 it->descent = it->phys_descent = height - it->ascent;
25166 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25167 take_vertical_position_into_account (it);
25169 else
25170 #endif
25171 it->nglyphs = width;
25174 /* Get information about special display element WHAT in an
25175 environment described by IT. WHAT is one of IT_TRUNCATION or
25176 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25177 non-null glyph_row member. This function ensures that fields like
25178 face_id, c, len of IT are left untouched. */
25180 static void
25181 produce_special_glyphs (struct it *it, enum display_element_type what)
25183 struct it temp_it;
25184 Lisp_Object gc;
25185 GLYPH glyph;
25187 temp_it = *it;
25188 temp_it.object = make_number (0);
25189 memset (&temp_it.current, 0, sizeof temp_it.current);
25191 if (what == IT_CONTINUATION)
25193 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25194 if (it->bidi_it.paragraph_dir == R2L)
25195 SET_GLYPH_FROM_CHAR (glyph, '/');
25196 else
25197 SET_GLYPH_FROM_CHAR (glyph, '\\');
25198 if (it->dp
25199 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25201 /* FIXME: Should we mirror GC for R2L lines? */
25202 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25203 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25206 else if (what == IT_TRUNCATION)
25208 /* Truncation glyph. */
25209 SET_GLYPH_FROM_CHAR (glyph, '$');
25210 if (it->dp
25211 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25213 /* FIXME: Should we mirror GC for R2L lines? */
25214 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25215 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25218 else
25219 emacs_abort ();
25221 #ifdef HAVE_WINDOW_SYSTEM
25222 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25223 is turned off, we precede the truncation/continuation glyphs by a
25224 stretch glyph whose width is computed such that these special
25225 glyphs are aligned at the window margin, even when very different
25226 fonts are used in different glyph rows. */
25227 if (FRAME_WINDOW_P (temp_it.f)
25228 /* init_iterator calls this with it->glyph_row == NULL, and it
25229 wants only the pixel width of the truncation/continuation
25230 glyphs. */
25231 && temp_it.glyph_row
25232 /* insert_left_trunc_glyphs calls us at the beginning of the
25233 row, and it has its own calculation of the stretch glyph
25234 width. */
25235 && temp_it.glyph_row->used[TEXT_AREA] > 0
25236 && (temp_it.glyph_row->reversed_p
25237 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25238 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25240 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25242 if (stretch_width > 0)
25244 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25245 struct font *font =
25246 face->font ? face->font : FRAME_FONT (temp_it.f);
25247 int stretch_ascent =
25248 (((temp_it.ascent + temp_it.descent)
25249 * FONT_BASE (font)) / FONT_HEIGHT (font));
25251 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25252 temp_it.ascent + temp_it.descent,
25253 stretch_ascent);
25256 #endif
25258 temp_it.dp = NULL;
25259 temp_it.what = IT_CHARACTER;
25260 temp_it.len = 1;
25261 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25262 temp_it.face_id = GLYPH_FACE (glyph);
25263 temp_it.len = CHAR_BYTES (temp_it.c);
25265 PRODUCE_GLYPHS (&temp_it);
25266 it->pixel_width = temp_it.pixel_width;
25267 it->nglyphs = temp_it.pixel_width;
25270 #ifdef HAVE_WINDOW_SYSTEM
25272 /* Calculate line-height and line-spacing properties.
25273 An integer value specifies explicit pixel value.
25274 A float value specifies relative value to current face height.
25275 A cons (float . face-name) specifies relative value to
25276 height of specified face font.
25278 Returns height in pixels, or nil. */
25281 static Lisp_Object
25282 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25283 int boff, int override)
25285 Lisp_Object face_name = Qnil;
25286 int ascent, descent, height;
25288 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25289 return val;
25291 if (CONSP (val))
25293 face_name = XCAR (val);
25294 val = XCDR (val);
25295 if (!NUMBERP (val))
25296 val = make_number (1);
25297 if (NILP (face_name))
25299 height = it->ascent + it->descent;
25300 goto scale;
25304 if (NILP (face_name))
25306 font = FRAME_FONT (it->f);
25307 boff = FRAME_BASELINE_OFFSET (it->f);
25309 else if (EQ (face_name, Qt))
25311 override = 0;
25313 else
25315 int face_id;
25316 struct face *face;
25318 face_id = lookup_named_face (it->f, face_name, 0);
25319 if (face_id < 0)
25320 return make_number (-1);
25322 face = FACE_FROM_ID (it->f, face_id);
25323 font = face->font;
25324 if (font == NULL)
25325 return make_number (-1);
25326 boff = font->baseline_offset;
25327 if (font->vertical_centering)
25328 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25331 ascent = FONT_BASE (font) + boff;
25332 descent = FONT_DESCENT (font) - boff;
25334 if (override)
25336 it->override_ascent = ascent;
25337 it->override_descent = descent;
25338 it->override_boff = boff;
25341 height = ascent + descent;
25343 scale:
25344 if (FLOATP (val))
25345 height = (int)(XFLOAT_DATA (val) * height);
25346 else if (INTEGERP (val))
25347 height *= XINT (val);
25349 return make_number (height);
25353 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25354 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25355 and only if this is for a character for which no font was found.
25357 If the display method (it->glyphless_method) is
25358 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25359 length of the acronym or the hexadecimal string, UPPER_XOFF and
25360 UPPER_YOFF are pixel offsets for the upper part of the string,
25361 LOWER_XOFF and LOWER_YOFF are for the lower part.
25363 For the other display methods, LEN through LOWER_YOFF are zero. */
25365 static void
25366 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25367 short upper_xoff, short upper_yoff,
25368 short lower_xoff, short lower_yoff)
25370 struct glyph *glyph;
25371 enum glyph_row_area area = it->area;
25373 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25374 if (glyph < it->glyph_row->glyphs[area + 1])
25376 /* If the glyph row is reversed, we need to prepend the glyph
25377 rather than append it. */
25378 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25380 struct glyph *g;
25382 /* Make room for the additional glyph. */
25383 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25384 g[1] = *g;
25385 glyph = it->glyph_row->glyphs[area];
25387 glyph->charpos = CHARPOS (it->position);
25388 glyph->object = it->object;
25389 glyph->pixel_width = it->pixel_width;
25390 glyph->ascent = it->ascent;
25391 glyph->descent = it->descent;
25392 glyph->voffset = it->voffset;
25393 glyph->type = GLYPHLESS_GLYPH;
25394 glyph->u.glyphless.method = it->glyphless_method;
25395 glyph->u.glyphless.for_no_font = for_no_font;
25396 glyph->u.glyphless.len = len;
25397 glyph->u.glyphless.ch = it->c;
25398 glyph->slice.glyphless.upper_xoff = upper_xoff;
25399 glyph->slice.glyphless.upper_yoff = upper_yoff;
25400 glyph->slice.glyphless.lower_xoff = lower_xoff;
25401 glyph->slice.glyphless.lower_yoff = lower_yoff;
25402 glyph->avoid_cursor_p = it->avoid_cursor_p;
25403 glyph->multibyte_p = it->multibyte_p;
25404 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25406 /* In R2L rows, the left and the right box edges need to be
25407 drawn in reverse direction. */
25408 glyph->right_box_line_p = it->start_of_box_run_p;
25409 glyph->left_box_line_p = it->end_of_box_run_p;
25411 else
25413 glyph->left_box_line_p = it->start_of_box_run_p;
25414 glyph->right_box_line_p = it->end_of_box_run_p;
25416 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25417 || it->phys_descent > it->descent);
25418 glyph->padding_p = 0;
25419 glyph->glyph_not_available_p = 0;
25420 glyph->face_id = face_id;
25421 glyph->font_type = FONT_TYPE_UNKNOWN;
25422 if (it->bidi_p)
25424 glyph->resolved_level = it->bidi_it.resolved_level;
25425 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25426 emacs_abort ();
25427 glyph->bidi_type = it->bidi_it.type;
25429 ++it->glyph_row->used[area];
25431 else
25432 IT_EXPAND_MATRIX_WIDTH (it, area);
25436 /* Produce a glyph for a glyphless character for iterator IT.
25437 IT->glyphless_method specifies which method to use for displaying
25438 the character. See the description of enum
25439 glyphless_display_method in dispextern.h for the detail.
25441 FOR_NO_FONT is nonzero if and only if this is for a character for
25442 which no font was found. ACRONYM, if non-nil, is an acronym string
25443 for the character. */
25445 static void
25446 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25448 int face_id;
25449 struct face *face;
25450 struct font *font;
25451 int base_width, base_height, width, height;
25452 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25453 int len;
25455 /* Get the metrics of the base font. We always refer to the current
25456 ASCII face. */
25457 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25458 font = face->font ? face->font : FRAME_FONT (it->f);
25459 it->ascent = FONT_BASE (font) + font->baseline_offset;
25460 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25461 base_height = it->ascent + it->descent;
25462 base_width = font->average_width;
25464 face_id = merge_glyphless_glyph_face (it);
25466 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25468 it->pixel_width = THIN_SPACE_WIDTH;
25469 len = 0;
25470 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25472 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25474 width = CHAR_WIDTH (it->c);
25475 if (width == 0)
25476 width = 1;
25477 else if (width > 4)
25478 width = 4;
25479 it->pixel_width = base_width * width;
25480 len = 0;
25481 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25483 else
25485 char buf[7];
25486 const char *str;
25487 unsigned int code[6];
25488 int upper_len;
25489 int ascent, descent;
25490 struct font_metrics metrics_upper, metrics_lower;
25492 face = FACE_FROM_ID (it->f, face_id);
25493 font = face->font ? face->font : FRAME_FONT (it->f);
25494 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25496 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25498 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25499 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25500 if (CONSP (acronym))
25501 acronym = XCAR (acronym);
25502 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25504 else
25506 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25507 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25508 str = buf;
25510 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25511 code[len] = font->driver->encode_char (font, str[len]);
25512 upper_len = (len + 1) / 2;
25513 font->driver->text_extents (font, code, upper_len,
25514 &metrics_upper);
25515 font->driver->text_extents (font, code + upper_len, len - upper_len,
25516 &metrics_lower);
25520 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25521 width = max (metrics_upper.width, metrics_lower.width) + 4;
25522 upper_xoff = upper_yoff = 2; /* the typical case */
25523 if (base_width >= width)
25525 /* Align the upper to the left, the lower to the right. */
25526 it->pixel_width = base_width;
25527 lower_xoff = base_width - 2 - metrics_lower.width;
25529 else
25531 /* Center the shorter one. */
25532 it->pixel_width = width;
25533 if (metrics_upper.width >= metrics_lower.width)
25534 lower_xoff = (width - metrics_lower.width) / 2;
25535 else
25537 /* FIXME: This code doesn't look right. It formerly was
25538 missing the "lower_xoff = 0;", which couldn't have
25539 been right since it left lower_xoff uninitialized. */
25540 lower_xoff = 0;
25541 upper_xoff = (width - metrics_upper.width) / 2;
25545 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25546 top, bottom, and between upper and lower strings. */
25547 height = (metrics_upper.ascent + metrics_upper.descent
25548 + metrics_lower.ascent + metrics_lower.descent) + 5;
25549 /* Center vertically.
25550 H:base_height, D:base_descent
25551 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25553 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25554 descent = D - H/2 + h/2;
25555 lower_yoff = descent - 2 - ld;
25556 upper_yoff = lower_yoff - la - 1 - ud; */
25557 ascent = - (it->descent - (base_height + height + 1) / 2);
25558 descent = it->descent - (base_height - height) / 2;
25559 lower_yoff = descent - 2 - metrics_lower.descent;
25560 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25561 - metrics_upper.descent);
25562 /* Don't make the height shorter than the base height. */
25563 if (height > base_height)
25565 it->ascent = ascent;
25566 it->descent = descent;
25570 it->phys_ascent = it->ascent;
25571 it->phys_descent = it->descent;
25572 if (it->glyph_row)
25573 append_glyphless_glyph (it, face_id, for_no_font, len,
25574 upper_xoff, upper_yoff,
25575 lower_xoff, lower_yoff);
25576 it->nglyphs = 1;
25577 take_vertical_position_into_account (it);
25581 /* RIF:
25582 Produce glyphs/get display metrics for the display element IT is
25583 loaded with. See the description of struct it in dispextern.h
25584 for an overview of struct it. */
25586 void
25587 x_produce_glyphs (struct it *it)
25589 int extra_line_spacing = it->extra_line_spacing;
25591 it->glyph_not_available_p = 0;
25593 if (it->what == IT_CHARACTER)
25595 XChar2b char2b;
25596 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25597 struct font *font = face->font;
25598 struct font_metrics *pcm = NULL;
25599 int boff; /* Baseline offset. */
25601 if (font == NULL)
25603 /* When no suitable font is found, display this character by
25604 the method specified in the first extra slot of
25605 Vglyphless_char_display. */
25606 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25608 eassert (it->what == IT_GLYPHLESS);
25609 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25610 goto done;
25613 boff = font->baseline_offset;
25614 if (font->vertical_centering)
25615 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25617 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25619 int stretched_p;
25621 it->nglyphs = 1;
25623 if (it->override_ascent >= 0)
25625 it->ascent = it->override_ascent;
25626 it->descent = it->override_descent;
25627 boff = it->override_boff;
25629 else
25631 it->ascent = FONT_BASE (font) + boff;
25632 it->descent = FONT_DESCENT (font) - boff;
25635 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25637 pcm = get_per_char_metric (font, &char2b);
25638 if (pcm->width == 0
25639 && pcm->rbearing == 0 && pcm->lbearing == 0)
25640 pcm = NULL;
25643 if (pcm)
25645 it->phys_ascent = pcm->ascent + boff;
25646 it->phys_descent = pcm->descent - boff;
25647 it->pixel_width = pcm->width;
25649 else
25651 it->glyph_not_available_p = 1;
25652 it->phys_ascent = it->ascent;
25653 it->phys_descent = it->descent;
25654 it->pixel_width = font->space_width;
25657 if (it->constrain_row_ascent_descent_p)
25659 if (it->descent > it->max_descent)
25661 it->ascent += it->descent - it->max_descent;
25662 it->descent = it->max_descent;
25664 if (it->ascent > it->max_ascent)
25666 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25667 it->ascent = it->max_ascent;
25669 it->phys_ascent = min (it->phys_ascent, it->ascent);
25670 it->phys_descent = min (it->phys_descent, it->descent);
25671 extra_line_spacing = 0;
25674 /* If this is a space inside a region of text with
25675 `space-width' property, change its width. */
25676 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25677 if (stretched_p)
25678 it->pixel_width *= XFLOATINT (it->space_width);
25680 /* If face has a box, add the box thickness to the character
25681 height. If character has a box line to the left and/or
25682 right, add the box line width to the character's width. */
25683 if (face->box != FACE_NO_BOX)
25685 int thick = face->box_line_width;
25687 if (thick > 0)
25689 it->ascent += thick;
25690 it->descent += thick;
25692 else
25693 thick = -thick;
25695 if (it->start_of_box_run_p)
25696 it->pixel_width += thick;
25697 if (it->end_of_box_run_p)
25698 it->pixel_width += thick;
25701 /* If face has an overline, add the height of the overline
25702 (1 pixel) and a 1 pixel margin to the character height. */
25703 if (face->overline_p)
25704 it->ascent += overline_margin;
25706 if (it->constrain_row_ascent_descent_p)
25708 if (it->ascent > it->max_ascent)
25709 it->ascent = it->max_ascent;
25710 if (it->descent > it->max_descent)
25711 it->descent = it->max_descent;
25714 take_vertical_position_into_account (it);
25716 /* If we have to actually produce glyphs, do it. */
25717 if (it->glyph_row)
25719 if (stretched_p)
25721 /* Translate a space with a `space-width' property
25722 into a stretch glyph. */
25723 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25724 / FONT_HEIGHT (font));
25725 append_stretch_glyph (it, it->object, it->pixel_width,
25726 it->ascent + it->descent, ascent);
25728 else
25729 append_glyph (it);
25731 /* If characters with lbearing or rbearing are displayed
25732 in this line, record that fact in a flag of the
25733 glyph row. This is used to optimize X output code. */
25734 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25735 it->glyph_row->contains_overlapping_glyphs_p = 1;
25737 if (! stretched_p && it->pixel_width == 0)
25738 /* We assure that all visible glyphs have at least 1-pixel
25739 width. */
25740 it->pixel_width = 1;
25742 else if (it->char_to_display == '\n')
25744 /* A newline has no width, but we need the height of the
25745 line. But if previous part of the line sets a height,
25746 don't increase that height. */
25748 Lisp_Object height;
25749 Lisp_Object total_height = Qnil;
25751 it->override_ascent = -1;
25752 it->pixel_width = 0;
25753 it->nglyphs = 0;
25755 height = get_it_property (it, Qline_height);
25756 /* Split (line-height total-height) list. */
25757 if (CONSP (height)
25758 && CONSP (XCDR (height))
25759 && NILP (XCDR (XCDR (height))))
25761 total_height = XCAR (XCDR (height));
25762 height = XCAR (height);
25764 height = calc_line_height_property (it, height, font, boff, 1);
25766 if (it->override_ascent >= 0)
25768 it->ascent = it->override_ascent;
25769 it->descent = it->override_descent;
25770 boff = it->override_boff;
25772 else
25774 it->ascent = FONT_BASE (font) + boff;
25775 it->descent = FONT_DESCENT (font) - boff;
25778 if (EQ (height, Qt))
25780 if (it->descent > it->max_descent)
25782 it->ascent += it->descent - it->max_descent;
25783 it->descent = it->max_descent;
25785 if (it->ascent > it->max_ascent)
25787 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25788 it->ascent = it->max_ascent;
25790 it->phys_ascent = min (it->phys_ascent, it->ascent);
25791 it->phys_descent = min (it->phys_descent, it->descent);
25792 it->constrain_row_ascent_descent_p = 1;
25793 extra_line_spacing = 0;
25795 else
25797 Lisp_Object spacing;
25799 it->phys_ascent = it->ascent;
25800 it->phys_descent = it->descent;
25802 if ((it->max_ascent > 0 || it->max_descent > 0)
25803 && face->box != FACE_NO_BOX
25804 && face->box_line_width > 0)
25806 it->ascent += face->box_line_width;
25807 it->descent += face->box_line_width;
25809 if (!NILP (height)
25810 && XINT (height) > it->ascent + it->descent)
25811 it->ascent = XINT (height) - it->descent;
25813 if (!NILP (total_height))
25814 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25815 else
25817 spacing = get_it_property (it, Qline_spacing);
25818 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25820 if (INTEGERP (spacing))
25822 extra_line_spacing = XINT (spacing);
25823 if (!NILP (total_height))
25824 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25828 else /* i.e. (it->char_to_display == '\t') */
25830 if (font->space_width > 0)
25832 int tab_width = it->tab_width * font->space_width;
25833 int x = it->current_x + it->continuation_lines_width;
25834 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25836 /* If the distance from the current position to the next tab
25837 stop is less than a space character width, use the
25838 tab stop after that. */
25839 if (next_tab_x - x < font->space_width)
25840 next_tab_x += tab_width;
25842 it->pixel_width = next_tab_x - x;
25843 it->nglyphs = 1;
25844 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25845 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25847 if (it->glyph_row)
25849 append_stretch_glyph (it, it->object, it->pixel_width,
25850 it->ascent + it->descent, it->ascent);
25853 else
25855 it->pixel_width = 0;
25856 it->nglyphs = 1;
25860 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25862 /* A static composition.
25864 Note: A composition is represented as one glyph in the
25865 glyph matrix. There are no padding glyphs.
25867 Important note: pixel_width, ascent, and descent are the
25868 values of what is drawn by draw_glyphs (i.e. the values of
25869 the overall glyphs composed). */
25870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25871 int boff; /* baseline offset */
25872 struct composition *cmp = composition_table[it->cmp_it.id];
25873 int glyph_len = cmp->glyph_len;
25874 struct font *font = face->font;
25876 it->nglyphs = 1;
25878 /* If we have not yet calculated pixel size data of glyphs of
25879 the composition for the current face font, calculate them
25880 now. Theoretically, we have to check all fonts for the
25881 glyphs, but that requires much time and memory space. So,
25882 here we check only the font of the first glyph. This may
25883 lead to incorrect display, but it's very rare, and C-l
25884 (recenter-top-bottom) can correct the display anyway. */
25885 if (! cmp->font || cmp->font != font)
25887 /* Ascent and descent of the font of the first character
25888 of this composition (adjusted by baseline offset).
25889 Ascent and descent of overall glyphs should not be less
25890 than these, respectively. */
25891 int font_ascent, font_descent, font_height;
25892 /* Bounding box of the overall glyphs. */
25893 int leftmost, rightmost, lowest, highest;
25894 int lbearing, rbearing;
25895 int i, width, ascent, descent;
25896 int left_padded = 0, right_padded = 0;
25897 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25898 XChar2b char2b;
25899 struct font_metrics *pcm;
25900 int font_not_found_p;
25901 ptrdiff_t pos;
25903 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25904 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25905 break;
25906 if (glyph_len < cmp->glyph_len)
25907 right_padded = 1;
25908 for (i = 0; i < glyph_len; i++)
25910 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25911 break;
25912 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25914 if (i > 0)
25915 left_padded = 1;
25917 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25918 : IT_CHARPOS (*it));
25919 /* If no suitable font is found, use the default font. */
25920 font_not_found_p = font == NULL;
25921 if (font_not_found_p)
25923 face = face->ascii_face;
25924 font = face->font;
25926 boff = font->baseline_offset;
25927 if (font->vertical_centering)
25928 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25929 font_ascent = FONT_BASE (font) + boff;
25930 font_descent = FONT_DESCENT (font) - boff;
25931 font_height = FONT_HEIGHT (font);
25933 cmp->font = font;
25935 pcm = NULL;
25936 if (! font_not_found_p)
25938 get_char_face_and_encoding (it->f, c, it->face_id,
25939 &char2b, 0);
25940 pcm = get_per_char_metric (font, &char2b);
25943 /* Initialize the bounding box. */
25944 if (pcm)
25946 width = cmp->glyph_len > 0 ? pcm->width : 0;
25947 ascent = pcm->ascent;
25948 descent = pcm->descent;
25949 lbearing = pcm->lbearing;
25950 rbearing = pcm->rbearing;
25952 else
25954 width = cmp->glyph_len > 0 ? font->space_width : 0;
25955 ascent = FONT_BASE (font);
25956 descent = FONT_DESCENT (font);
25957 lbearing = 0;
25958 rbearing = width;
25961 rightmost = width;
25962 leftmost = 0;
25963 lowest = - descent + boff;
25964 highest = ascent + boff;
25966 if (! font_not_found_p
25967 && font->default_ascent
25968 && CHAR_TABLE_P (Vuse_default_ascent)
25969 && !NILP (Faref (Vuse_default_ascent,
25970 make_number (it->char_to_display))))
25971 highest = font->default_ascent + boff;
25973 /* Draw the first glyph at the normal position. It may be
25974 shifted to right later if some other glyphs are drawn
25975 at the left. */
25976 cmp->offsets[i * 2] = 0;
25977 cmp->offsets[i * 2 + 1] = boff;
25978 cmp->lbearing = lbearing;
25979 cmp->rbearing = rbearing;
25981 /* Set cmp->offsets for the remaining glyphs. */
25982 for (i++; i < glyph_len; i++)
25984 int left, right, btm, top;
25985 int ch = COMPOSITION_GLYPH (cmp, i);
25986 int face_id;
25987 struct face *this_face;
25989 if (ch == '\t')
25990 ch = ' ';
25991 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25992 this_face = FACE_FROM_ID (it->f, face_id);
25993 font = this_face->font;
25995 if (font == NULL)
25996 pcm = NULL;
25997 else
25999 get_char_face_and_encoding (it->f, ch, face_id,
26000 &char2b, 0);
26001 pcm = get_per_char_metric (font, &char2b);
26003 if (! pcm)
26004 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26005 else
26007 width = pcm->width;
26008 ascent = pcm->ascent;
26009 descent = pcm->descent;
26010 lbearing = pcm->lbearing;
26011 rbearing = pcm->rbearing;
26012 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26014 /* Relative composition with or without
26015 alternate chars. */
26016 left = (leftmost + rightmost - width) / 2;
26017 btm = - descent + boff;
26018 if (font->relative_compose
26019 && (! CHAR_TABLE_P (Vignore_relative_composition)
26020 || NILP (Faref (Vignore_relative_composition,
26021 make_number (ch)))))
26024 if (- descent >= font->relative_compose)
26025 /* One extra pixel between two glyphs. */
26026 btm = highest + 1;
26027 else if (ascent <= 0)
26028 /* One extra pixel between two glyphs. */
26029 btm = lowest - 1 - ascent - descent;
26032 else
26034 /* A composition rule is specified by an integer
26035 value that encodes global and new reference
26036 points (GREF and NREF). GREF and NREF are
26037 specified by numbers as below:
26039 0---1---2 -- ascent
26043 9--10--11 -- center
26045 ---3---4---5--- baseline
26047 6---7---8 -- descent
26049 int rule = COMPOSITION_RULE (cmp, i);
26050 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26052 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26053 grefx = gref % 3, nrefx = nref % 3;
26054 grefy = gref / 3, nrefy = nref / 3;
26055 if (xoff)
26056 xoff = font_height * (xoff - 128) / 256;
26057 if (yoff)
26058 yoff = font_height * (yoff - 128) / 256;
26060 left = (leftmost
26061 + grefx * (rightmost - leftmost) / 2
26062 - nrefx * width / 2
26063 + xoff);
26065 btm = ((grefy == 0 ? highest
26066 : grefy == 1 ? 0
26067 : grefy == 2 ? lowest
26068 : (highest + lowest) / 2)
26069 - (nrefy == 0 ? ascent + descent
26070 : nrefy == 1 ? descent - boff
26071 : nrefy == 2 ? 0
26072 : (ascent + descent) / 2)
26073 + yoff);
26076 cmp->offsets[i * 2] = left;
26077 cmp->offsets[i * 2 + 1] = btm + descent;
26079 /* Update the bounding box of the overall glyphs. */
26080 if (width > 0)
26082 right = left + width;
26083 if (left < leftmost)
26084 leftmost = left;
26085 if (right > rightmost)
26086 rightmost = right;
26088 top = btm + descent + ascent;
26089 if (top > highest)
26090 highest = top;
26091 if (btm < lowest)
26092 lowest = btm;
26094 if (cmp->lbearing > left + lbearing)
26095 cmp->lbearing = left + lbearing;
26096 if (cmp->rbearing < left + rbearing)
26097 cmp->rbearing = left + rbearing;
26101 /* If there are glyphs whose x-offsets are negative,
26102 shift all glyphs to the right and make all x-offsets
26103 non-negative. */
26104 if (leftmost < 0)
26106 for (i = 0; i < cmp->glyph_len; i++)
26107 cmp->offsets[i * 2] -= leftmost;
26108 rightmost -= leftmost;
26109 cmp->lbearing -= leftmost;
26110 cmp->rbearing -= leftmost;
26113 if (left_padded && cmp->lbearing < 0)
26115 for (i = 0; i < cmp->glyph_len; i++)
26116 cmp->offsets[i * 2] -= cmp->lbearing;
26117 rightmost -= cmp->lbearing;
26118 cmp->rbearing -= cmp->lbearing;
26119 cmp->lbearing = 0;
26121 if (right_padded && rightmost < cmp->rbearing)
26123 rightmost = cmp->rbearing;
26126 cmp->pixel_width = rightmost;
26127 cmp->ascent = highest;
26128 cmp->descent = - lowest;
26129 if (cmp->ascent < font_ascent)
26130 cmp->ascent = font_ascent;
26131 if (cmp->descent < font_descent)
26132 cmp->descent = font_descent;
26135 if (it->glyph_row
26136 && (cmp->lbearing < 0
26137 || cmp->rbearing > cmp->pixel_width))
26138 it->glyph_row->contains_overlapping_glyphs_p = 1;
26140 it->pixel_width = cmp->pixel_width;
26141 it->ascent = it->phys_ascent = cmp->ascent;
26142 it->descent = it->phys_descent = cmp->descent;
26143 if (face->box != FACE_NO_BOX)
26145 int thick = face->box_line_width;
26147 if (thick > 0)
26149 it->ascent += thick;
26150 it->descent += thick;
26152 else
26153 thick = - thick;
26155 if (it->start_of_box_run_p)
26156 it->pixel_width += thick;
26157 if (it->end_of_box_run_p)
26158 it->pixel_width += thick;
26161 /* If face has an overline, add the height of the overline
26162 (1 pixel) and a 1 pixel margin to the character height. */
26163 if (face->overline_p)
26164 it->ascent += overline_margin;
26166 take_vertical_position_into_account (it);
26167 if (it->ascent < 0)
26168 it->ascent = 0;
26169 if (it->descent < 0)
26170 it->descent = 0;
26172 if (it->glyph_row && cmp->glyph_len > 0)
26173 append_composite_glyph (it);
26175 else if (it->what == IT_COMPOSITION)
26177 /* A dynamic (automatic) composition. */
26178 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26179 Lisp_Object gstring;
26180 struct font_metrics metrics;
26182 it->nglyphs = 1;
26184 gstring = composition_gstring_from_id (it->cmp_it.id);
26185 it->pixel_width
26186 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26187 &metrics);
26188 if (it->glyph_row
26189 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26190 it->glyph_row->contains_overlapping_glyphs_p = 1;
26191 it->ascent = it->phys_ascent = metrics.ascent;
26192 it->descent = it->phys_descent = metrics.descent;
26193 if (face->box != FACE_NO_BOX)
26195 int thick = face->box_line_width;
26197 if (thick > 0)
26199 it->ascent += thick;
26200 it->descent += thick;
26202 else
26203 thick = - thick;
26205 if (it->start_of_box_run_p)
26206 it->pixel_width += thick;
26207 if (it->end_of_box_run_p)
26208 it->pixel_width += thick;
26210 /* If face has an overline, add the height of the overline
26211 (1 pixel) and a 1 pixel margin to the character height. */
26212 if (face->overline_p)
26213 it->ascent += overline_margin;
26214 take_vertical_position_into_account (it);
26215 if (it->ascent < 0)
26216 it->ascent = 0;
26217 if (it->descent < 0)
26218 it->descent = 0;
26220 if (it->glyph_row)
26221 append_composite_glyph (it);
26223 else if (it->what == IT_GLYPHLESS)
26224 produce_glyphless_glyph (it, 0, Qnil);
26225 else if (it->what == IT_IMAGE)
26226 produce_image_glyph (it);
26227 else if (it->what == IT_STRETCH)
26228 produce_stretch_glyph (it);
26230 done:
26231 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26232 because this isn't true for images with `:ascent 100'. */
26233 eassert (it->ascent >= 0 && it->descent >= 0);
26234 if (it->area == TEXT_AREA)
26235 it->current_x += it->pixel_width;
26237 if (extra_line_spacing > 0)
26239 it->descent += extra_line_spacing;
26240 if (extra_line_spacing > it->max_extra_line_spacing)
26241 it->max_extra_line_spacing = extra_line_spacing;
26244 it->max_ascent = max (it->max_ascent, it->ascent);
26245 it->max_descent = max (it->max_descent, it->descent);
26246 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26247 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26250 /* EXPORT for RIF:
26251 Output LEN glyphs starting at START at the nominal cursor position.
26252 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26253 being updated, and UPDATED_AREA is the area of that row being updated. */
26255 void
26256 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26257 struct glyph *start, enum glyph_row_area updated_area, int len)
26259 int x, hpos, chpos = w->phys_cursor.hpos;
26261 eassert (updated_row);
26262 /* When the window is hscrolled, cursor hpos can legitimately be out
26263 of bounds, but we draw the cursor at the corresponding window
26264 margin in that case. */
26265 if (!updated_row->reversed_p && chpos < 0)
26266 chpos = 0;
26267 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26268 chpos = updated_row->used[TEXT_AREA] - 1;
26270 block_input ();
26272 /* Write glyphs. */
26274 hpos = start - updated_row->glyphs[updated_area];
26275 x = draw_glyphs (w, w->output_cursor.x,
26276 updated_row, updated_area,
26277 hpos, hpos + len,
26278 DRAW_NORMAL_TEXT, 0);
26280 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26281 if (updated_area == TEXT_AREA
26282 && w->phys_cursor_on_p
26283 && w->phys_cursor.vpos == w->output_cursor.vpos
26284 && chpos >= hpos
26285 && chpos < hpos + len)
26286 w->phys_cursor_on_p = 0;
26288 unblock_input ();
26290 /* Advance the output cursor. */
26291 w->output_cursor.hpos += len;
26292 w->output_cursor.x = x;
26296 /* EXPORT for RIF:
26297 Insert LEN glyphs from START at the nominal cursor position. */
26299 void
26300 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26301 struct glyph *start, enum glyph_row_area updated_area, int len)
26303 struct frame *f;
26304 int line_height, shift_by_width, shifted_region_width;
26305 struct glyph_row *row;
26306 struct glyph *glyph;
26307 int frame_x, frame_y;
26308 ptrdiff_t hpos;
26310 eassert (updated_row);
26311 block_input ();
26312 f = XFRAME (WINDOW_FRAME (w));
26314 /* Get the height of the line we are in. */
26315 row = updated_row;
26316 line_height = row->height;
26318 /* Get the width of the glyphs to insert. */
26319 shift_by_width = 0;
26320 for (glyph = start; glyph < start + len; ++glyph)
26321 shift_by_width += glyph->pixel_width;
26323 /* Get the width of the region to shift right. */
26324 shifted_region_width = (window_box_width (w, updated_area)
26325 - w->output_cursor.x
26326 - shift_by_width);
26328 /* Shift right. */
26329 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26330 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26332 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26333 line_height, shift_by_width);
26335 /* Write the glyphs. */
26336 hpos = start - row->glyphs[updated_area];
26337 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26338 hpos, hpos + len,
26339 DRAW_NORMAL_TEXT, 0);
26341 /* Advance the output cursor. */
26342 w->output_cursor.hpos += len;
26343 w->output_cursor.x += shift_by_width;
26344 unblock_input ();
26348 /* EXPORT for RIF:
26349 Erase the current text line from the nominal cursor position
26350 (inclusive) to pixel column TO_X (exclusive). The idea is that
26351 everything from TO_X onward is already erased.
26353 TO_X is a pixel position relative to UPDATED_AREA of currently
26354 updated window W. TO_X == -1 means clear to the end of this area. */
26356 void
26357 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26358 enum glyph_row_area updated_area, int to_x)
26360 struct frame *f;
26361 int max_x, min_y, max_y;
26362 int from_x, from_y, to_y;
26364 eassert (updated_row);
26365 f = XFRAME (w->frame);
26367 if (updated_row->full_width_p)
26368 max_x = (WINDOW_PIXEL_WIDTH (w)
26369 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26370 else
26371 max_x = window_box_width (w, updated_area);
26372 max_y = window_text_bottom_y (w);
26374 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26375 of window. For TO_X > 0, truncate to end of drawing area. */
26376 if (to_x == 0)
26377 return;
26378 else if (to_x < 0)
26379 to_x = max_x;
26380 else
26381 to_x = min (to_x, max_x);
26383 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26385 /* Notice if the cursor will be cleared by this operation. */
26386 if (!updated_row->full_width_p)
26387 notice_overwritten_cursor (w, updated_area,
26388 w->output_cursor.x, -1,
26389 updated_row->y,
26390 MATRIX_ROW_BOTTOM_Y (updated_row));
26392 from_x = w->output_cursor.x;
26394 /* Translate to frame coordinates. */
26395 if (updated_row->full_width_p)
26397 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26398 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26400 else
26402 int area_left = window_box_left (w, updated_area);
26403 from_x += area_left;
26404 to_x += area_left;
26407 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26408 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26409 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26411 /* Prevent inadvertently clearing to end of the X window. */
26412 if (to_x > from_x && to_y > from_y)
26414 block_input ();
26415 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26416 to_x - from_x, to_y - from_y);
26417 unblock_input ();
26421 #endif /* HAVE_WINDOW_SYSTEM */
26425 /***********************************************************************
26426 Cursor types
26427 ***********************************************************************/
26429 /* Value is the internal representation of the specified cursor type
26430 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26431 of the bar cursor. */
26433 static enum text_cursor_kinds
26434 get_specified_cursor_type (Lisp_Object arg, int *width)
26436 enum text_cursor_kinds type;
26438 if (NILP (arg))
26439 return NO_CURSOR;
26441 if (EQ (arg, Qbox))
26442 return FILLED_BOX_CURSOR;
26444 if (EQ (arg, Qhollow))
26445 return HOLLOW_BOX_CURSOR;
26447 if (EQ (arg, Qbar))
26449 *width = 2;
26450 return BAR_CURSOR;
26453 if (CONSP (arg)
26454 && EQ (XCAR (arg), Qbar)
26455 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26457 *width = XINT (XCDR (arg));
26458 return BAR_CURSOR;
26461 if (EQ (arg, Qhbar))
26463 *width = 2;
26464 return HBAR_CURSOR;
26467 if (CONSP (arg)
26468 && EQ (XCAR (arg), Qhbar)
26469 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26471 *width = XINT (XCDR (arg));
26472 return HBAR_CURSOR;
26475 /* Treat anything unknown as "hollow box cursor".
26476 It was bad to signal an error; people have trouble fixing
26477 .Xdefaults with Emacs, when it has something bad in it. */
26478 type = HOLLOW_BOX_CURSOR;
26480 return type;
26483 /* Set the default cursor types for specified frame. */
26484 void
26485 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26487 int width = 1;
26488 Lisp_Object tem;
26490 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26491 FRAME_CURSOR_WIDTH (f) = width;
26493 /* By default, set up the blink-off state depending on the on-state. */
26495 tem = Fassoc (arg, Vblink_cursor_alist);
26496 if (!NILP (tem))
26498 FRAME_BLINK_OFF_CURSOR (f)
26499 = get_specified_cursor_type (XCDR (tem), &width);
26500 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26502 else
26503 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26505 /* Make sure the cursor gets redrawn. */
26506 f->cursor_type_changed = 1;
26510 #ifdef HAVE_WINDOW_SYSTEM
26512 /* Return the cursor we want to be displayed in window W. Return
26513 width of bar/hbar cursor through WIDTH arg. Return with
26514 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26515 (i.e. if the `system caret' should track this cursor).
26517 In a mini-buffer window, we want the cursor only to appear if we
26518 are reading input from this window. For the selected window, we
26519 want the cursor type given by the frame parameter or buffer local
26520 setting of cursor-type. If explicitly marked off, draw no cursor.
26521 In all other cases, we want a hollow box cursor. */
26523 static enum text_cursor_kinds
26524 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26525 int *active_cursor)
26527 struct frame *f = XFRAME (w->frame);
26528 struct buffer *b = XBUFFER (w->contents);
26529 int cursor_type = DEFAULT_CURSOR;
26530 Lisp_Object alt_cursor;
26531 int non_selected = 0;
26533 *active_cursor = 1;
26535 /* Echo area */
26536 if (cursor_in_echo_area
26537 && FRAME_HAS_MINIBUF_P (f)
26538 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26540 if (w == XWINDOW (echo_area_window))
26542 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26544 *width = FRAME_CURSOR_WIDTH (f);
26545 return FRAME_DESIRED_CURSOR (f);
26547 else
26548 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26551 *active_cursor = 0;
26552 non_selected = 1;
26555 /* Detect a nonselected window or nonselected frame. */
26556 else if (w != XWINDOW (f->selected_window)
26557 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26559 *active_cursor = 0;
26561 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26562 return NO_CURSOR;
26564 non_selected = 1;
26567 /* Never display a cursor in a window in which cursor-type is nil. */
26568 if (NILP (BVAR (b, cursor_type)))
26569 return NO_CURSOR;
26571 /* Get the normal cursor type for this window. */
26572 if (EQ (BVAR (b, cursor_type), Qt))
26574 cursor_type = FRAME_DESIRED_CURSOR (f);
26575 *width = FRAME_CURSOR_WIDTH (f);
26577 else
26578 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26580 /* Use cursor-in-non-selected-windows instead
26581 for non-selected window or frame. */
26582 if (non_selected)
26584 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26585 if (!EQ (Qt, alt_cursor))
26586 return get_specified_cursor_type (alt_cursor, width);
26587 /* t means modify the normal cursor type. */
26588 if (cursor_type == FILLED_BOX_CURSOR)
26589 cursor_type = HOLLOW_BOX_CURSOR;
26590 else if (cursor_type == BAR_CURSOR && *width > 1)
26591 --*width;
26592 return cursor_type;
26595 /* Use normal cursor if not blinked off. */
26596 if (!w->cursor_off_p)
26598 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26600 if (cursor_type == FILLED_BOX_CURSOR)
26602 /* Using a block cursor on large images can be very annoying.
26603 So use a hollow cursor for "large" images.
26604 If image is not transparent (no mask), also use hollow cursor. */
26605 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26606 if (img != NULL && IMAGEP (img->spec))
26608 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26609 where N = size of default frame font size.
26610 This should cover most of the "tiny" icons people may use. */
26611 if (!img->mask
26612 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26613 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26614 cursor_type = HOLLOW_BOX_CURSOR;
26617 else if (cursor_type != NO_CURSOR)
26619 /* Display current only supports BOX and HOLLOW cursors for images.
26620 So for now, unconditionally use a HOLLOW cursor when cursor is
26621 not a solid box cursor. */
26622 cursor_type = HOLLOW_BOX_CURSOR;
26625 return cursor_type;
26628 /* Cursor is blinked off, so determine how to "toggle" it. */
26630 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26631 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26632 return get_specified_cursor_type (XCDR (alt_cursor), width);
26634 /* Then see if frame has specified a specific blink off cursor type. */
26635 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26637 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26638 return FRAME_BLINK_OFF_CURSOR (f);
26641 #if 0
26642 /* Some people liked having a permanently visible blinking cursor,
26643 while others had very strong opinions against it. So it was
26644 decided to remove it. KFS 2003-09-03 */
26646 /* Finally perform built-in cursor blinking:
26647 filled box <-> hollow box
26648 wide [h]bar <-> narrow [h]bar
26649 narrow [h]bar <-> no cursor
26650 other type <-> no cursor */
26652 if (cursor_type == FILLED_BOX_CURSOR)
26653 return HOLLOW_BOX_CURSOR;
26655 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26657 *width = 1;
26658 return cursor_type;
26660 #endif
26662 return NO_CURSOR;
26666 /* Notice when the text cursor of window W has been completely
26667 overwritten by a drawing operation that outputs glyphs in AREA
26668 starting at X0 and ending at X1 in the line starting at Y0 and
26669 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26670 the rest of the line after X0 has been written. Y coordinates
26671 are window-relative. */
26673 static void
26674 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26675 int x0, int x1, int y0, int y1)
26677 int cx0, cx1, cy0, cy1;
26678 struct glyph_row *row;
26680 if (!w->phys_cursor_on_p)
26681 return;
26682 if (area != TEXT_AREA)
26683 return;
26685 if (w->phys_cursor.vpos < 0
26686 || w->phys_cursor.vpos >= w->current_matrix->nrows
26687 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26688 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26689 return;
26691 if (row->cursor_in_fringe_p)
26693 row->cursor_in_fringe_p = 0;
26694 draw_fringe_bitmap (w, row, row->reversed_p);
26695 w->phys_cursor_on_p = 0;
26696 return;
26699 cx0 = w->phys_cursor.x;
26700 cx1 = cx0 + w->phys_cursor_width;
26701 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26702 return;
26704 /* The cursor image will be completely removed from the
26705 screen if the output area intersects the cursor area in
26706 y-direction. When we draw in [y0 y1[, and some part of
26707 the cursor is at y < y0, that part must have been drawn
26708 before. When scrolling, the cursor is erased before
26709 actually scrolling, so we don't come here. When not
26710 scrolling, the rows above the old cursor row must have
26711 changed, and in this case these rows must have written
26712 over the cursor image.
26714 Likewise if part of the cursor is below y1, with the
26715 exception of the cursor being in the first blank row at
26716 the buffer and window end because update_text_area
26717 doesn't draw that row. (Except when it does, but
26718 that's handled in update_text_area.) */
26720 cy0 = w->phys_cursor.y;
26721 cy1 = cy0 + w->phys_cursor_height;
26722 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26723 return;
26725 w->phys_cursor_on_p = 0;
26728 #endif /* HAVE_WINDOW_SYSTEM */
26731 /************************************************************************
26732 Mouse Face
26733 ************************************************************************/
26735 #ifdef HAVE_WINDOW_SYSTEM
26737 /* EXPORT for RIF:
26738 Fix the display of area AREA of overlapping row ROW in window W
26739 with respect to the overlapping part OVERLAPS. */
26741 void
26742 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26743 enum glyph_row_area area, int overlaps)
26745 int i, x;
26747 block_input ();
26749 x = 0;
26750 for (i = 0; i < row->used[area];)
26752 if (row->glyphs[area][i].overlaps_vertically_p)
26754 int start = i, start_x = x;
26758 x += row->glyphs[area][i].pixel_width;
26759 ++i;
26761 while (i < row->used[area]
26762 && row->glyphs[area][i].overlaps_vertically_p);
26764 draw_glyphs (w, start_x, row, area,
26765 start, i,
26766 DRAW_NORMAL_TEXT, overlaps);
26768 else
26770 x += row->glyphs[area][i].pixel_width;
26771 ++i;
26775 unblock_input ();
26779 /* EXPORT:
26780 Draw the cursor glyph of window W in glyph row ROW. See the
26781 comment of draw_glyphs for the meaning of HL. */
26783 void
26784 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26785 enum draw_glyphs_face hl)
26787 /* If cursor hpos is out of bounds, don't draw garbage. This can
26788 happen in mini-buffer windows when switching between echo area
26789 glyphs and mini-buffer. */
26790 if ((row->reversed_p
26791 ? (w->phys_cursor.hpos >= 0)
26792 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26794 int on_p = w->phys_cursor_on_p;
26795 int x1;
26796 int hpos = w->phys_cursor.hpos;
26798 /* When the window is hscrolled, cursor hpos can legitimately be
26799 out of bounds, but we draw the cursor at the corresponding
26800 window margin in that case. */
26801 if (!row->reversed_p && hpos < 0)
26802 hpos = 0;
26803 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26804 hpos = row->used[TEXT_AREA] - 1;
26806 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26807 hl, 0);
26808 w->phys_cursor_on_p = on_p;
26810 if (hl == DRAW_CURSOR)
26811 w->phys_cursor_width = x1 - w->phys_cursor.x;
26812 /* When we erase the cursor, and ROW is overlapped by other
26813 rows, make sure that these overlapping parts of other rows
26814 are redrawn. */
26815 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26817 w->phys_cursor_width = x1 - w->phys_cursor.x;
26819 if (row > w->current_matrix->rows
26820 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26821 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26822 OVERLAPS_ERASED_CURSOR);
26824 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26825 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26826 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26827 OVERLAPS_ERASED_CURSOR);
26833 /* Erase the image of a cursor of window W from the screen. */
26835 #ifndef HAVE_NTGUI
26836 static
26837 #endif
26838 void
26839 erase_phys_cursor (struct window *w)
26841 struct frame *f = XFRAME (w->frame);
26842 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26843 int hpos = w->phys_cursor.hpos;
26844 int vpos = w->phys_cursor.vpos;
26845 int mouse_face_here_p = 0;
26846 struct glyph_matrix *active_glyphs = w->current_matrix;
26847 struct glyph_row *cursor_row;
26848 struct glyph *cursor_glyph;
26849 enum draw_glyphs_face hl;
26851 /* No cursor displayed or row invalidated => nothing to do on the
26852 screen. */
26853 if (w->phys_cursor_type == NO_CURSOR)
26854 goto mark_cursor_off;
26856 /* VPOS >= active_glyphs->nrows means that window has been resized.
26857 Don't bother to erase the cursor. */
26858 if (vpos >= active_glyphs->nrows)
26859 goto mark_cursor_off;
26861 /* If row containing cursor is marked invalid, there is nothing we
26862 can do. */
26863 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26864 if (!cursor_row->enabled_p)
26865 goto mark_cursor_off;
26867 /* If line spacing is > 0, old cursor may only be partially visible in
26868 window after split-window. So adjust visible height. */
26869 cursor_row->visible_height = min (cursor_row->visible_height,
26870 window_text_bottom_y (w) - cursor_row->y);
26872 /* If row is completely invisible, don't attempt to delete a cursor which
26873 isn't there. This can happen if cursor is at top of a window, and
26874 we switch to a buffer with a header line in that window. */
26875 if (cursor_row->visible_height <= 0)
26876 goto mark_cursor_off;
26878 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26879 if (cursor_row->cursor_in_fringe_p)
26881 cursor_row->cursor_in_fringe_p = 0;
26882 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26883 goto mark_cursor_off;
26886 /* This can happen when the new row is shorter than the old one.
26887 In this case, either draw_glyphs or clear_end_of_line
26888 should have cleared the cursor. Note that we wouldn't be
26889 able to erase the cursor in this case because we don't have a
26890 cursor glyph at hand. */
26891 if ((cursor_row->reversed_p
26892 ? (w->phys_cursor.hpos < 0)
26893 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26894 goto mark_cursor_off;
26896 /* When the window is hscrolled, cursor hpos can legitimately be out
26897 of bounds, but we draw the cursor at the corresponding window
26898 margin in that case. */
26899 if (!cursor_row->reversed_p && hpos < 0)
26900 hpos = 0;
26901 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26902 hpos = cursor_row->used[TEXT_AREA] - 1;
26904 /* If the cursor is in the mouse face area, redisplay that when
26905 we clear the cursor. */
26906 if (! NILP (hlinfo->mouse_face_window)
26907 && coords_in_mouse_face_p (w, hpos, vpos)
26908 /* Don't redraw the cursor's spot in mouse face if it is at the
26909 end of a line (on a newline). The cursor appears there, but
26910 mouse highlighting does not. */
26911 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26912 mouse_face_here_p = 1;
26914 /* Maybe clear the display under the cursor. */
26915 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26917 int x, y, left_x;
26918 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26919 int width;
26921 cursor_glyph = get_phys_cursor_glyph (w);
26922 if (cursor_glyph == NULL)
26923 goto mark_cursor_off;
26925 width = cursor_glyph->pixel_width;
26926 left_x = window_box_left_offset (w, TEXT_AREA);
26927 x = w->phys_cursor.x;
26928 if (x < left_x)
26929 width -= left_x - x;
26930 width = min (width, window_box_width (w, TEXT_AREA) - x);
26931 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26932 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26934 if (width > 0)
26935 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26938 /* Erase the cursor by redrawing the character underneath it. */
26939 if (mouse_face_here_p)
26940 hl = DRAW_MOUSE_FACE;
26941 else
26942 hl = DRAW_NORMAL_TEXT;
26943 draw_phys_cursor_glyph (w, cursor_row, hl);
26945 mark_cursor_off:
26946 w->phys_cursor_on_p = 0;
26947 w->phys_cursor_type = NO_CURSOR;
26951 /* EXPORT:
26952 Display or clear cursor of window W. If ON is zero, clear the
26953 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26954 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26956 void
26957 display_and_set_cursor (struct window *w, bool on,
26958 int hpos, int vpos, int x, int y)
26960 struct frame *f = XFRAME (w->frame);
26961 int new_cursor_type;
26962 int new_cursor_width;
26963 int active_cursor;
26964 struct glyph_row *glyph_row;
26965 struct glyph *glyph;
26967 /* This is pointless on invisible frames, and dangerous on garbaged
26968 windows and frames; in the latter case, the frame or window may
26969 be in the midst of changing its size, and x and y may be off the
26970 window. */
26971 if (! FRAME_VISIBLE_P (f)
26972 || FRAME_GARBAGED_P (f)
26973 || vpos >= w->current_matrix->nrows
26974 || hpos >= w->current_matrix->matrix_w)
26975 return;
26977 /* If cursor is off and we want it off, return quickly. */
26978 if (!on && !w->phys_cursor_on_p)
26979 return;
26981 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26982 /* If cursor row is not enabled, we don't really know where to
26983 display the cursor. */
26984 if (!glyph_row->enabled_p)
26986 w->phys_cursor_on_p = 0;
26987 return;
26990 glyph = NULL;
26991 if (!glyph_row->exact_window_width_line_p
26992 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26993 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26995 eassert (input_blocked_p ());
26997 /* Set new_cursor_type to the cursor we want to be displayed. */
26998 new_cursor_type = get_window_cursor_type (w, glyph,
26999 &new_cursor_width, &active_cursor);
27001 /* If cursor is currently being shown and we don't want it to be or
27002 it is in the wrong place, or the cursor type is not what we want,
27003 erase it. */
27004 if (w->phys_cursor_on_p
27005 && (!on
27006 || w->phys_cursor.x != x
27007 || w->phys_cursor.y != y
27008 || new_cursor_type != w->phys_cursor_type
27009 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27010 && new_cursor_width != w->phys_cursor_width)))
27011 erase_phys_cursor (w);
27013 /* Don't check phys_cursor_on_p here because that flag is only set
27014 to zero in some cases where we know that the cursor has been
27015 completely erased, to avoid the extra work of erasing the cursor
27016 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27017 still not be visible, or it has only been partly erased. */
27018 if (on)
27020 w->phys_cursor_ascent = glyph_row->ascent;
27021 w->phys_cursor_height = glyph_row->height;
27023 /* Set phys_cursor_.* before x_draw_.* is called because some
27024 of them may need the information. */
27025 w->phys_cursor.x = x;
27026 w->phys_cursor.y = glyph_row->y;
27027 w->phys_cursor.hpos = hpos;
27028 w->phys_cursor.vpos = vpos;
27031 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27032 new_cursor_type, new_cursor_width,
27033 on, active_cursor);
27037 /* Switch the display of W's cursor on or off, according to the value
27038 of ON. */
27040 static void
27041 update_window_cursor (struct window *w, bool on)
27043 /* Don't update cursor in windows whose frame is in the process
27044 of being deleted. */
27045 if (w->current_matrix)
27047 int hpos = w->phys_cursor.hpos;
27048 int vpos = w->phys_cursor.vpos;
27049 struct glyph_row *row;
27051 if (vpos >= w->current_matrix->nrows
27052 || hpos >= w->current_matrix->matrix_w)
27053 return;
27055 row = MATRIX_ROW (w->current_matrix, vpos);
27057 /* When the window is hscrolled, cursor hpos can legitimately be
27058 out of bounds, but we draw the cursor at the corresponding
27059 window margin in that case. */
27060 if (!row->reversed_p && hpos < 0)
27061 hpos = 0;
27062 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27063 hpos = row->used[TEXT_AREA] - 1;
27065 block_input ();
27066 display_and_set_cursor (w, on, hpos, vpos,
27067 w->phys_cursor.x, w->phys_cursor.y);
27068 unblock_input ();
27073 /* Call update_window_cursor with parameter ON_P on all leaf windows
27074 in the window tree rooted at W. */
27076 static void
27077 update_cursor_in_window_tree (struct window *w, bool on_p)
27079 while (w)
27081 if (WINDOWP (w->contents))
27082 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27083 else
27084 update_window_cursor (w, on_p);
27086 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27091 /* EXPORT:
27092 Display the cursor on window W, or clear it, according to ON_P.
27093 Don't change the cursor's position. */
27095 void
27096 x_update_cursor (struct frame *f, bool on_p)
27098 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27102 /* EXPORT:
27103 Clear the cursor of window W to background color, and mark the
27104 cursor as not shown. This is used when the text where the cursor
27105 is about to be rewritten. */
27107 void
27108 x_clear_cursor (struct window *w)
27110 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27111 update_window_cursor (w, 0);
27114 #endif /* HAVE_WINDOW_SYSTEM */
27116 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27117 and MSDOS. */
27118 static void
27119 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27120 int start_hpos, int end_hpos,
27121 enum draw_glyphs_face draw)
27123 #ifdef HAVE_WINDOW_SYSTEM
27124 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27126 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27127 return;
27129 #endif
27130 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27131 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27132 #endif
27135 /* Display the active region described by mouse_face_* according to DRAW. */
27137 static void
27138 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27140 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27141 struct frame *f = XFRAME (WINDOW_FRAME (w));
27143 if (/* If window is in the process of being destroyed, don't bother
27144 to do anything. */
27145 w->current_matrix != NULL
27146 /* Don't update mouse highlight if hidden */
27147 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27148 /* Recognize when we are called to operate on rows that don't exist
27149 anymore. This can happen when a window is split. */
27150 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27152 int phys_cursor_on_p = w->phys_cursor_on_p;
27153 struct glyph_row *row, *first, *last;
27155 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27156 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27158 for (row = first; row <= last && row->enabled_p; ++row)
27160 int start_hpos, end_hpos, start_x;
27162 /* For all but the first row, the highlight starts at column 0. */
27163 if (row == first)
27165 /* R2L rows have BEG and END in reversed order, but the
27166 screen drawing geometry is always left to right. So
27167 we need to mirror the beginning and end of the
27168 highlighted area in R2L rows. */
27169 if (!row->reversed_p)
27171 start_hpos = hlinfo->mouse_face_beg_col;
27172 start_x = hlinfo->mouse_face_beg_x;
27174 else if (row == last)
27176 start_hpos = hlinfo->mouse_face_end_col;
27177 start_x = hlinfo->mouse_face_end_x;
27179 else
27181 start_hpos = 0;
27182 start_x = 0;
27185 else if (row->reversed_p && row == last)
27187 start_hpos = hlinfo->mouse_face_end_col;
27188 start_x = hlinfo->mouse_face_end_x;
27190 else
27192 start_hpos = 0;
27193 start_x = 0;
27196 if (row == last)
27198 if (!row->reversed_p)
27199 end_hpos = hlinfo->mouse_face_end_col;
27200 else if (row == first)
27201 end_hpos = hlinfo->mouse_face_beg_col;
27202 else
27204 end_hpos = row->used[TEXT_AREA];
27205 if (draw == DRAW_NORMAL_TEXT)
27206 row->fill_line_p = 1; /* Clear to end of line */
27209 else if (row->reversed_p && row == first)
27210 end_hpos = hlinfo->mouse_face_beg_col;
27211 else
27213 end_hpos = row->used[TEXT_AREA];
27214 if (draw == DRAW_NORMAL_TEXT)
27215 row->fill_line_p = 1; /* Clear to end of line */
27218 if (end_hpos > start_hpos)
27220 draw_row_with_mouse_face (w, start_x, row,
27221 start_hpos, end_hpos, draw);
27223 row->mouse_face_p
27224 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27228 #ifdef HAVE_WINDOW_SYSTEM
27229 /* When we've written over the cursor, arrange for it to
27230 be displayed again. */
27231 if (FRAME_WINDOW_P (f)
27232 && phys_cursor_on_p && !w->phys_cursor_on_p)
27234 int hpos = w->phys_cursor.hpos;
27236 /* When the window is hscrolled, cursor hpos can legitimately be
27237 out of bounds, but we draw the cursor at the corresponding
27238 window margin in that case. */
27239 if (!row->reversed_p && hpos < 0)
27240 hpos = 0;
27241 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27242 hpos = row->used[TEXT_AREA] - 1;
27244 block_input ();
27245 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27246 w->phys_cursor.x, w->phys_cursor.y);
27247 unblock_input ();
27249 #endif /* HAVE_WINDOW_SYSTEM */
27252 #ifdef HAVE_WINDOW_SYSTEM
27253 /* Change the mouse cursor. */
27254 if (FRAME_WINDOW_P (f))
27256 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27257 if (draw == DRAW_NORMAL_TEXT
27258 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27259 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27260 else
27261 #endif
27262 if (draw == DRAW_MOUSE_FACE)
27263 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27264 else
27265 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27267 #endif /* HAVE_WINDOW_SYSTEM */
27270 /* EXPORT:
27271 Clear out the mouse-highlighted active region.
27272 Redraw it un-highlighted first. Value is non-zero if mouse
27273 face was actually drawn unhighlighted. */
27276 clear_mouse_face (Mouse_HLInfo *hlinfo)
27278 int cleared = 0;
27280 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27282 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27283 cleared = 1;
27286 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27287 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27288 hlinfo->mouse_face_window = Qnil;
27289 hlinfo->mouse_face_overlay = Qnil;
27290 return cleared;
27293 /* Return true if the coordinates HPOS and VPOS on windows W are
27294 within the mouse face on that window. */
27295 static bool
27296 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27298 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27300 /* Quickly resolve the easy cases. */
27301 if (!(WINDOWP (hlinfo->mouse_face_window)
27302 && XWINDOW (hlinfo->mouse_face_window) == w))
27303 return false;
27304 if (vpos < hlinfo->mouse_face_beg_row
27305 || vpos > hlinfo->mouse_face_end_row)
27306 return false;
27307 if (vpos > hlinfo->mouse_face_beg_row
27308 && vpos < hlinfo->mouse_face_end_row)
27309 return true;
27311 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27313 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27315 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27316 return true;
27318 else if ((vpos == hlinfo->mouse_face_beg_row
27319 && hpos >= hlinfo->mouse_face_beg_col)
27320 || (vpos == hlinfo->mouse_face_end_row
27321 && hpos < hlinfo->mouse_face_end_col))
27322 return true;
27324 else
27326 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27328 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27329 return true;
27331 else if ((vpos == hlinfo->mouse_face_beg_row
27332 && hpos <= hlinfo->mouse_face_beg_col)
27333 || (vpos == hlinfo->mouse_face_end_row
27334 && hpos > hlinfo->mouse_face_end_col))
27335 return true;
27337 return false;
27341 /* EXPORT:
27342 True if physical cursor of window W is within mouse face. */
27344 bool
27345 cursor_in_mouse_face_p (struct window *w)
27347 int hpos = w->phys_cursor.hpos;
27348 int vpos = w->phys_cursor.vpos;
27349 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27351 /* When the window is hscrolled, cursor hpos can legitimately be out
27352 of bounds, but we draw the cursor at the corresponding window
27353 margin in that case. */
27354 if (!row->reversed_p && hpos < 0)
27355 hpos = 0;
27356 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27357 hpos = row->used[TEXT_AREA] - 1;
27359 return coords_in_mouse_face_p (w, hpos, vpos);
27364 /* Find the glyph rows START_ROW and END_ROW of window W that display
27365 characters between buffer positions START_CHARPOS and END_CHARPOS
27366 (excluding END_CHARPOS). DISP_STRING is a display string that
27367 covers these buffer positions. This is similar to
27368 row_containing_pos, but is more accurate when bidi reordering makes
27369 buffer positions change non-linearly with glyph rows. */
27370 static void
27371 rows_from_pos_range (struct window *w,
27372 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27373 Lisp_Object disp_string,
27374 struct glyph_row **start, struct glyph_row **end)
27376 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27377 int last_y = window_text_bottom_y (w);
27378 struct glyph_row *row;
27380 *start = NULL;
27381 *end = NULL;
27383 while (!first->enabled_p
27384 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27385 first++;
27387 /* Find the START row. */
27388 for (row = first;
27389 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27390 row++)
27392 /* A row can potentially be the START row if the range of the
27393 characters it displays intersects the range
27394 [START_CHARPOS..END_CHARPOS). */
27395 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27396 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27397 /* See the commentary in row_containing_pos, for the
27398 explanation of the complicated way to check whether
27399 some position is beyond the end of the characters
27400 displayed by a row. */
27401 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27402 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27403 && !row->ends_at_zv_p
27404 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27405 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27406 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27407 && !row->ends_at_zv_p
27408 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27410 /* Found a candidate row. Now make sure at least one of the
27411 glyphs it displays has a charpos from the range
27412 [START_CHARPOS..END_CHARPOS).
27414 This is not obvious because bidi reordering could make
27415 buffer positions of a row be 1,2,3,102,101,100, and if we
27416 want to highlight characters in [50..60), we don't want
27417 this row, even though [50..60) does intersect [1..103),
27418 the range of character positions given by the row's start
27419 and end positions. */
27420 struct glyph *g = row->glyphs[TEXT_AREA];
27421 struct glyph *e = g + row->used[TEXT_AREA];
27423 while (g < e)
27425 if (((BUFFERP (g->object) || INTEGERP (g->object))
27426 && start_charpos <= g->charpos && g->charpos < end_charpos)
27427 /* A glyph that comes from DISP_STRING is by
27428 definition to be highlighted. */
27429 || EQ (g->object, disp_string))
27430 *start = row;
27431 g++;
27433 if (*start)
27434 break;
27438 /* Find the END row. */
27439 if (!*start
27440 /* If the last row is partially visible, start looking for END
27441 from that row, instead of starting from FIRST. */
27442 && !(row->enabled_p
27443 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27444 row = first;
27445 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27447 struct glyph_row *next = row + 1;
27448 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27450 if (!next->enabled_p
27451 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27452 /* The first row >= START whose range of displayed characters
27453 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27454 is the row END + 1. */
27455 || (start_charpos < next_start
27456 && end_charpos < next_start)
27457 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27458 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27459 && !next->ends_at_zv_p
27460 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27461 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27462 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27463 && !next->ends_at_zv_p
27464 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27466 *end = row;
27467 break;
27469 else
27471 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27472 but none of the characters it displays are in the range, it is
27473 also END + 1. */
27474 struct glyph *g = next->glyphs[TEXT_AREA];
27475 struct glyph *s = g;
27476 struct glyph *e = g + next->used[TEXT_AREA];
27478 while (g < e)
27480 if (((BUFFERP (g->object) || INTEGERP (g->object))
27481 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27482 /* If the buffer position of the first glyph in
27483 the row is equal to END_CHARPOS, it means
27484 the last character to be highlighted is the
27485 newline of ROW, and we must consider NEXT as
27486 END, not END+1. */
27487 || (((!next->reversed_p && g == s)
27488 || (next->reversed_p && g == e - 1))
27489 && (g->charpos == end_charpos
27490 /* Special case for when NEXT is an
27491 empty line at ZV. */
27492 || (g->charpos == -1
27493 && !row->ends_at_zv_p
27494 && next_start == end_charpos)))))
27495 /* A glyph that comes from DISP_STRING is by
27496 definition to be highlighted. */
27497 || EQ (g->object, disp_string))
27498 break;
27499 g++;
27501 if (g == e)
27503 *end = row;
27504 break;
27506 /* The first row that ends at ZV must be the last to be
27507 highlighted. */
27508 else if (next->ends_at_zv_p)
27510 *end = next;
27511 break;
27517 /* This function sets the mouse_face_* elements of HLINFO, assuming
27518 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27519 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27520 for the overlay or run of text properties specifying the mouse
27521 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27522 before-string and after-string that must also be highlighted.
27523 DISP_STRING, if non-nil, is a display string that may cover some
27524 or all of the highlighted text. */
27526 static void
27527 mouse_face_from_buffer_pos (Lisp_Object window,
27528 Mouse_HLInfo *hlinfo,
27529 ptrdiff_t mouse_charpos,
27530 ptrdiff_t start_charpos,
27531 ptrdiff_t end_charpos,
27532 Lisp_Object before_string,
27533 Lisp_Object after_string,
27534 Lisp_Object disp_string)
27536 struct window *w = XWINDOW (window);
27537 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27538 struct glyph_row *r1, *r2;
27539 struct glyph *glyph, *end;
27540 ptrdiff_t ignore, pos;
27541 int x;
27543 eassert (NILP (disp_string) || STRINGP (disp_string));
27544 eassert (NILP (before_string) || STRINGP (before_string));
27545 eassert (NILP (after_string) || STRINGP (after_string));
27547 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27548 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27549 if (r1 == NULL)
27550 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27551 /* If the before-string or display-string contains newlines,
27552 rows_from_pos_range skips to its last row. Move back. */
27553 if (!NILP (before_string) || !NILP (disp_string))
27555 struct glyph_row *prev;
27556 while ((prev = r1 - 1, prev >= first)
27557 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27558 && prev->used[TEXT_AREA] > 0)
27560 struct glyph *beg = prev->glyphs[TEXT_AREA];
27561 glyph = beg + prev->used[TEXT_AREA];
27562 while (--glyph >= beg && INTEGERP (glyph->object));
27563 if (glyph < beg
27564 || !(EQ (glyph->object, before_string)
27565 || EQ (glyph->object, disp_string)))
27566 break;
27567 r1 = prev;
27570 if (r2 == NULL)
27572 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27573 hlinfo->mouse_face_past_end = 1;
27575 else if (!NILP (after_string))
27577 /* If the after-string has newlines, advance to its last row. */
27578 struct glyph_row *next;
27579 struct glyph_row *last
27580 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27582 for (next = r2 + 1;
27583 next <= last
27584 && next->used[TEXT_AREA] > 0
27585 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27586 ++next)
27587 r2 = next;
27589 /* The rest of the display engine assumes that mouse_face_beg_row is
27590 either above mouse_face_end_row or identical to it. But with
27591 bidi-reordered continued lines, the row for START_CHARPOS could
27592 be below the row for END_CHARPOS. If so, swap the rows and store
27593 them in correct order. */
27594 if (r1->y > r2->y)
27596 struct glyph_row *tem = r2;
27598 r2 = r1;
27599 r1 = tem;
27602 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27603 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27605 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27606 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27607 could be anywhere in the row and in any order. The strategy
27608 below is to find the leftmost and the rightmost glyph that
27609 belongs to either of these 3 strings, or whose position is
27610 between START_CHARPOS and END_CHARPOS, and highlight all the
27611 glyphs between those two. This may cover more than just the text
27612 between START_CHARPOS and END_CHARPOS if the range of characters
27613 strides the bidi level boundary, e.g. if the beginning is in R2L
27614 text while the end is in L2R text or vice versa. */
27615 if (!r1->reversed_p)
27617 /* This row is in a left to right paragraph. Scan it left to
27618 right. */
27619 glyph = r1->glyphs[TEXT_AREA];
27620 end = glyph + r1->used[TEXT_AREA];
27621 x = r1->x;
27623 /* Skip truncation glyphs at the start of the glyph row. */
27624 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27625 for (; glyph < end
27626 && INTEGERP (glyph->object)
27627 && glyph->charpos < 0;
27628 ++glyph)
27629 x += glyph->pixel_width;
27631 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27632 or DISP_STRING, and the first glyph from buffer whose
27633 position is between START_CHARPOS and END_CHARPOS. */
27634 for (; glyph < end
27635 && !INTEGERP (glyph->object)
27636 && !EQ (glyph->object, disp_string)
27637 && !(BUFFERP (glyph->object)
27638 && (glyph->charpos >= start_charpos
27639 && glyph->charpos < end_charpos));
27640 ++glyph)
27642 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27643 are present at buffer positions between START_CHARPOS and
27644 END_CHARPOS, or if they come from an overlay. */
27645 if (EQ (glyph->object, before_string))
27647 pos = string_buffer_position (before_string,
27648 start_charpos);
27649 /* If pos == 0, it means before_string came from an
27650 overlay, not from a buffer position. */
27651 if (!pos || (pos >= start_charpos && pos < end_charpos))
27652 break;
27654 else if (EQ (glyph->object, after_string))
27656 pos = string_buffer_position (after_string, end_charpos);
27657 if (!pos || (pos >= start_charpos && pos < end_charpos))
27658 break;
27660 x += glyph->pixel_width;
27662 hlinfo->mouse_face_beg_x = x;
27663 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27665 else
27667 /* This row is in a right to left paragraph. Scan it right to
27668 left. */
27669 struct glyph *g;
27671 end = r1->glyphs[TEXT_AREA] - 1;
27672 glyph = end + r1->used[TEXT_AREA];
27674 /* Skip truncation glyphs at the start of the glyph row. */
27675 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27676 for (; glyph > end
27677 && INTEGERP (glyph->object)
27678 && glyph->charpos < 0;
27679 --glyph)
27682 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27683 or DISP_STRING, and the first glyph from buffer whose
27684 position is between START_CHARPOS and END_CHARPOS. */
27685 for (; glyph > end
27686 && !INTEGERP (glyph->object)
27687 && !EQ (glyph->object, disp_string)
27688 && !(BUFFERP (glyph->object)
27689 && (glyph->charpos >= start_charpos
27690 && glyph->charpos < end_charpos));
27691 --glyph)
27693 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27694 are present at buffer positions between START_CHARPOS and
27695 END_CHARPOS, or if they come from an overlay. */
27696 if (EQ (glyph->object, before_string))
27698 pos = string_buffer_position (before_string, start_charpos);
27699 /* If pos == 0, it means before_string came from an
27700 overlay, not from a buffer position. */
27701 if (!pos || (pos >= start_charpos && pos < end_charpos))
27702 break;
27704 else if (EQ (glyph->object, after_string))
27706 pos = string_buffer_position (after_string, end_charpos);
27707 if (!pos || (pos >= start_charpos && pos < end_charpos))
27708 break;
27712 glyph++; /* first glyph to the right of the highlighted area */
27713 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27714 x += g->pixel_width;
27715 hlinfo->mouse_face_beg_x = x;
27716 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27719 /* If the highlight ends in a different row, compute GLYPH and END
27720 for the end row. Otherwise, reuse the values computed above for
27721 the row where the highlight begins. */
27722 if (r2 != r1)
27724 if (!r2->reversed_p)
27726 glyph = r2->glyphs[TEXT_AREA];
27727 end = glyph + r2->used[TEXT_AREA];
27728 x = r2->x;
27730 else
27732 end = r2->glyphs[TEXT_AREA] - 1;
27733 glyph = end + r2->used[TEXT_AREA];
27737 if (!r2->reversed_p)
27739 /* Skip truncation and continuation glyphs near the end of the
27740 row, and also blanks and stretch glyphs inserted by
27741 extend_face_to_end_of_line. */
27742 while (end > glyph
27743 && INTEGERP ((end - 1)->object))
27744 --end;
27745 /* Scan the rest of the glyph row from the end, looking for the
27746 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27747 DISP_STRING, or whose position is between START_CHARPOS
27748 and END_CHARPOS */
27749 for (--end;
27750 end > glyph
27751 && !INTEGERP (end->object)
27752 && !EQ (end->object, disp_string)
27753 && !(BUFFERP (end->object)
27754 && (end->charpos >= start_charpos
27755 && end->charpos < end_charpos));
27756 --end)
27758 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27759 are present at buffer positions between START_CHARPOS and
27760 END_CHARPOS, or if they come from an overlay. */
27761 if (EQ (end->object, before_string))
27763 pos = string_buffer_position (before_string, start_charpos);
27764 if (!pos || (pos >= start_charpos && pos < end_charpos))
27765 break;
27767 else if (EQ (end->object, after_string))
27769 pos = string_buffer_position (after_string, end_charpos);
27770 if (!pos || (pos >= start_charpos && pos < end_charpos))
27771 break;
27774 /* Find the X coordinate of the last glyph to be highlighted. */
27775 for (; glyph <= end; ++glyph)
27776 x += glyph->pixel_width;
27778 hlinfo->mouse_face_end_x = x;
27779 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27781 else
27783 /* Skip truncation and continuation glyphs near the end of the
27784 row, and also blanks and stretch glyphs inserted by
27785 extend_face_to_end_of_line. */
27786 x = r2->x;
27787 end++;
27788 while (end < glyph
27789 && INTEGERP (end->object))
27791 x += end->pixel_width;
27792 ++end;
27794 /* Scan the rest of the glyph row from the end, looking for the
27795 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27796 DISP_STRING, or whose position is between START_CHARPOS
27797 and END_CHARPOS */
27798 for ( ;
27799 end < glyph
27800 && !INTEGERP (end->object)
27801 && !EQ (end->object, disp_string)
27802 && !(BUFFERP (end->object)
27803 && (end->charpos >= start_charpos
27804 && end->charpos < end_charpos));
27805 ++end)
27807 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27808 are present at buffer positions between START_CHARPOS and
27809 END_CHARPOS, or if they come from an overlay. */
27810 if (EQ (end->object, before_string))
27812 pos = string_buffer_position (before_string, start_charpos);
27813 if (!pos || (pos >= start_charpos && pos < end_charpos))
27814 break;
27816 else if (EQ (end->object, after_string))
27818 pos = string_buffer_position (after_string, end_charpos);
27819 if (!pos || (pos >= start_charpos && pos < end_charpos))
27820 break;
27822 x += end->pixel_width;
27824 /* If we exited the above loop because we arrived at the last
27825 glyph of the row, and its buffer position is still not in
27826 range, it means the last character in range is the preceding
27827 newline. Bump the end column and x values to get past the
27828 last glyph. */
27829 if (end == glyph
27830 && BUFFERP (end->object)
27831 && (end->charpos < start_charpos
27832 || end->charpos >= end_charpos))
27834 x += end->pixel_width;
27835 ++end;
27837 hlinfo->mouse_face_end_x = x;
27838 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27841 hlinfo->mouse_face_window = window;
27842 hlinfo->mouse_face_face_id
27843 = face_at_buffer_position (w, mouse_charpos, &ignore,
27844 mouse_charpos + 1,
27845 !hlinfo->mouse_face_hidden, -1);
27846 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27849 /* The following function is not used anymore (replaced with
27850 mouse_face_from_string_pos), but I leave it here for the time
27851 being, in case someone would. */
27853 #if 0 /* not used */
27855 /* Find the position of the glyph for position POS in OBJECT in
27856 window W's current matrix, and return in *X, *Y the pixel
27857 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27859 RIGHT_P non-zero means return the position of the right edge of the
27860 glyph, RIGHT_P zero means return the left edge position.
27862 If no glyph for POS exists in the matrix, return the position of
27863 the glyph with the next smaller position that is in the matrix, if
27864 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27865 exists in the matrix, return the position of the glyph with the
27866 next larger position in OBJECT.
27868 Value is non-zero if a glyph was found. */
27870 static int
27871 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27872 int *hpos, int *vpos, int *x, int *y, int right_p)
27874 int yb = window_text_bottom_y (w);
27875 struct glyph_row *r;
27876 struct glyph *best_glyph = NULL;
27877 struct glyph_row *best_row = NULL;
27878 int best_x = 0;
27880 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27881 r->enabled_p && r->y < yb;
27882 ++r)
27884 struct glyph *g = r->glyphs[TEXT_AREA];
27885 struct glyph *e = g + r->used[TEXT_AREA];
27886 int gx;
27888 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27889 if (EQ (g->object, object))
27891 if (g->charpos == pos)
27893 best_glyph = g;
27894 best_x = gx;
27895 best_row = r;
27896 goto found;
27898 else if (best_glyph == NULL
27899 || ((eabs (g->charpos - pos)
27900 < eabs (best_glyph->charpos - pos))
27901 && (right_p
27902 ? g->charpos < pos
27903 : g->charpos > pos)))
27905 best_glyph = g;
27906 best_x = gx;
27907 best_row = r;
27912 found:
27914 if (best_glyph)
27916 *x = best_x;
27917 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27919 if (right_p)
27921 *x += best_glyph->pixel_width;
27922 ++*hpos;
27925 *y = best_row->y;
27926 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27929 return best_glyph != NULL;
27931 #endif /* not used */
27933 /* Find the positions of the first and the last glyphs in window W's
27934 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27935 (assumed to be a string), and return in HLINFO's mouse_face_*
27936 members the pixel and column/row coordinates of those glyphs. */
27938 static void
27939 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27940 Lisp_Object object,
27941 ptrdiff_t startpos, ptrdiff_t endpos)
27943 int yb = window_text_bottom_y (w);
27944 struct glyph_row *r;
27945 struct glyph *g, *e;
27946 int gx;
27947 int found = 0;
27949 /* Find the glyph row with at least one position in the range
27950 [STARTPOS..ENDPOS), and the first glyph in that row whose
27951 position belongs to that range. */
27952 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27953 r->enabled_p && r->y < yb;
27954 ++r)
27956 if (!r->reversed_p)
27958 g = r->glyphs[TEXT_AREA];
27959 e = g + r->used[TEXT_AREA];
27960 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27961 if (EQ (g->object, object)
27962 && startpos <= g->charpos && g->charpos < endpos)
27964 hlinfo->mouse_face_beg_row
27965 = MATRIX_ROW_VPOS (r, w->current_matrix);
27966 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27967 hlinfo->mouse_face_beg_x = gx;
27968 found = 1;
27969 break;
27972 else
27974 struct glyph *g1;
27976 e = r->glyphs[TEXT_AREA];
27977 g = e + r->used[TEXT_AREA];
27978 for ( ; g > e; --g)
27979 if (EQ ((g-1)->object, object)
27980 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27982 hlinfo->mouse_face_beg_row
27983 = MATRIX_ROW_VPOS (r, w->current_matrix);
27984 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27985 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27986 gx += g1->pixel_width;
27987 hlinfo->mouse_face_beg_x = gx;
27988 found = 1;
27989 break;
27992 if (found)
27993 break;
27996 if (!found)
27997 return;
27999 /* Starting with the next row, look for the first row which does NOT
28000 include any glyphs whose positions are in the range. */
28001 for (++r; r->enabled_p && r->y < yb; ++r)
28003 g = r->glyphs[TEXT_AREA];
28004 e = g + r->used[TEXT_AREA];
28005 found = 0;
28006 for ( ; g < e; ++g)
28007 if (EQ (g->object, object)
28008 && startpos <= g->charpos && g->charpos < endpos)
28010 found = 1;
28011 break;
28013 if (!found)
28014 break;
28017 /* The highlighted region ends on the previous row. */
28018 r--;
28020 /* Set the end row. */
28021 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28023 /* Compute and set the end column and the end column's horizontal
28024 pixel coordinate. */
28025 if (!r->reversed_p)
28027 g = r->glyphs[TEXT_AREA];
28028 e = g + r->used[TEXT_AREA];
28029 for ( ; e > g; --e)
28030 if (EQ ((e-1)->object, object)
28031 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28032 break;
28033 hlinfo->mouse_face_end_col = e - g;
28035 for (gx = r->x; g < e; ++g)
28036 gx += g->pixel_width;
28037 hlinfo->mouse_face_end_x = gx;
28039 else
28041 e = r->glyphs[TEXT_AREA];
28042 g = e + r->used[TEXT_AREA];
28043 for (gx = r->x ; e < g; ++e)
28045 if (EQ (e->object, object)
28046 && startpos <= e->charpos && e->charpos < endpos)
28047 break;
28048 gx += e->pixel_width;
28050 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28051 hlinfo->mouse_face_end_x = gx;
28055 #ifdef HAVE_WINDOW_SYSTEM
28057 /* See if position X, Y is within a hot-spot of an image. */
28059 static int
28060 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28062 if (!CONSP (hot_spot))
28063 return 0;
28065 if (EQ (XCAR (hot_spot), Qrect))
28067 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28068 Lisp_Object rect = XCDR (hot_spot);
28069 Lisp_Object tem;
28070 if (!CONSP (rect))
28071 return 0;
28072 if (!CONSP (XCAR (rect)))
28073 return 0;
28074 if (!CONSP (XCDR (rect)))
28075 return 0;
28076 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28077 return 0;
28078 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28079 return 0;
28080 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28081 return 0;
28082 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28083 return 0;
28084 return 1;
28086 else if (EQ (XCAR (hot_spot), Qcircle))
28088 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28089 Lisp_Object circ = XCDR (hot_spot);
28090 Lisp_Object lr, lx0, ly0;
28091 if (CONSP (circ)
28092 && CONSP (XCAR (circ))
28093 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28094 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28095 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28097 double r = XFLOATINT (lr);
28098 double dx = XINT (lx0) - x;
28099 double dy = XINT (ly0) - y;
28100 return (dx * dx + dy * dy <= r * r);
28103 else if (EQ (XCAR (hot_spot), Qpoly))
28105 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28106 if (VECTORP (XCDR (hot_spot)))
28108 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28109 Lisp_Object *poly = v->contents;
28110 ptrdiff_t n = v->header.size;
28111 ptrdiff_t i;
28112 int inside = 0;
28113 Lisp_Object lx, ly;
28114 int x0, y0;
28116 /* Need an even number of coordinates, and at least 3 edges. */
28117 if (n < 6 || n & 1)
28118 return 0;
28120 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28121 If count is odd, we are inside polygon. Pixels on edges
28122 may or may not be included depending on actual geometry of the
28123 polygon. */
28124 if ((lx = poly[n-2], !INTEGERP (lx))
28125 || (ly = poly[n-1], !INTEGERP (lx)))
28126 return 0;
28127 x0 = XINT (lx), y0 = XINT (ly);
28128 for (i = 0; i < n; i += 2)
28130 int x1 = x0, y1 = y0;
28131 if ((lx = poly[i], !INTEGERP (lx))
28132 || (ly = poly[i+1], !INTEGERP (ly)))
28133 return 0;
28134 x0 = XINT (lx), y0 = XINT (ly);
28136 /* Does this segment cross the X line? */
28137 if (x0 >= x)
28139 if (x1 >= x)
28140 continue;
28142 else if (x1 < x)
28143 continue;
28144 if (y > y0 && y > y1)
28145 continue;
28146 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28147 inside = !inside;
28149 return inside;
28152 return 0;
28155 Lisp_Object
28156 find_hot_spot (Lisp_Object map, int x, int y)
28158 while (CONSP (map))
28160 if (CONSP (XCAR (map))
28161 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28162 return XCAR (map);
28163 map = XCDR (map);
28166 return Qnil;
28169 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28170 3, 3, 0,
28171 doc: /* Lookup in image map MAP coordinates X and Y.
28172 An image map is an alist where each element has the format (AREA ID PLIST).
28173 An AREA is specified as either a rectangle, a circle, or a polygon:
28174 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28175 pixel coordinates of the upper left and bottom right corners.
28176 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28177 and the radius of the circle; r may be a float or integer.
28178 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28179 vector describes one corner in the polygon.
28180 Returns the alist element for the first matching AREA in MAP. */)
28181 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28183 if (NILP (map))
28184 return Qnil;
28186 CHECK_NUMBER (x);
28187 CHECK_NUMBER (y);
28189 return find_hot_spot (map,
28190 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28191 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28195 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28196 static void
28197 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28199 /* Do not change cursor shape while dragging mouse. */
28200 if (!NILP (do_mouse_tracking))
28201 return;
28203 if (!NILP (pointer))
28205 if (EQ (pointer, Qarrow))
28206 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28207 else if (EQ (pointer, Qhand))
28208 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28209 else if (EQ (pointer, Qtext))
28210 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28211 else if (EQ (pointer, intern ("hdrag")))
28212 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28213 else if (EQ (pointer, intern ("nhdrag")))
28214 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28215 #ifdef HAVE_X_WINDOWS
28216 else if (EQ (pointer, intern ("vdrag")))
28217 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28218 #endif
28219 else if (EQ (pointer, intern ("hourglass")))
28220 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28221 else if (EQ (pointer, Qmodeline))
28222 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28223 else
28224 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28227 if (cursor != No_Cursor)
28228 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28231 #endif /* HAVE_WINDOW_SYSTEM */
28233 /* Take proper action when mouse has moved to the mode or header line
28234 or marginal area AREA of window W, x-position X and y-position Y.
28235 X is relative to the start of the text display area of W, so the
28236 width of bitmap areas and scroll bars must be subtracted to get a
28237 position relative to the start of the mode line. */
28239 static void
28240 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28241 enum window_part area)
28243 struct window *w = XWINDOW (window);
28244 struct frame *f = XFRAME (w->frame);
28245 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28246 #ifdef HAVE_WINDOW_SYSTEM
28247 Display_Info *dpyinfo;
28248 #endif
28249 Cursor cursor = No_Cursor;
28250 Lisp_Object pointer = Qnil;
28251 int dx, dy, width, height;
28252 ptrdiff_t charpos;
28253 Lisp_Object string, object = Qnil;
28254 Lisp_Object pos IF_LINT (= Qnil), help;
28256 Lisp_Object mouse_face;
28257 int original_x_pixel = x;
28258 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28259 struct glyph_row *row IF_LINT (= 0);
28261 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28263 int x0;
28264 struct glyph *end;
28266 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28267 returns them in row/column units! */
28268 string = mode_line_string (w, area, &x, &y, &charpos,
28269 &object, &dx, &dy, &width, &height);
28271 row = (area == ON_MODE_LINE
28272 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28273 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28275 /* Find the glyph under the mouse pointer. */
28276 if (row->mode_line_p && row->enabled_p)
28278 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28279 end = glyph + row->used[TEXT_AREA];
28281 for (x0 = original_x_pixel;
28282 glyph < end && x0 >= glyph->pixel_width;
28283 ++glyph)
28284 x0 -= glyph->pixel_width;
28286 if (glyph >= end)
28287 glyph = NULL;
28290 else
28292 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28293 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28294 returns them in row/column units! */
28295 string = marginal_area_string (w, area, &x, &y, &charpos,
28296 &object, &dx, &dy, &width, &height);
28299 help = Qnil;
28301 #ifdef HAVE_WINDOW_SYSTEM
28302 if (IMAGEP (object))
28304 Lisp_Object image_map, hotspot;
28305 if ((image_map = Fplist_get (XCDR (object), QCmap),
28306 !NILP (image_map))
28307 && (hotspot = find_hot_spot (image_map, dx, dy),
28308 CONSP (hotspot))
28309 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28311 Lisp_Object plist;
28313 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28314 If so, we could look for mouse-enter, mouse-leave
28315 properties in PLIST (and do something...). */
28316 hotspot = XCDR (hotspot);
28317 if (CONSP (hotspot)
28318 && (plist = XCAR (hotspot), CONSP (plist)))
28320 pointer = Fplist_get (plist, Qpointer);
28321 if (NILP (pointer))
28322 pointer = Qhand;
28323 help = Fplist_get (plist, Qhelp_echo);
28324 if (!NILP (help))
28326 help_echo_string = help;
28327 XSETWINDOW (help_echo_window, w);
28328 help_echo_object = w->contents;
28329 help_echo_pos = charpos;
28333 if (NILP (pointer))
28334 pointer = Fplist_get (XCDR (object), QCpointer);
28336 #endif /* HAVE_WINDOW_SYSTEM */
28338 if (STRINGP (string))
28339 pos = make_number (charpos);
28341 /* Set the help text and mouse pointer. If the mouse is on a part
28342 of the mode line without any text (e.g. past the right edge of
28343 the mode line text), use the default help text and pointer. */
28344 if (STRINGP (string) || area == ON_MODE_LINE)
28346 /* Arrange to display the help by setting the global variables
28347 help_echo_string, help_echo_object, and help_echo_pos. */
28348 if (NILP (help))
28350 if (STRINGP (string))
28351 help = Fget_text_property (pos, Qhelp_echo, string);
28353 if (!NILP (help))
28355 help_echo_string = help;
28356 XSETWINDOW (help_echo_window, w);
28357 help_echo_object = string;
28358 help_echo_pos = charpos;
28360 else if (area == ON_MODE_LINE)
28362 Lisp_Object default_help
28363 = buffer_local_value_1 (Qmode_line_default_help_echo,
28364 w->contents);
28366 if (STRINGP (default_help))
28368 help_echo_string = default_help;
28369 XSETWINDOW (help_echo_window, w);
28370 help_echo_object = Qnil;
28371 help_echo_pos = -1;
28376 #ifdef HAVE_WINDOW_SYSTEM
28377 /* Change the mouse pointer according to what is under it. */
28378 if (FRAME_WINDOW_P (f))
28380 dpyinfo = FRAME_DISPLAY_INFO (f);
28381 if (STRINGP (string))
28383 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28385 if (NILP (pointer))
28386 pointer = Fget_text_property (pos, Qpointer, string);
28388 /* Change the mouse pointer according to what is under X/Y. */
28389 if (NILP (pointer)
28390 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28392 Lisp_Object map;
28393 map = Fget_text_property (pos, Qlocal_map, string);
28394 if (!KEYMAPP (map))
28395 map = Fget_text_property (pos, Qkeymap, string);
28396 if (!KEYMAPP (map))
28397 cursor = dpyinfo->vertical_scroll_bar_cursor;
28400 else
28401 /* Default mode-line pointer. */
28402 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28404 #endif
28407 /* Change the mouse face according to what is under X/Y. */
28408 if (STRINGP (string))
28410 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28411 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28412 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28413 && glyph)
28415 Lisp_Object b, e;
28417 struct glyph * tmp_glyph;
28419 int gpos;
28420 int gseq_length;
28421 int total_pixel_width;
28422 ptrdiff_t begpos, endpos, ignore;
28424 int vpos, hpos;
28426 b = Fprevious_single_property_change (make_number (charpos + 1),
28427 Qmouse_face, string, Qnil);
28428 if (NILP (b))
28429 begpos = 0;
28430 else
28431 begpos = XINT (b);
28433 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28434 if (NILP (e))
28435 endpos = SCHARS (string);
28436 else
28437 endpos = XINT (e);
28439 /* Calculate the glyph position GPOS of GLYPH in the
28440 displayed string, relative to the beginning of the
28441 highlighted part of the string.
28443 Note: GPOS is different from CHARPOS. CHARPOS is the
28444 position of GLYPH in the internal string object. A mode
28445 line string format has structures which are converted to
28446 a flattened string by the Emacs Lisp interpreter. The
28447 internal string is an element of those structures. The
28448 displayed string is the flattened string. */
28449 tmp_glyph = row_start_glyph;
28450 while (tmp_glyph < glyph
28451 && (!(EQ (tmp_glyph->object, glyph->object)
28452 && begpos <= tmp_glyph->charpos
28453 && tmp_glyph->charpos < endpos)))
28454 tmp_glyph++;
28455 gpos = glyph - tmp_glyph;
28457 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28458 the highlighted part of the displayed string to which
28459 GLYPH belongs. Note: GSEQ_LENGTH is different from
28460 SCHARS (STRING), because the latter returns the length of
28461 the internal string. */
28462 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28463 tmp_glyph > glyph
28464 && (!(EQ (tmp_glyph->object, glyph->object)
28465 && begpos <= tmp_glyph->charpos
28466 && tmp_glyph->charpos < endpos));
28467 tmp_glyph--)
28469 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28471 /* Calculate the total pixel width of all the glyphs between
28472 the beginning of the highlighted area and GLYPH. */
28473 total_pixel_width = 0;
28474 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28475 total_pixel_width += tmp_glyph->pixel_width;
28477 /* Pre calculation of re-rendering position. Note: X is in
28478 column units here, after the call to mode_line_string or
28479 marginal_area_string. */
28480 hpos = x - gpos;
28481 vpos = (area == ON_MODE_LINE
28482 ? (w->current_matrix)->nrows - 1
28483 : 0);
28485 /* If GLYPH's position is included in the region that is
28486 already drawn in mouse face, we have nothing to do. */
28487 if ( EQ (window, hlinfo->mouse_face_window)
28488 && (!row->reversed_p
28489 ? (hlinfo->mouse_face_beg_col <= hpos
28490 && hpos < hlinfo->mouse_face_end_col)
28491 /* In R2L rows we swap BEG and END, see below. */
28492 : (hlinfo->mouse_face_end_col <= hpos
28493 && hpos < hlinfo->mouse_face_beg_col))
28494 && hlinfo->mouse_face_beg_row == vpos )
28495 return;
28497 if (clear_mouse_face (hlinfo))
28498 cursor = No_Cursor;
28500 if (!row->reversed_p)
28502 hlinfo->mouse_face_beg_col = hpos;
28503 hlinfo->mouse_face_beg_x = original_x_pixel
28504 - (total_pixel_width + dx);
28505 hlinfo->mouse_face_end_col = hpos + gseq_length;
28506 hlinfo->mouse_face_end_x = 0;
28508 else
28510 /* In R2L rows, show_mouse_face expects BEG and END
28511 coordinates to be swapped. */
28512 hlinfo->mouse_face_end_col = hpos;
28513 hlinfo->mouse_face_end_x = original_x_pixel
28514 - (total_pixel_width + dx);
28515 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28516 hlinfo->mouse_face_beg_x = 0;
28519 hlinfo->mouse_face_beg_row = vpos;
28520 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28521 hlinfo->mouse_face_past_end = 0;
28522 hlinfo->mouse_face_window = window;
28524 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28525 charpos,
28526 0, &ignore,
28527 glyph->face_id,
28529 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28531 if (NILP (pointer))
28532 pointer = Qhand;
28534 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28535 clear_mouse_face (hlinfo);
28537 #ifdef HAVE_WINDOW_SYSTEM
28538 if (FRAME_WINDOW_P (f))
28539 define_frame_cursor1 (f, cursor, pointer);
28540 #endif
28544 /* EXPORT:
28545 Take proper action when the mouse has moved to position X, Y on
28546 frame F with regards to highlighting portions of display that have
28547 mouse-face properties. Also de-highlight portions of display where
28548 the mouse was before, set the mouse pointer shape as appropriate
28549 for the mouse coordinates, and activate help echo (tooltips).
28550 X and Y can be negative or out of range. */
28552 void
28553 note_mouse_highlight (struct frame *f, int x, int y)
28555 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28556 enum window_part part = ON_NOTHING;
28557 Lisp_Object window;
28558 struct window *w;
28559 Cursor cursor = No_Cursor;
28560 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28561 struct buffer *b;
28563 /* When a menu is active, don't highlight because this looks odd. */
28564 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28565 if (popup_activated ())
28566 return;
28567 #endif
28569 if (!f->glyphs_initialized_p
28570 || f->pointer_invisible)
28571 return;
28573 hlinfo->mouse_face_mouse_x = x;
28574 hlinfo->mouse_face_mouse_y = y;
28575 hlinfo->mouse_face_mouse_frame = f;
28577 if (hlinfo->mouse_face_defer)
28578 return;
28580 /* Which window is that in? */
28581 window = window_from_coordinates (f, x, y, &part, 1);
28583 /* If displaying active text in another window, clear that. */
28584 if (! EQ (window, hlinfo->mouse_face_window)
28585 /* Also clear if we move out of text area in same window. */
28586 || (!NILP (hlinfo->mouse_face_window)
28587 && !NILP (window)
28588 && part != ON_TEXT
28589 && part != ON_MODE_LINE
28590 && part != ON_HEADER_LINE))
28591 clear_mouse_face (hlinfo);
28593 /* Not on a window -> return. */
28594 if (!WINDOWP (window))
28595 return;
28597 /* Reset help_echo_string. It will get recomputed below. */
28598 help_echo_string = Qnil;
28600 /* Convert to window-relative pixel coordinates. */
28601 w = XWINDOW (window);
28602 frame_to_window_pixel_xy (w, &x, &y);
28604 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28605 /* Handle tool-bar window differently since it doesn't display a
28606 buffer. */
28607 if (EQ (window, f->tool_bar_window))
28609 note_tool_bar_highlight (f, x, y);
28610 return;
28612 #endif
28614 /* Mouse is on the mode, header line or margin? */
28615 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28616 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28618 note_mode_line_or_margin_highlight (window, x, y, part);
28620 #ifdef HAVE_WINDOW_SYSTEM
28621 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28623 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28624 /* Show non-text cursor (Bug#16647). */
28625 goto set_cursor;
28627 else
28628 #endif
28629 return;
28632 #ifdef HAVE_WINDOW_SYSTEM
28633 if (part == ON_VERTICAL_BORDER)
28635 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28636 help_echo_string = build_string ("drag-mouse-1: resize");
28638 else if (part == ON_RIGHT_DIVIDER)
28640 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28641 help_echo_string = build_string ("drag-mouse-1: resize");
28643 else if (part == ON_BOTTOM_DIVIDER)
28645 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28646 help_echo_string = build_string ("drag-mouse-1: resize");
28648 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28649 || part == ON_SCROLL_BAR)
28650 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28651 else
28652 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28653 #endif
28655 /* Are we in a window whose display is up to date?
28656 And verify the buffer's text has not changed. */
28657 b = XBUFFER (w->contents);
28658 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28660 int hpos, vpos, dx, dy, area = LAST_AREA;
28661 ptrdiff_t pos;
28662 struct glyph *glyph;
28663 Lisp_Object object;
28664 Lisp_Object mouse_face = Qnil, position;
28665 Lisp_Object *overlay_vec = NULL;
28666 ptrdiff_t i, noverlays;
28667 struct buffer *obuf;
28668 ptrdiff_t obegv, ozv;
28669 int same_region;
28671 /* Find the glyph under X/Y. */
28672 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28674 #ifdef HAVE_WINDOW_SYSTEM
28675 /* Look for :pointer property on image. */
28676 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28678 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28679 if (img != NULL && IMAGEP (img->spec))
28681 Lisp_Object image_map, hotspot;
28682 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28683 !NILP (image_map))
28684 && (hotspot = find_hot_spot (image_map,
28685 glyph->slice.img.x + dx,
28686 glyph->slice.img.y + dy),
28687 CONSP (hotspot))
28688 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28690 Lisp_Object plist;
28692 /* Could check XCAR (hotspot) to see if we enter/leave
28693 this hot-spot.
28694 If so, we could look for mouse-enter, mouse-leave
28695 properties in PLIST (and do something...). */
28696 hotspot = XCDR (hotspot);
28697 if (CONSP (hotspot)
28698 && (plist = XCAR (hotspot), CONSP (plist)))
28700 pointer = Fplist_get (plist, Qpointer);
28701 if (NILP (pointer))
28702 pointer = Qhand;
28703 help_echo_string = Fplist_get (plist, Qhelp_echo);
28704 if (!NILP (help_echo_string))
28706 help_echo_window = window;
28707 help_echo_object = glyph->object;
28708 help_echo_pos = glyph->charpos;
28712 if (NILP (pointer))
28713 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28716 #endif /* HAVE_WINDOW_SYSTEM */
28718 /* Clear mouse face if X/Y not over text. */
28719 if (glyph == NULL
28720 || area != TEXT_AREA
28721 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28722 /* Glyph's OBJECT is an integer for glyphs inserted by the
28723 display engine for its internal purposes, like truncation
28724 and continuation glyphs and blanks beyond the end of
28725 line's text on text terminals. If we are over such a
28726 glyph, we are not over any text. */
28727 || INTEGERP (glyph->object)
28728 /* R2L rows have a stretch glyph at their front, which
28729 stands for no text, whereas L2R rows have no glyphs at
28730 all beyond the end of text. Treat such stretch glyphs
28731 like we do with NULL glyphs in L2R rows. */
28732 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28733 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28734 && glyph->type == STRETCH_GLYPH
28735 && glyph->avoid_cursor_p))
28737 if (clear_mouse_face (hlinfo))
28738 cursor = No_Cursor;
28739 #ifdef HAVE_WINDOW_SYSTEM
28740 if (FRAME_WINDOW_P (f) && NILP (pointer))
28742 if (area != TEXT_AREA)
28743 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28744 else
28745 pointer = Vvoid_text_area_pointer;
28747 #endif
28748 goto set_cursor;
28751 pos = glyph->charpos;
28752 object = glyph->object;
28753 if (!STRINGP (object) && !BUFFERP (object))
28754 goto set_cursor;
28756 /* If we get an out-of-range value, return now; avoid an error. */
28757 if (BUFFERP (object) && pos > BUF_Z (b))
28758 goto set_cursor;
28760 /* Make the window's buffer temporarily current for
28761 overlays_at and compute_char_face. */
28762 obuf = current_buffer;
28763 current_buffer = b;
28764 obegv = BEGV;
28765 ozv = ZV;
28766 BEGV = BEG;
28767 ZV = Z;
28769 /* Is this char mouse-active or does it have help-echo? */
28770 position = make_number (pos);
28772 if (BUFFERP (object))
28774 /* Put all the overlays we want in a vector in overlay_vec. */
28775 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28776 /* Sort overlays into increasing priority order. */
28777 noverlays = sort_overlays (overlay_vec, noverlays, w);
28779 else
28780 noverlays = 0;
28782 if (NILP (Vmouse_highlight))
28784 clear_mouse_face (hlinfo);
28785 goto check_help_echo;
28788 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28790 if (same_region)
28791 cursor = No_Cursor;
28793 /* Check mouse-face highlighting. */
28794 if (! same_region
28795 /* If there exists an overlay with mouse-face overlapping
28796 the one we are currently highlighting, we have to
28797 check if we enter the overlapping overlay, and then
28798 highlight only that. */
28799 || (OVERLAYP (hlinfo->mouse_face_overlay)
28800 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28802 /* Find the highest priority overlay with a mouse-face. */
28803 Lisp_Object overlay = Qnil;
28804 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28806 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28807 if (!NILP (mouse_face))
28808 overlay = overlay_vec[i];
28811 /* If we're highlighting the same overlay as before, there's
28812 no need to do that again. */
28813 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28814 goto check_help_echo;
28815 hlinfo->mouse_face_overlay = overlay;
28817 /* Clear the display of the old active region, if any. */
28818 if (clear_mouse_face (hlinfo))
28819 cursor = No_Cursor;
28821 /* If no overlay applies, get a text property. */
28822 if (NILP (overlay))
28823 mouse_face = Fget_text_property (position, Qmouse_face, object);
28825 /* Next, compute the bounds of the mouse highlighting and
28826 display it. */
28827 if (!NILP (mouse_face) && STRINGP (object))
28829 /* The mouse-highlighting comes from a display string
28830 with a mouse-face. */
28831 Lisp_Object s, e;
28832 ptrdiff_t ignore;
28834 s = Fprevious_single_property_change
28835 (make_number (pos + 1), Qmouse_face, object, Qnil);
28836 e = Fnext_single_property_change
28837 (position, Qmouse_face, object, Qnil);
28838 if (NILP (s))
28839 s = make_number (0);
28840 if (NILP (e))
28841 e = make_number (SCHARS (object));
28842 mouse_face_from_string_pos (w, hlinfo, object,
28843 XINT (s), XINT (e));
28844 hlinfo->mouse_face_past_end = 0;
28845 hlinfo->mouse_face_window = window;
28846 hlinfo->mouse_face_face_id
28847 = face_at_string_position (w, object, pos, 0, &ignore,
28848 glyph->face_id, 1);
28849 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28850 cursor = No_Cursor;
28852 else
28854 /* The mouse-highlighting, if any, comes from an overlay
28855 or text property in the buffer. */
28856 Lisp_Object buffer IF_LINT (= Qnil);
28857 Lisp_Object disp_string IF_LINT (= Qnil);
28859 if (STRINGP (object))
28861 /* If we are on a display string with no mouse-face,
28862 check if the text under it has one. */
28863 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28864 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28865 pos = string_buffer_position (object, start);
28866 if (pos > 0)
28868 mouse_face = get_char_property_and_overlay
28869 (make_number (pos), Qmouse_face, w->contents, &overlay);
28870 buffer = w->contents;
28871 disp_string = object;
28874 else
28876 buffer = object;
28877 disp_string = Qnil;
28880 if (!NILP (mouse_face))
28882 Lisp_Object before, after;
28883 Lisp_Object before_string, after_string;
28884 /* To correctly find the limits of mouse highlight
28885 in a bidi-reordered buffer, we must not use the
28886 optimization of limiting the search in
28887 previous-single-property-change and
28888 next-single-property-change, because
28889 rows_from_pos_range needs the real start and end
28890 positions to DTRT in this case. That's because
28891 the first row visible in a window does not
28892 necessarily display the character whose position
28893 is the smallest. */
28894 Lisp_Object lim1
28895 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28896 ? Fmarker_position (w->start)
28897 : Qnil;
28898 Lisp_Object lim2
28899 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28900 ? make_number (BUF_Z (XBUFFER (buffer))
28901 - w->window_end_pos)
28902 : Qnil;
28904 if (NILP (overlay))
28906 /* Handle the text property case. */
28907 before = Fprevious_single_property_change
28908 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28909 after = Fnext_single_property_change
28910 (make_number (pos), Qmouse_face, buffer, lim2);
28911 before_string = after_string = Qnil;
28913 else
28915 /* Handle the overlay case. */
28916 before = Foverlay_start (overlay);
28917 after = Foverlay_end (overlay);
28918 before_string = Foverlay_get (overlay, Qbefore_string);
28919 after_string = Foverlay_get (overlay, Qafter_string);
28921 if (!STRINGP (before_string)) before_string = Qnil;
28922 if (!STRINGP (after_string)) after_string = Qnil;
28925 mouse_face_from_buffer_pos (window, hlinfo, pos,
28926 NILP (before)
28928 : XFASTINT (before),
28929 NILP (after)
28930 ? BUF_Z (XBUFFER (buffer))
28931 : XFASTINT (after),
28932 before_string, after_string,
28933 disp_string);
28934 cursor = No_Cursor;
28939 check_help_echo:
28941 /* Look for a `help-echo' property. */
28942 if (NILP (help_echo_string)) {
28943 Lisp_Object help, overlay;
28945 /* Check overlays first. */
28946 help = overlay = Qnil;
28947 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28949 overlay = overlay_vec[i];
28950 help = Foverlay_get (overlay, Qhelp_echo);
28953 if (!NILP (help))
28955 help_echo_string = help;
28956 help_echo_window = window;
28957 help_echo_object = overlay;
28958 help_echo_pos = pos;
28960 else
28962 Lisp_Object obj = glyph->object;
28963 ptrdiff_t charpos = glyph->charpos;
28965 /* Try text properties. */
28966 if (STRINGP (obj)
28967 && charpos >= 0
28968 && charpos < SCHARS (obj))
28970 help = Fget_text_property (make_number (charpos),
28971 Qhelp_echo, obj);
28972 if (NILP (help))
28974 /* If the string itself doesn't specify a help-echo,
28975 see if the buffer text ``under'' it does. */
28976 struct glyph_row *r
28977 = MATRIX_ROW (w->current_matrix, vpos);
28978 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28979 ptrdiff_t p = string_buffer_position (obj, start);
28980 if (p > 0)
28982 help = Fget_char_property (make_number (p),
28983 Qhelp_echo, w->contents);
28984 if (!NILP (help))
28986 charpos = p;
28987 obj = w->contents;
28992 else if (BUFFERP (obj)
28993 && charpos >= BEGV
28994 && charpos < ZV)
28995 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28996 obj);
28998 if (!NILP (help))
29000 help_echo_string = help;
29001 help_echo_window = window;
29002 help_echo_object = obj;
29003 help_echo_pos = charpos;
29008 #ifdef HAVE_WINDOW_SYSTEM
29009 /* Look for a `pointer' property. */
29010 if (FRAME_WINDOW_P (f) && NILP (pointer))
29012 /* Check overlays first. */
29013 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29014 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29016 if (NILP (pointer))
29018 Lisp_Object obj = glyph->object;
29019 ptrdiff_t charpos = glyph->charpos;
29021 /* Try text properties. */
29022 if (STRINGP (obj)
29023 && charpos >= 0
29024 && charpos < SCHARS (obj))
29026 pointer = Fget_text_property (make_number (charpos),
29027 Qpointer, obj);
29028 if (NILP (pointer))
29030 /* If the string itself doesn't specify a pointer,
29031 see if the buffer text ``under'' it does. */
29032 struct glyph_row *r
29033 = MATRIX_ROW (w->current_matrix, vpos);
29034 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29035 ptrdiff_t p = string_buffer_position (obj, start);
29036 if (p > 0)
29037 pointer = Fget_char_property (make_number (p),
29038 Qpointer, w->contents);
29041 else if (BUFFERP (obj)
29042 && charpos >= BEGV
29043 && charpos < ZV)
29044 pointer = Fget_text_property (make_number (charpos),
29045 Qpointer, obj);
29048 #endif /* HAVE_WINDOW_SYSTEM */
29050 BEGV = obegv;
29051 ZV = ozv;
29052 current_buffer = obuf;
29055 set_cursor:
29057 #ifdef HAVE_WINDOW_SYSTEM
29058 if (FRAME_WINDOW_P (f))
29059 define_frame_cursor1 (f, cursor, pointer);
29060 #else
29061 /* This is here to prevent a compiler error, about "label at end of
29062 compound statement". */
29063 return;
29064 #endif
29068 /* EXPORT for RIF:
29069 Clear any mouse-face on window W. This function is part of the
29070 redisplay interface, and is called from try_window_id and similar
29071 functions to ensure the mouse-highlight is off. */
29073 void
29074 x_clear_window_mouse_face (struct window *w)
29076 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29077 Lisp_Object window;
29079 block_input ();
29080 XSETWINDOW (window, w);
29081 if (EQ (window, hlinfo->mouse_face_window))
29082 clear_mouse_face (hlinfo);
29083 unblock_input ();
29087 /* EXPORT:
29088 Just discard the mouse face information for frame F, if any.
29089 This is used when the size of F is changed. */
29091 void
29092 cancel_mouse_face (struct frame *f)
29094 Lisp_Object window;
29095 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29097 window = hlinfo->mouse_face_window;
29098 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29099 reset_mouse_highlight (hlinfo);
29104 /***********************************************************************
29105 Exposure Events
29106 ***********************************************************************/
29108 #ifdef HAVE_WINDOW_SYSTEM
29110 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29111 which intersects rectangle R. R is in window-relative coordinates. */
29113 static void
29114 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29115 enum glyph_row_area area)
29117 struct glyph *first = row->glyphs[area];
29118 struct glyph *end = row->glyphs[area] + row->used[area];
29119 struct glyph *last;
29120 int first_x, start_x, x;
29122 if (area == TEXT_AREA && row->fill_line_p)
29123 /* If row extends face to end of line write the whole line. */
29124 draw_glyphs (w, 0, row, area,
29125 0, row->used[area],
29126 DRAW_NORMAL_TEXT, 0);
29127 else
29129 /* Set START_X to the window-relative start position for drawing glyphs of
29130 AREA. The first glyph of the text area can be partially visible.
29131 The first glyphs of other areas cannot. */
29132 start_x = window_box_left_offset (w, area);
29133 x = start_x;
29134 if (area == TEXT_AREA)
29135 x += row->x;
29137 /* Find the first glyph that must be redrawn. */
29138 while (first < end
29139 && x + first->pixel_width < r->x)
29141 x += first->pixel_width;
29142 ++first;
29145 /* Find the last one. */
29146 last = first;
29147 first_x = x;
29148 while (last < end
29149 && x < r->x + r->width)
29151 x += last->pixel_width;
29152 ++last;
29155 /* Repaint. */
29156 if (last > first)
29157 draw_glyphs (w, first_x - start_x, row, area,
29158 first - row->glyphs[area], last - row->glyphs[area],
29159 DRAW_NORMAL_TEXT, 0);
29164 /* Redraw the parts of the glyph row ROW on window W intersecting
29165 rectangle R. R is in window-relative coordinates. Value is
29166 non-zero if mouse-face was overwritten. */
29168 static int
29169 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29171 eassert (row->enabled_p);
29173 if (row->mode_line_p || w->pseudo_window_p)
29174 draw_glyphs (w, 0, row, TEXT_AREA,
29175 0, row->used[TEXT_AREA],
29176 DRAW_NORMAL_TEXT, 0);
29177 else
29179 if (row->used[LEFT_MARGIN_AREA])
29180 expose_area (w, row, r, LEFT_MARGIN_AREA);
29181 if (row->used[TEXT_AREA])
29182 expose_area (w, row, r, TEXT_AREA);
29183 if (row->used[RIGHT_MARGIN_AREA])
29184 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29185 draw_row_fringe_bitmaps (w, row);
29188 return row->mouse_face_p;
29192 /* Redraw those parts of glyphs rows during expose event handling that
29193 overlap other rows. Redrawing of an exposed line writes over parts
29194 of lines overlapping that exposed line; this function fixes that.
29196 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29197 row in W's current matrix that is exposed and overlaps other rows.
29198 LAST_OVERLAPPING_ROW is the last such row. */
29200 static void
29201 expose_overlaps (struct window *w,
29202 struct glyph_row *first_overlapping_row,
29203 struct glyph_row *last_overlapping_row,
29204 XRectangle *r)
29206 struct glyph_row *row;
29208 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29209 if (row->overlapping_p)
29211 eassert (row->enabled_p && !row->mode_line_p);
29213 row->clip = r;
29214 if (row->used[LEFT_MARGIN_AREA])
29215 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29217 if (row->used[TEXT_AREA])
29218 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29220 if (row->used[RIGHT_MARGIN_AREA])
29221 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29222 row->clip = NULL;
29227 /* Return non-zero if W's cursor intersects rectangle R. */
29229 static int
29230 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29232 XRectangle cr, result;
29233 struct glyph *cursor_glyph;
29234 struct glyph_row *row;
29236 if (w->phys_cursor.vpos >= 0
29237 && w->phys_cursor.vpos < w->current_matrix->nrows
29238 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29239 row->enabled_p)
29240 && row->cursor_in_fringe_p)
29242 /* Cursor is in the fringe. */
29243 cr.x = window_box_right_offset (w,
29244 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29245 ? RIGHT_MARGIN_AREA
29246 : TEXT_AREA));
29247 cr.y = row->y;
29248 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29249 cr.height = row->height;
29250 return x_intersect_rectangles (&cr, r, &result);
29253 cursor_glyph = get_phys_cursor_glyph (w);
29254 if (cursor_glyph)
29256 /* r is relative to W's box, but w->phys_cursor.x is relative
29257 to left edge of W's TEXT area. Adjust it. */
29258 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29259 cr.y = w->phys_cursor.y;
29260 cr.width = cursor_glyph->pixel_width;
29261 cr.height = w->phys_cursor_height;
29262 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29263 I assume the effect is the same -- and this is portable. */
29264 return x_intersect_rectangles (&cr, r, &result);
29266 /* If we don't understand the format, pretend we're not in the hot-spot. */
29267 return 0;
29271 /* EXPORT:
29272 Draw a vertical window border to the right of window W if W doesn't
29273 have vertical scroll bars. */
29275 void
29276 x_draw_vertical_border (struct window *w)
29278 struct frame *f = XFRAME (WINDOW_FRAME (w));
29280 /* We could do better, if we knew what type of scroll-bar the adjacent
29281 windows (on either side) have... But we don't :-(
29282 However, I think this works ok. ++KFS 2003-04-25 */
29284 /* Redraw borders between horizontally adjacent windows. Don't
29285 do it for frames with vertical scroll bars because either the
29286 right scroll bar of a window, or the left scroll bar of its
29287 neighbor will suffice as a border. */
29288 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29289 return;
29291 /* Note: It is necessary to redraw both the left and the right
29292 borders, for when only this single window W is being
29293 redisplayed. */
29294 if (!WINDOW_RIGHTMOST_P (w)
29295 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29297 int x0, x1, y0, y1;
29299 window_box_edges (w, &x0, &y0, &x1, &y1);
29300 y1 -= 1;
29302 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29303 x1 -= 1;
29305 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29308 if (!WINDOW_LEFTMOST_P (w)
29309 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29311 int x0, x1, y0, y1;
29313 window_box_edges (w, &x0, &y0, &x1, &y1);
29314 y1 -= 1;
29316 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29317 x0 -= 1;
29319 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29324 /* Draw window dividers for window W. */
29326 void
29327 x_draw_right_divider (struct window *w)
29329 struct frame *f = WINDOW_XFRAME (w);
29331 if (w->mini || w->pseudo_window_p)
29332 return;
29333 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29335 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29336 int x1 = WINDOW_RIGHT_EDGE_X (w);
29337 int y0 = WINDOW_TOP_EDGE_Y (w);
29338 /* The bottom divider prevails. */
29339 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29341 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29345 static void
29346 x_draw_bottom_divider (struct window *w)
29348 struct frame *f = XFRAME (WINDOW_FRAME (w));
29350 if (w->mini || w->pseudo_window_p)
29351 return;
29352 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29354 int x0 = WINDOW_LEFT_EDGE_X (w);
29355 int x1 = WINDOW_RIGHT_EDGE_X (w);
29356 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29357 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29359 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29363 /* Redraw the part of window W intersection rectangle FR. Pixel
29364 coordinates in FR are frame-relative. Call this function with
29365 input blocked. Value is non-zero if the exposure overwrites
29366 mouse-face. */
29368 static int
29369 expose_window (struct window *w, XRectangle *fr)
29371 struct frame *f = XFRAME (w->frame);
29372 XRectangle wr, r;
29373 int mouse_face_overwritten_p = 0;
29375 /* If window is not yet fully initialized, do nothing. This can
29376 happen when toolkit scroll bars are used and a window is split.
29377 Reconfiguring the scroll bar will generate an expose for a newly
29378 created window. */
29379 if (w->current_matrix == NULL)
29380 return 0;
29382 /* When we're currently updating the window, display and current
29383 matrix usually don't agree. Arrange for a thorough display
29384 later. */
29385 if (w->must_be_updated_p)
29387 SET_FRAME_GARBAGED (f);
29388 return 0;
29391 /* Frame-relative pixel rectangle of W. */
29392 wr.x = WINDOW_LEFT_EDGE_X (w);
29393 wr.y = WINDOW_TOP_EDGE_Y (w);
29394 wr.width = WINDOW_PIXEL_WIDTH (w);
29395 wr.height = WINDOW_PIXEL_HEIGHT (w);
29397 if (x_intersect_rectangles (fr, &wr, &r))
29399 int yb = window_text_bottom_y (w);
29400 struct glyph_row *row;
29401 int cursor_cleared_p, phys_cursor_on_p;
29402 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29404 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29405 r.x, r.y, r.width, r.height));
29407 /* Convert to window coordinates. */
29408 r.x -= WINDOW_LEFT_EDGE_X (w);
29409 r.y -= WINDOW_TOP_EDGE_Y (w);
29411 /* Turn off the cursor. */
29412 if (!w->pseudo_window_p
29413 && phys_cursor_in_rect_p (w, &r))
29415 x_clear_cursor (w);
29416 cursor_cleared_p = 1;
29418 else
29419 cursor_cleared_p = 0;
29421 /* If the row containing the cursor extends face to end of line,
29422 then expose_area might overwrite the cursor outside the
29423 rectangle and thus notice_overwritten_cursor might clear
29424 w->phys_cursor_on_p. We remember the original value and
29425 check later if it is changed. */
29426 phys_cursor_on_p = w->phys_cursor_on_p;
29428 /* Update lines intersecting rectangle R. */
29429 first_overlapping_row = last_overlapping_row = NULL;
29430 for (row = w->current_matrix->rows;
29431 row->enabled_p;
29432 ++row)
29434 int y0 = row->y;
29435 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29437 if ((y0 >= r.y && y0 < r.y + r.height)
29438 || (y1 > r.y && y1 < r.y + r.height)
29439 || (r.y >= y0 && r.y < y1)
29440 || (r.y + r.height > y0 && r.y + r.height < y1))
29442 /* A header line may be overlapping, but there is no need
29443 to fix overlapping areas for them. KFS 2005-02-12 */
29444 if (row->overlapping_p && !row->mode_line_p)
29446 if (first_overlapping_row == NULL)
29447 first_overlapping_row = row;
29448 last_overlapping_row = row;
29451 row->clip = fr;
29452 if (expose_line (w, row, &r))
29453 mouse_face_overwritten_p = 1;
29454 row->clip = NULL;
29456 else if (row->overlapping_p)
29458 /* We must redraw a row overlapping the exposed area. */
29459 if (y0 < r.y
29460 ? y0 + row->phys_height > r.y
29461 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29463 if (first_overlapping_row == NULL)
29464 first_overlapping_row = row;
29465 last_overlapping_row = row;
29469 if (y1 >= yb)
29470 break;
29473 /* Display the mode line if there is one. */
29474 if (WINDOW_WANTS_MODELINE_P (w)
29475 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29476 row->enabled_p)
29477 && row->y < r.y + r.height)
29479 if (expose_line (w, row, &r))
29480 mouse_face_overwritten_p = 1;
29483 if (!w->pseudo_window_p)
29485 /* Fix the display of overlapping rows. */
29486 if (first_overlapping_row)
29487 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29488 fr);
29490 /* Draw border between windows. */
29491 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29492 x_draw_right_divider (w);
29493 else
29494 x_draw_vertical_border (w);
29496 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29497 x_draw_bottom_divider (w);
29499 /* Turn the cursor on again. */
29500 if (cursor_cleared_p
29501 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29502 update_window_cursor (w, 1);
29506 return mouse_face_overwritten_p;
29511 /* Redraw (parts) of all windows in the window tree rooted at W that
29512 intersect R. R contains frame pixel coordinates. Value is
29513 non-zero if the exposure overwrites mouse-face. */
29515 static int
29516 expose_window_tree (struct window *w, XRectangle *r)
29518 struct frame *f = XFRAME (w->frame);
29519 int mouse_face_overwritten_p = 0;
29521 while (w && !FRAME_GARBAGED_P (f))
29523 if (WINDOWP (w->contents))
29524 mouse_face_overwritten_p
29525 |= expose_window_tree (XWINDOW (w->contents), r);
29526 else
29527 mouse_face_overwritten_p |= expose_window (w, r);
29529 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29532 return mouse_face_overwritten_p;
29536 /* EXPORT:
29537 Redisplay an exposed area of frame F. X and Y are the upper-left
29538 corner of the exposed rectangle. W and H are width and height of
29539 the exposed area. All are pixel values. W or H zero means redraw
29540 the entire frame. */
29542 void
29543 expose_frame (struct frame *f, int x, int y, int w, int h)
29545 XRectangle r;
29546 int mouse_face_overwritten_p = 0;
29548 TRACE ((stderr, "expose_frame "));
29550 /* No need to redraw if frame will be redrawn soon. */
29551 if (FRAME_GARBAGED_P (f))
29553 TRACE ((stderr, " garbaged\n"));
29554 return;
29557 /* If basic faces haven't been realized yet, there is no point in
29558 trying to redraw anything. This can happen when we get an expose
29559 event while Emacs is starting, e.g. by moving another window. */
29560 if (FRAME_FACE_CACHE (f) == NULL
29561 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29563 TRACE ((stderr, " no faces\n"));
29564 return;
29567 if (w == 0 || h == 0)
29569 r.x = r.y = 0;
29570 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29571 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29573 else
29575 r.x = x;
29576 r.y = y;
29577 r.width = w;
29578 r.height = h;
29581 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29582 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29584 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29585 if (WINDOWP (f->tool_bar_window))
29586 mouse_face_overwritten_p
29587 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29588 #endif
29590 #ifdef HAVE_X_WINDOWS
29591 #ifndef MSDOS
29592 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29593 if (WINDOWP (f->menu_bar_window))
29594 mouse_face_overwritten_p
29595 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29596 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29597 #endif
29598 #endif
29600 /* Some window managers support a focus-follows-mouse style with
29601 delayed raising of frames. Imagine a partially obscured frame,
29602 and moving the mouse into partially obscured mouse-face on that
29603 frame. The visible part of the mouse-face will be highlighted,
29604 then the WM raises the obscured frame. With at least one WM, KDE
29605 2.1, Emacs is not getting any event for the raising of the frame
29606 (even tried with SubstructureRedirectMask), only Expose events.
29607 These expose events will draw text normally, i.e. not
29608 highlighted. Which means we must redo the highlight here.
29609 Subsume it under ``we love X''. --gerd 2001-08-15 */
29610 /* Included in Windows version because Windows most likely does not
29611 do the right thing if any third party tool offers
29612 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29613 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29615 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29616 if (f == hlinfo->mouse_face_mouse_frame)
29618 int mouse_x = hlinfo->mouse_face_mouse_x;
29619 int mouse_y = hlinfo->mouse_face_mouse_y;
29620 clear_mouse_face (hlinfo);
29621 note_mouse_highlight (f, mouse_x, mouse_y);
29627 /* EXPORT:
29628 Determine the intersection of two rectangles R1 and R2. Return
29629 the intersection in *RESULT. Value is non-zero if RESULT is not
29630 empty. */
29633 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29635 XRectangle *left, *right;
29636 XRectangle *upper, *lower;
29637 int intersection_p = 0;
29639 /* Rearrange so that R1 is the left-most rectangle. */
29640 if (r1->x < r2->x)
29641 left = r1, right = r2;
29642 else
29643 left = r2, right = r1;
29645 /* X0 of the intersection is right.x0, if this is inside R1,
29646 otherwise there is no intersection. */
29647 if (right->x <= left->x + left->width)
29649 result->x = right->x;
29651 /* The right end of the intersection is the minimum of
29652 the right ends of left and right. */
29653 result->width = (min (left->x + left->width, right->x + right->width)
29654 - result->x);
29656 /* Same game for Y. */
29657 if (r1->y < r2->y)
29658 upper = r1, lower = r2;
29659 else
29660 upper = r2, lower = r1;
29662 /* The upper end of the intersection is lower.y0, if this is inside
29663 of upper. Otherwise, there is no intersection. */
29664 if (lower->y <= upper->y + upper->height)
29666 result->y = lower->y;
29668 /* The lower end of the intersection is the minimum of the lower
29669 ends of upper and lower. */
29670 result->height = (min (lower->y + lower->height,
29671 upper->y + upper->height)
29672 - result->y);
29673 intersection_p = 1;
29677 return intersection_p;
29680 #endif /* HAVE_WINDOW_SYSTEM */
29683 /***********************************************************************
29684 Initialization
29685 ***********************************************************************/
29687 void
29688 syms_of_xdisp (void)
29690 Vwith_echo_area_save_vector = Qnil;
29691 staticpro (&Vwith_echo_area_save_vector);
29693 Vmessage_stack = Qnil;
29694 staticpro (&Vmessage_stack);
29696 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29697 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29699 message_dolog_marker1 = Fmake_marker ();
29700 staticpro (&message_dolog_marker1);
29701 message_dolog_marker2 = Fmake_marker ();
29702 staticpro (&message_dolog_marker2);
29703 message_dolog_marker3 = Fmake_marker ();
29704 staticpro (&message_dolog_marker3);
29706 #ifdef GLYPH_DEBUG
29707 defsubr (&Sdump_frame_glyph_matrix);
29708 defsubr (&Sdump_glyph_matrix);
29709 defsubr (&Sdump_glyph_row);
29710 defsubr (&Sdump_tool_bar_row);
29711 defsubr (&Strace_redisplay);
29712 defsubr (&Strace_to_stderr);
29713 #endif
29714 #ifdef HAVE_WINDOW_SYSTEM
29715 defsubr (&Stool_bar_height);
29716 defsubr (&Slookup_image_map);
29717 #endif
29718 defsubr (&Sline_pixel_height);
29719 defsubr (&Sformat_mode_line);
29720 defsubr (&Sinvisible_p);
29721 defsubr (&Scurrent_bidi_paragraph_direction);
29722 defsubr (&Swindow_text_pixel_size);
29723 defsubr (&Smove_point_visually);
29725 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29726 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29727 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29728 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29729 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29730 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29731 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29732 DEFSYM (Qeval, "eval");
29733 DEFSYM (QCdata, ":data");
29734 DEFSYM (Qdisplay, "display");
29735 DEFSYM (Qspace_width, "space-width");
29736 DEFSYM (Qraise, "raise");
29737 DEFSYM (Qslice, "slice");
29738 DEFSYM (Qspace, "space");
29739 DEFSYM (Qmargin, "margin");
29740 DEFSYM (Qpointer, "pointer");
29741 DEFSYM (Qleft_margin, "left-margin");
29742 DEFSYM (Qright_margin, "right-margin");
29743 DEFSYM (Qcenter, "center");
29744 DEFSYM (Qline_height, "line-height");
29745 DEFSYM (QCalign_to, ":align-to");
29746 DEFSYM (QCrelative_width, ":relative-width");
29747 DEFSYM (QCrelative_height, ":relative-height");
29748 DEFSYM (QCeval, ":eval");
29749 DEFSYM (QCpropertize, ":propertize");
29750 DEFSYM (QCfile, ":file");
29751 DEFSYM (Qfontified, "fontified");
29752 DEFSYM (Qfontification_functions, "fontification-functions");
29753 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29754 DEFSYM (Qescape_glyph, "escape-glyph");
29755 DEFSYM (Qnobreak_space, "nobreak-space");
29756 DEFSYM (Qimage, "image");
29757 DEFSYM (Qtext, "text");
29758 DEFSYM (Qboth, "both");
29759 DEFSYM (Qboth_horiz, "both-horiz");
29760 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29761 DEFSYM (QCmap, ":map");
29762 DEFSYM (QCpointer, ":pointer");
29763 DEFSYM (Qrect, "rect");
29764 DEFSYM (Qcircle, "circle");
29765 DEFSYM (Qpoly, "poly");
29766 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29767 DEFSYM (Qgrow_only, "grow-only");
29768 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29769 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29770 DEFSYM (Qposition, "position");
29771 DEFSYM (Qbuffer_position, "buffer-position");
29772 DEFSYM (Qobject, "object");
29773 DEFSYM (Qbar, "bar");
29774 DEFSYM (Qhbar, "hbar");
29775 DEFSYM (Qbox, "box");
29776 DEFSYM (Qhollow, "hollow");
29777 DEFSYM (Qhand, "hand");
29778 DEFSYM (Qarrow, "arrow");
29779 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29781 list_of_error = list1 (list2 (intern_c_string ("error"),
29782 intern_c_string ("void-variable")));
29783 staticpro (&list_of_error);
29785 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29786 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29787 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29788 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29790 echo_buffer[0] = echo_buffer[1] = Qnil;
29791 staticpro (&echo_buffer[0]);
29792 staticpro (&echo_buffer[1]);
29794 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29795 staticpro (&echo_area_buffer[0]);
29796 staticpro (&echo_area_buffer[1]);
29798 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29799 staticpro (&Vmessages_buffer_name);
29801 mode_line_proptrans_alist = Qnil;
29802 staticpro (&mode_line_proptrans_alist);
29803 mode_line_string_list = Qnil;
29804 staticpro (&mode_line_string_list);
29805 mode_line_string_face = Qnil;
29806 staticpro (&mode_line_string_face);
29807 mode_line_string_face_prop = Qnil;
29808 staticpro (&mode_line_string_face_prop);
29809 Vmode_line_unwind_vector = Qnil;
29810 staticpro (&Vmode_line_unwind_vector);
29812 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29814 help_echo_string = Qnil;
29815 staticpro (&help_echo_string);
29816 help_echo_object = Qnil;
29817 staticpro (&help_echo_object);
29818 help_echo_window = Qnil;
29819 staticpro (&help_echo_window);
29820 previous_help_echo_string = Qnil;
29821 staticpro (&previous_help_echo_string);
29822 help_echo_pos = -1;
29824 DEFSYM (Qright_to_left, "right-to-left");
29825 DEFSYM (Qleft_to_right, "left-to-right");
29827 #ifdef HAVE_WINDOW_SYSTEM
29828 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29829 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29830 For example, if a block cursor is over a tab, it will be drawn as
29831 wide as that tab on the display. */);
29832 x_stretch_cursor_p = 0;
29833 #endif
29835 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29836 doc: /* Non-nil means highlight trailing whitespace.
29837 The face used for trailing whitespace is `trailing-whitespace'. */);
29838 Vshow_trailing_whitespace = Qnil;
29840 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29841 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29842 If the value is t, Emacs highlights non-ASCII chars which have the
29843 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29844 or `escape-glyph' face respectively.
29846 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29847 U+2011 (non-breaking hyphen) are affected.
29849 Any other non-nil value means to display these characters as a escape
29850 glyph followed by an ordinary space or hyphen.
29852 A value of nil means no special handling of these characters. */);
29853 Vnobreak_char_display = Qt;
29855 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29856 doc: /* The pointer shape to show in void text areas.
29857 A value of nil means to show the text pointer. Other options are `arrow',
29858 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29859 Vvoid_text_area_pointer = Qarrow;
29861 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29862 doc: /* Non-nil means don't actually do any redisplay.
29863 This is used for internal purposes. */);
29864 Vinhibit_redisplay = Qnil;
29866 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29867 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29868 Vglobal_mode_string = Qnil;
29870 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29871 doc: /* Marker for where to display an arrow on top of the buffer text.
29872 This must be the beginning of a line in order to work.
29873 See also `overlay-arrow-string'. */);
29874 Voverlay_arrow_position = Qnil;
29876 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29877 doc: /* String to display as an arrow in non-window frames.
29878 See also `overlay-arrow-position'. */);
29879 Voverlay_arrow_string = build_pure_c_string ("=>");
29881 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29882 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29883 The symbols on this list are examined during redisplay to determine
29884 where to display overlay arrows. */);
29885 Voverlay_arrow_variable_list
29886 = list1 (intern_c_string ("overlay-arrow-position"));
29888 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29889 doc: /* The number of lines to try scrolling a window by when point moves out.
29890 If that fails to bring point back on frame, point is centered instead.
29891 If this is zero, point is always centered after it moves off frame.
29892 If you want scrolling to always be a line at a time, you should set
29893 `scroll-conservatively' to a large value rather than set this to 1. */);
29895 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29896 doc: /* Scroll up to this many lines, to bring point back on screen.
29897 If point moves off-screen, redisplay will scroll by up to
29898 `scroll-conservatively' lines in order to bring point just barely
29899 onto the screen again. If that cannot be done, then redisplay
29900 recenters point as usual.
29902 If the value is greater than 100, redisplay will never recenter point,
29903 but will always scroll just enough text to bring point into view, even
29904 if you move far away.
29906 A value of zero means always recenter point if it moves off screen. */);
29907 scroll_conservatively = 0;
29909 DEFVAR_INT ("scroll-margin", scroll_margin,
29910 doc: /* Number of lines of margin at the top and bottom of a window.
29911 Recenter the window whenever point gets within this many lines
29912 of the top or bottom of the window. */);
29913 scroll_margin = 0;
29915 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29916 doc: /* Pixels per inch value for non-window system displays.
29917 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29918 Vdisplay_pixels_per_inch = make_float (72.0);
29920 #ifdef GLYPH_DEBUG
29921 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29922 #endif
29924 DEFVAR_LISP ("truncate-partial-width-windows",
29925 Vtruncate_partial_width_windows,
29926 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29927 For an integer value, truncate lines in each window narrower than the
29928 full frame width, provided the window width is less than that integer;
29929 otherwise, respect the value of `truncate-lines'.
29931 For any other non-nil value, truncate lines in all windows that do
29932 not span the full frame width.
29934 A value of nil means to respect the value of `truncate-lines'.
29936 If `word-wrap' is enabled, you might want to reduce this. */);
29937 Vtruncate_partial_width_windows = make_number (50);
29939 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29940 doc: /* Maximum buffer size for which line number should be displayed.
29941 If the buffer is bigger than this, the line number does not appear
29942 in the mode line. A value of nil means no limit. */);
29943 Vline_number_display_limit = Qnil;
29945 DEFVAR_INT ("line-number-display-limit-width",
29946 line_number_display_limit_width,
29947 doc: /* Maximum line width (in characters) for line number display.
29948 If the average length of the lines near point is bigger than this, then the
29949 line number may be omitted from the mode line. */);
29950 line_number_display_limit_width = 200;
29952 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29953 doc: /* Non-nil means highlight region even in nonselected windows. */);
29954 highlight_nonselected_windows = 0;
29956 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29957 doc: /* Non-nil if more than one frame is visible on this display.
29958 Minibuffer-only frames don't count, but iconified frames do.
29959 This variable is not guaranteed to be accurate except while processing
29960 `frame-title-format' and `icon-title-format'. */);
29962 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29963 doc: /* Template for displaying the title bar of visible frames.
29964 \(Assuming the window manager supports this feature.)
29966 This variable has the same structure as `mode-line-format', except that
29967 the %c and %l constructs are ignored. It is used only on frames for
29968 which no explicit name has been set \(see `modify-frame-parameters'). */);
29970 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29971 doc: /* Template for displaying the title bar of an iconified frame.
29972 \(Assuming the window manager supports this feature.)
29973 This variable has the same structure as `mode-line-format' (which see),
29974 and is used only on frames for which no explicit name has been set
29975 \(see `modify-frame-parameters'). */);
29976 Vicon_title_format
29977 = Vframe_title_format
29978 = listn (CONSTYPE_PURE, 3,
29979 intern_c_string ("multiple-frames"),
29980 build_pure_c_string ("%b"),
29981 listn (CONSTYPE_PURE, 4,
29982 empty_unibyte_string,
29983 intern_c_string ("invocation-name"),
29984 build_pure_c_string ("@"),
29985 intern_c_string ("system-name")));
29987 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29988 doc: /* Maximum number of lines to keep in the message log buffer.
29989 If nil, disable message logging. If t, log messages but don't truncate
29990 the buffer when it becomes large. */);
29991 Vmessage_log_max = make_number (1000);
29993 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29994 doc: /* Functions called before redisplay, if window sizes have changed.
29995 The value should be a list of functions that take one argument.
29996 Just before redisplay, for each frame, if any of its windows have changed
29997 size since the last redisplay, or have been split or deleted,
29998 all the functions in the list are called, with the frame as argument. */);
29999 Vwindow_size_change_functions = Qnil;
30001 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30002 doc: /* List of functions to call before redisplaying a window with scrolling.
30003 Each function is called with two arguments, the window and its new
30004 display-start position. Note that these functions are also called by
30005 `set-window-buffer'. Also note that the value of `window-end' is not
30006 valid when these functions are called.
30008 Warning: Do not use this feature to alter the way the window
30009 is scrolled. It is not designed for that, and such use probably won't
30010 work. */);
30011 Vwindow_scroll_functions = Qnil;
30013 DEFVAR_LISP ("window-text-change-functions",
30014 Vwindow_text_change_functions,
30015 doc: /* Functions to call in redisplay when text in the window might change. */);
30016 Vwindow_text_change_functions = Qnil;
30018 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30019 doc: /* Functions called when redisplay of a window reaches the end trigger.
30020 Each function is called with two arguments, the window and the end trigger value.
30021 See `set-window-redisplay-end-trigger'. */);
30022 Vredisplay_end_trigger_functions = Qnil;
30024 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30025 doc: /* Non-nil means autoselect window with mouse pointer.
30026 If nil, do not autoselect windows.
30027 A positive number means delay autoselection by that many seconds: a
30028 window is autoselected only after the mouse has remained in that
30029 window for the duration of the delay.
30030 A negative number has a similar effect, but causes windows to be
30031 autoselected only after the mouse has stopped moving. \(Because of
30032 the way Emacs compares mouse events, you will occasionally wait twice
30033 that time before the window gets selected.\)
30034 Any other value means to autoselect window instantaneously when the
30035 mouse pointer enters it.
30037 Autoselection selects the minibuffer only if it is active, and never
30038 unselects the minibuffer if it is active.
30040 When customizing this variable make sure that the actual value of
30041 `focus-follows-mouse' matches the behavior of your window manager. */);
30042 Vmouse_autoselect_window = Qnil;
30044 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30045 doc: /* Non-nil means automatically resize tool-bars.
30046 This dynamically changes the tool-bar's height to the minimum height
30047 that is needed to make all tool-bar items visible.
30048 If value is `grow-only', the tool-bar's height is only increased
30049 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30050 Vauto_resize_tool_bars = Qt;
30052 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30053 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30054 auto_raise_tool_bar_buttons_p = 1;
30056 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30057 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30058 make_cursor_line_fully_visible_p = 1;
30060 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30061 doc: /* Border below tool-bar in pixels.
30062 If an integer, use it as the height of the border.
30063 If it is one of `internal-border-width' or `border-width', use the
30064 value of the corresponding frame parameter.
30065 Otherwise, no border is added below the tool-bar. */);
30066 Vtool_bar_border = Qinternal_border_width;
30068 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30069 doc: /* Margin around tool-bar buttons in pixels.
30070 If an integer, use that for both horizontal and vertical margins.
30071 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30072 HORZ specifying the horizontal margin, and VERT specifying the
30073 vertical margin. */);
30074 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30076 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30077 doc: /* Relief thickness of tool-bar buttons. */);
30078 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30080 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30081 doc: /* Tool bar style to use.
30082 It can be one of
30083 image - show images only
30084 text - show text only
30085 both - show both, text below image
30086 both-horiz - show text to the right of the image
30087 text-image-horiz - show text to the left of the image
30088 any other - use system default or image if no system default.
30090 This variable only affects the GTK+ toolkit version of Emacs. */);
30091 Vtool_bar_style = Qnil;
30093 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30094 doc: /* Maximum number of characters a label can have to be shown.
30095 The tool bar style must also show labels for this to have any effect, see
30096 `tool-bar-style'. */);
30097 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30099 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30100 doc: /* List of functions to call to fontify regions of text.
30101 Each function is called with one argument POS. Functions must
30102 fontify a region starting at POS in the current buffer, and give
30103 fontified regions the property `fontified'. */);
30104 Vfontification_functions = Qnil;
30105 Fmake_variable_buffer_local (Qfontification_functions);
30107 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30108 unibyte_display_via_language_environment,
30109 doc: /* Non-nil means display unibyte text according to language environment.
30110 Specifically, this means that raw bytes in the range 160-255 decimal
30111 are displayed by converting them to the equivalent multibyte characters
30112 according to the current language environment. As a result, they are
30113 displayed according to the current fontset.
30115 Note that this variable affects only how these bytes are displayed,
30116 but does not change the fact they are interpreted as raw bytes. */);
30117 unibyte_display_via_language_environment = 0;
30119 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30120 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30121 If a float, it specifies a fraction of the mini-window frame's height.
30122 If an integer, it specifies a number of lines. */);
30123 Vmax_mini_window_height = make_float (0.25);
30125 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30126 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30127 A value of nil means don't automatically resize mini-windows.
30128 A value of t means resize them to fit the text displayed in them.
30129 A value of `grow-only', the default, means let mini-windows grow only;
30130 they return to their normal size when the minibuffer is closed, or the
30131 echo area becomes empty. */);
30132 Vresize_mini_windows = Qgrow_only;
30134 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30135 doc: /* Alist specifying how to blink the cursor off.
30136 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30137 `cursor-type' frame-parameter or variable equals ON-STATE,
30138 comparing using `equal', Emacs uses OFF-STATE to specify
30139 how to blink it off. ON-STATE and OFF-STATE are values for
30140 the `cursor-type' frame parameter.
30142 If a frame's ON-STATE has no entry in this list,
30143 the frame's other specifications determine how to blink the cursor off. */);
30144 Vblink_cursor_alist = Qnil;
30146 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30147 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30148 If non-nil, windows are automatically scrolled horizontally to make
30149 point visible. */);
30150 automatic_hscrolling_p = 1;
30151 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30153 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30154 doc: /* How many columns away from the window edge point is allowed to get
30155 before automatic hscrolling will horizontally scroll the window. */);
30156 hscroll_margin = 5;
30158 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30159 doc: /* How many columns to scroll the window when point gets too close to the edge.
30160 When point is less than `hscroll-margin' columns from the window
30161 edge, automatic hscrolling will scroll the window by the amount of columns
30162 determined by this variable. If its value is a positive integer, scroll that
30163 many columns. If it's a positive floating-point number, it specifies the
30164 fraction of the window's width to scroll. If it's nil or zero, point will be
30165 centered horizontally after the scroll. Any other value, including negative
30166 numbers, are treated as if the value were zero.
30168 Automatic hscrolling always moves point outside the scroll margin, so if
30169 point was more than scroll step columns inside the margin, the window will
30170 scroll more than the value given by the scroll step.
30172 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30173 and `scroll-right' overrides this variable's effect. */);
30174 Vhscroll_step = make_number (0);
30176 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30177 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30178 Bind this around calls to `message' to let it take effect. */);
30179 message_truncate_lines = 0;
30181 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30182 doc: /* Normal hook run to update the menu bar definitions.
30183 Redisplay runs this hook before it redisplays the menu bar.
30184 This is used to update menus such as Buffers, whose contents depend on
30185 various data. */);
30186 Vmenu_bar_update_hook = Qnil;
30188 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30189 doc: /* Frame for which we are updating a menu.
30190 The enable predicate for a menu binding should check this variable. */);
30191 Vmenu_updating_frame = Qnil;
30193 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30194 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30195 inhibit_menubar_update = 0;
30197 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30198 doc: /* Prefix prepended to all continuation lines at display time.
30199 The value may be a string, an image, or a stretch-glyph; it is
30200 interpreted in the same way as the value of a `display' text property.
30202 This variable is overridden by any `wrap-prefix' text or overlay
30203 property.
30205 To add a prefix to non-continuation lines, use `line-prefix'. */);
30206 Vwrap_prefix = Qnil;
30207 DEFSYM (Qwrap_prefix, "wrap-prefix");
30208 Fmake_variable_buffer_local (Qwrap_prefix);
30210 DEFVAR_LISP ("line-prefix", Vline_prefix,
30211 doc: /* Prefix prepended to all non-continuation lines at display time.
30212 The value may be a string, an image, or a stretch-glyph; it is
30213 interpreted in the same way as the value of a `display' text property.
30215 This variable is overridden by any `line-prefix' text or overlay
30216 property.
30218 To add a prefix to continuation lines, use `wrap-prefix'. */);
30219 Vline_prefix = Qnil;
30220 DEFSYM (Qline_prefix, "line-prefix");
30221 Fmake_variable_buffer_local (Qline_prefix);
30223 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30224 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30225 inhibit_eval_during_redisplay = 0;
30227 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30228 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30229 inhibit_free_realized_faces = 0;
30231 #ifdef GLYPH_DEBUG
30232 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30233 doc: /* Inhibit try_window_id display optimization. */);
30234 inhibit_try_window_id = 0;
30236 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30237 doc: /* Inhibit try_window_reusing display optimization. */);
30238 inhibit_try_window_reusing = 0;
30240 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30241 doc: /* Inhibit try_cursor_movement display optimization. */);
30242 inhibit_try_cursor_movement = 0;
30243 #endif /* GLYPH_DEBUG */
30245 DEFVAR_INT ("overline-margin", overline_margin,
30246 doc: /* Space between overline and text, in pixels.
30247 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30248 margin to the character height. */);
30249 overline_margin = 2;
30251 DEFVAR_INT ("underline-minimum-offset",
30252 underline_minimum_offset,
30253 doc: /* Minimum distance between baseline and underline.
30254 This can improve legibility of underlined text at small font sizes,
30255 particularly when using variable `x-use-underline-position-properties'
30256 with fonts that specify an UNDERLINE_POSITION relatively close to the
30257 baseline. The default value is 1. */);
30258 underline_minimum_offset = 1;
30260 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30261 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30262 This feature only works when on a window system that can change
30263 cursor shapes. */);
30264 display_hourglass_p = 1;
30266 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30267 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30268 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30270 #ifdef HAVE_WINDOW_SYSTEM
30271 hourglass_atimer = NULL;
30272 hourglass_shown_p = 0;
30273 #endif /* HAVE_WINDOW_SYSTEM */
30275 DEFSYM (Qglyphless_char, "glyphless-char");
30276 DEFSYM (Qhex_code, "hex-code");
30277 DEFSYM (Qempty_box, "empty-box");
30278 DEFSYM (Qthin_space, "thin-space");
30279 DEFSYM (Qzero_width, "zero-width");
30281 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30282 doc: /* Function run just before redisplay.
30283 It is called with one argument, which is the set of windows that are to
30284 be redisplayed. This set can be nil (meaning, only the selected window),
30285 or t (meaning all windows). */);
30286 Vpre_redisplay_function = intern ("ignore");
30288 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30289 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30291 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30292 doc: /* Char-table defining glyphless characters.
30293 Each element, if non-nil, should be one of the following:
30294 an ASCII acronym string: display this string in a box
30295 `hex-code': display the hexadecimal code of a character in a box
30296 `empty-box': display as an empty box
30297 `thin-space': display as 1-pixel width space
30298 `zero-width': don't display
30299 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30300 display method for graphical terminals and text terminals respectively.
30301 GRAPHICAL and TEXT should each have one of the values listed above.
30303 The char-table has one extra slot to control the display of a character for
30304 which no font is found. This slot only takes effect on graphical terminals.
30305 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30306 `thin-space'. The default is `empty-box'.
30308 If a character has a non-nil entry in an active display table, the
30309 display table takes effect; in this case, Emacs does not consult
30310 `glyphless-char-display' at all. */);
30311 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30312 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30313 Qempty_box);
30315 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30316 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30317 Vdebug_on_message = Qnil;
30319 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30320 doc: /* */);
30321 Vredisplay__all_windows_cause
30322 = Fmake_vector (make_number (100), make_number (0));
30324 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30325 doc: /* */);
30326 Vredisplay__mode_lines_cause
30327 = Fmake_vector (make_number (100), make_number (0));
30331 /* Initialize this module when Emacs starts. */
30333 void
30334 init_xdisp (void)
30336 CHARPOS (this_line_start_pos) = 0;
30338 if (!noninteractive)
30340 struct window *m = XWINDOW (minibuf_window);
30341 Lisp_Object frame = m->frame;
30342 struct frame *f = XFRAME (frame);
30343 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30344 struct window *r = XWINDOW (root);
30345 int i;
30347 echo_area_window = minibuf_window;
30349 r->top_line = FRAME_TOP_MARGIN (f);
30350 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30351 r->total_cols = FRAME_COLS (f);
30352 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30353 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30354 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30356 m->top_line = FRAME_LINES (f) - 1;
30357 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30358 m->total_cols = FRAME_COLS (f);
30359 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30360 m->total_lines = 1;
30361 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30363 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30364 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30365 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30367 /* The default ellipsis glyphs `...'. */
30368 for (i = 0; i < 3; ++i)
30369 default_invis_vector[i] = make_number ('.');
30373 /* Allocate the buffer for frame titles.
30374 Also used for `format-mode-line'. */
30375 int size = 100;
30376 mode_line_noprop_buf = xmalloc (size);
30377 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30378 mode_line_noprop_ptr = mode_line_noprop_buf;
30379 mode_line_target = MODE_LINE_DISPLAY;
30382 help_echo_showing_p = 0;
30385 #ifdef HAVE_WINDOW_SYSTEM
30387 /* Platform-independent portion of hourglass implementation. */
30389 /* Cancel a currently active hourglass timer, and start a new one. */
30390 void
30391 start_hourglass (void)
30393 struct timespec delay;
30395 cancel_hourglass ();
30397 if (INTEGERP (Vhourglass_delay)
30398 && XINT (Vhourglass_delay) > 0)
30399 delay = make_timespec (min (XINT (Vhourglass_delay),
30400 TYPE_MAXIMUM (time_t)),
30402 else if (FLOATP (Vhourglass_delay)
30403 && XFLOAT_DATA (Vhourglass_delay) > 0)
30404 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30405 else
30406 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30408 #ifdef HAVE_NTGUI
30410 extern void w32_note_current_window (void);
30411 w32_note_current_window ();
30413 #endif /* HAVE_NTGUI */
30415 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30416 show_hourglass, NULL);
30420 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30421 shown. */
30422 void
30423 cancel_hourglass (void)
30425 if (hourglass_atimer)
30427 cancel_atimer (hourglass_atimer);
30428 hourglass_atimer = NULL;
30431 if (hourglass_shown_p)
30432 hide_hourglass ();
30435 #endif /* HAVE_WINDOW_SYSTEM */