* syntax.c (Fskip_syntax_backward): Doc clarification.
[emacs.git] / src / xdisp.c
blobd941c7b1086ed7f43031462bdf21abc50ddd52a9
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);
2456 row_glyph:
2457 gr = 0, gy = 0;
2458 for (; r <= end_row && r->enabled_p; ++r)
2459 if (r->y + r->height > y)
2461 gr = r; gy = r->y;
2462 break;
2465 if (gr && gy <= y)
2466 height = gr->height;
2467 else
2469 /* Use nominal line height at end of window. */
2470 y -= gy;
2471 gy += (y / height) * height;
2473 break;
2475 default:
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2492 goto store_rect;
2495 gx += WINDOW_LEFT_EDGE_X (w);
2496 gy += WINDOW_TOP_EDGE_Y (w);
2498 store_rect:
2499 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2501 /* Visible feedback for debugging. */
2502 #if 0
2503 #if HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2508 #endif
2512 #endif /* HAVE_WINDOW_SYSTEM */
2514 static void
2515 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2517 eassert (w);
2518 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2519 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2520 w->window_end_vpos
2521 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2524 /***********************************************************************
2525 Lisp form evaluation
2526 ***********************************************************************/
2528 /* Error handler for safe_eval and safe_call. */
2530 static Lisp_Object
2531 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2533 add_to_log ("Error during redisplay: %S signaled %S",
2534 Flist (nargs, args), arg);
2535 return Qnil;
2538 /* Call function FUNC with the rest of NARGS - 1 arguments
2539 following. Return the result, or nil if something went
2540 wrong. Prevent redisplay during the evaluation. */
2542 Lisp_Object
2543 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2545 Lisp_Object val;
2547 if (inhibit_eval_during_redisplay)
2548 val = Qnil;
2549 else
2551 va_list ap;
2552 ptrdiff_t i;
2553 ptrdiff_t count = SPECPDL_INDEX ();
2554 struct gcpro gcpro1;
2555 Lisp_Object *args = alloca (nargs * word_size);
2557 args[0] = func;
2558 va_start (ap, func);
2559 for (i = 1; i < nargs; i++)
2560 args[i] = va_arg (ap, Lisp_Object);
2561 va_end (ap);
2563 GCPRO1 (args[0]);
2564 gcpro1.nvars = nargs;
2565 specbind (Qinhibit_redisplay, Qt);
2566 /* Use Qt to ensure debugger does not run,
2567 so there is no possibility of wanting to redisplay. */
2568 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2569 safe_eval_handler);
2570 UNGCPRO;
2571 val = unbind_to (count, val);
2574 return val;
2578 /* Call function FN with one argument ARG.
2579 Return the result, or nil if something went wrong. */
2581 Lisp_Object
2582 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2584 return safe_call (2, fn, arg);
2587 static Lisp_Object Qeval;
2589 Lisp_Object
2590 safe_eval (Lisp_Object sexpr)
2592 return safe_call1 (Qeval, sexpr);
2595 /* Call function FN with two arguments ARG1 and ARG2.
2596 Return the result, or nil if something went wrong. */
2598 Lisp_Object
2599 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2601 return safe_call (3, fn, arg1, arg2);
2606 /***********************************************************************
2607 Debugging
2608 ***********************************************************************/
2610 #if 0
2612 /* Define CHECK_IT to perform sanity checks on iterators.
2613 This is for debugging. It is too slow to do unconditionally. */
2615 static void
2616 check_it (struct it *it)
2618 if (it->method == GET_FROM_STRING)
2620 eassert (STRINGP (it->string));
2621 eassert (IT_STRING_CHARPOS (*it) >= 0);
2623 else
2625 eassert (IT_STRING_CHARPOS (*it) < 0);
2626 if (it->method == GET_FROM_BUFFER)
2628 /* Check that character and byte positions agree. */
2629 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2633 if (it->dpvec)
2634 eassert (it->current.dpvec_index >= 0);
2635 else
2636 eassert (it->current.dpvec_index < 0);
2639 #define CHECK_IT(IT) check_it ((IT))
2641 #else /* not 0 */
2643 #define CHECK_IT(IT) (void) 0
2645 #endif /* not 0 */
2648 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2650 /* Check that the window end of window W is what we expect it
2651 to be---the last row in the current matrix displaying text. */
2653 static void
2654 check_window_end (struct window *w)
2656 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2658 struct glyph_row *row;
2659 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2660 !row->enabled_p
2661 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2662 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2666 #define CHECK_WINDOW_END(W) check_window_end ((W))
2668 #else
2670 #define CHECK_WINDOW_END(W) (void) 0
2672 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2674 /***********************************************************************
2675 Iterator initialization
2676 ***********************************************************************/
2678 /* Initialize IT for displaying current_buffer in window W, starting
2679 at character position CHARPOS. CHARPOS < 0 means that no buffer
2680 position is specified which is useful when the iterator is assigned
2681 a position later. BYTEPOS is the byte position corresponding to
2682 CHARPOS.
2684 If ROW is not null, calls to produce_glyphs with IT as parameter
2685 will produce glyphs in that row.
2687 BASE_FACE_ID is the id of a base face to use. It must be one of
2688 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2689 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2690 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2692 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2694 will be initialized to use the corresponding mode line glyph row of
2695 the desired matrix of W. */
2697 void
2698 init_iterator (struct it *it, struct window *w,
2699 ptrdiff_t charpos, ptrdiff_t bytepos,
2700 struct glyph_row *row, enum face_id base_face_id)
2702 enum face_id remapped_base_face_id = base_face_id;
2704 /* Some precondition checks. */
2705 eassert (w != NULL && it != NULL);
2706 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2707 && charpos <= ZV));
2709 /* If face attributes have been changed since the last redisplay,
2710 free realized faces now because they depend on face definitions
2711 that might have changed. Don't free faces while there might be
2712 desired matrices pending which reference these faces. */
2713 if (face_change_count && !inhibit_free_realized_faces)
2715 face_change_count = 0;
2716 free_all_realized_faces (Qnil);
2719 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2720 if (! NILP (Vface_remapping_alist))
2721 remapped_base_face_id
2722 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2724 /* Use one of the mode line rows of W's desired matrix if
2725 appropriate. */
2726 if (row == NULL)
2728 if (base_face_id == MODE_LINE_FACE_ID
2729 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2730 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2731 else if (base_face_id == HEADER_LINE_FACE_ID)
2732 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2735 /* Clear IT. */
2736 memset (it, 0, sizeof *it);
2737 it->current.overlay_string_index = -1;
2738 it->current.dpvec_index = -1;
2739 it->base_face_id = remapped_base_face_id;
2740 it->string = Qnil;
2741 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2742 it->paragraph_embedding = L2R;
2743 it->bidi_it.string.lstring = Qnil;
2744 it->bidi_it.string.s = NULL;
2745 it->bidi_it.string.bufpos = 0;
2746 it->bidi_it.w = w;
2748 /* The window in which we iterate over current_buffer: */
2749 XSETWINDOW (it->window, w);
2750 it->w = w;
2751 it->f = XFRAME (w->frame);
2753 it->cmp_it.id = -1;
2755 /* Extra space between lines (on window systems only). */
2756 if (base_face_id == DEFAULT_FACE_ID
2757 && FRAME_WINDOW_P (it->f))
2759 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2760 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2761 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2762 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2763 * FRAME_LINE_HEIGHT (it->f));
2764 else if (it->f->extra_line_spacing > 0)
2765 it->extra_line_spacing = it->f->extra_line_spacing;
2766 it->max_extra_line_spacing = 0;
2769 /* If realized faces have been removed, e.g. because of face
2770 attribute changes of named faces, recompute them. When running
2771 in batch mode, the face cache of the initial frame is null. If
2772 we happen to get called, make a dummy face cache. */
2773 if (FRAME_FACE_CACHE (it->f) == NULL)
2774 init_frame_faces (it->f);
2775 if (FRAME_FACE_CACHE (it->f)->used == 0)
2776 recompute_basic_faces (it->f);
2778 /* Current value of the `slice', `space-width', and 'height' properties. */
2779 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2780 it->space_width = Qnil;
2781 it->font_height = Qnil;
2782 it->override_ascent = -1;
2784 /* Are control characters displayed as `^C'? */
2785 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2787 /* -1 means everything between a CR and the following line end
2788 is invisible. >0 means lines indented more than this value are
2789 invisible. */
2790 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2791 ? (clip_to_bounds
2792 (-1, XINT (BVAR (current_buffer, selective_display)),
2793 PTRDIFF_MAX))
2794 : (!NILP (BVAR (current_buffer, selective_display))
2795 ? -1 : 0));
2796 it->selective_display_ellipsis_p
2797 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2799 /* Display table to use. */
2800 it->dp = window_display_table (w);
2802 /* Are multibyte characters enabled in current_buffer? */
2803 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2805 /* Get the position at which the redisplay_end_trigger hook should
2806 be run, if it is to be run at all. */
2807 if (MARKERP (w->redisplay_end_trigger)
2808 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2809 it->redisplay_end_trigger_charpos
2810 = marker_position (w->redisplay_end_trigger);
2811 else if (INTEGERP (w->redisplay_end_trigger))
2812 it->redisplay_end_trigger_charpos
2813 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2814 PTRDIFF_MAX);
2816 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2818 /* Are lines in the display truncated? */
2819 if (base_face_id != DEFAULT_FACE_ID
2820 || it->w->hscroll
2821 || (! WINDOW_FULL_WIDTH_P (it->w)
2822 && ((!NILP (Vtruncate_partial_width_windows)
2823 && !INTEGERP (Vtruncate_partial_width_windows))
2824 || (INTEGERP (Vtruncate_partial_width_windows)
2825 /* PXW: Shall we do something about this? */
2826 && (WINDOW_TOTAL_COLS (it->w)
2827 < XINT (Vtruncate_partial_width_windows))))))
2828 it->line_wrap = TRUNCATE;
2829 else if (NILP (BVAR (current_buffer, truncate_lines)))
2830 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2831 ? WINDOW_WRAP : WORD_WRAP;
2832 else
2833 it->line_wrap = TRUNCATE;
2835 /* Get dimensions of truncation and continuation glyphs. These are
2836 displayed as fringe bitmaps under X, but we need them for such
2837 frames when the fringes are turned off. But leave the dimensions
2838 zero for tooltip frames, as these glyphs look ugly there and also
2839 sabotage calculations of tooltip dimensions in x-show-tip. */
2840 #ifdef HAVE_WINDOW_SYSTEM
2841 if (!(FRAME_WINDOW_P (it->f)
2842 && FRAMEP (tip_frame)
2843 && it->f == XFRAME (tip_frame)))
2844 #endif
2846 if (it->line_wrap == TRUNCATE)
2848 /* We will need the truncation glyph. */
2849 eassert (it->glyph_row == NULL);
2850 produce_special_glyphs (it, IT_TRUNCATION);
2851 it->truncation_pixel_width = it->pixel_width;
2853 else
2855 /* We will need the continuation glyph. */
2856 eassert (it->glyph_row == NULL);
2857 produce_special_glyphs (it, IT_CONTINUATION);
2858 it->continuation_pixel_width = it->pixel_width;
2862 /* Reset these values to zero because the produce_special_glyphs
2863 above has changed them. */
2864 it->pixel_width = it->ascent = it->descent = 0;
2865 it->phys_ascent = it->phys_descent = 0;
2867 /* Set this after getting the dimensions of truncation and
2868 continuation glyphs, so that we don't produce glyphs when calling
2869 produce_special_glyphs, above. */
2870 it->glyph_row = row;
2871 it->area = TEXT_AREA;
2873 /* Forget any previous info about this row being reversed. */
2874 if (it->glyph_row)
2875 it->glyph_row->reversed_p = 0;
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2887 else
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no fringes. Since we
2897 don't know at this point whether there will be any R2L lines in
2898 the window, we reserve space for truncation/continuation glyphs
2899 even if only one of the fringes is absent. */
2900 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2901 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2903 if (it->line_wrap == TRUNCATE)
2904 it->last_visible_x -= it->truncation_pixel_width;
2905 else
2906 it->last_visible_x -= it->continuation_pixel_width;
2909 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2910 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2913 /* Leave room for a border glyph. */
2914 if (!FRAME_WINDOW_P (it->f)
2915 && !WINDOW_RIGHTMOST_P (it->w))
2916 it->last_visible_x -= 1;
2918 it->last_visible_y = window_text_bottom_y (w);
2920 /* For mode lines and alike, arrange for the first glyph having a
2921 left box line if the face specifies a box. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2924 struct face *face;
2926 it->face_id = remapped_base_face_id;
2928 /* If we have a boxed mode line, make the first character appear
2929 with a left box line. */
2930 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2931 if (face->box != FACE_NO_BOX)
2932 it->start_of_box_run_p = true;
2935 /* If a buffer position was specified, set the iterator there,
2936 getting overlays and face properties from that position. */
2937 if (charpos >= BUF_BEG (current_buffer))
2939 it->end_charpos = ZV;
2940 eassert (charpos == BYTE_TO_CHAR (bytepos));
2941 IT_CHARPOS (*it) = charpos;
2942 IT_BYTEPOS (*it) = bytepos;
2944 /* We will rely on `reseat' to set this up properly, via
2945 handle_face_prop. */
2946 it->face_id = it->base_face_id;
2948 it->start = it->current;
2949 /* Do we need to reorder bidirectional text? Not if this is a
2950 unibyte buffer: by definition, none of the single-byte
2951 characters are strong R2L, so no reordering is needed. And
2952 bidi.c doesn't support unibyte buffers anyway. Also, don't
2953 reorder while we are loading loadup.el, since the tables of
2954 character properties needed for reordering are not yet
2955 available. */
2956 it->bidi_p =
2957 NILP (Vpurify_flag)
2958 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2959 && it->multibyte_p;
2961 /* If we are to reorder bidirectional text, init the bidi
2962 iterator. */
2963 if (it->bidi_p)
2965 /* Note the paragraph direction that this buffer wants to
2966 use. */
2967 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2968 Qleft_to_right))
2969 it->paragraph_embedding = L2R;
2970 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2971 Qright_to_left))
2972 it->paragraph_embedding = R2L;
2973 else
2974 it->paragraph_embedding = NEUTRAL_DIR;
2975 bidi_unshelve_cache (NULL, 0);
2976 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2977 &it->bidi_it);
2980 /* Compute faces etc. */
2981 reseat (it, it->current.pos, 1);
2984 CHECK_IT (it);
2988 /* Initialize IT for the display of window W with window start POS. */
2990 void
2991 start_display (struct it *it, struct window *w, struct text_pos pos)
2993 struct glyph_row *row;
2994 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2996 row = w->desired_matrix->rows + first_vpos;
2997 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2998 it->first_vpos = first_vpos;
3000 /* Don't reseat to previous visible line start if current start
3001 position is in a string or image. */
3002 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3004 int start_at_line_beg_p;
3005 int first_y = it->current_y;
3007 /* If window start is not at a line start, skip forward to POS to
3008 get the correct continuation lines width. */
3009 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3010 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3011 if (!start_at_line_beg_p)
3013 int new_x;
3015 reseat_at_previous_visible_line_start (it);
3016 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3018 new_x = it->current_x + it->pixel_width;
3020 /* If lines are continued, this line may end in the middle
3021 of a multi-glyph character (e.g. a control character
3022 displayed as \003, or in the middle of an overlay
3023 string). In this case move_it_to above will not have
3024 taken us to the start of the continuation line but to the
3025 end of the continued line. */
3026 if (it->current_x > 0
3027 && it->line_wrap != TRUNCATE /* Lines are continued. */
3028 && (/* And glyph doesn't fit on the line. */
3029 new_x > it->last_visible_x
3030 /* Or it fits exactly and we're on a window
3031 system frame. */
3032 || (new_x == it->last_visible_x
3033 && FRAME_WINDOW_P (it->f)
3034 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3035 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3036 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3038 if ((it->current.dpvec_index >= 0
3039 || it->current.overlay_string_index >= 0)
3040 /* If we are on a newline from a display vector or
3041 overlay string, then we are already at the end of
3042 a screen line; no need to go to the next line in
3043 that case, as this line is not really continued.
3044 (If we do go to the next line, C-e will not DTRT.) */
3045 && it->c != '\n')
3047 set_iterator_to_next (it, 1);
3048 move_it_in_display_line_to (it, -1, -1, 0);
3051 it->continuation_lines_width += it->current_x;
3053 /* If the character at POS is displayed via a display
3054 vector, move_it_to above stops at the final glyph of
3055 IT->dpvec. To make the caller redisplay that character
3056 again (a.k.a. start at POS), we need to reset the
3057 dpvec_index to the beginning of IT->dpvec. */
3058 else if (it->current.dpvec_index >= 0)
3059 it->current.dpvec_index = 0;
3061 /* We're starting a new display line, not affected by the
3062 height of the continued line, so clear the appropriate
3063 fields in the iterator structure. */
3064 it->max_ascent = it->max_descent = 0;
3065 it->max_phys_ascent = it->max_phys_descent = 0;
3067 it->current_y = first_y;
3068 it->vpos = 0;
3069 it->current_x = it->hpos = 0;
3075 /* Return 1 if POS is a position in ellipses displayed for invisible
3076 text. W is the window we display, for text property lookup. */
3078 static int
3079 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3081 Lisp_Object prop, window;
3082 int ellipses_p = 0;
3083 ptrdiff_t charpos = CHARPOS (pos->pos);
3085 /* If POS specifies a position in a display vector, this might
3086 be for an ellipsis displayed for invisible text. We won't
3087 get the iterator set up for delivering that ellipsis unless
3088 we make sure that it gets aware of the invisible text. */
3089 if (pos->dpvec_index >= 0
3090 && pos->overlay_string_index < 0
3091 && CHARPOS (pos->string_pos) < 0
3092 && charpos > BEGV
3093 && (XSETWINDOW (window, w),
3094 prop = Fget_char_property (make_number (charpos),
3095 Qinvisible, window),
3096 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3098 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3099 window);
3100 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3103 return ellipses_p;
3107 /* Initialize IT for stepping through current_buffer in window W,
3108 starting at position POS that includes overlay string and display
3109 vector/ control character translation position information. Value
3110 is zero if there are overlay strings with newlines at POS. */
3112 static int
3113 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3115 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3116 int i, overlay_strings_with_newlines = 0;
3118 /* If POS specifies a position in a display vector, this might
3119 be for an ellipsis displayed for invisible text. We won't
3120 get the iterator set up for delivering that ellipsis unless
3121 we make sure that it gets aware of the invisible text. */
3122 if (in_ellipses_for_invisible_text_p (pos, w))
3124 --charpos;
3125 bytepos = 0;
3128 /* Keep in mind: the call to reseat in init_iterator skips invisible
3129 text, so we might end up at a position different from POS. This
3130 is only a problem when POS is a row start after a newline and an
3131 overlay starts there with an after-string, and the overlay has an
3132 invisible property. Since we don't skip invisible text in
3133 display_line and elsewhere immediately after consuming the
3134 newline before the row start, such a POS will not be in a string,
3135 but the call to init_iterator below will move us to the
3136 after-string. */
3137 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3139 /* This only scans the current chunk -- it should scan all chunks.
3140 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3141 to 16 in 22.1 to make this a lesser problem. */
3142 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3144 const char *s = SSDATA (it->overlay_strings[i]);
3145 const char *e = s + SBYTES (it->overlay_strings[i]);
3147 while (s < e && *s != '\n')
3148 ++s;
3150 if (s < e)
3152 overlay_strings_with_newlines = 1;
3153 break;
3157 /* If position is within an overlay string, set up IT to the right
3158 overlay string. */
3159 if (pos->overlay_string_index >= 0)
3161 int relative_index;
3163 /* If the first overlay string happens to have a `display'
3164 property for an image, the iterator will be set up for that
3165 image, and we have to undo that setup first before we can
3166 correct the overlay string index. */
3167 if (it->method == GET_FROM_IMAGE)
3168 pop_it (it);
3170 /* We already have the first chunk of overlay strings in
3171 IT->overlay_strings. Load more until the one for
3172 pos->overlay_string_index is in IT->overlay_strings. */
3173 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3175 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3176 it->current.overlay_string_index = 0;
3177 while (n--)
3179 load_overlay_strings (it, 0);
3180 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3184 it->current.overlay_string_index = pos->overlay_string_index;
3185 relative_index = (it->current.overlay_string_index
3186 % OVERLAY_STRING_CHUNK_SIZE);
3187 it->string = it->overlay_strings[relative_index];
3188 eassert (STRINGP (it->string));
3189 it->current.string_pos = pos->string_pos;
3190 it->method = GET_FROM_STRING;
3191 it->end_charpos = SCHARS (it->string);
3192 /* Set up the bidi iterator for this overlay string. */
3193 if (it->bidi_p)
3195 it->bidi_it.string.lstring = it->string;
3196 it->bidi_it.string.s = NULL;
3197 it->bidi_it.string.schars = SCHARS (it->string);
3198 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3199 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3200 it->bidi_it.string.unibyte = !it->multibyte_p;
3201 it->bidi_it.w = it->w;
3202 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3203 FRAME_WINDOW_P (it->f), &it->bidi_it);
3205 /* Synchronize the state of the bidi iterator with
3206 pos->string_pos. For any string position other than
3207 zero, this will be done automagically when we resume
3208 iteration over the string and get_visually_first_element
3209 is called. But if string_pos is zero, and the string is
3210 to be reordered for display, we need to resync manually,
3211 since it could be that the iteration state recorded in
3212 pos ended at string_pos of 0 moving backwards in string. */
3213 if (CHARPOS (pos->string_pos) == 0)
3215 get_visually_first_element (it);
3216 if (IT_STRING_CHARPOS (*it) != 0)
3217 do {
3218 /* Paranoia. */
3219 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3220 bidi_move_to_visually_next (&it->bidi_it);
3221 } while (it->bidi_it.charpos != 0);
3223 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3224 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3228 if (CHARPOS (pos->string_pos) >= 0)
3230 /* Recorded position is not in an overlay string, but in another
3231 string. This can only be a string from a `display' property.
3232 IT should already be filled with that string. */
3233 it->current.string_pos = pos->string_pos;
3234 eassert (STRINGP (it->string));
3235 if (it->bidi_p)
3236 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3237 FRAME_WINDOW_P (it->f), &it->bidi_it);
3240 /* Restore position in display vector translations, control
3241 character translations or ellipses. */
3242 if (pos->dpvec_index >= 0)
3244 if (it->dpvec == NULL)
3245 get_next_display_element (it);
3246 eassert (it->dpvec && it->current.dpvec_index == 0);
3247 it->current.dpvec_index = pos->dpvec_index;
3250 CHECK_IT (it);
3251 return !overlay_strings_with_newlines;
3255 /* Initialize IT for stepping through current_buffer in window W
3256 starting at ROW->start. */
3258 static void
3259 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3261 init_from_display_pos (it, w, &row->start);
3262 it->start = row->start;
3263 it->continuation_lines_width = row->continuation_lines_width;
3264 CHECK_IT (it);
3268 /* Initialize IT for stepping through current_buffer in window W
3269 starting in the line following ROW, i.e. starting at ROW->end.
3270 Value is zero if there are overlay strings with newlines at ROW's
3271 end position. */
3273 static int
3274 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3276 int success = 0;
3278 if (init_from_display_pos (it, w, &row->end))
3280 if (row->continued_p)
3281 it->continuation_lines_width
3282 = row->continuation_lines_width + row->pixel_width;
3283 CHECK_IT (it);
3284 success = 1;
3287 return success;
3293 /***********************************************************************
3294 Text properties
3295 ***********************************************************************/
3297 /* Called when IT reaches IT->stop_charpos. Handle text property and
3298 overlay changes. Set IT->stop_charpos to the next position where
3299 to stop. */
3301 static void
3302 handle_stop (struct it *it)
3304 enum prop_handled handled;
3305 int handle_overlay_change_p;
3306 struct props *p;
3308 it->dpvec = NULL;
3309 it->current.dpvec_index = -1;
3310 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3311 it->ignore_overlay_strings_at_pos_p = 0;
3312 it->ellipsis_p = 0;
3314 /* Use face of preceding text for ellipsis (if invisible) */
3315 if (it->selective_display_ellipsis_p)
3316 it->saved_face_id = it->face_id;
3320 handled = HANDLED_NORMALLY;
3322 /* Call text property handlers. */
3323 for (p = it_props; p->handler; ++p)
3325 handled = p->handler (it);
3327 if (handled == HANDLED_RECOMPUTE_PROPS)
3328 break;
3329 else if (handled == HANDLED_RETURN)
3331 /* We still want to show before and after strings from
3332 overlays even if the actual buffer text is replaced. */
3333 if (!handle_overlay_change_p
3334 || it->sp > 1
3335 /* Don't call get_overlay_strings_1 if we already
3336 have overlay strings loaded, because doing so
3337 will load them again and push the iterator state
3338 onto the stack one more time, which is not
3339 expected by the rest of the code that processes
3340 overlay strings. */
3341 || (it->current.overlay_string_index < 0
3342 ? !get_overlay_strings_1 (it, 0, 0)
3343 : 0))
3345 if (it->ellipsis_p)
3346 setup_for_ellipsis (it, 0);
3347 /* When handling a display spec, we might load an
3348 empty string. In that case, discard it here. We
3349 used to discard it in handle_single_display_spec,
3350 but that causes get_overlay_strings_1, above, to
3351 ignore overlay strings that we must check. */
3352 if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 return;
3356 else if (STRINGP (it->string) && !SCHARS (it->string))
3357 pop_it (it);
3358 else
3360 it->ignore_overlay_strings_at_pos_p = true;
3361 it->string_from_display_prop_p = 0;
3362 it->from_disp_prop_p = 0;
3363 handle_overlay_change_p = 0;
3365 handled = HANDLED_RECOMPUTE_PROPS;
3366 break;
3368 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3369 handle_overlay_change_p = 0;
3372 if (handled != HANDLED_RECOMPUTE_PROPS)
3374 /* Don't check for overlay strings below when set to deliver
3375 characters from a display vector. */
3376 if (it->method == GET_FROM_DISPLAY_VECTOR)
3377 handle_overlay_change_p = 0;
3379 /* Handle overlay changes.
3380 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3381 if it finds overlays. */
3382 if (handle_overlay_change_p)
3383 handled = handle_overlay_change (it);
3386 if (it->ellipsis_p)
3388 setup_for_ellipsis (it, 0);
3389 break;
3392 while (handled == HANDLED_RECOMPUTE_PROPS);
3394 /* Determine where to stop next. */
3395 if (handled == HANDLED_NORMALLY)
3396 compute_stop_pos (it);
3400 /* Compute IT->stop_charpos from text property and overlay change
3401 information for IT's current position. */
3403 static void
3404 compute_stop_pos (struct it *it)
3406 register INTERVAL iv, next_iv;
3407 Lisp_Object object, limit, position;
3408 ptrdiff_t charpos, bytepos;
3410 if (STRINGP (it->string))
3412 /* Strings are usually short, so don't limit the search for
3413 properties. */
3414 it->stop_charpos = it->end_charpos;
3415 object = it->string;
3416 limit = Qnil;
3417 charpos = IT_STRING_CHARPOS (*it);
3418 bytepos = IT_STRING_BYTEPOS (*it);
3420 else
3422 ptrdiff_t pos;
3424 /* If end_charpos is out of range for some reason, such as a
3425 misbehaving display function, rationalize it (Bug#5984). */
3426 if (it->end_charpos > ZV)
3427 it->end_charpos = ZV;
3428 it->stop_charpos = it->end_charpos;
3430 /* If next overlay change is in front of the current stop pos
3431 (which is IT->end_charpos), stop there. Note: value of
3432 next_overlay_change is point-max if no overlay change
3433 follows. */
3434 charpos = IT_CHARPOS (*it);
3435 bytepos = IT_BYTEPOS (*it);
3436 pos = next_overlay_change (charpos);
3437 if (pos < it->stop_charpos)
3438 it->stop_charpos = pos;
3440 /* Set up variables for computing the stop position from text
3441 property changes. */
3442 XSETBUFFER (object, current_buffer);
3443 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3446 /* Get the interval containing IT's position. Value is a null
3447 interval if there isn't such an interval. */
3448 position = make_number (charpos);
3449 iv = validate_interval_range (object, &position, &position, 0);
3450 if (iv)
3452 Lisp_Object values_here[LAST_PROP_IDX];
3453 struct props *p;
3455 /* Get properties here. */
3456 for (p = it_props; p->handler; ++p)
3457 values_here[p->idx] = textget (iv->plist, *p->name);
3459 /* Look for an interval following iv that has different
3460 properties. */
3461 for (next_iv = next_interval (iv);
3462 (next_iv
3463 && (NILP (limit)
3464 || XFASTINT (limit) > next_iv->position));
3465 next_iv = next_interval (next_iv))
3467 for (p = it_props; p->handler; ++p)
3469 Lisp_Object new_value;
3471 new_value = textget (next_iv->plist, *p->name);
3472 if (!EQ (values_here[p->idx], new_value))
3473 break;
3476 if (p->handler)
3477 break;
3480 if (next_iv)
3482 if (INTEGERP (limit)
3483 && next_iv->position >= XFASTINT (limit))
3484 /* No text property change up to limit. */
3485 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3486 else
3487 /* Text properties change in next_iv. */
3488 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3492 if (it->cmp_it.id < 0)
3494 ptrdiff_t stoppos = it->end_charpos;
3496 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3497 stoppos = -1;
3498 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3499 stoppos, it->string);
3502 eassert (STRINGP (it->string)
3503 || (it->stop_charpos >= BEGV
3504 && it->stop_charpos >= IT_CHARPOS (*it)));
3508 /* Return the position of the next overlay change after POS in
3509 current_buffer. Value is point-max if no overlay change
3510 follows. This is like `next-overlay-change' but doesn't use
3511 xmalloc. */
3513 static ptrdiff_t
3514 next_overlay_change (ptrdiff_t pos)
3516 ptrdiff_t i, noverlays;
3517 ptrdiff_t endpos;
3518 Lisp_Object *overlays;
3520 /* Get all overlays at the given position. */
3521 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3523 /* If any of these overlays ends before endpos,
3524 use its ending point instead. */
3525 for (i = 0; i < noverlays; ++i)
3527 Lisp_Object oend;
3528 ptrdiff_t oendpos;
3530 oend = OVERLAY_END (overlays[i]);
3531 oendpos = OVERLAY_POSITION (oend);
3532 endpos = min (endpos, oendpos);
3535 return endpos;
3538 /* How many characters forward to search for a display property or
3539 display string. Searching too far forward makes the bidi display
3540 sluggish, especially in small windows. */
3541 #define MAX_DISP_SCAN 250
3543 /* Return the character position of a display string at or after
3544 position specified by POSITION. If no display string exists at or
3545 after POSITION, return ZV. A display string is either an overlay
3546 with `display' property whose value is a string, or a `display'
3547 text property whose value is a string. STRING is data about the
3548 string to iterate; if STRING->lstring is nil, we are iterating a
3549 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3550 on a GUI frame. DISP_PROP is set to zero if we searched
3551 MAX_DISP_SCAN characters forward without finding any display
3552 strings, non-zero otherwise. It is set to 2 if the display string
3553 uses any kind of `(space ...)' spec that will produce a stretch of
3554 white space in the text area. */
3555 ptrdiff_t
3556 compute_display_string_pos (struct text_pos *position,
3557 struct bidi_string_data *string,
3558 struct window *w,
3559 int frame_window_p, int *disp_prop)
3561 /* OBJECT = nil means current buffer. */
3562 Lisp_Object object, object1;
3563 Lisp_Object pos, spec, limpos;
3564 int string_p = (string && (STRINGP (string->lstring) || string->s));
3565 ptrdiff_t eob = string_p ? string->schars : ZV;
3566 ptrdiff_t begb = string_p ? 0 : BEGV;
3567 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3568 ptrdiff_t lim =
3569 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3570 struct text_pos tpos;
3571 int rv = 0;
3573 if (string && STRINGP (string->lstring))
3574 object1 = object = string->lstring;
3575 else if (w && !string_p)
3577 XSETWINDOW (object, w);
3578 object1 = Qnil;
3580 else
3581 object1 = object = Qnil;
3583 *disp_prop = 1;
3585 if (charpos >= eob
3586 /* We don't support display properties whose values are strings
3587 that have display string properties. */
3588 || string->from_disp_str
3589 /* C strings cannot have display properties. */
3590 || (string->s && !STRINGP (object)))
3592 *disp_prop = 0;
3593 return eob;
3596 /* If the character at CHARPOS is where the display string begins,
3597 return CHARPOS. */
3598 pos = make_number (charpos);
3599 if (STRINGP (object))
3600 bufpos = string->bufpos;
3601 else
3602 bufpos = charpos;
3603 tpos = *position;
3604 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3605 && (charpos <= begb
3606 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3607 object),
3608 spec))
3609 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3610 frame_window_p)))
3612 if (rv == 2)
3613 *disp_prop = 2;
3614 return charpos;
3617 /* Look forward for the first character with a `display' property
3618 that will replace the underlying text when displayed. */
3619 limpos = make_number (lim);
3620 do {
3621 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3622 CHARPOS (tpos) = XFASTINT (pos);
3623 if (CHARPOS (tpos) >= lim)
3625 *disp_prop = 0;
3626 break;
3628 if (STRINGP (object))
3629 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3630 else
3631 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3632 spec = Fget_char_property (pos, Qdisplay, object);
3633 if (!STRINGP (object))
3634 bufpos = CHARPOS (tpos);
3635 } while (NILP (spec)
3636 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3637 bufpos, frame_window_p)));
3638 if (rv == 2)
3639 *disp_prop = 2;
3641 return CHARPOS (tpos);
3644 /* Return the character position of the end of the display string that
3645 started at CHARPOS. If there's no display string at CHARPOS,
3646 return -1. A display string is either an overlay with `display'
3647 property whose value is a string or a `display' text property whose
3648 value is a string. */
3649 ptrdiff_t
3650 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3652 /* OBJECT = nil means current buffer. */
3653 Lisp_Object object =
3654 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3655 Lisp_Object pos = make_number (charpos);
3656 ptrdiff_t eob =
3657 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3659 if (charpos >= eob || (string->s && !STRINGP (object)))
3660 return eob;
3662 /* It could happen that the display property or overlay was removed
3663 since we found it in compute_display_string_pos above. One way
3664 this can happen is if JIT font-lock was called (through
3665 handle_fontified_prop), and jit-lock-functions remove text
3666 properties or overlays from the portion of buffer that includes
3667 CHARPOS. Muse mode is known to do that, for example. In this
3668 case, we return -1 to the caller, to signal that no display
3669 string is actually present at CHARPOS. See bidi_fetch_char for
3670 how this is handled.
3672 An alternative would be to never look for display properties past
3673 it->stop_charpos. But neither compute_display_string_pos nor
3674 bidi_fetch_char that calls it know or care where the next
3675 stop_charpos is. */
3676 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3677 return -1;
3679 /* Look forward for the first character where the `display' property
3680 changes. */
3681 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3683 return XFASTINT (pos);
3688 /***********************************************************************
3689 Fontification
3690 ***********************************************************************/
3692 /* Handle changes in the `fontified' property of the current buffer by
3693 calling hook functions from Qfontification_functions to fontify
3694 regions of text. */
3696 static enum prop_handled
3697 handle_fontified_prop (struct it *it)
3699 Lisp_Object prop, pos;
3700 enum prop_handled handled = HANDLED_NORMALLY;
3702 if (!NILP (Vmemory_full))
3703 return handled;
3705 /* Get the value of the `fontified' property at IT's current buffer
3706 position. (The `fontified' property doesn't have a special
3707 meaning in strings.) If the value is nil, call functions from
3708 Qfontification_functions. */
3709 if (!STRINGP (it->string)
3710 && it->s == NULL
3711 && !NILP (Vfontification_functions)
3712 && !NILP (Vrun_hooks)
3713 && (pos = make_number (IT_CHARPOS (*it)),
3714 prop = Fget_char_property (pos, Qfontified, Qnil),
3715 /* Ignore the special cased nil value always present at EOB since
3716 no amount of fontifying will be able to change it. */
3717 NILP (prop) && IT_CHARPOS (*it) < Z))
3719 ptrdiff_t count = SPECPDL_INDEX ();
3720 Lisp_Object val;
3721 struct buffer *obuf = current_buffer;
3722 ptrdiff_t begv = BEGV, zv = ZV;
3723 bool old_clip_changed = current_buffer->clip_changed;
3725 val = Vfontification_functions;
3726 specbind (Qfontification_functions, Qnil);
3728 eassert (it->end_charpos == ZV);
3730 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3731 safe_call1 (val, pos);
3732 else
3734 Lisp_Object fns, fn;
3735 struct gcpro gcpro1, gcpro2;
3737 fns = Qnil;
3738 GCPRO2 (val, fns);
3740 for (; CONSP (val); val = XCDR (val))
3742 fn = XCAR (val);
3744 if (EQ (fn, Qt))
3746 /* A value of t indicates this hook has a local
3747 binding; it means to run the global binding too.
3748 In a global value, t should not occur. If it
3749 does, we must ignore it to avoid an endless
3750 loop. */
3751 for (fns = Fdefault_value (Qfontification_functions);
3752 CONSP (fns);
3753 fns = XCDR (fns))
3755 fn = XCAR (fns);
3756 if (!EQ (fn, Qt))
3757 safe_call1 (fn, pos);
3760 else
3761 safe_call1 (fn, pos);
3764 UNGCPRO;
3767 unbind_to (count, Qnil);
3769 /* Fontification functions routinely call `save-restriction'.
3770 Normally, this tags clip_changed, which can confuse redisplay
3771 (see discussion in Bug#6671). Since we don't perform any
3772 special handling of fontification changes in the case where
3773 `save-restriction' isn't called, there's no point doing so in
3774 this case either. So, if the buffer's restrictions are
3775 actually left unchanged, reset clip_changed. */
3776 if (obuf == current_buffer)
3778 if (begv == BEGV && zv == ZV)
3779 current_buffer->clip_changed = old_clip_changed;
3781 /* There isn't much we can reasonably do to protect against
3782 misbehaving fontification, but here's a fig leaf. */
3783 else if (BUFFER_LIVE_P (obuf))
3784 set_buffer_internal_1 (obuf);
3786 /* The fontification code may have added/removed text.
3787 It could do even a lot worse, but let's at least protect against
3788 the most obvious case where only the text past `pos' gets changed',
3789 as is/was done in grep.el where some escapes sequences are turned
3790 into face properties (bug#7876). */
3791 it->end_charpos = ZV;
3793 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3794 something. This avoids an endless loop if they failed to
3795 fontify the text for which reason ever. */
3796 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3797 handled = HANDLED_RECOMPUTE_PROPS;
3800 return handled;
3805 /***********************************************************************
3806 Faces
3807 ***********************************************************************/
3809 /* Set up iterator IT from face properties at its current position.
3810 Called from handle_stop. */
3812 static enum prop_handled
3813 handle_face_prop (struct it *it)
3815 int new_face_id;
3816 ptrdiff_t next_stop;
3818 if (!STRINGP (it->string))
3820 new_face_id
3821 = face_at_buffer_position (it->w,
3822 IT_CHARPOS (*it),
3823 &next_stop,
3824 (IT_CHARPOS (*it)
3825 + TEXT_PROP_DISTANCE_LIMIT),
3826 0, it->base_face_id);
3828 /* Is this a start of a run of characters with box face?
3829 Caveat: this can be called for a freshly initialized
3830 iterator; face_id is -1 in this case. We know that the new
3831 face will not change until limit, i.e. if the new face has a
3832 box, all characters up to limit will have one. But, as
3833 usual, we don't know whether limit is really the end. */
3834 if (new_face_id != it->face_id)
3836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3837 /* If it->face_id is -1, old_face below will be NULL, see
3838 the definition of FACE_FROM_ID. This will happen if this
3839 is the initial call that gets the face. */
3840 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3842 /* If the value of face_id of the iterator is -1, we have to
3843 look in front of IT's position and see whether there is a
3844 face there that's different from new_face_id. */
3845 if (!old_face && IT_CHARPOS (*it) > BEG)
3847 int prev_face_id = face_before_it_pos (it);
3849 old_face = FACE_FROM_ID (it->f, prev_face_id);
3852 /* If the new face has a box, but the old face does not,
3853 this is the start of a run of characters with box face,
3854 i.e. this character has a shadow on the left side. */
3855 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3856 && (old_face == NULL || !old_face->box));
3857 it->face_box_p = new_face->box != FACE_NO_BOX;
3860 else
3862 int base_face_id;
3863 ptrdiff_t bufpos;
3864 int i;
3865 Lisp_Object from_overlay
3866 = (it->current.overlay_string_index >= 0
3867 ? it->string_overlays[it->current.overlay_string_index
3868 % OVERLAY_STRING_CHUNK_SIZE]
3869 : Qnil);
3871 /* See if we got to this string directly or indirectly from
3872 an overlay property. That includes the before-string or
3873 after-string of an overlay, strings in display properties
3874 provided by an overlay, their text properties, etc.
3876 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3877 if (! NILP (from_overlay))
3878 for (i = it->sp - 1; i >= 0; i--)
3880 if (it->stack[i].current.overlay_string_index >= 0)
3881 from_overlay
3882 = it->string_overlays[it->stack[i].current.overlay_string_index
3883 % OVERLAY_STRING_CHUNK_SIZE];
3884 else if (! NILP (it->stack[i].from_overlay))
3885 from_overlay = it->stack[i].from_overlay;
3887 if (!NILP (from_overlay))
3888 break;
3891 if (! NILP (from_overlay))
3893 bufpos = IT_CHARPOS (*it);
3894 /* For a string from an overlay, the base face depends
3895 only on text properties and ignores overlays. */
3896 base_face_id
3897 = face_for_overlay_string (it->w,
3898 IT_CHARPOS (*it),
3899 &next_stop,
3900 (IT_CHARPOS (*it)
3901 + TEXT_PROP_DISTANCE_LIMIT),
3903 from_overlay);
3905 else
3907 bufpos = 0;
3909 /* For strings from a `display' property, use the face at
3910 IT's current buffer position as the base face to merge
3911 with, so that overlay strings appear in the same face as
3912 surrounding text, unless they specify their own faces.
3913 For strings from wrap-prefix and line-prefix properties,
3914 use the default face, possibly remapped via
3915 Vface_remapping_alist. */
3916 base_face_id = it->string_from_prefix_prop_p
3917 ? (!NILP (Vface_remapping_alist)
3918 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3919 : DEFAULT_FACE_ID)
3920 : underlying_face_id (it);
3923 new_face_id = face_at_string_position (it->w,
3924 it->string,
3925 IT_STRING_CHARPOS (*it),
3926 bufpos,
3927 &next_stop,
3928 base_face_id, 0);
3930 /* Is this a start of a run of characters with box? Caveat:
3931 this can be called for a freshly allocated iterator; face_id
3932 is -1 is this case. We know that the new face will not
3933 change until the next check pos, i.e. if the new face has a
3934 box, all characters up to that position will have a
3935 box. But, as usual, we don't know whether that position
3936 is really the end. */
3937 if (new_face_id != it->face_id)
3939 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3940 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3942 /* If new face has a box but old face hasn't, this is the
3943 start of a run of characters with box, i.e. it has a
3944 shadow on the left side. */
3945 it->start_of_box_run_p
3946 = new_face->box && (old_face == NULL || !old_face->box);
3947 it->face_box_p = new_face->box != FACE_NO_BOX;
3951 it->face_id = new_face_id;
3952 return HANDLED_NORMALLY;
3956 /* Return the ID of the face ``underlying'' IT's current position,
3957 which is in a string. If the iterator is associated with a
3958 buffer, return the face at IT's current buffer position.
3959 Otherwise, use the iterator's base_face_id. */
3961 static int
3962 underlying_face_id (struct it *it)
3964 int face_id = it->base_face_id, i;
3966 eassert (STRINGP (it->string));
3968 for (i = it->sp - 1; i >= 0; --i)
3969 if (NILP (it->stack[i].string))
3970 face_id = it->stack[i].face_id;
3972 return face_id;
3976 /* Compute the face one character before or after the current position
3977 of IT, in the visual order. BEFORE_P non-zero means get the face
3978 in front (to the left in L2R paragraphs, to the right in R2L
3979 paragraphs) of IT's screen position. Value is the ID of the face. */
3981 static int
3982 face_before_or_after_it_pos (struct it *it, int before_p)
3984 int face_id, limit;
3985 ptrdiff_t next_check_charpos;
3986 struct it it_copy;
3987 void *it_copy_data = NULL;
3989 eassert (it->s == NULL);
3991 if (STRINGP (it->string))
3993 ptrdiff_t bufpos, charpos;
3994 int base_face_id;
3996 /* No face change past the end of the string (for the case
3997 we are padding with spaces). No face change before the
3998 string start. */
3999 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4000 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4001 return it->face_id;
4003 if (!it->bidi_p)
4005 /* Set charpos to the position before or after IT's current
4006 position, in the logical order, which in the non-bidi
4007 case is the same as the visual order. */
4008 if (before_p)
4009 charpos = IT_STRING_CHARPOS (*it) - 1;
4010 else if (it->what == IT_COMPOSITION)
4011 /* For composition, we must check the character after the
4012 composition. */
4013 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4014 else
4015 charpos = IT_STRING_CHARPOS (*it) + 1;
4017 else
4019 if (before_p)
4021 /* With bidi iteration, the character before the current
4022 in the visual order cannot be found by simple
4023 iteration, because "reverse" reordering is not
4024 supported. Instead, we need to use the move_it_*
4025 family of functions. */
4026 /* Ignore face changes before the first visible
4027 character on this display line. */
4028 if (it->current_x <= it->first_visible_x)
4029 return it->face_id;
4030 SAVE_IT (it_copy, *it, it_copy_data);
4031 /* Implementation note: Since move_it_in_display_line
4032 works in the iterator geometry, and thinks the first
4033 character is always the leftmost, even in R2L lines,
4034 we don't need to distinguish between the R2L and L2R
4035 cases here. */
4036 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4037 it_copy.current_x - 1, MOVE_TO_X);
4038 charpos = IT_STRING_CHARPOS (it_copy);
4039 RESTORE_IT (it, it, it_copy_data);
4041 else
4043 /* Set charpos to the string position of the character
4044 that comes after IT's current position in the visual
4045 order. */
4046 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4048 it_copy = *it;
4049 while (n--)
4050 bidi_move_to_visually_next (&it_copy.bidi_it);
4052 charpos = it_copy.bidi_it.charpos;
4055 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4057 if (it->current.overlay_string_index >= 0)
4058 bufpos = IT_CHARPOS (*it);
4059 else
4060 bufpos = 0;
4062 base_face_id = underlying_face_id (it);
4064 /* Get the face for ASCII, or unibyte. */
4065 face_id = face_at_string_position (it->w,
4066 it->string,
4067 charpos,
4068 bufpos,
4069 &next_check_charpos,
4070 base_face_id, 0);
4072 /* Correct the face for charsets different from ASCII. Do it
4073 for the multibyte case only. The face returned above is
4074 suitable for unibyte text if IT->string is unibyte. */
4075 if (STRING_MULTIBYTE (it->string))
4077 struct text_pos pos1 = string_pos (charpos, it->string);
4078 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4079 int c, len;
4080 struct face *face = FACE_FROM_ID (it->f, face_id);
4082 c = string_char_and_length (p, &len);
4083 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4086 else
4088 struct text_pos pos;
4090 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4091 || (IT_CHARPOS (*it) <= BEGV && before_p))
4092 return it->face_id;
4094 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4095 pos = it->current.pos;
4097 if (!it->bidi_p)
4099 if (before_p)
4100 DEC_TEXT_POS (pos, it->multibyte_p);
4101 else
4103 if (it->what == IT_COMPOSITION)
4105 /* For composition, we must check the position after
4106 the composition. */
4107 pos.charpos += it->cmp_it.nchars;
4108 pos.bytepos += it->len;
4110 else
4111 INC_TEXT_POS (pos, it->multibyte_p);
4114 else
4116 if (before_p)
4118 /* With bidi iteration, the character before the current
4119 in the visual order cannot be found by simple
4120 iteration, because "reverse" reordering is not
4121 supported. Instead, we need to use the move_it_*
4122 family of functions. */
4123 /* Ignore face changes before the first visible
4124 character on this display line. */
4125 if (it->current_x <= it->first_visible_x)
4126 return it->face_id;
4127 SAVE_IT (it_copy, *it, it_copy_data);
4128 /* Implementation note: Since move_it_in_display_line
4129 works in the iterator geometry, and thinks the first
4130 character is always the leftmost, even in R2L lines,
4131 we don't need to distinguish between the R2L and L2R
4132 cases here. */
4133 move_it_in_display_line (&it_copy, ZV,
4134 it_copy.current_x - 1, MOVE_TO_X);
4135 pos = it_copy.current.pos;
4136 RESTORE_IT (it, it, it_copy_data);
4138 else
4140 /* Set charpos to the buffer position of the character
4141 that comes after IT's current position in the visual
4142 order. */
4143 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4145 it_copy = *it;
4146 while (n--)
4147 bidi_move_to_visually_next (&it_copy.bidi_it);
4149 SET_TEXT_POS (pos,
4150 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4153 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4155 /* Determine face for CHARSET_ASCII, or unibyte. */
4156 face_id = face_at_buffer_position (it->w,
4157 CHARPOS (pos),
4158 &next_check_charpos,
4159 limit, 0, -1);
4161 /* Correct the face for charsets different from ASCII. Do it
4162 for the multibyte case only. The face returned above is
4163 suitable for unibyte text if current_buffer is unibyte. */
4164 if (it->multibyte_p)
4166 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4167 struct face *face = FACE_FROM_ID (it->f, face_id);
4168 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4172 return face_id;
4177 /***********************************************************************
4178 Invisible text
4179 ***********************************************************************/
4181 /* Set up iterator IT from invisible properties at its current
4182 position. Called from handle_stop. */
4184 static enum prop_handled
4185 handle_invisible_prop (struct it *it)
4187 enum prop_handled handled = HANDLED_NORMALLY;
4188 int invis_p;
4189 Lisp_Object prop;
4191 if (STRINGP (it->string))
4193 Lisp_Object end_charpos, limit, charpos;
4195 /* Get the value of the invisible text property at the
4196 current position. Value will be nil if there is no such
4197 property. */
4198 charpos = make_number (IT_STRING_CHARPOS (*it));
4199 prop = Fget_text_property (charpos, Qinvisible, it->string);
4200 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4202 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4204 /* Record whether we have to display an ellipsis for the
4205 invisible text. */
4206 int display_ellipsis_p = (invis_p == 2);
4207 ptrdiff_t len, endpos;
4209 handled = HANDLED_RECOMPUTE_PROPS;
4211 /* Get the position at which the next visible text can be
4212 found in IT->string, if any. */
4213 endpos = len = SCHARS (it->string);
4214 XSETINT (limit, len);
4217 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4218 it->string, limit);
4219 if (INTEGERP (end_charpos))
4221 endpos = XFASTINT (end_charpos);
4222 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4223 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4224 if (invis_p == 2)
4225 display_ellipsis_p = true;
4228 while (invis_p && endpos < len);
4230 if (display_ellipsis_p)
4231 it->ellipsis_p = true;
4233 if (endpos < len)
4235 /* Text at END_CHARPOS is visible. Move IT there. */
4236 struct text_pos old;
4237 ptrdiff_t oldpos;
4239 old = it->current.string_pos;
4240 oldpos = CHARPOS (old);
4241 if (it->bidi_p)
4243 if (it->bidi_it.first_elt
4244 && it->bidi_it.charpos < SCHARS (it->string))
4245 bidi_paragraph_init (it->paragraph_embedding,
4246 &it->bidi_it, 1);
4247 /* Bidi-iterate out of the invisible text. */
4250 bidi_move_to_visually_next (&it->bidi_it);
4252 while (oldpos <= it->bidi_it.charpos
4253 && it->bidi_it.charpos < endpos);
4255 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4256 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4257 if (IT_CHARPOS (*it) >= endpos)
4258 it->prev_stop = endpos;
4260 else
4262 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4263 compute_string_pos (&it->current.string_pos, old, it->string);
4266 else
4268 /* The rest of the string is invisible. If this is an
4269 overlay string, proceed with the next overlay string
4270 or whatever comes and return a character from there. */
4271 if (it->current.overlay_string_index >= 0
4272 && !display_ellipsis_p)
4274 next_overlay_string (it);
4275 /* Don't check for overlay strings when we just
4276 finished processing them. */
4277 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4279 else
4281 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4282 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4287 else
4289 ptrdiff_t newpos, next_stop, start_charpos, tem;
4290 Lisp_Object pos, overlay;
4292 /* First of all, is there invisible text at this position? */
4293 tem = start_charpos = IT_CHARPOS (*it);
4294 pos = make_number (tem);
4295 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4296 &overlay);
4297 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4299 /* If we are on invisible text, skip over it. */
4300 if (invis_p && start_charpos < it->end_charpos)
4302 /* Record whether we have to display an ellipsis for the
4303 invisible text. */
4304 int display_ellipsis_p = invis_p == 2;
4306 handled = HANDLED_RECOMPUTE_PROPS;
4308 /* Loop skipping over invisible text. The loop is left at
4309 ZV or with IT on the first char being visible again. */
4312 /* Try to skip some invisible text. Return value is the
4313 position reached which can be equal to where we start
4314 if there is nothing invisible there. This skips both
4315 over invisible text properties and overlays with
4316 invisible property. */
4317 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4319 /* If we skipped nothing at all we weren't at invisible
4320 text in the first place. If everything to the end of
4321 the buffer was skipped, end the loop. */
4322 if (newpos == tem || newpos >= ZV)
4323 invis_p = 0;
4324 else
4326 /* We skipped some characters but not necessarily
4327 all there are. Check if we ended up on visible
4328 text. Fget_char_property returns the property of
4329 the char before the given position, i.e. if we
4330 get invis_p = 0, this means that the char at
4331 newpos is visible. */
4332 pos = make_number (newpos);
4333 prop = Fget_char_property (pos, Qinvisible, it->window);
4334 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4337 /* If we ended up on invisible text, proceed to
4338 skip starting with next_stop. */
4339 if (invis_p)
4340 tem = next_stop;
4342 /* If there are adjacent invisible texts, don't lose the
4343 second one's ellipsis. */
4344 if (invis_p == 2)
4345 display_ellipsis_p = true;
4347 while (invis_p);
4349 /* The position newpos is now either ZV or on visible text. */
4350 if (it->bidi_p)
4352 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4353 int on_newline
4354 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4355 int after_newline
4356 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4358 /* If the invisible text ends on a newline or on a
4359 character after a newline, we can avoid the costly,
4360 character by character, bidi iteration to NEWPOS, and
4361 instead simply reseat the iterator there. That's
4362 because all bidi reordering information is tossed at
4363 the newline. This is a big win for modes that hide
4364 complete lines, like Outline, Org, etc. */
4365 if (on_newline || after_newline)
4367 struct text_pos tpos;
4368 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4370 SET_TEXT_POS (tpos, newpos, bpos);
4371 reseat_1 (it, tpos, 0);
4372 /* If we reseat on a newline/ZV, we need to prep the
4373 bidi iterator for advancing to the next character
4374 after the newline/EOB, keeping the current paragraph
4375 direction (so that PRODUCE_GLYPHS does TRT wrt
4376 prepending/appending glyphs to a glyph row). */
4377 if (on_newline)
4379 it->bidi_it.first_elt = 0;
4380 it->bidi_it.paragraph_dir = pdir;
4381 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4382 it->bidi_it.nchars = 1;
4383 it->bidi_it.ch_len = 1;
4386 else /* Must use the slow method. */
4388 /* With bidi iteration, the region of invisible text
4389 could start and/or end in the middle of a
4390 non-base embedding level. Therefore, we need to
4391 skip invisible text using the bidi iterator,
4392 starting at IT's current position, until we find
4393 ourselves outside of the invisible text.
4394 Skipping invisible text _after_ bidi iteration
4395 avoids affecting the visual order of the
4396 displayed text when invisible properties are
4397 added or removed. */
4398 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4400 /* If we were `reseat'ed to a new paragraph,
4401 determine the paragraph base direction. We
4402 need to do it now because
4403 next_element_from_buffer may not have a
4404 chance to do it, if we are going to skip any
4405 text at the beginning, which resets the
4406 FIRST_ELT flag. */
4407 bidi_paragraph_init (it->paragraph_embedding,
4408 &it->bidi_it, 1);
4412 bidi_move_to_visually_next (&it->bidi_it);
4414 while (it->stop_charpos <= it->bidi_it.charpos
4415 && it->bidi_it.charpos < newpos);
4416 IT_CHARPOS (*it) = it->bidi_it.charpos;
4417 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4418 /* If we overstepped NEWPOS, record its position in
4419 the iterator, so that we skip invisible text if
4420 later the bidi iteration lands us in the
4421 invisible region again. */
4422 if (IT_CHARPOS (*it) >= newpos)
4423 it->prev_stop = newpos;
4426 else
4428 IT_CHARPOS (*it) = newpos;
4429 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4432 /* If there are before-strings at the start of invisible
4433 text, and the text is invisible because of a text
4434 property, arrange to show before-strings because 20.x did
4435 it that way. (If the text is invisible because of an
4436 overlay property instead of a text property, this is
4437 already handled in the overlay code.) */
4438 if (NILP (overlay)
4439 && get_overlay_strings (it, it->stop_charpos))
4441 handled = HANDLED_RECOMPUTE_PROPS;
4442 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4444 else if (display_ellipsis_p)
4446 /* Make sure that the glyphs of the ellipsis will get
4447 correct `charpos' values. If we would not update
4448 it->position here, the glyphs would belong to the
4449 last visible character _before_ the invisible
4450 text, which confuses `set_cursor_from_row'.
4452 We use the last invisible position instead of the
4453 first because this way the cursor is always drawn on
4454 the first "." of the ellipsis, whenever PT is inside
4455 the invisible text. Otherwise the cursor would be
4456 placed _after_ the ellipsis when the point is after the
4457 first invisible character. */
4458 if (!STRINGP (it->object))
4460 it->position.charpos = newpos - 1;
4461 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4463 it->ellipsis_p = true;
4464 /* Let the ellipsis display before
4465 considering any properties of the following char.
4466 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4467 handled = HANDLED_RETURN;
4472 return handled;
4476 /* Make iterator IT return `...' next.
4477 Replaces LEN characters from buffer. */
4479 static void
4480 setup_for_ellipsis (struct it *it, int len)
4482 /* Use the display table definition for `...'. Invalid glyphs
4483 will be handled by the method returning elements from dpvec. */
4484 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4486 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4487 it->dpvec = v->contents;
4488 it->dpend = v->contents + v->header.size;
4490 else
4492 /* Default `...'. */
4493 it->dpvec = default_invis_vector;
4494 it->dpend = default_invis_vector + 3;
4497 it->dpvec_char_len = len;
4498 it->current.dpvec_index = 0;
4499 it->dpvec_face_id = -1;
4501 /* Remember the current face id in case glyphs specify faces.
4502 IT's face is restored in set_iterator_to_next.
4503 saved_face_id was set to preceding char's face in handle_stop. */
4504 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4505 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4507 it->method = GET_FROM_DISPLAY_VECTOR;
4508 it->ellipsis_p = true;
4513 /***********************************************************************
4514 'display' property
4515 ***********************************************************************/
4517 /* Set up iterator IT from `display' property at its current position.
4518 Called from handle_stop.
4519 We return HANDLED_RETURN if some part of the display property
4520 overrides the display of the buffer text itself.
4521 Otherwise we return HANDLED_NORMALLY. */
4523 static enum prop_handled
4524 handle_display_prop (struct it *it)
4526 Lisp_Object propval, object, overlay;
4527 struct text_pos *position;
4528 ptrdiff_t bufpos;
4529 /* Nonzero if some property replaces the display of the text itself. */
4530 int display_replaced_p = 0;
4532 if (STRINGP (it->string))
4534 object = it->string;
4535 position = &it->current.string_pos;
4536 bufpos = CHARPOS (it->current.pos);
4538 else
4540 XSETWINDOW (object, it->w);
4541 position = &it->current.pos;
4542 bufpos = CHARPOS (*position);
4545 /* Reset those iterator values set from display property values. */
4546 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4547 it->space_width = Qnil;
4548 it->font_height = Qnil;
4549 it->voffset = 0;
4551 /* We don't support recursive `display' properties, i.e. string
4552 values that have a string `display' property, that have a string
4553 `display' property etc. */
4554 if (!it->string_from_display_prop_p)
4555 it->area = TEXT_AREA;
4557 propval = get_char_property_and_overlay (make_number (position->charpos),
4558 Qdisplay, object, &overlay);
4559 if (NILP (propval))
4560 return HANDLED_NORMALLY;
4561 /* Now OVERLAY is the overlay that gave us this property, or nil
4562 if it was a text property. */
4564 if (!STRINGP (it->string))
4565 object = it->w->contents;
4567 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4568 position, bufpos,
4569 FRAME_WINDOW_P (it->f));
4571 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4574 /* Subroutine of handle_display_prop. Returns non-zero if the display
4575 specification in SPEC is a replacing specification, i.e. it would
4576 replace the text covered by `display' property with something else,
4577 such as an image or a display string. If SPEC includes any kind or
4578 `(space ...) specification, the value is 2; this is used by
4579 compute_display_string_pos, which see.
4581 See handle_single_display_spec for documentation of arguments.
4582 frame_window_p is non-zero if the window being redisplayed is on a
4583 GUI frame; this argument is used only if IT is NULL, see below.
4585 IT can be NULL, if this is called by the bidi reordering code
4586 through compute_display_string_pos, which see. In that case, this
4587 function only examines SPEC, but does not otherwise "handle" it, in
4588 the sense that it doesn't set up members of IT from the display
4589 spec. */
4590 static int
4591 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4592 Lisp_Object overlay, struct text_pos *position,
4593 ptrdiff_t bufpos, int frame_window_p)
4595 int replacing_p = 0;
4596 int rv;
4598 if (CONSP (spec)
4599 /* Simple specifications. */
4600 && !EQ (XCAR (spec), Qimage)
4601 && !EQ (XCAR (spec), Qspace)
4602 && !EQ (XCAR (spec), Qwhen)
4603 && !EQ (XCAR (spec), Qslice)
4604 && !EQ (XCAR (spec), Qspace_width)
4605 && !EQ (XCAR (spec), Qheight)
4606 && !EQ (XCAR (spec), Qraise)
4607 /* Marginal area specifications. */
4608 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4609 && !EQ (XCAR (spec), Qleft_fringe)
4610 && !EQ (XCAR (spec), Qright_fringe)
4611 && !NILP (XCAR (spec)))
4613 for (; CONSP (spec); spec = XCDR (spec))
4615 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4616 overlay, position, bufpos,
4617 replacing_p, frame_window_p)))
4619 replacing_p = rv;
4620 /* If some text in a string is replaced, `position' no
4621 longer points to the position of `object'. */
4622 if (!it || STRINGP (object))
4623 break;
4627 else if (VECTORP (spec))
4629 ptrdiff_t i;
4630 for (i = 0; i < ASIZE (spec); ++i)
4631 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4632 overlay, position, bufpos,
4633 replacing_p, frame_window_p)))
4635 replacing_p = rv;
4636 /* If some text in a string is replaced, `position' no
4637 longer points to the position of `object'. */
4638 if (!it || STRINGP (object))
4639 break;
4642 else
4644 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4645 position, bufpos, 0,
4646 frame_window_p)))
4647 replacing_p = rv;
4650 return replacing_p;
4653 /* Value is the position of the end of the `display' property starting
4654 at START_POS in OBJECT. */
4656 static struct text_pos
4657 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4659 Lisp_Object end;
4660 struct text_pos end_pos;
4662 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4663 Qdisplay, object, Qnil);
4664 CHARPOS (end_pos) = XFASTINT (end);
4665 if (STRINGP (object))
4666 compute_string_pos (&end_pos, start_pos, it->string);
4667 else
4668 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4670 return end_pos;
4674 /* Set up IT from a single `display' property specification SPEC. OBJECT
4675 is the object in which the `display' property was found. *POSITION
4676 is the position in OBJECT at which the `display' property was found.
4677 BUFPOS is the buffer position of OBJECT (different from POSITION if
4678 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4679 previously saw a display specification which already replaced text
4680 display with something else, for example an image; we ignore such
4681 properties after the first one has been processed.
4683 OVERLAY is the overlay this `display' property came from,
4684 or nil if it was a text property.
4686 If SPEC is a `space' or `image' specification, and in some other
4687 cases too, set *POSITION to the position where the `display'
4688 property ends.
4690 If IT is NULL, only examine the property specification in SPEC, but
4691 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4692 is intended to be displayed in a window on a GUI frame.
4694 Value is non-zero if something was found which replaces the display
4695 of buffer or string text. */
4697 static int
4698 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4699 Lisp_Object overlay, struct text_pos *position,
4700 ptrdiff_t bufpos, int display_replaced_p,
4701 int frame_window_p)
4703 Lisp_Object form;
4704 Lisp_Object location, value;
4705 struct text_pos start_pos = *position;
4706 int valid_p;
4708 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4709 If the result is non-nil, use VALUE instead of SPEC. */
4710 form = Qt;
4711 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4713 spec = XCDR (spec);
4714 if (!CONSP (spec))
4715 return 0;
4716 form = XCAR (spec);
4717 spec = XCDR (spec);
4720 if (!NILP (form) && !EQ (form, Qt))
4722 ptrdiff_t count = SPECPDL_INDEX ();
4723 struct gcpro gcpro1;
4725 /* Bind `object' to the object having the `display' property, a
4726 buffer or string. Bind `position' to the position in the
4727 object where the property was found, and `buffer-position'
4728 to the current position in the buffer. */
4730 if (NILP (object))
4731 XSETBUFFER (object, current_buffer);
4732 specbind (Qobject, object);
4733 specbind (Qposition, make_number (CHARPOS (*position)));
4734 specbind (Qbuffer_position, make_number (bufpos));
4735 GCPRO1 (form);
4736 form = safe_eval (form);
4737 UNGCPRO;
4738 unbind_to (count, Qnil);
4741 if (NILP (form))
4742 return 0;
4744 /* Handle `(height HEIGHT)' specifications. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qheight)
4747 && CONSP (XCDR (spec)))
4749 if (it)
4751 if (!FRAME_WINDOW_P (it->f))
4752 return 0;
4754 it->font_height = XCAR (XCDR (spec));
4755 if (!NILP (it->font_height))
4757 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4758 int new_height = -1;
4760 if (CONSP (it->font_height)
4761 && (EQ (XCAR (it->font_height), Qplus)
4762 || EQ (XCAR (it->font_height), Qminus))
4763 && CONSP (XCDR (it->font_height))
4764 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4766 /* `(+ N)' or `(- N)' where N is an integer. */
4767 int steps = XINT (XCAR (XCDR (it->font_height)));
4768 if (EQ (XCAR (it->font_height), Qplus))
4769 steps = - steps;
4770 it->face_id = smaller_face (it->f, it->face_id, steps);
4772 else if (FUNCTIONP (it->font_height))
4774 /* Call function with current height as argument.
4775 Value is the new height. */
4776 Lisp_Object height;
4777 height = safe_call1 (it->font_height,
4778 face->lface[LFACE_HEIGHT_INDEX]);
4779 if (NUMBERP (height))
4780 new_height = XFLOATINT (height);
4782 else if (NUMBERP (it->font_height))
4784 /* Value is a multiple of the canonical char height. */
4785 struct face *f;
4787 f = FACE_FROM_ID (it->f,
4788 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4789 new_height = (XFLOATINT (it->font_height)
4790 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4792 else
4794 /* Evaluate IT->font_height with `height' bound to the
4795 current specified height to get the new height. */
4796 ptrdiff_t count = SPECPDL_INDEX ();
4798 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4799 value = safe_eval (it->font_height);
4800 unbind_to (count, Qnil);
4802 if (NUMBERP (value))
4803 new_height = XFLOATINT (value);
4806 if (new_height > 0)
4807 it->face_id = face_with_height (it->f, it->face_id, new_height);
4811 return 0;
4814 /* Handle `(space-width WIDTH)'. */
4815 if (CONSP (spec)
4816 && EQ (XCAR (spec), Qspace_width)
4817 && CONSP (XCDR (spec)))
4819 if (it)
4821 if (!FRAME_WINDOW_P (it->f))
4822 return 0;
4824 value = XCAR (XCDR (spec));
4825 if (NUMBERP (value) && XFLOATINT (value) > 0)
4826 it->space_width = value;
4829 return 0;
4832 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4833 if (CONSP (spec)
4834 && EQ (XCAR (spec), Qslice))
4836 Lisp_Object tem;
4838 if (it)
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4843 if (tem = XCDR (spec), CONSP (tem))
4845 it->slice.x = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4848 it->slice.y = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4851 it->slice.width = XCAR (tem);
4852 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.height = XCAR (tem);
4859 return 0;
4862 /* Handle `(raise FACTOR)'. */
4863 if (CONSP (spec)
4864 && EQ (XCAR (spec), Qraise)
4865 && CONSP (XCDR (spec)))
4867 if (it)
4869 if (!FRAME_WINDOW_P (it->f))
4870 return 0;
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 value = XCAR (XCDR (spec));
4874 if (NUMBERP (value))
4876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4877 it->voffset = - (XFLOATINT (value)
4878 * (FONT_HEIGHT (face->font)));
4880 #endif /* HAVE_WINDOW_SYSTEM */
4883 return 0;
4886 /* Don't handle the other kinds of display specifications
4887 inside a string that we got from a `display' property. */
4888 if (it && it->string_from_display_prop_p)
4889 return 0;
4891 /* Characters having this form of property are not displayed, so
4892 we have to find the end of the property. */
4893 if (it)
4895 start_pos = *position;
4896 *position = display_prop_end (it, object, start_pos);
4898 value = Qnil;
4900 /* Stop the scan at that end position--we assume that all
4901 text properties change there. */
4902 if (it)
4903 it->stop_charpos = position->charpos;
4905 /* Handle `(left-fringe BITMAP [FACE])'
4906 and `(right-fringe BITMAP [FACE])'. */
4907 if (CONSP (spec)
4908 && (EQ (XCAR (spec), Qleft_fringe)
4909 || EQ (XCAR (spec), Qright_fringe))
4910 && CONSP (XCDR (spec)))
4912 int fringe_bitmap;
4914 if (it)
4916 if (!FRAME_WINDOW_P (it->f))
4917 /* If we return here, POSITION has been advanced
4918 across the text with this property. */
4920 /* Synchronize the bidi iterator with POSITION. This is
4921 needed because we are not going to push the iterator
4922 on behalf of this display property, so there will be
4923 no pop_it call to do this synchronization for us. */
4924 if (it->bidi_p)
4926 it->position = *position;
4927 iterate_out_of_display_property (it);
4928 *position = it->position;
4930 return 1;
4933 else if (!frame_window_p)
4934 return 1;
4936 #ifdef HAVE_WINDOW_SYSTEM
4937 value = XCAR (XCDR (spec));
4938 if (!SYMBOLP (value)
4939 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4940 /* If we return here, POSITION has been advanced
4941 across the text with this property. */
4943 if (it && it->bidi_p)
4945 it->position = *position;
4946 iterate_out_of_display_property (it);
4947 *position = it->position;
4949 return 1;
4952 if (it)
4954 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4956 if (CONSP (XCDR (XCDR (spec))))
4958 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4959 int face_id2 = lookup_derived_face (it->f, face_name,
4960 FRINGE_FACE_ID, 0);
4961 if (face_id2 >= 0)
4962 face_id = face_id2;
4965 /* Save current settings of IT so that we can restore them
4966 when we are finished with the glyph property value. */
4967 push_it (it, position);
4969 it->area = TEXT_AREA;
4970 it->what = IT_IMAGE;
4971 it->image_id = -1; /* no image */
4972 it->position = start_pos;
4973 it->object = NILP (object) ? it->w->contents : object;
4974 it->method = GET_FROM_IMAGE;
4975 it->from_overlay = Qnil;
4976 it->face_id = face_id;
4977 it->from_disp_prop_p = true;
4979 /* Say that we haven't consumed the characters with
4980 `display' property yet. The call to pop_it in
4981 set_iterator_to_next will clean this up. */
4982 *position = start_pos;
4984 if (EQ (XCAR (spec), Qleft_fringe))
4986 it->left_user_fringe_bitmap = fringe_bitmap;
4987 it->left_user_fringe_face_id = face_id;
4989 else
4991 it->right_user_fringe_bitmap = fringe_bitmap;
4992 it->right_user_fringe_face_id = face_id;
4995 #endif /* HAVE_WINDOW_SYSTEM */
4996 return 1;
4999 /* Prepare to handle `((margin left-margin) ...)',
5000 `((margin right-margin) ...)' and `((margin nil) ...)'
5001 prefixes for display specifications. */
5002 location = Qunbound;
5003 if (CONSP (spec) && CONSP (XCAR (spec)))
5005 Lisp_Object tem;
5007 value = XCDR (spec);
5008 if (CONSP (value))
5009 value = XCAR (value);
5011 tem = XCAR (spec);
5012 if (EQ (XCAR (tem), Qmargin)
5013 && (tem = XCDR (tem),
5014 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5015 (NILP (tem)
5016 || EQ (tem, Qleft_margin)
5017 || EQ (tem, Qright_margin))))
5018 location = tem;
5021 if (EQ (location, Qunbound))
5023 location = Qnil;
5024 value = spec;
5027 /* After this point, VALUE is the property after any
5028 margin prefix has been stripped. It must be a string,
5029 an image specification, or `(space ...)'.
5031 LOCATION specifies where to display: `left-margin',
5032 `right-margin' or nil. */
5034 valid_p = (STRINGP (value)
5035 #ifdef HAVE_WINDOW_SYSTEM
5036 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5037 && valid_image_p (value))
5038 #endif /* not HAVE_WINDOW_SYSTEM */
5039 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5041 if (valid_p && !display_replaced_p)
5043 int retval = 1;
5045 if (!it)
5047 /* Callers need to know whether the display spec is any kind
5048 of `(space ...)' spec that is about to affect text-area
5049 display. */
5050 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5051 retval = 2;
5052 return retval;
5055 /* Save current settings of IT so that we can restore them
5056 when we are finished with the glyph property value. */
5057 push_it (it, position);
5058 it->from_overlay = overlay;
5059 it->from_disp_prop_p = true;
5061 if (NILP (location))
5062 it->area = TEXT_AREA;
5063 else if (EQ (location, Qleft_margin))
5064 it->area = LEFT_MARGIN_AREA;
5065 else
5066 it->area = RIGHT_MARGIN_AREA;
5068 if (STRINGP (value))
5070 it->string = value;
5071 it->multibyte_p = STRING_MULTIBYTE (it->string);
5072 it->current.overlay_string_index = -1;
5073 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5074 it->end_charpos = it->string_nchars = SCHARS (it->string);
5075 it->method = GET_FROM_STRING;
5076 it->stop_charpos = 0;
5077 it->prev_stop = 0;
5078 it->base_level_stop = 0;
5079 it->string_from_display_prop_p = true;
5080 /* Say that we haven't consumed the characters with
5081 `display' property yet. The call to pop_it in
5082 set_iterator_to_next will clean this up. */
5083 if (BUFFERP (object))
5084 *position = start_pos;
5086 /* Force paragraph direction to be that of the parent
5087 object. If the parent object's paragraph direction is
5088 not yet determined, default to L2R. */
5089 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5090 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5091 else
5092 it->paragraph_embedding = L2R;
5094 /* Set up the bidi iterator for this display string. */
5095 if (it->bidi_p)
5097 it->bidi_it.string.lstring = it->string;
5098 it->bidi_it.string.s = NULL;
5099 it->bidi_it.string.schars = it->end_charpos;
5100 it->bidi_it.string.bufpos = bufpos;
5101 it->bidi_it.string.from_disp_str = 1;
5102 it->bidi_it.string.unibyte = !it->multibyte_p;
5103 it->bidi_it.w = it->w;
5104 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5107 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5109 it->method = GET_FROM_STRETCH;
5110 it->object = value;
5111 *position = it->position = start_pos;
5112 retval = 1 + (it->area == TEXT_AREA);
5114 #ifdef HAVE_WINDOW_SYSTEM
5115 else
5117 it->what = IT_IMAGE;
5118 it->image_id = lookup_image (it->f, value);
5119 it->position = start_pos;
5120 it->object = NILP (object) ? it->w->contents : object;
5121 it->method = GET_FROM_IMAGE;
5123 /* Say that we haven't consumed the characters with
5124 `display' property yet. The call to pop_it in
5125 set_iterator_to_next will clean this up. */
5126 *position = start_pos;
5128 #endif /* HAVE_WINDOW_SYSTEM */
5130 return retval;
5133 /* Invalid property or property not supported. Restore
5134 POSITION to what it was before. */
5135 *position = start_pos;
5136 return 0;
5139 /* Check if PROP is a display property value whose text should be
5140 treated as intangible. OVERLAY is the overlay from which PROP
5141 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5142 specify the buffer position covered by PROP. */
5145 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5146 ptrdiff_t charpos, ptrdiff_t bytepos)
5148 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5149 struct text_pos position;
5151 SET_TEXT_POS (position, charpos, bytepos);
5152 return handle_display_spec (NULL, prop, Qnil, overlay,
5153 &position, charpos, frame_window_p);
5157 /* Return 1 if PROP is a display sub-property value containing STRING.
5159 Implementation note: this and the following function are really
5160 special cases of handle_display_spec and
5161 handle_single_display_spec, and should ideally use the same code.
5162 Until they do, these two pairs must be consistent and must be
5163 modified in sync. */
5165 static int
5166 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5168 if (EQ (string, prop))
5169 return 1;
5171 /* Skip over `when FORM'. */
5172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5174 prop = XCDR (prop);
5175 if (!CONSP (prop))
5176 return 0;
5177 /* Actually, the condition following `when' should be eval'ed,
5178 like handle_single_display_spec does, and we should return
5179 zero if it evaluates to nil. However, this function is
5180 called only when the buffer was already displayed and some
5181 glyph in the glyph matrix was found to come from a display
5182 string. Therefore, the condition was already evaluated, and
5183 the result was non-nil, otherwise the display string wouldn't
5184 have been displayed and we would have never been called for
5185 this property. Thus, we can skip the evaluation and assume
5186 its result is non-nil. */
5187 prop = XCDR (prop);
5190 if (CONSP (prop))
5191 /* Skip over `margin LOCATION'. */
5192 if (EQ (XCAR (prop), Qmargin))
5194 prop = XCDR (prop);
5195 if (!CONSP (prop))
5196 return 0;
5198 prop = XCDR (prop);
5199 if (!CONSP (prop))
5200 return 0;
5203 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5207 /* Return 1 if STRING appears in the `display' property PROP. */
5209 static int
5210 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5212 if (CONSP (prop)
5213 && !EQ (XCAR (prop), Qwhen)
5214 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5216 /* A list of sub-properties. */
5217 while (CONSP (prop))
5219 if (single_display_spec_string_p (XCAR (prop), string))
5220 return 1;
5221 prop = XCDR (prop);
5224 else if (VECTORP (prop))
5226 /* A vector of sub-properties. */
5227 ptrdiff_t i;
5228 for (i = 0; i < ASIZE (prop); ++i)
5229 if (single_display_spec_string_p (AREF (prop, i), string))
5230 return 1;
5232 else
5233 return single_display_spec_string_p (prop, string);
5235 return 0;
5238 /* Look for STRING in overlays and text properties in the current
5239 buffer, between character positions FROM and TO (excluding TO).
5240 BACK_P non-zero means look back (in this case, TO is supposed to be
5241 less than FROM).
5242 Value is the first character position where STRING was found, or
5243 zero if it wasn't found before hitting TO.
5245 This function may only use code that doesn't eval because it is
5246 called asynchronously from note_mouse_highlight. */
5248 static ptrdiff_t
5249 string_buffer_position_lim (Lisp_Object string,
5250 ptrdiff_t from, ptrdiff_t to, int back_p)
5252 Lisp_Object limit, prop, pos;
5253 int found = 0;
5255 pos = make_number (max (from, BEGV));
5257 if (!back_p) /* looking forward */
5259 limit = make_number (min (to, ZV));
5260 while (!found && !EQ (pos, limit))
5262 prop = Fget_char_property (pos, Qdisplay, Qnil);
5263 if (!NILP (prop) && display_prop_string_p (prop, string))
5264 found = 1;
5265 else
5266 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5267 limit);
5270 else /* looking back */
5272 limit = make_number (max (to, BEGV));
5273 while (!found && !EQ (pos, limit))
5275 prop = Fget_char_property (pos, Qdisplay, Qnil);
5276 if (!NILP (prop) && display_prop_string_p (prop, string))
5277 found = 1;
5278 else
5279 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5280 limit);
5284 return found ? XINT (pos) : 0;
5287 /* Determine which buffer position in current buffer STRING comes from.
5288 AROUND_CHARPOS is an approximate position where it could come from.
5289 Value is the buffer position or 0 if it couldn't be determined.
5291 This function is necessary because we don't record buffer positions
5292 in glyphs generated from strings (to keep struct glyph small).
5293 This function may only use code that doesn't eval because it is
5294 called asynchronously from note_mouse_highlight. */
5296 static ptrdiff_t
5297 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5299 const int MAX_DISTANCE = 1000;
5300 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5301 around_charpos + MAX_DISTANCE,
5304 if (!found)
5305 found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos - MAX_DISTANCE, 1);
5307 return found;
5312 /***********************************************************************
5313 `composition' property
5314 ***********************************************************************/
5316 /* Set up iterator IT from `composition' property at its current
5317 position. Called from handle_stop. */
5319 static enum prop_handled
5320 handle_composition_prop (struct it *it)
5322 Lisp_Object prop, string;
5323 ptrdiff_t pos, pos_byte, start, end;
5325 if (STRINGP (it->string))
5327 unsigned char *s;
5329 pos = IT_STRING_CHARPOS (*it);
5330 pos_byte = IT_STRING_BYTEPOS (*it);
5331 string = it->string;
5332 s = SDATA (string) + pos_byte;
5333 it->c = STRING_CHAR (s);
5335 else
5337 pos = IT_CHARPOS (*it);
5338 pos_byte = IT_BYTEPOS (*it);
5339 string = Qnil;
5340 it->c = FETCH_CHAR (pos_byte);
5343 /* If there's a valid composition and point is not inside of the
5344 composition (in the case that the composition is from the current
5345 buffer), draw a glyph composed from the composition components. */
5346 if (find_composition (pos, -1, &start, &end, &prop, string)
5347 && composition_valid_p (start, end, prop)
5348 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5350 if (start < pos)
5351 /* As we can't handle this situation (perhaps font-lock added
5352 a new composition), we just return here hoping that next
5353 redisplay will detect this composition much earlier. */
5354 return HANDLED_NORMALLY;
5355 if (start != pos)
5357 if (STRINGP (it->string))
5358 pos_byte = string_char_to_byte (it->string, start);
5359 else
5360 pos_byte = CHAR_TO_BYTE (start);
5362 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5363 prop, string);
5365 if (it->cmp_it.id >= 0)
5367 it->cmp_it.ch = -1;
5368 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5369 it->cmp_it.nglyphs = -1;
5373 return HANDLED_NORMALLY;
5378 /***********************************************************************
5379 Overlay strings
5380 ***********************************************************************/
5382 /* The following structure is used to record overlay strings for
5383 later sorting in load_overlay_strings. */
5385 struct overlay_entry
5387 Lisp_Object overlay;
5388 Lisp_Object string;
5389 EMACS_INT priority;
5390 int after_string_p;
5394 /* Set up iterator IT from overlay strings at its current position.
5395 Called from handle_stop. */
5397 static enum prop_handled
5398 handle_overlay_change (struct it *it)
5400 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5401 return HANDLED_RECOMPUTE_PROPS;
5402 else
5403 return HANDLED_NORMALLY;
5407 /* Set up the next overlay string for delivery by IT, if there is an
5408 overlay string to deliver. Called by set_iterator_to_next when the
5409 end of the current overlay string is reached. If there are more
5410 overlay strings to display, IT->string and
5411 IT->current.overlay_string_index are set appropriately here.
5412 Otherwise IT->string is set to nil. */
5414 static void
5415 next_overlay_string (struct it *it)
5417 ++it->current.overlay_string_index;
5418 if (it->current.overlay_string_index == it->n_overlay_strings)
5420 /* No more overlay strings. Restore IT's settings to what
5421 they were before overlay strings were processed, and
5422 continue to deliver from current_buffer. */
5424 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5425 pop_it (it);
5426 eassert (it->sp > 0
5427 || (NILP (it->string)
5428 && it->method == GET_FROM_BUFFER
5429 && it->stop_charpos >= BEGV
5430 && it->stop_charpos <= it->end_charpos));
5431 it->current.overlay_string_index = -1;
5432 it->n_overlay_strings = 0;
5433 it->overlay_strings_charpos = -1;
5434 /* If there's an empty display string on the stack, pop the
5435 stack, to resync the bidi iterator with IT's position. Such
5436 empty strings are pushed onto the stack in
5437 get_overlay_strings_1. */
5438 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5439 pop_it (it);
5441 /* If we're at the end of the buffer, record that we have
5442 processed the overlay strings there already, so that
5443 next_element_from_buffer doesn't try it again. */
5444 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5445 it->overlay_strings_at_end_processed_p = true;
5447 else
5449 /* There are more overlay strings to process. If
5450 IT->current.overlay_string_index has advanced to a position
5451 where we must load IT->overlay_strings with more strings, do
5452 it. We must load at the IT->overlay_strings_charpos where
5453 IT->n_overlay_strings was originally computed; when invisible
5454 text is present, this might not be IT_CHARPOS (Bug#7016). */
5455 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5457 if (it->current.overlay_string_index && i == 0)
5458 load_overlay_strings (it, it->overlay_strings_charpos);
5460 /* Initialize IT to deliver display elements from the overlay
5461 string. */
5462 it->string = it->overlay_strings[i];
5463 it->multibyte_p = STRING_MULTIBYTE (it->string);
5464 SET_TEXT_POS (it->current.string_pos, 0, 0);
5465 it->method = GET_FROM_STRING;
5466 it->stop_charpos = 0;
5467 it->end_charpos = SCHARS (it->string);
5468 if (it->cmp_it.stop_pos >= 0)
5469 it->cmp_it.stop_pos = 0;
5470 it->prev_stop = 0;
5471 it->base_level_stop = 0;
5473 /* Set up the bidi iterator for this overlay string. */
5474 if (it->bidi_p)
5476 it->bidi_it.string.lstring = it->string;
5477 it->bidi_it.string.s = NULL;
5478 it->bidi_it.string.schars = SCHARS (it->string);
5479 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5480 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5481 it->bidi_it.string.unibyte = !it->multibyte_p;
5482 it->bidi_it.w = it->w;
5483 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5487 CHECK_IT (it);
5491 /* Compare two overlay_entry structures E1 and E2. Used as a
5492 comparison function for qsort in load_overlay_strings. Overlay
5493 strings for the same position are sorted so that
5495 1. All after-strings come in front of before-strings, except
5496 when they come from the same overlay.
5498 2. Within after-strings, strings are sorted so that overlay strings
5499 from overlays with higher priorities come first.
5501 2. Within before-strings, strings are sorted so that overlay
5502 strings from overlays with higher priorities come last.
5504 Value is analogous to strcmp. */
5507 static int
5508 compare_overlay_entries (const void *e1, const void *e2)
5510 struct overlay_entry const *entry1 = e1;
5511 struct overlay_entry const *entry2 = e2;
5512 int result;
5514 if (entry1->after_string_p != entry2->after_string_p)
5516 /* Let after-strings appear in front of before-strings if
5517 they come from different overlays. */
5518 if (EQ (entry1->overlay, entry2->overlay))
5519 result = entry1->after_string_p ? 1 : -1;
5520 else
5521 result = entry1->after_string_p ? -1 : 1;
5523 else if (entry1->priority != entry2->priority)
5525 if (entry1->after_string_p)
5526 /* After-strings sorted in order of decreasing priority. */
5527 result = entry2->priority < entry1->priority ? -1 : 1;
5528 else
5529 /* Before-strings sorted in order of increasing priority. */
5530 result = entry1->priority < entry2->priority ? -1 : 1;
5532 else
5533 result = 0;
5535 return result;
5539 /* Load the vector IT->overlay_strings with overlay strings from IT's
5540 current buffer position, or from CHARPOS if that is > 0. Set
5541 IT->n_overlays to the total number of overlay strings found.
5543 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5544 a time. On entry into load_overlay_strings,
5545 IT->current.overlay_string_index gives the number of overlay
5546 strings that have already been loaded by previous calls to this
5547 function.
5549 IT->add_overlay_start contains an additional overlay start
5550 position to consider for taking overlay strings from, if non-zero.
5551 This position comes into play when the overlay has an `invisible'
5552 property, and both before and after-strings. When we've skipped to
5553 the end of the overlay, because of its `invisible' property, we
5554 nevertheless want its before-string to appear.
5555 IT->add_overlay_start will contain the overlay start position
5556 in this case.
5558 Overlay strings are sorted so that after-string strings come in
5559 front of before-string strings. Within before and after-strings,
5560 strings are sorted by overlay priority. See also function
5561 compare_overlay_entries. */
5563 static void
5564 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5566 Lisp_Object overlay, window, str, invisible;
5567 struct Lisp_Overlay *ov;
5568 ptrdiff_t start, end;
5569 ptrdiff_t size = 20;
5570 ptrdiff_t n = 0, i, j;
5571 int invis_p;
5572 struct overlay_entry *entries = alloca (size * sizeof *entries);
5573 USE_SAFE_ALLOCA;
5575 if (charpos <= 0)
5576 charpos = IT_CHARPOS (*it);
5578 /* Append the overlay string STRING of overlay OVERLAY to vector
5579 `entries' which has size `size' and currently contains `n'
5580 elements. AFTER_P non-zero means STRING is an after-string of
5581 OVERLAY. */
5582 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5583 do \
5585 Lisp_Object priority; \
5587 if (n == size) \
5589 struct overlay_entry *old = entries; \
5590 SAFE_NALLOCA (entries, 2, size); \
5591 memcpy (entries, old, size * sizeof *entries); \
5592 size *= 2; \
5595 entries[n].string = (STRING); \
5596 entries[n].overlay = (OVERLAY); \
5597 priority = Foverlay_get ((OVERLAY), Qpriority); \
5598 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5599 entries[n].after_string_p = (AFTER_P); \
5600 ++n; \
5602 while (0)
5604 /* Process overlay before the overlay center. */
5605 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5607 XSETMISC (overlay, ov);
5608 eassert (OVERLAYP (overlay));
5609 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5610 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5612 if (end < charpos)
5613 break;
5615 /* Skip this overlay if it doesn't start or end at IT's current
5616 position. */
5617 if (end != charpos && start != charpos)
5618 continue;
5620 /* Skip this overlay if it doesn't apply to IT->w. */
5621 window = Foverlay_get (overlay, Qwindow);
5622 if (WINDOWP (window) && XWINDOW (window) != it->w)
5623 continue;
5625 /* If the text ``under'' the overlay is invisible, both before-
5626 and after-strings from this overlay are visible; start and
5627 end position are indistinguishable. */
5628 invisible = Foverlay_get (overlay, Qinvisible);
5629 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5631 /* If overlay has a non-empty before-string, record it. */
5632 if ((start == charpos || (end == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 0);
5637 /* If overlay has a non-empty after-string, record it. */
5638 if ((end == charpos || (start == charpos && invis_p))
5639 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5640 && SCHARS (str))
5641 RECORD_OVERLAY_STRING (overlay, str, 1);
5644 /* Process overlays after the overlay center. */
5645 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5647 XSETMISC (overlay, ov);
5648 eassert (OVERLAYP (overlay));
5649 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5650 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5652 if (start > charpos)
5653 break;
5655 /* Skip this overlay if it doesn't start or end at IT's current
5656 position. */
5657 if (end != charpos && start != charpos)
5658 continue;
5660 /* Skip this overlay if it doesn't apply to IT->w. */
5661 window = Foverlay_get (overlay, Qwindow);
5662 if (WINDOWP (window) && XWINDOW (window) != it->w)
5663 continue;
5665 /* If the text ``under'' the overlay is invisible, it has a zero
5666 dimension, and both before- and after-strings apply. */
5667 invisible = Foverlay_get (overlay, Qinvisible);
5668 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5670 /* If overlay has a non-empty before-string, record it. */
5671 if ((start == charpos || (end == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 0);
5676 /* If overlay has a non-empty after-string, record it. */
5677 if ((end == charpos || (start == charpos && invis_p))
5678 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5679 && SCHARS (str))
5680 RECORD_OVERLAY_STRING (overlay, str, 1);
5683 #undef RECORD_OVERLAY_STRING
5685 /* Sort entries. */
5686 if (n > 1)
5687 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5689 /* Record number of overlay strings, and where we computed it. */
5690 it->n_overlay_strings = n;
5691 it->overlay_strings_charpos = charpos;
5693 /* IT->current.overlay_string_index is the number of overlay strings
5694 that have already been consumed by IT. Copy some of the
5695 remaining overlay strings to IT->overlay_strings. */
5696 i = 0;
5697 j = it->current.overlay_string_index;
5698 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5700 it->overlay_strings[i] = entries[j].string;
5701 it->string_overlays[i++] = entries[j++].overlay;
5704 CHECK_IT (it);
5705 SAFE_FREE ();
5709 /* Get the first chunk of overlay strings at IT's current buffer
5710 position, or at CHARPOS if that is > 0. Value is non-zero if at
5711 least one overlay string was found. */
5713 static int
5714 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5716 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5717 process. This fills IT->overlay_strings with strings, and sets
5718 IT->n_overlay_strings to the total number of strings to process.
5719 IT->pos.overlay_string_index has to be set temporarily to zero
5720 because load_overlay_strings needs this; it must be set to -1
5721 when no overlay strings are found because a zero value would
5722 indicate a position in the first overlay string. */
5723 it->current.overlay_string_index = 0;
5724 load_overlay_strings (it, charpos);
5726 /* If we found overlay strings, set up IT to deliver display
5727 elements from the first one. Otherwise set up IT to deliver
5728 from current_buffer. */
5729 if (it->n_overlay_strings)
5731 /* Make sure we know settings in current_buffer, so that we can
5732 restore meaningful values when we're done with the overlay
5733 strings. */
5734 if (compute_stop_p)
5735 compute_stop_pos (it);
5736 eassert (it->face_id >= 0);
5738 /* Save IT's settings. They are restored after all overlay
5739 strings have been processed. */
5740 eassert (!compute_stop_p || it->sp == 0);
5742 /* When called from handle_stop, there might be an empty display
5743 string loaded. In that case, don't bother saving it. But
5744 don't use this optimization with the bidi iterator, since we
5745 need the corresponding pop_it call to resync the bidi
5746 iterator's position with IT's position, after we are done
5747 with the overlay strings. (The corresponding call to pop_it
5748 in case of an empty display string is in
5749 next_overlay_string.) */
5750 if (!(!it->bidi_p
5751 && STRINGP (it->string) && !SCHARS (it->string)))
5752 push_it (it, NULL);
5754 /* Set up IT to deliver display elements from the first overlay
5755 string. */
5756 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5757 it->string = it->overlay_strings[0];
5758 it->from_overlay = Qnil;
5759 it->stop_charpos = 0;
5760 eassert (STRINGP (it->string));
5761 it->end_charpos = SCHARS (it->string);
5762 it->prev_stop = 0;
5763 it->base_level_stop = 0;
5764 it->multibyte_p = STRING_MULTIBYTE (it->string);
5765 it->method = GET_FROM_STRING;
5766 it->from_disp_prop_p = 0;
5768 /* Force paragraph direction to be that of the parent
5769 buffer. */
5770 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5771 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5772 else
5773 it->paragraph_embedding = L2R;
5775 /* Set up the bidi iterator for this overlay string. */
5776 if (it->bidi_p)
5778 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5780 it->bidi_it.string.lstring = it->string;
5781 it->bidi_it.string.s = NULL;
5782 it->bidi_it.string.schars = SCHARS (it->string);
5783 it->bidi_it.string.bufpos = pos;
5784 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5785 it->bidi_it.string.unibyte = !it->multibyte_p;
5786 it->bidi_it.w = it->w;
5787 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5789 return 1;
5792 it->current.overlay_string_index = -1;
5793 return 0;
5796 static int
5797 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5799 it->string = Qnil;
5800 it->method = GET_FROM_BUFFER;
5802 (void) get_overlay_strings_1 (it, charpos, 1);
5804 CHECK_IT (it);
5806 /* Value is non-zero if we found at least one overlay string. */
5807 return STRINGP (it->string);
5812 /***********************************************************************
5813 Saving and restoring state
5814 ***********************************************************************/
5816 /* Save current settings of IT on IT->stack. Called, for example,
5817 before setting up IT for an overlay string, to be able to restore
5818 IT's settings to what they were after the overlay string has been
5819 processed. If POSITION is non-NULL, it is the position to save on
5820 the stack instead of IT->position. */
5822 static void
5823 push_it (struct it *it, struct text_pos *position)
5825 struct iterator_stack_entry *p;
5827 eassert (it->sp < IT_STACK_SIZE);
5828 p = it->stack + it->sp;
5830 p->stop_charpos = it->stop_charpos;
5831 p->prev_stop = it->prev_stop;
5832 p->base_level_stop = it->base_level_stop;
5833 p->cmp_it = it->cmp_it;
5834 eassert (it->face_id >= 0);
5835 p->face_id = it->face_id;
5836 p->string = it->string;
5837 p->method = it->method;
5838 p->from_overlay = it->from_overlay;
5839 switch (p->method)
5841 case GET_FROM_IMAGE:
5842 p->u.image.object = it->object;
5843 p->u.image.image_id = it->image_id;
5844 p->u.image.slice = it->slice;
5845 break;
5846 case GET_FROM_STRETCH:
5847 p->u.stretch.object = it->object;
5848 break;
5850 p->position = position ? *position : it->position;
5851 p->current = it->current;
5852 p->end_charpos = it->end_charpos;
5853 p->string_nchars = it->string_nchars;
5854 p->area = it->area;
5855 p->multibyte_p = it->multibyte_p;
5856 p->avoid_cursor_p = it->avoid_cursor_p;
5857 p->space_width = it->space_width;
5858 p->font_height = it->font_height;
5859 p->voffset = it->voffset;
5860 p->string_from_display_prop_p = it->string_from_display_prop_p;
5861 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5862 p->display_ellipsis_p = 0;
5863 p->line_wrap = it->line_wrap;
5864 p->bidi_p = it->bidi_p;
5865 p->paragraph_embedding = it->paragraph_embedding;
5866 p->from_disp_prop_p = it->from_disp_prop_p;
5867 ++it->sp;
5869 /* Save the state of the bidi iterator as well. */
5870 if (it->bidi_p)
5871 bidi_push_it (&it->bidi_it);
5874 static void
5875 iterate_out_of_display_property (struct it *it)
5877 int buffer_p = !STRINGP (it->string);
5878 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5879 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5881 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5883 /* Maybe initialize paragraph direction. If we are at the beginning
5884 of a new paragraph, next_element_from_buffer may not have a
5885 chance to do that. */
5886 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5887 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5888 /* prev_stop can be zero, so check against BEGV as well. */
5889 while (it->bidi_it.charpos >= bob
5890 && it->prev_stop <= it->bidi_it.charpos
5891 && it->bidi_it.charpos < CHARPOS (it->position)
5892 && it->bidi_it.charpos < eob)
5893 bidi_move_to_visually_next (&it->bidi_it);
5894 /* Record the stop_pos we just crossed, for when we cross it
5895 back, maybe. */
5896 if (it->bidi_it.charpos > CHARPOS (it->position))
5897 it->prev_stop = CHARPOS (it->position);
5898 /* If we ended up not where pop_it put us, resync IT's
5899 positional members with the bidi iterator. */
5900 if (it->bidi_it.charpos != CHARPOS (it->position))
5901 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5902 if (buffer_p)
5903 it->current.pos = it->position;
5904 else
5905 it->current.string_pos = it->position;
5908 /* Restore IT's settings from IT->stack. Called, for example, when no
5909 more overlay strings must be processed, and we return to delivering
5910 display elements from a buffer, or when the end of a string from a
5911 `display' property is reached and we return to delivering display
5912 elements from an overlay string, or from a buffer. */
5914 static void
5915 pop_it (struct it *it)
5917 struct iterator_stack_entry *p;
5918 int from_display_prop = it->from_disp_prop_p;
5920 eassert (it->sp > 0);
5921 --it->sp;
5922 p = it->stack + it->sp;
5923 it->stop_charpos = p->stop_charpos;
5924 it->prev_stop = p->prev_stop;
5925 it->base_level_stop = p->base_level_stop;
5926 it->cmp_it = p->cmp_it;
5927 it->face_id = p->face_id;
5928 it->current = p->current;
5929 it->position = p->position;
5930 it->string = p->string;
5931 it->from_overlay = p->from_overlay;
5932 if (NILP (it->string))
5933 SET_TEXT_POS (it->current.string_pos, -1, -1);
5934 it->method = p->method;
5935 switch (it->method)
5937 case GET_FROM_IMAGE:
5938 it->image_id = p->u.image.image_id;
5939 it->object = p->u.image.object;
5940 it->slice = p->u.image.slice;
5941 break;
5942 case GET_FROM_STRETCH:
5943 it->object = p->u.stretch.object;
5944 break;
5945 case GET_FROM_BUFFER:
5946 it->object = it->w->contents;
5947 break;
5948 case GET_FROM_STRING:
5949 it->object = it->string;
5950 break;
5951 case GET_FROM_DISPLAY_VECTOR:
5952 if (it->s)
5953 it->method = GET_FROM_C_STRING;
5954 else if (STRINGP (it->string))
5955 it->method = GET_FROM_STRING;
5956 else
5958 it->method = GET_FROM_BUFFER;
5959 it->object = it->w->contents;
5962 it->end_charpos = p->end_charpos;
5963 it->string_nchars = p->string_nchars;
5964 it->area = p->area;
5965 it->multibyte_p = p->multibyte_p;
5966 it->avoid_cursor_p = p->avoid_cursor_p;
5967 it->space_width = p->space_width;
5968 it->font_height = p->font_height;
5969 it->voffset = p->voffset;
5970 it->string_from_display_prop_p = p->string_from_display_prop_p;
5971 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5972 it->line_wrap = p->line_wrap;
5973 it->bidi_p = p->bidi_p;
5974 it->paragraph_embedding = p->paragraph_embedding;
5975 it->from_disp_prop_p = p->from_disp_prop_p;
5976 if (it->bidi_p)
5978 bidi_pop_it (&it->bidi_it);
5979 /* Bidi-iterate until we get out of the portion of text, if any,
5980 covered by a `display' text property or by an overlay with
5981 `display' property. (We cannot just jump there, because the
5982 internal coherency of the bidi iterator state can not be
5983 preserved across such jumps.) We also must determine the
5984 paragraph base direction if the overlay we just processed is
5985 at the beginning of a new paragraph. */
5986 if (from_display_prop
5987 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5988 iterate_out_of_display_property (it);
5990 eassert ((BUFFERP (it->object)
5991 && IT_CHARPOS (*it) == it->bidi_it.charpos
5992 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5993 || (STRINGP (it->object)
5994 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5995 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5996 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6002 /***********************************************************************
6003 Moving over lines
6004 ***********************************************************************/
6006 /* Set IT's current position to the previous line start. */
6008 static void
6009 back_to_previous_line_start (struct it *it)
6011 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6013 DEC_BOTH (cp, bp);
6014 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6018 /* Move IT to the next line start.
6020 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6021 we skipped over part of the text (as opposed to moving the iterator
6022 continuously over the text). Otherwise, don't change the value
6023 of *SKIPPED_P.
6025 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6026 iterator on the newline, if it was found.
6028 Newlines may come from buffer text, overlay strings, or strings
6029 displayed via the `display' property. That's the reason we can't
6030 simply use find_newline_no_quit.
6032 Note that this function may not skip over invisible text that is so
6033 because of text properties and immediately follows a newline. If
6034 it would, function reseat_at_next_visible_line_start, when called
6035 from set_iterator_to_next, would effectively make invisible
6036 characters following a newline part of the wrong glyph row, which
6037 leads to wrong cursor motion. */
6039 static int
6040 forward_to_next_line_start (struct it *it, int *skipped_p,
6041 struct bidi_it *bidi_it_prev)
6043 ptrdiff_t old_selective;
6044 int newline_found_p, n;
6045 const int MAX_NEWLINE_DISTANCE = 500;
6047 /* If already on a newline, just consume it to avoid unintended
6048 skipping over invisible text below. */
6049 if (it->what == IT_CHARACTER
6050 && it->c == '\n'
6051 && CHARPOS (it->position) == IT_CHARPOS (*it))
6053 if (it->bidi_p && bidi_it_prev)
6054 *bidi_it_prev = it->bidi_it;
6055 set_iterator_to_next (it, 0);
6056 it->c = 0;
6057 return 1;
6060 /* Don't handle selective display in the following. It's (a)
6061 unnecessary because it's done by the caller, and (b) leads to an
6062 infinite recursion because next_element_from_ellipsis indirectly
6063 calls this function. */
6064 old_selective = it->selective;
6065 it->selective = 0;
6067 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6068 from buffer text. */
6069 for (n = newline_found_p = 0;
6070 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6071 n += STRINGP (it->string) ? 0 : 1)
6073 if (!get_next_display_element (it))
6074 return 0;
6075 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6076 if (newline_found_p && it->bidi_p && bidi_it_prev)
6077 *bidi_it_prev = it->bidi_it;
6078 set_iterator_to_next (it, 0);
6081 /* If we didn't find a newline near enough, see if we can use a
6082 short-cut. */
6083 if (!newline_found_p)
6085 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6086 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6087 1, &bytepos);
6088 Lisp_Object pos;
6090 eassert (!STRINGP (it->string));
6092 /* If there isn't any `display' property in sight, and no
6093 overlays, we can just use the position of the newline in
6094 buffer text. */
6095 if (it->stop_charpos >= limit
6096 || ((pos = Fnext_single_property_change (make_number (start),
6097 Qdisplay, Qnil,
6098 make_number (limit)),
6099 NILP (pos))
6100 && next_overlay_change (start) == ZV))
6102 if (!it->bidi_p)
6104 IT_CHARPOS (*it) = limit;
6105 IT_BYTEPOS (*it) = bytepos;
6107 else
6109 struct bidi_it bprev;
6111 /* Help bidi.c avoid expensive searches for display
6112 properties and overlays, by telling it that there are
6113 none up to `limit'. */
6114 if (it->bidi_it.disp_pos < limit)
6116 it->bidi_it.disp_pos = limit;
6117 it->bidi_it.disp_prop = 0;
6119 do {
6120 bprev = it->bidi_it;
6121 bidi_move_to_visually_next (&it->bidi_it);
6122 } while (it->bidi_it.charpos != limit);
6123 IT_CHARPOS (*it) = limit;
6124 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6125 if (bidi_it_prev)
6126 *bidi_it_prev = bprev;
6128 *skipped_p = newline_found_p = true;
6130 else
6132 while (get_next_display_element (it)
6133 && !newline_found_p)
6135 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6136 if (newline_found_p && it->bidi_p && bidi_it_prev)
6137 *bidi_it_prev = it->bidi_it;
6138 set_iterator_to_next (it, 0);
6143 it->selective = old_selective;
6144 return newline_found_p;
6148 /* Set IT's current position to the previous visible line start. Skip
6149 invisible text that is so either due to text properties or due to
6150 selective display. Caution: this does not change IT->current_x and
6151 IT->hpos. */
6153 static void
6154 back_to_previous_visible_line_start (struct it *it)
6156 while (IT_CHARPOS (*it) > BEGV)
6158 back_to_previous_line_start (it);
6160 if (IT_CHARPOS (*it) <= BEGV)
6161 break;
6163 /* If selective > 0, then lines indented more than its value are
6164 invisible. */
6165 if (it->selective > 0
6166 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6167 it->selective))
6168 continue;
6170 /* Check the newline before point for invisibility. */
6172 Lisp_Object prop;
6173 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6174 Qinvisible, it->window);
6175 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6176 continue;
6179 if (IT_CHARPOS (*it) <= BEGV)
6180 break;
6183 struct it it2;
6184 void *it2data = NULL;
6185 ptrdiff_t pos;
6186 ptrdiff_t beg, end;
6187 Lisp_Object val, overlay;
6189 SAVE_IT (it2, *it, it2data);
6191 /* If newline is part of a composition, continue from start of composition */
6192 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6193 && beg < IT_CHARPOS (*it))
6194 goto replaced;
6196 /* If newline is replaced by a display property, find start of overlay
6197 or interval and continue search from that point. */
6198 pos = --IT_CHARPOS (it2);
6199 --IT_BYTEPOS (it2);
6200 it2.sp = 0;
6201 bidi_unshelve_cache (NULL, 0);
6202 it2.string_from_display_prop_p = 0;
6203 it2.from_disp_prop_p = 0;
6204 if (handle_display_prop (&it2) == HANDLED_RETURN
6205 && !NILP (val = get_char_property_and_overlay
6206 (make_number (pos), Qdisplay, Qnil, &overlay))
6207 && (OVERLAYP (overlay)
6208 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6209 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6211 RESTORE_IT (it, it, it2data);
6212 goto replaced;
6215 /* Newline is not replaced by anything -- so we are done. */
6216 RESTORE_IT (it, it, it2data);
6217 break;
6219 replaced:
6220 if (beg < BEGV)
6221 beg = BEGV;
6222 IT_CHARPOS (*it) = beg;
6223 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6227 it->continuation_lines_width = 0;
6229 eassert (IT_CHARPOS (*it) >= BEGV);
6230 eassert (IT_CHARPOS (*it) == BEGV
6231 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6232 CHECK_IT (it);
6236 /* Reseat iterator IT at the previous visible line start. Skip
6237 invisible text that is so either due to text properties or due to
6238 selective display. At the end, update IT's overlay information,
6239 face information etc. */
6241 void
6242 reseat_at_previous_visible_line_start (struct it *it)
6244 back_to_previous_visible_line_start (it);
6245 reseat (it, it->current.pos, 1);
6246 CHECK_IT (it);
6250 /* Reseat iterator IT on the next visible line start in the current
6251 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6252 preceding the line start. Skip over invisible text that is so
6253 because of selective display. Compute faces, overlays etc at the
6254 new position. Note that this function does not skip over text that
6255 is invisible because of text properties. */
6257 static void
6258 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6260 int newline_found_p, skipped_p = 0;
6261 struct bidi_it bidi_it_prev;
6263 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6265 /* Skip over lines that are invisible because they are indented
6266 more than the value of IT->selective. */
6267 if (it->selective > 0)
6268 while (IT_CHARPOS (*it) < ZV
6269 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6270 it->selective))
6272 eassert (IT_BYTEPOS (*it) == BEGV
6273 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6274 newline_found_p =
6275 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6278 /* Position on the newline if that's what's requested. */
6279 if (on_newline_p && newline_found_p)
6281 if (STRINGP (it->string))
6283 if (IT_STRING_CHARPOS (*it) > 0)
6285 if (!it->bidi_p)
6287 --IT_STRING_CHARPOS (*it);
6288 --IT_STRING_BYTEPOS (*it);
6290 else
6292 /* We need to restore the bidi iterator to the state
6293 it had on the newline, and resync the IT's
6294 position with that. */
6295 it->bidi_it = bidi_it_prev;
6296 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6297 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6301 else if (IT_CHARPOS (*it) > BEGV)
6303 if (!it->bidi_p)
6305 --IT_CHARPOS (*it);
6306 --IT_BYTEPOS (*it);
6308 else
6310 /* We need to restore the bidi iterator to the state it
6311 had on the newline and resync IT with that. */
6312 it->bidi_it = bidi_it_prev;
6313 IT_CHARPOS (*it) = it->bidi_it.charpos;
6314 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6316 reseat (it, it->current.pos, 0);
6319 else if (skipped_p)
6320 reseat (it, it->current.pos, 0);
6322 CHECK_IT (it);
6327 /***********************************************************************
6328 Changing an iterator's position
6329 ***********************************************************************/
6331 /* Change IT's current position to POS in current_buffer. If FORCE_P
6332 is non-zero, always check for text properties at the new position.
6333 Otherwise, text properties are only looked up if POS >=
6334 IT->check_charpos of a property. */
6336 static void
6337 reseat (struct it *it, struct text_pos pos, int force_p)
6339 ptrdiff_t original_pos = IT_CHARPOS (*it);
6341 reseat_1 (it, pos, 0);
6343 /* Determine where to check text properties. Avoid doing it
6344 where possible because text property lookup is very expensive. */
6345 if (force_p
6346 || CHARPOS (pos) > it->stop_charpos
6347 || CHARPOS (pos) < original_pos)
6349 if (it->bidi_p)
6351 /* For bidi iteration, we need to prime prev_stop and
6352 base_level_stop with our best estimations. */
6353 /* Implementation note: Of course, POS is not necessarily a
6354 stop position, so assigning prev_pos to it is a lie; we
6355 should have called compute_stop_backwards. However, if
6356 the current buffer does not include any R2L characters,
6357 that call would be a waste of cycles, because the
6358 iterator will never move back, and thus never cross this
6359 "fake" stop position. So we delay that backward search
6360 until the time we really need it, in next_element_from_buffer. */
6361 if (CHARPOS (pos) != it->prev_stop)
6362 it->prev_stop = CHARPOS (pos);
6363 if (CHARPOS (pos) < it->base_level_stop)
6364 it->base_level_stop = 0; /* meaning it's unknown */
6365 handle_stop (it);
6367 else
6369 handle_stop (it);
6370 it->prev_stop = it->base_level_stop = 0;
6375 CHECK_IT (it);
6379 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6380 IT->stop_pos to POS, also. */
6382 static void
6383 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6385 /* Don't call this function when scanning a C string. */
6386 eassert (it->s == NULL);
6388 /* POS must be a reasonable value. */
6389 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6391 it->current.pos = it->position = pos;
6392 it->end_charpos = ZV;
6393 it->dpvec = NULL;
6394 it->current.dpvec_index = -1;
6395 it->current.overlay_string_index = -1;
6396 IT_STRING_CHARPOS (*it) = -1;
6397 IT_STRING_BYTEPOS (*it) = -1;
6398 it->string = Qnil;
6399 it->method = GET_FROM_BUFFER;
6400 it->object = it->w->contents;
6401 it->area = TEXT_AREA;
6402 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6403 it->sp = 0;
6404 it->string_from_display_prop_p = 0;
6405 it->string_from_prefix_prop_p = 0;
6407 it->from_disp_prop_p = 0;
6408 it->face_before_selective_p = 0;
6409 if (it->bidi_p)
6411 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6412 &it->bidi_it);
6413 bidi_unshelve_cache (NULL, 0);
6414 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6415 it->bidi_it.string.s = NULL;
6416 it->bidi_it.string.lstring = Qnil;
6417 it->bidi_it.string.bufpos = 0;
6418 it->bidi_it.string.from_disp_str = 0;
6419 it->bidi_it.string.unibyte = 0;
6420 it->bidi_it.w = it->w;
6423 if (set_stop_p)
6425 it->stop_charpos = CHARPOS (pos);
6426 it->base_level_stop = CHARPOS (pos);
6428 /* This make the information stored in it->cmp_it invalidate. */
6429 it->cmp_it.id = -1;
6433 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6434 If S is non-null, it is a C string to iterate over. Otherwise,
6435 STRING gives a Lisp string to iterate over.
6437 If PRECISION > 0, don't return more then PRECISION number of
6438 characters from the string.
6440 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6441 characters have been returned. FIELD_WIDTH < 0 means an infinite
6442 field width.
6444 MULTIBYTE = 0 means disable processing of multibyte characters,
6445 MULTIBYTE > 0 means enable it,
6446 MULTIBYTE < 0 means use IT->multibyte_p.
6448 IT must be initialized via a prior call to init_iterator before
6449 calling this function. */
6451 static void
6452 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6453 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6454 int multibyte)
6456 /* No text property checks performed by default, but see below. */
6457 it->stop_charpos = -1;
6459 /* Set iterator position and end position. */
6460 memset (&it->current, 0, sizeof it->current);
6461 it->current.overlay_string_index = -1;
6462 it->current.dpvec_index = -1;
6463 eassert (charpos >= 0);
6465 /* If STRING is specified, use its multibyteness, otherwise use the
6466 setting of MULTIBYTE, if specified. */
6467 if (multibyte >= 0)
6468 it->multibyte_p = multibyte > 0;
6470 /* Bidirectional reordering of strings is controlled by the default
6471 value of bidi-display-reordering. Don't try to reorder while
6472 loading loadup.el, as the necessary character property tables are
6473 not yet available. */
6474 it->bidi_p =
6475 NILP (Vpurify_flag)
6476 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6478 if (s == NULL)
6480 eassert (STRINGP (string));
6481 it->string = string;
6482 it->s = NULL;
6483 it->end_charpos = it->string_nchars = SCHARS (string);
6484 it->method = GET_FROM_STRING;
6485 it->current.string_pos = string_pos (charpos, string);
6487 if (it->bidi_p)
6489 it->bidi_it.string.lstring = string;
6490 it->bidi_it.string.s = NULL;
6491 it->bidi_it.string.schars = it->end_charpos;
6492 it->bidi_it.string.bufpos = 0;
6493 it->bidi_it.string.from_disp_str = 0;
6494 it->bidi_it.string.unibyte = !it->multibyte_p;
6495 it->bidi_it.w = it->w;
6496 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6497 FRAME_WINDOW_P (it->f), &it->bidi_it);
6500 else
6502 it->s = (const unsigned char *) s;
6503 it->string = Qnil;
6505 /* Note that we use IT->current.pos, not it->current.string_pos,
6506 for displaying C strings. */
6507 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6508 if (it->multibyte_p)
6510 it->current.pos = c_string_pos (charpos, s, 1);
6511 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6513 else
6515 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6516 it->end_charpos = it->string_nchars = strlen (s);
6519 if (it->bidi_p)
6521 it->bidi_it.string.lstring = Qnil;
6522 it->bidi_it.string.s = (const unsigned char *) s;
6523 it->bidi_it.string.schars = it->end_charpos;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = !it->multibyte_p;
6527 it->bidi_it.w = it->w;
6528 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6529 &it->bidi_it);
6531 it->method = GET_FROM_C_STRING;
6534 /* PRECISION > 0 means don't return more than PRECISION characters
6535 from the string. */
6536 if (precision > 0 && it->end_charpos - charpos > precision)
6538 it->end_charpos = it->string_nchars = charpos + precision;
6539 if (it->bidi_p)
6540 it->bidi_it.string.schars = it->end_charpos;
6543 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6544 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6545 FIELD_WIDTH < 0 means infinite field width. This is useful for
6546 padding with `-' at the end of a mode line. */
6547 if (field_width < 0)
6548 field_width = INFINITY;
6549 /* Implementation note: We deliberately don't enlarge
6550 it->bidi_it.string.schars here to fit it->end_charpos, because
6551 the bidi iterator cannot produce characters out of thin air. */
6552 if (field_width > it->end_charpos - charpos)
6553 it->end_charpos = charpos + field_width;
6555 /* Use the standard display table for displaying strings. */
6556 if (DISP_TABLE_P (Vstandard_display_table))
6557 it->dp = XCHAR_TABLE (Vstandard_display_table);
6559 it->stop_charpos = charpos;
6560 it->prev_stop = charpos;
6561 it->base_level_stop = 0;
6562 if (it->bidi_p)
6564 it->bidi_it.first_elt = 1;
6565 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6566 it->bidi_it.disp_pos = -1;
6568 if (s == NULL && it->multibyte_p)
6570 ptrdiff_t endpos = SCHARS (it->string);
6571 if (endpos > it->end_charpos)
6572 endpos = it->end_charpos;
6573 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6574 it->string);
6576 CHECK_IT (it);
6581 /***********************************************************************
6582 Iteration
6583 ***********************************************************************/
6585 /* Map enum it_method value to corresponding next_element_from_* function. */
6587 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6589 next_element_from_buffer,
6590 next_element_from_display_vector,
6591 next_element_from_string,
6592 next_element_from_c_string,
6593 next_element_from_image,
6594 next_element_from_stretch
6597 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6600 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6601 (possibly with the following characters). */
6603 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6604 ((IT)->cmp_it.id >= 0 \
6605 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6606 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6607 END_CHARPOS, (IT)->w, \
6608 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6609 (IT)->string)))
6612 /* Lookup the char-table Vglyphless_char_display for character C (-1
6613 if we want information for no-font case), and return the display
6614 method symbol. By side-effect, update it->what and
6615 it->glyphless_method. This function is called from
6616 get_next_display_element for each character element, and from
6617 x_produce_glyphs when no suitable font was found. */
6619 Lisp_Object
6620 lookup_glyphless_char_display (int c, struct it *it)
6622 Lisp_Object glyphless_method = Qnil;
6624 if (CHAR_TABLE_P (Vglyphless_char_display)
6625 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6627 if (c >= 0)
6629 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6630 if (CONSP (glyphless_method))
6631 glyphless_method = FRAME_WINDOW_P (it->f)
6632 ? XCAR (glyphless_method)
6633 : XCDR (glyphless_method);
6635 else
6636 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6639 retry:
6640 if (NILP (glyphless_method))
6642 if (c >= 0)
6643 /* The default is to display the character by a proper font. */
6644 return Qnil;
6645 /* The default for the no-font case is to display an empty box. */
6646 glyphless_method = Qempty_box;
6648 if (EQ (glyphless_method, Qzero_width))
6650 if (c >= 0)
6651 return glyphless_method;
6652 /* This method can't be used for the no-font case. */
6653 glyphless_method = Qempty_box;
6655 if (EQ (glyphless_method, Qthin_space))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6657 else if (EQ (glyphless_method, Qempty_box))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6659 else if (EQ (glyphless_method, Qhex_code))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6661 else if (STRINGP (glyphless_method))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6663 else
6665 /* Invalid value. We use the default method. */
6666 glyphless_method = Qnil;
6667 goto retry;
6669 it->what = IT_GLYPHLESS;
6670 return glyphless_method;
6673 /* Merge escape glyph face and cache the result. */
6675 static struct frame *last_escape_glyph_frame = NULL;
6676 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6677 static int last_escape_glyph_merged_face_id = 0;
6679 static int
6680 merge_escape_glyph_face (struct it *it)
6682 int face_id;
6684 if (it->f == last_escape_glyph_frame
6685 && it->face_id == last_escape_glyph_face_id)
6686 face_id = last_escape_glyph_merged_face_id;
6687 else
6689 /* Merge the `escape-glyph' face into the current face. */
6690 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6691 last_escape_glyph_frame = it->f;
6692 last_escape_glyph_face_id = it->face_id;
6693 last_escape_glyph_merged_face_id = face_id;
6695 return face_id;
6698 /* Likewise for glyphless glyph face. */
6700 static struct frame *last_glyphless_glyph_frame = NULL;
6701 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6702 static int last_glyphless_glyph_merged_face_id = 0;
6705 merge_glyphless_glyph_face (struct it *it)
6707 int face_id;
6709 if (it->f == last_glyphless_glyph_frame
6710 && it->face_id == last_glyphless_glyph_face_id)
6711 face_id = last_glyphless_glyph_merged_face_id;
6712 else
6714 /* Merge the `glyphless-char' face into the current face. */
6715 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6716 last_glyphless_glyph_frame = it->f;
6717 last_glyphless_glyph_face_id = it->face_id;
6718 last_glyphless_glyph_merged_face_id = face_id;
6720 return face_id;
6723 /* Load IT's display element fields with information about the next
6724 display element from the current position of IT. Value is zero if
6725 end of buffer (or C string) is reached. */
6727 static int
6728 get_next_display_element (struct it *it)
6730 /* Non-zero means that we found a display element. Zero means that
6731 we hit the end of what we iterate over. Performance note: the
6732 function pointer `method' used here turns out to be faster than
6733 using a sequence of if-statements. */
6734 int success_p;
6736 get_next:
6737 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6739 if (it->what == IT_CHARACTER)
6741 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6742 and only if (a) the resolved directionality of that character
6743 is R..." */
6744 /* FIXME: Do we need an exception for characters from display
6745 tables? */
6746 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6747 it->c = bidi_mirror_char (it->c);
6748 /* Map via display table or translate control characters.
6749 IT->c, IT->len etc. have been set to the next character by
6750 the function call above. If we have a display table, and it
6751 contains an entry for IT->c, translate it. Don't do this if
6752 IT->c itself comes from a display table, otherwise we could
6753 end up in an infinite recursion. (An alternative could be to
6754 count the recursion depth of this function and signal an
6755 error when a certain maximum depth is reached.) Is it worth
6756 it? */
6757 if (success_p && it->dpvec == NULL)
6759 Lisp_Object dv;
6760 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6761 int nonascii_space_p = 0;
6762 int nonascii_hyphen_p = 0;
6763 int c = it->c; /* This is the character to display. */
6765 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6767 eassert (SINGLE_BYTE_CHAR_P (c));
6768 if (unibyte_display_via_language_environment)
6770 c = DECODE_CHAR (unibyte, c);
6771 if (c < 0)
6772 c = BYTE8_TO_CHAR (it->c);
6774 else
6775 c = BYTE8_TO_CHAR (it->c);
6778 if (it->dp
6779 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6780 VECTORP (dv)))
6782 struct Lisp_Vector *v = XVECTOR (dv);
6784 /* Return the first character from the display table
6785 entry, if not empty. If empty, don't display the
6786 current character. */
6787 if (v->header.size)
6789 it->dpvec_char_len = it->len;
6790 it->dpvec = v->contents;
6791 it->dpend = v->contents + v->header.size;
6792 it->current.dpvec_index = 0;
6793 it->dpvec_face_id = -1;
6794 it->saved_face_id = it->face_id;
6795 it->method = GET_FROM_DISPLAY_VECTOR;
6796 it->ellipsis_p = 0;
6798 else
6800 set_iterator_to_next (it, 0);
6802 goto get_next;
6805 if (! NILP (lookup_glyphless_char_display (c, it)))
6807 if (it->what == IT_GLYPHLESS)
6808 goto done;
6809 /* Don't display this character. */
6810 set_iterator_to_next (it, 0);
6811 goto get_next;
6814 /* If `nobreak-char-display' is non-nil, we display
6815 non-ASCII spaces and hyphens specially. */
6816 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6818 if (c == 0xA0)
6819 nonascii_space_p = true;
6820 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6821 nonascii_hyphen_p = true;
6824 /* Translate control characters into `\003' or `^C' form.
6825 Control characters coming from a display table entry are
6826 currently not translated because we use IT->dpvec to hold
6827 the translation. This could easily be changed but I
6828 don't believe that it is worth doing.
6830 The characters handled by `nobreak-char-display' must be
6831 translated too.
6833 Non-printable characters and raw-byte characters are also
6834 translated to octal form. */
6835 if (((c < ' ' || c == 127) /* ASCII control chars. */
6836 ? (it->area != TEXT_AREA
6837 /* In mode line, treat \n, \t like other crl chars. */
6838 || (c != '\t'
6839 && it->glyph_row
6840 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6841 || (c != '\n' && c != '\t'))
6842 : (nonascii_space_p
6843 || nonascii_hyphen_p
6844 || CHAR_BYTE8_P (c)
6845 || ! CHAR_PRINTABLE_P (c))))
6847 /* C is a control character, non-ASCII space/hyphen,
6848 raw-byte, or a non-printable character which must be
6849 displayed either as '\003' or as `^C' where the '\\'
6850 and '^' can be defined in the display table. Fill
6851 IT->ctl_chars with glyphs for what we have to
6852 display. Then, set IT->dpvec to these glyphs. */
6853 Lisp_Object gc;
6854 int ctl_len;
6855 int face_id;
6856 int lface_id = 0;
6857 int escape_glyph;
6859 /* Handle control characters with ^. */
6861 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6863 int g;
6865 g = '^'; /* default glyph for Control */
6866 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6867 if (it->dp
6868 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6870 g = GLYPH_CODE_CHAR (gc);
6871 lface_id = GLYPH_CODE_FACE (gc);
6874 face_id = (lface_id
6875 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6876 : merge_escape_glyph_face (it));
6878 XSETINT (it->ctl_chars[0], g);
6879 XSETINT (it->ctl_chars[1], c ^ 0100);
6880 ctl_len = 2;
6881 goto display_control;
6884 /* Handle non-ascii space in the mode where it only gets
6885 highlighting. */
6887 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6889 /* Merge `nobreak-space' into the current face. */
6890 face_id = merge_faces (it->f, Qnobreak_space, 0,
6891 it->face_id);
6892 XSETINT (it->ctl_chars[0], ' ');
6893 ctl_len = 1;
6894 goto display_control;
6897 /* Handle sequences that start with the "escape glyph". */
6899 /* the default escape glyph is \. */
6900 escape_glyph = '\\';
6902 if (it->dp
6903 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6905 escape_glyph = GLYPH_CODE_CHAR (gc);
6906 lface_id = GLYPH_CODE_FACE (gc);
6909 face_id = (lface_id
6910 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6911 : merge_escape_glyph_face (it));
6913 /* Draw non-ASCII hyphen with just highlighting: */
6915 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6917 XSETINT (it->ctl_chars[0], '-');
6918 ctl_len = 1;
6919 goto display_control;
6922 /* Draw non-ASCII space/hyphen with escape glyph: */
6924 if (nonascii_space_p || nonascii_hyphen_p)
6926 XSETINT (it->ctl_chars[0], escape_glyph);
6927 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6928 ctl_len = 2;
6929 goto display_control;
6933 char str[10];
6934 int len, i;
6936 if (CHAR_BYTE8_P (c))
6937 /* Display \200 instead of \17777600. */
6938 c = CHAR_TO_BYTE8 (c);
6939 len = sprintf (str, "%03o", c);
6941 XSETINT (it->ctl_chars[0], escape_glyph);
6942 for (i = 0; i < len; i++)
6943 XSETINT (it->ctl_chars[i + 1], str[i]);
6944 ctl_len = len + 1;
6947 display_control:
6948 /* Set up IT->dpvec and return first character from it. */
6949 it->dpvec_char_len = it->len;
6950 it->dpvec = it->ctl_chars;
6951 it->dpend = it->dpvec + ctl_len;
6952 it->current.dpvec_index = 0;
6953 it->dpvec_face_id = face_id;
6954 it->saved_face_id = it->face_id;
6955 it->method = GET_FROM_DISPLAY_VECTOR;
6956 it->ellipsis_p = 0;
6957 goto get_next;
6959 it->char_to_display = c;
6961 else if (success_p)
6963 it->char_to_display = it->c;
6967 #ifdef HAVE_WINDOW_SYSTEM
6968 /* Adjust face id for a multibyte character. There are no multibyte
6969 character in unibyte text. */
6970 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6971 && it->multibyte_p
6972 && success_p
6973 && FRAME_WINDOW_P (it->f))
6975 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6977 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6979 /* Automatic composition with glyph-string. */
6980 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6982 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6984 else
6986 ptrdiff_t pos = (it->s ? -1
6987 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6988 : IT_CHARPOS (*it));
6989 int c;
6991 if (it->what == IT_CHARACTER)
6992 c = it->char_to_display;
6993 else
6995 struct composition *cmp = composition_table[it->cmp_it.id];
6996 int i;
6998 c = ' ';
6999 for (i = 0; i < cmp->glyph_len; i++)
7000 /* TAB in a composition means display glyphs with
7001 padding space on the left or right. */
7002 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7003 break;
7005 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7008 #endif /* HAVE_WINDOW_SYSTEM */
7010 done:
7011 /* Is this character the last one of a run of characters with
7012 box? If yes, set IT->end_of_box_run_p to 1. */
7013 if (it->face_box_p
7014 && it->s == NULL)
7016 if (it->method == GET_FROM_STRING && it->sp)
7018 int face_id = underlying_face_id (it);
7019 struct face *face = FACE_FROM_ID (it->f, face_id);
7021 if (face)
7023 if (face->box == FACE_NO_BOX)
7025 /* If the box comes from face properties in a
7026 display string, check faces in that string. */
7027 int string_face_id = face_after_it_pos (it);
7028 it->end_of_box_run_p
7029 = (FACE_FROM_ID (it->f, string_face_id)->box
7030 == FACE_NO_BOX);
7032 /* Otherwise, the box comes from the underlying face.
7033 If this is the last string character displayed, check
7034 the next buffer location. */
7035 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7036 && (it->current.overlay_string_index
7037 == it->n_overlay_strings - 1))
7039 ptrdiff_t ignore;
7040 int next_face_id;
7041 struct text_pos pos = it->current.pos;
7042 INC_TEXT_POS (pos, it->multibyte_p);
7044 next_face_id = face_at_buffer_position
7045 (it->w, CHARPOS (pos), &ignore,
7046 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7047 -1);
7048 it->end_of_box_run_p
7049 = (FACE_FROM_ID (it->f, next_face_id)->box
7050 == FACE_NO_BOX);
7054 /* next_element_from_display_vector sets this flag according to
7055 faces of the display vector glyphs, see there. */
7056 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7058 int face_id = face_after_it_pos (it);
7059 it->end_of_box_run_p
7060 = (face_id != it->face_id
7061 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7064 /* If we reached the end of the object we've been iterating (e.g., a
7065 display string or an overlay string), and there's something on
7066 IT->stack, proceed with what's on the stack. It doesn't make
7067 sense to return zero if there's unprocessed stuff on the stack,
7068 because otherwise that stuff will never be displayed. */
7069 if (!success_p && it->sp > 0)
7071 set_iterator_to_next (it, 0);
7072 success_p = get_next_display_element (it);
7075 /* Value is 0 if end of buffer or string reached. */
7076 return success_p;
7080 /* Move IT to the next display element.
7082 RESEAT_P non-zero means if called on a newline in buffer text,
7083 skip to the next visible line start.
7085 Functions get_next_display_element and set_iterator_to_next are
7086 separate because I find this arrangement easier to handle than a
7087 get_next_display_element function that also increments IT's
7088 position. The way it is we can first look at an iterator's current
7089 display element, decide whether it fits on a line, and if it does,
7090 increment the iterator position. The other way around we probably
7091 would either need a flag indicating whether the iterator has to be
7092 incremented the next time, or we would have to implement a
7093 decrement position function which would not be easy to write. */
7095 void
7096 set_iterator_to_next (struct it *it, int reseat_p)
7098 /* Reset flags indicating start and end of a sequence of characters
7099 with box. Reset them at the start of this function because
7100 moving the iterator to a new position might set them. */
7101 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7103 switch (it->method)
7105 case GET_FROM_BUFFER:
7106 /* The current display element of IT is a character from
7107 current_buffer. Advance in the buffer, and maybe skip over
7108 invisible lines that are so because of selective display. */
7109 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7110 reseat_at_next_visible_line_start (it, 0);
7111 else if (it->cmp_it.id >= 0)
7113 /* We are currently getting glyphs from a composition. */
7114 int i;
7116 if (! it->bidi_p)
7118 IT_CHARPOS (*it) += it->cmp_it.nchars;
7119 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7120 if (it->cmp_it.to < it->cmp_it.nglyphs)
7122 it->cmp_it.from = it->cmp_it.to;
7124 else
7126 it->cmp_it.id = -1;
7127 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7128 IT_BYTEPOS (*it),
7129 it->end_charpos, Qnil);
7132 else if (! it->cmp_it.reversed_p)
7134 /* Composition created while scanning forward. */
7135 /* Update IT's char/byte positions to point to the first
7136 character of the next grapheme cluster, or to the
7137 character visually after the current composition. */
7138 for (i = 0; i < it->cmp_it.nchars; i++)
7139 bidi_move_to_visually_next (&it->bidi_it);
7140 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7141 IT_CHARPOS (*it) = it->bidi_it.charpos;
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7145 /* Proceed to the next grapheme cluster. */
7146 it->cmp_it.from = it->cmp_it.to;
7148 else
7150 /* No more grapheme clusters in this composition.
7151 Find the next stop position. */
7152 ptrdiff_t stop = it->end_charpos;
7153 if (it->bidi_it.scan_dir < 0)
7154 /* Now we are scanning backward and don't know
7155 where to stop. */
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7158 IT_BYTEPOS (*it), stop, Qnil);
7161 else
7163 /* Composition created while scanning backward. */
7164 /* Update IT's char/byte positions to point to the last
7165 character of the previous grapheme cluster, or the
7166 character visually after the current composition. */
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7173 /* Proceed to the previous grapheme cluster. */
7174 it->cmp_it.to = it->cmp_it.from;
7176 else
7178 /* No more grapheme clusters in this composition.
7179 Find the next stop position. */
7180 ptrdiff_t stop = it->end_charpos;
7181 if (it->bidi_it.scan_dir < 0)
7182 /* Now we are scanning backward and don't know
7183 where to stop. */
7184 stop = -1;
7185 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7186 IT_BYTEPOS (*it), stop, Qnil);
7190 else
7192 eassert (it->len != 0);
7194 if (!it->bidi_p)
7196 IT_BYTEPOS (*it) += it->len;
7197 IT_CHARPOS (*it) += 1;
7199 else
7201 int prev_scan_dir = it->bidi_it.scan_dir;
7202 /* If this is a new paragraph, determine its base
7203 direction (a.k.a. its base embedding level). */
7204 if (it->bidi_it.new_paragraph)
7205 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7206 bidi_move_to_visually_next (&it->bidi_it);
7207 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7208 IT_CHARPOS (*it) = it->bidi_it.charpos;
7209 if (prev_scan_dir != it->bidi_it.scan_dir)
7211 /* As the scan direction was changed, we must
7212 re-compute the stop position for composition. */
7213 ptrdiff_t stop = it->end_charpos;
7214 if (it->bidi_it.scan_dir < 0)
7215 stop = -1;
7216 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7217 IT_BYTEPOS (*it), stop, Qnil);
7220 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7222 break;
7224 case GET_FROM_C_STRING:
7225 /* Current display element of IT is from a C string. */
7226 if (!it->bidi_p
7227 /* If the string position is beyond string's end, it means
7228 next_element_from_c_string is padding the string with
7229 blanks, in which case we bypass the bidi iterator,
7230 because it cannot deal with such virtual characters. */
7231 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7233 IT_BYTEPOS (*it) += it->len;
7234 IT_CHARPOS (*it) += 1;
7236 else
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_CHARPOS (*it) = it->bidi_it.charpos;
7242 break;
7244 case GET_FROM_DISPLAY_VECTOR:
7245 /* Current display element of IT is from a display table entry.
7246 Advance in the display table definition. Reset it to null if
7247 end reached, and continue with characters from buffers/
7248 strings. */
7249 ++it->current.dpvec_index;
7251 /* Restore face of the iterator to what they were before the
7252 display vector entry (these entries may contain faces). */
7253 it->face_id = it->saved_face_id;
7255 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7257 int recheck_faces = it->ellipsis_p;
7259 if (it->s)
7260 it->method = GET_FROM_C_STRING;
7261 else if (STRINGP (it->string))
7262 it->method = GET_FROM_STRING;
7263 else
7265 it->method = GET_FROM_BUFFER;
7266 it->object = it->w->contents;
7269 it->dpvec = NULL;
7270 it->current.dpvec_index = -1;
7272 /* Skip over characters which were displayed via IT->dpvec. */
7273 if (it->dpvec_char_len < 0)
7274 reseat_at_next_visible_line_start (it, 1);
7275 else if (it->dpvec_char_len > 0)
7277 if (it->method == GET_FROM_STRING
7278 && it->current.overlay_string_index >= 0
7279 && it->n_overlay_strings > 0)
7280 it->ignore_overlay_strings_at_pos_p = true;
7281 it->len = it->dpvec_char_len;
7282 set_iterator_to_next (it, reseat_p);
7285 /* Maybe recheck faces after display vector. */
7286 if (recheck_faces)
7287 it->stop_charpos = IT_CHARPOS (*it);
7289 break;
7291 case GET_FROM_STRING:
7292 /* Current display element is a character from a Lisp string. */
7293 eassert (it->s == NULL && STRINGP (it->string));
7294 /* Don't advance past string end. These conditions are true
7295 when set_iterator_to_next is called at the end of
7296 get_next_display_element, in which case the Lisp string is
7297 already exhausted, and all we want is pop the iterator
7298 stack. */
7299 if (it->current.overlay_string_index >= 0)
7301 /* This is an overlay string, so there's no padding with
7302 spaces, and the number of characters in the string is
7303 where the string ends. */
7304 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7305 goto consider_string_end;
7307 else
7309 /* Not an overlay string. There could be padding, so test
7310 against it->end_charpos. */
7311 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7312 goto consider_string_end;
7314 if (it->cmp_it.id >= 0)
7316 int i;
7318 if (! it->bidi_p)
7320 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7321 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7322 if (it->cmp_it.to < it->cmp_it.nglyphs)
7323 it->cmp_it.from = it->cmp_it.to;
7324 else
7326 it->cmp_it.id = -1;
7327 composition_compute_stop_pos (&it->cmp_it,
7328 IT_STRING_CHARPOS (*it),
7329 IT_STRING_BYTEPOS (*it),
7330 it->end_charpos, it->string);
7333 else if (! it->cmp_it.reversed_p)
7335 for (i = 0; i < it->cmp_it.nchars; i++)
7336 bidi_move_to_visually_next (&it->bidi_it);
7337 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7338 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7340 if (it->cmp_it.to < it->cmp_it.nglyphs)
7341 it->cmp_it.from = it->cmp_it.to;
7342 else
7344 ptrdiff_t stop = it->end_charpos;
7345 if (it->bidi_it.scan_dir < 0)
7346 stop = -1;
7347 composition_compute_stop_pos (&it->cmp_it,
7348 IT_STRING_CHARPOS (*it),
7349 IT_STRING_BYTEPOS (*it), stop,
7350 it->string);
7353 else
7355 for (i = 0; i < it->cmp_it.nchars; i++)
7356 bidi_move_to_visually_next (&it->bidi_it);
7357 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7358 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7359 if (it->cmp_it.from > 0)
7360 it->cmp_it.to = it->cmp_it.from;
7361 else
7363 ptrdiff_t stop = it->end_charpos;
7364 if (it->bidi_it.scan_dir < 0)
7365 stop = -1;
7366 composition_compute_stop_pos (&it->cmp_it,
7367 IT_STRING_CHARPOS (*it),
7368 IT_STRING_BYTEPOS (*it), stop,
7369 it->string);
7373 else
7375 if (!it->bidi_p
7376 /* If the string position is beyond string's end, it
7377 means next_element_from_string is padding the string
7378 with blanks, in which case we bypass the bidi
7379 iterator, because it cannot deal with such virtual
7380 characters. */
7381 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7383 IT_STRING_BYTEPOS (*it) += it->len;
7384 IT_STRING_CHARPOS (*it) += 1;
7386 else
7388 int prev_scan_dir = it->bidi_it.scan_dir;
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7393 if (prev_scan_dir != it->bidi_it.scan_dir)
7395 ptrdiff_t stop = it->end_charpos;
7397 if (it->bidi_it.scan_dir < 0)
7398 stop = -1;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7407 consider_string_end:
7409 if (it->current.overlay_string_index >= 0)
7411 /* IT->string is an overlay string. Advance to the
7412 next, if there is one. */
7413 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7415 it->ellipsis_p = 0;
7416 next_overlay_string (it);
7417 if (it->ellipsis_p)
7418 setup_for_ellipsis (it, 0);
7421 else
7423 /* IT->string is not an overlay string. If we reached
7424 its end, and there is something on IT->stack, proceed
7425 with what is on the stack. This can be either another
7426 string, this time an overlay string, or a buffer. */
7427 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7428 && it->sp > 0)
7430 pop_it (it);
7431 if (it->method == GET_FROM_STRING)
7432 goto consider_string_end;
7435 break;
7437 case GET_FROM_IMAGE:
7438 case GET_FROM_STRETCH:
7439 /* The position etc with which we have to proceed are on
7440 the stack. The position may be at the end of a string,
7441 if the `display' property takes up the whole string. */
7442 eassert (it->sp > 0);
7443 pop_it (it);
7444 if (it->method == GET_FROM_STRING)
7445 goto consider_string_end;
7446 break;
7448 default:
7449 /* There are no other methods defined, so this should be a bug. */
7450 emacs_abort ();
7453 eassert (it->method != GET_FROM_STRING
7454 || (STRINGP (it->string)
7455 && IT_STRING_CHARPOS (*it) >= 0));
7458 /* Load IT's display element fields with information about the next
7459 display element which comes from a display table entry or from the
7460 result of translating a control character to one of the forms `^C'
7461 or `\003'.
7463 IT->dpvec holds the glyphs to return as characters.
7464 IT->saved_face_id holds the face id before the display vector--it
7465 is restored into IT->face_id in set_iterator_to_next. */
7467 static int
7468 next_element_from_display_vector (struct it *it)
7470 Lisp_Object gc;
7471 int prev_face_id = it->face_id;
7472 int next_face_id;
7474 /* Precondition. */
7475 eassert (it->dpvec && it->current.dpvec_index >= 0);
7477 it->face_id = it->saved_face_id;
7479 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7480 That seemed totally bogus - so I changed it... */
7481 gc = it->dpvec[it->current.dpvec_index];
7483 if (GLYPH_CODE_P (gc))
7485 struct face *this_face, *prev_face, *next_face;
7487 it->c = GLYPH_CODE_CHAR (gc);
7488 it->len = CHAR_BYTES (it->c);
7490 /* The entry may contain a face id to use. Such a face id is
7491 the id of a Lisp face, not a realized face. A face id of
7492 zero means no face is specified. */
7493 if (it->dpvec_face_id >= 0)
7494 it->face_id = it->dpvec_face_id;
7495 else
7497 int lface_id = GLYPH_CODE_FACE (gc);
7498 if (lface_id > 0)
7499 it->face_id = merge_faces (it->f, Qt, lface_id,
7500 it->saved_face_id);
7503 /* Glyphs in the display vector could have the box face, so we
7504 need to set the related flags in the iterator, as
7505 appropriate. */
7506 this_face = FACE_FROM_ID (it->f, it->face_id);
7507 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7509 /* Is this character the first character of a box-face run? */
7510 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7511 && (!prev_face
7512 || prev_face->box == FACE_NO_BOX));
7514 /* For the last character of the box-face run, we need to look
7515 either at the next glyph from the display vector, or at the
7516 face we saw before the display vector. */
7517 next_face_id = it->saved_face_id;
7518 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7520 if (it->dpvec_face_id >= 0)
7521 next_face_id = it->dpvec_face_id;
7522 else
7524 int lface_id =
7525 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7527 if (lface_id > 0)
7528 next_face_id = merge_faces (it->f, Qt, lface_id,
7529 it->saved_face_id);
7532 next_face = FACE_FROM_ID (it->f, next_face_id);
7533 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7534 && (!next_face
7535 || next_face->box == FACE_NO_BOX));
7536 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7538 else
7539 /* Display table entry is invalid. Return a space. */
7540 it->c = ' ', it->len = 1;
7542 /* Don't change position and object of the iterator here. They are
7543 still the values of the character that had this display table
7544 entry or was translated, and that's what we want. */
7545 it->what = IT_CHARACTER;
7546 return 1;
7549 /* Get the first element of string/buffer in the visual order, after
7550 being reseated to a new position in a string or a buffer. */
7551 static void
7552 get_visually_first_element (struct it *it)
7554 int string_p = STRINGP (it->string) || it->s;
7555 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7556 ptrdiff_t bob = (string_p ? 0 : BEGV);
7558 if (STRINGP (it->string))
7560 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7563 else
7565 it->bidi_it.charpos = IT_CHARPOS (*it);
7566 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7569 if (it->bidi_it.charpos == eob)
7571 /* Nothing to do, but reset the FIRST_ELT flag, like
7572 bidi_paragraph_init does, because we are not going to
7573 call it. */
7574 it->bidi_it.first_elt = 0;
7576 else if (it->bidi_it.charpos == bob
7577 || (!string_p
7578 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7579 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7581 /* If we are at the beginning of a line/string, we can produce
7582 the next element right away. */
7583 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7584 bidi_move_to_visually_next (&it->bidi_it);
7586 else
7588 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7590 /* We need to prime the bidi iterator starting at the line's or
7591 string's beginning, before we will be able to produce the
7592 next element. */
7593 if (string_p)
7594 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7595 else
7596 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7597 IT_BYTEPOS (*it), -1,
7598 &it->bidi_it.bytepos);
7599 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7602 /* Now return to buffer/string position where we were asked
7603 to get the next display element, and produce that. */
7604 bidi_move_to_visually_next (&it->bidi_it);
7606 while (it->bidi_it.bytepos != orig_bytepos
7607 && it->bidi_it.charpos < eob);
7610 /* Adjust IT's position information to where we ended up. */
7611 if (STRINGP (it->string))
7613 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7616 else
7618 IT_CHARPOS (*it) = it->bidi_it.charpos;
7619 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7622 if (STRINGP (it->string) || !it->s)
7624 ptrdiff_t stop, charpos, bytepos;
7626 if (STRINGP (it->string))
7628 eassert (!it->s);
7629 stop = SCHARS (it->string);
7630 if (stop > it->end_charpos)
7631 stop = it->end_charpos;
7632 charpos = IT_STRING_CHARPOS (*it);
7633 bytepos = IT_STRING_BYTEPOS (*it);
7635 else
7637 stop = it->end_charpos;
7638 charpos = IT_CHARPOS (*it);
7639 bytepos = IT_BYTEPOS (*it);
7641 if (it->bidi_it.scan_dir < 0)
7642 stop = -1;
7643 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7644 it->string);
7648 /* Load IT with the next display element from Lisp string IT->string.
7649 IT->current.string_pos is the current position within the string.
7650 If IT->current.overlay_string_index >= 0, the Lisp string is an
7651 overlay string. */
7653 static int
7654 next_element_from_string (struct it *it)
7656 struct text_pos position;
7658 eassert (STRINGP (it->string));
7659 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7660 eassert (IT_STRING_CHARPOS (*it) >= 0);
7661 position = it->current.string_pos;
7663 /* With bidi reordering, the character to display might not be the
7664 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7665 that we were reseat()ed to a new string, whose paragraph
7666 direction is not known. */
7667 if (it->bidi_p && it->bidi_it.first_elt)
7669 get_visually_first_element (it);
7670 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7673 /* Time to check for invisible text? */
7674 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7676 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7678 if (!(!it->bidi_p
7679 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7680 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7682 /* With bidi non-linear iteration, we could find
7683 ourselves far beyond the last computed stop_charpos,
7684 with several other stop positions in between that we
7685 missed. Scan them all now, in buffer's logical
7686 order, until we find and handle the last stop_charpos
7687 that precedes our current position. */
7688 handle_stop_backwards (it, it->stop_charpos);
7689 return GET_NEXT_DISPLAY_ELEMENT (it);
7691 else
7693 if (it->bidi_p)
7695 /* Take note of the stop position we just moved
7696 across, for when we will move back across it. */
7697 it->prev_stop = it->stop_charpos;
7698 /* If we are at base paragraph embedding level, take
7699 note of the last stop position seen at this
7700 level. */
7701 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7702 it->base_level_stop = it->stop_charpos;
7704 handle_stop (it);
7706 /* Since a handler may have changed IT->method, we must
7707 recurse here. */
7708 return GET_NEXT_DISPLAY_ELEMENT (it);
7711 else if (it->bidi_p
7712 /* If we are before prev_stop, we may have overstepped
7713 on our way backwards a stop_pos, and if so, we need
7714 to handle that stop_pos. */
7715 && IT_STRING_CHARPOS (*it) < it->prev_stop
7716 /* We can sometimes back up for reasons that have nothing
7717 to do with bidi reordering. E.g., compositions. The
7718 code below is only needed when we are above the base
7719 embedding level, so test for that explicitly. */
7720 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7722 /* If we lost track of base_level_stop, we have no better
7723 place for handle_stop_backwards to start from than string
7724 beginning. This happens, e.g., when we were reseated to
7725 the previous screenful of text by vertical-motion. */
7726 if (it->base_level_stop <= 0
7727 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7728 it->base_level_stop = 0;
7729 handle_stop_backwards (it, it->base_level_stop);
7730 return GET_NEXT_DISPLAY_ELEMENT (it);
7734 if (it->current.overlay_string_index >= 0)
7736 /* Get the next character from an overlay string. In overlay
7737 strings, there is no field width or padding with spaces to
7738 do. */
7739 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7741 it->what = IT_EOB;
7742 return 0;
7744 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7745 IT_STRING_BYTEPOS (*it),
7746 it->bidi_it.scan_dir < 0
7747 ? -1
7748 : SCHARS (it->string))
7749 && next_element_from_composition (it))
7751 return 1;
7753 else if (STRING_MULTIBYTE (it->string))
7755 const unsigned char *s = (SDATA (it->string)
7756 + IT_STRING_BYTEPOS (*it));
7757 it->c = string_char_and_length (s, &it->len);
7759 else
7761 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7762 it->len = 1;
7765 else
7767 /* Get the next character from a Lisp string that is not an
7768 overlay string. Such strings come from the mode line, for
7769 example. We may have to pad with spaces, or truncate the
7770 string. See also next_element_from_c_string. */
7771 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7773 it->what = IT_EOB;
7774 return 0;
7776 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7778 /* Pad with spaces. */
7779 it->c = ' ', it->len = 1;
7780 CHARPOS (position) = BYTEPOS (position) = -1;
7782 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7783 IT_STRING_BYTEPOS (*it),
7784 it->bidi_it.scan_dir < 0
7785 ? -1
7786 : it->string_nchars)
7787 && next_element_from_composition (it))
7789 return 1;
7791 else if (STRING_MULTIBYTE (it->string))
7793 const unsigned char *s = (SDATA (it->string)
7794 + IT_STRING_BYTEPOS (*it));
7795 it->c = string_char_and_length (s, &it->len);
7797 else
7799 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7800 it->len = 1;
7804 /* Record what we have and where it came from. */
7805 it->what = IT_CHARACTER;
7806 it->object = it->string;
7807 it->position = position;
7808 return 1;
7812 /* Load IT with next display element from C string IT->s.
7813 IT->string_nchars is the maximum number of characters to return
7814 from the string. IT->end_charpos may be greater than
7815 IT->string_nchars when this function is called, in which case we
7816 may have to return padding spaces. Value is zero if end of string
7817 reached, including padding spaces. */
7819 static int
7820 next_element_from_c_string (struct it *it)
7822 bool success_p = true;
7824 eassert (it->s);
7825 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7826 it->what = IT_CHARACTER;
7827 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7828 it->object = Qnil;
7830 /* With bidi reordering, the character to display might not be the
7831 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7832 we were reseated to a new string, whose paragraph direction is
7833 not known. */
7834 if (it->bidi_p && it->bidi_it.first_elt)
7835 get_visually_first_element (it);
7837 /* IT's position can be greater than IT->string_nchars in case a
7838 field width or precision has been specified when the iterator was
7839 initialized. */
7840 if (IT_CHARPOS (*it) >= it->end_charpos)
7842 /* End of the game. */
7843 it->what = IT_EOB;
7844 success_p = 0;
7846 else if (IT_CHARPOS (*it) >= it->string_nchars)
7848 /* Pad with spaces. */
7849 it->c = ' ', it->len = 1;
7850 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7852 else if (it->multibyte_p)
7853 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7854 else
7855 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7857 return success_p;
7861 /* Set up IT to return characters from an ellipsis, if appropriate.
7862 The definition of the ellipsis glyphs may come from a display table
7863 entry. This function fills IT with the first glyph from the
7864 ellipsis if an ellipsis is to be displayed. */
7866 static int
7867 next_element_from_ellipsis (struct it *it)
7869 if (it->selective_display_ellipsis_p)
7870 setup_for_ellipsis (it, it->len);
7871 else
7873 /* The face at the current position may be different from the
7874 face we find after the invisible text. Remember what it
7875 was in IT->saved_face_id, and signal that it's there by
7876 setting face_before_selective_p. */
7877 it->saved_face_id = it->face_id;
7878 it->method = GET_FROM_BUFFER;
7879 it->object = it->w->contents;
7880 reseat_at_next_visible_line_start (it, 1);
7881 it->face_before_selective_p = true;
7884 return GET_NEXT_DISPLAY_ELEMENT (it);
7888 /* Deliver an image display element. The iterator IT is already
7889 filled with image information (done in handle_display_prop). Value
7890 is always 1. */
7893 static int
7894 next_element_from_image (struct it *it)
7896 it->what = IT_IMAGE;
7897 it->ignore_overlay_strings_at_pos_p = 0;
7898 return 1;
7902 /* Fill iterator IT with next display element from a stretch glyph
7903 property. IT->object is the value of the text property. Value is
7904 always 1. */
7906 static int
7907 next_element_from_stretch (struct it *it)
7909 it->what = IT_STRETCH;
7910 return 1;
7913 /* Scan backwards from IT's current position until we find a stop
7914 position, or until BEGV. This is called when we find ourself
7915 before both the last known prev_stop and base_level_stop while
7916 reordering bidirectional text. */
7918 static void
7919 compute_stop_pos_backwards (struct it *it)
7921 const int SCAN_BACK_LIMIT = 1000;
7922 struct text_pos pos;
7923 struct display_pos save_current = it->current;
7924 struct text_pos save_position = it->position;
7925 ptrdiff_t charpos = IT_CHARPOS (*it);
7926 ptrdiff_t where_we_are = charpos;
7927 ptrdiff_t save_stop_pos = it->stop_charpos;
7928 ptrdiff_t save_end_pos = it->end_charpos;
7930 eassert (NILP (it->string) && !it->s);
7931 eassert (it->bidi_p);
7932 it->bidi_p = 0;
7935 it->end_charpos = min (charpos + 1, ZV);
7936 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7937 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7938 reseat_1 (it, pos, 0);
7939 compute_stop_pos (it);
7940 /* We must advance forward, right? */
7941 if (it->stop_charpos <= charpos)
7942 emacs_abort ();
7944 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7946 if (it->stop_charpos <= where_we_are)
7947 it->prev_stop = it->stop_charpos;
7948 else
7949 it->prev_stop = BEGV;
7950 it->bidi_p = true;
7951 it->current = save_current;
7952 it->position = save_position;
7953 it->stop_charpos = save_stop_pos;
7954 it->end_charpos = save_end_pos;
7957 /* Scan forward from CHARPOS in the current buffer/string, until we
7958 find a stop position > current IT's position. Then handle the stop
7959 position before that. This is called when we bump into a stop
7960 position while reordering bidirectional text. CHARPOS should be
7961 the last previously processed stop_pos (or BEGV/0, if none were
7962 processed yet) whose position is less that IT's current
7963 position. */
7965 static void
7966 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7968 int bufp = !STRINGP (it->string);
7969 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7970 struct display_pos save_current = it->current;
7971 struct text_pos save_position = it->position;
7972 struct text_pos pos1;
7973 ptrdiff_t next_stop;
7975 /* Scan in strict logical order. */
7976 eassert (it->bidi_p);
7977 it->bidi_p = 0;
7980 it->prev_stop = charpos;
7981 if (bufp)
7983 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7984 reseat_1 (it, pos1, 0);
7986 else
7987 it->current.string_pos = string_pos (charpos, it->string);
7988 compute_stop_pos (it);
7989 /* We must advance forward, right? */
7990 if (it->stop_charpos <= it->prev_stop)
7991 emacs_abort ();
7992 charpos = it->stop_charpos;
7994 while (charpos <= where_we_are);
7996 it->bidi_p = true;
7997 it->current = save_current;
7998 it->position = save_position;
7999 next_stop = it->stop_charpos;
8000 it->stop_charpos = it->prev_stop;
8001 handle_stop (it);
8002 it->stop_charpos = next_stop;
8005 /* Load IT with the next display element from current_buffer. Value
8006 is zero if end of buffer reached. IT->stop_charpos is the next
8007 position at which to stop and check for text properties or buffer
8008 end. */
8010 static int
8011 next_element_from_buffer (struct it *it)
8013 bool success_p = true;
8015 eassert (IT_CHARPOS (*it) >= BEGV);
8016 eassert (NILP (it->string) && !it->s);
8017 eassert (!it->bidi_p
8018 || (EQ (it->bidi_it.string.lstring, Qnil)
8019 && it->bidi_it.string.s == NULL));
8021 /* With bidi reordering, the character to display might not be the
8022 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8023 we were reseat()ed to a new buffer position, which is potentially
8024 a different paragraph. */
8025 if (it->bidi_p && it->bidi_it.first_elt)
8027 get_visually_first_element (it);
8028 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8031 if (IT_CHARPOS (*it) >= it->stop_charpos)
8033 if (IT_CHARPOS (*it) >= it->end_charpos)
8035 int overlay_strings_follow_p;
8037 /* End of the game, except when overlay strings follow that
8038 haven't been returned yet. */
8039 if (it->overlay_strings_at_end_processed_p)
8040 overlay_strings_follow_p = 0;
8041 else
8043 it->overlay_strings_at_end_processed_p = true;
8044 overlay_strings_follow_p = get_overlay_strings (it, 0);
8047 if (overlay_strings_follow_p)
8048 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8049 else
8051 it->what = IT_EOB;
8052 it->position = it->current.pos;
8053 success_p = 0;
8056 else if (!(!it->bidi_p
8057 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8058 || IT_CHARPOS (*it) == it->stop_charpos))
8060 /* With bidi non-linear iteration, we could find ourselves
8061 far beyond the last computed stop_charpos, with several
8062 other stop positions in between that we missed. Scan
8063 them all now, in buffer's logical order, until we find
8064 and handle the last stop_charpos that precedes our
8065 current position. */
8066 handle_stop_backwards (it, it->stop_charpos);
8067 return GET_NEXT_DISPLAY_ELEMENT (it);
8069 else
8071 if (it->bidi_p)
8073 /* Take note of the stop position we just moved across,
8074 for when we will move back across it. */
8075 it->prev_stop = it->stop_charpos;
8076 /* If we are at base paragraph embedding level, take
8077 note of the last stop position seen at this
8078 level. */
8079 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8080 it->base_level_stop = it->stop_charpos;
8082 handle_stop (it);
8083 return GET_NEXT_DISPLAY_ELEMENT (it);
8086 else if (it->bidi_p
8087 /* If we are before prev_stop, we may have overstepped on
8088 our way backwards a stop_pos, and if so, we need to
8089 handle that stop_pos. */
8090 && IT_CHARPOS (*it) < it->prev_stop
8091 /* We can sometimes back up for reasons that have nothing
8092 to do with bidi reordering. E.g., compositions. The
8093 code below is only needed when we are above the base
8094 embedding level, so test for that explicitly. */
8095 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8097 if (it->base_level_stop <= 0
8098 || IT_CHARPOS (*it) < it->base_level_stop)
8100 /* If we lost track of base_level_stop, we need to find
8101 prev_stop by looking backwards. This happens, e.g., when
8102 we were reseated to the previous screenful of text by
8103 vertical-motion. */
8104 it->base_level_stop = BEGV;
8105 compute_stop_pos_backwards (it);
8106 handle_stop_backwards (it, it->prev_stop);
8108 else
8109 handle_stop_backwards (it, it->base_level_stop);
8110 return GET_NEXT_DISPLAY_ELEMENT (it);
8112 else
8114 /* No face changes, overlays etc. in sight, so just return a
8115 character from current_buffer. */
8116 unsigned char *p;
8117 ptrdiff_t stop;
8119 /* Maybe run the redisplay end trigger hook. Performance note:
8120 This doesn't seem to cost measurable time. */
8121 if (it->redisplay_end_trigger_charpos
8122 && it->glyph_row
8123 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8124 run_redisplay_end_trigger_hook (it);
8126 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8127 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8128 stop)
8129 && next_element_from_composition (it))
8131 return 1;
8134 /* Get the next character, maybe multibyte. */
8135 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8136 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8137 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8138 else
8139 it->c = *p, it->len = 1;
8141 /* Record what we have and where it came from. */
8142 it->what = IT_CHARACTER;
8143 it->object = it->w->contents;
8144 it->position = it->current.pos;
8146 /* Normally we return the character found above, except when we
8147 really want to return an ellipsis for selective display. */
8148 if (it->selective)
8150 if (it->c == '\n')
8152 /* A value of selective > 0 means hide lines indented more
8153 than that number of columns. */
8154 if (it->selective > 0
8155 && IT_CHARPOS (*it) + 1 < ZV
8156 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8157 IT_BYTEPOS (*it) + 1,
8158 it->selective))
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8164 else if (it->c == '\r' && it->selective == -1)
8166 /* A value of selective == -1 means that everything from the
8167 CR to the end of the line is invisible, with maybe an
8168 ellipsis displayed for it. */
8169 success_p = next_element_from_ellipsis (it);
8170 it->dpvec_char_len = -1;
8175 /* Value is zero if end of buffer reached. */
8176 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8177 return success_p;
8181 /* Run the redisplay end trigger hook for IT. */
8183 static void
8184 run_redisplay_end_trigger_hook (struct it *it)
8186 Lisp_Object args[3];
8188 /* IT->glyph_row should be non-null, i.e. we should be actually
8189 displaying something, or otherwise we should not run the hook. */
8190 eassert (it->glyph_row);
8192 /* Set up hook arguments. */
8193 args[0] = Qredisplay_end_trigger_functions;
8194 args[1] = it->window;
8195 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8196 it->redisplay_end_trigger_charpos = 0;
8198 /* Since we are *trying* to run these functions, don't try to run
8199 them again, even if they get an error. */
8200 wset_redisplay_end_trigger (it->w, Qnil);
8201 Frun_hook_with_args (3, args);
8203 /* Notice if it changed the face of the character we are on. */
8204 handle_face_prop (it);
8208 /* Deliver a composition display element. Unlike the other
8209 next_element_from_XXX, this function is not registered in the array
8210 get_next_element[]. It is called from next_element_from_buffer and
8211 next_element_from_string when necessary. */
8213 static int
8214 next_element_from_composition (struct it *it)
8216 it->what = IT_COMPOSITION;
8217 it->len = it->cmp_it.nbytes;
8218 if (STRINGP (it->string))
8220 if (it->c < 0)
8222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8224 return 0;
8226 it->position = it->current.string_pos;
8227 it->object = it->string;
8228 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8229 IT_STRING_BYTEPOS (*it), it->string);
8231 else
8233 if (it->c < 0)
8235 IT_CHARPOS (*it) += it->cmp_it.nchars;
8236 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8237 if (it->bidi_p)
8239 if (it->bidi_it.new_paragraph)
8240 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8241 /* Resync the bidi iterator with IT's new position.
8242 FIXME: this doesn't support bidirectional text. */
8243 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8244 bidi_move_to_visually_next (&it->bidi_it);
8246 return 0;
8248 it->position = it->current.pos;
8249 it->object = it->w->contents;
8250 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8251 IT_BYTEPOS (*it), Qnil);
8253 return 1;
8258 /***********************************************************************
8259 Moving an iterator without producing glyphs
8260 ***********************************************************************/
8262 /* Check if iterator is at a position corresponding to a valid buffer
8263 position after some move_it_ call. */
8265 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8266 ((it)->method == GET_FROM_STRING \
8267 ? IT_STRING_CHARPOS (*it) == 0 \
8268 : 1)
8271 /* Move iterator IT to a specified buffer or X position within one
8272 line on the display without producing glyphs.
8274 OP should be a bit mask including some or all of these bits:
8275 MOVE_TO_X: Stop upon reaching x-position TO_X.
8276 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8277 Regardless of OP's value, stop upon reaching the end of the display line.
8279 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8280 This means, in particular, that TO_X includes window's horizontal
8281 scroll amount.
8283 The return value has several possible values that
8284 say what condition caused the scan to stop:
8286 MOVE_POS_MATCH_OR_ZV
8287 - when TO_POS or ZV was reached.
8289 MOVE_X_REACHED
8290 -when TO_X was reached before TO_POS or ZV were reached.
8292 MOVE_LINE_CONTINUED
8293 - when we reached the end of the display area and the line must
8294 be continued.
8296 MOVE_LINE_TRUNCATED
8297 - when we reached the end of the display area and the line is
8298 truncated.
8300 MOVE_NEWLINE_OR_CR
8301 - when we stopped at a line end, i.e. a newline or a CR and selective
8302 display is on. */
8304 static enum move_it_result
8305 move_it_in_display_line_to (struct it *it,
8306 ptrdiff_t to_charpos, int to_x,
8307 enum move_operation_enum op)
8309 enum move_it_result result = MOVE_UNDEFINED;
8310 struct glyph_row *saved_glyph_row;
8311 struct it wrap_it, atpos_it, atx_it, ppos_it;
8312 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8313 void *ppos_data = NULL;
8314 int may_wrap = 0;
8315 enum it_method prev_method = it->method;
8316 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8317 int saw_smaller_pos = prev_pos < to_charpos;
8319 /* Don't produce glyphs in produce_glyphs. */
8320 saved_glyph_row = it->glyph_row;
8321 it->glyph_row = NULL;
8323 /* Use wrap_it to save a copy of IT wherever a word wrap could
8324 occur. Use atpos_it to save a copy of IT at the desired buffer
8325 position, if found, so that we can scan ahead and check if the
8326 word later overshoots the window edge. Use atx_it similarly, for
8327 pixel positions. */
8328 wrap_it.sp = -1;
8329 atpos_it.sp = -1;
8330 atx_it.sp = -1;
8332 /* Use ppos_it under bidi reordering to save a copy of IT for the
8333 position > CHARPOS that is the closest to CHARPOS. We restore
8334 that position in IT when we have scanned the entire display line
8335 without finding a match for CHARPOS and all the character
8336 positions are greater than CHARPOS. */
8337 if (it->bidi_p)
8339 SAVE_IT (ppos_it, *it, ppos_data);
8340 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8341 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8342 SAVE_IT (ppos_it, *it, ppos_data);
8345 #define BUFFER_POS_REACHED_P() \
8346 ((op & MOVE_TO_POS) != 0 \
8347 && BUFFERP (it->object) \
8348 && (IT_CHARPOS (*it) == to_charpos \
8349 || ((!it->bidi_p \
8350 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8351 && IT_CHARPOS (*it) > to_charpos) \
8352 || (it->what == IT_COMPOSITION \
8353 && ((IT_CHARPOS (*it) > to_charpos \
8354 && to_charpos >= it->cmp_it.charpos) \
8355 || (IT_CHARPOS (*it) < to_charpos \
8356 && to_charpos <= it->cmp_it.charpos)))) \
8357 && (it->method == GET_FROM_BUFFER \
8358 || (it->method == GET_FROM_DISPLAY_VECTOR \
8359 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8361 /* If there's a line-/wrap-prefix, handle it. */
8362 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8363 && it->current_y < it->last_visible_y)
8364 handle_line_prefix (it);
8366 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8367 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8369 while (1)
8371 int x, i, ascent = 0, descent = 0;
8373 /* Utility macro to reset an iterator with x, ascent, and descent. */
8374 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8375 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8376 (IT)->max_descent = descent)
8378 /* Stop if we move beyond TO_CHARPOS (after an image or a
8379 display string or stretch glyph). */
8380 if ((op & MOVE_TO_POS) != 0
8381 && BUFFERP (it->object)
8382 && it->method == GET_FROM_BUFFER
8383 && (((!it->bidi_p
8384 /* When the iterator is at base embedding level, we
8385 are guaranteed that characters are delivered for
8386 display in strictly increasing order of their
8387 buffer positions. */
8388 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8389 && IT_CHARPOS (*it) > to_charpos)
8390 || (it->bidi_p
8391 && (prev_method == GET_FROM_IMAGE
8392 || prev_method == GET_FROM_STRETCH
8393 || prev_method == GET_FROM_STRING)
8394 /* Passed TO_CHARPOS from left to right. */
8395 && ((prev_pos < to_charpos
8396 && IT_CHARPOS (*it) > to_charpos)
8397 /* Passed TO_CHARPOS from right to left. */
8398 || (prev_pos > to_charpos
8399 && IT_CHARPOS (*it) < to_charpos)))))
8401 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8403 result = MOVE_POS_MATCH_OR_ZV;
8404 break;
8406 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8407 /* If wrap_it is valid, the current position might be in a
8408 word that is wrapped. So, save the iterator in
8409 atpos_it and continue to see if wrapping happens. */
8410 SAVE_IT (atpos_it, *it, atpos_data);
8413 /* Stop when ZV reached.
8414 We used to stop here when TO_CHARPOS reached as well, but that is
8415 too soon if this glyph does not fit on this line. So we handle it
8416 explicitly below. */
8417 if (!get_next_display_element (it))
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8423 if (it->line_wrap == TRUNCATE)
8425 if (BUFFER_POS_REACHED_P ())
8427 result = MOVE_POS_MATCH_OR_ZV;
8428 break;
8431 else
8433 if (it->line_wrap == WORD_WRAP)
8435 if (IT_DISPLAYING_WHITESPACE (it))
8436 may_wrap = 1;
8437 else if (may_wrap)
8439 /* We have reached a glyph that follows one or more
8440 whitespace characters. If the position is
8441 already found, we are done. */
8442 if (atpos_it.sp >= 0)
8444 RESTORE_IT (it, &atpos_it, atpos_data);
8445 result = MOVE_POS_MATCH_OR_ZV;
8446 goto done;
8448 if (atx_it.sp >= 0)
8450 RESTORE_IT (it, &atx_it, atx_data);
8451 result = MOVE_X_REACHED;
8452 goto done;
8454 /* Otherwise, we can wrap here. */
8455 SAVE_IT (wrap_it, *it, wrap_data);
8456 may_wrap = 0;
8461 /* Remember the line height for the current line, in case
8462 the next element doesn't fit on the line. */
8463 ascent = it->max_ascent;
8464 descent = it->max_descent;
8466 /* The call to produce_glyphs will get the metrics of the
8467 display element IT is loaded with. Record the x-position
8468 before this display element, in case it doesn't fit on the
8469 line. */
8470 x = it->current_x;
8472 PRODUCE_GLYPHS (it);
8474 if (it->area != TEXT_AREA)
8476 prev_method = it->method;
8477 if (it->method == GET_FROM_BUFFER)
8478 prev_pos = IT_CHARPOS (*it);
8479 set_iterator_to_next (it, 1);
8480 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8481 SET_TEXT_POS (this_line_min_pos,
8482 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8483 if (it->bidi_p
8484 && (op & MOVE_TO_POS)
8485 && IT_CHARPOS (*it) > to_charpos
8486 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8487 SAVE_IT (ppos_it, *it, ppos_data);
8488 continue;
8491 /* The number of glyphs we get back in IT->nglyphs will normally
8492 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8493 character on a terminal frame, or (iii) a line end. For the
8494 second case, IT->nglyphs - 1 padding glyphs will be present.
8495 (On X frames, there is only one glyph produced for a
8496 composite character.)
8498 The behavior implemented below means, for continuation lines,
8499 that as many spaces of a TAB as fit on the current line are
8500 displayed there. For terminal frames, as many glyphs of a
8501 multi-glyph character are displayed in the current line, too.
8502 This is what the old redisplay code did, and we keep it that
8503 way. Under X, the whole shape of a complex character must
8504 fit on the line or it will be completely displayed in the
8505 next line.
8507 Note that both for tabs and padding glyphs, all glyphs have
8508 the same width. */
8509 if (it->nglyphs)
8511 /* More than one glyph or glyph doesn't fit on line. All
8512 glyphs have the same width. */
8513 int single_glyph_width = it->pixel_width / it->nglyphs;
8514 int new_x;
8515 int x_before_this_char = x;
8516 int hpos_before_this_char = it->hpos;
8518 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8520 new_x = x + single_glyph_width;
8522 /* We want to leave anything reaching TO_X to the caller. */
8523 if ((op & MOVE_TO_X) && new_x > to_x)
8525 if (BUFFER_POS_REACHED_P ())
8527 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8528 goto buffer_pos_reached;
8529 if (atpos_it.sp < 0)
8531 SAVE_IT (atpos_it, *it, atpos_data);
8532 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8535 else
8537 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8539 it->current_x = x;
8540 result = MOVE_X_REACHED;
8541 break;
8543 if (atx_it.sp < 0)
8545 SAVE_IT (atx_it, *it, atx_data);
8546 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8551 if (/* Lines are continued. */
8552 it->line_wrap != TRUNCATE
8553 && (/* And glyph doesn't fit on the line. */
8554 new_x > it->last_visible_x
8555 /* Or it fits exactly and we're on a window
8556 system frame. */
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)
8559 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8560 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8561 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8563 if (/* IT->hpos == 0 means the very first glyph
8564 doesn't fit on the line, e.g. a wide image. */
8565 it->hpos == 0
8566 || (new_x == it->last_visible_x
8567 && FRAME_WINDOW_P (it->f)))
8569 ++it->hpos;
8570 it->current_x = new_x;
8572 /* The character's last glyph just barely fits
8573 in this row. */
8574 if (i == it->nglyphs - 1)
8576 /* If this is the destination position,
8577 return a position *before* it in this row,
8578 now that we know it fits in this row. */
8579 if (BUFFER_POS_REACHED_P ())
8581 if (it->line_wrap != WORD_WRAP
8582 || wrap_it.sp < 0)
8584 it->hpos = hpos_before_this_char;
8585 it->current_x = x_before_this_char;
8586 result = MOVE_POS_MATCH_OR_ZV;
8587 break;
8589 if (it->line_wrap == WORD_WRAP
8590 && atpos_it.sp < 0)
8592 SAVE_IT (atpos_it, *it, atpos_data);
8593 atpos_it.current_x = x_before_this_char;
8594 atpos_it.hpos = hpos_before_this_char;
8598 prev_method = it->method;
8599 if (it->method == GET_FROM_BUFFER)
8600 prev_pos = IT_CHARPOS (*it);
8601 set_iterator_to_next (it, 1);
8602 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8603 SET_TEXT_POS (this_line_min_pos,
8604 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8605 /* On graphical terminals, newlines may
8606 "overflow" into the fringe if
8607 overflow-newline-into-fringe is non-nil.
8608 On text terminals, and on graphical
8609 terminals with no right margin, newlines
8610 may overflow into the last glyph on the
8611 display line.*/
8612 if (!FRAME_WINDOW_P (it->f)
8613 || ((it->bidi_p
8614 && it->bidi_it.paragraph_dir == R2L)
8615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8617 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8619 if (!get_next_display_element (it))
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8624 if (BUFFER_POS_REACHED_P ())
8626 if (ITERATOR_AT_END_OF_LINE_P (it))
8627 result = MOVE_POS_MATCH_OR_ZV;
8628 else
8629 result = MOVE_LINE_CONTINUED;
8630 break;
8632 if (ITERATOR_AT_END_OF_LINE_P (it)
8633 && (it->line_wrap != WORD_WRAP
8634 || wrap_it.sp < 0))
8636 result = MOVE_NEWLINE_OR_CR;
8637 break;
8642 else
8643 IT_RESET_X_ASCENT_DESCENT (it);
8645 if (wrap_it.sp >= 0)
8647 RESTORE_IT (it, &wrap_it, wrap_data);
8648 atpos_it.sp = -1;
8649 atx_it.sp = -1;
8652 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8653 IT_CHARPOS (*it)));
8654 result = MOVE_LINE_CONTINUED;
8655 break;
8658 if (BUFFER_POS_REACHED_P ())
8660 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8661 goto buffer_pos_reached;
8662 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8664 SAVE_IT (atpos_it, *it, atpos_data);
8665 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8669 if (new_x > it->first_visible_x)
8671 /* Glyph is visible. Increment number of glyphs that
8672 would be displayed. */
8673 ++it->hpos;
8677 if (result != MOVE_UNDEFINED)
8678 break;
8680 else if (BUFFER_POS_REACHED_P ())
8682 buffer_pos_reached:
8683 IT_RESET_X_ASCENT_DESCENT (it);
8684 result = MOVE_POS_MATCH_OR_ZV;
8685 break;
8687 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8689 /* Stop when TO_X specified and reached. This check is
8690 necessary here because of lines consisting of a line end,
8691 only. The line end will not produce any glyphs and we
8692 would never get MOVE_X_REACHED. */
8693 eassert (it->nglyphs == 0);
8694 result = MOVE_X_REACHED;
8695 break;
8698 /* Is this a line end? If yes, we're done. */
8699 if (ITERATOR_AT_END_OF_LINE_P (it))
8701 /* If we are past TO_CHARPOS, but never saw any character
8702 positions smaller than TO_CHARPOS, return
8703 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8704 did. */
8705 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8707 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8709 if (IT_CHARPOS (ppos_it) < ZV)
8711 RESTORE_IT (it, &ppos_it, ppos_data);
8712 result = MOVE_POS_MATCH_OR_ZV;
8714 else
8715 goto buffer_pos_reached;
8717 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8718 && IT_CHARPOS (*it) > to_charpos)
8719 goto buffer_pos_reached;
8720 else
8721 result = MOVE_NEWLINE_OR_CR;
8723 else
8724 result = MOVE_NEWLINE_OR_CR;
8725 break;
8728 prev_method = it->method;
8729 if (it->method == GET_FROM_BUFFER)
8730 prev_pos = IT_CHARPOS (*it);
8731 /* The current display element has been consumed. Advance
8732 to the next. */
8733 set_iterator_to_next (it, 1);
8734 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8735 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8736 if (IT_CHARPOS (*it) < to_charpos)
8737 saw_smaller_pos = 1;
8738 if (it->bidi_p
8739 && (op & MOVE_TO_POS)
8740 && IT_CHARPOS (*it) >= to_charpos
8741 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8742 SAVE_IT (ppos_it, *it, ppos_data);
8744 /* Stop if lines are truncated and IT's current x-position is
8745 past the right edge of the window now. */
8746 if (it->line_wrap == TRUNCATE
8747 && it->current_x >= it->last_visible_x)
8749 if (!FRAME_WINDOW_P (it->f)
8750 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8751 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8752 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8753 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8755 int at_eob_p = 0;
8757 if ((at_eob_p = !get_next_display_element (it))
8758 || BUFFER_POS_REACHED_P ()
8759 /* If we are past TO_CHARPOS, but never saw any
8760 character positions smaller than TO_CHARPOS,
8761 return MOVE_POS_MATCH_OR_ZV, like the
8762 unidirectional display did. */
8763 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8764 && !saw_smaller_pos
8765 && IT_CHARPOS (*it) > to_charpos))
8767 if (it->bidi_p
8768 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8769 RESTORE_IT (it, &ppos_it, ppos_data);
8770 result = MOVE_POS_MATCH_OR_ZV;
8771 break;
8773 if (ITERATOR_AT_END_OF_LINE_P (it))
8775 result = MOVE_NEWLINE_OR_CR;
8776 break;
8779 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8780 && !saw_smaller_pos
8781 && IT_CHARPOS (*it) > to_charpos)
8783 if (IT_CHARPOS (ppos_it) < ZV)
8784 RESTORE_IT (it, &ppos_it, ppos_data);
8785 result = MOVE_POS_MATCH_OR_ZV;
8786 break;
8788 result = MOVE_LINE_TRUNCATED;
8789 break;
8791 #undef IT_RESET_X_ASCENT_DESCENT
8794 #undef BUFFER_POS_REACHED_P
8796 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8797 restore the saved iterator. */
8798 if (atpos_it.sp >= 0)
8799 RESTORE_IT (it, &atpos_it, atpos_data);
8800 else if (atx_it.sp >= 0)
8801 RESTORE_IT (it, &atx_it, atx_data);
8803 done:
8805 if (atpos_data)
8806 bidi_unshelve_cache (atpos_data, 1);
8807 if (atx_data)
8808 bidi_unshelve_cache (atx_data, 1);
8809 if (wrap_data)
8810 bidi_unshelve_cache (wrap_data, 1);
8811 if (ppos_data)
8812 bidi_unshelve_cache (ppos_data, 1);
8814 /* Restore the iterator settings altered at the beginning of this
8815 function. */
8816 it->glyph_row = saved_glyph_row;
8817 return result;
8820 /* For external use. */
8821 void
8822 move_it_in_display_line (struct it *it,
8823 ptrdiff_t to_charpos, int to_x,
8824 enum move_operation_enum op)
8826 if (it->line_wrap == WORD_WRAP
8827 && (op & MOVE_TO_X))
8829 struct it save_it;
8830 void *save_data = NULL;
8831 int skip;
8833 SAVE_IT (save_it, *it, save_data);
8834 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8835 /* When word-wrap is on, TO_X may lie past the end
8836 of a wrapped line. Then it->current is the
8837 character on the next line, so backtrack to the
8838 space before the wrap point. */
8839 if (skip == MOVE_LINE_CONTINUED)
8841 int prev_x = max (it->current_x - 1, 0);
8842 RESTORE_IT (it, &save_it, save_data);
8843 move_it_in_display_line_to
8844 (it, -1, prev_x, MOVE_TO_X);
8846 else
8847 bidi_unshelve_cache (save_data, 1);
8849 else
8850 move_it_in_display_line_to (it, to_charpos, to_x, op);
8854 /* Move IT forward until it satisfies one or more of the criteria in
8855 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8857 OP is a bit-mask that specifies where to stop, and in particular,
8858 which of those four position arguments makes a difference. See the
8859 description of enum move_operation_enum.
8861 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8862 screen line, this function will set IT to the next position that is
8863 displayed to the right of TO_CHARPOS on the screen.
8865 Return the maximum pixel length of any line scanned but never more
8866 than it.last_visible_x. */
8869 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8871 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8872 int line_height, line_start_x = 0, reached = 0;
8873 int max_current_x = 0;
8874 void *backup_data = NULL;
8876 for (;;)
8878 if (op & MOVE_TO_VPOS)
8880 /* If no TO_CHARPOS and no TO_X specified, stop at the
8881 start of the line TO_VPOS. */
8882 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8884 if (it->vpos == to_vpos)
8886 reached = 1;
8887 break;
8889 else
8890 skip = move_it_in_display_line_to (it, -1, -1, 0);
8892 else
8894 /* TO_VPOS >= 0 means stop at TO_X in the line at
8895 TO_VPOS, or at TO_POS, whichever comes first. */
8896 if (it->vpos == to_vpos)
8898 reached = 2;
8899 break;
8902 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8904 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8906 reached = 3;
8907 break;
8909 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8911 /* We have reached TO_X but not in the line we want. */
8912 skip = move_it_in_display_line_to (it, to_charpos,
8913 -1, MOVE_TO_POS);
8914 if (skip == MOVE_POS_MATCH_OR_ZV)
8916 reached = 4;
8917 break;
8922 else if (op & MOVE_TO_Y)
8924 struct it it_backup;
8926 if (it->line_wrap == WORD_WRAP)
8927 SAVE_IT (it_backup, *it, backup_data);
8929 /* TO_Y specified means stop at TO_X in the line containing
8930 TO_Y---or at TO_CHARPOS if this is reached first. The
8931 problem is that we can't really tell whether the line
8932 contains TO_Y before we have completely scanned it, and
8933 this may skip past TO_X. What we do is to first scan to
8934 TO_X.
8936 If TO_X is not specified, use a TO_X of zero. The reason
8937 is to make the outcome of this function more predictable.
8938 If we didn't use TO_X == 0, we would stop at the end of
8939 the line which is probably not what a caller would expect
8940 to happen. */
8941 skip = move_it_in_display_line_to
8942 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8943 (MOVE_TO_X | (op & MOVE_TO_POS)));
8945 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8946 if (skip == MOVE_POS_MATCH_OR_ZV)
8947 reached = 5;
8948 else if (skip == MOVE_X_REACHED)
8950 /* If TO_X was reached, we want to know whether TO_Y is
8951 in the line. We know this is the case if the already
8952 scanned glyphs make the line tall enough. Otherwise,
8953 we must check by scanning the rest of the line. */
8954 line_height = it->max_ascent + it->max_descent;
8955 if (to_y >= it->current_y
8956 && to_y < it->current_y + line_height)
8958 reached = 6;
8959 break;
8961 SAVE_IT (it_backup, *it, backup_data);
8962 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8963 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8964 op & MOVE_TO_POS);
8965 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8966 line_height = it->max_ascent + it->max_descent;
8967 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8969 if (to_y >= it->current_y
8970 && to_y < it->current_y + line_height)
8972 /* If TO_Y is in this line and TO_X was reached
8973 above, we scanned too far. We have to restore
8974 IT's settings to the ones before skipping. But
8975 keep the more accurate values of max_ascent and
8976 max_descent we've found while skipping the rest
8977 of the line, for the sake of callers, such as
8978 pos_visible_p, that need to know the line
8979 height. */
8980 int max_ascent = it->max_ascent;
8981 int max_descent = it->max_descent;
8983 RESTORE_IT (it, &it_backup, backup_data);
8984 it->max_ascent = max_ascent;
8985 it->max_descent = max_descent;
8986 reached = 6;
8988 else
8990 skip = skip2;
8991 if (skip == MOVE_POS_MATCH_OR_ZV)
8992 reached = 7;
8995 else
8997 /* Check whether TO_Y is in this line. */
8998 line_height = it->max_ascent + it->max_descent;
8999 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9001 if (to_y >= it->current_y
9002 && to_y < it->current_y + line_height)
9004 if (to_y > it->current_y)
9005 max_current_x = max (it->current_x, max_current_x);
9007 /* When word-wrap is on, TO_X may lie past the end
9008 of a wrapped line. Then it->current is the
9009 character on the next line, so backtrack to the
9010 space before the wrap point. */
9011 if (skip == MOVE_LINE_CONTINUED
9012 && it->line_wrap == WORD_WRAP)
9014 int prev_x = max (it->current_x - 1, 0);
9015 RESTORE_IT (it, &it_backup, backup_data);
9016 skip = move_it_in_display_line_to
9017 (it, -1, prev_x, MOVE_TO_X);
9020 reached = 6;
9024 if (reached)
9026 max_current_x = max (it->current_x, max_current_x);
9027 break;
9030 else if (BUFFERP (it->object)
9031 && (it->method == GET_FROM_BUFFER
9032 || it->method == GET_FROM_STRETCH)
9033 && IT_CHARPOS (*it) >= to_charpos
9034 /* Under bidi iteration, a call to set_iterator_to_next
9035 can scan far beyond to_charpos if the initial
9036 portion of the next line needs to be reordered. In
9037 that case, give move_it_in_display_line_to another
9038 chance below. */
9039 && !(it->bidi_p
9040 && it->bidi_it.scan_dir == -1))
9041 skip = MOVE_POS_MATCH_OR_ZV;
9042 else
9043 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9045 switch (skip)
9047 case MOVE_POS_MATCH_OR_ZV:
9048 max_current_x = max (it->current_x, max_current_x);
9049 reached = 8;
9050 goto out;
9052 case MOVE_NEWLINE_OR_CR:
9053 max_current_x = max (it->current_x, max_current_x);
9054 set_iterator_to_next (it, 1);
9055 it->continuation_lines_width = 0;
9056 break;
9058 case MOVE_LINE_TRUNCATED:
9059 max_current_x = it->last_visible_x;
9060 it->continuation_lines_width = 0;
9061 reseat_at_next_visible_line_start (it, 0);
9062 if ((op & MOVE_TO_POS) != 0
9063 && IT_CHARPOS (*it) > to_charpos)
9065 reached = 9;
9066 goto out;
9068 break;
9070 case MOVE_LINE_CONTINUED:
9071 max_current_x = it->last_visible_x;
9072 /* For continued lines ending in a tab, some of the glyphs
9073 associated with the tab are displayed on the current
9074 line. Since it->current_x does not include these glyphs,
9075 we use it->last_visible_x instead. */
9076 if (it->c == '\t')
9078 it->continuation_lines_width += it->last_visible_x;
9079 /* When moving by vpos, ensure that the iterator really
9080 advances to the next line (bug#847, bug#969). Fixme:
9081 do we need to do this in other circumstances? */
9082 if (it->current_x != it->last_visible_x
9083 && (op & MOVE_TO_VPOS)
9084 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9086 line_start_x = it->current_x + it->pixel_width
9087 - it->last_visible_x;
9088 set_iterator_to_next (it, 0);
9091 else
9092 it->continuation_lines_width += it->current_x;
9093 break;
9095 default:
9096 emacs_abort ();
9099 /* Reset/increment for the next run. */
9100 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9101 it->current_x = line_start_x;
9102 line_start_x = 0;
9103 it->hpos = 0;
9104 it->current_y += it->max_ascent + it->max_descent;
9105 ++it->vpos;
9106 last_height = it->max_ascent + it->max_descent;
9107 it->max_ascent = it->max_descent = 0;
9110 out:
9112 /* On text terminals, we may stop at the end of a line in the middle
9113 of a multi-character glyph. If the glyph itself is continued,
9114 i.e. it is actually displayed on the next line, don't treat this
9115 stopping point as valid; move to the next line instead (unless
9116 that brings us offscreen). */
9117 if (!FRAME_WINDOW_P (it->f)
9118 && op & MOVE_TO_POS
9119 && IT_CHARPOS (*it) == to_charpos
9120 && it->what == IT_CHARACTER
9121 && it->nglyphs > 1
9122 && it->line_wrap == WINDOW_WRAP
9123 && it->current_x == it->last_visible_x - 1
9124 && it->c != '\n'
9125 && it->c != '\t'
9126 && it->vpos < it->w->window_end_vpos)
9128 it->continuation_lines_width += it->current_x;
9129 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9130 it->current_y += it->max_ascent + it->max_descent;
9131 ++it->vpos;
9132 last_height = it->max_ascent + it->max_descent;
9135 if (backup_data)
9136 bidi_unshelve_cache (backup_data, 1);
9138 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9140 return max_current_x;
9144 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9146 If DY > 0, move IT backward at least that many pixels. DY = 0
9147 means move IT backward to the preceding line start or BEGV. This
9148 function may move over more than DY pixels if IT->current_y - DY
9149 ends up in the middle of a line; in this case IT->current_y will be
9150 set to the top of the line moved to. */
9152 void
9153 move_it_vertically_backward (struct it *it, int dy)
9155 int nlines, h;
9156 struct it it2, it3;
9157 void *it2data = NULL, *it3data = NULL;
9158 ptrdiff_t start_pos;
9159 int nchars_per_row
9160 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9161 ptrdiff_t pos_limit;
9163 move_further_back:
9164 eassert (dy >= 0);
9166 start_pos = IT_CHARPOS (*it);
9168 /* Estimate how many newlines we must move back. */
9169 nlines = max (1, dy / default_line_pixel_height (it->w));
9170 if (it->line_wrap == TRUNCATE)
9171 pos_limit = BEGV;
9172 else
9173 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9175 /* Set the iterator's position that many lines back. But don't go
9176 back more than NLINES full screen lines -- this wins a day with
9177 buffers which have very long lines. */
9178 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9179 back_to_previous_visible_line_start (it);
9181 /* Reseat the iterator here. When moving backward, we don't want
9182 reseat to skip forward over invisible text, set up the iterator
9183 to deliver from overlay strings at the new position etc. So,
9184 use reseat_1 here. */
9185 reseat_1 (it, it->current.pos, 1);
9187 /* We are now surely at a line start. */
9188 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9189 reordering is in effect. */
9190 it->continuation_lines_width = 0;
9192 /* Move forward and see what y-distance we moved. First move to the
9193 start of the next line so that we get its height. We need this
9194 height to be able to tell whether we reached the specified
9195 y-distance. */
9196 SAVE_IT (it2, *it, it2data);
9197 it2.max_ascent = it2.max_descent = 0;
9200 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9201 MOVE_TO_POS | MOVE_TO_VPOS);
9203 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9204 /* If we are in a display string which starts at START_POS,
9205 and that display string includes a newline, and we are
9206 right after that newline (i.e. at the beginning of a
9207 display line), exit the loop, because otherwise we will
9208 infloop, since move_it_to will see that it is already at
9209 START_POS and will not move. */
9210 || (it2.method == GET_FROM_STRING
9211 && IT_CHARPOS (it2) == start_pos
9212 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9213 eassert (IT_CHARPOS (*it) >= BEGV);
9214 SAVE_IT (it3, it2, it3data);
9216 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9217 eassert (IT_CHARPOS (*it) >= BEGV);
9218 /* H is the actual vertical distance from the position in *IT
9219 and the starting position. */
9220 h = it2.current_y - it->current_y;
9221 /* NLINES is the distance in number of lines. */
9222 nlines = it2.vpos - it->vpos;
9224 /* Correct IT's y and vpos position
9225 so that they are relative to the starting point. */
9226 it->vpos -= nlines;
9227 it->current_y -= h;
9229 if (dy == 0)
9231 /* DY == 0 means move to the start of the screen line. The
9232 value of nlines is > 0 if continuation lines were involved,
9233 or if the original IT position was at start of a line. */
9234 RESTORE_IT (it, it, it2data);
9235 if (nlines > 0)
9236 move_it_by_lines (it, nlines);
9237 /* The above code moves us to some position NLINES down,
9238 usually to its first glyph (leftmost in an L2R line), but
9239 that's not necessarily the start of the line, under bidi
9240 reordering. We want to get to the character position
9241 that is immediately after the newline of the previous
9242 line. */
9243 if (it->bidi_p
9244 && !it->continuation_lines_width
9245 && !STRINGP (it->string)
9246 && IT_CHARPOS (*it) > BEGV
9247 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9249 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9251 DEC_BOTH (cp, bp);
9252 cp = find_newline_no_quit (cp, bp, -1, NULL);
9253 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9255 bidi_unshelve_cache (it3data, 1);
9257 else
9259 /* The y-position we try to reach, relative to *IT.
9260 Note that H has been subtracted in front of the if-statement. */
9261 int target_y = it->current_y + h - dy;
9262 int y0 = it3.current_y;
9263 int y1;
9264 int line_height;
9266 RESTORE_IT (&it3, &it3, it3data);
9267 y1 = line_bottom_y (&it3);
9268 line_height = y1 - y0;
9269 RESTORE_IT (it, it, it2data);
9270 /* If we did not reach target_y, try to move further backward if
9271 we can. If we moved too far backward, try to move forward. */
9272 if (target_y < it->current_y
9273 /* This is heuristic. In a window that's 3 lines high, with
9274 a line height of 13 pixels each, recentering with point
9275 on the bottom line will try to move -39/2 = 19 pixels
9276 backward. Try to avoid moving into the first line. */
9277 && (it->current_y - target_y
9278 > min (window_box_height (it->w), line_height * 2 / 3))
9279 && IT_CHARPOS (*it) > BEGV)
9281 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9282 target_y - it->current_y));
9283 dy = it->current_y - target_y;
9284 goto move_further_back;
9286 else if (target_y >= it->current_y + line_height
9287 && IT_CHARPOS (*it) < ZV)
9289 /* Should move forward by at least one line, maybe more.
9291 Note: Calling move_it_by_lines can be expensive on
9292 terminal frames, where compute_motion is used (via
9293 vmotion) to do the job, when there are very long lines
9294 and truncate-lines is nil. That's the reason for
9295 treating terminal frames specially here. */
9297 if (!FRAME_WINDOW_P (it->f))
9298 move_it_vertically (it, target_y - (it->current_y + line_height));
9299 else
9303 move_it_by_lines (it, 1);
9305 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9312 /* Move IT by a specified amount of pixel lines DY. DY negative means
9313 move backwards. DY = 0 means move to start of screen line. At the
9314 end, IT will be on the start of a screen line. */
9316 void
9317 move_it_vertically (struct it *it, int dy)
9319 if (dy <= 0)
9320 move_it_vertically_backward (it, -dy);
9321 else
9323 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9324 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9325 MOVE_TO_POS | MOVE_TO_Y);
9326 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9328 /* If buffer ends in ZV without a newline, move to the start of
9329 the line to satisfy the post-condition. */
9330 if (IT_CHARPOS (*it) == ZV
9331 && ZV > BEGV
9332 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9333 move_it_by_lines (it, 0);
9338 /* Move iterator IT past the end of the text line it is in. */
9340 void
9341 move_it_past_eol (struct it *it)
9343 enum move_it_result rc;
9345 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9346 if (rc == MOVE_NEWLINE_OR_CR)
9347 set_iterator_to_next (it, 0);
9351 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9352 negative means move up. DVPOS == 0 means move to the start of the
9353 screen line.
9355 Optimization idea: If we would know that IT->f doesn't use
9356 a face with proportional font, we could be faster for
9357 truncate-lines nil. */
9359 void
9360 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9363 /* The commented-out optimization uses vmotion on terminals. This
9364 gives bad results, because elements like it->what, on which
9365 callers such as pos_visible_p rely, aren't updated. */
9366 /* struct position pos;
9367 if (!FRAME_WINDOW_P (it->f))
9369 struct text_pos textpos;
9371 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9372 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9373 reseat (it, textpos, 1);
9374 it->vpos += pos.vpos;
9375 it->current_y += pos.vpos;
9377 else */
9379 if (dvpos == 0)
9381 /* DVPOS == 0 means move to the start of the screen line. */
9382 move_it_vertically_backward (it, 0);
9383 /* Let next call to line_bottom_y calculate real line height. */
9384 last_height = 0;
9386 else if (dvpos > 0)
9388 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9389 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9391 /* Only move to the next buffer position if we ended up in a
9392 string from display property, not in an overlay string
9393 (before-string or after-string). That is because the
9394 latter don't conceal the underlying buffer position, so
9395 we can ask to move the iterator to the exact position we
9396 are interested in. Note that, even if we are already at
9397 IT_CHARPOS (*it), the call below is not a no-op, as it
9398 will detect that we are at the end of the string, pop the
9399 iterator, and compute it->current_x and it->hpos
9400 correctly. */
9401 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9402 -1, -1, -1, MOVE_TO_POS);
9405 else
9407 struct it it2;
9408 void *it2data = NULL;
9409 ptrdiff_t start_charpos, i;
9410 int nchars_per_row
9411 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9412 ptrdiff_t pos_limit;
9414 /* Start at the beginning of the screen line containing IT's
9415 position. This may actually move vertically backwards,
9416 in case of overlays, so adjust dvpos accordingly. */
9417 dvpos += it->vpos;
9418 move_it_vertically_backward (it, 0);
9419 dvpos -= it->vpos;
9421 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9422 screen lines, and reseat the iterator there. */
9423 start_charpos = IT_CHARPOS (*it);
9424 if (it->line_wrap == TRUNCATE)
9425 pos_limit = BEGV;
9426 else
9427 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9428 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9429 back_to_previous_visible_line_start (it);
9430 reseat (it, it->current.pos, 1);
9432 /* Move further back if we end up in a string or an image. */
9433 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9435 /* First try to move to start of display line. */
9436 dvpos += it->vpos;
9437 move_it_vertically_backward (it, 0);
9438 dvpos -= it->vpos;
9439 if (IT_POS_VALID_AFTER_MOVE_P (it))
9440 break;
9441 /* If start of line is still in string or image,
9442 move further back. */
9443 back_to_previous_visible_line_start (it);
9444 reseat (it, it->current.pos, 1);
9445 dvpos--;
9448 it->current_x = it->hpos = 0;
9450 /* Above call may have moved too far if continuation lines
9451 are involved. Scan forward and see if it did. */
9452 SAVE_IT (it2, *it, it2data);
9453 it2.vpos = it2.current_y = 0;
9454 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9455 it->vpos -= it2.vpos;
9456 it->current_y -= it2.current_y;
9457 it->current_x = it->hpos = 0;
9459 /* If we moved too far back, move IT some lines forward. */
9460 if (it2.vpos > -dvpos)
9462 int delta = it2.vpos + dvpos;
9464 RESTORE_IT (&it2, &it2, it2data);
9465 SAVE_IT (it2, *it, it2data);
9466 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9467 /* Move back again if we got too far ahead. */
9468 if (IT_CHARPOS (*it) >= start_charpos)
9469 RESTORE_IT (it, &it2, it2data);
9470 else
9471 bidi_unshelve_cache (it2data, 1);
9473 else
9474 RESTORE_IT (it, it, it2data);
9478 /* Return true if IT points into the middle of a display vector. */
9480 bool
9481 in_display_vector_p (struct it *it)
9483 return (it->method == GET_FROM_DISPLAY_VECTOR
9484 && it->current.dpvec_index > 0
9485 && it->dpvec + it->current.dpvec_index != it->dpend);
9488 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9489 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9490 WINDOW must be a live window and defaults to the selected one. The
9491 return value is a cons of the maximum pixel-width of any text line and
9492 the maximum pixel-height of all text lines.
9494 The optional argument FROM, if non-nil, specifies the first text
9495 position and defaults to the minimum accessible position of the buffer.
9496 If FROM is t, use the minimum accessible position that is not a newline
9497 character. TO, if non-nil, specifies the last text position and
9498 defaults to the maximum accessible position of the buffer. If TO is t,
9499 use the maximum accessible position that is not a newline character.
9501 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9502 width that can be returned. X_LIMIT nil or omitted, means to use the
9503 pixel-width of WINDOW's body; use this if you do not intend to change
9504 the width of WINDOW. Use the maximum width WINDOW may assume if you
9505 intend to change WINDOW's width.
9507 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9508 height that can be returned. Text lines whose y-coordinate is beyond
9509 Y_LIMIT are ignored. Since calculating the text height of a large
9510 buffer can take some time, it makes sense to specify this argument if
9511 the size of the buffer is unknown.
9513 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9514 include the height of the mode- or header-line of WINDOW in the return
9515 value. If it is either the symbol `mode-line' or `header-line', include
9516 only the height of that line, if present, in the return value. If t,
9517 include the height of both, if present, in the return value. */)
9518 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9519 Lisp_Object mode_and_header_line)
9521 struct window *w = decode_live_window (window);
9522 Lisp_Object buf;
9523 struct buffer *b;
9524 struct it it;
9525 struct buffer *old_buffer = NULL;
9526 ptrdiff_t start, end, pos;
9527 struct text_pos startp;
9528 void *itdata = NULL;
9529 int c, max_y = -1, x = 0, y = 0;
9531 buf = w->contents;
9532 CHECK_BUFFER (buf);
9533 b = XBUFFER (buf);
9535 if (b != current_buffer)
9537 old_buffer = current_buffer;
9538 set_buffer_internal (b);
9541 if (NILP (from))
9542 start = BEGV;
9543 else if (EQ (from, Qt))
9545 start = pos = BEGV;
9546 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9547 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9548 start = pos;
9549 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9550 start = pos;
9552 else
9554 CHECK_NUMBER_COERCE_MARKER (from);
9555 start = min (max (XINT (from), BEGV), ZV);
9558 if (NILP (to))
9559 end = ZV;
9560 else if (EQ (to, Qt))
9562 end = pos = ZV;
9563 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9564 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9565 end = pos;
9566 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9567 end = pos;
9569 else
9571 CHECK_NUMBER_COERCE_MARKER (to);
9572 end = max (start, min (XINT (to), ZV));
9575 if (!NILP (y_limit))
9577 CHECK_NUMBER (y_limit);
9578 max_y = min (XINT (y_limit), INT_MAX);
9581 itdata = bidi_shelve_cache ();
9582 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9583 start_display (&it, w, startp);
9585 if (NILP (x_limit))
9586 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9587 else
9589 CHECK_NUMBER (x_limit);
9590 it.last_visible_x = min (XINT (x_limit), INFINITY);
9591 /* Actually, we never want move_it_to stop at to_x. But to make
9592 sure that move_it_in_display_line_to always moves far enough,
9593 we set it to INT_MAX and specify MOVE_TO_X. */
9594 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9595 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9598 y = it.current_y + it.max_ascent + it.max_descent;
9600 if (!EQ (mode_and_header_line, Qheader_line)
9601 && !EQ (mode_and_header_line, Qt))
9602 /* Do not count the header-line which was counted automatically by
9603 start_display. */
9604 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9606 if (EQ (mode_and_header_line, Qmode_line)
9607 || EQ (mode_and_header_line, Qt))
9608 /* Do count the mode-line which is not included automatically by
9609 start_display. */
9610 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9612 bidi_unshelve_cache (itdata, 0);
9614 if (old_buffer)
9615 set_buffer_internal (old_buffer);
9617 return Fcons (make_number (x), make_number (y));
9620 /***********************************************************************
9621 Messages
9622 ***********************************************************************/
9625 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9626 to *Messages*. */
9628 void
9629 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9631 Lisp_Object args[3];
9632 Lisp_Object msg, fmt;
9633 char *buffer;
9634 ptrdiff_t len;
9635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9636 USE_SAFE_ALLOCA;
9638 fmt = msg = Qnil;
9639 GCPRO4 (fmt, msg, arg1, arg2);
9641 args[0] = fmt = build_string (format);
9642 args[1] = arg1;
9643 args[2] = arg2;
9644 msg = Fformat (3, args);
9646 len = SBYTES (msg) + 1;
9647 buffer = SAFE_ALLOCA (len);
9648 memcpy (buffer, SDATA (msg), len);
9650 message_dolog (buffer, len - 1, 1, 0);
9651 SAFE_FREE ();
9653 UNGCPRO;
9657 /* Output a newline in the *Messages* buffer if "needs" one. */
9659 void
9660 message_log_maybe_newline (void)
9662 if (message_log_need_newline)
9663 message_dolog ("", 0, 1, 0);
9667 /* Add a string M of length NBYTES to the message log, optionally
9668 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9669 true, means interpret the contents of M as multibyte. This
9670 function calls low-level routines in order to bypass text property
9671 hooks, etc. which might not be safe to run.
9673 This may GC (insert may run before/after change hooks),
9674 so the buffer M must NOT point to a Lisp string. */
9676 void
9677 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9679 const unsigned char *msg = (const unsigned char *) m;
9681 if (!NILP (Vmemory_full))
9682 return;
9684 if (!NILP (Vmessage_log_max))
9686 struct buffer *oldbuf;
9687 Lisp_Object oldpoint, oldbegv, oldzv;
9688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9689 ptrdiff_t point_at_end = 0;
9690 ptrdiff_t zv_at_end = 0;
9691 Lisp_Object old_deactivate_mark;
9692 struct gcpro gcpro1;
9694 old_deactivate_mark = Vdeactivate_mark;
9695 oldbuf = current_buffer;
9697 /* Ensure the Messages buffer exists, and switch to it.
9698 If we created it, set the major-mode. */
9700 int newbuffer = 0;
9701 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9703 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9705 if (newbuffer
9706 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9707 call0 (intern ("messages-buffer-mode"));
9710 bset_undo_list (current_buffer, Qt);
9711 bset_cache_long_scans (current_buffer, Qnil);
9713 oldpoint = message_dolog_marker1;
9714 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9715 oldbegv = message_dolog_marker2;
9716 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9717 oldzv = message_dolog_marker3;
9718 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9719 GCPRO1 (old_deactivate_mark);
9721 if (PT == Z)
9722 point_at_end = 1;
9723 if (ZV == Z)
9724 zv_at_end = 1;
9726 BEGV = BEG;
9727 BEGV_BYTE = BEG_BYTE;
9728 ZV = Z;
9729 ZV_BYTE = Z_BYTE;
9730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9732 /* Insert the string--maybe converting multibyte to single byte
9733 or vice versa, so that all the text fits the buffer. */
9734 if (multibyte
9735 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9737 ptrdiff_t i;
9738 int c, char_bytes;
9739 char work[1];
9741 /* Convert a multibyte string to single-byte
9742 for the *Message* buffer. */
9743 for (i = 0; i < nbytes; i += char_bytes)
9745 c = string_char_and_length (msg + i, &char_bytes);
9746 work[0] = (ASCII_CHAR_P (c)
9748 : multibyte_char_to_unibyte (c));
9749 insert_1_both (work, 1, 1, 1, 0, 0);
9752 else if (! multibyte
9753 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9755 ptrdiff_t i;
9756 int c, char_bytes;
9757 unsigned char str[MAX_MULTIBYTE_LENGTH];
9758 /* Convert a single-byte string to multibyte
9759 for the *Message* buffer. */
9760 for (i = 0; i < nbytes; i++)
9762 c = msg[i];
9763 MAKE_CHAR_MULTIBYTE (c);
9764 char_bytes = CHAR_STRING (c, str);
9765 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9768 else if (nbytes)
9769 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9771 if (nlflag)
9773 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9774 printmax_t dups;
9776 insert_1_both ("\n", 1, 1, 1, 0, 0);
9778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9779 this_bol = PT;
9780 this_bol_byte = PT_BYTE;
9782 /* See if this line duplicates the previous one.
9783 If so, combine duplicates. */
9784 if (this_bol > BEG)
9786 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9787 prev_bol = PT;
9788 prev_bol_byte = PT_BYTE;
9790 dups = message_log_check_duplicate (prev_bol_byte,
9791 this_bol_byte);
9792 if (dups)
9794 del_range_both (prev_bol, prev_bol_byte,
9795 this_bol, this_bol_byte, 0);
9796 if (dups > 1)
9798 char dupstr[sizeof " [ times]"
9799 + INT_STRLEN_BOUND (printmax_t)];
9801 /* If you change this format, don't forget to also
9802 change message_log_check_duplicate. */
9803 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9805 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9810 /* If we have more than the desired maximum number of lines
9811 in the *Messages* buffer now, delete the oldest ones.
9812 This is safe because we don't have undo in this buffer. */
9814 if (NATNUMP (Vmessage_log_max))
9816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9817 -XFASTINT (Vmessage_log_max) - 1, 0);
9818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9821 BEGV = marker_position (oldbegv);
9822 BEGV_BYTE = marker_byte_position (oldbegv);
9824 if (zv_at_end)
9826 ZV = Z;
9827 ZV_BYTE = Z_BYTE;
9829 else
9831 ZV = marker_position (oldzv);
9832 ZV_BYTE = marker_byte_position (oldzv);
9835 if (point_at_end)
9836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9837 else
9838 /* We can't do Fgoto_char (oldpoint) because it will run some
9839 Lisp code. */
9840 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9841 marker_byte_position (oldpoint));
9843 UNGCPRO;
9844 unchain_marker (XMARKER (oldpoint));
9845 unchain_marker (XMARKER (oldbegv));
9846 unchain_marker (XMARKER (oldzv));
9848 /* We called insert_1_both above with its 5th argument (PREPARE)
9849 zero, which prevents insert_1_both from calling
9850 prepare_to_modify_buffer, which in turns prevents us from
9851 incrementing windows_or_buffers_changed even if *Messages* is
9852 shown in some window. So we must manually set
9853 windows_or_buffers_changed here to make up for that. */
9854 windows_or_buffers_changed = old_windows_or_buffers_changed;
9855 bset_redisplay (current_buffer);
9857 set_buffer_internal (oldbuf);
9859 message_log_need_newline = !nlflag;
9860 Vdeactivate_mark = old_deactivate_mark;
9865 /* We are at the end of the buffer after just having inserted a newline.
9866 (Note: We depend on the fact we won't be crossing the gap.)
9867 Check to see if the most recent message looks a lot like the previous one.
9868 Return 0 if different, 1 if the new one should just replace it, or a
9869 value N > 1 if we should also append " [N times]". */
9871 static intmax_t
9872 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9874 ptrdiff_t i;
9875 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9876 int seen_dots = 0;
9877 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9878 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9880 for (i = 0; i < len; i++)
9882 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9883 seen_dots = 1;
9884 if (p1[i] != p2[i])
9885 return seen_dots;
9887 p1 += len;
9888 if (*p1 == '\n')
9889 return 2;
9890 if (*p1++ == ' ' && *p1++ == '[')
9892 char *pend;
9893 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9894 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9895 return n + 1;
9897 return 0;
9901 /* Display an echo area message M with a specified length of NBYTES
9902 bytes. The string may include null characters. If M is not a
9903 string, clear out any existing message, and let the mini-buffer
9904 text show through.
9906 This function cancels echoing. */
9908 void
9909 message3 (Lisp_Object m)
9911 struct gcpro gcpro1;
9913 GCPRO1 (m);
9914 clear_message (true, true);
9915 cancel_echoing ();
9917 /* First flush out any partial line written with print. */
9918 message_log_maybe_newline ();
9919 if (STRINGP (m))
9921 ptrdiff_t nbytes = SBYTES (m);
9922 bool multibyte = STRING_MULTIBYTE (m);
9923 USE_SAFE_ALLOCA;
9924 char *buffer = SAFE_ALLOCA (nbytes);
9925 memcpy (buffer, SDATA (m), nbytes);
9926 message_dolog (buffer, nbytes, 1, multibyte);
9927 SAFE_FREE ();
9929 message3_nolog (m);
9931 UNGCPRO;
9935 /* The non-logging version of message3.
9936 This does not cancel echoing, because it is used for echoing.
9937 Perhaps we need to make a separate function for echoing
9938 and make this cancel echoing. */
9940 void
9941 message3_nolog (Lisp_Object m)
9943 struct frame *sf = SELECTED_FRAME ();
9945 if (FRAME_INITIAL_P (sf))
9947 if (noninteractive_need_newline)
9948 putc ('\n', stderr);
9949 noninteractive_need_newline = 0;
9950 if (STRINGP (m))
9952 Lisp_Object s = ENCODE_SYSTEM (m);
9954 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9956 if (cursor_in_echo_area == 0)
9957 fprintf (stderr, "\n");
9958 fflush (stderr);
9960 /* Error messages get reported properly by cmd_error, so this must be just an
9961 informative message; if the frame hasn't really been initialized yet, just
9962 toss it. */
9963 else if (INTERACTIVE && sf->glyphs_initialized_p)
9965 /* Get the frame containing the mini-buffer
9966 that the selected frame is using. */
9967 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9968 Lisp_Object frame = XWINDOW (mini_window)->frame;
9969 struct frame *f = XFRAME (frame);
9971 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9972 Fmake_frame_visible (frame);
9974 if (STRINGP (m) && SCHARS (m) > 0)
9976 set_message (m);
9977 if (minibuffer_auto_raise)
9978 Fraise_frame (frame);
9979 /* Assume we are not echoing.
9980 (If we are, echo_now will override this.) */
9981 echo_message_buffer = Qnil;
9983 else
9984 clear_message (true, true);
9986 do_pending_window_change (0);
9987 echo_area_display (1);
9988 do_pending_window_change (0);
9989 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9990 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9995 /* Display a null-terminated echo area message M. If M is 0, clear
9996 out any existing message, and let the mini-buffer text show through.
9998 The buffer M must continue to exist until after the echo area gets
9999 cleared or some other message gets displayed there. Do not pass
10000 text that is stored in a Lisp string. Do not pass text in a buffer
10001 that was alloca'd. */
10003 void
10004 message1 (const char *m)
10006 message3 (m ? build_unibyte_string (m) : Qnil);
10010 /* The non-logging counterpart of message1. */
10012 void
10013 message1_nolog (const char *m)
10015 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10018 /* Display a message M which contains a single %s
10019 which gets replaced with STRING. */
10021 void
10022 message_with_string (const char *m, Lisp_Object string, int log)
10024 CHECK_STRING (string);
10026 if (noninteractive)
10028 if (m)
10030 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10031 String whose data pointer might be passed to us in M. So
10032 we use a local copy. */
10033 char *fmt = xstrdup (m);
10035 if (noninteractive_need_newline)
10036 putc ('\n', stderr);
10037 noninteractive_need_newline = 0;
10038 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10039 if (!cursor_in_echo_area)
10040 fprintf (stderr, "\n");
10041 fflush (stderr);
10042 xfree (fmt);
10045 else if (INTERACTIVE)
10047 /* The frame whose minibuffer we're going to display the message on.
10048 It may be larger than the selected frame, so we need
10049 to use its buffer, not the selected frame's buffer. */
10050 Lisp_Object mini_window;
10051 struct frame *f, *sf = SELECTED_FRAME ();
10053 /* Get the frame containing the minibuffer
10054 that the selected frame is using. */
10055 mini_window = FRAME_MINIBUF_WINDOW (sf);
10056 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10058 /* Error messages get reported properly by cmd_error, so this must be
10059 just an informative message; if the frame hasn't really been
10060 initialized yet, just toss it. */
10061 if (f->glyphs_initialized_p)
10063 Lisp_Object args[2], msg;
10064 struct gcpro gcpro1, gcpro2;
10066 args[0] = build_string (m);
10067 args[1] = msg = string;
10068 GCPRO2 (args[0], msg);
10069 gcpro1.nvars = 2;
10071 msg = Fformat (2, args);
10073 if (log)
10074 message3 (msg);
10075 else
10076 message3_nolog (msg);
10078 UNGCPRO;
10080 /* Print should start at the beginning of the message
10081 buffer next time. */
10082 message_buf_print = 0;
10088 /* Dump an informative message to the minibuf. If M is 0, clear out
10089 any existing message, and let the mini-buffer text show through. */
10091 static void
10092 vmessage (const char *m, va_list ap)
10094 if (noninteractive)
10096 if (m)
10098 if (noninteractive_need_newline)
10099 putc ('\n', stderr);
10100 noninteractive_need_newline = 0;
10101 vfprintf (stderr, m, ap);
10102 if (cursor_in_echo_area == 0)
10103 fprintf (stderr, "\n");
10104 fflush (stderr);
10107 else if (INTERACTIVE)
10109 /* The frame whose mini-buffer we're going to display the message
10110 on. It may be larger than the selected frame, so we need to
10111 use its buffer, not the selected frame's buffer. */
10112 Lisp_Object mini_window;
10113 struct frame *f, *sf = SELECTED_FRAME ();
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10120 /* Error messages get reported properly by cmd_error, so this must be
10121 just an informative message; if the frame hasn't really been
10122 initialized yet, just toss it. */
10123 if (f->glyphs_initialized_p)
10125 if (m)
10127 ptrdiff_t len;
10128 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10129 char *message_buf = alloca (maxsize + 1);
10131 len = doprnt (message_buf, maxsize, m, 0, ap);
10133 message3 (make_string (message_buf, len));
10135 else
10136 message1 (0);
10138 /* Print should start at the beginning of the message
10139 buffer next time. */
10140 message_buf_print = 0;
10145 void
10146 message (const char *m, ...)
10148 va_list ap;
10149 va_start (ap, m);
10150 vmessage (m, ap);
10151 va_end (ap);
10155 #if 0
10156 /* The non-logging version of message. */
10158 void
10159 message_nolog (const char *m, ...)
10161 Lisp_Object old_log_max;
10162 va_list ap;
10163 va_start (ap, m);
10164 old_log_max = Vmessage_log_max;
10165 Vmessage_log_max = Qnil;
10166 vmessage (m, ap);
10167 Vmessage_log_max = old_log_max;
10168 va_end (ap);
10170 #endif
10173 /* Display the current message in the current mini-buffer. This is
10174 only called from error handlers in process.c, and is not time
10175 critical. */
10177 void
10178 update_echo_area (void)
10180 if (!NILP (echo_area_buffer[0]))
10182 Lisp_Object string;
10183 string = Fcurrent_message ();
10184 message3 (string);
10189 /* Make sure echo area buffers in `echo_buffers' are live.
10190 If they aren't, make new ones. */
10192 static void
10193 ensure_echo_area_buffers (void)
10195 int i;
10197 for (i = 0; i < 2; ++i)
10198 if (!BUFFERP (echo_buffer[i])
10199 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10201 char name[30];
10202 Lisp_Object old_buffer;
10203 int j;
10205 old_buffer = echo_buffer[i];
10206 echo_buffer[i] = Fget_buffer_create
10207 (make_formatted_string (name, " *Echo Area %d*", i));
10208 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10209 /* to force word wrap in echo area -
10210 it was decided to postpone this*/
10211 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10213 for (j = 0; j < 2; ++j)
10214 if (EQ (old_buffer, echo_area_buffer[j]))
10215 echo_area_buffer[j] = echo_buffer[i];
10220 /* Call FN with args A1..A2 with either the current or last displayed
10221 echo_area_buffer as current buffer.
10223 WHICH zero means use the current message buffer
10224 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10225 from echo_buffer[] and clear it.
10227 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10228 suitable buffer from echo_buffer[] and clear it.
10230 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10231 that the current message becomes the last displayed one, make
10232 choose a suitable buffer for echo_area_buffer[0], and clear it.
10234 Value is what FN returns. */
10236 static int
10237 with_echo_area_buffer (struct window *w, int which,
10238 int (*fn) (ptrdiff_t, Lisp_Object),
10239 ptrdiff_t a1, Lisp_Object a2)
10241 Lisp_Object buffer;
10242 int this_one, the_other, clear_buffer_p, rc;
10243 ptrdiff_t count = SPECPDL_INDEX ();
10245 /* If buffers aren't live, make new ones. */
10246 ensure_echo_area_buffers ();
10248 clear_buffer_p = 0;
10250 if (which == 0)
10251 this_one = 0, the_other = 1;
10252 else if (which > 0)
10253 this_one = 1, the_other = 0;
10254 else
10256 this_one = 0, the_other = 1;
10257 clear_buffer_p = true;
10259 /* We need a fresh one in case the current echo buffer equals
10260 the one containing the last displayed echo area message. */
10261 if (!NILP (echo_area_buffer[this_one])
10262 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10263 echo_area_buffer[this_one] = Qnil;
10266 /* Choose a suitable buffer from echo_buffer[] is we don't
10267 have one. */
10268 if (NILP (echo_area_buffer[this_one]))
10270 echo_area_buffer[this_one]
10271 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10272 ? echo_buffer[the_other]
10273 : echo_buffer[this_one]);
10274 clear_buffer_p = true;
10277 buffer = echo_area_buffer[this_one];
10279 /* Don't get confused by reusing the buffer used for echoing
10280 for a different purpose. */
10281 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10282 cancel_echoing ();
10284 record_unwind_protect (unwind_with_echo_area_buffer,
10285 with_echo_area_buffer_unwind_data (w));
10287 /* Make the echo area buffer current. Note that for display
10288 purposes, it is not necessary that the displayed window's buffer
10289 == current_buffer, except for text property lookup. So, let's
10290 only set that buffer temporarily here without doing a full
10291 Fset_window_buffer. We must also change w->pointm, though,
10292 because otherwise an assertions in unshow_buffer fails, and Emacs
10293 aborts. */
10294 set_buffer_internal_1 (XBUFFER (buffer));
10295 if (w)
10297 wset_buffer (w, buffer);
10298 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10301 bset_undo_list (current_buffer, Qt);
10302 bset_read_only (current_buffer, Qnil);
10303 specbind (Qinhibit_read_only, Qt);
10304 specbind (Qinhibit_modification_hooks, Qt);
10306 if (clear_buffer_p && Z > BEG)
10307 del_range (BEG, Z);
10309 eassert (BEGV >= BEG);
10310 eassert (ZV <= Z && ZV >= BEGV);
10312 rc = fn (a1, a2);
10314 eassert (BEGV >= BEG);
10315 eassert (ZV <= Z && ZV >= BEGV);
10317 unbind_to (count, Qnil);
10318 return rc;
10322 /* Save state that should be preserved around the call to the function
10323 FN called in with_echo_area_buffer. */
10325 static Lisp_Object
10326 with_echo_area_buffer_unwind_data (struct window *w)
10328 int i = 0;
10329 Lisp_Object vector, tmp;
10331 /* Reduce consing by keeping one vector in
10332 Vwith_echo_area_save_vector. */
10333 vector = Vwith_echo_area_save_vector;
10334 Vwith_echo_area_save_vector = Qnil;
10336 if (NILP (vector))
10337 vector = Fmake_vector (make_number (9), Qnil);
10339 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10340 ASET (vector, i, Vdeactivate_mark); ++i;
10341 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10343 if (w)
10345 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10346 ASET (vector, i, w->contents); ++i;
10347 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10348 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10349 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10352 else
10354 int end = i + 6;
10355 for (; i < end; ++i)
10356 ASET (vector, i, Qnil);
10359 eassert (i == ASIZE (vector));
10360 return vector;
10364 /* Restore global state from VECTOR which was created by
10365 with_echo_area_buffer_unwind_data. */
10367 static void
10368 unwind_with_echo_area_buffer (Lisp_Object vector)
10370 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10371 Vdeactivate_mark = AREF (vector, 1);
10372 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10374 if (WINDOWP (AREF (vector, 3)))
10376 struct window *w;
10377 Lisp_Object buffer;
10379 w = XWINDOW (AREF (vector, 3));
10380 buffer = AREF (vector, 4);
10382 wset_buffer (w, buffer);
10383 set_marker_both (w->pointm, buffer,
10384 XFASTINT (AREF (vector, 5)),
10385 XFASTINT (AREF (vector, 6)));
10386 set_marker_both (w->start, buffer,
10387 XFASTINT (AREF (vector, 7)),
10388 XFASTINT (AREF (vector, 8)));
10391 Vwith_echo_area_save_vector = vector;
10395 /* Set up the echo area for use by print functions. MULTIBYTE_P
10396 non-zero means we will print multibyte. */
10398 void
10399 setup_echo_area_for_printing (int multibyte_p)
10401 /* If we can't find an echo area any more, exit. */
10402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10403 Fkill_emacs (Qnil);
10405 ensure_echo_area_buffers ();
10407 if (!message_buf_print)
10409 /* A message has been output since the last time we printed.
10410 Choose a fresh echo area buffer. */
10411 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10412 echo_area_buffer[0] = echo_buffer[1];
10413 else
10414 echo_area_buffer[0] = echo_buffer[0];
10416 /* Switch to that buffer and clear it. */
10417 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10418 bset_truncate_lines (current_buffer, Qnil);
10420 if (Z > BEG)
10422 ptrdiff_t count = SPECPDL_INDEX ();
10423 specbind (Qinhibit_read_only, Qt);
10424 /* Note that undo recording is always disabled. */
10425 del_range (BEG, Z);
10426 unbind_to (count, Qnil);
10428 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10430 /* Set up the buffer for the multibyteness we need. */
10431 if (multibyte_p
10432 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10433 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10435 /* Raise the frame containing the echo area. */
10436 if (minibuffer_auto_raise)
10438 struct frame *sf = SELECTED_FRAME ();
10439 Lisp_Object mini_window;
10440 mini_window = FRAME_MINIBUF_WINDOW (sf);
10441 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10444 message_log_maybe_newline ();
10445 message_buf_print = 1;
10447 else
10449 if (NILP (echo_area_buffer[0]))
10451 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10452 echo_area_buffer[0] = echo_buffer[1];
10453 else
10454 echo_area_buffer[0] = echo_buffer[0];
10457 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10459 /* Someone switched buffers between print requests. */
10460 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10461 bset_truncate_lines (current_buffer, Qnil);
10467 /* Display an echo area message in window W. Value is non-zero if W's
10468 height is changed. If display_last_displayed_message_p is
10469 non-zero, display the message that was last displayed, otherwise
10470 display the current message. */
10472 static int
10473 display_echo_area (struct window *w)
10475 int i, no_message_p, window_height_changed_p;
10477 /* Temporarily disable garbage collections while displaying the echo
10478 area. This is done because a GC can print a message itself.
10479 That message would modify the echo area buffer's contents while a
10480 redisplay of the buffer is going on, and seriously confuse
10481 redisplay. */
10482 ptrdiff_t count = inhibit_garbage_collection ();
10484 /* If there is no message, we must call display_echo_area_1
10485 nevertheless because it resizes the window. But we will have to
10486 reset the echo_area_buffer in question to nil at the end because
10487 with_echo_area_buffer will sets it to an empty buffer. */
10488 i = display_last_displayed_message_p ? 1 : 0;
10489 no_message_p = NILP (echo_area_buffer[i]);
10491 window_height_changed_p
10492 = with_echo_area_buffer (w, display_last_displayed_message_p,
10493 display_echo_area_1,
10494 (intptr_t) w, Qnil);
10496 if (no_message_p)
10497 echo_area_buffer[i] = Qnil;
10499 unbind_to (count, Qnil);
10500 return window_height_changed_p;
10504 /* Helper for display_echo_area. Display the current buffer which
10505 contains the current echo area message in window W, a mini-window,
10506 a pointer to which is passed in A1. A2..A4 are currently not used.
10507 Change the height of W so that all of the message is displayed.
10508 Value is non-zero if height of W was changed. */
10510 static int
10511 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10513 intptr_t i1 = a1;
10514 struct window *w = (struct window *) i1;
10515 Lisp_Object window;
10516 struct text_pos start;
10517 int window_height_changed_p = 0;
10519 /* Do this before displaying, so that we have a large enough glyph
10520 matrix for the display. If we can't get enough space for the
10521 whole text, display the last N lines. That works by setting w->start. */
10522 window_height_changed_p = resize_mini_window (w, 0);
10524 /* Use the starting position chosen by resize_mini_window. */
10525 SET_TEXT_POS_FROM_MARKER (start, w->start);
10527 /* Display. */
10528 clear_glyph_matrix (w->desired_matrix);
10529 XSETWINDOW (window, w);
10530 try_window (window, start, 0);
10532 return window_height_changed_p;
10536 /* Resize the echo area window to exactly the size needed for the
10537 currently displayed message, if there is one. If a mini-buffer
10538 is active, don't shrink it. */
10540 void
10541 resize_echo_area_exactly (void)
10543 if (BUFFERP (echo_area_buffer[0])
10544 && WINDOWP (echo_area_window))
10546 struct window *w = XWINDOW (echo_area_window);
10547 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10548 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10549 (intptr_t) w, resize_exactly);
10550 if (resized_p)
10552 windows_or_buffers_changed = 42;
10553 update_mode_lines = 30;
10554 redisplay_internal ();
10560 /* Callback function for with_echo_area_buffer, when used from
10561 resize_echo_area_exactly. A1 contains a pointer to the window to
10562 resize, EXACTLY non-nil means resize the mini-window exactly to the
10563 size of the text displayed. A3 and A4 are not used. Value is what
10564 resize_mini_window returns. */
10566 static int
10567 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10569 intptr_t i1 = a1;
10570 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10574 /* Resize mini-window W to fit the size of its contents. EXACT_P
10575 means size the window exactly to the size needed. Otherwise, it's
10576 only enlarged until W's buffer is empty.
10578 Set W->start to the right place to begin display. If the whole
10579 contents fit, start at the beginning. Otherwise, start so as
10580 to make the end of the contents appear. This is particularly
10581 important for y-or-n-p, but seems desirable generally.
10583 Value is non-zero if the window height has been changed. */
10586 resize_mini_window (struct window *w, int exact_p)
10588 struct frame *f = XFRAME (w->frame);
10589 int window_height_changed_p = 0;
10591 eassert (MINI_WINDOW_P (w));
10593 /* By default, start display at the beginning. */
10594 set_marker_both (w->start, w->contents,
10595 BUF_BEGV (XBUFFER (w->contents)),
10596 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10598 /* Don't resize windows while redisplaying a window; it would
10599 confuse redisplay functions when the size of the window they are
10600 displaying changes from under them. Such a resizing can happen,
10601 for instance, when which-func prints a long message while
10602 we are running fontification-functions. We're running these
10603 functions with safe_call which binds inhibit-redisplay to t. */
10604 if (!NILP (Vinhibit_redisplay))
10605 return 0;
10607 /* Nil means don't try to resize. */
10608 if (NILP (Vresize_mini_windows)
10609 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10610 return 0;
10612 if (!FRAME_MINIBUF_ONLY_P (f))
10614 struct it it;
10615 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10616 + WINDOW_PIXEL_HEIGHT (w));
10617 int unit = FRAME_LINE_HEIGHT (f);
10618 int height, max_height;
10619 struct text_pos start;
10620 struct buffer *old_current_buffer = NULL;
10622 if (current_buffer != XBUFFER (w->contents))
10624 old_current_buffer = current_buffer;
10625 set_buffer_internal (XBUFFER (w->contents));
10628 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10630 /* Compute the max. number of lines specified by the user. */
10631 if (FLOATP (Vmax_mini_window_height))
10632 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10633 else if (INTEGERP (Vmax_mini_window_height))
10634 max_height = XINT (Vmax_mini_window_height) * unit;
10635 else
10636 max_height = total_height / 4;
10638 /* Correct that max. height if it's bogus. */
10639 max_height = clip_to_bounds (unit, max_height, total_height);
10641 /* Find out the height of the text in the window. */
10642 if (it.line_wrap == TRUNCATE)
10643 height = unit;
10644 else
10646 last_height = 0;
10647 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10648 if (it.max_ascent == 0 && it.max_descent == 0)
10649 height = it.current_y + last_height;
10650 else
10651 height = it.current_y + it.max_ascent + it.max_descent;
10652 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10655 /* Compute a suitable window start. */
10656 if (height > max_height)
10658 height = (max_height / unit) * unit;
10659 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10660 move_it_vertically_backward (&it, height - unit);
10661 start = it.current.pos;
10663 else
10664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10665 SET_MARKER_FROM_TEXT_POS (w->start, start);
10667 if (EQ (Vresize_mini_windows, Qgrow_only))
10669 /* Let it grow only, until we display an empty message, in which
10670 case the window shrinks again. */
10671 if (height > WINDOW_PIXEL_HEIGHT (w))
10673 int old_height = WINDOW_PIXEL_HEIGHT (w);
10675 FRAME_WINDOWS_FROZEN (f) = 1;
10676 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10677 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10679 else if (height < WINDOW_PIXEL_HEIGHT (w)
10680 && (exact_p || BEGV == ZV))
10682 int old_height = WINDOW_PIXEL_HEIGHT (w);
10684 FRAME_WINDOWS_FROZEN (f) = 0;
10685 shrink_mini_window (w, 1);
10686 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10689 else
10691 /* Always resize to exact size needed. */
10692 if (height > WINDOW_PIXEL_HEIGHT (w))
10694 int old_height = WINDOW_PIXEL_HEIGHT (w);
10696 FRAME_WINDOWS_FROZEN (f) = 1;
10697 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10698 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10700 else if (height < WINDOW_PIXEL_HEIGHT (w))
10702 int old_height = WINDOW_PIXEL_HEIGHT (w);
10704 FRAME_WINDOWS_FROZEN (f) = 0;
10705 shrink_mini_window (w, 1);
10707 if (height)
10709 FRAME_WINDOWS_FROZEN (f) = 1;
10710 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10713 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10717 if (old_current_buffer)
10718 set_buffer_internal (old_current_buffer);
10721 return window_height_changed_p;
10725 /* Value is the current message, a string, or nil if there is no
10726 current message. */
10728 Lisp_Object
10729 current_message (void)
10731 Lisp_Object msg;
10733 if (!BUFFERP (echo_area_buffer[0]))
10734 msg = Qnil;
10735 else
10737 with_echo_area_buffer (0, 0, current_message_1,
10738 (intptr_t) &msg, Qnil);
10739 if (NILP (msg))
10740 echo_area_buffer[0] = Qnil;
10743 return msg;
10747 static int
10748 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10750 intptr_t i1 = a1;
10751 Lisp_Object *msg = (Lisp_Object *) i1;
10753 if (Z > BEG)
10754 *msg = make_buffer_string (BEG, Z, 1);
10755 else
10756 *msg = Qnil;
10757 return 0;
10761 /* Push the current message on Vmessage_stack for later restoration
10762 by restore_message. Value is non-zero if the current message isn't
10763 empty. This is a relatively infrequent operation, so it's not
10764 worth optimizing. */
10766 bool
10767 push_message (void)
10769 Lisp_Object msg = current_message ();
10770 Vmessage_stack = Fcons (msg, Vmessage_stack);
10771 return STRINGP (msg);
10775 /* Restore message display from the top of Vmessage_stack. */
10777 void
10778 restore_message (void)
10780 eassert (CONSP (Vmessage_stack));
10781 message3_nolog (XCAR (Vmessage_stack));
10785 /* Handler for unwind-protect calling pop_message. */
10787 void
10788 pop_message_unwind (void)
10790 /* Pop the top-most entry off Vmessage_stack. */
10791 eassert (CONSP (Vmessage_stack));
10792 Vmessage_stack = XCDR (Vmessage_stack);
10796 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10797 exits. If the stack is not empty, we have a missing pop_message
10798 somewhere. */
10800 void
10801 check_message_stack (void)
10803 if (!NILP (Vmessage_stack))
10804 emacs_abort ();
10808 /* Truncate to NCHARS what will be displayed in the echo area the next
10809 time we display it---but don't redisplay it now. */
10811 void
10812 truncate_echo_area (ptrdiff_t nchars)
10814 if (nchars == 0)
10815 echo_area_buffer[0] = Qnil;
10816 else if (!noninteractive
10817 && INTERACTIVE
10818 && !NILP (echo_area_buffer[0]))
10820 struct frame *sf = SELECTED_FRAME ();
10821 /* Error messages get reported properly by cmd_error, so this must be
10822 just an informative message; if the frame hasn't really been
10823 initialized yet, just toss it. */
10824 if (sf->glyphs_initialized_p)
10825 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10830 /* Helper function for truncate_echo_area. Truncate the current
10831 message to at most NCHARS characters. */
10833 static int
10834 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10836 if (BEG + nchars < Z)
10837 del_range (BEG + nchars, Z);
10838 if (Z == BEG)
10839 echo_area_buffer[0] = Qnil;
10840 return 0;
10843 /* Set the current message to STRING. */
10845 static void
10846 set_message (Lisp_Object string)
10848 eassert (STRINGP (string));
10850 message_enable_multibyte = STRING_MULTIBYTE (string);
10852 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10853 message_buf_print = 0;
10854 help_echo_showing_p = 0;
10856 if (STRINGP (Vdebug_on_message)
10857 && STRINGP (string)
10858 && fast_string_match (Vdebug_on_message, string) >= 0)
10859 call_debugger (list2 (Qerror, string));
10863 /* Helper function for set_message. First argument is ignored and second
10864 argument has the same meaning as for set_message.
10865 This function is called with the echo area buffer being current. */
10867 static int
10868 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10870 eassert (STRINGP (string));
10872 /* Change multibyteness of the echo buffer appropriately. */
10873 if (message_enable_multibyte
10874 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10875 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10877 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10878 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10879 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10881 /* Insert new message at BEG. */
10882 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10884 /* This function takes care of single/multibyte conversion.
10885 We just have to ensure that the echo area buffer has the right
10886 setting of enable_multibyte_characters. */
10887 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10889 return 0;
10893 /* Clear messages. CURRENT_P non-zero means clear the current
10894 message. LAST_DISPLAYED_P non-zero means clear the message
10895 last displayed. */
10897 void
10898 clear_message (bool current_p, bool last_displayed_p)
10900 if (current_p)
10902 echo_area_buffer[0] = Qnil;
10903 message_cleared_p = true;
10906 if (last_displayed_p)
10907 echo_area_buffer[1] = Qnil;
10909 message_buf_print = 0;
10912 /* Clear garbaged frames.
10914 This function is used where the old redisplay called
10915 redraw_garbaged_frames which in turn called redraw_frame which in
10916 turn called clear_frame. The call to clear_frame was a source of
10917 flickering. I believe a clear_frame is not necessary. It should
10918 suffice in the new redisplay to invalidate all current matrices,
10919 and ensure a complete redisplay of all windows. */
10921 static void
10922 clear_garbaged_frames (void)
10924 if (frame_garbaged)
10926 Lisp_Object tail, frame;
10928 FOR_EACH_FRAME (tail, frame)
10930 struct frame *f = XFRAME (frame);
10932 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10934 if (f->resized_p)
10935 redraw_frame (f);
10936 else
10937 clear_current_matrices (f);
10938 fset_redisplay (f);
10939 f->garbaged = false;
10940 f->resized_p = false;
10944 frame_garbaged = false;
10949 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10950 is non-zero update selected_frame. Value is non-zero if the
10951 mini-windows height has been changed. */
10953 static int
10954 echo_area_display (int update_frame_p)
10956 Lisp_Object mini_window;
10957 struct window *w;
10958 struct frame *f;
10959 int window_height_changed_p = 0;
10960 struct frame *sf = SELECTED_FRAME ();
10962 mini_window = FRAME_MINIBUF_WINDOW (sf);
10963 w = XWINDOW (mini_window);
10964 f = XFRAME (WINDOW_FRAME (w));
10966 /* Don't display if frame is invisible or not yet initialized. */
10967 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10968 return 0;
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 /* When Emacs starts, selected_frame may be the initial terminal
10972 frame. If we let this through, a message would be displayed on
10973 the terminal. */
10974 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10975 return 0;
10976 #endif /* HAVE_WINDOW_SYSTEM */
10978 /* Redraw garbaged frames. */
10979 clear_garbaged_frames ();
10981 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10983 echo_area_window = mini_window;
10984 window_height_changed_p = display_echo_area (w);
10985 w->must_be_updated_p = true;
10987 /* Update the display, unless called from redisplay_internal.
10988 Also don't update the screen during redisplay itself. The
10989 update will happen at the end of redisplay, and an update
10990 here could cause confusion. */
10991 if (update_frame_p && !redisplaying_p)
10993 int n = 0;
10995 /* If the display update has been interrupted by pending
10996 input, update mode lines in the frame. Due to the
10997 pending input, it might have been that redisplay hasn't
10998 been called, so that mode lines above the echo area are
10999 garbaged. This looks odd, so we prevent it here. */
11000 if (!display_completed)
11001 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11003 if (window_height_changed_p
11004 /* Don't do this if Emacs is shutting down. Redisplay
11005 needs to run hooks. */
11006 && !NILP (Vrun_hooks))
11008 /* Must update other windows. Likewise as in other
11009 cases, don't let this update be interrupted by
11010 pending input. */
11011 ptrdiff_t count = SPECPDL_INDEX ();
11012 specbind (Qredisplay_dont_pause, Qt);
11013 windows_or_buffers_changed = 44;
11014 redisplay_internal ();
11015 unbind_to (count, Qnil);
11017 else if (FRAME_WINDOW_P (f) && n == 0)
11019 /* Window configuration is the same as before.
11020 Can do with a display update of the echo area,
11021 unless we displayed some mode lines. */
11022 update_single_window (w, 1);
11023 flush_frame (f);
11025 else
11026 update_frame (f, 1, 1);
11028 /* If cursor is in the echo area, make sure that the next
11029 redisplay displays the minibuffer, so that the cursor will
11030 be replaced with what the minibuffer wants. */
11031 if (cursor_in_echo_area)
11032 wset_redisplay (XWINDOW (mini_window));
11035 else if (!EQ (mini_window, selected_window))
11036 wset_redisplay (XWINDOW (mini_window));
11038 /* Last displayed message is now the current message. */
11039 echo_area_buffer[1] = echo_area_buffer[0];
11040 /* Inform read_char that we're not echoing. */
11041 echo_message_buffer = Qnil;
11043 /* Prevent redisplay optimization in redisplay_internal by resetting
11044 this_line_start_pos. This is done because the mini-buffer now
11045 displays the message instead of its buffer text. */
11046 if (EQ (mini_window, selected_window))
11047 CHARPOS (this_line_start_pos) = 0;
11049 return window_height_changed_p;
11052 /* Nonzero if W's buffer was changed but not saved. */
11054 static int
11055 window_buffer_changed (struct window *w)
11057 struct buffer *b = XBUFFER (w->contents);
11059 eassert (BUFFER_LIVE_P (b));
11061 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11064 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11066 static int
11067 mode_line_update_needed (struct window *w)
11069 return (w->column_number_displayed != -1
11070 && !(PT == w->last_point && !window_outdated (w))
11071 && (w->column_number_displayed != current_column ()));
11074 /* Nonzero if window start of W is frozen and may not be changed during
11075 redisplay. */
11077 static bool
11078 window_frozen_p (struct window *w)
11080 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11082 Lisp_Object window;
11084 XSETWINDOW (window, w);
11085 if (MINI_WINDOW_P (w))
11086 return 0;
11087 else if (EQ (window, selected_window))
11088 return 0;
11089 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11090 && EQ (window, Vminibuf_scroll_window))
11091 /* This special window can't be frozen too. */
11092 return 0;
11093 else
11094 return 1;
11096 return 0;
11099 /***********************************************************************
11100 Mode Lines and Frame Titles
11101 ***********************************************************************/
11103 /* A buffer for constructing non-propertized mode-line strings and
11104 frame titles in it; allocated from the heap in init_xdisp and
11105 resized as needed in store_mode_line_noprop_char. */
11107 static char *mode_line_noprop_buf;
11109 /* The buffer's end, and a current output position in it. */
11111 static char *mode_line_noprop_buf_end;
11112 static char *mode_line_noprop_ptr;
11114 #define MODE_LINE_NOPROP_LEN(start) \
11115 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11117 static enum {
11118 MODE_LINE_DISPLAY = 0,
11119 MODE_LINE_TITLE,
11120 MODE_LINE_NOPROP,
11121 MODE_LINE_STRING
11122 } mode_line_target;
11124 /* Alist that caches the results of :propertize.
11125 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11126 static Lisp_Object mode_line_proptrans_alist;
11128 /* List of strings making up the mode-line. */
11129 static Lisp_Object mode_line_string_list;
11131 /* Base face property when building propertized mode line string. */
11132 static Lisp_Object mode_line_string_face;
11133 static Lisp_Object mode_line_string_face_prop;
11136 /* Unwind data for mode line strings */
11138 static Lisp_Object Vmode_line_unwind_vector;
11140 static Lisp_Object
11141 format_mode_line_unwind_data (struct frame *target_frame,
11142 struct buffer *obuf,
11143 Lisp_Object owin,
11144 int save_proptrans)
11146 Lisp_Object vector, tmp;
11148 /* Reduce consing by keeping one vector in
11149 Vwith_echo_area_save_vector. */
11150 vector = Vmode_line_unwind_vector;
11151 Vmode_line_unwind_vector = Qnil;
11153 if (NILP (vector))
11154 vector = Fmake_vector (make_number (10), Qnil);
11156 ASET (vector, 0, make_number (mode_line_target));
11157 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11158 ASET (vector, 2, mode_line_string_list);
11159 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11160 ASET (vector, 4, mode_line_string_face);
11161 ASET (vector, 5, mode_line_string_face_prop);
11163 if (obuf)
11164 XSETBUFFER (tmp, obuf);
11165 else
11166 tmp = Qnil;
11167 ASET (vector, 6, tmp);
11168 ASET (vector, 7, owin);
11169 if (target_frame)
11171 /* Similarly to `with-selected-window', if the operation selects
11172 a window on another frame, we must restore that frame's
11173 selected window, and (for a tty) the top-frame. */
11174 ASET (vector, 8, target_frame->selected_window);
11175 if (FRAME_TERMCAP_P (target_frame))
11176 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11179 return vector;
11182 static void
11183 unwind_format_mode_line (Lisp_Object vector)
11185 Lisp_Object old_window = AREF (vector, 7);
11186 Lisp_Object target_frame_window = AREF (vector, 8);
11187 Lisp_Object old_top_frame = AREF (vector, 9);
11189 mode_line_target = XINT (AREF (vector, 0));
11190 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11191 mode_line_string_list = AREF (vector, 2);
11192 if (! EQ (AREF (vector, 3), Qt))
11193 mode_line_proptrans_alist = AREF (vector, 3);
11194 mode_line_string_face = AREF (vector, 4);
11195 mode_line_string_face_prop = AREF (vector, 5);
11197 /* Select window before buffer, since it may change the buffer. */
11198 if (!NILP (old_window))
11200 /* If the operation that we are unwinding had selected a window
11201 on a different frame, reset its frame-selected-window. For a
11202 text terminal, reset its top-frame if necessary. */
11203 if (!NILP (target_frame_window))
11205 Lisp_Object frame
11206 = WINDOW_FRAME (XWINDOW (target_frame_window));
11208 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11209 Fselect_window (target_frame_window, Qt);
11211 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11212 Fselect_frame (old_top_frame, Qt);
11215 Fselect_window (old_window, Qt);
11218 if (!NILP (AREF (vector, 6)))
11220 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11221 ASET (vector, 6, Qnil);
11224 Vmode_line_unwind_vector = vector;
11228 /* Store a single character C for the frame title in mode_line_noprop_buf.
11229 Re-allocate mode_line_noprop_buf if necessary. */
11231 static void
11232 store_mode_line_noprop_char (char c)
11234 /* If output position has reached the end of the allocated buffer,
11235 increase the buffer's size. */
11236 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11238 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11239 ptrdiff_t size = len;
11240 mode_line_noprop_buf =
11241 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11242 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11243 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11246 *mode_line_noprop_ptr++ = c;
11250 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11251 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11252 characters that yield more columns than PRECISION; PRECISION <= 0
11253 means copy the whole string. Pad with spaces until FIELD_WIDTH
11254 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11255 pad. Called from display_mode_element when it is used to build a
11256 frame title. */
11258 static int
11259 store_mode_line_noprop (const char *string, int field_width, int precision)
11261 const unsigned char *str = (const unsigned char *) string;
11262 int n = 0;
11263 ptrdiff_t dummy, nbytes;
11265 /* Copy at most PRECISION chars from STR. */
11266 nbytes = strlen (string);
11267 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11268 while (nbytes--)
11269 store_mode_line_noprop_char (*str++);
11271 /* Fill up with spaces until FIELD_WIDTH reached. */
11272 while (field_width > 0
11273 && n < field_width)
11275 store_mode_line_noprop_char (' ');
11276 ++n;
11279 return n;
11282 /***********************************************************************
11283 Frame Titles
11284 ***********************************************************************/
11286 #ifdef HAVE_WINDOW_SYSTEM
11288 /* Set the title of FRAME, if it has changed. The title format is
11289 Vicon_title_format if FRAME is iconified, otherwise it is
11290 frame_title_format. */
11292 static void
11293 x_consider_frame_title (Lisp_Object frame)
11295 struct frame *f = XFRAME (frame);
11297 if (FRAME_WINDOW_P (f)
11298 || FRAME_MINIBUF_ONLY_P (f)
11299 || f->explicit_name)
11301 /* Do we have more than one visible frame on this X display? */
11302 Lisp_Object tail, other_frame, fmt;
11303 ptrdiff_t title_start;
11304 char *title;
11305 ptrdiff_t len;
11306 struct it it;
11307 ptrdiff_t count = SPECPDL_INDEX ();
11309 FOR_EACH_FRAME (tail, other_frame)
11311 struct frame *tf = XFRAME (other_frame);
11313 if (tf != f
11314 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11315 && !FRAME_MINIBUF_ONLY_P (tf)
11316 && !EQ (other_frame, tip_frame)
11317 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11318 break;
11321 /* Set global variable indicating that multiple frames exist. */
11322 multiple_frames = CONSP (tail);
11324 /* Switch to the buffer of selected window of the frame. Set up
11325 mode_line_target so that display_mode_element will output into
11326 mode_line_noprop_buf; then display the title. */
11327 record_unwind_protect (unwind_format_mode_line,
11328 format_mode_line_unwind_data
11329 (f, current_buffer, selected_window, 0));
11331 Fselect_window (f->selected_window, Qt);
11332 set_buffer_internal_1
11333 (XBUFFER (XWINDOW (f->selected_window)->contents));
11334 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11336 mode_line_target = MODE_LINE_TITLE;
11337 title_start = MODE_LINE_NOPROP_LEN (0);
11338 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11339 NULL, DEFAULT_FACE_ID);
11340 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11341 len = MODE_LINE_NOPROP_LEN (title_start);
11342 title = mode_line_noprop_buf + title_start;
11343 unbind_to (count, Qnil);
11345 /* Set the title only if it's changed. This avoids consing in
11346 the common case where it hasn't. (If it turns out that we've
11347 already wasted too much time by walking through the list with
11348 display_mode_element, then we might need to optimize at a
11349 higher level than this.) */
11350 if (! STRINGP (f->name)
11351 || SBYTES (f->name) != len
11352 || memcmp (title, SDATA (f->name), len) != 0)
11353 x_implicitly_set_name (f, make_string (title, len), Qnil);
11357 #endif /* not HAVE_WINDOW_SYSTEM */
11360 /***********************************************************************
11361 Menu Bars
11362 ***********************************************************************/
11364 /* Non-zero if we will not redisplay all visible windows. */
11365 #define REDISPLAY_SOME_P() \
11366 ((windows_or_buffers_changed == 0 \
11367 || windows_or_buffers_changed == REDISPLAY_SOME) \
11368 && (update_mode_lines == 0 \
11369 || update_mode_lines == REDISPLAY_SOME))
11371 /* Prepare for redisplay by updating menu-bar item lists when
11372 appropriate. This can call eval. */
11374 static void
11375 prepare_menu_bars (void)
11377 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11378 bool some_windows = REDISPLAY_SOME_P ();
11379 struct gcpro gcpro1, gcpro2;
11380 Lisp_Object tooltip_frame;
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 tooltip_frame = tip_frame;
11384 #else
11385 tooltip_frame = Qnil;
11386 #endif
11388 if (FUNCTIONP (Vpre_redisplay_function))
11390 Lisp_Object windows = all_windows ? Qt : Qnil;
11391 if (all_windows && some_windows)
11393 Lisp_Object ws = window_list ();
11394 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11396 Lisp_Object this = XCAR (ws);
11397 struct window *w = XWINDOW (this);
11398 if (w->redisplay
11399 || XFRAME (w->frame)->redisplay
11400 || XBUFFER (w->contents)->text->redisplay)
11402 windows = Fcons (this, windows);
11406 safe_call1 (Vpre_redisplay_function, windows);
11409 /* Update all frame titles based on their buffer names, etc. We do
11410 this before the menu bars so that the buffer-menu will show the
11411 up-to-date frame titles. */
11412 #ifdef HAVE_WINDOW_SYSTEM
11413 if (all_windows)
11415 Lisp_Object tail, frame;
11417 FOR_EACH_FRAME (tail, frame)
11419 struct frame *f = XFRAME (frame);
11420 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11421 if (some_windows
11422 && !f->redisplay
11423 && !w->redisplay
11424 && !XBUFFER (w->contents)->text->redisplay)
11425 continue;
11427 if (!EQ (frame, tooltip_frame)
11428 && (FRAME_ICONIFIED_P (f)
11429 || FRAME_VISIBLE_P (f) == 1
11430 /* Exclude TTY frames that are obscured because they
11431 are not the top frame on their console. This is
11432 because x_consider_frame_title actually switches
11433 to the frame, which for TTY frames means it is
11434 marked as garbaged, and will be completely
11435 redrawn on the next redisplay cycle. This causes
11436 TTY frames to be completely redrawn, when there
11437 are more than one of them, even though nothing
11438 should be changed on display. */
11439 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11440 x_consider_frame_title (frame);
11443 #endif /* HAVE_WINDOW_SYSTEM */
11445 /* Update the menu bar item lists, if appropriate. This has to be
11446 done before any actual redisplay or generation of display lines. */
11448 if (all_windows)
11450 Lisp_Object tail, frame;
11451 ptrdiff_t count = SPECPDL_INDEX ();
11452 /* 1 means that update_menu_bar has run its hooks
11453 so any further calls to update_menu_bar shouldn't do so again. */
11454 int menu_bar_hooks_run = 0;
11456 record_unwind_save_match_data ();
11458 FOR_EACH_FRAME (tail, frame)
11460 struct frame *f = XFRAME (frame);
11461 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11463 /* Ignore tooltip frame. */
11464 if (EQ (frame, tooltip_frame))
11465 continue;
11467 if (some_windows
11468 && !f->redisplay
11469 && !w->redisplay
11470 && !XBUFFER (w->contents)->text->redisplay)
11471 continue;
11473 /* If a window on this frame changed size, report that to
11474 the user and clear the size-change flag. */
11475 if (FRAME_WINDOW_SIZES_CHANGED (f))
11477 Lisp_Object functions;
11479 /* Clear flag first in case we get an error below. */
11480 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11481 functions = Vwindow_size_change_functions;
11482 GCPRO2 (tail, functions);
11484 while (CONSP (functions))
11486 if (!EQ (XCAR (functions), Qt))
11487 call1 (XCAR (functions), frame);
11488 functions = XCDR (functions);
11490 UNGCPRO;
11493 GCPRO1 (tail);
11494 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11495 #ifdef HAVE_WINDOW_SYSTEM
11496 update_tool_bar (f, 0);
11497 #endif
11498 #ifdef HAVE_NS
11499 if (windows_or_buffers_changed
11500 && FRAME_NS_P (f))
11501 ns_set_doc_edited
11502 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11503 #endif
11504 UNGCPRO;
11507 unbind_to (count, Qnil);
11509 else
11511 struct frame *sf = SELECTED_FRAME ();
11512 update_menu_bar (sf, 1, 0);
11513 #ifdef HAVE_WINDOW_SYSTEM
11514 update_tool_bar (sf, 1);
11515 #endif
11520 /* Update the menu bar item list for frame F. This has to be done
11521 before we start to fill in any display lines, because it can call
11522 eval.
11524 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11526 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11527 already ran the menu bar hooks for this redisplay, so there
11528 is no need to run them again. The return value is the
11529 updated value of this flag, to pass to the next call. */
11531 static int
11532 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11534 Lisp_Object window;
11535 register struct window *w;
11537 /* If called recursively during a menu update, do nothing. This can
11538 happen when, for instance, an activate-menubar-hook causes a
11539 redisplay. */
11540 if (inhibit_menubar_update)
11541 return hooks_run;
11543 window = FRAME_SELECTED_WINDOW (f);
11544 w = XWINDOW (window);
11546 if (FRAME_WINDOW_P (f)
11548 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11549 || defined (HAVE_NS) || defined (USE_GTK)
11550 FRAME_EXTERNAL_MENU_BAR (f)
11551 #else
11552 FRAME_MENU_BAR_LINES (f) > 0
11553 #endif
11554 : FRAME_MENU_BAR_LINES (f) > 0)
11556 /* If the user has switched buffers or windows, we need to
11557 recompute to reflect the new bindings. But we'll
11558 recompute when update_mode_lines is set too; that means
11559 that people can use force-mode-line-update to request
11560 that the menu bar be recomputed. The adverse effect on
11561 the rest of the redisplay algorithm is about the same as
11562 windows_or_buffers_changed anyway. */
11563 if (windows_or_buffers_changed
11564 /* This used to test w->update_mode_line, but we believe
11565 there is no need to recompute the menu in that case. */
11566 || update_mode_lines
11567 || window_buffer_changed (w))
11569 struct buffer *prev = current_buffer;
11570 ptrdiff_t count = SPECPDL_INDEX ();
11572 specbind (Qinhibit_menubar_update, Qt);
11574 set_buffer_internal_1 (XBUFFER (w->contents));
11575 if (save_match_data)
11576 record_unwind_save_match_data ();
11577 if (NILP (Voverriding_local_map_menu_flag))
11579 specbind (Qoverriding_terminal_local_map, Qnil);
11580 specbind (Qoverriding_local_map, Qnil);
11583 if (!hooks_run)
11585 /* Run the Lucid hook. */
11586 safe_run_hooks (Qactivate_menubar_hook);
11588 /* If it has changed current-menubar from previous value,
11589 really recompute the menu-bar from the value. */
11590 if (! NILP (Vlucid_menu_bar_dirty_flag))
11591 call0 (Qrecompute_lucid_menubar);
11593 safe_run_hooks (Qmenu_bar_update_hook);
11595 hooks_run = 1;
11598 XSETFRAME (Vmenu_updating_frame, f);
11599 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11601 /* Redisplay the menu bar in case we changed it. */
11602 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11603 || defined (HAVE_NS) || defined (USE_GTK)
11604 if (FRAME_WINDOW_P (f))
11606 #if defined (HAVE_NS)
11607 /* All frames on Mac OS share the same menubar. So only
11608 the selected frame should be allowed to set it. */
11609 if (f == SELECTED_FRAME ())
11610 #endif
11611 set_frame_menubar (f, 0, 0);
11613 else
11614 /* On a terminal screen, the menu bar is an ordinary screen
11615 line, and this makes it get updated. */
11616 w->update_mode_line = 1;
11617 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11618 /* In the non-toolkit version, the menu bar is an ordinary screen
11619 line, and this makes it get updated. */
11620 w->update_mode_line = 1;
11621 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11623 unbind_to (count, Qnil);
11624 set_buffer_internal_1 (prev);
11628 return hooks_run;
11631 /***********************************************************************
11632 Tool-bars
11633 ***********************************************************************/
11635 #ifdef HAVE_WINDOW_SYSTEM
11637 /* Tool-bar item index of the item on which a mouse button was pressed
11638 or -1. */
11640 int last_tool_bar_item;
11642 /* Select `frame' temporarily without running all the code in
11643 do_switch_frame.
11644 FIXME: Maybe do_switch_frame should be trimmed down similarly
11645 when `norecord' is set. */
11646 static void
11647 fast_set_selected_frame (Lisp_Object frame)
11649 if (!EQ (selected_frame, frame))
11651 selected_frame = frame;
11652 selected_window = XFRAME (frame)->selected_window;
11656 /* Update the tool-bar item list for frame F. This has to be done
11657 before we start to fill in any display lines. Called from
11658 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11659 and restore it here. */
11661 static void
11662 update_tool_bar (struct frame *f, int save_match_data)
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11666 #else
11667 int do_update = (WINDOWP (f->tool_bar_window)
11668 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11669 #endif
11671 if (do_update)
11673 Lisp_Object window;
11674 struct window *w;
11676 window = FRAME_SELECTED_WINDOW (f);
11677 w = XWINDOW (window);
11679 /* If the user has switched buffers or windows, we need to
11680 recompute to reflect the new bindings. But we'll
11681 recompute when update_mode_lines is set too; that means
11682 that people can use force-mode-line-update to request
11683 that the menu bar be recomputed. The adverse effect on
11684 the rest of the redisplay algorithm is about the same as
11685 windows_or_buffers_changed anyway. */
11686 if (windows_or_buffers_changed
11687 || w->update_mode_line
11688 || update_mode_lines
11689 || window_buffer_changed (w))
11691 struct buffer *prev = current_buffer;
11692 ptrdiff_t count = SPECPDL_INDEX ();
11693 Lisp_Object frame, new_tool_bar;
11694 int new_n_tool_bar;
11695 struct gcpro gcpro1;
11697 /* Set current_buffer to the buffer of the selected
11698 window of the frame, so that we get the right local
11699 keymaps. */
11700 set_buffer_internal_1 (XBUFFER (w->contents));
11702 /* Save match data, if we must. */
11703 if (save_match_data)
11704 record_unwind_save_match_data ();
11706 /* Make sure that we don't accidentally use bogus keymaps. */
11707 if (NILP (Voverriding_local_map_menu_flag))
11709 specbind (Qoverriding_terminal_local_map, Qnil);
11710 specbind (Qoverriding_local_map, Qnil);
11713 GCPRO1 (new_tool_bar);
11715 /* We must temporarily set the selected frame to this frame
11716 before calling tool_bar_items, because the calculation of
11717 the tool-bar keymap uses the selected frame (see
11718 `tool-bar-make-keymap' in tool-bar.el). */
11719 eassert (EQ (selected_window,
11720 /* Since we only explicitly preserve selected_frame,
11721 check that selected_window would be redundant. */
11722 XFRAME (selected_frame)->selected_window));
11723 record_unwind_protect (fast_set_selected_frame, selected_frame);
11724 XSETFRAME (frame, f);
11725 fast_set_selected_frame (frame);
11727 /* Build desired tool-bar items from keymaps. */
11728 new_tool_bar
11729 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11730 &new_n_tool_bar);
11732 /* Redisplay the tool-bar if we changed it. */
11733 if (new_n_tool_bar != f->n_tool_bar_items
11734 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11736 /* Redisplay that happens asynchronously due to an expose event
11737 may access f->tool_bar_items. Make sure we update both
11738 variables within BLOCK_INPUT so no such event interrupts. */
11739 block_input ();
11740 fset_tool_bar_items (f, new_tool_bar);
11741 f->n_tool_bar_items = new_n_tool_bar;
11742 w->update_mode_line = 1;
11743 unblock_input ();
11746 UNGCPRO;
11748 unbind_to (count, Qnil);
11749 set_buffer_internal_1 (prev);
11754 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11756 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11757 F's desired tool-bar contents. F->tool_bar_items must have
11758 been set up previously by calling prepare_menu_bars. */
11760 static void
11761 build_desired_tool_bar_string (struct frame *f)
11763 int i, size, size_needed;
11764 struct gcpro gcpro1, gcpro2, gcpro3;
11765 Lisp_Object image, plist, props;
11767 image = plist = props = Qnil;
11768 GCPRO3 (image, plist, props);
11770 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11771 Otherwise, make a new string. */
11773 /* The size of the string we might be able to reuse. */
11774 size = (STRINGP (f->desired_tool_bar_string)
11775 ? SCHARS (f->desired_tool_bar_string)
11776 : 0);
11778 /* We need one space in the string for each image. */
11779 size_needed = f->n_tool_bar_items;
11781 /* Reuse f->desired_tool_bar_string, if possible. */
11782 if (size < size_needed || NILP (f->desired_tool_bar_string))
11783 fset_desired_tool_bar_string
11784 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11785 else
11787 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11788 Fremove_text_properties (make_number (0), make_number (size),
11789 props, f->desired_tool_bar_string);
11792 /* Put a `display' property on the string for the images to display,
11793 put a `menu_item' property on tool-bar items with a value that
11794 is the index of the item in F's tool-bar item vector. */
11795 for (i = 0; i < f->n_tool_bar_items; ++i)
11797 #define PROP(IDX) \
11798 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11800 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11801 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11802 int hmargin, vmargin, relief, idx, end;
11804 /* If image is a vector, choose the image according to the
11805 button state. */
11806 image = PROP (TOOL_BAR_ITEM_IMAGES);
11807 if (VECTORP (image))
11809 if (enabled_p)
11810 idx = (selected_p
11811 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11812 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11813 else
11814 idx = (selected_p
11815 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11816 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11818 eassert (ASIZE (image) >= idx);
11819 image = AREF (image, idx);
11821 else
11822 idx = -1;
11824 /* Ignore invalid image specifications. */
11825 if (!valid_image_p (image))
11826 continue;
11828 /* Display the tool-bar button pressed, or depressed. */
11829 plist = Fcopy_sequence (XCDR (image));
11831 /* Compute margin and relief to draw. */
11832 relief = (tool_bar_button_relief >= 0
11833 ? tool_bar_button_relief
11834 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11835 hmargin = vmargin = relief;
11837 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11838 INT_MAX - max (hmargin, vmargin)))
11840 hmargin += XFASTINT (Vtool_bar_button_margin);
11841 vmargin += XFASTINT (Vtool_bar_button_margin);
11843 else if (CONSP (Vtool_bar_button_margin))
11845 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11846 INT_MAX - hmargin))
11847 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11849 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11850 INT_MAX - vmargin))
11851 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11854 if (auto_raise_tool_bar_buttons_p)
11856 /* Add a `:relief' property to the image spec if the item is
11857 selected. */
11858 if (selected_p)
11860 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11861 hmargin -= relief;
11862 vmargin -= relief;
11865 else
11867 /* If image is selected, display it pressed, i.e. with a
11868 negative relief. If it's not selected, display it with a
11869 raised relief. */
11870 plist = Fplist_put (plist, QCrelief,
11871 (selected_p
11872 ? make_number (-relief)
11873 : make_number (relief)));
11874 hmargin -= relief;
11875 vmargin -= relief;
11878 /* Put a margin around the image. */
11879 if (hmargin || vmargin)
11881 if (hmargin == vmargin)
11882 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11883 else
11884 plist = Fplist_put (plist, QCmargin,
11885 Fcons (make_number (hmargin),
11886 make_number (vmargin)));
11889 /* If button is not enabled, and we don't have special images
11890 for the disabled state, make the image appear disabled by
11891 applying an appropriate algorithm to it. */
11892 if (!enabled_p && idx < 0)
11893 plist = Fplist_put (plist, QCconversion, Qdisabled);
11895 /* Put a `display' text property on the string for the image to
11896 display. Put a `menu-item' property on the string that gives
11897 the start of this item's properties in the tool-bar items
11898 vector. */
11899 image = Fcons (Qimage, plist);
11900 props = list4 (Qdisplay, image,
11901 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11903 /* Let the last image hide all remaining spaces in the tool bar
11904 string. The string can be longer than needed when we reuse a
11905 previous string. */
11906 if (i + 1 == f->n_tool_bar_items)
11907 end = SCHARS (f->desired_tool_bar_string);
11908 else
11909 end = i + 1;
11910 Fadd_text_properties (make_number (i), make_number (end),
11911 props, f->desired_tool_bar_string);
11912 #undef PROP
11915 UNGCPRO;
11919 /* Display one line of the tool-bar of frame IT->f.
11921 HEIGHT specifies the desired height of the tool-bar line.
11922 If the actual height of the glyph row is less than HEIGHT, the
11923 row's height is increased to HEIGHT, and the icons are centered
11924 vertically in the new height.
11926 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11927 count a final empty row in case the tool-bar width exactly matches
11928 the window width.
11931 static void
11932 display_tool_bar_line (struct it *it, int height)
11934 struct glyph_row *row = it->glyph_row;
11935 int max_x = it->last_visible_x;
11936 struct glyph *last;
11938 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11939 clear_glyph_row (row);
11940 row->enabled_p = true;
11941 row->y = it->current_y;
11943 /* Note that this isn't made use of if the face hasn't a box,
11944 so there's no need to check the face here. */
11945 it->start_of_box_run_p = 1;
11947 while (it->current_x < max_x)
11949 int x, n_glyphs_before, i, nglyphs;
11950 struct it it_before;
11952 /* Get the next display element. */
11953 if (!get_next_display_element (it))
11955 /* Don't count empty row if we are counting needed tool-bar lines. */
11956 if (height < 0 && !it->hpos)
11957 return;
11958 break;
11961 /* Produce glyphs. */
11962 n_glyphs_before = row->used[TEXT_AREA];
11963 it_before = *it;
11965 PRODUCE_GLYPHS (it);
11967 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11968 i = 0;
11969 x = it_before.current_x;
11970 while (i < nglyphs)
11972 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11974 if (x + glyph->pixel_width > max_x)
11976 /* Glyph doesn't fit on line. Backtrack. */
11977 row->used[TEXT_AREA] = n_glyphs_before;
11978 *it = it_before;
11979 /* If this is the only glyph on this line, it will never fit on the
11980 tool-bar, so skip it. But ensure there is at least one glyph,
11981 so we don't accidentally disable the tool-bar. */
11982 if (n_glyphs_before == 0
11983 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11984 break;
11985 goto out;
11988 ++it->hpos;
11989 x += glyph->pixel_width;
11990 ++i;
11993 /* Stop at line end. */
11994 if (ITERATOR_AT_END_OF_LINE_P (it))
11995 break;
11997 set_iterator_to_next (it, 1);
12000 out:;
12002 row->displays_text_p = row->used[TEXT_AREA] != 0;
12004 /* Use default face for the border below the tool bar.
12006 FIXME: When auto-resize-tool-bars is grow-only, there is
12007 no additional border below the possibly empty tool-bar lines.
12008 So to make the extra empty lines look "normal", we have to
12009 use the tool-bar face for the border too. */
12010 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12011 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12012 it->face_id = DEFAULT_FACE_ID;
12014 extend_face_to_end_of_line (it);
12015 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12016 last->right_box_line_p = 1;
12017 if (last == row->glyphs[TEXT_AREA])
12018 last->left_box_line_p = 1;
12020 /* Make line the desired height and center it vertically. */
12021 if ((height -= it->max_ascent + it->max_descent) > 0)
12023 /* Don't add more than one line height. */
12024 height %= FRAME_LINE_HEIGHT (it->f);
12025 it->max_ascent += height / 2;
12026 it->max_descent += (height + 1) / 2;
12029 compute_line_metrics (it);
12031 /* If line is empty, make it occupy the rest of the tool-bar. */
12032 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12034 row->height = row->phys_height = it->last_visible_y - row->y;
12035 row->visible_height = row->height;
12036 row->ascent = row->phys_ascent = 0;
12037 row->extra_line_spacing = 0;
12040 row->full_width_p = 1;
12041 row->continued_p = 0;
12042 row->truncated_on_left_p = 0;
12043 row->truncated_on_right_p = 0;
12045 it->current_x = it->hpos = 0;
12046 it->current_y += row->height;
12047 ++it->vpos;
12048 ++it->glyph_row;
12052 /* Max tool-bar height. Basically, this is what makes all other windows
12053 disappear when the frame gets too small. Rethink this! */
12055 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12056 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12058 /* Value is the number of pixels needed to make all tool-bar items of
12059 frame F visible. The actual number of glyph rows needed is
12060 returned in *N_ROWS if non-NULL. */
12062 static int
12063 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12065 struct window *w = XWINDOW (f->tool_bar_window);
12066 struct it it;
12067 /* tool_bar_height is called from redisplay_tool_bar after building
12068 the desired matrix, so use (unused) mode-line row as temporary row to
12069 avoid destroying the first tool-bar row. */
12070 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12072 /* Initialize an iterator for iteration over
12073 F->desired_tool_bar_string in the tool-bar window of frame F. */
12074 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12075 it.first_visible_x = 0;
12076 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12077 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12078 it.paragraph_embedding = L2R;
12080 while (!ITERATOR_AT_END_P (&it))
12082 clear_glyph_row (temp_row);
12083 it.glyph_row = temp_row;
12084 display_tool_bar_line (&it, -1);
12086 clear_glyph_row (temp_row);
12088 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12089 if (n_rows)
12090 *n_rows = it.vpos > 0 ? it.vpos : -1;
12092 if (pixelwise)
12093 return it.current_y;
12094 else
12095 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12098 #endif /* !USE_GTK && !HAVE_NS */
12100 #if defined USE_GTK || defined HAVE_NS
12101 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12102 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12103 #endif
12105 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12106 0, 2, 0,
12107 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12108 If FRAME is nil or omitted, use the selected frame. Optional argument
12109 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12110 (Lisp_Object frame, Lisp_Object pixelwise)
12112 int height = 0;
12114 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12115 struct frame *f = decode_any_frame (frame);
12117 if (WINDOWP (f->tool_bar_window)
12118 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12120 update_tool_bar (f, 1);
12121 if (f->n_tool_bar_items)
12123 build_desired_tool_bar_string (f);
12124 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12127 #endif
12129 return make_number (height);
12133 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12134 height should be changed. */
12136 static int
12137 redisplay_tool_bar (struct frame *f)
12139 #if defined (USE_GTK) || defined (HAVE_NS)
12141 if (FRAME_EXTERNAL_TOOL_BAR (f))
12142 update_frame_tool_bar (f);
12143 return 0;
12145 #else /* !USE_GTK && !HAVE_NS */
12147 struct window *w;
12148 struct it it;
12149 struct glyph_row *row;
12151 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12152 do anything. This means you must start with tool-bar-lines
12153 non-zero to get the auto-sizing effect. Or in other words, you
12154 can turn off tool-bars by specifying tool-bar-lines zero. */
12155 if (!WINDOWP (f->tool_bar_window)
12156 || (w = XWINDOW (f->tool_bar_window),
12157 WINDOW_PIXEL_HEIGHT (w) == 0))
12158 return 0;
12160 /* Set up an iterator for the tool-bar window. */
12161 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12162 it.first_visible_x = 0;
12163 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12164 row = it.glyph_row;
12166 /* Build a string that represents the contents of the tool-bar. */
12167 build_desired_tool_bar_string (f);
12168 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12169 /* FIXME: This should be controlled by a user option. But it
12170 doesn't make sense to have an R2L tool bar if the menu bar cannot
12171 be drawn also R2L, and making the menu bar R2L is tricky due
12172 toolkit-specific code that implements it. If an R2L tool bar is
12173 ever supported, display_tool_bar_line should also be augmented to
12174 call unproduce_glyphs like display_line and display_string
12175 do. */
12176 it.paragraph_embedding = L2R;
12178 if (f->n_tool_bar_rows == 0)
12180 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12182 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12184 Lisp_Object frame;
12185 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12186 / FRAME_LINE_HEIGHT (f));
12188 XSETFRAME (frame, f);
12189 Fmodify_frame_parameters (frame,
12190 list1 (Fcons (Qtool_bar_lines,
12191 make_number (new_lines))));
12192 /* Always do that now. */
12193 clear_glyph_matrix (w->desired_matrix);
12194 f->fonts_changed = 1;
12195 return 1;
12199 /* Display as many lines as needed to display all tool-bar items. */
12201 if (f->n_tool_bar_rows > 0)
12203 int border, rows, height, extra;
12205 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12206 border = XINT (Vtool_bar_border);
12207 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12208 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12209 else if (EQ (Vtool_bar_border, Qborder_width))
12210 border = f->border_width;
12211 else
12212 border = 0;
12213 if (border < 0)
12214 border = 0;
12216 rows = f->n_tool_bar_rows;
12217 height = max (1, (it.last_visible_y - border) / rows);
12218 extra = it.last_visible_y - border - height * rows;
12220 while (it.current_y < it.last_visible_y)
12222 int h = 0;
12223 if (extra > 0 && rows-- > 0)
12225 h = (extra + rows - 1) / rows;
12226 extra -= h;
12228 display_tool_bar_line (&it, height + h);
12231 else
12233 while (it.current_y < it.last_visible_y)
12234 display_tool_bar_line (&it, 0);
12237 /* It doesn't make much sense to try scrolling in the tool-bar
12238 window, so don't do it. */
12239 w->desired_matrix->no_scrolling_p = 1;
12240 w->must_be_updated_p = 1;
12242 if (!NILP (Vauto_resize_tool_bars))
12244 /* Do we really allow the toolbar to occupy the whole frame? */
12245 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12246 int change_height_p = 0;
12248 /* If we couldn't display everything, change the tool-bar's
12249 height if there is room for more. */
12250 if (IT_STRING_CHARPOS (it) < it.end_charpos
12251 && it.current_y < max_tool_bar_height)
12252 change_height_p = 1;
12254 /* We subtract 1 because display_tool_bar_line advances the
12255 glyph_row pointer before returning to its caller. We want to
12256 examine the last glyph row produced by
12257 display_tool_bar_line. */
12258 row = it.glyph_row - 1;
12260 /* If there are blank lines at the end, except for a partially
12261 visible blank line at the end that is smaller than
12262 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12263 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12264 && row->height >= FRAME_LINE_HEIGHT (f))
12265 change_height_p = 1;
12267 /* If row displays tool-bar items, but is partially visible,
12268 change the tool-bar's height. */
12269 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12270 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12271 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12272 change_height_p = 1;
12274 /* Resize windows as needed by changing the `tool-bar-lines'
12275 frame parameter. */
12276 if (change_height_p)
12278 Lisp_Object frame;
12279 int nrows;
12280 int new_height = tool_bar_height (f, &nrows, 1);
12282 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12283 && !f->minimize_tool_bar_window_p)
12284 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12285 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12286 f->minimize_tool_bar_window_p = 0;
12288 if (change_height_p)
12290 /* Current size of the tool-bar window in canonical line
12291 units. */
12292 int old_lines = WINDOW_TOTAL_LINES (w);
12293 /* Required size of the tool-bar window in canonical
12294 line units. */
12295 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12296 / FRAME_LINE_HEIGHT (f));
12297 /* Maximum size of the tool-bar window in canonical line
12298 units that this frame can allow. */
12299 int max_lines =
12300 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12302 /* Don't try to change the tool-bar window size and set
12303 the fonts_changed flag unless really necessary. That
12304 flag causes redisplay to give up and retry
12305 redisplaying the frame from scratch, so setting it
12306 unnecessarily can lead to nasty redisplay loops. */
12307 if (new_lines <= max_lines
12308 && eabs (new_lines - old_lines) >= 1)
12310 XSETFRAME (frame, f);
12311 Fmodify_frame_parameters (frame,
12312 list1 (Fcons (Qtool_bar_lines,
12313 make_number (new_lines))));
12314 clear_glyph_matrix (w->desired_matrix);
12315 f->n_tool_bar_rows = nrows;
12316 f->fonts_changed = 1;
12317 return 1;
12323 f->minimize_tool_bar_window_p = 0;
12324 return 0;
12326 #endif /* USE_GTK || HAVE_NS */
12329 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12331 /* Get information about the tool-bar item which is displayed in GLYPH
12332 on frame F. Return in *PROP_IDX the index where tool-bar item
12333 properties start in F->tool_bar_items. Value is zero if
12334 GLYPH doesn't display a tool-bar item. */
12336 static int
12337 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12339 Lisp_Object prop;
12340 int success_p;
12341 int charpos;
12343 /* This function can be called asynchronously, which means we must
12344 exclude any possibility that Fget_text_property signals an
12345 error. */
12346 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12347 charpos = max (0, charpos);
12349 /* Get the text property `menu-item' at pos. The value of that
12350 property is the start index of this item's properties in
12351 F->tool_bar_items. */
12352 prop = Fget_text_property (make_number (charpos),
12353 Qmenu_item, f->current_tool_bar_string);
12354 if (INTEGERP (prop))
12356 *prop_idx = XINT (prop);
12357 success_p = 1;
12359 else
12360 success_p = 0;
12362 return success_p;
12366 /* Get information about the tool-bar item at position X/Y on frame F.
12367 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12368 the current matrix of the tool-bar window of F, or NULL if not
12369 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12370 item in F->tool_bar_items. Value is
12372 -1 if X/Y is not on a tool-bar item
12373 0 if X/Y is on the same item that was highlighted before.
12374 1 otherwise. */
12376 static int
12377 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12378 int *hpos, int *vpos, int *prop_idx)
12380 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12381 struct window *w = XWINDOW (f->tool_bar_window);
12382 int area;
12384 /* Find the glyph under X/Y. */
12385 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12386 if (*glyph == NULL)
12387 return -1;
12389 /* Get the start of this tool-bar item's properties in
12390 f->tool_bar_items. */
12391 if (!tool_bar_item_info (f, *glyph, prop_idx))
12392 return -1;
12394 /* Is mouse on the highlighted item? */
12395 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12396 && *vpos >= hlinfo->mouse_face_beg_row
12397 && *vpos <= hlinfo->mouse_face_end_row
12398 && (*vpos > hlinfo->mouse_face_beg_row
12399 || *hpos >= hlinfo->mouse_face_beg_col)
12400 && (*vpos < hlinfo->mouse_face_end_row
12401 || *hpos < hlinfo->mouse_face_end_col
12402 || hlinfo->mouse_face_past_end))
12403 return 0;
12405 return 1;
12409 /* EXPORT:
12410 Handle mouse button event on the tool-bar of frame F, at
12411 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12412 0 for button release. MODIFIERS is event modifiers for button
12413 release. */
12415 void
12416 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12417 int modifiers)
12419 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12420 struct window *w = XWINDOW (f->tool_bar_window);
12421 int hpos, vpos, prop_idx;
12422 struct glyph *glyph;
12423 Lisp_Object enabled_p;
12424 int ts;
12426 /* If not on the highlighted tool-bar item, and mouse-highlight is
12427 non-nil, return. This is so we generate the tool-bar button
12428 click only when the mouse button is released on the same item as
12429 where it was pressed. However, when mouse-highlight is disabled,
12430 generate the click when the button is released regardless of the
12431 highlight, since tool-bar items are not highlighted in that
12432 case. */
12433 frame_to_window_pixel_xy (w, &x, &y);
12434 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12435 if (ts == -1
12436 || (ts != 0 && !NILP (Vmouse_highlight)))
12437 return;
12439 /* When mouse-highlight is off, generate the click for the item
12440 where the button was pressed, disregarding where it was
12441 released. */
12442 if (NILP (Vmouse_highlight) && !down_p)
12443 prop_idx = last_tool_bar_item;
12445 /* If item is disabled, do nothing. */
12446 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12447 if (NILP (enabled_p))
12448 return;
12450 if (down_p)
12452 /* Show item in pressed state. */
12453 if (!NILP (Vmouse_highlight))
12454 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12455 last_tool_bar_item = prop_idx;
12457 else
12459 Lisp_Object key, frame;
12460 struct input_event event;
12461 EVENT_INIT (event);
12463 /* Show item in released state. */
12464 if (!NILP (Vmouse_highlight))
12465 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12467 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12469 XSETFRAME (frame, f);
12470 event.kind = TOOL_BAR_EVENT;
12471 event.frame_or_window = frame;
12472 event.arg = frame;
12473 kbd_buffer_store_event (&event);
12475 event.kind = TOOL_BAR_EVENT;
12476 event.frame_or_window = frame;
12477 event.arg = key;
12478 event.modifiers = modifiers;
12479 kbd_buffer_store_event (&event);
12480 last_tool_bar_item = -1;
12485 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12486 tool-bar window-relative coordinates X/Y. Called from
12487 note_mouse_highlight. */
12489 static void
12490 note_tool_bar_highlight (struct frame *f, int x, int y)
12492 Lisp_Object window = f->tool_bar_window;
12493 struct window *w = XWINDOW (window);
12494 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12495 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12496 int hpos, vpos;
12497 struct glyph *glyph;
12498 struct glyph_row *row;
12499 int i;
12500 Lisp_Object enabled_p;
12501 int prop_idx;
12502 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12503 int mouse_down_p, rc;
12505 /* Function note_mouse_highlight is called with negative X/Y
12506 values when mouse moves outside of the frame. */
12507 if (x <= 0 || y <= 0)
12509 clear_mouse_face (hlinfo);
12510 return;
12513 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12514 if (rc < 0)
12516 /* Not on tool-bar item. */
12517 clear_mouse_face (hlinfo);
12518 return;
12520 else if (rc == 0)
12521 /* On same tool-bar item as before. */
12522 goto set_help_echo;
12524 clear_mouse_face (hlinfo);
12526 /* Mouse is down, but on different tool-bar item? */
12527 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12528 && f == dpyinfo->last_mouse_frame);
12530 if (mouse_down_p
12531 && last_tool_bar_item != prop_idx)
12532 return;
12534 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12536 /* If tool-bar item is not enabled, don't highlight it. */
12537 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12538 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12540 /* Compute the x-position of the glyph. In front and past the
12541 image is a space. We include this in the highlighted area. */
12542 row = MATRIX_ROW (w->current_matrix, vpos);
12543 for (i = x = 0; i < hpos; ++i)
12544 x += row->glyphs[TEXT_AREA][i].pixel_width;
12546 /* Record this as the current active region. */
12547 hlinfo->mouse_face_beg_col = hpos;
12548 hlinfo->mouse_face_beg_row = vpos;
12549 hlinfo->mouse_face_beg_x = x;
12550 hlinfo->mouse_face_past_end = 0;
12552 hlinfo->mouse_face_end_col = hpos + 1;
12553 hlinfo->mouse_face_end_row = vpos;
12554 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12555 hlinfo->mouse_face_window = window;
12556 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12558 /* Display it as active. */
12559 show_mouse_face (hlinfo, draw);
12562 set_help_echo:
12564 /* Set help_echo_string to a help string to display for this tool-bar item.
12565 XTread_socket does the rest. */
12566 help_echo_object = help_echo_window = Qnil;
12567 help_echo_pos = -1;
12568 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12569 if (NILP (help_echo_string))
12570 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12573 #endif /* !USE_GTK && !HAVE_NS */
12575 #endif /* HAVE_WINDOW_SYSTEM */
12579 /************************************************************************
12580 Horizontal scrolling
12581 ************************************************************************/
12583 static int hscroll_window_tree (Lisp_Object);
12584 static int hscroll_windows (Lisp_Object);
12586 /* For all leaf windows in the window tree rooted at WINDOW, set their
12587 hscroll value so that PT is (i) visible in the window, and (ii) so
12588 that it is not within a certain margin at the window's left and
12589 right border. Value is non-zero if any window's hscroll has been
12590 changed. */
12592 static int
12593 hscroll_window_tree (Lisp_Object window)
12595 int hscrolled_p = 0;
12596 int hscroll_relative_p = FLOATP (Vhscroll_step);
12597 int hscroll_step_abs = 0;
12598 double hscroll_step_rel = 0;
12600 if (hscroll_relative_p)
12602 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12603 if (hscroll_step_rel < 0)
12605 hscroll_relative_p = 0;
12606 hscroll_step_abs = 0;
12609 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12611 hscroll_step_abs = XINT (Vhscroll_step);
12612 if (hscroll_step_abs < 0)
12613 hscroll_step_abs = 0;
12615 else
12616 hscroll_step_abs = 0;
12618 while (WINDOWP (window))
12620 struct window *w = XWINDOW (window);
12622 if (WINDOWP (w->contents))
12623 hscrolled_p |= hscroll_window_tree (w->contents);
12624 else if (w->cursor.vpos >= 0)
12626 int h_margin;
12627 int text_area_width;
12628 struct glyph_row *cursor_row;
12629 struct glyph_row *bottom_row;
12630 int row_r2l_p;
12632 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12633 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12634 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12635 else
12636 cursor_row = bottom_row - 1;
12638 if (!cursor_row->enabled_p)
12640 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12641 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12642 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12643 else
12644 cursor_row = bottom_row - 1;
12646 row_r2l_p = cursor_row->reversed_p;
12648 text_area_width = window_box_width (w, TEXT_AREA);
12650 /* Scroll when cursor is inside this scroll margin. */
12651 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12653 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12654 /* For left-to-right rows, hscroll when cursor is either
12655 (i) inside the right hscroll margin, or (ii) if it is
12656 inside the left margin and the window is already
12657 hscrolled. */
12658 && ((!row_r2l_p
12659 && ((w->hscroll
12660 && w->cursor.x <= h_margin)
12661 || (cursor_row->enabled_p
12662 && cursor_row->truncated_on_right_p
12663 && (w->cursor.x >= text_area_width - h_margin))))
12664 /* For right-to-left rows, the logic is similar,
12665 except that rules for scrolling to left and right
12666 are reversed. E.g., if cursor.x <= h_margin, we
12667 need to hscroll "to the right" unconditionally,
12668 and that will scroll the screen to the left so as
12669 to reveal the next portion of the row. */
12670 || (row_r2l_p
12671 && ((cursor_row->enabled_p
12672 /* FIXME: It is confusing to set the
12673 truncated_on_right_p flag when R2L rows
12674 are actually truncated on the left. */
12675 && cursor_row->truncated_on_right_p
12676 && w->cursor.x <= h_margin)
12677 || (w->hscroll
12678 && (w->cursor.x >= text_area_width - h_margin))))))
12680 struct it it;
12681 ptrdiff_t hscroll;
12682 struct buffer *saved_current_buffer;
12683 ptrdiff_t pt;
12684 int wanted_x;
12686 /* Find point in a display of infinite width. */
12687 saved_current_buffer = current_buffer;
12688 current_buffer = XBUFFER (w->contents);
12690 if (w == XWINDOW (selected_window))
12691 pt = PT;
12692 else
12693 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12695 /* Move iterator to pt starting at cursor_row->start in
12696 a line with infinite width. */
12697 init_to_row_start (&it, w, cursor_row);
12698 it.last_visible_x = INFINITY;
12699 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12700 current_buffer = saved_current_buffer;
12702 /* Position cursor in window. */
12703 if (!hscroll_relative_p && hscroll_step_abs == 0)
12704 hscroll = max (0, (it.current_x
12705 - (ITERATOR_AT_END_OF_LINE_P (&it)
12706 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12707 : (text_area_width / 2))))
12708 / FRAME_COLUMN_WIDTH (it.f);
12709 else if ((!row_r2l_p
12710 && w->cursor.x >= text_area_width - h_margin)
12711 || (row_r2l_p && w->cursor.x <= h_margin))
12713 if (hscroll_relative_p)
12714 wanted_x = text_area_width * (1 - hscroll_step_rel)
12715 - h_margin;
12716 else
12717 wanted_x = text_area_width
12718 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12719 - h_margin;
12720 hscroll
12721 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12723 else
12725 if (hscroll_relative_p)
12726 wanted_x = text_area_width * hscroll_step_rel
12727 + h_margin;
12728 else
12729 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12730 + h_margin;
12731 hscroll
12732 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12734 hscroll = max (hscroll, w->min_hscroll);
12736 /* Don't prevent redisplay optimizations if hscroll
12737 hasn't changed, as it will unnecessarily slow down
12738 redisplay. */
12739 if (w->hscroll != hscroll)
12741 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12742 w->hscroll = hscroll;
12743 hscrolled_p = 1;
12748 window = w->next;
12751 /* Value is non-zero if hscroll of any leaf window has been changed. */
12752 return hscrolled_p;
12756 /* Set hscroll so that cursor is visible and not inside horizontal
12757 scroll margins for all windows in the tree rooted at WINDOW. See
12758 also hscroll_window_tree above. Value is non-zero if any window's
12759 hscroll has been changed. If it has, desired matrices on the frame
12760 of WINDOW are cleared. */
12762 static int
12763 hscroll_windows (Lisp_Object window)
12765 int hscrolled_p = hscroll_window_tree (window);
12766 if (hscrolled_p)
12767 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12768 return hscrolled_p;
12773 /************************************************************************
12774 Redisplay
12775 ************************************************************************/
12777 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12778 to a non-zero value. This is sometimes handy to have in a debugger
12779 session. */
12781 #ifdef GLYPH_DEBUG
12783 /* First and last unchanged row for try_window_id. */
12785 static int debug_first_unchanged_at_end_vpos;
12786 static int debug_last_unchanged_at_beg_vpos;
12788 /* Delta vpos and y. */
12790 static int debug_dvpos, debug_dy;
12792 /* Delta in characters and bytes for try_window_id. */
12794 static ptrdiff_t debug_delta, debug_delta_bytes;
12796 /* Values of window_end_pos and window_end_vpos at the end of
12797 try_window_id. */
12799 static ptrdiff_t debug_end_vpos;
12801 /* Append a string to W->desired_matrix->method. FMT is a printf
12802 format string. If trace_redisplay_p is true also printf the
12803 resulting string to stderr. */
12805 static void debug_method_add (struct window *, char const *, ...)
12806 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12808 static void
12809 debug_method_add (struct window *w, char const *fmt, ...)
12811 void *ptr = w;
12812 char *method = w->desired_matrix->method;
12813 int len = strlen (method);
12814 int size = sizeof w->desired_matrix->method;
12815 int remaining = size - len - 1;
12816 va_list ap;
12818 if (len && remaining)
12820 method[len] = '|';
12821 --remaining, ++len;
12824 va_start (ap, fmt);
12825 vsnprintf (method + len, remaining + 1, fmt, ap);
12826 va_end (ap);
12828 if (trace_redisplay_p)
12829 fprintf (stderr, "%p (%s): %s\n",
12830 ptr,
12831 ((BUFFERP (w->contents)
12832 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12833 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12834 : "no buffer"),
12835 method + len);
12838 #endif /* GLYPH_DEBUG */
12841 /* Value is non-zero if all changes in window W, which displays
12842 current_buffer, are in the text between START and END. START is a
12843 buffer position, END is given as a distance from Z. Used in
12844 redisplay_internal for display optimization. */
12846 static int
12847 text_outside_line_unchanged_p (struct window *w,
12848 ptrdiff_t start, ptrdiff_t end)
12850 int unchanged_p = 1;
12852 /* If text or overlays have changed, see where. */
12853 if (window_outdated (w))
12855 /* Gap in the line? */
12856 if (GPT < start || Z - GPT < end)
12857 unchanged_p = 0;
12859 /* Changes start in front of the line, or end after it? */
12860 if (unchanged_p
12861 && (BEG_UNCHANGED < start - 1
12862 || END_UNCHANGED < end))
12863 unchanged_p = 0;
12865 /* If selective display, can't optimize if changes start at the
12866 beginning of the line. */
12867 if (unchanged_p
12868 && INTEGERP (BVAR (current_buffer, selective_display))
12869 && XINT (BVAR (current_buffer, selective_display)) > 0
12870 && (BEG_UNCHANGED < start || GPT <= start))
12871 unchanged_p = 0;
12873 /* If there are overlays at the start or end of the line, these
12874 may have overlay strings with newlines in them. A change at
12875 START, for instance, may actually concern the display of such
12876 overlay strings as well, and they are displayed on different
12877 lines. So, quickly rule out this case. (For the future, it
12878 might be desirable to implement something more telling than
12879 just BEG/END_UNCHANGED.) */
12880 if (unchanged_p)
12882 if (BEG + BEG_UNCHANGED == start
12883 && overlay_touches_p (start))
12884 unchanged_p = 0;
12885 if (END_UNCHANGED == end
12886 && overlay_touches_p (Z - end))
12887 unchanged_p = 0;
12890 /* Under bidi reordering, adding or deleting a character in the
12891 beginning of a paragraph, before the first strong directional
12892 character, can change the base direction of the paragraph (unless
12893 the buffer specifies a fixed paragraph direction), which will
12894 require to redisplay the whole paragraph. It might be worthwhile
12895 to find the paragraph limits and widen the range of redisplayed
12896 lines to that, but for now just give up this optimization. */
12897 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12898 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12899 unchanged_p = 0;
12902 return unchanged_p;
12906 /* Do a frame update, taking possible shortcuts into account. This is
12907 the main external entry point for redisplay.
12909 If the last redisplay displayed an echo area message and that message
12910 is no longer requested, we clear the echo area or bring back the
12911 mini-buffer if that is in use. */
12913 void
12914 redisplay (void)
12916 redisplay_internal ();
12920 static Lisp_Object
12921 overlay_arrow_string_or_property (Lisp_Object var)
12923 Lisp_Object val;
12925 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12926 return val;
12928 return Voverlay_arrow_string;
12931 /* Return 1 if there are any overlay-arrows in current_buffer. */
12932 static int
12933 overlay_arrow_in_current_buffer_p (void)
12935 Lisp_Object vlist;
12937 for (vlist = Voverlay_arrow_variable_list;
12938 CONSP (vlist);
12939 vlist = XCDR (vlist))
12941 Lisp_Object var = XCAR (vlist);
12942 Lisp_Object val;
12944 if (!SYMBOLP (var))
12945 continue;
12946 val = find_symbol_value (var);
12947 if (MARKERP (val)
12948 && current_buffer == XMARKER (val)->buffer)
12949 return 1;
12951 return 0;
12955 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12956 has changed. */
12958 static int
12959 overlay_arrows_changed_p (void)
12961 Lisp_Object vlist;
12963 for (vlist = Voverlay_arrow_variable_list;
12964 CONSP (vlist);
12965 vlist = XCDR (vlist))
12967 Lisp_Object var = XCAR (vlist);
12968 Lisp_Object val, pstr;
12970 if (!SYMBOLP (var))
12971 continue;
12972 val = find_symbol_value (var);
12973 if (!MARKERP (val))
12974 continue;
12975 if (! EQ (COERCE_MARKER (val),
12976 Fget (var, Qlast_arrow_position))
12977 || ! (pstr = overlay_arrow_string_or_property (var),
12978 EQ (pstr, Fget (var, Qlast_arrow_string))))
12979 return 1;
12981 return 0;
12984 /* Mark overlay arrows to be updated on next redisplay. */
12986 static void
12987 update_overlay_arrows (int up_to_date)
12989 Lisp_Object vlist;
12991 for (vlist = Voverlay_arrow_variable_list;
12992 CONSP (vlist);
12993 vlist = XCDR (vlist))
12995 Lisp_Object var = XCAR (vlist);
12997 if (!SYMBOLP (var))
12998 continue;
13000 if (up_to_date > 0)
13002 Lisp_Object val = find_symbol_value (var);
13003 Fput (var, Qlast_arrow_position,
13004 COERCE_MARKER (val));
13005 Fput (var, Qlast_arrow_string,
13006 overlay_arrow_string_or_property (var));
13008 else if (up_to_date < 0
13009 || !NILP (Fget (var, Qlast_arrow_position)))
13011 Fput (var, Qlast_arrow_position, Qt);
13012 Fput (var, Qlast_arrow_string, Qt);
13018 /* Return overlay arrow string to display at row.
13019 Return integer (bitmap number) for arrow bitmap in left fringe.
13020 Return nil if no overlay arrow. */
13022 static Lisp_Object
13023 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13025 Lisp_Object vlist;
13027 for (vlist = Voverlay_arrow_variable_list;
13028 CONSP (vlist);
13029 vlist = XCDR (vlist))
13031 Lisp_Object var = XCAR (vlist);
13032 Lisp_Object val;
13034 if (!SYMBOLP (var))
13035 continue;
13037 val = find_symbol_value (var);
13039 if (MARKERP (val)
13040 && current_buffer == XMARKER (val)->buffer
13041 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13043 if (FRAME_WINDOW_P (it->f)
13044 /* FIXME: if ROW->reversed_p is set, this should test
13045 the right fringe, not the left one. */
13046 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13048 #ifdef HAVE_WINDOW_SYSTEM
13049 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13051 int fringe_bitmap;
13052 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13053 return make_number (fringe_bitmap);
13055 #endif
13056 return make_number (-1); /* Use default arrow bitmap. */
13058 return overlay_arrow_string_or_property (var);
13062 return Qnil;
13065 /* Return 1 if point moved out of or into a composition. Otherwise
13066 return 0. PREV_BUF and PREV_PT are the last point buffer and
13067 position. BUF and PT are the current point buffer and position. */
13069 static int
13070 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13071 struct buffer *buf, ptrdiff_t pt)
13073 ptrdiff_t start, end;
13074 Lisp_Object prop;
13075 Lisp_Object buffer;
13077 XSETBUFFER (buffer, buf);
13078 /* Check a composition at the last point if point moved within the
13079 same buffer. */
13080 if (prev_buf == buf)
13082 if (prev_pt == pt)
13083 /* Point didn't move. */
13084 return 0;
13086 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13087 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13088 && composition_valid_p (start, end, prop)
13089 && start < prev_pt && end > prev_pt)
13090 /* The last point was within the composition. Return 1 iff
13091 point moved out of the composition. */
13092 return (pt <= start || pt >= end);
13095 /* Check a composition at the current point. */
13096 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13097 && find_composition (pt, -1, &start, &end, &prop, buffer)
13098 && composition_valid_p (start, end, prop)
13099 && start < pt && end > pt);
13102 /* Reconsider the clip changes of buffer which is displayed in W. */
13104 static void
13105 reconsider_clip_changes (struct window *w)
13107 struct buffer *b = XBUFFER (w->contents);
13109 if (b->clip_changed
13110 && w->window_end_valid
13111 && w->current_matrix->buffer == b
13112 && w->current_matrix->zv == BUF_ZV (b)
13113 && w->current_matrix->begv == BUF_BEGV (b))
13114 b->clip_changed = 0;
13116 /* If display wasn't paused, and W is not a tool bar window, see if
13117 point has been moved into or out of a composition. In that case,
13118 we set b->clip_changed to 1 to force updating the screen. If
13119 b->clip_changed has already been set to 1, we can skip this
13120 check. */
13121 if (!b->clip_changed && w->window_end_valid)
13123 ptrdiff_t pt = (w == XWINDOW (selected_window)
13124 ? PT : marker_position (w->pointm));
13126 if ((w->current_matrix->buffer != b || pt != w->last_point)
13127 && check_point_in_composition (w->current_matrix->buffer,
13128 w->last_point, b, pt))
13129 b->clip_changed = 1;
13133 static void
13134 propagate_buffer_redisplay (void)
13135 { /* Resetting b->text->redisplay is problematic!
13136 We can't just reset it in the case that some window that displays
13137 it has not been redisplayed; and such a window can stay
13138 unredisplayed for a long time if it's currently invisible.
13139 But we do want to reset it at the end of redisplay otherwise
13140 its displayed windows will keep being redisplayed over and over
13141 again.
13142 So we copy all b->text->redisplay flags up to their windows here,
13143 such that mark_window_display_accurate can safely reset
13144 b->text->redisplay. */
13145 Lisp_Object ws = window_list ();
13146 for (; CONSP (ws); ws = XCDR (ws))
13148 struct window *thisw = XWINDOW (XCAR (ws));
13149 struct buffer *thisb = XBUFFER (thisw->contents);
13150 if (thisb->text->redisplay)
13151 thisw->redisplay = true;
13155 #define STOP_POLLING \
13156 do { if (! polling_stopped_here) stop_polling (); \
13157 polling_stopped_here = 1; } while (0)
13159 #define RESUME_POLLING \
13160 do { if (polling_stopped_here) start_polling (); \
13161 polling_stopped_here = 0; } while (0)
13164 /* Perhaps in the future avoid recentering windows if it
13165 is not necessary; currently that causes some problems. */
13167 static void
13168 redisplay_internal (void)
13170 struct window *w = XWINDOW (selected_window);
13171 struct window *sw;
13172 struct frame *fr;
13173 int pending;
13174 bool must_finish = 0, match_p;
13175 struct text_pos tlbufpos, tlendpos;
13176 int number_of_visible_frames;
13177 ptrdiff_t count;
13178 struct frame *sf;
13179 int polling_stopped_here = 0;
13180 Lisp_Object tail, frame;
13182 /* True means redisplay has to consider all windows on all
13183 frames. False, only selected_window is considered. */
13184 bool consider_all_windows_p;
13186 /* True means redisplay has to redisplay the miniwindow. */
13187 bool update_miniwindow_p = false;
13189 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13191 /* No redisplay if running in batch mode or frame is not yet fully
13192 initialized, or redisplay is explicitly turned off by setting
13193 Vinhibit_redisplay. */
13194 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13195 || !NILP (Vinhibit_redisplay))
13196 return;
13198 /* Don't examine these until after testing Vinhibit_redisplay.
13199 When Emacs is shutting down, perhaps because its connection to
13200 X has dropped, we should not look at them at all. */
13201 fr = XFRAME (w->frame);
13202 sf = SELECTED_FRAME ();
13204 if (!fr->glyphs_initialized_p)
13205 return;
13207 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13208 if (popup_activated ())
13209 return;
13210 #endif
13212 /* I don't think this happens but let's be paranoid. */
13213 if (redisplaying_p)
13214 return;
13216 /* Record a function that clears redisplaying_p
13217 when we leave this function. */
13218 count = SPECPDL_INDEX ();
13219 record_unwind_protect_void (unwind_redisplay);
13220 redisplaying_p = 1;
13221 specbind (Qinhibit_free_realized_faces, Qnil);
13223 /* Record this function, so it appears on the profiler's backtraces. */
13224 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13226 FOR_EACH_FRAME (tail, frame)
13227 XFRAME (frame)->already_hscrolled_p = 0;
13229 retry:
13230 /* Remember the currently selected window. */
13231 sw = w;
13233 pending = 0;
13234 last_escape_glyph_frame = NULL;
13235 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13236 last_glyphless_glyph_frame = NULL;
13237 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13239 /* If face_change_count is non-zero, init_iterator will free all
13240 realized faces, which includes the faces referenced from current
13241 matrices. So, we can't reuse current matrices in this case. */
13242 if (face_change_count)
13243 windows_or_buffers_changed = 47;
13245 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13246 && FRAME_TTY (sf)->previous_frame != sf)
13248 /* Since frames on a single ASCII terminal share the same
13249 display area, displaying a different frame means redisplay
13250 the whole thing. */
13251 SET_FRAME_GARBAGED (sf);
13252 #ifndef DOS_NT
13253 set_tty_color_mode (FRAME_TTY (sf), sf);
13254 #endif
13255 FRAME_TTY (sf)->previous_frame = sf;
13258 /* Set the visible flags for all frames. Do this before checking for
13259 resized or garbaged frames; they want to know if their frames are
13260 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13261 number_of_visible_frames = 0;
13263 FOR_EACH_FRAME (tail, frame)
13265 struct frame *f = XFRAME (frame);
13267 if (FRAME_VISIBLE_P (f))
13269 ++number_of_visible_frames;
13270 /* Adjust matrices for visible frames only. */
13271 if (f->fonts_changed)
13273 adjust_frame_glyphs (f);
13274 f->fonts_changed = 0;
13276 /* If cursor type has been changed on the frame
13277 other than selected, consider all frames. */
13278 if (f != sf && f->cursor_type_changed)
13279 update_mode_lines = 31;
13281 clear_desired_matrices (f);
13284 /* Notice any pending interrupt request to change frame size. */
13285 do_pending_window_change (1);
13287 /* do_pending_window_change could change the selected_window due to
13288 frame resizing which makes the selected window too small. */
13289 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13290 sw = w;
13292 /* Clear frames marked as garbaged. */
13293 clear_garbaged_frames ();
13295 /* Build menubar and tool-bar items. */
13296 if (NILP (Vmemory_full))
13297 prepare_menu_bars ();
13299 reconsider_clip_changes (w);
13301 /* In most cases selected window displays current buffer. */
13302 match_p = XBUFFER (w->contents) == current_buffer;
13303 if (match_p)
13305 /* Detect case that we need to write or remove a star in the mode line. */
13306 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13307 w->update_mode_line = 1;
13309 if (mode_line_update_needed (w))
13310 w->update_mode_line = 1;
13313 /* Normally the message* functions will have already displayed and
13314 updated the echo area, but the frame may have been trashed, or
13315 the update may have been preempted, so display the echo area
13316 again here. Checking message_cleared_p captures the case that
13317 the echo area should be cleared. */
13318 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13319 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13320 || (message_cleared_p
13321 && minibuf_level == 0
13322 /* If the mini-window is currently selected, this means the
13323 echo-area doesn't show through. */
13324 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13326 int window_height_changed_p = echo_area_display (0);
13328 if (message_cleared_p)
13329 update_miniwindow_p = true;
13331 must_finish = 1;
13333 /* If we don't display the current message, don't clear the
13334 message_cleared_p flag, because, if we did, we wouldn't clear
13335 the echo area in the next redisplay which doesn't preserve
13336 the echo area. */
13337 if (!display_last_displayed_message_p)
13338 message_cleared_p = 0;
13340 if (window_height_changed_p)
13342 windows_or_buffers_changed = 50;
13344 /* If window configuration was changed, frames may have been
13345 marked garbaged. Clear them or we will experience
13346 surprises wrt scrolling. */
13347 clear_garbaged_frames ();
13350 else if (EQ (selected_window, minibuf_window)
13351 && (current_buffer->clip_changed || window_outdated (w))
13352 && resize_mini_window (w, 0))
13354 /* Resized active mini-window to fit the size of what it is
13355 showing if its contents might have changed. */
13356 must_finish = 1;
13358 /* If window configuration was changed, frames may have been
13359 marked garbaged. Clear them or we will experience
13360 surprises wrt scrolling. */
13361 clear_garbaged_frames ();
13364 if (windows_or_buffers_changed && !update_mode_lines)
13365 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13366 only the windows's contents needs to be refreshed, or whether the
13367 mode-lines also need a refresh. */
13368 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13369 ? REDISPLAY_SOME : 32);
13371 /* If specs for an arrow have changed, do thorough redisplay
13372 to ensure we remove any arrow that should no longer exist. */
13373 if (overlay_arrows_changed_p ())
13374 /* Apparently, this is the only case where we update other windows,
13375 without updating other mode-lines. */
13376 windows_or_buffers_changed = 49;
13378 consider_all_windows_p = (update_mode_lines
13379 || windows_or_buffers_changed);
13381 #define AINC(a,i) \
13382 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13383 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13385 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13386 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13388 /* Optimize the case that only the line containing the cursor in the
13389 selected window has changed. Variables starting with this_ are
13390 set in display_line and record information about the line
13391 containing the cursor. */
13392 tlbufpos = this_line_start_pos;
13393 tlendpos = this_line_end_pos;
13394 if (!consider_all_windows_p
13395 && CHARPOS (tlbufpos) > 0
13396 && !w->update_mode_line
13397 && !current_buffer->clip_changed
13398 && !current_buffer->prevent_redisplay_optimizations_p
13399 && FRAME_VISIBLE_P (XFRAME (w->frame))
13400 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13401 && !XFRAME (w->frame)->cursor_type_changed
13402 /* Make sure recorded data applies to current buffer, etc. */
13403 && this_line_buffer == current_buffer
13404 && match_p
13405 && !w->force_start
13406 && !w->optional_new_start
13407 /* Point must be on the line that we have info recorded about. */
13408 && PT >= CHARPOS (tlbufpos)
13409 && PT <= Z - CHARPOS (tlendpos)
13410 /* All text outside that line, including its final newline,
13411 must be unchanged. */
13412 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13413 CHARPOS (tlendpos)))
13415 if (CHARPOS (tlbufpos) > BEGV
13416 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13417 && (CHARPOS (tlbufpos) == ZV
13418 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13419 /* Former continuation line has disappeared by becoming empty. */
13420 goto cancel;
13421 else if (window_outdated (w) || MINI_WINDOW_P (w))
13423 /* We have to handle the case of continuation around a
13424 wide-column character (see the comment in indent.c around
13425 line 1340).
13427 For instance, in the following case:
13429 -------- Insert --------
13430 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13431 J_I_ ==> J_I_ `^^' are cursors.
13432 ^^ ^^
13433 -------- --------
13435 As we have to redraw the line above, we cannot use this
13436 optimization. */
13438 struct it it;
13439 int line_height_before = this_line_pixel_height;
13441 /* Note that start_display will handle the case that the
13442 line starting at tlbufpos is a continuation line. */
13443 start_display (&it, w, tlbufpos);
13445 /* Implementation note: It this still necessary? */
13446 if (it.current_x != this_line_start_x)
13447 goto cancel;
13449 TRACE ((stderr, "trying display optimization 1\n"));
13450 w->cursor.vpos = -1;
13451 overlay_arrow_seen = 0;
13452 it.vpos = this_line_vpos;
13453 it.current_y = this_line_y;
13454 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13455 display_line (&it);
13457 /* If line contains point, is not continued,
13458 and ends at same distance from eob as before, we win. */
13459 if (w->cursor.vpos >= 0
13460 /* Line is not continued, otherwise this_line_start_pos
13461 would have been set to 0 in display_line. */
13462 && CHARPOS (this_line_start_pos)
13463 /* Line ends as before. */
13464 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13465 /* Line has same height as before. Otherwise other lines
13466 would have to be shifted up or down. */
13467 && this_line_pixel_height == line_height_before)
13469 /* If this is not the window's last line, we must adjust
13470 the charstarts of the lines below. */
13471 if (it.current_y < it.last_visible_y)
13473 struct glyph_row *row
13474 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13475 ptrdiff_t delta, delta_bytes;
13477 /* We used to distinguish between two cases here,
13478 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13479 when the line ends in a newline or the end of the
13480 buffer's accessible portion. But both cases did
13481 the same, so they were collapsed. */
13482 delta = (Z
13483 - CHARPOS (tlendpos)
13484 - MATRIX_ROW_START_CHARPOS (row));
13485 delta_bytes = (Z_BYTE
13486 - BYTEPOS (tlendpos)
13487 - MATRIX_ROW_START_BYTEPOS (row));
13489 increment_matrix_positions (w->current_matrix,
13490 this_line_vpos + 1,
13491 w->current_matrix->nrows,
13492 delta, delta_bytes);
13495 /* If this row displays text now but previously didn't,
13496 or vice versa, w->window_end_vpos may have to be
13497 adjusted. */
13498 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13500 if (w->window_end_vpos < this_line_vpos)
13501 w->window_end_vpos = this_line_vpos;
13503 else if (w->window_end_vpos == this_line_vpos
13504 && this_line_vpos > 0)
13505 w->window_end_vpos = this_line_vpos - 1;
13506 w->window_end_valid = 0;
13508 /* Update hint: No need to try to scroll in update_window. */
13509 w->desired_matrix->no_scrolling_p = 1;
13511 #ifdef GLYPH_DEBUG
13512 *w->desired_matrix->method = 0;
13513 debug_method_add (w, "optimization 1");
13514 #endif
13515 #ifdef HAVE_WINDOW_SYSTEM
13516 update_window_fringes (w, 0);
13517 #endif
13518 goto update;
13520 else
13521 goto cancel;
13523 else if (/* Cursor position hasn't changed. */
13524 PT == w->last_point
13525 /* Make sure the cursor was last displayed
13526 in this window. Otherwise we have to reposition it. */
13528 /* PXW: Must be converted to pixels, probably. */
13529 && 0 <= w->cursor.vpos
13530 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13532 if (!must_finish)
13534 do_pending_window_change (1);
13535 /* If selected_window changed, redisplay again. */
13536 if (WINDOWP (selected_window)
13537 && (w = XWINDOW (selected_window)) != sw)
13538 goto retry;
13540 /* We used to always goto end_of_redisplay here, but this
13541 isn't enough if we have a blinking cursor. */
13542 if (w->cursor_off_p == w->last_cursor_off_p)
13543 goto end_of_redisplay;
13545 goto update;
13547 /* If highlighting the region, or if the cursor is in the echo area,
13548 then we can't just move the cursor. */
13549 else if (NILP (Vshow_trailing_whitespace)
13550 && !cursor_in_echo_area)
13552 struct it it;
13553 struct glyph_row *row;
13555 /* Skip from tlbufpos to PT and see where it is. Note that
13556 PT may be in invisible text. If so, we will end at the
13557 next visible position. */
13558 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13559 NULL, DEFAULT_FACE_ID);
13560 it.current_x = this_line_start_x;
13561 it.current_y = this_line_y;
13562 it.vpos = this_line_vpos;
13564 /* The call to move_it_to stops in front of PT, but
13565 moves over before-strings. */
13566 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13568 if (it.vpos == this_line_vpos
13569 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13570 row->enabled_p))
13572 eassert (this_line_vpos == it.vpos);
13573 eassert (this_line_y == it.current_y);
13574 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13575 #ifdef GLYPH_DEBUG
13576 *w->desired_matrix->method = 0;
13577 debug_method_add (w, "optimization 3");
13578 #endif
13579 goto update;
13581 else
13582 goto cancel;
13585 cancel:
13586 /* Text changed drastically or point moved off of line. */
13587 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13590 CHARPOS (this_line_start_pos) = 0;
13591 ++clear_face_cache_count;
13592 #ifdef HAVE_WINDOW_SYSTEM
13593 ++clear_image_cache_count;
13594 #endif
13596 /* Build desired matrices, and update the display. If
13597 consider_all_windows_p is non-zero, do it for all windows on all
13598 frames. Otherwise do it for selected_window, only. */
13600 if (consider_all_windows_p)
13602 FOR_EACH_FRAME (tail, frame)
13603 XFRAME (frame)->updated_p = 0;
13605 propagate_buffer_redisplay ();
13607 FOR_EACH_FRAME (tail, frame)
13609 struct frame *f = XFRAME (frame);
13611 /* We don't have to do anything for unselected terminal
13612 frames. */
13613 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13614 && !EQ (FRAME_TTY (f)->top_frame, frame))
13615 continue;
13617 retry_frame:
13619 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13621 bool gcscrollbars
13622 /* Only GC scrollbars when we redisplay the whole frame. */
13623 = f->redisplay || !REDISPLAY_SOME_P ();
13624 /* Mark all the scroll bars to be removed; we'll redeem
13625 the ones we want when we redisplay their windows. */
13626 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13627 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13629 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13630 redisplay_windows (FRAME_ROOT_WINDOW (f));
13631 /* Remember that the invisible frames need to be redisplayed next
13632 time they're visible. */
13633 else if (!REDISPLAY_SOME_P ())
13634 f->redisplay = true;
13636 /* The X error handler may have deleted that frame. */
13637 if (!FRAME_LIVE_P (f))
13638 continue;
13640 /* Any scroll bars which redisplay_windows should have
13641 nuked should now go away. */
13642 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13643 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13645 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13647 /* If fonts changed on visible frame, display again. */
13648 if (f->fonts_changed)
13650 adjust_frame_glyphs (f);
13651 f->fonts_changed = 0;
13652 goto retry_frame;
13655 /* See if we have to hscroll. */
13656 if (!f->already_hscrolled_p)
13658 f->already_hscrolled_p = 1;
13659 if (hscroll_windows (f->root_window))
13660 goto retry_frame;
13663 /* Prevent various kinds of signals during display
13664 update. stdio is not robust about handling
13665 signals, which can cause an apparent I/O error. */
13666 if (interrupt_input)
13667 unrequest_sigio ();
13668 STOP_POLLING;
13670 pending |= update_frame (f, 0, 0);
13671 f->cursor_type_changed = 0;
13672 f->updated_p = 1;
13677 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13679 if (!pending)
13681 /* Do the mark_window_display_accurate after all windows have
13682 been redisplayed because this call resets flags in buffers
13683 which are needed for proper redisplay. */
13684 FOR_EACH_FRAME (tail, frame)
13686 struct frame *f = XFRAME (frame);
13687 if (f->updated_p)
13689 f->redisplay = false;
13690 mark_window_display_accurate (f->root_window, 1);
13691 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13692 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13697 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13699 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13700 struct frame *mini_frame;
13702 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13703 /* Use list_of_error, not Qerror, so that
13704 we catch only errors and don't run the debugger. */
13705 internal_condition_case_1 (redisplay_window_1, selected_window,
13706 list_of_error,
13707 redisplay_window_error);
13708 if (update_miniwindow_p)
13709 internal_condition_case_1 (redisplay_window_1, mini_window,
13710 list_of_error,
13711 redisplay_window_error);
13713 /* Compare desired and current matrices, perform output. */
13715 update:
13716 /* If fonts changed, display again. */
13717 if (sf->fonts_changed)
13718 goto retry;
13720 /* Prevent various kinds of signals during display update.
13721 stdio is not robust about handling signals,
13722 which can cause an apparent I/O error. */
13723 if (interrupt_input)
13724 unrequest_sigio ();
13725 STOP_POLLING;
13727 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13729 if (hscroll_windows (selected_window))
13730 goto retry;
13732 XWINDOW (selected_window)->must_be_updated_p = true;
13733 pending = update_frame (sf, 0, 0);
13734 sf->cursor_type_changed = 0;
13737 /* We may have called echo_area_display at the top of this
13738 function. If the echo area is on another frame, that may
13739 have put text on a frame other than the selected one, so the
13740 above call to update_frame would not have caught it. Catch
13741 it here. */
13742 mini_window = FRAME_MINIBUF_WINDOW (sf);
13743 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13745 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13747 XWINDOW (mini_window)->must_be_updated_p = true;
13748 pending |= update_frame (mini_frame, 0, 0);
13749 mini_frame->cursor_type_changed = 0;
13750 if (!pending && hscroll_windows (mini_window))
13751 goto retry;
13755 /* If display was paused because of pending input, make sure we do a
13756 thorough update the next time. */
13757 if (pending)
13759 /* Prevent the optimization at the beginning of
13760 redisplay_internal that tries a single-line update of the
13761 line containing the cursor in the selected window. */
13762 CHARPOS (this_line_start_pos) = 0;
13764 /* Let the overlay arrow be updated the next time. */
13765 update_overlay_arrows (0);
13767 /* If we pause after scrolling, some rows in the current
13768 matrices of some windows are not valid. */
13769 if (!WINDOW_FULL_WIDTH_P (w)
13770 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13771 update_mode_lines = 36;
13773 else
13775 if (!consider_all_windows_p)
13777 /* This has already been done above if
13778 consider_all_windows_p is set. */
13779 if (XBUFFER (w->contents)->text->redisplay
13780 && buffer_window_count (XBUFFER (w->contents)) > 1)
13781 /* This can happen if b->text->redisplay was set during
13782 jit-lock. */
13783 propagate_buffer_redisplay ();
13784 mark_window_display_accurate_1 (w, 1);
13786 /* Say overlay arrows are up to date. */
13787 update_overlay_arrows (1);
13789 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13790 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13793 update_mode_lines = 0;
13794 windows_or_buffers_changed = 0;
13797 /* Start SIGIO interrupts coming again. Having them off during the
13798 code above makes it less likely one will discard output, but not
13799 impossible, since there might be stuff in the system buffer here.
13800 But it is much hairier to try to do anything about that. */
13801 if (interrupt_input)
13802 request_sigio ();
13803 RESUME_POLLING;
13805 /* If a frame has become visible which was not before, redisplay
13806 again, so that we display it. Expose events for such a frame
13807 (which it gets when becoming visible) don't call the parts of
13808 redisplay constructing glyphs, so simply exposing a frame won't
13809 display anything in this case. So, we have to display these
13810 frames here explicitly. */
13811 if (!pending)
13813 int new_count = 0;
13815 FOR_EACH_FRAME (tail, frame)
13817 if (XFRAME (frame)->visible)
13818 new_count++;
13821 if (new_count != number_of_visible_frames)
13822 windows_or_buffers_changed = 52;
13825 /* Change frame size now if a change is pending. */
13826 do_pending_window_change (1);
13828 /* If we just did a pending size change, or have additional
13829 visible frames, or selected_window changed, redisplay again. */
13830 if ((windows_or_buffers_changed && !pending)
13831 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13832 goto retry;
13834 /* Clear the face and image caches.
13836 We used to do this only if consider_all_windows_p. But the cache
13837 needs to be cleared if a timer creates images in the current
13838 buffer (e.g. the test case in Bug#6230). */
13840 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13842 clear_face_cache (0);
13843 clear_face_cache_count = 0;
13846 #ifdef HAVE_WINDOW_SYSTEM
13847 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13849 clear_image_caches (Qnil);
13850 clear_image_cache_count = 0;
13852 #endif /* HAVE_WINDOW_SYSTEM */
13854 end_of_redisplay:
13855 if (interrupt_input && interrupts_deferred)
13856 request_sigio ();
13858 unbind_to (count, Qnil);
13859 RESUME_POLLING;
13863 /* Redisplay, but leave alone any recent echo area message unless
13864 another message has been requested in its place.
13866 This is useful in situations where you need to redisplay but no
13867 user action has occurred, making it inappropriate for the message
13868 area to be cleared. See tracking_off and
13869 wait_reading_process_output for examples of these situations.
13871 FROM_WHERE is an integer saying from where this function was
13872 called. This is useful for debugging. */
13874 void
13875 redisplay_preserve_echo_area (int from_where)
13877 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13879 if (!NILP (echo_area_buffer[1]))
13881 /* We have a previously displayed message, but no current
13882 message. Redisplay the previous message. */
13883 display_last_displayed_message_p = 1;
13884 redisplay_internal ();
13885 display_last_displayed_message_p = 0;
13887 else
13888 redisplay_internal ();
13890 flush_frame (SELECTED_FRAME ());
13894 /* Function registered with record_unwind_protect in redisplay_internal. */
13896 static void
13897 unwind_redisplay (void)
13899 redisplaying_p = 0;
13903 /* Mark the display of leaf window W as accurate or inaccurate.
13904 If ACCURATE_P is non-zero mark display of W as accurate. If
13905 ACCURATE_P is zero, arrange for W to be redisplayed the next
13906 time redisplay_internal is called. */
13908 static void
13909 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13911 struct buffer *b = XBUFFER (w->contents);
13913 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13914 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13915 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13917 if (accurate_p)
13919 b->clip_changed = false;
13920 b->prevent_redisplay_optimizations_p = false;
13921 eassert (buffer_window_count (b) > 0);
13922 /* Resetting b->text->redisplay is problematic!
13923 In order to make it safer to do it here, redisplay_internal must
13924 have copied all b->text->redisplay to their respective windows. */
13925 b->text->redisplay = false;
13927 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13928 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13929 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13930 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13932 w->current_matrix->buffer = b;
13933 w->current_matrix->begv = BUF_BEGV (b);
13934 w->current_matrix->zv = BUF_ZV (b);
13936 w->last_cursor_vpos = w->cursor.vpos;
13937 w->last_cursor_off_p = w->cursor_off_p;
13939 if (w == XWINDOW (selected_window))
13940 w->last_point = BUF_PT (b);
13941 else
13942 w->last_point = marker_position (w->pointm);
13944 w->window_end_valid = true;
13945 w->update_mode_line = false;
13948 w->redisplay = !accurate_p;
13952 /* Mark the display of windows in the window tree rooted at WINDOW as
13953 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13954 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13955 be redisplayed the next time redisplay_internal is called. */
13957 void
13958 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13960 struct window *w;
13962 for (; !NILP (window); window = w->next)
13964 w = XWINDOW (window);
13965 if (WINDOWP (w->contents))
13966 mark_window_display_accurate (w->contents, accurate_p);
13967 else
13968 mark_window_display_accurate_1 (w, accurate_p);
13971 if (accurate_p)
13972 update_overlay_arrows (1);
13973 else
13974 /* Force a thorough redisplay the next time by setting
13975 last_arrow_position and last_arrow_string to t, which is
13976 unequal to any useful value of Voverlay_arrow_... */
13977 update_overlay_arrows (-1);
13981 /* Return value in display table DP (Lisp_Char_Table *) for character
13982 C. Since a display table doesn't have any parent, we don't have to
13983 follow parent. Do not call this function directly but use the
13984 macro DISP_CHAR_VECTOR. */
13986 Lisp_Object
13987 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13989 Lisp_Object val;
13991 if (ASCII_CHAR_P (c))
13993 val = dp->ascii;
13994 if (SUB_CHAR_TABLE_P (val))
13995 val = XSUB_CHAR_TABLE (val)->contents[c];
13997 else
13999 Lisp_Object table;
14001 XSETCHAR_TABLE (table, dp);
14002 val = char_table_ref (table, c);
14004 if (NILP (val))
14005 val = dp->defalt;
14006 return val;
14011 /***********************************************************************
14012 Window Redisplay
14013 ***********************************************************************/
14015 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14017 static void
14018 redisplay_windows (Lisp_Object window)
14020 while (!NILP (window))
14022 struct window *w = XWINDOW (window);
14024 if (WINDOWP (w->contents))
14025 redisplay_windows (w->contents);
14026 else if (BUFFERP (w->contents))
14028 displayed_buffer = XBUFFER (w->contents);
14029 /* Use list_of_error, not Qerror, so that
14030 we catch only errors and don't run the debugger. */
14031 internal_condition_case_1 (redisplay_window_0, window,
14032 list_of_error,
14033 redisplay_window_error);
14036 window = w->next;
14040 static Lisp_Object
14041 redisplay_window_error (Lisp_Object ignore)
14043 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14044 return Qnil;
14047 static Lisp_Object
14048 redisplay_window_0 (Lisp_Object window)
14050 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14051 redisplay_window (window, false);
14052 return Qnil;
14055 static Lisp_Object
14056 redisplay_window_1 (Lisp_Object window)
14058 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14059 redisplay_window (window, true);
14060 return Qnil;
14064 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14065 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14066 which positions recorded in ROW differ from current buffer
14067 positions.
14069 Return 0 if cursor is not on this row, 1 otherwise. */
14071 static int
14072 set_cursor_from_row (struct window *w, struct glyph_row *row,
14073 struct glyph_matrix *matrix,
14074 ptrdiff_t delta, ptrdiff_t delta_bytes,
14075 int dy, int dvpos)
14077 struct glyph *glyph = row->glyphs[TEXT_AREA];
14078 struct glyph *end = glyph + row->used[TEXT_AREA];
14079 struct glyph *cursor = NULL;
14080 /* The last known character position in row. */
14081 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14082 int x = row->x;
14083 ptrdiff_t pt_old = PT - delta;
14084 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14085 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14086 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14087 /* A glyph beyond the edge of TEXT_AREA which we should never
14088 touch. */
14089 struct glyph *glyphs_end = end;
14090 /* Non-zero means we've found a match for cursor position, but that
14091 glyph has the avoid_cursor_p flag set. */
14092 int match_with_avoid_cursor = 0;
14093 /* Non-zero means we've seen at least one glyph that came from a
14094 display string. */
14095 int string_seen = 0;
14096 /* Largest and smallest buffer positions seen so far during scan of
14097 glyph row. */
14098 ptrdiff_t bpos_max = pos_before;
14099 ptrdiff_t bpos_min = pos_after;
14100 /* Last buffer position covered by an overlay string with an integer
14101 `cursor' property. */
14102 ptrdiff_t bpos_covered = 0;
14103 /* Non-zero means the display string on which to display the cursor
14104 comes from a text property, not from an overlay. */
14105 int string_from_text_prop = 0;
14107 /* Don't even try doing anything if called for a mode-line or
14108 header-line row, since the rest of the code isn't prepared to
14109 deal with such calamities. */
14110 eassert (!row->mode_line_p);
14111 if (row->mode_line_p)
14112 return 0;
14114 /* Skip over glyphs not having an object at the start and the end of
14115 the row. These are special glyphs like truncation marks on
14116 terminal frames. */
14117 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14119 if (!row->reversed_p)
14121 while (glyph < end
14122 && INTEGERP (glyph->object)
14123 && glyph->charpos < 0)
14125 x += glyph->pixel_width;
14126 ++glyph;
14128 while (end > glyph
14129 && INTEGERP ((end - 1)->object)
14130 /* CHARPOS is zero for blanks and stretch glyphs
14131 inserted by extend_face_to_end_of_line. */
14132 && (end - 1)->charpos <= 0)
14133 --end;
14134 glyph_before = glyph - 1;
14135 glyph_after = end;
14137 else
14139 struct glyph *g;
14141 /* If the glyph row is reversed, we need to process it from back
14142 to front, so swap the edge pointers. */
14143 glyphs_end = end = glyph - 1;
14144 glyph += row->used[TEXT_AREA] - 1;
14146 while (glyph > end + 1
14147 && INTEGERP (glyph->object)
14148 && glyph->charpos < 0)
14150 --glyph;
14151 x -= glyph->pixel_width;
14153 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14154 --glyph;
14155 /* By default, in reversed rows we put the cursor on the
14156 rightmost (first in the reading order) glyph. */
14157 for (g = end + 1; g < glyph; g++)
14158 x += g->pixel_width;
14159 while (end < glyph
14160 && INTEGERP ((end + 1)->object)
14161 && (end + 1)->charpos <= 0)
14162 ++end;
14163 glyph_before = glyph + 1;
14164 glyph_after = end;
14167 else if (row->reversed_p)
14169 /* In R2L rows that don't display text, put the cursor on the
14170 rightmost glyph. Case in point: an empty last line that is
14171 part of an R2L paragraph. */
14172 cursor = end - 1;
14173 /* Avoid placing the cursor on the last glyph of the row, where
14174 on terminal frames we hold the vertical border between
14175 adjacent windows. */
14176 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14177 && !WINDOW_RIGHTMOST_P (w)
14178 && cursor == row->glyphs[LAST_AREA] - 1)
14179 cursor--;
14180 x = -1; /* will be computed below, at label compute_x */
14183 /* Step 1: Try to find the glyph whose character position
14184 corresponds to point. If that's not possible, find 2 glyphs
14185 whose character positions are the closest to point, one before
14186 point, the other after it. */
14187 if (!row->reversed_p)
14188 while (/* not marched to end of glyph row */
14189 glyph < end
14190 /* glyph was not inserted by redisplay for internal purposes */
14191 && !INTEGERP (glyph->object))
14193 if (BUFFERP (glyph->object))
14195 ptrdiff_t dpos = glyph->charpos - pt_old;
14197 if (glyph->charpos > bpos_max)
14198 bpos_max = glyph->charpos;
14199 if (glyph->charpos < bpos_min)
14200 bpos_min = glyph->charpos;
14201 if (!glyph->avoid_cursor_p)
14203 /* If we hit point, we've found the glyph on which to
14204 display the cursor. */
14205 if (dpos == 0)
14207 match_with_avoid_cursor = 0;
14208 break;
14210 /* See if we've found a better approximation to
14211 POS_BEFORE or to POS_AFTER. */
14212 if (0 > dpos && dpos > pos_before - pt_old)
14214 pos_before = glyph->charpos;
14215 glyph_before = glyph;
14217 else if (0 < dpos && dpos < pos_after - pt_old)
14219 pos_after = glyph->charpos;
14220 glyph_after = glyph;
14223 else if (dpos == 0)
14224 match_with_avoid_cursor = 1;
14226 else if (STRINGP (glyph->object))
14228 Lisp_Object chprop;
14229 ptrdiff_t glyph_pos = glyph->charpos;
14231 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14232 glyph->object);
14233 if (!NILP (chprop))
14235 /* If the string came from a `display' text property,
14236 look up the buffer position of that property and
14237 use that position to update bpos_max, as if we
14238 actually saw such a position in one of the row's
14239 glyphs. This helps with supporting integer values
14240 of `cursor' property on the display string in
14241 situations where most or all of the row's buffer
14242 text is completely covered by display properties,
14243 so that no glyph with valid buffer positions is
14244 ever seen in the row. */
14245 ptrdiff_t prop_pos =
14246 string_buffer_position_lim (glyph->object, pos_before,
14247 pos_after, 0);
14249 if (prop_pos >= pos_before)
14250 bpos_max = prop_pos - 1;
14252 if (INTEGERP (chprop))
14254 bpos_covered = bpos_max + XINT (chprop);
14255 /* If the `cursor' property covers buffer positions up
14256 to and including point, we should display cursor on
14257 this glyph. Note that, if a `cursor' property on one
14258 of the string's characters has an integer value, we
14259 will break out of the loop below _before_ we get to
14260 the position match above. IOW, integer values of
14261 the `cursor' property override the "exact match for
14262 point" strategy of positioning the cursor. */
14263 /* Implementation note: bpos_max == pt_old when, e.g.,
14264 we are in an empty line, where bpos_max is set to
14265 MATRIX_ROW_START_CHARPOS, see above. */
14266 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14268 cursor = glyph;
14269 break;
14273 string_seen = 1;
14275 x += glyph->pixel_width;
14276 ++glyph;
14278 else if (glyph > end) /* row is reversed */
14279 while (!INTEGERP (glyph->object))
14281 if (BUFFERP (glyph->object))
14283 ptrdiff_t dpos = glyph->charpos - pt_old;
14285 if (glyph->charpos > bpos_max)
14286 bpos_max = glyph->charpos;
14287 if (glyph->charpos < bpos_min)
14288 bpos_min = glyph->charpos;
14289 if (!glyph->avoid_cursor_p)
14291 if (dpos == 0)
14293 match_with_avoid_cursor = 0;
14294 break;
14296 if (0 > dpos && dpos > pos_before - pt_old)
14298 pos_before = glyph->charpos;
14299 glyph_before = glyph;
14301 else if (0 < dpos && dpos < pos_after - pt_old)
14303 pos_after = glyph->charpos;
14304 glyph_after = glyph;
14307 else if (dpos == 0)
14308 match_with_avoid_cursor = 1;
14310 else if (STRINGP (glyph->object))
14312 Lisp_Object chprop;
14313 ptrdiff_t glyph_pos = glyph->charpos;
14315 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14316 glyph->object);
14317 if (!NILP (chprop))
14319 ptrdiff_t prop_pos =
14320 string_buffer_position_lim (glyph->object, pos_before,
14321 pos_after, 0);
14323 if (prop_pos >= pos_before)
14324 bpos_max = prop_pos - 1;
14326 if (INTEGERP (chprop))
14328 bpos_covered = bpos_max + XINT (chprop);
14329 /* If the `cursor' property covers buffer positions up
14330 to and including point, we should display cursor on
14331 this glyph. */
14332 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14334 cursor = glyph;
14335 break;
14338 string_seen = 1;
14340 --glyph;
14341 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14343 x--; /* can't use any pixel_width */
14344 break;
14346 x -= glyph->pixel_width;
14349 /* Step 2: If we didn't find an exact match for point, we need to
14350 look for a proper place to put the cursor among glyphs between
14351 GLYPH_BEFORE and GLYPH_AFTER. */
14352 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14353 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14354 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14356 /* An empty line has a single glyph whose OBJECT is zero and
14357 whose CHARPOS is the position of a newline on that line.
14358 Note that on a TTY, there are more glyphs after that, which
14359 were produced by extend_face_to_end_of_line, but their
14360 CHARPOS is zero or negative. */
14361 int empty_line_p =
14362 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14363 && INTEGERP (glyph->object) && glyph->charpos > 0
14364 /* On a TTY, continued and truncated rows also have a glyph at
14365 their end whose OBJECT is zero and whose CHARPOS is
14366 positive (the continuation and truncation glyphs), but such
14367 rows are obviously not "empty". */
14368 && !(row->continued_p || row->truncated_on_right_p);
14370 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14372 ptrdiff_t ellipsis_pos;
14374 /* Scan back over the ellipsis glyphs. */
14375 if (!row->reversed_p)
14377 ellipsis_pos = (glyph - 1)->charpos;
14378 while (glyph > row->glyphs[TEXT_AREA]
14379 && (glyph - 1)->charpos == ellipsis_pos)
14380 glyph--, x -= glyph->pixel_width;
14381 /* That loop always goes one position too far, including
14382 the glyph before the ellipsis. So scan forward over
14383 that one. */
14384 x += glyph->pixel_width;
14385 glyph++;
14387 else /* row is reversed */
14389 ellipsis_pos = (glyph + 1)->charpos;
14390 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14391 && (glyph + 1)->charpos == ellipsis_pos)
14392 glyph++, x += glyph->pixel_width;
14393 x -= glyph->pixel_width;
14394 glyph--;
14397 else if (match_with_avoid_cursor)
14399 cursor = glyph_after;
14400 x = -1;
14402 else if (string_seen)
14404 int incr = row->reversed_p ? -1 : +1;
14406 /* Need to find the glyph that came out of a string which is
14407 present at point. That glyph is somewhere between
14408 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14409 positioned between POS_BEFORE and POS_AFTER in the
14410 buffer. */
14411 struct glyph *start, *stop;
14412 ptrdiff_t pos = pos_before;
14414 x = -1;
14416 /* If the row ends in a newline from a display string,
14417 reordering could have moved the glyphs belonging to the
14418 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14419 in this case we extend the search to the last glyph in
14420 the row that was not inserted by redisplay. */
14421 if (row->ends_in_newline_from_string_p)
14423 glyph_after = end;
14424 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14427 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14428 correspond to POS_BEFORE and POS_AFTER, respectively. We
14429 need START and STOP in the order that corresponds to the
14430 row's direction as given by its reversed_p flag. If the
14431 directionality of characters between POS_BEFORE and
14432 POS_AFTER is the opposite of the row's base direction,
14433 these characters will have been reordered for display,
14434 and we need to reverse START and STOP. */
14435 if (!row->reversed_p)
14437 start = min (glyph_before, glyph_after);
14438 stop = max (glyph_before, glyph_after);
14440 else
14442 start = max (glyph_before, glyph_after);
14443 stop = min (glyph_before, glyph_after);
14445 for (glyph = start + incr;
14446 row->reversed_p ? glyph > stop : glyph < stop; )
14449 /* Any glyphs that come from the buffer are here because
14450 of bidi reordering. Skip them, and only pay
14451 attention to glyphs that came from some string. */
14452 if (STRINGP (glyph->object))
14454 Lisp_Object str;
14455 ptrdiff_t tem;
14456 /* If the display property covers the newline, we
14457 need to search for it one position farther. */
14458 ptrdiff_t lim = pos_after
14459 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14461 string_from_text_prop = 0;
14462 str = glyph->object;
14463 tem = string_buffer_position_lim (str, pos, lim, 0);
14464 if (tem == 0 /* from overlay */
14465 || pos <= tem)
14467 /* If the string from which this glyph came is
14468 found in the buffer at point, or at position
14469 that is closer to point than pos_after, then
14470 we've found the glyph we've been looking for.
14471 If it comes from an overlay (tem == 0), and
14472 it has the `cursor' property on one of its
14473 glyphs, record that glyph as a candidate for
14474 displaying the cursor. (As in the
14475 unidirectional version, we will display the
14476 cursor on the last candidate we find.) */
14477 if (tem == 0
14478 || tem == pt_old
14479 || (tem - pt_old > 0 && tem < pos_after))
14481 /* The glyphs from this string could have
14482 been reordered. Find the one with the
14483 smallest string position. Or there could
14484 be a character in the string with the
14485 `cursor' property, which means display
14486 cursor on that character's glyph. */
14487 ptrdiff_t strpos = glyph->charpos;
14489 if (tem)
14491 cursor = glyph;
14492 string_from_text_prop = 1;
14494 for ( ;
14495 (row->reversed_p ? glyph > stop : glyph < stop)
14496 && EQ (glyph->object, str);
14497 glyph += incr)
14499 Lisp_Object cprop;
14500 ptrdiff_t gpos = glyph->charpos;
14502 cprop = Fget_char_property (make_number (gpos),
14503 Qcursor,
14504 glyph->object);
14505 if (!NILP (cprop))
14507 cursor = glyph;
14508 break;
14510 if (tem && glyph->charpos < strpos)
14512 strpos = glyph->charpos;
14513 cursor = glyph;
14517 if (tem == pt_old
14518 || (tem - pt_old > 0 && tem < pos_after))
14519 goto compute_x;
14521 if (tem)
14522 pos = tem + 1; /* don't find previous instances */
14524 /* This string is not what we want; skip all of the
14525 glyphs that came from it. */
14526 while ((row->reversed_p ? glyph > stop : glyph < stop)
14527 && EQ (glyph->object, str))
14528 glyph += incr;
14530 else
14531 glyph += incr;
14534 /* If we reached the end of the line, and END was from a string,
14535 the cursor is not on this line. */
14536 if (cursor == NULL
14537 && (row->reversed_p ? glyph <= end : glyph >= end)
14538 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14539 && STRINGP (end->object)
14540 && row->continued_p)
14541 return 0;
14543 /* A truncated row may not include PT among its character positions.
14544 Setting the cursor inside the scroll margin will trigger
14545 recalculation of hscroll in hscroll_window_tree. But if a
14546 display string covers point, defer to the string-handling
14547 code below to figure this out. */
14548 else if (row->truncated_on_left_p && pt_old < bpos_min)
14550 cursor = glyph_before;
14551 x = -1;
14553 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14554 /* Zero-width characters produce no glyphs. */
14555 || (!empty_line_p
14556 && (row->reversed_p
14557 ? glyph_after > glyphs_end
14558 : glyph_after < glyphs_end)))
14560 cursor = glyph_after;
14561 x = -1;
14565 compute_x:
14566 if (cursor != NULL)
14567 glyph = cursor;
14568 else if (glyph == glyphs_end
14569 && pos_before == pos_after
14570 && STRINGP ((row->reversed_p
14571 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14572 : row->glyphs[TEXT_AREA])->object))
14574 /* If all the glyphs of this row came from strings, put the
14575 cursor on the first glyph of the row. This avoids having the
14576 cursor outside of the text area in this very rare and hard
14577 use case. */
14578 glyph =
14579 row->reversed_p
14580 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14581 : row->glyphs[TEXT_AREA];
14583 if (x < 0)
14585 struct glyph *g;
14587 /* Need to compute x that corresponds to GLYPH. */
14588 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14590 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14591 emacs_abort ();
14592 x += g->pixel_width;
14596 /* ROW could be part of a continued line, which, under bidi
14597 reordering, might have other rows whose start and end charpos
14598 occlude point. Only set w->cursor if we found a better
14599 approximation to the cursor position than we have from previously
14600 examined candidate rows belonging to the same continued line. */
14601 if (/* We already have a candidate row. */
14602 w->cursor.vpos >= 0
14603 /* That candidate is not the row we are processing. */
14604 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14605 /* Make sure cursor.vpos specifies a row whose start and end
14606 charpos occlude point, and it is valid candidate for being a
14607 cursor-row. This is because some callers of this function
14608 leave cursor.vpos at the row where the cursor was displayed
14609 during the last redisplay cycle. */
14610 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14611 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14612 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14614 struct glyph *g1
14615 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14617 /* Don't consider glyphs that are outside TEXT_AREA. */
14618 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14619 return 0;
14620 /* Keep the candidate whose buffer position is the closest to
14621 point or has the `cursor' property. */
14622 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14623 w->cursor.hpos >= 0
14624 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14625 && ((BUFFERP (g1->object)
14626 && (g1->charpos == pt_old /* An exact match always wins. */
14627 || (BUFFERP (glyph->object)
14628 && eabs (g1->charpos - pt_old)
14629 < eabs (glyph->charpos - pt_old))))
14630 /* Previous candidate is a glyph from a string that has
14631 a non-nil `cursor' property. */
14632 || (STRINGP (g1->object)
14633 && (!NILP (Fget_char_property (make_number (g1->charpos),
14634 Qcursor, g1->object))
14635 /* Previous candidate is from the same display
14636 string as this one, and the display string
14637 came from a text property. */
14638 || (EQ (g1->object, glyph->object)
14639 && string_from_text_prop)
14640 /* this candidate is from newline and its
14641 position is not an exact match */
14642 || (INTEGERP (glyph->object)
14643 && glyph->charpos != pt_old)))))
14644 return 0;
14645 /* If this candidate gives an exact match, use that. */
14646 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14647 /* If this candidate is a glyph created for the
14648 terminating newline of a line, and point is on that
14649 newline, it wins because it's an exact match. */
14650 || (!row->continued_p
14651 && INTEGERP (glyph->object)
14652 && glyph->charpos == 0
14653 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14654 /* Otherwise, keep the candidate that comes from a row
14655 spanning less buffer positions. This may win when one or
14656 both candidate positions are on glyphs that came from
14657 display strings, for which we cannot compare buffer
14658 positions. */
14659 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14660 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14661 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14662 return 0;
14664 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14665 w->cursor.x = x;
14666 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14667 w->cursor.y = row->y + dy;
14669 if (w == XWINDOW (selected_window))
14671 if (!row->continued_p
14672 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14673 && row->x == 0)
14675 this_line_buffer = XBUFFER (w->contents);
14677 CHARPOS (this_line_start_pos)
14678 = MATRIX_ROW_START_CHARPOS (row) + delta;
14679 BYTEPOS (this_line_start_pos)
14680 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14682 CHARPOS (this_line_end_pos)
14683 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14684 BYTEPOS (this_line_end_pos)
14685 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14687 this_line_y = w->cursor.y;
14688 this_line_pixel_height = row->height;
14689 this_line_vpos = w->cursor.vpos;
14690 this_line_start_x = row->x;
14692 else
14693 CHARPOS (this_line_start_pos) = 0;
14696 return 1;
14700 /* Run window scroll functions, if any, for WINDOW with new window
14701 start STARTP. Sets the window start of WINDOW to that position.
14703 We assume that the window's buffer is really current. */
14705 static struct text_pos
14706 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14708 struct window *w = XWINDOW (window);
14709 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14711 eassert (current_buffer == XBUFFER (w->contents));
14713 if (!NILP (Vwindow_scroll_functions))
14715 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14716 make_number (CHARPOS (startp)));
14717 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14718 /* In case the hook functions switch buffers. */
14719 set_buffer_internal (XBUFFER (w->contents));
14722 return startp;
14726 /* Make sure the line containing the cursor is fully visible.
14727 A value of 1 means there is nothing to be done.
14728 (Either the line is fully visible, or it cannot be made so,
14729 or we cannot tell.)
14731 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14732 is higher than window.
14734 A value of 0 means the caller should do scrolling
14735 as if point had gone off the screen. */
14737 static int
14738 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14740 struct glyph_matrix *matrix;
14741 struct glyph_row *row;
14742 int window_height;
14744 if (!make_cursor_line_fully_visible_p)
14745 return 1;
14747 /* It's not always possible to find the cursor, e.g, when a window
14748 is full of overlay strings. Don't do anything in that case. */
14749 if (w->cursor.vpos < 0)
14750 return 1;
14752 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14753 row = MATRIX_ROW (matrix, w->cursor.vpos);
14755 /* If the cursor row is not partially visible, there's nothing to do. */
14756 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14757 return 1;
14759 /* If the row the cursor is in is taller than the window's height,
14760 it's not clear what to do, so do nothing. */
14761 window_height = window_box_height (w);
14762 if (row->height >= window_height)
14764 if (!force_p || MINI_WINDOW_P (w)
14765 || w->vscroll || w->cursor.vpos == 0)
14766 return 1;
14768 return 0;
14772 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14773 non-zero means only WINDOW is redisplayed in redisplay_internal.
14774 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14775 in redisplay_window to bring a partially visible line into view in
14776 the case that only the cursor has moved.
14778 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14779 last screen line's vertical height extends past the end of the screen.
14781 Value is
14783 1 if scrolling succeeded
14785 0 if scrolling didn't find point.
14787 -1 if new fonts have been loaded so that we must interrupt
14788 redisplay, adjust glyph matrices, and try again. */
14790 enum
14792 SCROLLING_SUCCESS,
14793 SCROLLING_FAILED,
14794 SCROLLING_NEED_LARGER_MATRICES
14797 /* If scroll-conservatively is more than this, never recenter.
14799 If you change this, don't forget to update the doc string of
14800 `scroll-conservatively' and the Emacs manual. */
14801 #define SCROLL_LIMIT 100
14803 static int
14804 try_scrolling (Lisp_Object window, int just_this_one_p,
14805 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14806 int temp_scroll_step, int last_line_misfit)
14808 struct window *w = XWINDOW (window);
14809 struct frame *f = XFRAME (w->frame);
14810 struct text_pos pos, startp;
14811 struct it it;
14812 int this_scroll_margin, scroll_max, rc, height;
14813 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14814 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14815 Lisp_Object aggressive;
14816 /* We will never try scrolling more than this number of lines. */
14817 int scroll_limit = SCROLL_LIMIT;
14818 int frame_line_height = default_line_pixel_height (w);
14819 int window_total_lines
14820 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14822 #ifdef GLYPH_DEBUG
14823 debug_method_add (w, "try_scrolling");
14824 #endif
14826 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14828 /* Compute scroll margin height in pixels. We scroll when point is
14829 within this distance from the top or bottom of the window. */
14830 if (scroll_margin > 0)
14831 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14832 * frame_line_height;
14833 else
14834 this_scroll_margin = 0;
14836 /* Force arg_scroll_conservatively to have a reasonable value, to
14837 avoid scrolling too far away with slow move_it_* functions. Note
14838 that the user can supply scroll-conservatively equal to
14839 `most-positive-fixnum', which can be larger than INT_MAX. */
14840 if (arg_scroll_conservatively > scroll_limit)
14842 arg_scroll_conservatively = scroll_limit + 1;
14843 scroll_max = scroll_limit * frame_line_height;
14845 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14846 /* Compute how much we should try to scroll maximally to bring
14847 point into view. */
14848 scroll_max = (max (scroll_step,
14849 max (arg_scroll_conservatively, temp_scroll_step))
14850 * frame_line_height);
14851 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14852 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14853 /* We're trying to scroll because of aggressive scrolling but no
14854 scroll_step is set. Choose an arbitrary one. */
14855 scroll_max = 10 * frame_line_height;
14856 else
14857 scroll_max = 0;
14859 too_near_end:
14861 /* Decide whether to scroll down. */
14862 if (PT > CHARPOS (startp))
14864 int scroll_margin_y;
14866 /* Compute the pixel ypos of the scroll margin, then move IT to
14867 either that ypos or PT, whichever comes first. */
14868 start_display (&it, w, startp);
14869 scroll_margin_y = it.last_visible_y - this_scroll_margin
14870 - frame_line_height * extra_scroll_margin_lines;
14871 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14872 (MOVE_TO_POS | MOVE_TO_Y));
14874 if (PT > CHARPOS (it.current.pos))
14876 int y0 = line_bottom_y (&it);
14877 /* Compute how many pixels below window bottom to stop searching
14878 for PT. This avoids costly search for PT that is far away if
14879 the user limited scrolling by a small number of lines, but
14880 always finds PT if scroll_conservatively is set to a large
14881 number, such as most-positive-fixnum. */
14882 int slack = max (scroll_max, 10 * frame_line_height);
14883 int y_to_move = it.last_visible_y + slack;
14885 /* Compute the distance from the scroll margin to PT or to
14886 the scroll limit, whichever comes first. This should
14887 include the height of the cursor line, to make that line
14888 fully visible. */
14889 move_it_to (&it, PT, -1, y_to_move,
14890 -1, MOVE_TO_POS | MOVE_TO_Y);
14891 dy = line_bottom_y (&it) - y0;
14893 if (dy > scroll_max)
14894 return SCROLLING_FAILED;
14896 if (dy > 0)
14897 scroll_down_p = 1;
14901 if (scroll_down_p)
14903 /* Point is in or below the bottom scroll margin, so move the
14904 window start down. If scrolling conservatively, move it just
14905 enough down to make point visible. If scroll_step is set,
14906 move it down by scroll_step. */
14907 if (arg_scroll_conservatively)
14908 amount_to_scroll
14909 = min (max (dy, frame_line_height),
14910 frame_line_height * arg_scroll_conservatively);
14911 else if (scroll_step || temp_scroll_step)
14912 amount_to_scroll = scroll_max;
14913 else
14915 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14916 height = WINDOW_BOX_TEXT_HEIGHT (w);
14917 if (NUMBERP (aggressive))
14919 double float_amount = XFLOATINT (aggressive) * height;
14920 int aggressive_scroll = float_amount;
14921 if (aggressive_scroll == 0 && float_amount > 0)
14922 aggressive_scroll = 1;
14923 /* Don't let point enter the scroll margin near top of
14924 the window. This could happen if the value of
14925 scroll_up_aggressively is too large and there are
14926 non-zero margins, because scroll_up_aggressively
14927 means put point that fraction of window height
14928 _from_the_bottom_margin_. */
14929 if (aggressive_scroll + 2*this_scroll_margin > height)
14930 aggressive_scroll = height - 2*this_scroll_margin;
14931 amount_to_scroll = dy + aggressive_scroll;
14935 if (amount_to_scroll <= 0)
14936 return SCROLLING_FAILED;
14938 start_display (&it, w, startp);
14939 if (arg_scroll_conservatively <= scroll_limit)
14940 move_it_vertically (&it, amount_to_scroll);
14941 else
14943 /* Extra precision for users who set scroll-conservatively
14944 to a large number: make sure the amount we scroll
14945 the window start is never less than amount_to_scroll,
14946 which was computed as distance from window bottom to
14947 point. This matters when lines at window top and lines
14948 below window bottom have different height. */
14949 struct it it1;
14950 void *it1data = NULL;
14951 /* We use a temporary it1 because line_bottom_y can modify
14952 its argument, if it moves one line down; see there. */
14953 int start_y;
14955 SAVE_IT (it1, it, it1data);
14956 start_y = line_bottom_y (&it1);
14957 do {
14958 RESTORE_IT (&it, &it, it1data);
14959 move_it_by_lines (&it, 1);
14960 SAVE_IT (it1, it, it1data);
14961 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14964 /* If STARTP is unchanged, move it down another screen line. */
14965 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14966 move_it_by_lines (&it, 1);
14967 startp = it.current.pos;
14969 else
14971 struct text_pos scroll_margin_pos = startp;
14972 int y_offset = 0;
14974 /* See if point is inside the scroll margin at the top of the
14975 window. */
14976 if (this_scroll_margin)
14978 int y_start;
14980 start_display (&it, w, startp);
14981 y_start = it.current_y;
14982 move_it_vertically (&it, this_scroll_margin);
14983 scroll_margin_pos = it.current.pos;
14984 /* If we didn't move enough before hitting ZV, request
14985 additional amount of scroll, to move point out of the
14986 scroll margin. */
14987 if (IT_CHARPOS (it) == ZV
14988 && it.current_y - y_start < this_scroll_margin)
14989 y_offset = this_scroll_margin - (it.current_y - y_start);
14992 if (PT < CHARPOS (scroll_margin_pos))
14994 /* Point is in the scroll margin at the top of the window or
14995 above what is displayed in the window. */
14996 int y0, y_to_move;
14998 /* Compute the vertical distance from PT to the scroll
14999 margin position. Move as far as scroll_max allows, or
15000 one screenful, or 10 screen lines, whichever is largest.
15001 Give up if distance is greater than scroll_max or if we
15002 didn't reach the scroll margin position. */
15003 SET_TEXT_POS (pos, PT, PT_BYTE);
15004 start_display (&it, w, pos);
15005 y0 = it.current_y;
15006 y_to_move = max (it.last_visible_y,
15007 max (scroll_max, 10 * frame_line_height));
15008 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15009 y_to_move, -1,
15010 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15011 dy = it.current_y - y0;
15012 if (dy > scroll_max
15013 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15014 return SCROLLING_FAILED;
15016 /* Additional scroll for when ZV was too close to point. */
15017 dy += y_offset;
15019 /* Compute new window start. */
15020 start_display (&it, w, startp);
15022 if (arg_scroll_conservatively)
15023 amount_to_scroll = max (dy, frame_line_height *
15024 max (scroll_step, temp_scroll_step));
15025 else if (scroll_step || temp_scroll_step)
15026 amount_to_scroll = scroll_max;
15027 else
15029 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15030 height = WINDOW_BOX_TEXT_HEIGHT (w);
15031 if (NUMBERP (aggressive))
15033 double float_amount = XFLOATINT (aggressive) * height;
15034 int aggressive_scroll = float_amount;
15035 if (aggressive_scroll == 0 && float_amount > 0)
15036 aggressive_scroll = 1;
15037 /* Don't let point enter the scroll margin near
15038 bottom of the window, if the value of
15039 scroll_down_aggressively happens to be too
15040 large. */
15041 if (aggressive_scroll + 2*this_scroll_margin > height)
15042 aggressive_scroll = height - 2*this_scroll_margin;
15043 amount_to_scroll = dy + aggressive_scroll;
15047 if (amount_to_scroll <= 0)
15048 return SCROLLING_FAILED;
15050 move_it_vertically_backward (&it, amount_to_scroll);
15051 startp = it.current.pos;
15055 /* Run window scroll functions. */
15056 startp = run_window_scroll_functions (window, startp);
15058 /* Display the window. Give up if new fonts are loaded, or if point
15059 doesn't appear. */
15060 if (!try_window (window, startp, 0))
15061 rc = SCROLLING_NEED_LARGER_MATRICES;
15062 else if (w->cursor.vpos < 0)
15064 clear_glyph_matrix (w->desired_matrix);
15065 rc = SCROLLING_FAILED;
15067 else
15069 /* Maybe forget recorded base line for line number display. */
15070 if (!just_this_one_p
15071 || current_buffer->clip_changed
15072 || BEG_UNCHANGED < CHARPOS (startp))
15073 w->base_line_number = 0;
15075 /* If cursor ends up on a partially visible line,
15076 treat that as being off the bottom of the screen. */
15077 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15078 /* It's possible that the cursor is on the first line of the
15079 buffer, which is partially obscured due to a vscroll
15080 (Bug#7537). In that case, avoid looping forever. */
15081 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15083 clear_glyph_matrix (w->desired_matrix);
15084 ++extra_scroll_margin_lines;
15085 goto too_near_end;
15087 rc = SCROLLING_SUCCESS;
15090 return rc;
15094 /* Compute a suitable window start for window W if display of W starts
15095 on a continuation line. Value is non-zero if a new window start
15096 was computed.
15098 The new window start will be computed, based on W's width, starting
15099 from the start of the continued line. It is the start of the
15100 screen line with the minimum distance from the old start W->start. */
15102 static int
15103 compute_window_start_on_continuation_line (struct window *w)
15105 struct text_pos pos, start_pos;
15106 int window_start_changed_p = 0;
15108 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15110 /* If window start is on a continuation line... Window start may be
15111 < BEGV in case there's invisible text at the start of the
15112 buffer (M-x rmail, for example). */
15113 if (CHARPOS (start_pos) > BEGV
15114 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15116 struct it it;
15117 struct glyph_row *row;
15119 /* Handle the case that the window start is out of range. */
15120 if (CHARPOS (start_pos) < BEGV)
15121 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15122 else if (CHARPOS (start_pos) > ZV)
15123 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15125 /* Find the start of the continued line. This should be fast
15126 because find_newline is fast (newline cache). */
15127 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15128 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15129 row, DEFAULT_FACE_ID);
15130 reseat_at_previous_visible_line_start (&it);
15132 /* If the line start is "too far" away from the window start,
15133 say it takes too much time to compute a new window start. */
15134 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15135 /* PXW: Do we need upper bounds here? */
15136 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15138 int min_distance, distance;
15140 /* Move forward by display lines to find the new window
15141 start. If window width was enlarged, the new start can
15142 be expected to be > the old start. If window width was
15143 decreased, the new window start will be < the old start.
15144 So, we're looking for the display line start with the
15145 minimum distance from the old window start. */
15146 pos = it.current.pos;
15147 min_distance = INFINITY;
15148 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15149 distance < min_distance)
15151 min_distance = distance;
15152 pos = it.current.pos;
15153 if (it.line_wrap == WORD_WRAP)
15155 /* Under WORD_WRAP, move_it_by_lines is likely to
15156 overshoot and stop not at the first, but the
15157 second character from the left margin. So in
15158 that case, we need a more tight control on the X
15159 coordinate of the iterator than move_it_by_lines
15160 promises in its contract. The method is to first
15161 go to the last (rightmost) visible character of a
15162 line, then move to the leftmost character on the
15163 next line in a separate call. */
15164 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15165 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15166 move_it_to (&it, ZV, 0,
15167 it.current_y + it.max_ascent + it.max_descent, -1,
15168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15170 else
15171 move_it_by_lines (&it, 1);
15174 /* Set the window start there. */
15175 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15176 window_start_changed_p = 1;
15180 return window_start_changed_p;
15184 /* Try cursor movement in case text has not changed in window WINDOW,
15185 with window start STARTP. Value is
15187 CURSOR_MOVEMENT_SUCCESS if successful
15189 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15191 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15192 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15193 we want to scroll as if scroll-step were set to 1. See the code.
15195 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15196 which case we have to abort this redisplay, and adjust matrices
15197 first. */
15199 enum
15201 CURSOR_MOVEMENT_SUCCESS,
15202 CURSOR_MOVEMENT_CANNOT_BE_USED,
15203 CURSOR_MOVEMENT_MUST_SCROLL,
15204 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15207 static int
15208 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15210 struct window *w = XWINDOW (window);
15211 struct frame *f = XFRAME (w->frame);
15212 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15214 #ifdef GLYPH_DEBUG
15215 if (inhibit_try_cursor_movement)
15216 return rc;
15217 #endif
15219 /* Previously, there was a check for Lisp integer in the
15220 if-statement below. Now, this field is converted to
15221 ptrdiff_t, thus zero means invalid position in a buffer. */
15222 eassert (w->last_point > 0);
15223 /* Likewise there was a check whether window_end_vpos is nil or larger
15224 than the window. Now window_end_vpos is int and so never nil, but
15225 let's leave eassert to check whether it fits in the window. */
15226 eassert (w->window_end_vpos < w->current_matrix->nrows);
15228 /* Handle case where text has not changed, only point, and it has
15229 not moved off the frame. */
15230 if (/* Point may be in this window. */
15231 PT >= CHARPOS (startp)
15232 /* Selective display hasn't changed. */
15233 && !current_buffer->clip_changed
15234 /* Function force-mode-line-update is used to force a thorough
15235 redisplay. It sets either windows_or_buffers_changed or
15236 update_mode_lines. So don't take a shortcut here for these
15237 cases. */
15238 && !update_mode_lines
15239 && !windows_or_buffers_changed
15240 && !f->cursor_type_changed
15241 && NILP (Vshow_trailing_whitespace)
15242 /* This code is not used for mini-buffer for the sake of the case
15243 of redisplaying to replace an echo area message; since in
15244 that case the mini-buffer contents per se are usually
15245 unchanged. This code is of no real use in the mini-buffer
15246 since the handling of this_line_start_pos, etc., in redisplay
15247 handles the same cases. */
15248 && !EQ (window, minibuf_window)
15249 && (FRAME_WINDOW_P (f)
15250 || !overlay_arrow_in_current_buffer_p ()))
15252 int this_scroll_margin, top_scroll_margin;
15253 struct glyph_row *row = NULL;
15254 int frame_line_height = default_line_pixel_height (w);
15255 int window_total_lines
15256 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15258 #ifdef GLYPH_DEBUG
15259 debug_method_add (w, "cursor movement");
15260 #endif
15262 /* Scroll if point within this distance from the top or bottom
15263 of the window. This is a pixel value. */
15264 if (scroll_margin > 0)
15266 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15267 this_scroll_margin *= frame_line_height;
15269 else
15270 this_scroll_margin = 0;
15272 top_scroll_margin = this_scroll_margin;
15273 if (WINDOW_WANTS_HEADER_LINE_P (w))
15274 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15276 /* Start with the row the cursor was displayed during the last
15277 not paused redisplay. Give up if that row is not valid. */
15278 if (w->last_cursor_vpos < 0
15279 || w->last_cursor_vpos >= w->current_matrix->nrows)
15280 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15281 else
15283 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15284 if (row->mode_line_p)
15285 ++row;
15286 if (!row->enabled_p)
15287 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15290 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15292 int scroll_p = 0, must_scroll = 0;
15293 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15295 if (PT > w->last_point)
15297 /* Point has moved forward. */
15298 while (MATRIX_ROW_END_CHARPOS (row) < PT
15299 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15301 eassert (row->enabled_p);
15302 ++row;
15305 /* If the end position of a row equals the start
15306 position of the next row, and PT is at that position,
15307 we would rather display cursor in the next line. */
15308 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15309 && MATRIX_ROW_END_CHARPOS (row) == PT
15310 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15311 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15312 && !cursor_row_p (row))
15313 ++row;
15315 /* If within the scroll margin, scroll. Note that
15316 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15317 the next line would be drawn, and that
15318 this_scroll_margin can be zero. */
15319 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15320 || PT > MATRIX_ROW_END_CHARPOS (row)
15321 /* Line is completely visible last line in window
15322 and PT is to be set in the next line. */
15323 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15324 && PT == MATRIX_ROW_END_CHARPOS (row)
15325 && !row->ends_at_zv_p
15326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15327 scroll_p = 1;
15329 else if (PT < w->last_point)
15331 /* Cursor has to be moved backward. Note that PT >=
15332 CHARPOS (startp) because of the outer if-statement. */
15333 while (!row->mode_line_p
15334 && (MATRIX_ROW_START_CHARPOS (row) > PT
15335 || (MATRIX_ROW_START_CHARPOS (row) == PT
15336 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15337 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15338 row > w->current_matrix->rows
15339 && (row-1)->ends_in_newline_from_string_p))))
15340 && (row->y > top_scroll_margin
15341 || CHARPOS (startp) == BEGV))
15343 eassert (row->enabled_p);
15344 --row;
15347 /* Consider the following case: Window starts at BEGV,
15348 there is invisible, intangible text at BEGV, so that
15349 display starts at some point START > BEGV. It can
15350 happen that we are called with PT somewhere between
15351 BEGV and START. Try to handle that case. */
15352 if (row < w->current_matrix->rows
15353 || row->mode_line_p)
15355 row = w->current_matrix->rows;
15356 if (row->mode_line_p)
15357 ++row;
15360 /* Due to newlines in overlay strings, we may have to
15361 skip forward over overlay strings. */
15362 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15363 && MATRIX_ROW_END_CHARPOS (row) == PT
15364 && !cursor_row_p (row))
15365 ++row;
15367 /* If within the scroll margin, scroll. */
15368 if (row->y < top_scroll_margin
15369 && CHARPOS (startp) != BEGV)
15370 scroll_p = 1;
15372 else
15374 /* Cursor did not move. So don't scroll even if cursor line
15375 is partially visible, as it was so before. */
15376 rc = CURSOR_MOVEMENT_SUCCESS;
15379 if (PT < MATRIX_ROW_START_CHARPOS (row)
15380 || PT > MATRIX_ROW_END_CHARPOS (row))
15382 /* if PT is not in the glyph row, give up. */
15383 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15384 must_scroll = 1;
15386 else if (rc != CURSOR_MOVEMENT_SUCCESS
15387 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15389 struct glyph_row *row1;
15391 /* If rows are bidi-reordered and point moved, back up
15392 until we find a row that does not belong to a
15393 continuation line. This is because we must consider
15394 all rows of a continued line as candidates for the
15395 new cursor positioning, since row start and end
15396 positions change non-linearly with vertical position
15397 in such rows. */
15398 /* FIXME: Revisit this when glyph ``spilling'' in
15399 continuation lines' rows is implemented for
15400 bidi-reordered rows. */
15401 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15402 MATRIX_ROW_CONTINUATION_LINE_P (row);
15403 --row)
15405 /* If we hit the beginning of the displayed portion
15406 without finding the first row of a continued
15407 line, give up. */
15408 if (row <= row1)
15410 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15411 break;
15413 eassert (row->enabled_p);
15416 if (must_scroll)
15418 else if (rc != CURSOR_MOVEMENT_SUCCESS
15419 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15420 /* Make sure this isn't a header line by any chance, since
15421 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15422 && !row->mode_line_p
15423 && make_cursor_line_fully_visible_p)
15425 if (PT == MATRIX_ROW_END_CHARPOS (row)
15426 && !row->ends_at_zv_p
15427 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15428 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15429 else if (row->height > window_box_height (w))
15431 /* If we end up in a partially visible line, let's
15432 make it fully visible, except when it's taller
15433 than the window, in which case we can't do much
15434 about it. */
15435 *scroll_step = 1;
15436 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15438 else
15440 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15441 if (!cursor_row_fully_visible_p (w, 0, 1))
15442 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15443 else
15444 rc = CURSOR_MOVEMENT_SUCCESS;
15447 else if (scroll_p)
15448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15449 else if (rc != CURSOR_MOVEMENT_SUCCESS
15450 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15452 /* With bidi-reordered rows, there could be more than
15453 one candidate row whose start and end positions
15454 occlude point. We need to let set_cursor_from_row
15455 find the best candidate. */
15456 /* FIXME: Revisit this when glyph ``spilling'' in
15457 continuation lines' rows is implemented for
15458 bidi-reordered rows. */
15459 int rv = 0;
15463 int at_zv_p = 0, exact_match_p = 0;
15465 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15466 && PT <= MATRIX_ROW_END_CHARPOS (row)
15467 && cursor_row_p (row))
15468 rv |= set_cursor_from_row (w, row, w->current_matrix,
15469 0, 0, 0, 0);
15470 /* As soon as we've found the exact match for point,
15471 or the first suitable row whose ends_at_zv_p flag
15472 is set, we are done. */
15473 at_zv_p =
15474 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15475 if (rv && !at_zv_p
15476 && w->cursor.hpos >= 0
15477 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15478 w->cursor.vpos))
15480 struct glyph_row *candidate =
15481 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15482 struct glyph *g =
15483 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15484 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15486 exact_match_p =
15487 (BUFFERP (g->object) && g->charpos == PT)
15488 || (INTEGERP (g->object)
15489 && (g->charpos == PT
15490 || (g->charpos == 0 && endpos - 1 == PT)));
15492 if (rv && (at_zv_p || exact_match_p))
15494 rc = CURSOR_MOVEMENT_SUCCESS;
15495 break;
15497 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15498 break;
15499 ++row;
15501 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15502 || row->continued_p)
15503 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15504 || (MATRIX_ROW_START_CHARPOS (row) == PT
15505 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15506 /* If we didn't find any candidate rows, or exited the
15507 loop before all the candidates were examined, signal
15508 to the caller that this method failed. */
15509 if (rc != CURSOR_MOVEMENT_SUCCESS
15510 && !(rv
15511 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15512 && !row->continued_p))
15513 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15514 else if (rv)
15515 rc = CURSOR_MOVEMENT_SUCCESS;
15517 else
15521 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15523 rc = CURSOR_MOVEMENT_SUCCESS;
15524 break;
15526 ++row;
15528 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15529 && MATRIX_ROW_START_CHARPOS (row) == PT
15530 && cursor_row_p (row));
15535 return rc;
15538 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15539 static
15540 #endif
15541 void
15542 set_vertical_scroll_bar (struct window *w)
15544 ptrdiff_t start, end, whole;
15546 /* Calculate the start and end positions for the current window.
15547 At some point, it would be nice to choose between scrollbars
15548 which reflect the whole buffer size, with special markers
15549 indicating narrowing, and scrollbars which reflect only the
15550 visible region.
15552 Note that mini-buffers sometimes aren't displaying any text. */
15553 if (!MINI_WINDOW_P (w)
15554 || (w == XWINDOW (minibuf_window)
15555 && NILP (echo_area_buffer[0])))
15557 struct buffer *buf = XBUFFER (w->contents);
15558 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15559 start = marker_position (w->start) - BUF_BEGV (buf);
15560 /* I don't think this is guaranteed to be right. For the
15561 moment, we'll pretend it is. */
15562 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15564 if (end < start)
15565 end = start;
15566 if (whole < (end - start))
15567 whole = end - start;
15569 else
15570 start = end = whole = 0;
15572 /* Indicate what this scroll bar ought to be displaying now. */
15573 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15574 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15575 (w, end - start, whole, start);
15579 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15580 selected_window is redisplayed.
15582 We can return without actually redisplaying the window if fonts has been
15583 changed on window's frame. In that case, redisplay_internal will retry. */
15585 static void
15586 redisplay_window (Lisp_Object window, bool just_this_one_p)
15588 struct window *w = XWINDOW (window);
15589 struct frame *f = XFRAME (w->frame);
15590 struct buffer *buffer = XBUFFER (w->contents);
15591 struct buffer *old = current_buffer;
15592 struct text_pos lpoint, opoint, startp;
15593 int update_mode_line;
15594 int tem;
15595 struct it it;
15596 /* Record it now because it's overwritten. */
15597 bool current_matrix_up_to_date_p = false;
15598 bool used_current_matrix_p = false;
15599 /* This is less strict than current_matrix_up_to_date_p.
15600 It indicates that the buffer contents and narrowing are unchanged. */
15601 bool buffer_unchanged_p = false;
15602 int temp_scroll_step = 0;
15603 ptrdiff_t count = SPECPDL_INDEX ();
15604 int rc;
15605 int centering_position = -1;
15606 int last_line_misfit = 0;
15607 ptrdiff_t beg_unchanged, end_unchanged;
15608 int frame_line_height;
15610 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15611 opoint = lpoint;
15613 #ifdef GLYPH_DEBUG
15614 *w->desired_matrix->method = 0;
15615 #endif
15617 if (!just_this_one_p
15618 && REDISPLAY_SOME_P ()
15619 && !w->redisplay
15620 && !f->redisplay
15621 && !buffer->text->redisplay
15622 && BUF_PT (buffer) == w->last_point)
15623 return;
15625 /* Make sure that both W's markers are valid. */
15626 eassert (XMARKER (w->start)->buffer == buffer);
15627 eassert (XMARKER (w->pointm)->buffer == buffer);
15629 restart:
15630 reconsider_clip_changes (w);
15631 frame_line_height = default_line_pixel_height (w);
15633 /* Has the mode line to be updated? */
15634 update_mode_line = (w->update_mode_line
15635 || update_mode_lines
15636 || buffer->clip_changed
15637 || buffer->prevent_redisplay_optimizations_p);
15639 if (!just_this_one_p)
15640 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15641 cleverly elsewhere. */
15642 w->must_be_updated_p = true;
15644 if (MINI_WINDOW_P (w))
15646 if (w == XWINDOW (echo_area_window)
15647 && !NILP (echo_area_buffer[0]))
15649 if (update_mode_line)
15650 /* We may have to update a tty frame's menu bar or a
15651 tool-bar. Example `M-x C-h C-h C-g'. */
15652 goto finish_menu_bars;
15653 else
15654 /* We've already displayed the echo area glyphs in this window. */
15655 goto finish_scroll_bars;
15657 else if ((w != XWINDOW (minibuf_window)
15658 || minibuf_level == 0)
15659 /* When buffer is nonempty, redisplay window normally. */
15660 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15661 /* Quail displays non-mini buffers in minibuffer window.
15662 In that case, redisplay the window normally. */
15663 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15665 /* W is a mini-buffer window, but it's not active, so clear
15666 it. */
15667 int yb = window_text_bottom_y (w);
15668 struct glyph_row *row;
15669 int y;
15671 for (y = 0, row = w->desired_matrix->rows;
15672 y < yb;
15673 y += row->height, ++row)
15674 blank_row (w, row, y);
15675 goto finish_scroll_bars;
15678 clear_glyph_matrix (w->desired_matrix);
15681 /* Otherwise set up data on this window; select its buffer and point
15682 value. */
15683 /* Really select the buffer, for the sake of buffer-local
15684 variables. */
15685 set_buffer_internal_1 (XBUFFER (w->contents));
15687 current_matrix_up_to_date_p
15688 = (w->window_end_valid
15689 && !current_buffer->clip_changed
15690 && !current_buffer->prevent_redisplay_optimizations_p
15691 && !window_outdated (w));
15693 /* Run the window-bottom-change-functions
15694 if it is possible that the text on the screen has changed
15695 (either due to modification of the text, or any other reason). */
15696 if (!current_matrix_up_to_date_p
15697 && !NILP (Vwindow_text_change_functions))
15699 safe_run_hooks (Qwindow_text_change_functions);
15700 goto restart;
15703 beg_unchanged = BEG_UNCHANGED;
15704 end_unchanged = END_UNCHANGED;
15706 SET_TEXT_POS (opoint, PT, PT_BYTE);
15708 specbind (Qinhibit_point_motion_hooks, Qt);
15710 buffer_unchanged_p
15711 = (w->window_end_valid
15712 && !current_buffer->clip_changed
15713 && !window_outdated (w));
15715 /* When windows_or_buffers_changed is non-zero, we can't rely
15716 on the window end being valid, so set it to zero there. */
15717 if (windows_or_buffers_changed)
15719 /* If window starts on a continuation line, maybe adjust the
15720 window start in case the window's width changed. */
15721 if (XMARKER (w->start)->buffer == current_buffer)
15722 compute_window_start_on_continuation_line (w);
15724 w->window_end_valid = false;
15725 /* If so, we also can't rely on current matrix
15726 and should not fool try_cursor_movement below. */
15727 current_matrix_up_to_date_p = false;
15730 /* Some sanity checks. */
15731 CHECK_WINDOW_END (w);
15732 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15733 emacs_abort ();
15734 if (BYTEPOS (opoint) < CHARPOS (opoint))
15735 emacs_abort ();
15737 if (mode_line_update_needed (w))
15738 update_mode_line = 1;
15740 /* Point refers normally to the selected window. For any other
15741 window, set up appropriate value. */
15742 if (!EQ (window, selected_window))
15744 ptrdiff_t new_pt = marker_position (w->pointm);
15745 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15746 if (new_pt < BEGV)
15748 new_pt = BEGV;
15749 new_pt_byte = BEGV_BYTE;
15750 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15752 else if (new_pt > (ZV - 1))
15754 new_pt = ZV;
15755 new_pt_byte = ZV_BYTE;
15756 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15759 /* We don't use SET_PT so that the point-motion hooks don't run. */
15760 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15763 /* If any of the character widths specified in the display table
15764 have changed, invalidate the width run cache. It's true that
15765 this may be a bit late to catch such changes, but the rest of
15766 redisplay goes (non-fatally) haywire when the display table is
15767 changed, so why should we worry about doing any better? */
15768 if (current_buffer->width_run_cache
15769 || (current_buffer->base_buffer
15770 && current_buffer->base_buffer->width_run_cache))
15772 struct Lisp_Char_Table *disptab = buffer_display_table ();
15774 if (! disptab_matches_widthtab
15775 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15777 struct buffer *buf = current_buffer;
15779 if (buf->base_buffer)
15780 buf = buf->base_buffer;
15781 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15782 recompute_width_table (current_buffer, disptab);
15786 /* If window-start is screwed up, choose a new one. */
15787 if (XMARKER (w->start)->buffer != current_buffer)
15788 goto recenter;
15790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15792 /* If someone specified a new starting point but did not insist,
15793 check whether it can be used. */
15794 if (w->optional_new_start
15795 && CHARPOS (startp) >= BEGV
15796 && CHARPOS (startp) <= ZV)
15798 w->optional_new_start = 0;
15799 start_display (&it, w, startp);
15800 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15801 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15802 if (IT_CHARPOS (it) == PT)
15803 w->force_start = 1;
15804 /* IT may overshoot PT if text at PT is invisible. */
15805 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15806 w->force_start = 1;
15809 force_start:
15811 /* Handle case where place to start displaying has been specified,
15812 unless the specified location is outside the accessible range. */
15813 if (w->force_start || window_frozen_p (w))
15815 /* We set this later on if we have to adjust point. */
15816 int new_vpos = -1;
15818 w->force_start = 0;
15819 w->vscroll = 0;
15820 w->window_end_valid = 0;
15822 /* Forget any recorded base line for line number display. */
15823 if (!buffer_unchanged_p)
15824 w->base_line_number = 0;
15826 /* Redisplay the mode line. Select the buffer properly for that.
15827 Also, run the hook window-scroll-functions
15828 because we have scrolled. */
15829 /* Note, we do this after clearing force_start because
15830 if there's an error, it is better to forget about force_start
15831 than to get into an infinite loop calling the hook functions
15832 and having them get more errors. */
15833 if (!update_mode_line
15834 || ! NILP (Vwindow_scroll_functions))
15836 update_mode_line = 1;
15837 w->update_mode_line = 1;
15838 startp = run_window_scroll_functions (window, startp);
15841 if (CHARPOS (startp) < BEGV)
15842 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15843 else if (CHARPOS (startp) > ZV)
15844 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15846 /* Redisplay, then check if cursor has been set during the
15847 redisplay. Give up if new fonts were loaded. */
15848 /* We used to issue a CHECK_MARGINS argument to try_window here,
15849 but this causes scrolling to fail when point begins inside
15850 the scroll margin (bug#148) -- cyd */
15851 if (!try_window (window, startp, 0))
15853 w->force_start = 1;
15854 clear_glyph_matrix (w->desired_matrix);
15855 goto need_larger_matrices;
15858 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15860 /* If point does not appear, try to move point so it does
15861 appear. The desired matrix has been built above, so we
15862 can use it here. */
15863 new_vpos = window_box_height (w) / 2;
15866 if (!cursor_row_fully_visible_p (w, 0, 0))
15868 /* Point does appear, but on a line partly visible at end of window.
15869 Move it back to a fully-visible line. */
15870 new_vpos = window_box_height (w);
15872 else if (w->cursor.vpos >= 0)
15874 /* Some people insist on not letting point enter the scroll
15875 margin, even though this part handles windows that didn't
15876 scroll at all. */
15877 int window_total_lines
15878 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15879 int margin = min (scroll_margin, window_total_lines / 4);
15880 int pixel_margin = margin * frame_line_height;
15881 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15883 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15884 below, which finds the row to move point to, advances by
15885 the Y coordinate of the _next_ row, see the definition of
15886 MATRIX_ROW_BOTTOM_Y. */
15887 if (w->cursor.vpos < margin + header_line)
15889 w->cursor.vpos = -1;
15890 clear_glyph_matrix (w->desired_matrix);
15891 goto try_to_scroll;
15893 else
15895 int window_height = window_box_height (w);
15897 if (header_line)
15898 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15899 if (w->cursor.y >= window_height - pixel_margin)
15901 w->cursor.vpos = -1;
15902 clear_glyph_matrix (w->desired_matrix);
15903 goto try_to_scroll;
15908 /* If we need to move point for either of the above reasons,
15909 now actually do it. */
15910 if (new_vpos >= 0)
15912 struct glyph_row *row;
15914 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15915 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15916 ++row;
15918 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15919 MATRIX_ROW_START_BYTEPOS (row));
15921 if (w != XWINDOW (selected_window))
15922 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15923 else if (current_buffer == old)
15924 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15926 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15928 /* If we are highlighting the region, then we just changed
15929 the region, so redisplay to show it. */
15930 /* FIXME: We need to (re)run pre-redisplay-function! */
15931 /* if (markpos_of_region () >= 0)
15933 clear_glyph_matrix (w->desired_matrix);
15934 if (!try_window (window, startp, 0))
15935 goto need_larger_matrices;
15940 #ifdef GLYPH_DEBUG
15941 debug_method_add (w, "forced window start");
15942 #endif
15943 goto done;
15946 /* Handle case where text has not changed, only point, and it has
15947 not moved off the frame, and we are not retrying after hscroll.
15948 (current_matrix_up_to_date_p is nonzero when retrying.) */
15949 if (current_matrix_up_to_date_p
15950 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15951 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15953 switch (rc)
15955 case CURSOR_MOVEMENT_SUCCESS:
15956 used_current_matrix_p = 1;
15957 goto done;
15959 case CURSOR_MOVEMENT_MUST_SCROLL:
15960 goto try_to_scroll;
15962 default:
15963 emacs_abort ();
15966 /* If current starting point was originally the beginning of a line
15967 but no longer is, find a new starting point. */
15968 else if (w->start_at_line_beg
15969 && !(CHARPOS (startp) <= BEGV
15970 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15972 #ifdef GLYPH_DEBUG
15973 debug_method_add (w, "recenter 1");
15974 #endif
15975 goto recenter;
15978 /* Try scrolling with try_window_id. Value is > 0 if update has
15979 been done, it is -1 if we know that the same window start will
15980 not work. It is 0 if unsuccessful for some other reason. */
15981 else if ((tem = try_window_id (w)) != 0)
15983 #ifdef GLYPH_DEBUG
15984 debug_method_add (w, "try_window_id %d", tem);
15985 #endif
15987 if (f->fonts_changed)
15988 goto need_larger_matrices;
15989 if (tem > 0)
15990 goto done;
15992 /* Otherwise try_window_id has returned -1 which means that we
15993 don't want the alternative below this comment to execute. */
15995 else if (CHARPOS (startp) >= BEGV
15996 && CHARPOS (startp) <= ZV
15997 && PT >= CHARPOS (startp)
15998 && (CHARPOS (startp) < ZV
15999 /* Avoid starting at end of buffer. */
16000 || CHARPOS (startp) == BEGV
16001 || !window_outdated (w)))
16003 int d1, d2, d3, d4, d5, d6;
16005 /* If first window line is a continuation line, and window start
16006 is inside the modified region, but the first change is before
16007 current window start, we must select a new window start.
16009 However, if this is the result of a down-mouse event (e.g. by
16010 extending the mouse-drag-overlay), we don't want to select a
16011 new window start, since that would change the position under
16012 the mouse, resulting in an unwanted mouse-movement rather
16013 than a simple mouse-click. */
16014 if (!w->start_at_line_beg
16015 && NILP (do_mouse_tracking)
16016 && CHARPOS (startp) > BEGV
16017 && CHARPOS (startp) > BEG + beg_unchanged
16018 && CHARPOS (startp) <= Z - end_unchanged
16019 /* Even if w->start_at_line_beg is nil, a new window may
16020 start at a line_beg, since that's how set_buffer_window
16021 sets it. So, we need to check the return value of
16022 compute_window_start_on_continuation_line. (See also
16023 bug#197). */
16024 && XMARKER (w->start)->buffer == current_buffer
16025 && compute_window_start_on_continuation_line (w)
16026 /* It doesn't make sense to force the window start like we
16027 do at label force_start if it is already known that point
16028 will not be visible in the resulting window, because
16029 doing so will move point from its correct position
16030 instead of scrolling the window to bring point into view.
16031 See bug#9324. */
16032 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16034 w->force_start = 1;
16035 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16036 goto force_start;
16039 #ifdef GLYPH_DEBUG
16040 debug_method_add (w, "same window start");
16041 #endif
16043 /* Try to redisplay starting at same place as before.
16044 If point has not moved off frame, accept the results. */
16045 if (!current_matrix_up_to_date_p
16046 /* Don't use try_window_reusing_current_matrix in this case
16047 because a window scroll function can have changed the
16048 buffer. */
16049 || !NILP (Vwindow_scroll_functions)
16050 || MINI_WINDOW_P (w)
16051 || !(used_current_matrix_p
16052 = try_window_reusing_current_matrix (w)))
16054 IF_DEBUG (debug_method_add (w, "1"));
16055 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16056 /* -1 means we need to scroll.
16057 0 means we need new matrices, but fonts_changed
16058 is set in that case, so we will detect it below. */
16059 goto try_to_scroll;
16062 if (f->fonts_changed)
16063 goto need_larger_matrices;
16065 if (w->cursor.vpos >= 0)
16067 if (!just_this_one_p
16068 || current_buffer->clip_changed
16069 || BEG_UNCHANGED < CHARPOS (startp))
16070 /* Forget any recorded base line for line number display. */
16071 w->base_line_number = 0;
16073 if (!cursor_row_fully_visible_p (w, 1, 0))
16075 clear_glyph_matrix (w->desired_matrix);
16076 last_line_misfit = 1;
16078 /* Drop through and scroll. */
16079 else
16080 goto done;
16082 else
16083 clear_glyph_matrix (w->desired_matrix);
16086 try_to_scroll:
16088 /* Redisplay the mode line. Select the buffer properly for that. */
16089 if (!update_mode_line)
16091 update_mode_line = 1;
16092 w->update_mode_line = 1;
16095 /* Try to scroll by specified few lines. */
16096 if ((scroll_conservatively
16097 || emacs_scroll_step
16098 || temp_scroll_step
16099 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16100 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16101 && CHARPOS (startp) >= BEGV
16102 && CHARPOS (startp) <= ZV)
16104 /* The function returns -1 if new fonts were loaded, 1 if
16105 successful, 0 if not successful. */
16106 int ss = try_scrolling (window, just_this_one_p,
16107 scroll_conservatively,
16108 emacs_scroll_step,
16109 temp_scroll_step, last_line_misfit);
16110 switch (ss)
16112 case SCROLLING_SUCCESS:
16113 goto done;
16115 case SCROLLING_NEED_LARGER_MATRICES:
16116 goto need_larger_matrices;
16118 case SCROLLING_FAILED:
16119 break;
16121 default:
16122 emacs_abort ();
16126 /* Finally, just choose a place to start which positions point
16127 according to user preferences. */
16129 recenter:
16131 #ifdef GLYPH_DEBUG
16132 debug_method_add (w, "recenter");
16133 #endif
16135 /* Forget any previously recorded base line for line number display. */
16136 if (!buffer_unchanged_p)
16137 w->base_line_number = 0;
16139 /* Determine the window start relative to point. */
16140 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16141 it.current_y = it.last_visible_y;
16142 if (centering_position < 0)
16144 int window_total_lines
16145 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16146 int margin =
16147 scroll_margin > 0
16148 ? min (scroll_margin, window_total_lines / 4)
16149 : 0;
16150 ptrdiff_t margin_pos = CHARPOS (startp);
16151 Lisp_Object aggressive;
16152 int scrolling_up;
16154 /* If there is a scroll margin at the top of the window, find
16155 its character position. */
16156 if (margin
16157 /* Cannot call start_display if startp is not in the
16158 accessible region of the buffer. This can happen when we
16159 have just switched to a different buffer and/or changed
16160 its restriction. In that case, startp is initialized to
16161 the character position 1 (BEGV) because we did not yet
16162 have chance to display the buffer even once. */
16163 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16165 struct it it1;
16166 void *it1data = NULL;
16168 SAVE_IT (it1, it, it1data);
16169 start_display (&it1, w, startp);
16170 move_it_vertically (&it1, margin * frame_line_height);
16171 margin_pos = IT_CHARPOS (it1);
16172 RESTORE_IT (&it, &it, it1data);
16174 scrolling_up = PT > margin_pos;
16175 aggressive =
16176 scrolling_up
16177 ? BVAR (current_buffer, scroll_up_aggressively)
16178 : BVAR (current_buffer, scroll_down_aggressively);
16180 if (!MINI_WINDOW_P (w)
16181 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16183 int pt_offset = 0;
16185 /* Setting scroll-conservatively overrides
16186 scroll-*-aggressively. */
16187 if (!scroll_conservatively && NUMBERP (aggressive))
16189 double float_amount = XFLOATINT (aggressive);
16191 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16192 if (pt_offset == 0 && float_amount > 0)
16193 pt_offset = 1;
16194 if (pt_offset && margin > 0)
16195 margin -= 1;
16197 /* Compute how much to move the window start backward from
16198 point so that point will be displayed where the user
16199 wants it. */
16200 if (scrolling_up)
16202 centering_position = it.last_visible_y;
16203 if (pt_offset)
16204 centering_position -= pt_offset;
16205 centering_position -=
16206 frame_line_height * (1 + margin + (last_line_misfit != 0))
16207 + WINDOW_HEADER_LINE_HEIGHT (w);
16208 /* Don't let point enter the scroll margin near top of
16209 the window. */
16210 if (centering_position < margin * frame_line_height)
16211 centering_position = margin * frame_line_height;
16213 else
16214 centering_position = margin * frame_line_height + pt_offset;
16216 else
16217 /* Set the window start half the height of the window backward
16218 from point. */
16219 centering_position = window_box_height (w) / 2;
16221 move_it_vertically_backward (&it, centering_position);
16223 eassert (IT_CHARPOS (it) >= BEGV);
16225 /* The function move_it_vertically_backward may move over more
16226 than the specified y-distance. If it->w is small, e.g. a
16227 mini-buffer window, we may end up in front of the window's
16228 display area. Start displaying at the start of the line
16229 containing PT in this case. */
16230 if (it.current_y <= 0)
16232 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16233 move_it_vertically_backward (&it, 0);
16234 it.current_y = 0;
16237 it.current_x = it.hpos = 0;
16239 /* Set the window start position here explicitly, to avoid an
16240 infinite loop in case the functions in window-scroll-functions
16241 get errors. */
16242 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16244 /* Run scroll hooks. */
16245 startp = run_window_scroll_functions (window, it.current.pos);
16247 /* Redisplay the window. */
16248 if (!current_matrix_up_to_date_p
16249 || windows_or_buffers_changed
16250 || f->cursor_type_changed
16251 /* Don't use try_window_reusing_current_matrix in this case
16252 because it can have changed the buffer. */
16253 || !NILP (Vwindow_scroll_functions)
16254 || !just_this_one_p
16255 || MINI_WINDOW_P (w)
16256 || !(used_current_matrix_p
16257 = try_window_reusing_current_matrix (w)))
16258 try_window (window, startp, 0);
16260 /* If new fonts have been loaded (due to fontsets), give up. We
16261 have to start a new redisplay since we need to re-adjust glyph
16262 matrices. */
16263 if (f->fonts_changed)
16264 goto need_larger_matrices;
16266 /* If cursor did not appear assume that the middle of the window is
16267 in the first line of the window. Do it again with the next line.
16268 (Imagine a window of height 100, displaying two lines of height
16269 60. Moving back 50 from it->last_visible_y will end in the first
16270 line.) */
16271 if (w->cursor.vpos < 0)
16273 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16275 clear_glyph_matrix (w->desired_matrix);
16276 move_it_by_lines (&it, 1);
16277 try_window (window, it.current.pos, 0);
16279 else if (PT < IT_CHARPOS (it))
16281 clear_glyph_matrix (w->desired_matrix);
16282 move_it_by_lines (&it, -1);
16283 try_window (window, it.current.pos, 0);
16285 else
16287 /* Not much we can do about it. */
16291 /* Consider the following case: Window starts at BEGV, there is
16292 invisible, intangible text at BEGV, so that display starts at
16293 some point START > BEGV. It can happen that we are called with
16294 PT somewhere between BEGV and START. Try to handle that case. */
16295 if (w->cursor.vpos < 0)
16297 struct glyph_row *row = w->current_matrix->rows;
16298 if (row->mode_line_p)
16299 ++row;
16300 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16303 if (!cursor_row_fully_visible_p (w, 0, 0))
16305 /* If vscroll is enabled, disable it and try again. */
16306 if (w->vscroll)
16308 w->vscroll = 0;
16309 clear_glyph_matrix (w->desired_matrix);
16310 goto recenter;
16313 /* Users who set scroll-conservatively to a large number want
16314 point just above/below the scroll margin. If we ended up
16315 with point's row partially visible, move the window start to
16316 make that row fully visible and out of the margin. */
16317 if (scroll_conservatively > SCROLL_LIMIT)
16319 int window_total_lines
16320 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16321 int margin =
16322 scroll_margin > 0
16323 ? min (scroll_margin, window_total_lines / 4)
16324 : 0;
16325 int move_down = w->cursor.vpos >= window_total_lines / 2;
16327 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16328 clear_glyph_matrix (w->desired_matrix);
16329 if (1 == try_window (window, it.current.pos,
16330 TRY_WINDOW_CHECK_MARGINS))
16331 goto done;
16334 /* If centering point failed to make the whole line visible,
16335 put point at the top instead. That has to make the whole line
16336 visible, if it can be done. */
16337 if (centering_position == 0)
16338 goto done;
16340 clear_glyph_matrix (w->desired_matrix);
16341 centering_position = 0;
16342 goto recenter;
16345 done:
16347 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16348 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16349 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16351 /* Display the mode line, if we must. */
16352 if ((update_mode_line
16353 /* If window not full width, must redo its mode line
16354 if (a) the window to its side is being redone and
16355 (b) we do a frame-based redisplay. This is a consequence
16356 of how inverted lines are drawn in frame-based redisplay. */
16357 || (!just_this_one_p
16358 && !FRAME_WINDOW_P (f)
16359 && !WINDOW_FULL_WIDTH_P (w))
16360 /* Line number to display. */
16361 || w->base_line_pos > 0
16362 /* Column number is displayed and different from the one displayed. */
16363 || (w->column_number_displayed != -1
16364 && (w->column_number_displayed != current_column ())))
16365 /* This means that the window has a mode line. */
16366 && (WINDOW_WANTS_MODELINE_P (w)
16367 || WINDOW_WANTS_HEADER_LINE_P (w)))
16370 display_mode_lines (w);
16372 /* If mode line height has changed, arrange for a thorough
16373 immediate redisplay using the correct mode line height. */
16374 if (WINDOW_WANTS_MODELINE_P (w)
16375 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16377 f->fonts_changed = 1;
16378 w->mode_line_height = -1;
16379 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16380 = DESIRED_MODE_LINE_HEIGHT (w);
16383 /* If header line height has changed, arrange for a thorough
16384 immediate redisplay using the correct header line height. */
16385 if (WINDOW_WANTS_HEADER_LINE_P (w)
16386 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16388 f->fonts_changed = 1;
16389 w->header_line_height = -1;
16390 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16391 = DESIRED_HEADER_LINE_HEIGHT (w);
16394 if (f->fonts_changed)
16395 goto need_larger_matrices;
16398 if (!line_number_displayed && w->base_line_pos != -1)
16400 w->base_line_pos = 0;
16401 w->base_line_number = 0;
16404 finish_menu_bars:
16406 /* When we reach a frame's selected window, redo the frame's menu bar. */
16407 if (update_mode_line
16408 && EQ (FRAME_SELECTED_WINDOW (f), window))
16410 int redisplay_menu_p = 0;
16412 if (FRAME_WINDOW_P (f))
16414 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16415 || defined (HAVE_NS) || defined (USE_GTK)
16416 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16417 #else
16418 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16419 #endif
16421 else
16422 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16424 if (redisplay_menu_p)
16425 display_menu_bar (w);
16427 #ifdef HAVE_WINDOW_SYSTEM
16428 if (FRAME_WINDOW_P (f))
16430 #if defined (USE_GTK) || defined (HAVE_NS)
16431 if (FRAME_EXTERNAL_TOOL_BAR (f))
16432 redisplay_tool_bar (f);
16433 #else
16434 if (WINDOWP (f->tool_bar_window)
16435 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16436 || !NILP (Vauto_resize_tool_bars))
16437 && redisplay_tool_bar (f))
16438 ignore_mouse_drag_p = 1;
16439 #endif
16441 #endif
16444 #ifdef HAVE_WINDOW_SYSTEM
16445 if (FRAME_WINDOW_P (f)
16446 && update_window_fringes (w, (just_this_one_p
16447 || (!used_current_matrix_p && !overlay_arrow_seen)
16448 || w->pseudo_window_p)))
16450 update_begin (f);
16451 block_input ();
16452 if (draw_window_fringes (w, 1))
16454 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16455 x_draw_right_divider (w);
16456 else
16457 x_draw_vertical_border (w);
16459 unblock_input ();
16460 update_end (f);
16463 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16464 x_draw_bottom_divider (w);
16465 #endif /* HAVE_WINDOW_SYSTEM */
16467 /* We go to this label, with fonts_changed set, if it is
16468 necessary to try again using larger glyph matrices.
16469 We have to redeem the scroll bar even in this case,
16470 because the loop in redisplay_internal expects that. */
16471 need_larger_matrices:
16473 finish_scroll_bars:
16475 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16477 /* Set the thumb's position and size. */
16478 set_vertical_scroll_bar (w);
16480 /* Note that we actually used the scroll bar attached to this
16481 window, so it shouldn't be deleted at the end of redisplay. */
16482 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16483 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16486 /* Restore current_buffer and value of point in it. The window
16487 update may have changed the buffer, so first make sure `opoint'
16488 is still valid (Bug#6177). */
16489 if (CHARPOS (opoint) < BEGV)
16490 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16491 else if (CHARPOS (opoint) > ZV)
16492 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16493 else
16494 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16496 set_buffer_internal_1 (old);
16497 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16498 shorter. This can be caused by log truncation in *Messages*. */
16499 if (CHARPOS (lpoint) <= ZV)
16500 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16502 unbind_to (count, Qnil);
16506 /* Build the complete desired matrix of WINDOW with a window start
16507 buffer position POS.
16509 Value is 1 if successful. It is zero if fonts were loaded during
16510 redisplay which makes re-adjusting glyph matrices necessary, and -1
16511 if point would appear in the scroll margins.
16512 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16513 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16514 set in FLAGS.) */
16517 try_window (Lisp_Object window, struct text_pos pos, int flags)
16519 struct window *w = XWINDOW (window);
16520 struct it it;
16521 struct glyph_row *last_text_row = NULL;
16522 struct frame *f = XFRAME (w->frame);
16523 int frame_line_height = default_line_pixel_height (w);
16525 /* Make POS the new window start. */
16526 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16528 /* Mark cursor position as unknown. No overlay arrow seen. */
16529 w->cursor.vpos = -1;
16530 overlay_arrow_seen = 0;
16532 /* Initialize iterator and info to start at POS. */
16533 start_display (&it, w, pos);
16535 /* Display all lines of W. */
16536 while (it.current_y < it.last_visible_y)
16538 if (display_line (&it))
16539 last_text_row = it.glyph_row - 1;
16540 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16541 return 0;
16544 /* Don't let the cursor end in the scroll margins. */
16545 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16546 && !MINI_WINDOW_P (w))
16548 int this_scroll_margin;
16549 int window_total_lines
16550 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16552 if (scroll_margin > 0)
16554 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16555 this_scroll_margin *= frame_line_height;
16557 else
16558 this_scroll_margin = 0;
16560 if ((w->cursor.y >= 0 /* not vscrolled */
16561 && w->cursor.y < this_scroll_margin
16562 && CHARPOS (pos) > BEGV
16563 && IT_CHARPOS (it) < ZV)
16564 /* rms: considering make_cursor_line_fully_visible_p here
16565 seems to give wrong results. We don't want to recenter
16566 when the last line is partly visible, we want to allow
16567 that case to be handled in the usual way. */
16568 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16570 w->cursor.vpos = -1;
16571 clear_glyph_matrix (w->desired_matrix);
16572 return -1;
16576 /* If bottom moved off end of frame, change mode line percentage. */
16577 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16578 w->update_mode_line = 1;
16580 /* Set window_end_pos to the offset of the last character displayed
16581 on the window from the end of current_buffer. Set
16582 window_end_vpos to its row number. */
16583 if (last_text_row)
16585 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16586 adjust_window_ends (w, last_text_row, 0);
16587 eassert
16588 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16589 w->window_end_vpos)));
16591 else
16593 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16594 w->window_end_pos = Z - ZV;
16595 w->window_end_vpos = 0;
16598 /* But that is not valid info until redisplay finishes. */
16599 w->window_end_valid = 0;
16600 return 1;
16605 /************************************************************************
16606 Window redisplay reusing current matrix when buffer has not changed
16607 ************************************************************************/
16609 /* Try redisplay of window W showing an unchanged buffer with a
16610 different window start than the last time it was displayed by
16611 reusing its current matrix. Value is non-zero if successful.
16612 W->start is the new window start. */
16614 static int
16615 try_window_reusing_current_matrix (struct window *w)
16617 struct frame *f = XFRAME (w->frame);
16618 struct glyph_row *bottom_row;
16619 struct it it;
16620 struct run run;
16621 struct text_pos start, new_start;
16622 int nrows_scrolled, i;
16623 struct glyph_row *last_text_row;
16624 struct glyph_row *last_reused_text_row;
16625 struct glyph_row *start_row;
16626 int start_vpos, min_y, max_y;
16628 #ifdef GLYPH_DEBUG
16629 if (inhibit_try_window_reusing)
16630 return 0;
16631 #endif
16633 if (/* This function doesn't handle terminal frames. */
16634 !FRAME_WINDOW_P (f)
16635 /* Don't try to reuse the display if windows have been split
16636 or such. */
16637 || windows_or_buffers_changed
16638 || f->cursor_type_changed)
16639 return 0;
16641 /* Can't do this if showing trailing whitespace. */
16642 if (!NILP (Vshow_trailing_whitespace))
16643 return 0;
16645 /* If top-line visibility has changed, give up. */
16646 if (WINDOW_WANTS_HEADER_LINE_P (w)
16647 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16648 return 0;
16650 /* Give up if old or new display is scrolled vertically. We could
16651 make this function handle this, but right now it doesn't. */
16652 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16653 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16654 return 0;
16656 /* The variable new_start now holds the new window start. The old
16657 start `start' can be determined from the current matrix. */
16658 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16659 start = start_row->minpos;
16660 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16662 /* Clear the desired matrix for the display below. */
16663 clear_glyph_matrix (w->desired_matrix);
16665 if (CHARPOS (new_start) <= CHARPOS (start))
16667 /* Don't use this method if the display starts with an ellipsis
16668 displayed for invisible text. It's not easy to handle that case
16669 below, and it's certainly not worth the effort since this is
16670 not a frequent case. */
16671 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16672 return 0;
16674 IF_DEBUG (debug_method_add (w, "twu1"));
16676 /* Display up to a row that can be reused. The variable
16677 last_text_row is set to the last row displayed that displays
16678 text. Note that it.vpos == 0 if or if not there is a
16679 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16680 start_display (&it, w, new_start);
16681 w->cursor.vpos = -1;
16682 last_text_row = last_reused_text_row = NULL;
16684 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16686 /* If we have reached into the characters in the START row,
16687 that means the line boundaries have changed. So we
16688 can't start copying with the row START. Maybe it will
16689 work to start copying with the following row. */
16690 while (IT_CHARPOS (it) > CHARPOS (start))
16692 /* Advance to the next row as the "start". */
16693 start_row++;
16694 start = start_row->minpos;
16695 /* If there are no more rows to try, or just one, give up. */
16696 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16697 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16698 || CHARPOS (start) == ZV)
16700 clear_glyph_matrix (w->desired_matrix);
16701 return 0;
16704 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16706 /* If we have reached alignment, we can copy the rest of the
16707 rows. */
16708 if (IT_CHARPOS (it) == CHARPOS (start)
16709 /* Don't accept "alignment" inside a display vector,
16710 since start_row could have started in the middle of
16711 that same display vector (thus their character
16712 positions match), and we have no way of telling if
16713 that is the case. */
16714 && it.current.dpvec_index < 0)
16715 break;
16717 if (display_line (&it))
16718 last_text_row = it.glyph_row - 1;
16722 /* A value of current_y < last_visible_y means that we stopped
16723 at the previous window start, which in turn means that we
16724 have at least one reusable row. */
16725 if (it.current_y < it.last_visible_y)
16727 struct glyph_row *row;
16729 /* IT.vpos always starts from 0; it counts text lines. */
16730 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16732 /* Find PT if not already found in the lines displayed. */
16733 if (w->cursor.vpos < 0)
16735 int dy = it.current_y - start_row->y;
16737 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16738 row = row_containing_pos (w, PT, row, NULL, dy);
16739 if (row)
16740 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16741 dy, nrows_scrolled);
16742 else
16744 clear_glyph_matrix (w->desired_matrix);
16745 return 0;
16749 /* Scroll the display. Do it before the current matrix is
16750 changed. The problem here is that update has not yet
16751 run, i.e. part of the current matrix is not up to date.
16752 scroll_run_hook will clear the cursor, and use the
16753 current matrix to get the height of the row the cursor is
16754 in. */
16755 run.current_y = start_row->y;
16756 run.desired_y = it.current_y;
16757 run.height = it.last_visible_y - it.current_y;
16759 if (run.height > 0 && run.current_y != run.desired_y)
16761 update_begin (f);
16762 FRAME_RIF (f)->update_window_begin_hook (w);
16763 FRAME_RIF (f)->clear_window_mouse_face (w);
16764 FRAME_RIF (f)->scroll_run_hook (w, &run);
16765 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16766 update_end (f);
16769 /* Shift current matrix down by nrows_scrolled lines. */
16770 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16771 rotate_matrix (w->current_matrix,
16772 start_vpos,
16773 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16774 nrows_scrolled);
16776 /* Disable lines that must be updated. */
16777 for (i = 0; i < nrows_scrolled; ++i)
16778 (start_row + i)->enabled_p = false;
16780 /* Re-compute Y positions. */
16781 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16782 max_y = it.last_visible_y;
16783 for (row = start_row + nrows_scrolled;
16784 row < bottom_row;
16785 ++row)
16787 row->y = it.current_y;
16788 row->visible_height = row->height;
16790 if (row->y < min_y)
16791 row->visible_height -= min_y - row->y;
16792 if (row->y + row->height > max_y)
16793 row->visible_height -= row->y + row->height - max_y;
16794 if (row->fringe_bitmap_periodic_p)
16795 row->redraw_fringe_bitmaps_p = 1;
16797 it.current_y += row->height;
16799 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16800 last_reused_text_row = row;
16801 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16802 break;
16805 /* Disable lines in the current matrix which are now
16806 below the window. */
16807 for (++row; row < bottom_row; ++row)
16808 row->enabled_p = row->mode_line_p = 0;
16811 /* Update window_end_pos etc.; last_reused_text_row is the last
16812 reused row from the current matrix containing text, if any.
16813 The value of last_text_row is the last displayed line
16814 containing text. */
16815 if (last_reused_text_row)
16816 adjust_window_ends (w, last_reused_text_row, 1);
16817 else if (last_text_row)
16818 adjust_window_ends (w, last_text_row, 0);
16819 else
16821 /* This window must be completely empty. */
16822 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16823 w->window_end_pos = Z - ZV;
16824 w->window_end_vpos = 0;
16826 w->window_end_valid = 0;
16828 /* Update hint: don't try scrolling again in update_window. */
16829 w->desired_matrix->no_scrolling_p = 1;
16831 #ifdef GLYPH_DEBUG
16832 debug_method_add (w, "try_window_reusing_current_matrix 1");
16833 #endif
16834 return 1;
16836 else if (CHARPOS (new_start) > CHARPOS (start))
16838 struct glyph_row *pt_row, *row;
16839 struct glyph_row *first_reusable_row;
16840 struct glyph_row *first_row_to_display;
16841 int dy;
16842 int yb = window_text_bottom_y (w);
16844 /* Find the row starting at new_start, if there is one. Don't
16845 reuse a partially visible line at the end. */
16846 first_reusable_row = start_row;
16847 while (first_reusable_row->enabled_p
16848 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16849 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16850 < CHARPOS (new_start)))
16851 ++first_reusable_row;
16853 /* Give up if there is no row to reuse. */
16854 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16855 || !first_reusable_row->enabled_p
16856 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16857 != CHARPOS (new_start)))
16858 return 0;
16860 /* We can reuse fully visible rows beginning with
16861 first_reusable_row to the end of the window. Set
16862 first_row_to_display to the first row that cannot be reused.
16863 Set pt_row to the row containing point, if there is any. */
16864 pt_row = NULL;
16865 for (first_row_to_display = first_reusable_row;
16866 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16867 ++first_row_to_display)
16869 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16870 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16871 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16872 && first_row_to_display->ends_at_zv_p
16873 && pt_row == NULL)))
16874 pt_row = first_row_to_display;
16877 /* Start displaying at the start of first_row_to_display. */
16878 eassert (first_row_to_display->y < yb);
16879 init_to_row_start (&it, w, first_row_to_display);
16881 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16882 - start_vpos);
16883 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16884 - nrows_scrolled);
16885 it.current_y = (first_row_to_display->y - first_reusable_row->y
16886 + WINDOW_HEADER_LINE_HEIGHT (w));
16888 /* Display lines beginning with first_row_to_display in the
16889 desired matrix. Set last_text_row to the last row displayed
16890 that displays text. */
16891 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16892 if (pt_row == NULL)
16893 w->cursor.vpos = -1;
16894 last_text_row = NULL;
16895 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16896 if (display_line (&it))
16897 last_text_row = it.glyph_row - 1;
16899 /* If point is in a reused row, adjust y and vpos of the cursor
16900 position. */
16901 if (pt_row)
16903 w->cursor.vpos -= nrows_scrolled;
16904 w->cursor.y -= first_reusable_row->y - start_row->y;
16907 /* Give up if point isn't in a row displayed or reused. (This
16908 also handles the case where w->cursor.vpos < nrows_scrolled
16909 after the calls to display_line, which can happen with scroll
16910 margins. See bug#1295.) */
16911 if (w->cursor.vpos < 0)
16913 clear_glyph_matrix (w->desired_matrix);
16914 return 0;
16917 /* Scroll the display. */
16918 run.current_y = first_reusable_row->y;
16919 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16920 run.height = it.last_visible_y - run.current_y;
16921 dy = run.current_y - run.desired_y;
16923 if (run.height)
16925 update_begin (f);
16926 FRAME_RIF (f)->update_window_begin_hook (w);
16927 FRAME_RIF (f)->clear_window_mouse_face (w);
16928 FRAME_RIF (f)->scroll_run_hook (w, &run);
16929 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16930 update_end (f);
16933 /* Adjust Y positions of reused rows. */
16934 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16935 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16936 max_y = it.last_visible_y;
16937 for (row = first_reusable_row; row < first_row_to_display; ++row)
16939 row->y -= dy;
16940 row->visible_height = row->height;
16941 if (row->y < min_y)
16942 row->visible_height -= min_y - row->y;
16943 if (row->y + row->height > max_y)
16944 row->visible_height -= row->y + row->height - max_y;
16945 if (row->fringe_bitmap_periodic_p)
16946 row->redraw_fringe_bitmaps_p = 1;
16949 /* Scroll the current matrix. */
16950 eassert (nrows_scrolled > 0);
16951 rotate_matrix (w->current_matrix,
16952 start_vpos,
16953 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16954 -nrows_scrolled);
16956 /* Disable rows not reused. */
16957 for (row -= nrows_scrolled; row < bottom_row; ++row)
16958 row->enabled_p = false;
16960 /* Point may have moved to a different line, so we cannot assume that
16961 the previous cursor position is valid; locate the correct row. */
16962 if (pt_row)
16964 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16965 row < bottom_row
16966 && PT >= MATRIX_ROW_END_CHARPOS (row)
16967 && !row->ends_at_zv_p;
16968 row++)
16970 w->cursor.vpos++;
16971 w->cursor.y = row->y;
16973 if (row < bottom_row)
16975 /* Can't simply scan the row for point with
16976 bidi-reordered glyph rows. Let set_cursor_from_row
16977 figure out where to put the cursor, and if it fails,
16978 give up. */
16979 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16981 if (!set_cursor_from_row (w, row, w->current_matrix,
16982 0, 0, 0, 0))
16984 clear_glyph_matrix (w->desired_matrix);
16985 return 0;
16988 else
16990 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16991 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16993 for (; glyph < end
16994 && (!BUFFERP (glyph->object)
16995 || glyph->charpos < PT);
16996 glyph++)
16998 w->cursor.hpos++;
16999 w->cursor.x += glyph->pixel_width;
17005 /* Adjust window end. A null value of last_text_row means that
17006 the window end is in reused rows which in turn means that
17007 only its vpos can have changed. */
17008 if (last_text_row)
17009 adjust_window_ends (w, last_text_row, 0);
17010 else
17011 w->window_end_vpos -= nrows_scrolled;
17013 w->window_end_valid = 0;
17014 w->desired_matrix->no_scrolling_p = 1;
17016 #ifdef GLYPH_DEBUG
17017 debug_method_add (w, "try_window_reusing_current_matrix 2");
17018 #endif
17019 return 1;
17022 return 0;
17027 /************************************************************************
17028 Window redisplay reusing current matrix when buffer has changed
17029 ************************************************************************/
17031 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17032 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17033 ptrdiff_t *, ptrdiff_t *);
17034 static struct glyph_row *
17035 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17036 struct glyph_row *);
17039 /* Return the last row in MATRIX displaying text. If row START is
17040 non-null, start searching with that row. IT gives the dimensions
17041 of the display. Value is null if matrix is empty; otherwise it is
17042 a pointer to the row found. */
17044 static struct glyph_row *
17045 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17046 struct glyph_row *start)
17048 struct glyph_row *row, *row_found;
17050 /* Set row_found to the last row in IT->w's current matrix
17051 displaying text. The loop looks funny but think of partially
17052 visible lines. */
17053 row_found = NULL;
17054 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17055 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17057 eassert (row->enabled_p);
17058 row_found = row;
17059 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17060 break;
17061 ++row;
17064 return row_found;
17068 /* Return the last row in the current matrix of W that is not affected
17069 by changes at the start of current_buffer that occurred since W's
17070 current matrix was built. Value is null if no such row exists.
17072 BEG_UNCHANGED us the number of characters unchanged at the start of
17073 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17074 first changed character in current_buffer. Characters at positions <
17075 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17076 when the current matrix was built. */
17078 static struct glyph_row *
17079 find_last_unchanged_at_beg_row (struct window *w)
17081 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17082 struct glyph_row *row;
17083 struct glyph_row *row_found = NULL;
17084 int yb = window_text_bottom_y (w);
17086 /* Find the last row displaying unchanged text. */
17087 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17088 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17089 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17090 ++row)
17092 if (/* If row ends before first_changed_pos, it is unchanged,
17093 except in some case. */
17094 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17095 /* When row ends in ZV and we write at ZV it is not
17096 unchanged. */
17097 && !row->ends_at_zv_p
17098 /* When first_changed_pos is the end of a continued line,
17099 row is not unchanged because it may be no longer
17100 continued. */
17101 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17102 && (row->continued_p
17103 || row->exact_window_width_line_p))
17104 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17105 needs to be recomputed, so don't consider this row as
17106 unchanged. This happens when the last line was
17107 bidi-reordered and was killed immediately before this
17108 redisplay cycle. In that case, ROW->end stores the
17109 buffer position of the first visual-order character of
17110 the killed text, which is now beyond ZV. */
17111 && CHARPOS (row->end.pos) <= ZV)
17112 row_found = row;
17114 /* Stop if last visible row. */
17115 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17116 break;
17119 return row_found;
17123 /* Find the first glyph row in the current matrix of W that is not
17124 affected by changes at the end of current_buffer since the
17125 time W's current matrix was built.
17127 Return in *DELTA the number of chars by which buffer positions in
17128 unchanged text at the end of current_buffer must be adjusted.
17130 Return in *DELTA_BYTES the corresponding number of bytes.
17132 Value is null if no such row exists, i.e. all rows are affected by
17133 changes. */
17135 static struct glyph_row *
17136 find_first_unchanged_at_end_row (struct window *w,
17137 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17139 struct glyph_row *row;
17140 struct glyph_row *row_found = NULL;
17142 *delta = *delta_bytes = 0;
17144 /* Display must not have been paused, otherwise the current matrix
17145 is not up to date. */
17146 eassert (w->window_end_valid);
17148 /* A value of window_end_pos >= END_UNCHANGED means that the window
17149 end is in the range of changed text. If so, there is no
17150 unchanged row at the end of W's current matrix. */
17151 if (w->window_end_pos >= END_UNCHANGED)
17152 return NULL;
17154 /* Set row to the last row in W's current matrix displaying text. */
17155 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17157 /* If matrix is entirely empty, no unchanged row exists. */
17158 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17160 /* The value of row is the last glyph row in the matrix having a
17161 meaningful buffer position in it. The end position of row
17162 corresponds to window_end_pos. This allows us to translate
17163 buffer positions in the current matrix to current buffer
17164 positions for characters not in changed text. */
17165 ptrdiff_t Z_old =
17166 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17167 ptrdiff_t Z_BYTE_old =
17168 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17169 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17170 struct glyph_row *first_text_row
17171 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17173 *delta = Z - Z_old;
17174 *delta_bytes = Z_BYTE - Z_BYTE_old;
17176 /* Set last_unchanged_pos to the buffer position of the last
17177 character in the buffer that has not been changed. Z is the
17178 index + 1 of the last character in current_buffer, i.e. by
17179 subtracting END_UNCHANGED we get the index of the last
17180 unchanged character, and we have to add BEG to get its buffer
17181 position. */
17182 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17183 last_unchanged_pos_old = last_unchanged_pos - *delta;
17185 /* Search backward from ROW for a row displaying a line that
17186 starts at a minimum position >= last_unchanged_pos_old. */
17187 for (; row > first_text_row; --row)
17189 /* This used to abort, but it can happen.
17190 It is ok to just stop the search instead here. KFS. */
17191 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17192 break;
17194 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17195 row_found = row;
17199 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17201 return row_found;
17205 /* Make sure that glyph rows in the current matrix of window W
17206 reference the same glyph memory as corresponding rows in the
17207 frame's frame matrix. This function is called after scrolling W's
17208 current matrix on a terminal frame in try_window_id and
17209 try_window_reusing_current_matrix. */
17211 static void
17212 sync_frame_with_window_matrix_rows (struct window *w)
17214 struct frame *f = XFRAME (w->frame);
17215 struct glyph_row *window_row, *window_row_end, *frame_row;
17217 /* Preconditions: W must be a leaf window and full-width. Its frame
17218 must have a frame matrix. */
17219 eassert (BUFFERP (w->contents));
17220 eassert (WINDOW_FULL_WIDTH_P (w));
17221 eassert (!FRAME_WINDOW_P (f));
17223 /* If W is a full-width window, glyph pointers in W's current matrix
17224 have, by definition, to be the same as glyph pointers in the
17225 corresponding frame matrix. Note that frame matrices have no
17226 marginal areas (see build_frame_matrix). */
17227 window_row = w->current_matrix->rows;
17228 window_row_end = window_row + w->current_matrix->nrows;
17229 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17230 while (window_row < window_row_end)
17232 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17233 struct glyph *end = window_row->glyphs[LAST_AREA];
17235 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17236 frame_row->glyphs[TEXT_AREA] = start;
17237 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17238 frame_row->glyphs[LAST_AREA] = end;
17240 /* Disable frame rows whose corresponding window rows have
17241 been disabled in try_window_id. */
17242 if (!window_row->enabled_p)
17243 frame_row->enabled_p = false;
17245 ++window_row, ++frame_row;
17250 /* Find the glyph row in window W containing CHARPOS. Consider all
17251 rows between START and END (not inclusive). END null means search
17252 all rows to the end of the display area of W. Value is the row
17253 containing CHARPOS or null. */
17255 struct glyph_row *
17256 row_containing_pos (struct window *w, ptrdiff_t charpos,
17257 struct glyph_row *start, struct glyph_row *end, int dy)
17259 struct glyph_row *row = start;
17260 struct glyph_row *best_row = NULL;
17261 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17262 int last_y;
17264 /* If we happen to start on a header-line, skip that. */
17265 if (row->mode_line_p)
17266 ++row;
17268 if ((end && row >= end) || !row->enabled_p)
17269 return NULL;
17271 last_y = window_text_bottom_y (w) - dy;
17273 while (1)
17275 /* Give up if we have gone too far. */
17276 if (end && row >= end)
17277 return NULL;
17278 /* This formerly returned if they were equal.
17279 I think that both quantities are of a "last plus one" type;
17280 if so, when they are equal, the row is within the screen. -- rms. */
17281 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17282 return NULL;
17284 /* If it is in this row, return this row. */
17285 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17286 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17287 /* The end position of a row equals the start
17288 position of the next row. If CHARPOS is there, we
17289 would rather consider it displayed in the next
17290 line, except when this line ends in ZV. */
17291 && !row_for_charpos_p (row, charpos)))
17292 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17294 struct glyph *g;
17296 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17297 || (!best_row && !row->continued_p))
17298 return row;
17299 /* In bidi-reordered rows, there could be several rows whose
17300 edges surround CHARPOS, all of these rows belonging to
17301 the same continued line. We need to find the row which
17302 fits CHARPOS the best. */
17303 for (g = row->glyphs[TEXT_AREA];
17304 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17305 g++)
17307 if (!STRINGP (g->object))
17309 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17311 mindif = eabs (g->charpos - charpos);
17312 best_row = row;
17313 /* Exact match always wins. */
17314 if (mindif == 0)
17315 return best_row;
17320 else if (best_row && !row->continued_p)
17321 return best_row;
17322 ++row;
17327 /* Try to redisplay window W by reusing its existing display. W's
17328 current matrix must be up to date when this function is called,
17329 i.e. window_end_valid must be nonzero.
17331 Value is
17333 >= 1 if successful, i.e. display has been updated
17334 specifically:
17335 1 means the changes were in front of a newline that precedes
17336 the window start, and the whole current matrix was reused
17337 2 means the changes were after the last position displayed
17338 in the window, and the whole current matrix was reused
17339 3 means portions of the current matrix were reused, while
17340 some of the screen lines were redrawn
17341 -1 if redisplay with same window start is known not to succeed
17342 0 if otherwise unsuccessful
17344 The following steps are performed:
17346 1. Find the last row in the current matrix of W that is not
17347 affected by changes at the start of current_buffer. If no such row
17348 is found, give up.
17350 2. Find the first row in W's current matrix that is not affected by
17351 changes at the end of current_buffer. Maybe there is no such row.
17353 3. Display lines beginning with the row + 1 found in step 1 to the
17354 row found in step 2 or, if step 2 didn't find a row, to the end of
17355 the window.
17357 4. If cursor is not known to appear on the window, give up.
17359 5. If display stopped at the row found in step 2, scroll the
17360 display and current matrix as needed.
17362 6. Maybe display some lines at the end of W, if we must. This can
17363 happen under various circumstances, like a partially visible line
17364 becoming fully visible, or because newly displayed lines are displayed
17365 in smaller font sizes.
17367 7. Update W's window end information. */
17369 static int
17370 try_window_id (struct window *w)
17372 struct frame *f = XFRAME (w->frame);
17373 struct glyph_matrix *current_matrix = w->current_matrix;
17374 struct glyph_matrix *desired_matrix = w->desired_matrix;
17375 struct glyph_row *last_unchanged_at_beg_row;
17376 struct glyph_row *first_unchanged_at_end_row;
17377 struct glyph_row *row;
17378 struct glyph_row *bottom_row;
17379 int bottom_vpos;
17380 struct it it;
17381 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17382 int dvpos, dy;
17383 struct text_pos start_pos;
17384 struct run run;
17385 int first_unchanged_at_end_vpos = 0;
17386 struct glyph_row *last_text_row, *last_text_row_at_end;
17387 struct text_pos start;
17388 ptrdiff_t first_changed_charpos, last_changed_charpos;
17390 #ifdef GLYPH_DEBUG
17391 if (inhibit_try_window_id)
17392 return 0;
17393 #endif
17395 /* This is handy for debugging. */
17396 #if 0
17397 #define GIVE_UP(X) \
17398 do { \
17399 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17400 return 0; \
17401 } while (0)
17402 #else
17403 #define GIVE_UP(X) return 0
17404 #endif
17406 SET_TEXT_POS_FROM_MARKER (start, w->start);
17408 /* Don't use this for mini-windows because these can show
17409 messages and mini-buffers, and we don't handle that here. */
17410 if (MINI_WINDOW_P (w))
17411 GIVE_UP (1);
17413 /* This flag is used to prevent redisplay optimizations. */
17414 if (windows_or_buffers_changed || f->cursor_type_changed)
17415 GIVE_UP (2);
17417 /* This function's optimizations cannot be used if overlays have
17418 changed in the buffer displayed by the window, so give up if they
17419 have. */
17420 if (w->last_overlay_modified != OVERLAY_MODIFF)
17421 GIVE_UP (21);
17423 /* Verify that narrowing has not changed.
17424 Also verify that we were not told to prevent redisplay optimizations.
17425 It would be nice to further
17426 reduce the number of cases where this prevents try_window_id. */
17427 if (current_buffer->clip_changed
17428 || current_buffer->prevent_redisplay_optimizations_p)
17429 GIVE_UP (3);
17431 /* Window must either use window-based redisplay or be full width. */
17432 if (!FRAME_WINDOW_P (f)
17433 && (!FRAME_LINE_INS_DEL_OK (f)
17434 || !WINDOW_FULL_WIDTH_P (w)))
17435 GIVE_UP (4);
17437 /* Give up if point is known NOT to appear in W. */
17438 if (PT < CHARPOS (start))
17439 GIVE_UP (5);
17441 /* Another way to prevent redisplay optimizations. */
17442 if (w->last_modified == 0)
17443 GIVE_UP (6);
17445 /* Verify that window is not hscrolled. */
17446 if (w->hscroll != 0)
17447 GIVE_UP (7);
17449 /* Verify that display wasn't paused. */
17450 if (!w->window_end_valid)
17451 GIVE_UP (8);
17453 /* Likewise if highlighting trailing whitespace. */
17454 if (!NILP (Vshow_trailing_whitespace))
17455 GIVE_UP (11);
17457 /* Can't use this if overlay arrow position and/or string have
17458 changed. */
17459 if (overlay_arrows_changed_p ())
17460 GIVE_UP (12);
17462 /* When word-wrap is on, adding a space to the first word of a
17463 wrapped line can change the wrap position, altering the line
17464 above it. It might be worthwhile to handle this more
17465 intelligently, but for now just redisplay from scratch. */
17466 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17467 GIVE_UP (21);
17469 /* Under bidi reordering, adding or deleting a character in the
17470 beginning of a paragraph, before the first strong directional
17471 character, can change the base direction of the paragraph (unless
17472 the buffer specifies a fixed paragraph direction), which will
17473 require to redisplay the whole paragraph. It might be worthwhile
17474 to find the paragraph limits and widen the range of redisplayed
17475 lines to that, but for now just give up this optimization and
17476 redisplay from scratch. */
17477 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17478 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17479 GIVE_UP (22);
17481 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17482 only if buffer has really changed. The reason is that the gap is
17483 initially at Z for freshly visited files. The code below would
17484 set end_unchanged to 0 in that case. */
17485 if (MODIFF > SAVE_MODIFF
17486 /* This seems to happen sometimes after saving a buffer. */
17487 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17489 if (GPT - BEG < BEG_UNCHANGED)
17490 BEG_UNCHANGED = GPT - BEG;
17491 if (Z - GPT < END_UNCHANGED)
17492 END_UNCHANGED = Z - GPT;
17495 /* The position of the first and last character that has been changed. */
17496 first_changed_charpos = BEG + BEG_UNCHANGED;
17497 last_changed_charpos = Z - END_UNCHANGED;
17499 /* If window starts after a line end, and the last change is in
17500 front of that newline, then changes don't affect the display.
17501 This case happens with stealth-fontification. Note that although
17502 the display is unchanged, glyph positions in the matrix have to
17503 be adjusted, of course. */
17504 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17505 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17506 && ((last_changed_charpos < CHARPOS (start)
17507 && CHARPOS (start) == BEGV)
17508 || (last_changed_charpos < CHARPOS (start) - 1
17509 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17511 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17512 struct glyph_row *r0;
17514 /* Compute how many chars/bytes have been added to or removed
17515 from the buffer. */
17516 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17517 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17518 Z_delta = Z - Z_old;
17519 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17521 /* Give up if PT is not in the window. Note that it already has
17522 been checked at the start of try_window_id that PT is not in
17523 front of the window start. */
17524 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17525 GIVE_UP (13);
17527 /* If window start is unchanged, we can reuse the whole matrix
17528 as is, after adjusting glyph positions. No need to compute
17529 the window end again, since its offset from Z hasn't changed. */
17530 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17531 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17532 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17533 /* PT must not be in a partially visible line. */
17534 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17535 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17537 /* Adjust positions in the glyph matrix. */
17538 if (Z_delta || Z_delta_bytes)
17540 struct glyph_row *r1
17541 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17542 increment_matrix_positions (w->current_matrix,
17543 MATRIX_ROW_VPOS (r0, current_matrix),
17544 MATRIX_ROW_VPOS (r1, current_matrix),
17545 Z_delta, Z_delta_bytes);
17548 /* Set the cursor. */
17549 row = row_containing_pos (w, PT, r0, NULL, 0);
17550 if (row)
17551 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17552 return 1;
17556 /* Handle the case that changes are all below what is displayed in
17557 the window, and that PT is in the window. This shortcut cannot
17558 be taken if ZV is visible in the window, and text has been added
17559 there that is visible in the window. */
17560 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17561 /* ZV is not visible in the window, or there are no
17562 changes at ZV, actually. */
17563 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17564 || first_changed_charpos == last_changed_charpos))
17566 struct glyph_row *r0;
17568 /* Give up if PT is not in the window. Note that it already has
17569 been checked at the start of try_window_id that PT is not in
17570 front of the window start. */
17571 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17572 GIVE_UP (14);
17574 /* If window start is unchanged, we can reuse the whole matrix
17575 as is, without changing glyph positions since no text has
17576 been added/removed in front of the window end. */
17577 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17578 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17579 /* PT must not be in a partially visible line. */
17580 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17581 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17583 /* We have to compute the window end anew since text
17584 could have been added/removed after it. */
17585 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17586 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17588 /* Set the cursor. */
17589 row = row_containing_pos (w, PT, r0, NULL, 0);
17590 if (row)
17591 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17592 return 2;
17596 /* Give up if window start is in the changed area.
17598 The condition used to read
17600 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17602 but why that was tested escapes me at the moment. */
17603 if (CHARPOS (start) >= first_changed_charpos
17604 && CHARPOS (start) <= last_changed_charpos)
17605 GIVE_UP (15);
17607 /* Check that window start agrees with the start of the first glyph
17608 row in its current matrix. Check this after we know the window
17609 start is not in changed text, otherwise positions would not be
17610 comparable. */
17611 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17612 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17613 GIVE_UP (16);
17615 /* Give up if the window ends in strings. Overlay strings
17616 at the end are difficult to handle, so don't try. */
17617 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17618 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17619 GIVE_UP (20);
17621 /* Compute the position at which we have to start displaying new
17622 lines. Some of the lines at the top of the window might be
17623 reusable because they are not displaying changed text. Find the
17624 last row in W's current matrix not affected by changes at the
17625 start of current_buffer. Value is null if changes start in the
17626 first line of window. */
17627 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17628 if (last_unchanged_at_beg_row)
17630 /* Avoid starting to display in the middle of a character, a TAB
17631 for instance. This is easier than to set up the iterator
17632 exactly, and it's not a frequent case, so the additional
17633 effort wouldn't really pay off. */
17634 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17635 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17636 && last_unchanged_at_beg_row > w->current_matrix->rows)
17637 --last_unchanged_at_beg_row;
17639 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17640 GIVE_UP (17);
17642 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17643 GIVE_UP (18);
17644 start_pos = it.current.pos;
17646 /* Start displaying new lines in the desired matrix at the same
17647 vpos we would use in the current matrix, i.e. below
17648 last_unchanged_at_beg_row. */
17649 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17650 current_matrix);
17651 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17652 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17654 eassert (it.hpos == 0 && it.current_x == 0);
17656 else
17658 /* There are no reusable lines at the start of the window.
17659 Start displaying in the first text line. */
17660 start_display (&it, w, start);
17661 it.vpos = it.first_vpos;
17662 start_pos = it.current.pos;
17665 /* Find the first row that is not affected by changes at the end of
17666 the buffer. Value will be null if there is no unchanged row, in
17667 which case we must redisplay to the end of the window. delta
17668 will be set to the value by which buffer positions beginning with
17669 first_unchanged_at_end_row have to be adjusted due to text
17670 changes. */
17671 first_unchanged_at_end_row
17672 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17673 IF_DEBUG (debug_delta = delta);
17674 IF_DEBUG (debug_delta_bytes = delta_bytes);
17676 /* Set stop_pos to the buffer position up to which we will have to
17677 display new lines. If first_unchanged_at_end_row != NULL, this
17678 is the buffer position of the start of the line displayed in that
17679 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17680 that we don't stop at a buffer position. */
17681 stop_pos = 0;
17682 if (first_unchanged_at_end_row)
17684 eassert (last_unchanged_at_beg_row == NULL
17685 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17687 /* If this is a continuation line, move forward to the next one
17688 that isn't. Changes in lines above affect this line.
17689 Caution: this may move first_unchanged_at_end_row to a row
17690 not displaying text. */
17691 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17692 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17693 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17694 < it.last_visible_y))
17695 ++first_unchanged_at_end_row;
17697 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17698 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17699 >= it.last_visible_y))
17700 first_unchanged_at_end_row = NULL;
17701 else
17703 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17704 + delta);
17705 first_unchanged_at_end_vpos
17706 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17707 eassert (stop_pos >= Z - END_UNCHANGED);
17710 else if (last_unchanged_at_beg_row == NULL)
17711 GIVE_UP (19);
17714 #ifdef GLYPH_DEBUG
17716 /* Either there is no unchanged row at the end, or the one we have
17717 now displays text. This is a necessary condition for the window
17718 end pos calculation at the end of this function. */
17719 eassert (first_unchanged_at_end_row == NULL
17720 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17722 debug_last_unchanged_at_beg_vpos
17723 = (last_unchanged_at_beg_row
17724 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17725 : -1);
17726 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17728 #endif /* GLYPH_DEBUG */
17731 /* Display new lines. Set last_text_row to the last new line
17732 displayed which has text on it, i.e. might end up as being the
17733 line where the window_end_vpos is. */
17734 w->cursor.vpos = -1;
17735 last_text_row = NULL;
17736 overlay_arrow_seen = 0;
17737 while (it.current_y < it.last_visible_y
17738 && !f->fonts_changed
17739 && (first_unchanged_at_end_row == NULL
17740 || IT_CHARPOS (it) < stop_pos))
17742 if (display_line (&it))
17743 last_text_row = it.glyph_row - 1;
17746 if (f->fonts_changed)
17747 return -1;
17750 /* Compute differences in buffer positions, y-positions etc. for
17751 lines reused at the bottom of the window. Compute what we can
17752 scroll. */
17753 if (first_unchanged_at_end_row
17754 /* No lines reused because we displayed everything up to the
17755 bottom of the window. */
17756 && it.current_y < it.last_visible_y)
17758 dvpos = (it.vpos
17759 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17760 current_matrix));
17761 dy = it.current_y - first_unchanged_at_end_row->y;
17762 run.current_y = first_unchanged_at_end_row->y;
17763 run.desired_y = run.current_y + dy;
17764 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17766 else
17768 delta = delta_bytes = dvpos = dy
17769 = run.current_y = run.desired_y = run.height = 0;
17770 first_unchanged_at_end_row = NULL;
17772 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17775 /* Find the cursor if not already found. We have to decide whether
17776 PT will appear on this window (it sometimes doesn't, but this is
17777 not a very frequent case.) This decision has to be made before
17778 the current matrix is altered. A value of cursor.vpos < 0 means
17779 that PT is either in one of the lines beginning at
17780 first_unchanged_at_end_row or below the window. Don't care for
17781 lines that might be displayed later at the window end; as
17782 mentioned, this is not a frequent case. */
17783 if (w->cursor.vpos < 0)
17785 /* Cursor in unchanged rows at the top? */
17786 if (PT < CHARPOS (start_pos)
17787 && last_unchanged_at_beg_row)
17789 row = row_containing_pos (w, PT,
17790 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17791 last_unchanged_at_beg_row + 1, 0);
17792 if (row)
17793 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17796 /* Start from first_unchanged_at_end_row looking for PT. */
17797 else if (first_unchanged_at_end_row)
17799 row = row_containing_pos (w, PT - delta,
17800 first_unchanged_at_end_row, NULL, 0);
17801 if (row)
17802 set_cursor_from_row (w, row, w->current_matrix, delta,
17803 delta_bytes, dy, dvpos);
17806 /* Give up if cursor was not found. */
17807 if (w->cursor.vpos < 0)
17809 clear_glyph_matrix (w->desired_matrix);
17810 return -1;
17814 /* Don't let the cursor end in the scroll margins. */
17816 int this_scroll_margin, cursor_height;
17817 int frame_line_height = default_line_pixel_height (w);
17818 int window_total_lines
17819 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17821 this_scroll_margin =
17822 max (0, min (scroll_margin, window_total_lines / 4));
17823 this_scroll_margin *= frame_line_height;
17824 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17826 if ((w->cursor.y < this_scroll_margin
17827 && CHARPOS (start) > BEGV)
17828 /* Old redisplay didn't take scroll margin into account at the bottom,
17829 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17830 || (w->cursor.y + (make_cursor_line_fully_visible_p
17831 ? cursor_height + this_scroll_margin
17832 : 1)) > it.last_visible_y)
17834 w->cursor.vpos = -1;
17835 clear_glyph_matrix (w->desired_matrix);
17836 return -1;
17840 /* Scroll the display. Do it before changing the current matrix so
17841 that xterm.c doesn't get confused about where the cursor glyph is
17842 found. */
17843 if (dy && run.height)
17845 update_begin (f);
17847 if (FRAME_WINDOW_P (f))
17849 FRAME_RIF (f)->update_window_begin_hook (w);
17850 FRAME_RIF (f)->clear_window_mouse_face (w);
17851 FRAME_RIF (f)->scroll_run_hook (w, &run);
17852 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17854 else
17856 /* Terminal frame. In this case, dvpos gives the number of
17857 lines to scroll by; dvpos < 0 means scroll up. */
17858 int from_vpos
17859 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17860 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17861 int end = (WINDOW_TOP_EDGE_LINE (w)
17862 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17863 + window_internal_height (w));
17865 #if defined (HAVE_GPM) || defined (MSDOS)
17866 x_clear_window_mouse_face (w);
17867 #endif
17868 /* Perform the operation on the screen. */
17869 if (dvpos > 0)
17871 /* Scroll last_unchanged_at_beg_row to the end of the
17872 window down dvpos lines. */
17873 set_terminal_window (f, end);
17875 /* On dumb terminals delete dvpos lines at the end
17876 before inserting dvpos empty lines. */
17877 if (!FRAME_SCROLL_REGION_OK (f))
17878 ins_del_lines (f, end - dvpos, -dvpos);
17880 /* Insert dvpos empty lines in front of
17881 last_unchanged_at_beg_row. */
17882 ins_del_lines (f, from, dvpos);
17884 else if (dvpos < 0)
17886 /* Scroll up last_unchanged_at_beg_vpos to the end of
17887 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17888 set_terminal_window (f, end);
17890 /* Delete dvpos lines in front of
17891 last_unchanged_at_beg_vpos. ins_del_lines will set
17892 the cursor to the given vpos and emit |dvpos| delete
17893 line sequences. */
17894 ins_del_lines (f, from + dvpos, dvpos);
17896 /* On a dumb terminal insert dvpos empty lines at the
17897 end. */
17898 if (!FRAME_SCROLL_REGION_OK (f))
17899 ins_del_lines (f, end + dvpos, -dvpos);
17902 set_terminal_window (f, 0);
17905 update_end (f);
17908 /* Shift reused rows of the current matrix to the right position.
17909 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17910 text. */
17911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17912 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17913 if (dvpos < 0)
17915 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17916 bottom_vpos, dvpos);
17917 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17918 bottom_vpos);
17920 else if (dvpos > 0)
17922 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17923 bottom_vpos, dvpos);
17924 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17925 first_unchanged_at_end_vpos + dvpos);
17928 /* For frame-based redisplay, make sure that current frame and window
17929 matrix are in sync with respect to glyph memory. */
17930 if (!FRAME_WINDOW_P (f))
17931 sync_frame_with_window_matrix_rows (w);
17933 /* Adjust buffer positions in reused rows. */
17934 if (delta || delta_bytes)
17935 increment_matrix_positions (current_matrix,
17936 first_unchanged_at_end_vpos + dvpos,
17937 bottom_vpos, delta, delta_bytes);
17939 /* Adjust Y positions. */
17940 if (dy)
17941 shift_glyph_matrix (w, current_matrix,
17942 first_unchanged_at_end_vpos + dvpos,
17943 bottom_vpos, dy);
17945 if (first_unchanged_at_end_row)
17947 first_unchanged_at_end_row += dvpos;
17948 if (first_unchanged_at_end_row->y >= it.last_visible_y
17949 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17950 first_unchanged_at_end_row = NULL;
17953 /* If scrolling up, there may be some lines to display at the end of
17954 the window. */
17955 last_text_row_at_end = NULL;
17956 if (dy < 0)
17958 /* Scrolling up can leave for example a partially visible line
17959 at the end of the window to be redisplayed. */
17960 /* Set last_row to the glyph row in the current matrix where the
17961 window end line is found. It has been moved up or down in
17962 the matrix by dvpos. */
17963 int last_vpos = w->window_end_vpos + dvpos;
17964 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17966 /* If last_row is the window end line, it should display text. */
17967 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17969 /* If window end line was partially visible before, begin
17970 displaying at that line. Otherwise begin displaying with the
17971 line following it. */
17972 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17974 init_to_row_start (&it, w, last_row);
17975 it.vpos = last_vpos;
17976 it.current_y = last_row->y;
17978 else
17980 init_to_row_end (&it, w, last_row);
17981 it.vpos = 1 + last_vpos;
17982 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17983 ++last_row;
17986 /* We may start in a continuation line. If so, we have to
17987 get the right continuation_lines_width and current_x. */
17988 it.continuation_lines_width = last_row->continuation_lines_width;
17989 it.hpos = it.current_x = 0;
17991 /* Display the rest of the lines at the window end. */
17992 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17993 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17995 /* Is it always sure that the display agrees with lines in
17996 the current matrix? I don't think so, so we mark rows
17997 displayed invalid in the current matrix by setting their
17998 enabled_p flag to zero. */
17999 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18000 if (display_line (&it))
18001 last_text_row_at_end = it.glyph_row - 1;
18005 /* Update window_end_pos and window_end_vpos. */
18006 if (first_unchanged_at_end_row && !last_text_row_at_end)
18008 /* Window end line if one of the preserved rows from the current
18009 matrix. Set row to the last row displaying text in current
18010 matrix starting at first_unchanged_at_end_row, after
18011 scrolling. */
18012 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18013 row = find_last_row_displaying_text (w->current_matrix, &it,
18014 first_unchanged_at_end_row);
18015 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18016 adjust_window_ends (w, row, 1);
18017 eassert (w->window_end_bytepos >= 0);
18018 IF_DEBUG (debug_method_add (w, "A"));
18020 else if (last_text_row_at_end)
18022 adjust_window_ends (w, last_text_row_at_end, 0);
18023 eassert (w->window_end_bytepos >= 0);
18024 IF_DEBUG (debug_method_add (w, "B"));
18026 else if (last_text_row)
18028 /* We have displayed either to the end of the window or at the
18029 end of the window, i.e. the last row with text is to be found
18030 in the desired matrix. */
18031 adjust_window_ends (w, last_text_row, 0);
18032 eassert (w->window_end_bytepos >= 0);
18034 else if (first_unchanged_at_end_row == NULL
18035 && last_text_row == NULL
18036 && last_text_row_at_end == NULL)
18038 /* Displayed to end of window, but no line containing text was
18039 displayed. Lines were deleted at the end of the window. */
18040 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18041 int vpos = w->window_end_vpos;
18042 struct glyph_row *current_row = current_matrix->rows + vpos;
18043 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18045 for (row = NULL;
18046 row == NULL && vpos >= first_vpos;
18047 --vpos, --current_row, --desired_row)
18049 if (desired_row->enabled_p)
18051 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18052 row = desired_row;
18054 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18055 row = current_row;
18058 eassert (row != NULL);
18059 w->window_end_vpos = vpos + 1;
18060 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18061 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18062 eassert (w->window_end_bytepos >= 0);
18063 IF_DEBUG (debug_method_add (w, "C"));
18065 else
18066 emacs_abort ();
18068 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18069 debug_end_vpos = w->window_end_vpos));
18071 /* Record that display has not been completed. */
18072 w->window_end_valid = 0;
18073 w->desired_matrix->no_scrolling_p = 1;
18074 return 3;
18076 #undef GIVE_UP
18081 /***********************************************************************
18082 More debugging support
18083 ***********************************************************************/
18085 #ifdef GLYPH_DEBUG
18087 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18088 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18089 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18092 /* Dump the contents of glyph matrix MATRIX on stderr.
18094 GLYPHS 0 means don't show glyph contents.
18095 GLYPHS 1 means show glyphs in short form
18096 GLYPHS > 1 means show glyphs in long form. */
18098 void
18099 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18101 int i;
18102 for (i = 0; i < matrix->nrows; ++i)
18103 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18107 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18108 the glyph row and area where the glyph comes from. */
18110 void
18111 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18113 if (glyph->type == CHAR_GLYPH
18114 || glyph->type == GLYPHLESS_GLYPH)
18116 fprintf (stderr,
18117 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18118 glyph - row->glyphs[TEXT_AREA],
18119 (glyph->type == CHAR_GLYPH
18120 ? 'C'
18121 : 'G'),
18122 glyph->charpos,
18123 (BUFFERP (glyph->object)
18124 ? 'B'
18125 : (STRINGP (glyph->object)
18126 ? 'S'
18127 : (INTEGERP (glyph->object)
18128 ? '0'
18129 : '-'))),
18130 glyph->pixel_width,
18131 glyph->u.ch,
18132 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18133 ? glyph->u.ch
18134 : '.'),
18135 glyph->face_id,
18136 glyph->left_box_line_p,
18137 glyph->right_box_line_p);
18139 else if (glyph->type == STRETCH_GLYPH)
18141 fprintf (stderr,
18142 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18143 glyph - row->glyphs[TEXT_AREA],
18144 'S',
18145 glyph->charpos,
18146 (BUFFERP (glyph->object)
18147 ? 'B'
18148 : (STRINGP (glyph->object)
18149 ? 'S'
18150 : (INTEGERP (glyph->object)
18151 ? '0'
18152 : '-'))),
18153 glyph->pixel_width,
18155 ' ',
18156 glyph->face_id,
18157 glyph->left_box_line_p,
18158 glyph->right_box_line_p);
18160 else if (glyph->type == IMAGE_GLYPH)
18162 fprintf (stderr,
18163 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18164 glyph - row->glyphs[TEXT_AREA],
18165 'I',
18166 glyph->charpos,
18167 (BUFFERP (glyph->object)
18168 ? 'B'
18169 : (STRINGP (glyph->object)
18170 ? 'S'
18171 : (INTEGERP (glyph->object)
18172 ? '0'
18173 : '-'))),
18174 glyph->pixel_width,
18175 glyph->u.img_id,
18176 '.',
18177 glyph->face_id,
18178 glyph->left_box_line_p,
18179 glyph->right_box_line_p);
18181 else if (glyph->type == COMPOSITE_GLYPH)
18183 fprintf (stderr,
18184 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18185 glyph - row->glyphs[TEXT_AREA],
18186 '+',
18187 glyph->charpos,
18188 (BUFFERP (glyph->object)
18189 ? 'B'
18190 : (STRINGP (glyph->object)
18191 ? 'S'
18192 : (INTEGERP (glyph->object)
18193 ? '0'
18194 : '-'))),
18195 glyph->pixel_width,
18196 glyph->u.cmp.id);
18197 if (glyph->u.cmp.automatic)
18198 fprintf (stderr,
18199 "[%d-%d]",
18200 glyph->slice.cmp.from, glyph->slice.cmp.to);
18201 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18202 glyph->face_id,
18203 glyph->left_box_line_p,
18204 glyph->right_box_line_p);
18209 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18210 GLYPHS 0 means don't show glyph contents.
18211 GLYPHS 1 means show glyphs in short form
18212 GLYPHS > 1 means show glyphs in long form. */
18214 void
18215 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18217 if (glyphs != 1)
18219 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18220 fprintf (stderr, "==============================================================================\n");
18222 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18223 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18224 vpos,
18225 MATRIX_ROW_START_CHARPOS (row),
18226 MATRIX_ROW_END_CHARPOS (row),
18227 row->used[TEXT_AREA],
18228 row->contains_overlapping_glyphs_p,
18229 row->enabled_p,
18230 row->truncated_on_left_p,
18231 row->truncated_on_right_p,
18232 row->continued_p,
18233 MATRIX_ROW_CONTINUATION_LINE_P (row),
18234 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18235 row->ends_at_zv_p,
18236 row->fill_line_p,
18237 row->ends_in_middle_of_char_p,
18238 row->starts_in_middle_of_char_p,
18239 row->mouse_face_p,
18240 row->x,
18241 row->y,
18242 row->pixel_width,
18243 row->height,
18244 row->visible_height,
18245 row->ascent,
18246 row->phys_ascent);
18247 /* The next 3 lines should align to "Start" in the header. */
18248 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18249 row->end.overlay_string_index,
18250 row->continuation_lines_width);
18251 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18252 CHARPOS (row->start.string_pos),
18253 CHARPOS (row->end.string_pos));
18254 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18255 row->end.dpvec_index);
18258 if (glyphs > 1)
18260 int area;
18262 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18264 struct glyph *glyph = row->glyphs[area];
18265 struct glyph *glyph_end = glyph + row->used[area];
18267 /* Glyph for a line end in text. */
18268 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18269 ++glyph_end;
18271 if (glyph < glyph_end)
18272 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18274 for (; glyph < glyph_end; ++glyph)
18275 dump_glyph (row, glyph, area);
18278 else if (glyphs == 1)
18280 int area;
18282 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18284 char *s = alloca (row->used[area] + 4);
18285 int i;
18287 for (i = 0; i < row->used[area]; ++i)
18289 struct glyph *glyph = row->glyphs[area] + i;
18290 if (i == row->used[area] - 1
18291 && area == TEXT_AREA
18292 && INTEGERP (glyph->object)
18293 && glyph->type == CHAR_GLYPH
18294 && glyph->u.ch == ' ')
18296 strcpy (&s[i], "[\\n]");
18297 i += 4;
18299 else if (glyph->type == CHAR_GLYPH
18300 && glyph->u.ch < 0x80
18301 && glyph->u.ch >= ' ')
18302 s[i] = glyph->u.ch;
18303 else
18304 s[i] = '.';
18307 s[i] = '\0';
18308 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18314 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18315 Sdump_glyph_matrix, 0, 1, "p",
18316 doc: /* Dump the current matrix of the selected window to stderr.
18317 Shows contents of glyph row structures. With non-nil
18318 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18319 glyphs in short form, otherwise show glyphs in long form. */)
18320 (Lisp_Object glyphs)
18322 struct window *w = XWINDOW (selected_window);
18323 struct buffer *buffer = XBUFFER (w->contents);
18325 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18326 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18327 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18328 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18329 fprintf (stderr, "=============================================\n");
18330 dump_glyph_matrix (w->current_matrix,
18331 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18332 return Qnil;
18336 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18337 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18338 (void)
18340 struct frame *f = XFRAME (selected_frame);
18341 dump_glyph_matrix (f->current_matrix, 1);
18342 return Qnil;
18346 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18347 doc: /* Dump glyph row ROW to stderr.
18348 GLYPH 0 means don't dump glyphs.
18349 GLYPH 1 means dump glyphs in short form.
18350 GLYPH > 1 or omitted means dump glyphs in long form. */)
18351 (Lisp_Object row, Lisp_Object glyphs)
18353 struct glyph_matrix *matrix;
18354 EMACS_INT vpos;
18356 CHECK_NUMBER (row);
18357 matrix = XWINDOW (selected_window)->current_matrix;
18358 vpos = XINT (row);
18359 if (vpos >= 0 && vpos < matrix->nrows)
18360 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18361 vpos,
18362 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18363 return Qnil;
18367 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18368 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18369 GLYPH 0 means don't dump glyphs.
18370 GLYPH 1 means dump glyphs in short form.
18371 GLYPH > 1 or omitted means dump glyphs in long form.
18373 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18374 do nothing. */)
18375 (Lisp_Object row, Lisp_Object glyphs)
18377 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18378 struct frame *sf = SELECTED_FRAME ();
18379 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18380 EMACS_INT vpos;
18382 CHECK_NUMBER (row);
18383 vpos = XINT (row);
18384 if (vpos >= 0 && vpos < m->nrows)
18385 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18386 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18387 #endif
18388 return Qnil;
18392 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18393 doc: /* Toggle tracing of redisplay.
18394 With ARG, turn tracing on if and only if ARG is positive. */)
18395 (Lisp_Object arg)
18397 if (NILP (arg))
18398 trace_redisplay_p = !trace_redisplay_p;
18399 else
18401 arg = Fprefix_numeric_value (arg);
18402 trace_redisplay_p = XINT (arg) > 0;
18405 return Qnil;
18409 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18410 doc: /* Like `format', but print result to stderr.
18411 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18412 (ptrdiff_t nargs, Lisp_Object *args)
18414 Lisp_Object s = Fformat (nargs, args);
18415 fprintf (stderr, "%s", SDATA (s));
18416 return Qnil;
18419 #endif /* GLYPH_DEBUG */
18423 /***********************************************************************
18424 Building Desired Matrix Rows
18425 ***********************************************************************/
18427 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18428 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18430 static struct glyph_row *
18431 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18433 struct frame *f = XFRAME (WINDOW_FRAME (w));
18434 struct buffer *buffer = XBUFFER (w->contents);
18435 struct buffer *old = current_buffer;
18436 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18437 int arrow_len = SCHARS (overlay_arrow_string);
18438 const unsigned char *arrow_end = arrow_string + arrow_len;
18439 const unsigned char *p;
18440 struct it it;
18441 bool multibyte_p;
18442 int n_glyphs_before;
18444 set_buffer_temp (buffer);
18445 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18446 it.glyph_row->used[TEXT_AREA] = 0;
18447 SET_TEXT_POS (it.position, 0, 0);
18449 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18450 p = arrow_string;
18451 while (p < arrow_end)
18453 Lisp_Object face, ilisp;
18455 /* Get the next character. */
18456 if (multibyte_p)
18457 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18458 else
18460 it.c = it.char_to_display = *p, it.len = 1;
18461 if (! ASCII_CHAR_P (it.c))
18462 it.char_to_display = BYTE8_TO_CHAR (it.c);
18464 p += it.len;
18466 /* Get its face. */
18467 ilisp = make_number (p - arrow_string);
18468 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18469 it.face_id = compute_char_face (f, it.char_to_display, face);
18471 /* Compute its width, get its glyphs. */
18472 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18473 SET_TEXT_POS (it.position, -1, -1);
18474 PRODUCE_GLYPHS (&it);
18476 /* If this character doesn't fit any more in the line, we have
18477 to remove some glyphs. */
18478 if (it.current_x > it.last_visible_x)
18480 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18481 break;
18485 set_buffer_temp (old);
18486 return it.glyph_row;
18490 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18491 glyphs to insert is determined by produce_special_glyphs. */
18493 static void
18494 insert_left_trunc_glyphs (struct it *it)
18496 struct it truncate_it;
18497 struct glyph *from, *end, *to, *toend;
18499 eassert (!FRAME_WINDOW_P (it->f)
18500 || (!it->glyph_row->reversed_p
18501 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18502 || (it->glyph_row->reversed_p
18503 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18505 /* Get the truncation glyphs. */
18506 truncate_it = *it;
18507 truncate_it.current_x = 0;
18508 truncate_it.face_id = DEFAULT_FACE_ID;
18509 truncate_it.glyph_row = &scratch_glyph_row;
18510 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18511 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18512 truncate_it.object = make_number (0);
18513 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18515 /* Overwrite glyphs from IT with truncation glyphs. */
18516 if (!it->glyph_row->reversed_p)
18518 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18520 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18521 end = from + tused;
18522 to = it->glyph_row->glyphs[TEXT_AREA];
18523 toend = to + it->glyph_row->used[TEXT_AREA];
18524 if (FRAME_WINDOW_P (it->f))
18526 /* On GUI frames, when variable-size fonts are displayed,
18527 the truncation glyphs may need more pixels than the row's
18528 glyphs they overwrite. We overwrite more glyphs to free
18529 enough screen real estate, and enlarge the stretch glyph
18530 on the right (see display_line), if there is one, to
18531 preserve the screen position of the truncation glyphs on
18532 the right. */
18533 int w = 0;
18534 struct glyph *g = to;
18535 short used;
18537 /* The first glyph could be partially visible, in which case
18538 it->glyph_row->x will be negative. But we want the left
18539 truncation glyphs to be aligned at the left margin of the
18540 window, so we override the x coordinate at which the row
18541 will begin. */
18542 it->glyph_row->x = 0;
18543 while (g < toend && w < it->truncation_pixel_width)
18545 w += g->pixel_width;
18546 ++g;
18548 if (g - to - tused > 0)
18550 memmove (to + tused, g, (toend - g) * sizeof(*g));
18551 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18553 used = it->glyph_row->used[TEXT_AREA];
18554 if (it->glyph_row->truncated_on_right_p
18555 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18556 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18557 == STRETCH_GLYPH)
18559 int extra = w - it->truncation_pixel_width;
18561 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18565 while (from < end)
18566 *to++ = *from++;
18568 /* There may be padding glyphs left over. Overwrite them too. */
18569 if (!FRAME_WINDOW_P (it->f))
18571 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18573 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18574 while (from < end)
18575 *to++ = *from++;
18579 if (to > toend)
18580 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18582 else
18584 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18586 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18587 that back to front. */
18588 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18589 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18590 toend = it->glyph_row->glyphs[TEXT_AREA];
18591 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18592 if (FRAME_WINDOW_P (it->f))
18594 int w = 0;
18595 struct glyph *g = to;
18597 while (g >= toend && w < it->truncation_pixel_width)
18599 w += g->pixel_width;
18600 --g;
18602 if (to - g - tused > 0)
18603 to = g + tused;
18604 if (it->glyph_row->truncated_on_right_p
18605 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18606 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18608 int extra = w - it->truncation_pixel_width;
18610 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18614 while (from >= end && to >= toend)
18615 *to-- = *from--;
18616 if (!FRAME_WINDOW_P (it->f))
18618 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18620 from =
18621 truncate_it.glyph_row->glyphs[TEXT_AREA]
18622 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18623 while (from >= end && to >= toend)
18624 *to-- = *from--;
18627 if (from >= end)
18629 /* Need to free some room before prepending additional
18630 glyphs. */
18631 int move_by = from - end + 1;
18632 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18633 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18635 for ( ; g >= g0; g--)
18636 g[move_by] = *g;
18637 while (from >= end)
18638 *to-- = *from--;
18639 it->glyph_row->used[TEXT_AREA] += move_by;
18644 /* Compute the hash code for ROW. */
18645 unsigned
18646 row_hash (struct glyph_row *row)
18648 int area, k;
18649 unsigned hashval = 0;
18651 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18652 for (k = 0; k < row->used[area]; ++k)
18653 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18654 + row->glyphs[area][k].u.val
18655 + row->glyphs[area][k].face_id
18656 + row->glyphs[area][k].padding_p
18657 + (row->glyphs[area][k].type << 2));
18659 return hashval;
18662 /* Compute the pixel height and width of IT->glyph_row.
18664 Most of the time, ascent and height of a display line will be equal
18665 to the max_ascent and max_height values of the display iterator
18666 structure. This is not the case if
18668 1. We hit ZV without displaying anything. In this case, max_ascent
18669 and max_height will be zero.
18671 2. We have some glyphs that don't contribute to the line height.
18672 (The glyph row flag contributes_to_line_height_p is for future
18673 pixmap extensions).
18675 The first case is easily covered by using default values because in
18676 these cases, the line height does not really matter, except that it
18677 must not be zero. */
18679 static void
18680 compute_line_metrics (struct it *it)
18682 struct glyph_row *row = it->glyph_row;
18684 if (FRAME_WINDOW_P (it->f))
18686 int i, min_y, max_y;
18688 /* The line may consist of one space only, that was added to
18689 place the cursor on it. If so, the row's height hasn't been
18690 computed yet. */
18691 if (row->height == 0)
18693 if (it->max_ascent + it->max_descent == 0)
18694 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18695 row->ascent = it->max_ascent;
18696 row->height = it->max_ascent + it->max_descent;
18697 row->phys_ascent = it->max_phys_ascent;
18698 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18699 row->extra_line_spacing = it->max_extra_line_spacing;
18702 /* Compute the width of this line. */
18703 row->pixel_width = row->x;
18704 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18705 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18707 eassert (row->pixel_width >= 0);
18708 eassert (row->ascent >= 0 && row->height > 0);
18710 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18711 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18713 /* If first line's physical ascent is larger than its logical
18714 ascent, use the physical ascent, and make the row taller.
18715 This makes accented characters fully visible. */
18716 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18717 && row->phys_ascent > row->ascent)
18719 row->height += row->phys_ascent - row->ascent;
18720 row->ascent = row->phys_ascent;
18723 /* Compute how much of the line is visible. */
18724 row->visible_height = row->height;
18726 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18727 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18729 if (row->y < min_y)
18730 row->visible_height -= min_y - row->y;
18731 if (row->y + row->height > max_y)
18732 row->visible_height -= row->y + row->height - max_y;
18734 else
18736 row->pixel_width = row->used[TEXT_AREA];
18737 if (row->continued_p)
18738 row->pixel_width -= it->continuation_pixel_width;
18739 else if (row->truncated_on_right_p)
18740 row->pixel_width -= it->truncation_pixel_width;
18741 row->ascent = row->phys_ascent = 0;
18742 row->height = row->phys_height = row->visible_height = 1;
18743 row->extra_line_spacing = 0;
18746 /* Compute a hash code for this row. */
18747 row->hash = row_hash (row);
18749 it->max_ascent = it->max_descent = 0;
18750 it->max_phys_ascent = it->max_phys_descent = 0;
18754 /* Append one space to the glyph row of iterator IT if doing a
18755 window-based redisplay. The space has the same face as
18756 IT->face_id. Value is non-zero if a space was added.
18758 This function is called to make sure that there is always one glyph
18759 at the end of a glyph row that the cursor can be set on under
18760 window-systems. (If there weren't such a glyph we would not know
18761 how wide and tall a box cursor should be displayed).
18763 At the same time this space let's a nicely handle clearing to the
18764 end of the line if the row ends in italic text. */
18766 static int
18767 append_space_for_newline (struct it *it, int default_face_p)
18769 if (FRAME_WINDOW_P (it->f))
18771 int n = it->glyph_row->used[TEXT_AREA];
18773 if (it->glyph_row->glyphs[TEXT_AREA] + n
18774 < it->glyph_row->glyphs[1 + TEXT_AREA])
18776 /* Save some values that must not be changed.
18777 Must save IT->c and IT->len because otherwise
18778 ITERATOR_AT_END_P wouldn't work anymore after
18779 append_space_for_newline has been called. */
18780 enum display_element_type saved_what = it->what;
18781 int saved_c = it->c, saved_len = it->len;
18782 int saved_char_to_display = it->char_to_display;
18783 int saved_x = it->current_x;
18784 int saved_face_id = it->face_id;
18785 int saved_box_end = it->end_of_box_run_p;
18786 struct text_pos saved_pos;
18787 Lisp_Object saved_object;
18788 struct face *face;
18790 saved_object = it->object;
18791 saved_pos = it->position;
18793 it->what = IT_CHARACTER;
18794 memset (&it->position, 0, sizeof it->position);
18795 it->object = make_number (0);
18796 it->c = it->char_to_display = ' ';
18797 it->len = 1;
18799 /* If the default face was remapped, be sure to use the
18800 remapped face for the appended newline. */
18801 if (default_face_p)
18802 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18803 else if (it->face_before_selective_p)
18804 it->face_id = it->saved_face_id;
18805 face = FACE_FROM_ID (it->f, it->face_id);
18806 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18807 /* In R2L rows, we will prepend a stretch glyph that will
18808 have the end_of_box_run_p flag set for it, so there's no
18809 need for the appended newline glyph to have that flag
18810 set. */
18811 if (it->glyph_row->reversed_p
18812 /* But if the appended newline glyph goes all the way to
18813 the end of the row, there will be no stretch glyph,
18814 so leave the box flag set. */
18815 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18816 it->end_of_box_run_p = 0;
18818 PRODUCE_GLYPHS (it);
18820 it->override_ascent = -1;
18821 it->constrain_row_ascent_descent_p = 0;
18822 it->current_x = saved_x;
18823 it->object = saved_object;
18824 it->position = saved_pos;
18825 it->what = saved_what;
18826 it->face_id = saved_face_id;
18827 it->len = saved_len;
18828 it->c = saved_c;
18829 it->char_to_display = saved_char_to_display;
18830 it->end_of_box_run_p = saved_box_end;
18831 return 1;
18835 return 0;
18839 /* Extend the face of the last glyph in the text area of IT->glyph_row
18840 to the end of the display line. Called from display_line. If the
18841 glyph row is empty, add a space glyph to it so that we know the
18842 face to draw. Set the glyph row flag fill_line_p. If the glyph
18843 row is R2L, prepend a stretch glyph to cover the empty space to the
18844 left of the leftmost glyph. */
18846 static void
18847 extend_face_to_end_of_line (struct it *it)
18849 struct face *face, *default_face;
18850 struct frame *f = it->f;
18852 /* If line is already filled, do nothing. Non window-system frames
18853 get a grace of one more ``pixel'' because their characters are
18854 1-``pixel'' wide, so they hit the equality too early. This grace
18855 is needed only for R2L rows that are not continued, to produce
18856 one extra blank where we could display the cursor. */
18857 if ((it->current_x >= it->last_visible_x
18858 + (!FRAME_WINDOW_P (f)
18859 && it->glyph_row->reversed_p
18860 && !it->glyph_row->continued_p))
18861 /* If the window has display margins, we will need to extend
18862 their face even if the text area is filled. */
18863 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18864 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18865 return;
18867 /* The default face, possibly remapped. */
18868 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18870 /* Face extension extends the background and box of IT->face_id
18871 to the end of the line. If the background equals the background
18872 of the frame, we don't have to do anything. */
18873 if (it->face_before_selective_p)
18874 face = FACE_FROM_ID (f, it->saved_face_id);
18875 else
18876 face = FACE_FROM_ID (f, it->face_id);
18878 if (FRAME_WINDOW_P (f)
18879 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18880 && face->box == FACE_NO_BOX
18881 && face->background == FRAME_BACKGROUND_PIXEL (f)
18882 #ifdef HAVE_WINDOW_SYSTEM
18883 && !face->stipple
18884 #endif
18885 && !it->glyph_row->reversed_p)
18886 return;
18888 /* Set the glyph row flag indicating that the face of the last glyph
18889 in the text area has to be drawn to the end of the text area. */
18890 it->glyph_row->fill_line_p = 1;
18892 /* If current character of IT is not ASCII, make sure we have the
18893 ASCII face. This will be automatically undone the next time
18894 get_next_display_element returns a multibyte character. Note
18895 that the character will always be single byte in unibyte
18896 text. */
18897 if (!ASCII_CHAR_P (it->c))
18899 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18902 if (FRAME_WINDOW_P (f))
18904 /* If the row is empty, add a space with the current face of IT,
18905 so that we know which face to draw. */
18906 if (it->glyph_row->used[TEXT_AREA] == 0)
18908 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18909 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18910 it->glyph_row->used[TEXT_AREA] = 1;
18912 /* Mode line and the header line don't have margins, and
18913 likewise the frame's tool-bar window, if there is any. */
18914 if (!(it->glyph_row->mode_line_p
18915 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18916 || (WINDOWP (f->tool_bar_window)
18917 && it->w == XWINDOW (f->tool_bar_window))
18918 #endif
18921 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18922 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18924 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18925 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18926 default_face->id;
18927 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18929 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18930 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18932 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18933 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18934 default_face->id;
18935 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18938 #ifdef HAVE_WINDOW_SYSTEM
18939 if (it->glyph_row->reversed_p)
18941 /* Prepend a stretch glyph to the row, such that the
18942 rightmost glyph will be drawn flushed all the way to the
18943 right margin of the window. The stretch glyph that will
18944 occupy the empty space, if any, to the left of the
18945 glyphs. */
18946 struct font *font = face->font ? face->font : FRAME_FONT (f);
18947 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18948 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18949 struct glyph *g;
18950 int row_width, stretch_ascent, stretch_width;
18951 struct text_pos saved_pos;
18952 int saved_face_id, saved_avoid_cursor, saved_box_start;
18954 for (row_width = 0, g = row_start; g < row_end; g++)
18955 row_width += g->pixel_width;
18956 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18957 if (stretch_width > 0)
18959 stretch_ascent =
18960 (((it->ascent + it->descent)
18961 * FONT_BASE (font)) / FONT_HEIGHT (font));
18962 saved_pos = it->position;
18963 memset (&it->position, 0, sizeof it->position);
18964 saved_avoid_cursor = it->avoid_cursor_p;
18965 it->avoid_cursor_p = 1;
18966 saved_face_id = it->face_id;
18967 saved_box_start = it->start_of_box_run_p;
18968 /* The last row's stretch glyph should get the default
18969 face, to avoid painting the rest of the window with
18970 the region face, if the region ends at ZV. */
18971 if (it->glyph_row->ends_at_zv_p)
18972 it->face_id = default_face->id;
18973 else
18974 it->face_id = face->id;
18975 it->start_of_box_run_p = 0;
18976 append_stretch_glyph (it, make_number (0), stretch_width,
18977 it->ascent + it->descent, stretch_ascent);
18978 it->position = saved_pos;
18979 it->avoid_cursor_p = saved_avoid_cursor;
18980 it->face_id = saved_face_id;
18981 it->start_of_box_run_p = saved_box_start;
18984 #endif /* HAVE_WINDOW_SYSTEM */
18986 else
18988 /* Save some values that must not be changed. */
18989 int saved_x = it->current_x;
18990 struct text_pos saved_pos;
18991 Lisp_Object saved_object;
18992 enum display_element_type saved_what = it->what;
18993 int saved_face_id = it->face_id;
18995 saved_object = it->object;
18996 saved_pos = it->position;
18998 it->what = IT_CHARACTER;
18999 memset (&it->position, 0, sizeof it->position);
19000 it->object = make_number (0);
19001 it->c = it->char_to_display = ' ';
19002 it->len = 1;
19004 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19005 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19006 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19007 && !it->glyph_row->mode_line_p
19008 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19010 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19011 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19013 for (it->current_x = 0; g < e; g++)
19014 it->current_x += g->pixel_width;
19016 it->area = LEFT_MARGIN_AREA;
19017 it->face_id = default_face->id;
19018 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19019 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19021 PRODUCE_GLYPHS (it);
19022 /* term.c:produce_glyphs advances it->current_x only for
19023 TEXT_AREA. */
19024 it->current_x += it->pixel_width;
19027 it->current_x = saved_x;
19028 it->area = TEXT_AREA;
19031 /* The last row's blank glyphs should get the default face, to
19032 avoid painting the rest of the window with the region face,
19033 if the region ends at ZV. */
19034 if (it->glyph_row->ends_at_zv_p)
19035 it->face_id = default_face->id;
19036 else
19037 it->face_id = face->id;
19038 PRODUCE_GLYPHS (it);
19040 while (it->current_x <= it->last_visible_x)
19041 PRODUCE_GLYPHS (it);
19043 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19044 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19045 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19046 && !it->glyph_row->mode_line_p
19047 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19049 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19050 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19052 for ( ; g < e; g++)
19053 it->current_x += g->pixel_width;
19055 it->area = RIGHT_MARGIN_AREA;
19056 it->face_id = default_face->id;
19057 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19058 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19060 PRODUCE_GLYPHS (it);
19061 it->current_x += it->pixel_width;
19064 it->area = TEXT_AREA;
19067 /* Don't count these blanks really. It would let us insert a left
19068 truncation glyph below and make us set the cursor on them, maybe. */
19069 it->current_x = saved_x;
19070 it->object = saved_object;
19071 it->position = saved_pos;
19072 it->what = saved_what;
19073 it->face_id = saved_face_id;
19078 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19079 trailing whitespace. */
19081 static int
19082 trailing_whitespace_p (ptrdiff_t charpos)
19084 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19085 int c = 0;
19087 while (bytepos < ZV_BYTE
19088 && (c = FETCH_CHAR (bytepos),
19089 c == ' ' || c == '\t'))
19090 ++bytepos;
19092 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19094 if (bytepos != PT_BYTE)
19095 return 1;
19097 return 0;
19101 /* Highlight trailing whitespace, if any, in ROW. */
19103 static void
19104 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19106 int used = row->used[TEXT_AREA];
19108 if (used)
19110 struct glyph *start = row->glyphs[TEXT_AREA];
19111 struct glyph *glyph = start + used - 1;
19113 if (row->reversed_p)
19115 /* Right-to-left rows need to be processed in the opposite
19116 direction, so swap the edge pointers. */
19117 glyph = start;
19118 start = row->glyphs[TEXT_AREA] + used - 1;
19121 /* Skip over glyphs inserted to display the cursor at the
19122 end of a line, for extending the face of the last glyph
19123 to the end of the line on terminals, and for truncation
19124 and continuation glyphs. */
19125 if (!row->reversed_p)
19127 while (glyph >= start
19128 && glyph->type == CHAR_GLYPH
19129 && INTEGERP (glyph->object))
19130 --glyph;
19132 else
19134 while (glyph <= start
19135 && glyph->type == CHAR_GLYPH
19136 && INTEGERP (glyph->object))
19137 ++glyph;
19140 /* If last glyph is a space or stretch, and it's trailing
19141 whitespace, set the face of all trailing whitespace glyphs in
19142 IT->glyph_row to `trailing-whitespace'. */
19143 if ((row->reversed_p ? glyph <= start : glyph >= start)
19144 && BUFFERP (glyph->object)
19145 && (glyph->type == STRETCH_GLYPH
19146 || (glyph->type == CHAR_GLYPH
19147 && glyph->u.ch == ' '))
19148 && trailing_whitespace_p (glyph->charpos))
19150 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19151 if (face_id < 0)
19152 return;
19154 if (!row->reversed_p)
19156 while (glyph >= start
19157 && BUFFERP (glyph->object)
19158 && (glyph->type == STRETCH_GLYPH
19159 || (glyph->type == CHAR_GLYPH
19160 && glyph->u.ch == ' ')))
19161 (glyph--)->face_id = face_id;
19163 else
19165 while (glyph <= start
19166 && BUFFERP (glyph->object)
19167 && (glyph->type == STRETCH_GLYPH
19168 || (glyph->type == CHAR_GLYPH
19169 && glyph->u.ch == ' ')))
19170 (glyph++)->face_id = face_id;
19177 /* Value is non-zero if glyph row ROW should be
19178 considered to hold the buffer position CHARPOS. */
19180 static int
19181 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19183 int result = 1;
19185 if (charpos == CHARPOS (row->end.pos)
19186 || charpos == MATRIX_ROW_END_CHARPOS (row))
19188 /* Suppose the row ends on a string.
19189 Unless the row is continued, that means it ends on a newline
19190 in the string. If it's anything other than a display string
19191 (e.g., a before-string from an overlay), we don't want the
19192 cursor there. (This heuristic seems to give the optimal
19193 behavior for the various types of multi-line strings.)
19194 One exception: if the string has `cursor' property on one of
19195 its characters, we _do_ want the cursor there. */
19196 if (CHARPOS (row->end.string_pos) >= 0)
19198 if (row->continued_p)
19199 result = 1;
19200 else
19202 /* Check for `display' property. */
19203 struct glyph *beg = row->glyphs[TEXT_AREA];
19204 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19205 struct glyph *glyph;
19207 result = 0;
19208 for (glyph = end; glyph >= beg; --glyph)
19209 if (STRINGP (glyph->object))
19211 Lisp_Object prop
19212 = Fget_char_property (make_number (charpos),
19213 Qdisplay, Qnil);
19214 result =
19215 (!NILP (prop)
19216 && display_prop_string_p (prop, glyph->object));
19217 /* If there's a `cursor' property on one of the
19218 string's characters, this row is a cursor row,
19219 even though this is not a display string. */
19220 if (!result)
19222 Lisp_Object s = glyph->object;
19224 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19226 ptrdiff_t gpos = glyph->charpos;
19228 if (!NILP (Fget_char_property (make_number (gpos),
19229 Qcursor, s)))
19231 result = 1;
19232 break;
19236 break;
19240 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19242 /* If the row ends in middle of a real character,
19243 and the line is continued, we want the cursor here.
19244 That's because CHARPOS (ROW->end.pos) would equal
19245 PT if PT is before the character. */
19246 if (!row->ends_in_ellipsis_p)
19247 result = row->continued_p;
19248 else
19249 /* If the row ends in an ellipsis, then
19250 CHARPOS (ROW->end.pos) will equal point after the
19251 invisible text. We want that position to be displayed
19252 after the ellipsis. */
19253 result = 0;
19255 /* If the row ends at ZV, display the cursor at the end of that
19256 row instead of at the start of the row below. */
19257 else if (row->ends_at_zv_p)
19258 result = 1;
19259 else
19260 result = 0;
19263 return result;
19266 /* Value is non-zero if glyph row ROW should be
19267 used to hold the cursor. */
19269 static int
19270 cursor_row_p (struct glyph_row *row)
19272 return row_for_charpos_p (row, PT);
19277 /* Push the property PROP so that it will be rendered at the current
19278 position in IT. Return 1 if PROP was successfully pushed, 0
19279 otherwise. Called from handle_line_prefix to handle the
19280 `line-prefix' and `wrap-prefix' properties. */
19282 static int
19283 push_prefix_prop (struct it *it, Lisp_Object prop)
19285 struct text_pos pos =
19286 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19288 eassert (it->method == GET_FROM_BUFFER
19289 || it->method == GET_FROM_DISPLAY_VECTOR
19290 || it->method == GET_FROM_STRING);
19292 /* We need to save the current buffer/string position, so it will be
19293 restored by pop_it, because iterate_out_of_display_property
19294 depends on that being set correctly, but some situations leave
19295 it->position not yet set when this function is called. */
19296 push_it (it, &pos);
19298 if (STRINGP (prop))
19300 if (SCHARS (prop) == 0)
19302 pop_it (it);
19303 return 0;
19306 it->string = prop;
19307 it->string_from_prefix_prop_p = 1;
19308 it->multibyte_p = STRING_MULTIBYTE (it->string);
19309 it->current.overlay_string_index = -1;
19310 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19311 it->end_charpos = it->string_nchars = SCHARS (it->string);
19312 it->method = GET_FROM_STRING;
19313 it->stop_charpos = 0;
19314 it->prev_stop = 0;
19315 it->base_level_stop = 0;
19317 /* Force paragraph direction to be that of the parent
19318 buffer/string. */
19319 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19320 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19321 else
19322 it->paragraph_embedding = L2R;
19324 /* Set up the bidi iterator for this display string. */
19325 if (it->bidi_p)
19327 it->bidi_it.string.lstring = it->string;
19328 it->bidi_it.string.s = NULL;
19329 it->bidi_it.string.schars = it->end_charpos;
19330 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19331 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19332 it->bidi_it.string.unibyte = !it->multibyte_p;
19333 it->bidi_it.w = it->w;
19334 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19337 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19339 it->method = GET_FROM_STRETCH;
19340 it->object = prop;
19342 #ifdef HAVE_WINDOW_SYSTEM
19343 else if (IMAGEP (prop))
19345 it->what = IT_IMAGE;
19346 it->image_id = lookup_image (it->f, prop);
19347 it->method = GET_FROM_IMAGE;
19349 #endif /* HAVE_WINDOW_SYSTEM */
19350 else
19352 pop_it (it); /* bogus display property, give up */
19353 return 0;
19356 return 1;
19359 /* Return the character-property PROP at the current position in IT. */
19361 static Lisp_Object
19362 get_it_property (struct it *it, Lisp_Object prop)
19364 Lisp_Object position, object = it->object;
19366 if (STRINGP (object))
19367 position = make_number (IT_STRING_CHARPOS (*it));
19368 else if (BUFFERP (object))
19370 position = make_number (IT_CHARPOS (*it));
19371 object = it->window;
19373 else
19374 return Qnil;
19376 return Fget_char_property (position, prop, object);
19379 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19381 static void
19382 handle_line_prefix (struct it *it)
19384 Lisp_Object prefix;
19386 if (it->continuation_lines_width > 0)
19388 prefix = get_it_property (it, Qwrap_prefix);
19389 if (NILP (prefix))
19390 prefix = Vwrap_prefix;
19392 else
19394 prefix = get_it_property (it, Qline_prefix);
19395 if (NILP (prefix))
19396 prefix = Vline_prefix;
19398 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19400 /* If the prefix is wider than the window, and we try to wrap
19401 it, it would acquire its own wrap prefix, and so on till the
19402 iterator stack overflows. So, don't wrap the prefix. */
19403 it->line_wrap = TRUNCATE;
19404 it->avoid_cursor_p = 1;
19410 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19411 only for R2L lines from display_line and display_string, when they
19412 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19413 the line/string needs to be continued on the next glyph row. */
19414 static void
19415 unproduce_glyphs (struct it *it, int n)
19417 struct glyph *glyph, *end;
19419 eassert (it->glyph_row);
19420 eassert (it->glyph_row->reversed_p);
19421 eassert (it->area == TEXT_AREA);
19422 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19424 if (n > it->glyph_row->used[TEXT_AREA])
19425 n = it->glyph_row->used[TEXT_AREA];
19426 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19427 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19428 for ( ; glyph < end; glyph++)
19429 glyph[-n] = *glyph;
19432 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19433 and ROW->maxpos. */
19434 static void
19435 find_row_edges (struct it *it, struct glyph_row *row,
19436 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19437 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19439 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19440 lines' rows is implemented for bidi-reordered rows. */
19442 /* ROW->minpos is the value of min_pos, the minimal buffer position
19443 we have in ROW, or ROW->start.pos if that is smaller. */
19444 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19445 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19446 else
19447 /* We didn't find buffer positions smaller than ROW->start, or
19448 didn't find _any_ valid buffer positions in any of the glyphs,
19449 so we must trust the iterator's computed positions. */
19450 row->minpos = row->start.pos;
19451 if (max_pos <= 0)
19453 max_pos = CHARPOS (it->current.pos);
19454 max_bpos = BYTEPOS (it->current.pos);
19457 /* Here are the various use-cases for ending the row, and the
19458 corresponding values for ROW->maxpos:
19460 Line ends in a newline from buffer eol_pos + 1
19461 Line is continued from buffer max_pos + 1
19462 Line is truncated on right it->current.pos
19463 Line ends in a newline from string max_pos + 1(*)
19464 (*) + 1 only when line ends in a forward scan
19465 Line is continued from string max_pos
19466 Line is continued from display vector max_pos
19467 Line is entirely from a string min_pos == max_pos
19468 Line is entirely from a display vector min_pos == max_pos
19469 Line that ends at ZV ZV
19471 If you discover other use-cases, please add them here as
19472 appropriate. */
19473 if (row->ends_at_zv_p)
19474 row->maxpos = it->current.pos;
19475 else if (row->used[TEXT_AREA])
19477 int seen_this_string = 0;
19478 struct glyph_row *r1 = row - 1;
19480 /* Did we see the same display string on the previous row? */
19481 if (STRINGP (it->object)
19482 /* this is not the first row */
19483 && row > it->w->desired_matrix->rows
19484 /* previous row is not the header line */
19485 && !r1->mode_line_p
19486 /* previous row also ends in a newline from a string */
19487 && r1->ends_in_newline_from_string_p)
19489 struct glyph *start, *end;
19491 /* Search for the last glyph of the previous row that came
19492 from buffer or string. Depending on whether the row is
19493 L2R or R2L, we need to process it front to back or the
19494 other way round. */
19495 if (!r1->reversed_p)
19497 start = r1->glyphs[TEXT_AREA];
19498 end = start + r1->used[TEXT_AREA];
19499 /* Glyphs inserted by redisplay have an integer (zero)
19500 as their object. */
19501 while (end > start
19502 && INTEGERP ((end - 1)->object)
19503 && (end - 1)->charpos <= 0)
19504 --end;
19505 if (end > start)
19507 if (EQ ((end - 1)->object, it->object))
19508 seen_this_string = 1;
19510 else
19511 /* If all the glyphs of the previous row were inserted
19512 by redisplay, it means the previous row was
19513 produced from a single newline, which is only
19514 possible if that newline came from the same string
19515 as the one which produced this ROW. */
19516 seen_this_string = 1;
19518 else
19520 end = r1->glyphs[TEXT_AREA] - 1;
19521 start = end + r1->used[TEXT_AREA];
19522 while (end < start
19523 && INTEGERP ((end + 1)->object)
19524 && (end + 1)->charpos <= 0)
19525 ++end;
19526 if (end < start)
19528 if (EQ ((end + 1)->object, it->object))
19529 seen_this_string = 1;
19531 else
19532 seen_this_string = 1;
19535 /* Take note of each display string that covers a newline only
19536 once, the first time we see it. This is for when a display
19537 string includes more than one newline in it. */
19538 if (row->ends_in_newline_from_string_p && !seen_this_string)
19540 /* If we were scanning the buffer forward when we displayed
19541 the string, we want to account for at least one buffer
19542 position that belongs to this row (position covered by
19543 the display string), so that cursor positioning will
19544 consider this row as a candidate when point is at the end
19545 of the visual line represented by this row. This is not
19546 required when scanning back, because max_pos will already
19547 have a much larger value. */
19548 if (CHARPOS (row->end.pos) > max_pos)
19549 INC_BOTH (max_pos, max_bpos);
19550 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19552 else if (CHARPOS (it->eol_pos) > 0)
19553 SET_TEXT_POS (row->maxpos,
19554 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19555 else if (row->continued_p)
19557 /* If max_pos is different from IT's current position, it
19558 means IT->method does not belong to the display element
19559 at max_pos. However, it also means that the display
19560 element at max_pos was displayed in its entirety on this
19561 line, which is equivalent to saying that the next line
19562 starts at the next buffer position. */
19563 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19564 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19565 else
19567 INC_BOTH (max_pos, max_bpos);
19568 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19571 else if (row->truncated_on_right_p)
19572 /* display_line already called reseat_at_next_visible_line_start,
19573 which puts the iterator at the beginning of the next line, in
19574 the logical order. */
19575 row->maxpos = it->current.pos;
19576 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19577 /* A line that is entirely from a string/image/stretch... */
19578 row->maxpos = row->minpos;
19579 else
19580 emacs_abort ();
19582 else
19583 row->maxpos = it->current.pos;
19586 /* Construct the glyph row IT->glyph_row in the desired matrix of
19587 IT->w from text at the current position of IT. See dispextern.h
19588 for an overview of struct it. Value is non-zero if
19589 IT->glyph_row displays text, as opposed to a line displaying ZV
19590 only. */
19592 static int
19593 display_line (struct it *it)
19595 struct glyph_row *row = it->glyph_row;
19596 Lisp_Object overlay_arrow_string;
19597 struct it wrap_it;
19598 void *wrap_data = NULL;
19599 int may_wrap = 0, wrap_x IF_LINT (= 0);
19600 int wrap_row_used = -1;
19601 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19602 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19603 int wrap_row_extra_line_spacing IF_LINT (= 0);
19604 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19605 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19606 int cvpos;
19607 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19608 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19610 /* We always start displaying at hpos zero even if hscrolled. */
19611 eassert (it->hpos == 0 && it->current_x == 0);
19613 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19614 >= it->w->desired_matrix->nrows)
19616 it->w->nrows_scale_factor++;
19617 it->f->fonts_changed = 1;
19618 return 0;
19621 /* Clear the result glyph row and enable it. */
19622 prepare_desired_row (row);
19624 row->y = it->current_y;
19625 row->start = it->start;
19626 row->continuation_lines_width = it->continuation_lines_width;
19627 row->displays_text_p = 1;
19628 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19629 it->starts_in_middle_of_char_p = 0;
19631 /* Arrange the overlays nicely for our purposes. Usually, we call
19632 display_line on only one line at a time, in which case this
19633 can't really hurt too much, or we call it on lines which appear
19634 one after another in the buffer, in which case all calls to
19635 recenter_overlay_lists but the first will be pretty cheap. */
19636 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19638 /* Move over display elements that are not visible because we are
19639 hscrolled. This may stop at an x-position < IT->first_visible_x
19640 if the first glyph is partially visible or if we hit a line end. */
19641 if (it->current_x < it->first_visible_x)
19643 enum move_it_result move_result;
19645 this_line_min_pos = row->start.pos;
19646 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19647 MOVE_TO_POS | MOVE_TO_X);
19648 /* If we are under a large hscroll, move_it_in_display_line_to
19649 could hit the end of the line without reaching
19650 it->first_visible_x. Pretend that we did reach it. This is
19651 especially important on a TTY, where we will call
19652 extend_face_to_end_of_line, which needs to know how many
19653 blank glyphs to produce. */
19654 if (it->current_x < it->first_visible_x
19655 && (move_result == MOVE_NEWLINE_OR_CR
19656 || move_result == MOVE_POS_MATCH_OR_ZV))
19657 it->current_x = it->first_visible_x;
19659 /* Record the smallest positions seen while we moved over
19660 display elements that are not visible. This is needed by
19661 redisplay_internal for optimizing the case where the cursor
19662 stays inside the same line. The rest of this function only
19663 considers positions that are actually displayed, so
19664 RECORD_MAX_MIN_POS will not otherwise record positions that
19665 are hscrolled to the left of the left edge of the window. */
19666 min_pos = CHARPOS (this_line_min_pos);
19667 min_bpos = BYTEPOS (this_line_min_pos);
19669 else
19671 /* We only do this when not calling `move_it_in_display_line_to'
19672 above, because move_it_in_display_line_to calls
19673 handle_line_prefix itself. */
19674 handle_line_prefix (it);
19677 /* Get the initial row height. This is either the height of the
19678 text hscrolled, if there is any, or zero. */
19679 row->ascent = it->max_ascent;
19680 row->height = it->max_ascent + it->max_descent;
19681 row->phys_ascent = it->max_phys_ascent;
19682 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19683 row->extra_line_spacing = it->max_extra_line_spacing;
19685 /* Utility macro to record max and min buffer positions seen until now. */
19686 #define RECORD_MAX_MIN_POS(IT) \
19687 do \
19689 int composition_p = !STRINGP ((IT)->string) \
19690 && ((IT)->what == IT_COMPOSITION); \
19691 ptrdiff_t current_pos = \
19692 composition_p ? (IT)->cmp_it.charpos \
19693 : IT_CHARPOS (*(IT)); \
19694 ptrdiff_t current_bpos = \
19695 composition_p ? CHAR_TO_BYTE (current_pos) \
19696 : IT_BYTEPOS (*(IT)); \
19697 if (current_pos < min_pos) \
19699 min_pos = current_pos; \
19700 min_bpos = current_bpos; \
19702 if (IT_CHARPOS (*it) > max_pos) \
19704 max_pos = IT_CHARPOS (*it); \
19705 max_bpos = IT_BYTEPOS (*it); \
19708 while (0)
19710 /* Loop generating characters. The loop is left with IT on the next
19711 character to display. */
19712 while (1)
19714 int n_glyphs_before, hpos_before, x_before;
19715 int x, nglyphs;
19716 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19718 /* Retrieve the next thing to display. Value is zero if end of
19719 buffer reached. */
19720 if (!get_next_display_element (it))
19722 /* Maybe add a space at the end of this line that is used to
19723 display the cursor there under X. Set the charpos of the
19724 first glyph of blank lines not corresponding to any text
19725 to -1. */
19726 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19727 row->exact_window_width_line_p = 1;
19728 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19729 || row->used[TEXT_AREA] == 0)
19731 row->glyphs[TEXT_AREA]->charpos = -1;
19732 row->displays_text_p = 0;
19734 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19735 && (!MINI_WINDOW_P (it->w)
19736 || (minibuf_level && EQ (it->window, minibuf_window))))
19737 row->indicate_empty_line_p = 1;
19740 it->continuation_lines_width = 0;
19741 row->ends_at_zv_p = 1;
19742 /* A row that displays right-to-left text must always have
19743 its last face extended all the way to the end of line,
19744 even if this row ends in ZV, because we still write to
19745 the screen left to right. We also need to extend the
19746 last face if the default face is remapped to some
19747 different face, otherwise the functions that clear
19748 portions of the screen will clear with the default face's
19749 background color. */
19750 if (row->reversed_p
19751 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19752 extend_face_to_end_of_line (it);
19753 break;
19756 /* Now, get the metrics of what we want to display. This also
19757 generates glyphs in `row' (which is IT->glyph_row). */
19758 n_glyphs_before = row->used[TEXT_AREA];
19759 x = it->current_x;
19761 /* Remember the line height so far in case the next element doesn't
19762 fit on the line. */
19763 if (it->line_wrap != TRUNCATE)
19765 ascent = it->max_ascent;
19766 descent = it->max_descent;
19767 phys_ascent = it->max_phys_ascent;
19768 phys_descent = it->max_phys_descent;
19770 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19772 if (IT_DISPLAYING_WHITESPACE (it))
19773 may_wrap = 1;
19774 else if (may_wrap)
19776 SAVE_IT (wrap_it, *it, wrap_data);
19777 wrap_x = x;
19778 wrap_row_used = row->used[TEXT_AREA];
19779 wrap_row_ascent = row->ascent;
19780 wrap_row_height = row->height;
19781 wrap_row_phys_ascent = row->phys_ascent;
19782 wrap_row_phys_height = row->phys_height;
19783 wrap_row_extra_line_spacing = row->extra_line_spacing;
19784 wrap_row_min_pos = min_pos;
19785 wrap_row_min_bpos = min_bpos;
19786 wrap_row_max_pos = max_pos;
19787 wrap_row_max_bpos = max_bpos;
19788 may_wrap = 0;
19793 PRODUCE_GLYPHS (it);
19795 /* If this display element was in marginal areas, continue with
19796 the next one. */
19797 if (it->area != TEXT_AREA)
19799 row->ascent = max (row->ascent, it->max_ascent);
19800 row->height = max (row->height, it->max_ascent + it->max_descent);
19801 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19802 row->phys_height = max (row->phys_height,
19803 it->max_phys_ascent + it->max_phys_descent);
19804 row->extra_line_spacing = max (row->extra_line_spacing,
19805 it->max_extra_line_spacing);
19806 set_iterator_to_next (it, 1);
19807 continue;
19810 /* Does the display element fit on the line? If we truncate
19811 lines, we should draw past the right edge of the window. If
19812 we don't truncate, we want to stop so that we can display the
19813 continuation glyph before the right margin. If lines are
19814 continued, there are two possible strategies for characters
19815 resulting in more than 1 glyph (e.g. tabs): Display as many
19816 glyphs as possible in this line and leave the rest for the
19817 continuation line, or display the whole element in the next
19818 line. Original redisplay did the former, so we do it also. */
19819 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19820 hpos_before = it->hpos;
19821 x_before = x;
19823 if (/* Not a newline. */
19824 nglyphs > 0
19825 /* Glyphs produced fit entirely in the line. */
19826 && it->current_x < it->last_visible_x)
19828 it->hpos += nglyphs;
19829 row->ascent = max (row->ascent, it->max_ascent);
19830 row->height = max (row->height, it->max_ascent + it->max_descent);
19831 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19832 row->phys_height = max (row->phys_height,
19833 it->max_phys_ascent + it->max_phys_descent);
19834 row->extra_line_spacing = max (row->extra_line_spacing,
19835 it->max_extra_line_spacing);
19836 if (it->current_x - it->pixel_width < it->first_visible_x)
19837 row->x = x - it->first_visible_x;
19838 /* Record the maximum and minimum buffer positions seen so
19839 far in glyphs that will be displayed by this row. */
19840 if (it->bidi_p)
19841 RECORD_MAX_MIN_POS (it);
19843 else
19845 int i, new_x;
19846 struct glyph *glyph;
19848 for (i = 0; i < nglyphs; ++i, x = new_x)
19850 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19851 new_x = x + glyph->pixel_width;
19853 if (/* Lines are continued. */
19854 it->line_wrap != TRUNCATE
19855 && (/* Glyph doesn't fit on the line. */
19856 new_x > it->last_visible_x
19857 /* Or it fits exactly on a window system frame. */
19858 || (new_x == it->last_visible_x
19859 && FRAME_WINDOW_P (it->f)
19860 && (row->reversed_p
19861 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19862 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19864 /* End of a continued line. */
19866 if (it->hpos == 0
19867 || (new_x == it->last_visible_x
19868 && FRAME_WINDOW_P (it->f)
19869 && (row->reversed_p
19870 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19871 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19873 /* Current glyph is the only one on the line or
19874 fits exactly on the line. We must continue
19875 the line because we can't draw the cursor
19876 after the glyph. */
19877 row->continued_p = 1;
19878 it->current_x = new_x;
19879 it->continuation_lines_width += new_x;
19880 ++it->hpos;
19881 if (i == nglyphs - 1)
19883 /* If line-wrap is on, check if a previous
19884 wrap point was found. */
19885 if (wrap_row_used > 0
19886 /* Even if there is a previous wrap
19887 point, continue the line here as
19888 usual, if (i) the previous character
19889 was a space or tab AND (ii) the
19890 current character is not. */
19891 && (!may_wrap
19892 || IT_DISPLAYING_WHITESPACE (it)))
19893 goto back_to_wrap;
19895 /* Record the maximum and minimum buffer
19896 positions seen so far in glyphs that will be
19897 displayed by this row. */
19898 if (it->bidi_p)
19899 RECORD_MAX_MIN_POS (it);
19900 set_iterator_to_next (it, 1);
19901 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19903 if (!get_next_display_element (it))
19905 row->exact_window_width_line_p = 1;
19906 it->continuation_lines_width = 0;
19907 row->continued_p = 0;
19908 row->ends_at_zv_p = 1;
19910 else if (ITERATOR_AT_END_OF_LINE_P (it))
19912 row->continued_p = 0;
19913 row->exact_window_width_line_p = 1;
19917 else if (it->bidi_p)
19918 RECORD_MAX_MIN_POS (it);
19919 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19920 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19921 extend_face_to_end_of_line (it);
19923 else if (CHAR_GLYPH_PADDING_P (*glyph)
19924 && !FRAME_WINDOW_P (it->f))
19926 /* A padding glyph that doesn't fit on this line.
19927 This means the whole character doesn't fit
19928 on the line. */
19929 if (row->reversed_p)
19930 unproduce_glyphs (it, row->used[TEXT_AREA]
19931 - n_glyphs_before);
19932 row->used[TEXT_AREA] = n_glyphs_before;
19934 /* Fill the rest of the row with continuation
19935 glyphs like in 20.x. */
19936 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19937 < row->glyphs[1 + TEXT_AREA])
19938 produce_special_glyphs (it, IT_CONTINUATION);
19940 row->continued_p = 1;
19941 it->current_x = x_before;
19942 it->continuation_lines_width += x_before;
19944 /* Restore the height to what it was before the
19945 element not fitting on the line. */
19946 it->max_ascent = ascent;
19947 it->max_descent = descent;
19948 it->max_phys_ascent = phys_ascent;
19949 it->max_phys_descent = phys_descent;
19950 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19951 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19952 extend_face_to_end_of_line (it);
19954 else if (wrap_row_used > 0)
19956 back_to_wrap:
19957 if (row->reversed_p)
19958 unproduce_glyphs (it,
19959 row->used[TEXT_AREA] - wrap_row_used);
19960 RESTORE_IT (it, &wrap_it, wrap_data);
19961 it->continuation_lines_width += wrap_x;
19962 row->used[TEXT_AREA] = wrap_row_used;
19963 row->ascent = wrap_row_ascent;
19964 row->height = wrap_row_height;
19965 row->phys_ascent = wrap_row_phys_ascent;
19966 row->phys_height = wrap_row_phys_height;
19967 row->extra_line_spacing = wrap_row_extra_line_spacing;
19968 min_pos = wrap_row_min_pos;
19969 min_bpos = wrap_row_min_bpos;
19970 max_pos = wrap_row_max_pos;
19971 max_bpos = wrap_row_max_bpos;
19972 row->continued_p = 1;
19973 row->ends_at_zv_p = 0;
19974 row->exact_window_width_line_p = 0;
19975 it->continuation_lines_width += x;
19977 /* Make sure that a non-default face is extended
19978 up to the right margin of the window. */
19979 extend_face_to_end_of_line (it);
19981 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19983 /* A TAB that extends past the right edge of the
19984 window. This produces a single glyph on
19985 window system frames. We leave the glyph in
19986 this row and let it fill the row, but don't
19987 consume the TAB. */
19988 if ((row->reversed_p
19989 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19990 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19991 produce_special_glyphs (it, IT_CONTINUATION);
19992 it->continuation_lines_width += it->last_visible_x;
19993 row->ends_in_middle_of_char_p = 1;
19994 row->continued_p = 1;
19995 glyph->pixel_width = it->last_visible_x - x;
19996 it->starts_in_middle_of_char_p = 1;
19997 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19998 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19999 extend_face_to_end_of_line (it);
20001 else
20003 /* Something other than a TAB that draws past
20004 the right edge of the window. Restore
20005 positions to values before the element. */
20006 if (row->reversed_p)
20007 unproduce_glyphs (it, row->used[TEXT_AREA]
20008 - (n_glyphs_before + i));
20009 row->used[TEXT_AREA] = n_glyphs_before + i;
20011 /* Display continuation glyphs. */
20012 it->current_x = x_before;
20013 it->continuation_lines_width += x;
20014 if (!FRAME_WINDOW_P (it->f)
20015 || (row->reversed_p
20016 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20017 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20018 produce_special_glyphs (it, IT_CONTINUATION);
20019 row->continued_p = 1;
20021 extend_face_to_end_of_line (it);
20023 if (nglyphs > 1 && i > 0)
20025 row->ends_in_middle_of_char_p = 1;
20026 it->starts_in_middle_of_char_p = 1;
20029 /* Restore the height to what it was before the
20030 element not fitting on the line. */
20031 it->max_ascent = ascent;
20032 it->max_descent = descent;
20033 it->max_phys_ascent = phys_ascent;
20034 it->max_phys_descent = phys_descent;
20037 break;
20039 else if (new_x > it->first_visible_x)
20041 /* Increment number of glyphs actually displayed. */
20042 ++it->hpos;
20044 /* Record the maximum and minimum buffer positions
20045 seen so far in glyphs that will be displayed by
20046 this row. */
20047 if (it->bidi_p)
20048 RECORD_MAX_MIN_POS (it);
20050 if (x < it->first_visible_x)
20051 /* Glyph is partially visible, i.e. row starts at
20052 negative X position. */
20053 row->x = x - it->first_visible_x;
20055 else
20057 /* Glyph is completely off the left margin of the
20058 window. This should not happen because of the
20059 move_it_in_display_line at the start of this
20060 function, unless the text display area of the
20061 window is empty. */
20062 eassert (it->first_visible_x <= it->last_visible_x);
20065 /* Even if this display element produced no glyphs at all,
20066 we want to record its position. */
20067 if (it->bidi_p && nglyphs == 0)
20068 RECORD_MAX_MIN_POS (it);
20070 row->ascent = max (row->ascent, it->max_ascent);
20071 row->height = max (row->height, it->max_ascent + it->max_descent);
20072 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20073 row->phys_height = max (row->phys_height,
20074 it->max_phys_ascent + it->max_phys_descent);
20075 row->extra_line_spacing = max (row->extra_line_spacing,
20076 it->max_extra_line_spacing);
20078 /* End of this display line if row is continued. */
20079 if (row->continued_p || row->ends_at_zv_p)
20080 break;
20083 at_end_of_line:
20084 /* Is this a line end? If yes, we're also done, after making
20085 sure that a non-default face is extended up to the right
20086 margin of the window. */
20087 if (ITERATOR_AT_END_OF_LINE_P (it))
20089 int used_before = row->used[TEXT_AREA];
20091 row->ends_in_newline_from_string_p = STRINGP (it->object);
20093 /* Add a space at the end of the line that is used to
20094 display the cursor there. */
20095 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20096 append_space_for_newline (it, 0);
20098 /* Extend the face to the end of the line. */
20099 extend_face_to_end_of_line (it);
20101 /* Make sure we have the position. */
20102 if (used_before == 0)
20103 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20105 /* Record the position of the newline, for use in
20106 find_row_edges. */
20107 it->eol_pos = it->current.pos;
20109 /* Consume the line end. This skips over invisible lines. */
20110 set_iterator_to_next (it, 1);
20111 it->continuation_lines_width = 0;
20112 break;
20115 /* Proceed with next display element. Note that this skips
20116 over lines invisible because of selective display. */
20117 set_iterator_to_next (it, 1);
20119 /* If we truncate lines, we are done when the last displayed
20120 glyphs reach past the right margin of the window. */
20121 if (it->line_wrap == TRUNCATE
20122 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20123 ? (it->current_x >= it->last_visible_x)
20124 : (it->current_x > it->last_visible_x)))
20126 /* Maybe add truncation glyphs. */
20127 if (!FRAME_WINDOW_P (it->f)
20128 || (row->reversed_p
20129 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20130 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20132 int i, n;
20134 if (!row->reversed_p)
20136 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20137 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20138 break;
20140 else
20142 for (i = 0; i < row->used[TEXT_AREA]; i++)
20143 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20144 break;
20145 /* Remove any padding glyphs at the front of ROW, to
20146 make room for the truncation glyphs we will be
20147 adding below. The loop below always inserts at
20148 least one truncation glyph, so also remove the
20149 last glyph added to ROW. */
20150 unproduce_glyphs (it, i + 1);
20151 /* Adjust i for the loop below. */
20152 i = row->used[TEXT_AREA] - (i + 1);
20155 it->current_x = x_before;
20156 if (!FRAME_WINDOW_P (it->f))
20158 for (n = row->used[TEXT_AREA]; i < n; ++i)
20160 row->used[TEXT_AREA] = i;
20161 produce_special_glyphs (it, IT_TRUNCATION);
20164 else
20166 row->used[TEXT_AREA] = i;
20167 produce_special_glyphs (it, IT_TRUNCATION);
20170 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20172 /* Don't truncate if we can overflow newline into fringe. */
20173 if (!get_next_display_element (it))
20175 it->continuation_lines_width = 0;
20176 row->ends_at_zv_p = 1;
20177 row->exact_window_width_line_p = 1;
20178 break;
20180 if (ITERATOR_AT_END_OF_LINE_P (it))
20182 row->exact_window_width_line_p = 1;
20183 goto at_end_of_line;
20185 it->current_x = x_before;
20188 row->truncated_on_right_p = 1;
20189 it->continuation_lines_width = 0;
20190 reseat_at_next_visible_line_start (it, 0);
20191 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20192 it->hpos = hpos_before;
20193 break;
20197 if (wrap_data)
20198 bidi_unshelve_cache (wrap_data, 1);
20200 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20201 at the left window margin. */
20202 if (it->first_visible_x
20203 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20205 if (!FRAME_WINDOW_P (it->f)
20206 || (row->reversed_p
20207 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20208 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20209 insert_left_trunc_glyphs (it);
20210 row->truncated_on_left_p = 1;
20213 /* Remember the position at which this line ends.
20215 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20216 cannot be before the call to find_row_edges below, since that is
20217 where these positions are determined. */
20218 row->end = it->current;
20219 if (!it->bidi_p)
20221 row->minpos = row->start.pos;
20222 row->maxpos = row->end.pos;
20224 else
20226 /* ROW->minpos and ROW->maxpos must be the smallest and
20227 `1 + the largest' buffer positions in ROW. But if ROW was
20228 bidi-reordered, these two positions can be anywhere in the
20229 row, so we must determine them now. */
20230 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20233 /* If the start of this line is the overlay arrow-position, then
20234 mark this glyph row as the one containing the overlay arrow.
20235 This is clearly a mess with variable size fonts. It would be
20236 better to let it be displayed like cursors under X. */
20237 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20238 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20239 !NILP (overlay_arrow_string)))
20241 /* Overlay arrow in window redisplay is a fringe bitmap. */
20242 if (STRINGP (overlay_arrow_string))
20244 struct glyph_row *arrow_row
20245 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20246 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20247 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20248 struct glyph *p = row->glyphs[TEXT_AREA];
20249 struct glyph *p2, *end;
20251 /* Copy the arrow glyphs. */
20252 while (glyph < arrow_end)
20253 *p++ = *glyph++;
20255 /* Throw away padding glyphs. */
20256 p2 = p;
20257 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20258 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20259 ++p2;
20260 if (p2 > p)
20262 while (p2 < end)
20263 *p++ = *p2++;
20264 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20267 else
20269 eassert (INTEGERP (overlay_arrow_string));
20270 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20272 overlay_arrow_seen = 1;
20275 /* Highlight trailing whitespace. */
20276 if (!NILP (Vshow_trailing_whitespace))
20277 highlight_trailing_whitespace (it->f, it->glyph_row);
20279 /* Compute pixel dimensions of this line. */
20280 compute_line_metrics (it);
20282 /* Implementation note: No changes in the glyphs of ROW or in their
20283 faces can be done past this point, because compute_line_metrics
20284 computes ROW's hash value and stores it within the glyph_row
20285 structure. */
20287 /* Record whether this row ends inside an ellipsis. */
20288 row->ends_in_ellipsis_p
20289 = (it->method == GET_FROM_DISPLAY_VECTOR
20290 && it->ellipsis_p);
20292 /* Save fringe bitmaps in this row. */
20293 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20294 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20295 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20296 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20298 it->left_user_fringe_bitmap = 0;
20299 it->left_user_fringe_face_id = 0;
20300 it->right_user_fringe_bitmap = 0;
20301 it->right_user_fringe_face_id = 0;
20303 /* Maybe set the cursor. */
20304 cvpos = it->w->cursor.vpos;
20305 if ((cvpos < 0
20306 /* In bidi-reordered rows, keep checking for proper cursor
20307 position even if one has been found already, because buffer
20308 positions in such rows change non-linearly with ROW->VPOS,
20309 when a line is continued. One exception: when we are at ZV,
20310 display cursor on the first suitable glyph row, since all
20311 the empty rows after that also have their position set to ZV. */
20312 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20313 lines' rows is implemented for bidi-reordered rows. */
20314 || (it->bidi_p
20315 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20316 && PT >= MATRIX_ROW_START_CHARPOS (row)
20317 && PT <= MATRIX_ROW_END_CHARPOS (row)
20318 && cursor_row_p (row))
20319 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20321 /* Prepare for the next line. This line starts horizontally at (X
20322 HPOS) = (0 0). Vertical positions are incremented. As a
20323 convenience for the caller, IT->glyph_row is set to the next
20324 row to be used. */
20325 it->current_x = it->hpos = 0;
20326 it->current_y += row->height;
20327 SET_TEXT_POS (it->eol_pos, 0, 0);
20328 ++it->vpos;
20329 ++it->glyph_row;
20330 /* The next row should by default use the same value of the
20331 reversed_p flag as this one. set_iterator_to_next decides when
20332 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20333 the flag accordingly. */
20334 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20335 it->glyph_row->reversed_p = row->reversed_p;
20336 it->start = row->end;
20337 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20339 #undef RECORD_MAX_MIN_POS
20342 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20343 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20344 doc: /* Return paragraph direction at point in BUFFER.
20345 Value is either `left-to-right' or `right-to-left'.
20346 If BUFFER is omitted or nil, it defaults to the current buffer.
20348 Paragraph direction determines how the text in the paragraph is displayed.
20349 In left-to-right paragraphs, text begins at the left margin of the window
20350 and the reading direction is generally left to right. In right-to-left
20351 paragraphs, text begins at the right margin and is read from right to left.
20353 See also `bidi-paragraph-direction'. */)
20354 (Lisp_Object buffer)
20356 struct buffer *buf = current_buffer;
20357 struct buffer *old = buf;
20359 if (! NILP (buffer))
20361 CHECK_BUFFER (buffer);
20362 buf = XBUFFER (buffer);
20365 if (NILP (BVAR (buf, bidi_display_reordering))
20366 || NILP (BVAR (buf, enable_multibyte_characters))
20367 /* When we are loading loadup.el, the character property tables
20368 needed for bidi iteration are not yet available. */
20369 || !NILP (Vpurify_flag))
20370 return Qleft_to_right;
20371 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20372 return BVAR (buf, bidi_paragraph_direction);
20373 else
20375 /* Determine the direction from buffer text. We could try to
20376 use current_matrix if it is up to date, but this seems fast
20377 enough as it is. */
20378 struct bidi_it itb;
20379 ptrdiff_t pos = BUF_PT (buf);
20380 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20381 int c;
20382 void *itb_data = bidi_shelve_cache ();
20384 set_buffer_temp (buf);
20385 /* bidi_paragraph_init finds the base direction of the paragraph
20386 by searching forward from paragraph start. We need the base
20387 direction of the current or _previous_ paragraph, so we need
20388 to make sure we are within that paragraph. To that end, find
20389 the previous non-empty line. */
20390 if (pos >= ZV && pos > BEGV)
20391 DEC_BOTH (pos, bytepos);
20392 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20393 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20395 while ((c = FETCH_BYTE (bytepos)) == '\n'
20396 || c == ' ' || c == '\t' || c == '\f')
20398 if (bytepos <= BEGV_BYTE)
20399 break;
20400 bytepos--;
20401 pos--;
20403 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20404 bytepos--;
20406 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20407 itb.paragraph_dir = NEUTRAL_DIR;
20408 itb.string.s = NULL;
20409 itb.string.lstring = Qnil;
20410 itb.string.bufpos = 0;
20411 itb.string.from_disp_str = 0;
20412 itb.string.unibyte = 0;
20413 /* We have no window to use here for ignoring window-specific
20414 overlays. Using NULL for window pointer will cause
20415 compute_display_string_pos to use the current buffer. */
20416 itb.w = NULL;
20417 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20418 bidi_unshelve_cache (itb_data, 0);
20419 set_buffer_temp (old);
20420 switch (itb.paragraph_dir)
20422 case L2R:
20423 return Qleft_to_right;
20424 break;
20425 case R2L:
20426 return Qright_to_left;
20427 break;
20428 default:
20429 emacs_abort ();
20434 DEFUN ("move-point-visually", Fmove_point_visually,
20435 Smove_point_visually, 1, 1, 0,
20436 doc: /* Move point in the visual order in the specified DIRECTION.
20437 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20438 left.
20440 Value is the new character position of point. */)
20441 (Lisp_Object direction)
20443 struct window *w = XWINDOW (selected_window);
20444 struct buffer *b = XBUFFER (w->contents);
20445 struct glyph_row *row;
20446 int dir;
20447 Lisp_Object paragraph_dir;
20449 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20450 (!(ROW)->continued_p \
20451 && INTEGERP ((GLYPH)->object) \
20452 && (GLYPH)->type == CHAR_GLYPH \
20453 && (GLYPH)->u.ch == ' ' \
20454 && (GLYPH)->charpos >= 0 \
20455 && !(GLYPH)->avoid_cursor_p)
20457 CHECK_NUMBER (direction);
20458 dir = XINT (direction);
20459 if (dir > 0)
20460 dir = 1;
20461 else
20462 dir = -1;
20464 /* If current matrix is up-to-date, we can use the information
20465 recorded in the glyphs, at least as long as the goal is on the
20466 screen. */
20467 if (w->window_end_valid
20468 && !windows_or_buffers_changed
20469 && b
20470 && !b->clip_changed
20471 && !b->prevent_redisplay_optimizations_p
20472 && !window_outdated (w)
20473 && w->cursor.vpos >= 0
20474 && w->cursor.vpos < w->current_matrix->nrows
20475 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20477 struct glyph *g = row->glyphs[TEXT_AREA];
20478 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20479 struct glyph *gpt = g + w->cursor.hpos;
20481 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20483 if (BUFFERP (g->object) && g->charpos != PT)
20485 SET_PT (g->charpos);
20486 w->cursor.vpos = -1;
20487 return make_number (PT);
20489 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20491 ptrdiff_t new_pos;
20493 if (BUFFERP (gpt->object))
20495 new_pos = PT;
20496 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20497 new_pos += (row->reversed_p ? -dir : dir);
20498 else
20499 new_pos -= (row->reversed_p ? -dir : dir);;
20501 else if (BUFFERP (g->object))
20502 new_pos = g->charpos;
20503 else
20504 break;
20505 SET_PT (new_pos);
20506 w->cursor.vpos = -1;
20507 return make_number (PT);
20509 else if (ROW_GLYPH_NEWLINE_P (row, g))
20511 /* Glyphs inserted at the end of a non-empty line for
20512 positioning the cursor have zero charpos, so we must
20513 deduce the value of point by other means. */
20514 if (g->charpos > 0)
20515 SET_PT (g->charpos);
20516 else if (row->ends_at_zv_p && PT != ZV)
20517 SET_PT (ZV);
20518 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20519 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20520 else
20521 break;
20522 w->cursor.vpos = -1;
20523 return make_number (PT);
20526 if (g == e || INTEGERP (g->object))
20528 if (row->truncated_on_left_p || row->truncated_on_right_p)
20529 goto simulate_display;
20530 if (!row->reversed_p)
20531 row += dir;
20532 else
20533 row -= dir;
20534 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20535 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20536 goto simulate_display;
20538 if (dir > 0)
20540 if (row->reversed_p && !row->continued_p)
20542 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20543 w->cursor.vpos = -1;
20544 return make_number (PT);
20546 g = row->glyphs[TEXT_AREA];
20547 e = g + row->used[TEXT_AREA];
20548 for ( ; g < e; g++)
20550 if (BUFFERP (g->object)
20551 /* Empty lines have only one glyph, which stands
20552 for the newline, and whose charpos is the
20553 buffer position of the newline. */
20554 || ROW_GLYPH_NEWLINE_P (row, g)
20555 /* When the buffer ends in a newline, the line at
20556 EOB also has one glyph, but its charpos is -1. */
20557 || (row->ends_at_zv_p
20558 && !row->reversed_p
20559 && INTEGERP (g->object)
20560 && g->type == CHAR_GLYPH
20561 && g->u.ch == ' '))
20563 if (g->charpos > 0)
20564 SET_PT (g->charpos);
20565 else if (!row->reversed_p
20566 && row->ends_at_zv_p
20567 && PT != ZV)
20568 SET_PT (ZV);
20569 else
20570 continue;
20571 w->cursor.vpos = -1;
20572 return make_number (PT);
20576 else
20578 if (!row->reversed_p && !row->continued_p)
20580 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20581 w->cursor.vpos = -1;
20582 return make_number (PT);
20584 e = row->glyphs[TEXT_AREA];
20585 g = e + row->used[TEXT_AREA] - 1;
20586 for ( ; g >= e; g--)
20588 if (BUFFERP (g->object)
20589 || (ROW_GLYPH_NEWLINE_P (row, g)
20590 && g->charpos > 0)
20591 /* Empty R2L lines on GUI frames have the buffer
20592 position of the newline stored in the stretch
20593 glyph. */
20594 || g->type == STRETCH_GLYPH
20595 || (row->ends_at_zv_p
20596 && row->reversed_p
20597 && INTEGERP (g->object)
20598 && g->type == CHAR_GLYPH
20599 && g->u.ch == ' '))
20601 if (g->charpos > 0)
20602 SET_PT (g->charpos);
20603 else if (row->reversed_p
20604 && row->ends_at_zv_p
20605 && PT != ZV)
20606 SET_PT (ZV);
20607 else
20608 continue;
20609 w->cursor.vpos = -1;
20610 return make_number (PT);
20617 simulate_display:
20619 /* If we wind up here, we failed to move by using the glyphs, so we
20620 need to simulate display instead. */
20622 if (b)
20623 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20624 else
20625 paragraph_dir = Qleft_to_right;
20626 if (EQ (paragraph_dir, Qright_to_left))
20627 dir = -dir;
20628 if (PT <= BEGV && dir < 0)
20629 xsignal0 (Qbeginning_of_buffer);
20630 else if (PT >= ZV && dir > 0)
20631 xsignal0 (Qend_of_buffer);
20632 else
20634 struct text_pos pt;
20635 struct it it;
20636 int pt_x, target_x, pixel_width, pt_vpos;
20637 bool at_eol_p;
20638 bool overshoot_expected = false;
20639 bool target_is_eol_p = false;
20641 /* Setup the arena. */
20642 SET_TEXT_POS (pt, PT, PT_BYTE);
20643 start_display (&it, w, pt);
20645 if (it.cmp_it.id < 0
20646 && it.method == GET_FROM_STRING
20647 && it.area == TEXT_AREA
20648 && it.string_from_display_prop_p
20649 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20650 overshoot_expected = true;
20652 /* Find the X coordinate of point. We start from the beginning
20653 of this or previous line to make sure we are before point in
20654 the logical order (since the move_it_* functions can only
20655 move forward). */
20656 reseat:
20657 reseat_at_previous_visible_line_start (&it);
20658 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20659 if (IT_CHARPOS (it) != PT)
20661 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20662 -1, -1, -1, MOVE_TO_POS);
20663 /* If we missed point because the character there is
20664 displayed out of a display vector that has more than one
20665 glyph, retry expecting overshoot. */
20666 if (it.method == GET_FROM_DISPLAY_VECTOR
20667 && it.current.dpvec_index > 0
20668 && !overshoot_expected)
20670 overshoot_expected = true;
20671 goto reseat;
20673 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20674 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20676 pt_x = it.current_x;
20677 pt_vpos = it.vpos;
20678 if (dir > 0 || overshoot_expected)
20680 struct glyph_row *row = it.glyph_row;
20682 /* When point is at beginning of line, we don't have
20683 information about the glyph there loaded into struct
20684 it. Calling get_next_display_element fixes that. */
20685 if (pt_x == 0)
20686 get_next_display_element (&it);
20687 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20688 it.glyph_row = NULL;
20689 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20690 it.glyph_row = row;
20691 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20692 it, lest it will become out of sync with it's buffer
20693 position. */
20694 it.current_x = pt_x;
20696 else
20697 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20698 pixel_width = it.pixel_width;
20699 if (overshoot_expected && at_eol_p)
20700 pixel_width = 0;
20701 else if (pixel_width <= 0)
20702 pixel_width = 1;
20704 /* If there's a display string (or something similar) at point,
20705 we are actually at the glyph to the left of point, so we need
20706 to correct the X coordinate. */
20707 if (overshoot_expected)
20709 if (it.bidi_p)
20710 pt_x += pixel_width * it.bidi_it.scan_dir;
20711 else
20712 pt_x += pixel_width;
20715 /* Compute target X coordinate, either to the left or to the
20716 right of point. On TTY frames, all characters have the same
20717 pixel width of 1, so we can use that. On GUI frames we don't
20718 have an easy way of getting at the pixel width of the
20719 character to the left of point, so we use a different method
20720 of getting to that place. */
20721 if (dir > 0)
20722 target_x = pt_x + pixel_width;
20723 else
20724 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20726 /* Target X coordinate could be one line above or below the line
20727 of point, in which case we need to adjust the target X
20728 coordinate. Also, if moving to the left, we need to begin at
20729 the left edge of the point's screen line. */
20730 if (dir < 0)
20732 if (pt_x > 0)
20734 start_display (&it, w, pt);
20735 reseat_at_previous_visible_line_start (&it);
20736 it.current_x = it.current_y = it.hpos = 0;
20737 if (pt_vpos != 0)
20738 move_it_by_lines (&it, pt_vpos);
20740 else
20742 move_it_by_lines (&it, -1);
20743 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20744 target_is_eol_p = true;
20747 else
20749 if (at_eol_p
20750 || (target_x >= it.last_visible_x
20751 && it.line_wrap != TRUNCATE))
20753 if (pt_x > 0)
20754 move_it_by_lines (&it, 0);
20755 move_it_by_lines (&it, 1);
20756 target_x = 0;
20760 /* Move to the target X coordinate. */
20761 #ifdef HAVE_WINDOW_SYSTEM
20762 /* On GUI frames, as we don't know the X coordinate of the
20763 character to the left of point, moving point to the left
20764 requires walking, one grapheme cluster at a time, until we
20765 find ourself at a place immediately to the left of the
20766 character at point. */
20767 if (FRAME_WINDOW_P (it.f) && dir < 0)
20769 struct text_pos new_pos;
20770 enum move_it_result rc = MOVE_X_REACHED;
20772 if (it.current_x == 0)
20773 get_next_display_element (&it);
20774 if (it.what == IT_COMPOSITION)
20776 new_pos.charpos = it.cmp_it.charpos;
20777 new_pos.bytepos = -1;
20779 else
20780 new_pos = it.current.pos;
20782 while (it.current_x + it.pixel_width <= target_x
20783 && rc == MOVE_X_REACHED)
20785 int new_x = it.current_x + it.pixel_width;
20787 /* For composed characters, we want the position of the
20788 first character in the grapheme cluster (usually, the
20789 composition's base character), whereas it.current
20790 might give us the position of the _last_ one, e.g. if
20791 the composition is rendered in reverse due to bidi
20792 reordering. */
20793 if (it.what == IT_COMPOSITION)
20795 new_pos.charpos = it.cmp_it.charpos;
20796 new_pos.bytepos = -1;
20798 else
20799 new_pos = it.current.pos;
20800 if (new_x == it.current_x)
20801 new_x++;
20802 rc = move_it_in_display_line_to (&it, ZV, new_x,
20803 MOVE_TO_POS | MOVE_TO_X);
20804 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20805 break;
20807 /* The previous position we saw in the loop is the one we
20808 want. */
20809 if (new_pos.bytepos == -1)
20810 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20811 it.current.pos = new_pos;
20813 else
20814 #endif
20815 if (it.current_x != target_x)
20816 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20818 /* When lines are truncated, the above loop will stop at the
20819 window edge. But we want to get to the end of line, even if
20820 it is beyond the window edge; automatic hscroll will then
20821 scroll the window to show point as appropriate. */
20822 if (target_is_eol_p && it.line_wrap == TRUNCATE
20823 && get_next_display_element (&it))
20825 struct text_pos new_pos = it.current.pos;
20827 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20829 set_iterator_to_next (&it, 0);
20830 if (it.method == GET_FROM_BUFFER)
20831 new_pos = it.current.pos;
20832 if (!get_next_display_element (&it))
20833 break;
20836 it.current.pos = new_pos;
20839 /* If we ended up in a display string that covers point, move to
20840 buffer position to the right in the visual order. */
20841 if (dir > 0)
20843 while (IT_CHARPOS (it) == PT)
20845 set_iterator_to_next (&it, 0);
20846 if (!get_next_display_element (&it))
20847 break;
20851 /* Move point to that position. */
20852 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20855 return make_number (PT);
20857 #undef ROW_GLYPH_NEWLINE_P
20861 /***********************************************************************
20862 Menu Bar
20863 ***********************************************************************/
20865 /* Redisplay the menu bar in the frame for window W.
20867 The menu bar of X frames that don't have X toolkit support is
20868 displayed in a special window W->frame->menu_bar_window.
20870 The menu bar of terminal frames is treated specially as far as
20871 glyph matrices are concerned. Menu bar lines are not part of
20872 windows, so the update is done directly on the frame matrix rows
20873 for the menu bar. */
20875 static void
20876 display_menu_bar (struct window *w)
20878 struct frame *f = XFRAME (WINDOW_FRAME (w));
20879 struct it it;
20880 Lisp_Object items;
20881 int i;
20883 /* Don't do all this for graphical frames. */
20884 #ifdef HAVE_NTGUI
20885 if (FRAME_W32_P (f))
20886 return;
20887 #endif
20888 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20889 if (FRAME_X_P (f))
20890 return;
20891 #endif
20893 #ifdef HAVE_NS
20894 if (FRAME_NS_P (f))
20895 return;
20896 #endif /* HAVE_NS */
20898 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20899 eassert (!FRAME_WINDOW_P (f));
20900 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20901 it.first_visible_x = 0;
20902 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20903 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20904 if (FRAME_WINDOW_P (f))
20906 /* Menu bar lines are displayed in the desired matrix of the
20907 dummy window menu_bar_window. */
20908 struct window *menu_w;
20909 menu_w = XWINDOW (f->menu_bar_window);
20910 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20911 MENU_FACE_ID);
20912 it.first_visible_x = 0;
20913 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20915 else
20916 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20918 /* This is a TTY frame, i.e. character hpos/vpos are used as
20919 pixel x/y. */
20920 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20921 MENU_FACE_ID);
20922 it.first_visible_x = 0;
20923 it.last_visible_x = FRAME_COLS (f);
20926 /* FIXME: This should be controlled by a user option. See the
20927 comments in redisplay_tool_bar and display_mode_line about
20928 this. */
20929 it.paragraph_embedding = L2R;
20931 /* Clear all rows of the menu bar. */
20932 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20934 struct glyph_row *row = it.glyph_row + i;
20935 clear_glyph_row (row);
20936 row->enabled_p = true;
20937 row->full_width_p = 1;
20940 /* Display all items of the menu bar. */
20941 items = FRAME_MENU_BAR_ITEMS (it.f);
20942 for (i = 0; i < ASIZE (items); i += 4)
20944 Lisp_Object string;
20946 /* Stop at nil string. */
20947 string = AREF (items, i + 1);
20948 if (NILP (string))
20949 break;
20951 /* Remember where item was displayed. */
20952 ASET (items, i + 3, make_number (it.hpos));
20954 /* Display the item, pad with one space. */
20955 if (it.current_x < it.last_visible_x)
20956 display_string (NULL, string, Qnil, 0, 0, &it,
20957 SCHARS (string) + 1, 0, 0, -1);
20960 /* Fill out the line with spaces. */
20961 if (it.current_x < it.last_visible_x)
20962 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20964 /* Compute the total height of the lines. */
20965 compute_line_metrics (&it);
20968 /* Deep copy of a glyph row, including the glyphs. */
20969 static void
20970 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20972 struct glyph *pointers[1 + LAST_AREA];
20973 int to_used = to->used[TEXT_AREA];
20975 /* Save glyph pointers of TO. */
20976 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20978 /* Do a structure assignment. */
20979 *to = *from;
20981 /* Restore original glyph pointers of TO. */
20982 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20984 /* Copy the glyphs. */
20985 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20986 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20988 /* If we filled only part of the TO row, fill the rest with
20989 space_glyph (which will display as empty space). */
20990 if (to_used > from->used[TEXT_AREA])
20991 fill_up_frame_row_with_spaces (to, to_used);
20994 /* Display one menu item on a TTY, by overwriting the glyphs in the
20995 frame F's desired glyph matrix with glyphs produced from the menu
20996 item text. Called from term.c to display TTY drop-down menus one
20997 item at a time.
20999 ITEM_TEXT is the menu item text as a C string.
21001 FACE_ID is the face ID to be used for this menu item. FACE_ID
21002 could specify one of 3 faces: a face for an enabled item, a face
21003 for a disabled item, or a face for a selected item.
21005 X and Y are coordinates of the first glyph in the frame's desired
21006 matrix to be overwritten by the menu item. Since this is a TTY, Y
21007 is the zero-based number of the glyph row and X is the zero-based
21008 glyph number in the row, starting from left, where to start
21009 displaying the item.
21011 SUBMENU non-zero means this menu item drops down a submenu, which
21012 should be indicated by displaying a proper visual cue after the
21013 item text. */
21015 void
21016 display_tty_menu_item (const char *item_text, int width, int face_id,
21017 int x, int y, int submenu)
21019 struct it it;
21020 struct frame *f = SELECTED_FRAME ();
21021 struct window *w = XWINDOW (f->selected_window);
21022 int saved_used, saved_truncated, saved_width, saved_reversed;
21023 struct glyph_row *row;
21024 size_t item_len = strlen (item_text);
21026 eassert (FRAME_TERMCAP_P (f));
21028 /* Don't write beyond the matrix's last row. This can happen for
21029 TTY screens that are not high enough to show the entire menu.
21030 (This is actually a bit of defensive programming, as
21031 tty_menu_display already limits the number of menu items to one
21032 less than the number of screen lines.) */
21033 if (y >= f->desired_matrix->nrows)
21034 return;
21036 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21037 it.first_visible_x = 0;
21038 it.last_visible_x = FRAME_COLS (f) - 1;
21039 row = it.glyph_row;
21040 /* Start with the row contents from the current matrix. */
21041 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21042 saved_width = row->full_width_p;
21043 row->full_width_p = 1;
21044 saved_reversed = row->reversed_p;
21045 row->reversed_p = 0;
21046 row->enabled_p = true;
21048 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21049 desired face. */
21050 eassert (x < f->desired_matrix->matrix_w);
21051 it.current_x = it.hpos = x;
21052 it.current_y = it.vpos = y;
21053 saved_used = row->used[TEXT_AREA];
21054 saved_truncated = row->truncated_on_right_p;
21055 row->used[TEXT_AREA] = x;
21056 it.face_id = face_id;
21057 it.line_wrap = TRUNCATE;
21059 /* FIXME: This should be controlled by a user option. See the
21060 comments in redisplay_tool_bar and display_mode_line about this.
21061 Also, if paragraph_embedding could ever be R2L, changes will be
21062 needed to avoid shifting to the right the row characters in
21063 term.c:append_glyph. */
21064 it.paragraph_embedding = L2R;
21066 /* Pad with a space on the left. */
21067 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21068 width--;
21069 /* Display the menu item, pad with spaces to WIDTH. */
21070 if (submenu)
21072 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21073 item_len, 0, FRAME_COLS (f) - 1, -1);
21074 width -= item_len;
21075 /* Indicate with " >" that there's a submenu. */
21076 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21077 FRAME_COLS (f) - 1, -1);
21079 else
21080 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21081 width, 0, FRAME_COLS (f) - 1, -1);
21083 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21084 row->truncated_on_right_p = saved_truncated;
21085 row->hash = row_hash (row);
21086 row->full_width_p = saved_width;
21087 row->reversed_p = saved_reversed;
21090 /***********************************************************************
21091 Mode Line
21092 ***********************************************************************/
21094 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21095 FORCE is non-zero, redisplay mode lines unconditionally.
21096 Otherwise, redisplay only mode lines that are garbaged. Value is
21097 the number of windows whose mode lines were redisplayed. */
21099 static int
21100 redisplay_mode_lines (Lisp_Object window, bool force)
21102 int nwindows = 0;
21104 while (!NILP (window))
21106 struct window *w = XWINDOW (window);
21108 if (WINDOWP (w->contents))
21109 nwindows += redisplay_mode_lines (w->contents, force);
21110 else if (force
21111 || FRAME_GARBAGED_P (XFRAME (w->frame))
21112 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21114 struct text_pos lpoint;
21115 struct buffer *old = current_buffer;
21117 /* Set the window's buffer for the mode line display. */
21118 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21119 set_buffer_internal_1 (XBUFFER (w->contents));
21121 /* Point refers normally to the selected window. For any
21122 other window, set up appropriate value. */
21123 if (!EQ (window, selected_window))
21125 struct text_pos pt;
21127 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21128 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21131 /* Display mode lines. */
21132 clear_glyph_matrix (w->desired_matrix);
21133 if (display_mode_lines (w))
21134 ++nwindows;
21136 /* Restore old settings. */
21137 set_buffer_internal_1 (old);
21138 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21141 window = w->next;
21144 return nwindows;
21148 /* Display the mode and/or header line of window W. Value is the
21149 sum number of mode lines and header lines displayed. */
21151 static int
21152 display_mode_lines (struct window *w)
21154 Lisp_Object old_selected_window = selected_window;
21155 Lisp_Object old_selected_frame = selected_frame;
21156 Lisp_Object new_frame = w->frame;
21157 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21158 int n = 0;
21160 selected_frame = new_frame;
21161 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21162 or window's point, then we'd need select_window_1 here as well. */
21163 XSETWINDOW (selected_window, w);
21164 XFRAME (new_frame)->selected_window = selected_window;
21166 /* These will be set while the mode line specs are processed. */
21167 line_number_displayed = 0;
21168 w->column_number_displayed = -1;
21170 if (WINDOW_WANTS_MODELINE_P (w))
21172 struct window *sel_w = XWINDOW (old_selected_window);
21174 /* Select mode line face based on the real selected window. */
21175 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21176 BVAR (current_buffer, mode_line_format));
21177 ++n;
21180 if (WINDOW_WANTS_HEADER_LINE_P (w))
21182 display_mode_line (w, HEADER_LINE_FACE_ID,
21183 BVAR (current_buffer, header_line_format));
21184 ++n;
21187 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21188 selected_frame = old_selected_frame;
21189 selected_window = old_selected_window;
21190 if (n > 0)
21191 w->must_be_updated_p = true;
21192 return n;
21196 /* Display mode or header line of window W. FACE_ID specifies which
21197 line to display; it is either MODE_LINE_FACE_ID or
21198 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21199 display. Value is the pixel height of the mode/header line
21200 displayed. */
21202 static int
21203 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21205 struct it it;
21206 struct face *face;
21207 ptrdiff_t count = SPECPDL_INDEX ();
21209 init_iterator (&it, w, -1, -1, NULL, face_id);
21210 /* Don't extend on a previously drawn mode-line.
21211 This may happen if called from pos_visible_p. */
21212 it.glyph_row->enabled_p = false;
21213 prepare_desired_row (it.glyph_row);
21215 it.glyph_row->mode_line_p = 1;
21217 /* FIXME: This should be controlled by a user option. But
21218 supporting such an option is not trivial, since the mode line is
21219 made up of many separate strings. */
21220 it.paragraph_embedding = L2R;
21222 record_unwind_protect (unwind_format_mode_line,
21223 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21225 mode_line_target = MODE_LINE_DISPLAY;
21227 /* Temporarily make frame's keyboard the current kboard so that
21228 kboard-local variables in the mode_line_format will get the right
21229 values. */
21230 push_kboard (FRAME_KBOARD (it.f));
21231 record_unwind_save_match_data ();
21232 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21233 pop_kboard ();
21235 unbind_to (count, Qnil);
21237 /* Fill up with spaces. */
21238 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21240 compute_line_metrics (&it);
21241 it.glyph_row->full_width_p = 1;
21242 it.glyph_row->continued_p = 0;
21243 it.glyph_row->truncated_on_left_p = 0;
21244 it.glyph_row->truncated_on_right_p = 0;
21246 /* Make a 3D mode-line have a shadow at its right end. */
21247 face = FACE_FROM_ID (it.f, face_id);
21248 extend_face_to_end_of_line (&it);
21249 if (face->box != FACE_NO_BOX)
21251 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21252 + it.glyph_row->used[TEXT_AREA] - 1);
21253 last->right_box_line_p = 1;
21256 return it.glyph_row->height;
21259 /* Move element ELT in LIST to the front of LIST.
21260 Return the updated list. */
21262 static Lisp_Object
21263 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21265 register Lisp_Object tail, prev;
21266 register Lisp_Object tem;
21268 tail = list;
21269 prev = Qnil;
21270 while (CONSP (tail))
21272 tem = XCAR (tail);
21274 if (EQ (elt, tem))
21276 /* Splice out the link TAIL. */
21277 if (NILP (prev))
21278 list = XCDR (tail);
21279 else
21280 Fsetcdr (prev, XCDR (tail));
21282 /* Now make it the first. */
21283 Fsetcdr (tail, list);
21284 return tail;
21286 else
21287 prev = tail;
21288 tail = XCDR (tail);
21289 QUIT;
21292 /* Not found--return unchanged LIST. */
21293 return list;
21296 /* Contribute ELT to the mode line for window IT->w. How it
21297 translates into text depends on its data type.
21299 IT describes the display environment in which we display, as usual.
21301 DEPTH is the depth in recursion. It is used to prevent
21302 infinite recursion here.
21304 FIELD_WIDTH is the number of characters the display of ELT should
21305 occupy in the mode line, and PRECISION is the maximum number of
21306 characters to display from ELT's representation. See
21307 display_string for details.
21309 Returns the hpos of the end of the text generated by ELT.
21311 PROPS is a property list to add to any string we encounter.
21313 If RISKY is nonzero, remove (disregard) any properties in any string
21314 we encounter, and ignore :eval and :propertize.
21316 The global variable `mode_line_target' determines whether the
21317 output is passed to `store_mode_line_noprop',
21318 `store_mode_line_string', or `display_string'. */
21320 static int
21321 display_mode_element (struct it *it, int depth, int field_width, int precision,
21322 Lisp_Object elt, Lisp_Object props, int risky)
21324 int n = 0, field, prec;
21325 int literal = 0;
21327 tail_recurse:
21328 if (depth > 100)
21329 elt = build_string ("*too-deep*");
21331 depth++;
21333 switch (XTYPE (elt))
21335 case Lisp_String:
21337 /* A string: output it and check for %-constructs within it. */
21338 unsigned char c;
21339 ptrdiff_t offset = 0;
21341 if (SCHARS (elt) > 0
21342 && (!NILP (props) || risky))
21344 Lisp_Object oprops, aelt;
21345 oprops = Ftext_properties_at (make_number (0), elt);
21347 /* If the starting string's properties are not what
21348 we want, translate the string. Also, if the string
21349 is risky, do that anyway. */
21351 if (NILP (Fequal (props, oprops)) || risky)
21353 /* If the starting string has properties,
21354 merge the specified ones onto the existing ones. */
21355 if (! NILP (oprops) && !risky)
21357 Lisp_Object tem;
21359 oprops = Fcopy_sequence (oprops);
21360 tem = props;
21361 while (CONSP (tem))
21363 oprops = Fplist_put (oprops, XCAR (tem),
21364 XCAR (XCDR (tem)));
21365 tem = XCDR (XCDR (tem));
21367 props = oprops;
21370 aelt = Fassoc (elt, mode_line_proptrans_alist);
21371 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21373 /* AELT is what we want. Move it to the front
21374 without consing. */
21375 elt = XCAR (aelt);
21376 mode_line_proptrans_alist
21377 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21379 else
21381 Lisp_Object tem;
21383 /* If AELT has the wrong props, it is useless.
21384 so get rid of it. */
21385 if (! NILP (aelt))
21386 mode_line_proptrans_alist
21387 = Fdelq (aelt, mode_line_proptrans_alist);
21389 elt = Fcopy_sequence (elt);
21390 Fset_text_properties (make_number (0), Flength (elt),
21391 props, elt);
21392 /* Add this item to mode_line_proptrans_alist. */
21393 mode_line_proptrans_alist
21394 = Fcons (Fcons (elt, props),
21395 mode_line_proptrans_alist);
21396 /* Truncate mode_line_proptrans_alist
21397 to at most 50 elements. */
21398 tem = Fnthcdr (make_number (50),
21399 mode_line_proptrans_alist);
21400 if (! NILP (tem))
21401 XSETCDR (tem, Qnil);
21406 offset = 0;
21408 if (literal)
21410 prec = precision - n;
21411 switch (mode_line_target)
21413 case MODE_LINE_NOPROP:
21414 case MODE_LINE_TITLE:
21415 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21416 break;
21417 case MODE_LINE_STRING:
21418 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21419 break;
21420 case MODE_LINE_DISPLAY:
21421 n += display_string (NULL, elt, Qnil, 0, 0, it,
21422 0, prec, 0, STRING_MULTIBYTE (elt));
21423 break;
21426 break;
21429 /* Handle the non-literal case. */
21431 while ((precision <= 0 || n < precision)
21432 && SREF (elt, offset) != 0
21433 && (mode_line_target != MODE_LINE_DISPLAY
21434 || it->current_x < it->last_visible_x))
21436 ptrdiff_t last_offset = offset;
21438 /* Advance to end of string or next format specifier. */
21439 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21442 if (offset - 1 != last_offset)
21444 ptrdiff_t nchars, nbytes;
21446 /* Output to end of string or up to '%'. Field width
21447 is length of string. Don't output more than
21448 PRECISION allows us. */
21449 offset--;
21451 prec = c_string_width (SDATA (elt) + last_offset,
21452 offset - last_offset, precision - n,
21453 &nchars, &nbytes);
21455 switch (mode_line_target)
21457 case MODE_LINE_NOPROP:
21458 case MODE_LINE_TITLE:
21459 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21460 break;
21461 case MODE_LINE_STRING:
21463 ptrdiff_t bytepos = last_offset;
21464 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21465 ptrdiff_t endpos = (precision <= 0
21466 ? string_byte_to_char (elt, offset)
21467 : charpos + nchars);
21469 n += store_mode_line_string (NULL,
21470 Fsubstring (elt, make_number (charpos),
21471 make_number (endpos)),
21472 0, 0, 0, Qnil);
21474 break;
21475 case MODE_LINE_DISPLAY:
21477 ptrdiff_t bytepos = last_offset;
21478 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21480 if (precision <= 0)
21481 nchars = string_byte_to_char (elt, offset) - charpos;
21482 n += display_string (NULL, elt, Qnil, 0, charpos,
21483 it, 0, nchars, 0,
21484 STRING_MULTIBYTE (elt));
21486 break;
21489 else /* c == '%' */
21491 ptrdiff_t percent_position = offset;
21493 /* Get the specified minimum width. Zero means
21494 don't pad. */
21495 field = 0;
21496 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21497 field = field * 10 + c - '0';
21499 /* Don't pad beyond the total padding allowed. */
21500 if (field_width - n > 0 && field > field_width - n)
21501 field = field_width - n;
21503 /* Note that either PRECISION <= 0 or N < PRECISION. */
21504 prec = precision - n;
21506 if (c == 'M')
21507 n += display_mode_element (it, depth, field, prec,
21508 Vglobal_mode_string, props,
21509 risky);
21510 else if (c != 0)
21512 bool multibyte;
21513 ptrdiff_t bytepos, charpos;
21514 const char *spec;
21515 Lisp_Object string;
21517 bytepos = percent_position;
21518 charpos = (STRING_MULTIBYTE (elt)
21519 ? string_byte_to_char (elt, bytepos)
21520 : bytepos);
21521 spec = decode_mode_spec (it->w, c, field, &string);
21522 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21524 switch (mode_line_target)
21526 case MODE_LINE_NOPROP:
21527 case MODE_LINE_TITLE:
21528 n += store_mode_line_noprop (spec, field, prec);
21529 break;
21530 case MODE_LINE_STRING:
21532 Lisp_Object tem = build_string (spec);
21533 props = Ftext_properties_at (make_number (charpos), elt);
21534 /* Should only keep face property in props */
21535 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21537 break;
21538 case MODE_LINE_DISPLAY:
21540 int nglyphs_before, nwritten;
21542 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21543 nwritten = display_string (spec, string, elt,
21544 charpos, 0, it,
21545 field, prec, 0,
21546 multibyte);
21548 /* Assign to the glyphs written above the
21549 string where the `%x' came from, position
21550 of the `%'. */
21551 if (nwritten > 0)
21553 struct glyph *glyph
21554 = (it->glyph_row->glyphs[TEXT_AREA]
21555 + nglyphs_before);
21556 int i;
21558 for (i = 0; i < nwritten; ++i)
21560 glyph[i].object = elt;
21561 glyph[i].charpos = charpos;
21564 n += nwritten;
21567 break;
21570 else /* c == 0 */
21571 break;
21575 break;
21577 case Lisp_Symbol:
21578 /* A symbol: process the value of the symbol recursively
21579 as if it appeared here directly. Avoid error if symbol void.
21580 Special case: if value of symbol is a string, output the string
21581 literally. */
21583 register Lisp_Object tem;
21585 /* If the variable is not marked as risky to set
21586 then its contents are risky to use. */
21587 if (NILP (Fget (elt, Qrisky_local_variable)))
21588 risky = 1;
21590 tem = Fboundp (elt);
21591 if (!NILP (tem))
21593 tem = Fsymbol_value (elt);
21594 /* If value is a string, output that string literally:
21595 don't check for % within it. */
21596 if (STRINGP (tem))
21597 literal = 1;
21599 if (!EQ (tem, elt))
21601 /* Give up right away for nil or t. */
21602 elt = tem;
21603 goto tail_recurse;
21607 break;
21609 case Lisp_Cons:
21611 register Lisp_Object car, tem;
21613 /* A cons cell: five distinct cases.
21614 If first element is :eval or :propertize, do something special.
21615 If first element is a string or a cons, process all the elements
21616 and effectively concatenate them.
21617 If first element is a negative number, truncate displaying cdr to
21618 at most that many characters. If positive, pad (with spaces)
21619 to at least that many characters.
21620 If first element is a symbol, process the cadr or caddr recursively
21621 according to whether the symbol's value is non-nil or nil. */
21622 car = XCAR (elt);
21623 if (EQ (car, QCeval))
21625 /* An element of the form (:eval FORM) means evaluate FORM
21626 and use the result as mode line elements. */
21628 if (risky)
21629 break;
21631 if (CONSP (XCDR (elt)))
21633 Lisp_Object spec;
21634 spec = safe_eval (XCAR (XCDR (elt)));
21635 n += display_mode_element (it, depth, field_width - n,
21636 precision - n, spec, props,
21637 risky);
21640 else if (EQ (car, QCpropertize))
21642 /* An element of the form (:propertize ELT PROPS...)
21643 means display ELT but applying properties PROPS. */
21645 if (risky)
21646 break;
21648 if (CONSP (XCDR (elt)))
21649 n += display_mode_element (it, depth, field_width - n,
21650 precision - n, XCAR (XCDR (elt)),
21651 XCDR (XCDR (elt)), risky);
21653 else if (SYMBOLP (car))
21655 tem = Fboundp (car);
21656 elt = XCDR (elt);
21657 if (!CONSP (elt))
21658 goto invalid;
21659 /* elt is now the cdr, and we know it is a cons cell.
21660 Use its car if CAR has a non-nil value. */
21661 if (!NILP (tem))
21663 tem = Fsymbol_value (car);
21664 if (!NILP (tem))
21666 elt = XCAR (elt);
21667 goto tail_recurse;
21670 /* Symbol's value is nil (or symbol is unbound)
21671 Get the cddr of the original list
21672 and if possible find the caddr and use that. */
21673 elt = XCDR (elt);
21674 if (NILP (elt))
21675 break;
21676 else if (!CONSP (elt))
21677 goto invalid;
21678 elt = XCAR (elt);
21679 goto tail_recurse;
21681 else if (INTEGERP (car))
21683 register int lim = XINT (car);
21684 elt = XCDR (elt);
21685 if (lim < 0)
21687 /* Negative int means reduce maximum width. */
21688 if (precision <= 0)
21689 precision = -lim;
21690 else
21691 precision = min (precision, -lim);
21693 else if (lim > 0)
21695 /* Padding specified. Don't let it be more than
21696 current maximum. */
21697 if (precision > 0)
21698 lim = min (precision, lim);
21700 /* If that's more padding than already wanted, queue it.
21701 But don't reduce padding already specified even if
21702 that is beyond the current truncation point. */
21703 field_width = max (lim, field_width);
21705 goto tail_recurse;
21707 else if (STRINGP (car) || CONSP (car))
21709 Lisp_Object halftail = elt;
21710 int len = 0;
21712 while (CONSP (elt)
21713 && (precision <= 0 || n < precision))
21715 n += display_mode_element (it, depth,
21716 /* Do padding only after the last
21717 element in the list. */
21718 (! CONSP (XCDR (elt))
21719 ? field_width - n
21720 : 0),
21721 precision - n, XCAR (elt),
21722 props, risky);
21723 elt = XCDR (elt);
21724 len++;
21725 if ((len & 1) == 0)
21726 halftail = XCDR (halftail);
21727 /* Check for cycle. */
21728 if (EQ (halftail, elt))
21729 break;
21733 break;
21735 default:
21736 invalid:
21737 elt = build_string ("*invalid*");
21738 goto tail_recurse;
21741 /* Pad to FIELD_WIDTH. */
21742 if (field_width > 0 && n < field_width)
21744 switch (mode_line_target)
21746 case MODE_LINE_NOPROP:
21747 case MODE_LINE_TITLE:
21748 n += store_mode_line_noprop ("", field_width - n, 0);
21749 break;
21750 case MODE_LINE_STRING:
21751 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21752 break;
21753 case MODE_LINE_DISPLAY:
21754 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21755 0, 0, 0);
21756 break;
21760 return n;
21763 /* Store a mode-line string element in mode_line_string_list.
21765 If STRING is non-null, display that C string. Otherwise, the Lisp
21766 string LISP_STRING is displayed.
21768 FIELD_WIDTH is the minimum number of output glyphs to produce.
21769 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21770 with spaces. FIELD_WIDTH <= 0 means don't pad.
21772 PRECISION is the maximum number of characters to output from
21773 STRING. PRECISION <= 0 means don't truncate the string.
21775 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21776 properties to the string.
21778 PROPS are the properties to add to the string.
21779 The mode_line_string_face face property is always added to the string.
21782 static int
21783 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21784 int field_width, int precision, Lisp_Object props)
21786 ptrdiff_t len;
21787 int n = 0;
21789 if (string != NULL)
21791 len = strlen (string);
21792 if (precision > 0 && len > precision)
21793 len = precision;
21794 lisp_string = make_string (string, len);
21795 if (NILP (props))
21796 props = mode_line_string_face_prop;
21797 else if (!NILP (mode_line_string_face))
21799 Lisp_Object face = Fplist_get (props, Qface);
21800 props = Fcopy_sequence (props);
21801 if (NILP (face))
21802 face = mode_line_string_face;
21803 else
21804 face = list2 (face, mode_line_string_face);
21805 props = Fplist_put (props, Qface, face);
21807 Fadd_text_properties (make_number (0), make_number (len),
21808 props, lisp_string);
21810 else
21812 len = XFASTINT (Flength (lisp_string));
21813 if (precision > 0 && len > precision)
21815 len = precision;
21816 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21817 precision = -1;
21819 if (!NILP (mode_line_string_face))
21821 Lisp_Object face;
21822 if (NILP (props))
21823 props = Ftext_properties_at (make_number (0), lisp_string);
21824 face = Fplist_get (props, Qface);
21825 if (NILP (face))
21826 face = mode_line_string_face;
21827 else
21828 face = list2 (face, mode_line_string_face);
21829 props = list2 (Qface, face);
21830 if (copy_string)
21831 lisp_string = Fcopy_sequence (lisp_string);
21833 if (!NILP (props))
21834 Fadd_text_properties (make_number (0), make_number (len),
21835 props, lisp_string);
21838 if (len > 0)
21840 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21841 n += len;
21844 if (field_width > len)
21846 field_width -= len;
21847 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21848 if (!NILP (props))
21849 Fadd_text_properties (make_number (0), make_number (field_width),
21850 props, lisp_string);
21851 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21852 n += field_width;
21855 return n;
21859 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21860 1, 4, 0,
21861 doc: /* Format a string out of a mode line format specification.
21862 First arg FORMAT specifies the mode line format (see `mode-line-format'
21863 for details) to use.
21865 By default, the format is evaluated for the currently selected window.
21867 Optional second arg FACE specifies the face property to put on all
21868 characters for which no face is specified. The value nil means the
21869 default face. The value t means whatever face the window's mode line
21870 currently uses (either `mode-line' or `mode-line-inactive',
21871 depending on whether the window is the selected window or not).
21872 An integer value means the value string has no text
21873 properties.
21875 Optional third and fourth args WINDOW and BUFFER specify the window
21876 and buffer to use as the context for the formatting (defaults
21877 are the selected window and the WINDOW's buffer). */)
21878 (Lisp_Object format, Lisp_Object face,
21879 Lisp_Object window, Lisp_Object buffer)
21881 struct it it;
21882 int len;
21883 struct window *w;
21884 struct buffer *old_buffer = NULL;
21885 int face_id;
21886 int no_props = INTEGERP (face);
21887 ptrdiff_t count = SPECPDL_INDEX ();
21888 Lisp_Object str;
21889 int string_start = 0;
21891 w = decode_any_window (window);
21892 XSETWINDOW (window, w);
21894 if (NILP (buffer))
21895 buffer = w->contents;
21896 CHECK_BUFFER (buffer);
21898 /* Make formatting the modeline a non-op when noninteractive, otherwise
21899 there will be problems later caused by a partially initialized frame. */
21900 if (NILP (format) || noninteractive)
21901 return empty_unibyte_string;
21903 if (no_props)
21904 face = Qnil;
21906 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21907 : EQ (face, Qt) ? (EQ (window, selected_window)
21908 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21909 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21910 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21911 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21912 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21913 : DEFAULT_FACE_ID;
21915 old_buffer = current_buffer;
21917 /* Save things including mode_line_proptrans_alist,
21918 and set that to nil so that we don't alter the outer value. */
21919 record_unwind_protect (unwind_format_mode_line,
21920 format_mode_line_unwind_data
21921 (XFRAME (WINDOW_FRAME (w)),
21922 old_buffer, selected_window, 1));
21923 mode_line_proptrans_alist = Qnil;
21925 Fselect_window (window, Qt);
21926 set_buffer_internal_1 (XBUFFER (buffer));
21928 init_iterator (&it, w, -1, -1, NULL, face_id);
21930 if (no_props)
21932 mode_line_target = MODE_LINE_NOPROP;
21933 mode_line_string_face_prop = Qnil;
21934 mode_line_string_list = Qnil;
21935 string_start = MODE_LINE_NOPROP_LEN (0);
21937 else
21939 mode_line_target = MODE_LINE_STRING;
21940 mode_line_string_list = Qnil;
21941 mode_line_string_face = face;
21942 mode_line_string_face_prop
21943 = NILP (face) ? Qnil : list2 (Qface, face);
21946 push_kboard (FRAME_KBOARD (it.f));
21947 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21948 pop_kboard ();
21950 if (no_props)
21952 len = MODE_LINE_NOPROP_LEN (string_start);
21953 str = make_string (mode_line_noprop_buf + string_start, len);
21955 else
21957 mode_line_string_list = Fnreverse (mode_line_string_list);
21958 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21959 empty_unibyte_string);
21962 unbind_to (count, Qnil);
21963 return str;
21966 /* Write a null-terminated, right justified decimal representation of
21967 the positive integer D to BUF using a minimal field width WIDTH. */
21969 static void
21970 pint2str (register char *buf, register int width, register ptrdiff_t d)
21972 register char *p = buf;
21974 if (d <= 0)
21975 *p++ = '0';
21976 else
21978 while (d > 0)
21980 *p++ = d % 10 + '0';
21981 d /= 10;
21985 for (width -= (int) (p - buf); width > 0; --width)
21986 *p++ = ' ';
21987 *p-- = '\0';
21988 while (p > buf)
21990 d = *buf;
21991 *buf++ = *p;
21992 *p-- = d;
21996 /* Write a null-terminated, right justified decimal and "human
21997 readable" representation of the nonnegative integer D to BUF using
21998 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22000 static const char power_letter[] =
22002 0, /* no letter */
22003 'k', /* kilo */
22004 'M', /* mega */
22005 'G', /* giga */
22006 'T', /* tera */
22007 'P', /* peta */
22008 'E', /* exa */
22009 'Z', /* zetta */
22010 'Y' /* yotta */
22013 static void
22014 pint2hrstr (char *buf, int width, ptrdiff_t d)
22016 /* We aim to represent the nonnegative integer D as
22017 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22018 ptrdiff_t quotient = d;
22019 int remainder = 0;
22020 /* -1 means: do not use TENTHS. */
22021 int tenths = -1;
22022 int exponent = 0;
22024 /* Length of QUOTIENT.TENTHS as a string. */
22025 int length;
22027 char * psuffix;
22028 char * p;
22030 if (quotient >= 1000)
22032 /* Scale to the appropriate EXPONENT. */
22035 remainder = quotient % 1000;
22036 quotient /= 1000;
22037 exponent++;
22039 while (quotient >= 1000);
22041 /* Round to nearest and decide whether to use TENTHS or not. */
22042 if (quotient <= 9)
22044 tenths = remainder / 100;
22045 if (remainder % 100 >= 50)
22047 if (tenths < 9)
22048 tenths++;
22049 else
22051 quotient++;
22052 if (quotient == 10)
22053 tenths = -1;
22054 else
22055 tenths = 0;
22059 else
22060 if (remainder >= 500)
22062 if (quotient < 999)
22063 quotient++;
22064 else
22066 quotient = 1;
22067 exponent++;
22068 tenths = 0;
22073 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22074 if (tenths == -1 && quotient <= 99)
22075 if (quotient <= 9)
22076 length = 1;
22077 else
22078 length = 2;
22079 else
22080 length = 3;
22081 p = psuffix = buf + max (width, length);
22083 /* Print EXPONENT. */
22084 *psuffix++ = power_letter[exponent];
22085 *psuffix = '\0';
22087 /* Print TENTHS. */
22088 if (tenths >= 0)
22090 *--p = '0' + tenths;
22091 *--p = '.';
22094 /* Print QUOTIENT. */
22097 int digit = quotient % 10;
22098 *--p = '0' + digit;
22100 while ((quotient /= 10) != 0);
22102 /* Print leading spaces. */
22103 while (buf < p)
22104 *--p = ' ';
22107 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22108 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22109 type of CODING_SYSTEM. Return updated pointer into BUF. */
22111 static unsigned char invalid_eol_type[] = "(*invalid*)";
22113 static char *
22114 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22116 Lisp_Object val;
22117 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22118 const unsigned char *eol_str;
22119 int eol_str_len;
22120 /* The EOL conversion we are using. */
22121 Lisp_Object eoltype;
22123 val = CODING_SYSTEM_SPEC (coding_system);
22124 eoltype = Qnil;
22126 if (!VECTORP (val)) /* Not yet decided. */
22128 *buf++ = multibyte ? '-' : ' ';
22129 if (eol_flag)
22130 eoltype = eol_mnemonic_undecided;
22131 /* Don't mention EOL conversion if it isn't decided. */
22133 else
22135 Lisp_Object attrs;
22136 Lisp_Object eolvalue;
22138 attrs = AREF (val, 0);
22139 eolvalue = AREF (val, 2);
22141 *buf++ = multibyte
22142 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22143 : ' ';
22145 if (eol_flag)
22147 /* The EOL conversion that is normal on this system. */
22149 if (NILP (eolvalue)) /* Not yet decided. */
22150 eoltype = eol_mnemonic_undecided;
22151 else if (VECTORP (eolvalue)) /* Not yet decided. */
22152 eoltype = eol_mnemonic_undecided;
22153 else /* eolvalue is Qunix, Qdos, or Qmac. */
22154 eoltype = (EQ (eolvalue, Qunix)
22155 ? eol_mnemonic_unix
22156 : (EQ (eolvalue, Qdos) == 1
22157 ? eol_mnemonic_dos : eol_mnemonic_mac));
22161 if (eol_flag)
22163 /* Mention the EOL conversion if it is not the usual one. */
22164 if (STRINGP (eoltype))
22166 eol_str = SDATA (eoltype);
22167 eol_str_len = SBYTES (eoltype);
22169 else if (CHARACTERP (eoltype))
22171 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22172 int c = XFASTINT (eoltype);
22173 eol_str_len = CHAR_STRING (c, tmp);
22174 eol_str = tmp;
22176 else
22178 eol_str = invalid_eol_type;
22179 eol_str_len = sizeof (invalid_eol_type) - 1;
22181 memcpy (buf, eol_str, eol_str_len);
22182 buf += eol_str_len;
22185 return buf;
22188 /* Return a string for the output of a mode line %-spec for window W,
22189 generated by character C. FIELD_WIDTH > 0 means pad the string
22190 returned with spaces to that value. Return a Lisp string in
22191 *STRING if the resulting string is taken from that Lisp string.
22193 Note we operate on the current buffer for most purposes. */
22195 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22197 static const char *
22198 decode_mode_spec (struct window *w, register int c, int field_width,
22199 Lisp_Object *string)
22201 Lisp_Object obj;
22202 struct frame *f = XFRAME (WINDOW_FRAME (w));
22203 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22204 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22205 produce strings from numerical values, so limit preposterously
22206 large values of FIELD_WIDTH to avoid overrunning the buffer's
22207 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22208 bytes plus the terminating null. */
22209 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22210 struct buffer *b = current_buffer;
22212 obj = Qnil;
22213 *string = Qnil;
22215 switch (c)
22217 case '*':
22218 if (!NILP (BVAR (b, read_only)))
22219 return "%";
22220 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22221 return "*";
22222 return "-";
22224 case '+':
22225 /* This differs from %* only for a modified read-only buffer. */
22226 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22227 return "*";
22228 if (!NILP (BVAR (b, read_only)))
22229 return "%";
22230 return "-";
22232 case '&':
22233 /* This differs from %* in ignoring read-only-ness. */
22234 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22235 return "*";
22236 return "-";
22238 case '%':
22239 return "%";
22241 case '[':
22243 int i;
22244 char *p;
22246 if (command_loop_level > 5)
22247 return "[[[... ";
22248 p = decode_mode_spec_buf;
22249 for (i = 0; i < command_loop_level; i++)
22250 *p++ = '[';
22251 *p = 0;
22252 return decode_mode_spec_buf;
22255 case ']':
22257 int i;
22258 char *p;
22260 if (command_loop_level > 5)
22261 return " ...]]]";
22262 p = decode_mode_spec_buf;
22263 for (i = 0; i < command_loop_level; i++)
22264 *p++ = ']';
22265 *p = 0;
22266 return decode_mode_spec_buf;
22269 case '-':
22271 register int i;
22273 /* Let lots_of_dashes be a string of infinite length. */
22274 if (mode_line_target == MODE_LINE_NOPROP
22275 || mode_line_target == MODE_LINE_STRING)
22276 return "--";
22277 if (field_width <= 0
22278 || field_width > sizeof (lots_of_dashes))
22280 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22281 decode_mode_spec_buf[i] = '-';
22282 decode_mode_spec_buf[i] = '\0';
22283 return decode_mode_spec_buf;
22285 else
22286 return lots_of_dashes;
22289 case 'b':
22290 obj = BVAR (b, name);
22291 break;
22293 case 'c':
22294 /* %c and %l are ignored in `frame-title-format'.
22295 (In redisplay_internal, the frame title is drawn _before_ the
22296 windows are updated, so the stuff which depends on actual
22297 window contents (such as %l) may fail to render properly, or
22298 even crash emacs.) */
22299 if (mode_line_target == MODE_LINE_TITLE)
22300 return "";
22301 else
22303 ptrdiff_t col = current_column ();
22304 w->column_number_displayed = col;
22305 pint2str (decode_mode_spec_buf, width, col);
22306 return decode_mode_spec_buf;
22309 case 'e':
22310 #ifndef SYSTEM_MALLOC
22312 if (NILP (Vmemory_full))
22313 return "";
22314 else
22315 return "!MEM FULL! ";
22317 #else
22318 return "";
22319 #endif
22321 case 'F':
22322 /* %F displays the frame name. */
22323 if (!NILP (f->title))
22324 return SSDATA (f->title);
22325 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22326 return SSDATA (f->name);
22327 return "Emacs";
22329 case 'f':
22330 obj = BVAR (b, filename);
22331 break;
22333 case 'i':
22335 ptrdiff_t size = ZV - BEGV;
22336 pint2str (decode_mode_spec_buf, width, size);
22337 return decode_mode_spec_buf;
22340 case 'I':
22342 ptrdiff_t size = ZV - BEGV;
22343 pint2hrstr (decode_mode_spec_buf, width, size);
22344 return decode_mode_spec_buf;
22347 case 'l':
22349 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22350 ptrdiff_t topline, nlines, height;
22351 ptrdiff_t junk;
22353 /* %c and %l are ignored in `frame-title-format'. */
22354 if (mode_line_target == MODE_LINE_TITLE)
22355 return "";
22357 startpos = marker_position (w->start);
22358 startpos_byte = marker_byte_position (w->start);
22359 height = WINDOW_TOTAL_LINES (w);
22361 /* If we decided that this buffer isn't suitable for line numbers,
22362 don't forget that too fast. */
22363 if (w->base_line_pos == -1)
22364 goto no_value;
22366 /* If the buffer is very big, don't waste time. */
22367 if (INTEGERP (Vline_number_display_limit)
22368 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22370 w->base_line_pos = 0;
22371 w->base_line_number = 0;
22372 goto no_value;
22375 if (w->base_line_number > 0
22376 && w->base_line_pos > 0
22377 && w->base_line_pos <= startpos)
22379 line = w->base_line_number;
22380 linepos = w->base_line_pos;
22381 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22383 else
22385 line = 1;
22386 linepos = BUF_BEGV (b);
22387 linepos_byte = BUF_BEGV_BYTE (b);
22390 /* Count lines from base line to window start position. */
22391 nlines = display_count_lines (linepos_byte,
22392 startpos_byte,
22393 startpos, &junk);
22395 topline = nlines + line;
22397 /* Determine a new base line, if the old one is too close
22398 or too far away, or if we did not have one.
22399 "Too close" means it's plausible a scroll-down would
22400 go back past it. */
22401 if (startpos == BUF_BEGV (b))
22403 w->base_line_number = topline;
22404 w->base_line_pos = BUF_BEGV (b);
22406 else if (nlines < height + 25 || nlines > height * 3 + 50
22407 || linepos == BUF_BEGV (b))
22409 ptrdiff_t limit = BUF_BEGV (b);
22410 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22411 ptrdiff_t position;
22412 ptrdiff_t distance =
22413 (height * 2 + 30) * line_number_display_limit_width;
22415 if (startpos - distance > limit)
22417 limit = startpos - distance;
22418 limit_byte = CHAR_TO_BYTE (limit);
22421 nlines = display_count_lines (startpos_byte,
22422 limit_byte,
22423 - (height * 2 + 30),
22424 &position);
22425 /* If we couldn't find the lines we wanted within
22426 line_number_display_limit_width chars per line,
22427 give up on line numbers for this window. */
22428 if (position == limit_byte && limit == startpos - distance)
22430 w->base_line_pos = -1;
22431 w->base_line_number = 0;
22432 goto no_value;
22435 w->base_line_number = topline - nlines;
22436 w->base_line_pos = BYTE_TO_CHAR (position);
22439 /* Now count lines from the start pos to point. */
22440 nlines = display_count_lines (startpos_byte,
22441 PT_BYTE, PT, &junk);
22443 /* Record that we did display the line number. */
22444 line_number_displayed = 1;
22446 /* Make the string to show. */
22447 pint2str (decode_mode_spec_buf, width, topline + nlines);
22448 return decode_mode_spec_buf;
22449 no_value:
22451 char* p = decode_mode_spec_buf;
22452 int pad = width - 2;
22453 while (pad-- > 0)
22454 *p++ = ' ';
22455 *p++ = '?';
22456 *p++ = '?';
22457 *p = '\0';
22458 return decode_mode_spec_buf;
22461 break;
22463 case 'm':
22464 obj = BVAR (b, mode_name);
22465 break;
22467 case 'n':
22468 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22469 return " Narrow";
22470 break;
22472 case 'p':
22474 ptrdiff_t pos = marker_position (w->start);
22475 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22477 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22479 if (pos <= BUF_BEGV (b))
22480 return "All";
22481 else
22482 return "Bottom";
22484 else if (pos <= BUF_BEGV (b))
22485 return "Top";
22486 else
22488 if (total > 1000000)
22489 /* Do it differently for a large value, to avoid overflow. */
22490 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22491 else
22492 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22493 /* We can't normally display a 3-digit number,
22494 so get us a 2-digit number that is close. */
22495 if (total == 100)
22496 total = 99;
22497 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22498 return decode_mode_spec_buf;
22502 /* Display percentage of size above the bottom of the screen. */
22503 case 'P':
22505 ptrdiff_t toppos = marker_position (w->start);
22506 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22507 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22509 if (botpos >= BUF_ZV (b))
22511 if (toppos <= BUF_BEGV (b))
22512 return "All";
22513 else
22514 return "Bottom";
22516 else
22518 if (total > 1000000)
22519 /* Do it differently for a large value, to avoid overflow. */
22520 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22521 else
22522 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22523 /* We can't normally display a 3-digit number,
22524 so get us a 2-digit number that is close. */
22525 if (total == 100)
22526 total = 99;
22527 if (toppos <= BUF_BEGV (b))
22528 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22529 else
22530 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22531 return decode_mode_spec_buf;
22535 case 's':
22536 /* status of process */
22537 obj = Fget_buffer_process (Fcurrent_buffer ());
22538 if (NILP (obj))
22539 return "no process";
22540 #ifndef MSDOS
22541 obj = Fsymbol_name (Fprocess_status (obj));
22542 #endif
22543 break;
22545 case '@':
22547 ptrdiff_t count = inhibit_garbage_collection ();
22548 Lisp_Object val = call1 (intern ("file-remote-p"),
22549 BVAR (current_buffer, directory));
22550 unbind_to (count, Qnil);
22552 if (NILP (val))
22553 return "-";
22554 else
22555 return "@";
22558 case 'z':
22559 /* coding-system (not including end-of-line format) */
22560 case 'Z':
22561 /* coding-system (including end-of-line type) */
22563 int eol_flag = (c == 'Z');
22564 char *p = decode_mode_spec_buf;
22566 if (! FRAME_WINDOW_P (f))
22568 /* No need to mention EOL here--the terminal never needs
22569 to do EOL conversion. */
22570 p = decode_mode_spec_coding (CODING_ID_NAME
22571 (FRAME_KEYBOARD_CODING (f)->id),
22572 p, 0);
22573 p = decode_mode_spec_coding (CODING_ID_NAME
22574 (FRAME_TERMINAL_CODING (f)->id),
22575 p, 0);
22577 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22578 p, eol_flag);
22580 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22581 #ifdef subprocesses
22582 obj = Fget_buffer_process (Fcurrent_buffer ());
22583 if (PROCESSP (obj))
22585 p = decode_mode_spec_coding
22586 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22587 p = decode_mode_spec_coding
22588 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22590 #endif /* subprocesses */
22591 #endif /* 0 */
22592 *p = 0;
22593 return decode_mode_spec_buf;
22597 if (STRINGP (obj))
22599 *string = obj;
22600 return SSDATA (obj);
22602 else
22603 return "";
22607 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22608 means count lines back from START_BYTE. But don't go beyond
22609 LIMIT_BYTE. Return the number of lines thus found (always
22610 nonnegative).
22612 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22613 either the position COUNT lines after/before START_BYTE, if we
22614 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22615 COUNT lines. */
22617 static ptrdiff_t
22618 display_count_lines (ptrdiff_t start_byte,
22619 ptrdiff_t limit_byte, ptrdiff_t count,
22620 ptrdiff_t *byte_pos_ptr)
22622 register unsigned char *cursor;
22623 unsigned char *base;
22625 register ptrdiff_t ceiling;
22626 register unsigned char *ceiling_addr;
22627 ptrdiff_t orig_count = count;
22629 /* If we are not in selective display mode,
22630 check only for newlines. */
22631 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22632 && !INTEGERP (BVAR (current_buffer, selective_display)));
22634 if (count > 0)
22636 while (start_byte < limit_byte)
22638 ceiling = BUFFER_CEILING_OF (start_byte);
22639 ceiling = min (limit_byte - 1, ceiling);
22640 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22641 base = (cursor = BYTE_POS_ADDR (start_byte));
22645 if (selective_display)
22647 while (*cursor != '\n' && *cursor != 015
22648 && ++cursor != ceiling_addr)
22649 continue;
22650 if (cursor == ceiling_addr)
22651 break;
22653 else
22655 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22656 if (! cursor)
22657 break;
22660 cursor++;
22662 if (--count == 0)
22664 start_byte += cursor - base;
22665 *byte_pos_ptr = start_byte;
22666 return orig_count;
22669 while (cursor < ceiling_addr);
22671 start_byte += ceiling_addr - base;
22674 else
22676 while (start_byte > limit_byte)
22678 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22679 ceiling = max (limit_byte, ceiling);
22680 ceiling_addr = BYTE_POS_ADDR (ceiling);
22681 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22682 while (1)
22684 if (selective_display)
22686 while (--cursor >= ceiling_addr
22687 && *cursor != '\n' && *cursor != 015)
22688 continue;
22689 if (cursor < ceiling_addr)
22690 break;
22692 else
22694 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22695 if (! cursor)
22696 break;
22699 if (++count == 0)
22701 start_byte += cursor - base + 1;
22702 *byte_pos_ptr = start_byte;
22703 /* When scanning backwards, we should
22704 not count the newline posterior to which we stop. */
22705 return - orig_count - 1;
22708 start_byte += ceiling_addr - base;
22712 *byte_pos_ptr = limit_byte;
22714 if (count < 0)
22715 return - orig_count + count;
22716 return orig_count - count;
22722 /***********************************************************************
22723 Displaying strings
22724 ***********************************************************************/
22726 /* Display a NUL-terminated string, starting with index START.
22728 If STRING is non-null, display that C string. Otherwise, the Lisp
22729 string LISP_STRING is displayed. There's a case that STRING is
22730 non-null and LISP_STRING is not nil. It means STRING is a string
22731 data of LISP_STRING. In that case, we display LISP_STRING while
22732 ignoring its text properties.
22734 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22735 FACE_STRING. Display STRING or LISP_STRING with the face at
22736 FACE_STRING_POS in FACE_STRING:
22738 Display the string in the environment given by IT, but use the
22739 standard display table, temporarily.
22741 FIELD_WIDTH is the minimum number of output glyphs to produce.
22742 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22743 with spaces. If STRING has more characters, more than FIELD_WIDTH
22744 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22746 PRECISION is the maximum number of characters to output from
22747 STRING. PRECISION < 0 means don't truncate the string.
22749 This is roughly equivalent to printf format specifiers:
22751 FIELD_WIDTH PRECISION PRINTF
22752 ----------------------------------------
22753 -1 -1 %s
22754 -1 10 %.10s
22755 10 -1 %10s
22756 20 10 %20.10s
22758 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22759 display them, and < 0 means obey the current buffer's value of
22760 enable_multibyte_characters.
22762 Value is the number of columns displayed. */
22764 static int
22765 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22766 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22767 int field_width, int precision, int max_x, int multibyte)
22769 int hpos_at_start = it->hpos;
22770 int saved_face_id = it->face_id;
22771 struct glyph_row *row = it->glyph_row;
22772 ptrdiff_t it_charpos;
22774 /* Initialize the iterator IT for iteration over STRING beginning
22775 with index START. */
22776 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22777 precision, field_width, multibyte);
22778 if (string && STRINGP (lisp_string))
22779 /* LISP_STRING is the one returned by decode_mode_spec. We should
22780 ignore its text properties. */
22781 it->stop_charpos = it->end_charpos;
22783 /* If displaying STRING, set up the face of the iterator from
22784 FACE_STRING, if that's given. */
22785 if (STRINGP (face_string))
22787 ptrdiff_t endptr;
22788 struct face *face;
22790 it->face_id
22791 = face_at_string_position (it->w, face_string, face_string_pos,
22792 0, &endptr, it->base_face_id, 0);
22793 face = FACE_FROM_ID (it->f, it->face_id);
22794 it->face_box_p = face->box != FACE_NO_BOX;
22797 /* Set max_x to the maximum allowed X position. Don't let it go
22798 beyond the right edge of the window. */
22799 if (max_x <= 0)
22800 max_x = it->last_visible_x;
22801 else
22802 max_x = min (max_x, it->last_visible_x);
22804 /* Skip over display elements that are not visible. because IT->w is
22805 hscrolled. */
22806 if (it->current_x < it->first_visible_x)
22807 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22808 MOVE_TO_POS | MOVE_TO_X);
22810 row->ascent = it->max_ascent;
22811 row->height = it->max_ascent + it->max_descent;
22812 row->phys_ascent = it->max_phys_ascent;
22813 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22814 row->extra_line_spacing = it->max_extra_line_spacing;
22816 if (STRINGP (it->string))
22817 it_charpos = IT_STRING_CHARPOS (*it);
22818 else
22819 it_charpos = IT_CHARPOS (*it);
22821 /* This condition is for the case that we are called with current_x
22822 past last_visible_x. */
22823 while (it->current_x < max_x)
22825 int x_before, x, n_glyphs_before, i, nglyphs;
22827 /* Get the next display element. */
22828 if (!get_next_display_element (it))
22829 break;
22831 /* Produce glyphs. */
22832 x_before = it->current_x;
22833 n_glyphs_before = row->used[TEXT_AREA];
22834 PRODUCE_GLYPHS (it);
22836 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22837 i = 0;
22838 x = x_before;
22839 while (i < nglyphs)
22841 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22843 if (it->line_wrap != TRUNCATE
22844 && x + glyph->pixel_width > max_x)
22846 /* End of continued line or max_x reached. */
22847 if (CHAR_GLYPH_PADDING_P (*glyph))
22849 /* A wide character is unbreakable. */
22850 if (row->reversed_p)
22851 unproduce_glyphs (it, row->used[TEXT_AREA]
22852 - n_glyphs_before);
22853 row->used[TEXT_AREA] = n_glyphs_before;
22854 it->current_x = x_before;
22856 else
22858 if (row->reversed_p)
22859 unproduce_glyphs (it, row->used[TEXT_AREA]
22860 - (n_glyphs_before + i));
22861 row->used[TEXT_AREA] = n_glyphs_before + i;
22862 it->current_x = x;
22864 break;
22866 else if (x + glyph->pixel_width >= it->first_visible_x)
22868 /* Glyph is at least partially visible. */
22869 ++it->hpos;
22870 if (x < it->first_visible_x)
22871 row->x = x - it->first_visible_x;
22873 else
22875 /* Glyph is off the left margin of the display area.
22876 Should not happen. */
22877 emacs_abort ();
22880 row->ascent = max (row->ascent, it->max_ascent);
22881 row->height = max (row->height, it->max_ascent + it->max_descent);
22882 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22883 row->phys_height = max (row->phys_height,
22884 it->max_phys_ascent + it->max_phys_descent);
22885 row->extra_line_spacing = max (row->extra_line_spacing,
22886 it->max_extra_line_spacing);
22887 x += glyph->pixel_width;
22888 ++i;
22891 /* Stop if max_x reached. */
22892 if (i < nglyphs)
22893 break;
22895 /* Stop at line ends. */
22896 if (ITERATOR_AT_END_OF_LINE_P (it))
22898 it->continuation_lines_width = 0;
22899 break;
22902 set_iterator_to_next (it, 1);
22903 if (STRINGP (it->string))
22904 it_charpos = IT_STRING_CHARPOS (*it);
22905 else
22906 it_charpos = IT_CHARPOS (*it);
22908 /* Stop if truncating at the right edge. */
22909 if (it->line_wrap == TRUNCATE
22910 && it->current_x >= it->last_visible_x)
22912 /* Add truncation mark, but don't do it if the line is
22913 truncated at a padding space. */
22914 if (it_charpos < it->string_nchars)
22916 if (!FRAME_WINDOW_P (it->f))
22918 int ii, n;
22920 if (it->current_x > it->last_visible_x)
22922 if (!row->reversed_p)
22924 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22925 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22926 break;
22928 else
22930 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22931 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22932 break;
22933 unproduce_glyphs (it, ii + 1);
22934 ii = row->used[TEXT_AREA] - (ii + 1);
22936 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22938 row->used[TEXT_AREA] = ii;
22939 produce_special_glyphs (it, IT_TRUNCATION);
22942 produce_special_glyphs (it, IT_TRUNCATION);
22944 row->truncated_on_right_p = 1;
22946 break;
22950 /* Maybe insert a truncation at the left. */
22951 if (it->first_visible_x
22952 && it_charpos > 0)
22954 if (!FRAME_WINDOW_P (it->f)
22955 || (row->reversed_p
22956 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22957 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22958 insert_left_trunc_glyphs (it);
22959 row->truncated_on_left_p = 1;
22962 it->face_id = saved_face_id;
22964 /* Value is number of columns displayed. */
22965 return it->hpos - hpos_at_start;
22970 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22971 appears as an element of LIST or as the car of an element of LIST.
22972 If PROPVAL is a list, compare each element against LIST in that
22973 way, and return 1/2 if any element of PROPVAL is found in LIST.
22974 Otherwise return 0. This function cannot quit.
22975 The return value is 2 if the text is invisible but with an ellipsis
22976 and 1 if it's invisible and without an ellipsis. */
22979 invisible_p (register Lisp_Object propval, Lisp_Object list)
22981 register Lisp_Object tail, proptail;
22983 for (tail = list; CONSP (tail); tail = XCDR (tail))
22985 register Lisp_Object tem;
22986 tem = XCAR (tail);
22987 if (EQ (propval, tem))
22988 return 1;
22989 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22990 return NILP (XCDR (tem)) ? 1 : 2;
22993 if (CONSP (propval))
22995 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22997 Lisp_Object propelt;
22998 propelt = XCAR (proptail);
22999 for (tail = list; CONSP (tail); tail = XCDR (tail))
23001 register Lisp_Object tem;
23002 tem = XCAR (tail);
23003 if (EQ (propelt, tem))
23004 return 1;
23005 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23006 return NILP (XCDR (tem)) ? 1 : 2;
23011 return 0;
23014 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23015 doc: /* Non-nil if the property makes the text invisible.
23016 POS-OR-PROP can be a marker or number, in which case it is taken to be
23017 a position in the current buffer and the value of the `invisible' property
23018 is checked; or it can be some other value, which is then presumed to be the
23019 value of the `invisible' property of the text of interest.
23020 The non-nil value returned can be t for truly invisible text or something
23021 else if the text is replaced by an ellipsis. */)
23022 (Lisp_Object pos_or_prop)
23024 Lisp_Object prop
23025 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23026 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23027 : pos_or_prop);
23028 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23029 return (invis == 0 ? Qnil
23030 : invis == 1 ? Qt
23031 : make_number (invis));
23034 /* Calculate a width or height in pixels from a specification using
23035 the following elements:
23037 SPEC ::=
23038 NUM - a (fractional) multiple of the default font width/height
23039 (NUM) - specifies exactly NUM pixels
23040 UNIT - a fixed number of pixels, see below.
23041 ELEMENT - size of a display element in pixels, see below.
23042 (NUM . SPEC) - equals NUM * SPEC
23043 (+ SPEC SPEC ...) - add pixel values
23044 (- SPEC SPEC ...) - subtract pixel values
23045 (- SPEC) - negate pixel value
23047 NUM ::=
23048 INT or FLOAT - a number constant
23049 SYMBOL - use symbol's (buffer local) variable binding.
23051 UNIT ::=
23052 in - pixels per inch *)
23053 mm - pixels per 1/1000 meter *)
23054 cm - pixels per 1/100 meter *)
23055 width - width of current font in pixels.
23056 height - height of current font in pixels.
23058 *) using the ratio(s) defined in display-pixels-per-inch.
23060 ELEMENT ::=
23062 left-fringe - left fringe width in pixels
23063 right-fringe - right fringe width in pixels
23065 left-margin - left margin width in pixels
23066 right-margin - right margin width in pixels
23068 scroll-bar - scroll-bar area width in pixels
23070 Examples:
23072 Pixels corresponding to 5 inches:
23073 (5 . in)
23075 Total width of non-text areas on left side of window (if scroll-bar is on left):
23076 '(space :width (+ left-fringe left-margin scroll-bar))
23078 Align to first text column (in header line):
23079 '(space :align-to 0)
23081 Align to middle of text area minus half the width of variable `my-image'
23082 containing a loaded image:
23083 '(space :align-to (0.5 . (- text my-image)))
23085 Width of left margin minus width of 1 character in the default font:
23086 '(space :width (- left-margin 1))
23088 Width of left margin minus width of 2 characters in the current font:
23089 '(space :width (- left-margin (2 . width)))
23091 Center 1 character over left-margin (in header line):
23092 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23094 Different ways to express width of left fringe plus left margin minus one pixel:
23095 '(space :width (- (+ left-fringe left-margin) (1)))
23096 '(space :width (+ left-fringe left-margin (- (1))))
23097 '(space :width (+ left-fringe left-margin (-1)))
23101 static int
23102 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23103 struct font *font, int width_p, int *align_to)
23105 double pixels;
23107 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23108 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23110 if (NILP (prop))
23111 return OK_PIXELS (0);
23113 eassert (FRAME_LIVE_P (it->f));
23115 if (SYMBOLP (prop))
23117 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23119 char *unit = SSDATA (SYMBOL_NAME (prop));
23121 if (unit[0] == 'i' && unit[1] == 'n')
23122 pixels = 1.0;
23123 else if (unit[0] == 'm' && unit[1] == 'm')
23124 pixels = 25.4;
23125 else if (unit[0] == 'c' && unit[1] == 'm')
23126 pixels = 2.54;
23127 else
23128 pixels = 0;
23129 if (pixels > 0)
23131 double ppi = (width_p ? FRAME_RES_X (it->f)
23132 : FRAME_RES_Y (it->f));
23134 if (ppi > 0)
23135 return OK_PIXELS (ppi / pixels);
23136 return 0;
23140 #ifdef HAVE_WINDOW_SYSTEM
23141 if (EQ (prop, Qheight))
23142 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23143 if (EQ (prop, Qwidth))
23144 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23145 #else
23146 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23147 return OK_PIXELS (1);
23148 #endif
23150 if (EQ (prop, Qtext))
23151 return OK_PIXELS (width_p
23152 ? window_box_width (it->w, TEXT_AREA)
23153 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23155 if (align_to && *align_to < 0)
23157 *res = 0;
23158 if (EQ (prop, Qleft))
23159 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23160 if (EQ (prop, Qright))
23161 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23162 if (EQ (prop, Qcenter))
23163 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23164 + window_box_width (it->w, TEXT_AREA) / 2);
23165 if (EQ (prop, Qleft_fringe))
23166 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23167 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23168 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23169 if (EQ (prop, Qright_fringe))
23170 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23171 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23172 : window_box_right_offset (it->w, TEXT_AREA));
23173 if (EQ (prop, Qleft_margin))
23174 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23175 if (EQ (prop, Qright_margin))
23176 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23177 if (EQ (prop, Qscroll_bar))
23178 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23180 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23181 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23182 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23183 : 0)));
23185 else
23187 if (EQ (prop, Qleft_fringe))
23188 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23189 if (EQ (prop, Qright_fringe))
23190 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23191 if (EQ (prop, Qleft_margin))
23192 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23193 if (EQ (prop, Qright_margin))
23194 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23195 if (EQ (prop, Qscroll_bar))
23196 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23199 prop = buffer_local_value_1 (prop, it->w->contents);
23200 if (EQ (prop, Qunbound))
23201 prop = Qnil;
23204 if (INTEGERP (prop) || FLOATP (prop))
23206 int base_unit = (width_p
23207 ? FRAME_COLUMN_WIDTH (it->f)
23208 : FRAME_LINE_HEIGHT (it->f));
23209 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23212 if (CONSP (prop))
23214 Lisp_Object car = XCAR (prop);
23215 Lisp_Object cdr = XCDR (prop);
23217 if (SYMBOLP (car))
23219 #ifdef HAVE_WINDOW_SYSTEM
23220 if (FRAME_WINDOW_P (it->f)
23221 && valid_image_p (prop))
23223 ptrdiff_t id = lookup_image (it->f, prop);
23224 struct image *img = IMAGE_FROM_ID (it->f, id);
23226 return OK_PIXELS (width_p ? img->width : img->height);
23228 #endif
23229 if (EQ (car, Qplus) || EQ (car, Qminus))
23231 int first = 1;
23232 double px;
23234 pixels = 0;
23235 while (CONSP (cdr))
23237 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23238 font, width_p, align_to))
23239 return 0;
23240 if (first)
23241 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23242 else
23243 pixels += px;
23244 cdr = XCDR (cdr);
23246 if (EQ (car, Qminus))
23247 pixels = -pixels;
23248 return OK_PIXELS (pixels);
23251 car = buffer_local_value_1 (car, it->w->contents);
23252 if (EQ (car, Qunbound))
23253 car = Qnil;
23256 if (INTEGERP (car) || FLOATP (car))
23258 double fact;
23259 pixels = XFLOATINT (car);
23260 if (NILP (cdr))
23261 return OK_PIXELS (pixels);
23262 if (calc_pixel_width_or_height (&fact, it, cdr,
23263 font, width_p, align_to))
23264 return OK_PIXELS (pixels * fact);
23265 return 0;
23268 return 0;
23271 return 0;
23275 /***********************************************************************
23276 Glyph Display
23277 ***********************************************************************/
23279 #ifdef HAVE_WINDOW_SYSTEM
23281 #ifdef GLYPH_DEBUG
23283 void
23284 dump_glyph_string (struct glyph_string *s)
23286 fprintf (stderr, "glyph string\n");
23287 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23288 s->x, s->y, s->width, s->height);
23289 fprintf (stderr, " ybase = %d\n", s->ybase);
23290 fprintf (stderr, " hl = %d\n", s->hl);
23291 fprintf (stderr, " left overhang = %d, right = %d\n",
23292 s->left_overhang, s->right_overhang);
23293 fprintf (stderr, " nchars = %d\n", s->nchars);
23294 fprintf (stderr, " extends to end of line = %d\n",
23295 s->extends_to_end_of_line_p);
23296 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23297 fprintf (stderr, " bg width = %d\n", s->background_width);
23300 #endif /* GLYPH_DEBUG */
23302 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23303 of XChar2b structures for S; it can't be allocated in
23304 init_glyph_string because it must be allocated via `alloca'. W
23305 is the window on which S is drawn. ROW and AREA are the glyph row
23306 and area within the row from which S is constructed. START is the
23307 index of the first glyph structure covered by S. HL is a
23308 face-override for drawing S. */
23310 #ifdef HAVE_NTGUI
23311 #define OPTIONAL_HDC(hdc) HDC hdc,
23312 #define DECLARE_HDC(hdc) HDC hdc;
23313 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23314 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23315 #endif
23317 #ifndef OPTIONAL_HDC
23318 #define OPTIONAL_HDC(hdc)
23319 #define DECLARE_HDC(hdc)
23320 #define ALLOCATE_HDC(hdc, f)
23321 #define RELEASE_HDC(hdc, f)
23322 #endif
23324 static void
23325 init_glyph_string (struct glyph_string *s,
23326 OPTIONAL_HDC (hdc)
23327 XChar2b *char2b, struct window *w, struct glyph_row *row,
23328 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23330 memset (s, 0, sizeof *s);
23331 s->w = w;
23332 s->f = XFRAME (w->frame);
23333 #ifdef HAVE_NTGUI
23334 s->hdc = hdc;
23335 #endif
23336 s->display = FRAME_X_DISPLAY (s->f);
23337 s->window = FRAME_X_WINDOW (s->f);
23338 s->char2b = char2b;
23339 s->hl = hl;
23340 s->row = row;
23341 s->area = area;
23342 s->first_glyph = row->glyphs[area] + start;
23343 s->height = row->height;
23344 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23345 s->ybase = s->y + row->ascent;
23349 /* Append the list of glyph strings with head H and tail T to the list
23350 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23352 static void
23353 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23354 struct glyph_string *h, struct glyph_string *t)
23356 if (h)
23358 if (*head)
23359 (*tail)->next = h;
23360 else
23361 *head = h;
23362 h->prev = *tail;
23363 *tail = t;
23368 /* Prepend the list of glyph strings with head H and tail T to the
23369 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23370 result. */
23372 static void
23373 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23374 struct glyph_string *h, struct glyph_string *t)
23376 if (h)
23378 if (*head)
23379 (*head)->prev = t;
23380 else
23381 *tail = t;
23382 t->next = *head;
23383 *head = h;
23388 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23389 Set *HEAD and *TAIL to the resulting list. */
23391 static void
23392 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23393 struct glyph_string *s)
23395 s->next = s->prev = NULL;
23396 append_glyph_string_lists (head, tail, s, s);
23400 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23401 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23402 make sure that X resources for the face returned are allocated.
23403 Value is a pointer to a realized face that is ready for display if
23404 DISPLAY_P is non-zero. */
23406 static struct face *
23407 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23408 XChar2b *char2b, int display_p)
23410 struct face *face = FACE_FROM_ID (f, face_id);
23411 unsigned code = 0;
23413 if (face->font)
23415 code = face->font->driver->encode_char (face->font, c);
23417 if (code == FONT_INVALID_CODE)
23418 code = 0;
23420 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23422 /* Make sure X resources of the face are allocated. */
23423 #ifdef HAVE_X_WINDOWS
23424 if (display_p)
23425 #endif
23427 eassert (face != NULL);
23428 PREPARE_FACE_FOR_DISPLAY (f, face);
23431 return face;
23435 /* Get face and two-byte form of character glyph GLYPH on frame F.
23436 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23437 a pointer to a realized face that is ready for display. */
23439 static struct face *
23440 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23441 XChar2b *char2b, int *two_byte_p)
23443 struct face *face;
23444 unsigned code = 0;
23446 eassert (glyph->type == CHAR_GLYPH);
23447 face = FACE_FROM_ID (f, glyph->face_id);
23449 /* Make sure X resources of the face are allocated. */
23450 eassert (face != NULL);
23451 PREPARE_FACE_FOR_DISPLAY (f, face);
23453 if (two_byte_p)
23454 *two_byte_p = 0;
23456 if (face->font)
23458 if (CHAR_BYTE8_P (glyph->u.ch))
23459 code = CHAR_TO_BYTE8 (glyph->u.ch);
23460 else
23461 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23463 if (code == FONT_INVALID_CODE)
23464 code = 0;
23467 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23468 return face;
23472 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23473 Return 1 if FONT has a glyph for C, otherwise return 0. */
23475 static int
23476 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23478 unsigned code;
23480 if (CHAR_BYTE8_P (c))
23481 code = CHAR_TO_BYTE8 (c);
23482 else
23483 code = font->driver->encode_char (font, c);
23485 if (code == FONT_INVALID_CODE)
23486 return 0;
23487 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23488 return 1;
23492 /* Fill glyph string S with composition components specified by S->cmp.
23494 BASE_FACE is the base face of the composition.
23495 S->cmp_from is the index of the first component for S.
23497 OVERLAPS non-zero means S should draw the foreground only, and use
23498 its physical height for clipping. See also draw_glyphs.
23500 Value is the index of a component not in S. */
23502 static int
23503 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23504 int overlaps)
23506 int i;
23507 /* For all glyphs of this composition, starting at the offset
23508 S->cmp_from, until we reach the end of the definition or encounter a
23509 glyph that requires the different face, add it to S. */
23510 struct face *face;
23512 eassert (s);
23514 s->for_overlaps = overlaps;
23515 s->face = NULL;
23516 s->font = NULL;
23517 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23519 int c = COMPOSITION_GLYPH (s->cmp, i);
23521 /* TAB in a composition means display glyphs with padding space
23522 on the left or right. */
23523 if (c != '\t')
23525 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23526 -1, Qnil);
23528 face = get_char_face_and_encoding (s->f, c, face_id,
23529 s->char2b + i, 1);
23530 if (face)
23532 if (! s->face)
23534 s->face = face;
23535 s->font = s->face->font;
23537 else if (s->face != face)
23538 break;
23541 ++s->nchars;
23543 s->cmp_to = i;
23545 if (s->face == NULL)
23547 s->face = base_face->ascii_face;
23548 s->font = s->face->font;
23551 /* All glyph strings for the same composition has the same width,
23552 i.e. the width set for the first component of the composition. */
23553 s->width = s->first_glyph->pixel_width;
23555 /* If the specified font could not be loaded, use the frame's
23556 default font, but record the fact that we couldn't load it in
23557 the glyph string so that we can draw rectangles for the
23558 characters of the glyph string. */
23559 if (s->font == NULL)
23561 s->font_not_found_p = 1;
23562 s->font = FRAME_FONT (s->f);
23565 /* Adjust base line for subscript/superscript text. */
23566 s->ybase += s->first_glyph->voffset;
23568 /* This glyph string must always be drawn with 16-bit functions. */
23569 s->two_byte_p = 1;
23571 return s->cmp_to;
23574 static int
23575 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23576 int start, int end, int overlaps)
23578 struct glyph *glyph, *last;
23579 Lisp_Object lgstring;
23580 int i;
23582 s->for_overlaps = overlaps;
23583 glyph = s->row->glyphs[s->area] + start;
23584 last = s->row->glyphs[s->area] + end;
23585 s->cmp_id = glyph->u.cmp.id;
23586 s->cmp_from = glyph->slice.cmp.from;
23587 s->cmp_to = glyph->slice.cmp.to + 1;
23588 s->face = FACE_FROM_ID (s->f, face_id);
23589 lgstring = composition_gstring_from_id (s->cmp_id);
23590 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23591 glyph++;
23592 while (glyph < last
23593 && glyph->u.cmp.automatic
23594 && glyph->u.cmp.id == s->cmp_id
23595 && s->cmp_to == glyph->slice.cmp.from)
23596 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23598 for (i = s->cmp_from; i < s->cmp_to; i++)
23600 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23601 unsigned code = LGLYPH_CODE (lglyph);
23603 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23605 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23606 return glyph - s->row->glyphs[s->area];
23610 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23611 See the comment of fill_glyph_string for arguments.
23612 Value is the index of the first glyph not in S. */
23615 static int
23616 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23617 int start, int end, int overlaps)
23619 struct glyph *glyph, *last;
23620 int voffset;
23622 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23623 s->for_overlaps = overlaps;
23624 glyph = s->row->glyphs[s->area] + start;
23625 last = s->row->glyphs[s->area] + end;
23626 voffset = glyph->voffset;
23627 s->face = FACE_FROM_ID (s->f, face_id);
23628 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23629 s->nchars = 1;
23630 s->width = glyph->pixel_width;
23631 glyph++;
23632 while (glyph < last
23633 && glyph->type == GLYPHLESS_GLYPH
23634 && glyph->voffset == voffset
23635 && glyph->face_id == face_id)
23637 s->nchars++;
23638 s->width += glyph->pixel_width;
23639 glyph++;
23641 s->ybase += voffset;
23642 return glyph - s->row->glyphs[s->area];
23646 /* Fill glyph string S from a sequence of character glyphs.
23648 FACE_ID is the face id of the string. START is the index of the
23649 first glyph to consider, END is the index of the last + 1.
23650 OVERLAPS non-zero means S should draw the foreground only, and use
23651 its physical height for clipping. See also draw_glyphs.
23653 Value is the index of the first glyph not in S. */
23655 static int
23656 fill_glyph_string (struct glyph_string *s, int face_id,
23657 int start, int end, int overlaps)
23659 struct glyph *glyph, *last;
23660 int voffset;
23661 int glyph_not_available_p;
23663 eassert (s->f == XFRAME (s->w->frame));
23664 eassert (s->nchars == 0);
23665 eassert (start >= 0 && end > start);
23667 s->for_overlaps = overlaps;
23668 glyph = s->row->glyphs[s->area] + start;
23669 last = s->row->glyphs[s->area] + end;
23670 voffset = glyph->voffset;
23671 s->padding_p = glyph->padding_p;
23672 glyph_not_available_p = glyph->glyph_not_available_p;
23674 while (glyph < last
23675 && glyph->type == CHAR_GLYPH
23676 && glyph->voffset == voffset
23677 /* Same face id implies same font, nowadays. */
23678 && glyph->face_id == face_id
23679 && glyph->glyph_not_available_p == glyph_not_available_p)
23681 int two_byte_p;
23683 s->face = get_glyph_face_and_encoding (s->f, glyph,
23684 s->char2b + s->nchars,
23685 &two_byte_p);
23686 s->two_byte_p = two_byte_p;
23687 ++s->nchars;
23688 eassert (s->nchars <= end - start);
23689 s->width += glyph->pixel_width;
23690 if (glyph++->padding_p != s->padding_p)
23691 break;
23694 s->font = s->face->font;
23696 /* If the specified font could not be loaded, use the frame's font,
23697 but record the fact that we couldn't load it in
23698 S->font_not_found_p so that we can draw rectangles for the
23699 characters of the glyph string. */
23700 if (s->font == NULL || glyph_not_available_p)
23702 s->font_not_found_p = 1;
23703 s->font = FRAME_FONT (s->f);
23706 /* Adjust base line for subscript/superscript text. */
23707 s->ybase += voffset;
23709 eassert (s->face && s->face->gc);
23710 return glyph - s->row->glyphs[s->area];
23714 /* Fill glyph string S from image glyph S->first_glyph. */
23716 static void
23717 fill_image_glyph_string (struct glyph_string *s)
23719 eassert (s->first_glyph->type == IMAGE_GLYPH);
23720 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23721 eassert (s->img);
23722 s->slice = s->first_glyph->slice.img;
23723 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23724 s->font = s->face->font;
23725 s->width = s->first_glyph->pixel_width;
23727 /* Adjust base line for subscript/superscript text. */
23728 s->ybase += s->first_glyph->voffset;
23732 /* Fill glyph string S from a sequence of stretch glyphs.
23734 START is the index of the first glyph to consider,
23735 END is the index of the last + 1.
23737 Value is the index of the first glyph not in S. */
23739 static int
23740 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23742 struct glyph *glyph, *last;
23743 int voffset, face_id;
23745 eassert (s->first_glyph->type == STRETCH_GLYPH);
23747 glyph = s->row->glyphs[s->area] + start;
23748 last = s->row->glyphs[s->area] + end;
23749 face_id = glyph->face_id;
23750 s->face = FACE_FROM_ID (s->f, face_id);
23751 s->font = s->face->font;
23752 s->width = glyph->pixel_width;
23753 s->nchars = 1;
23754 voffset = glyph->voffset;
23756 for (++glyph;
23757 (glyph < last
23758 && glyph->type == STRETCH_GLYPH
23759 && glyph->voffset == voffset
23760 && glyph->face_id == face_id);
23761 ++glyph)
23762 s->width += glyph->pixel_width;
23764 /* Adjust base line for subscript/superscript text. */
23765 s->ybase += voffset;
23767 /* The case that face->gc == 0 is handled when drawing the glyph
23768 string by calling PREPARE_FACE_FOR_DISPLAY. */
23769 eassert (s->face);
23770 return glyph - s->row->glyphs[s->area];
23773 static struct font_metrics *
23774 get_per_char_metric (struct font *font, XChar2b *char2b)
23776 static struct font_metrics metrics;
23777 unsigned code;
23779 if (! font)
23780 return NULL;
23781 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23782 if (code == FONT_INVALID_CODE)
23783 return NULL;
23784 font->driver->text_extents (font, &code, 1, &metrics);
23785 return &metrics;
23788 /* EXPORT for RIF:
23789 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23790 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23791 assumed to be zero. */
23793 void
23794 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23796 *left = *right = 0;
23798 if (glyph->type == CHAR_GLYPH)
23800 struct face *face;
23801 XChar2b char2b;
23802 struct font_metrics *pcm;
23804 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23805 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23807 if (pcm->rbearing > pcm->width)
23808 *right = pcm->rbearing - pcm->width;
23809 if (pcm->lbearing < 0)
23810 *left = -pcm->lbearing;
23813 else if (glyph->type == COMPOSITE_GLYPH)
23815 if (! glyph->u.cmp.automatic)
23817 struct composition *cmp = composition_table[glyph->u.cmp.id];
23819 if (cmp->rbearing > cmp->pixel_width)
23820 *right = cmp->rbearing - cmp->pixel_width;
23821 if (cmp->lbearing < 0)
23822 *left = - cmp->lbearing;
23824 else
23826 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23827 struct font_metrics metrics;
23829 composition_gstring_width (gstring, glyph->slice.cmp.from,
23830 glyph->slice.cmp.to + 1, &metrics);
23831 if (metrics.rbearing > metrics.width)
23832 *right = metrics.rbearing - metrics.width;
23833 if (metrics.lbearing < 0)
23834 *left = - metrics.lbearing;
23840 /* Return the index of the first glyph preceding glyph string S that
23841 is overwritten by S because of S's left overhang. Value is -1
23842 if no glyphs are overwritten. */
23844 static int
23845 left_overwritten (struct glyph_string *s)
23847 int k;
23849 if (s->left_overhang)
23851 int x = 0, i;
23852 struct glyph *glyphs = s->row->glyphs[s->area];
23853 int first = s->first_glyph - glyphs;
23855 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23856 x -= glyphs[i].pixel_width;
23858 k = i + 1;
23860 else
23861 k = -1;
23863 return k;
23867 /* Return the index of the first glyph preceding glyph string S that
23868 is overwriting S because of its right overhang. Value is -1 if no
23869 glyph in front of S overwrites S. */
23871 static int
23872 left_overwriting (struct glyph_string *s)
23874 int i, k, x;
23875 struct glyph *glyphs = s->row->glyphs[s->area];
23876 int first = s->first_glyph - glyphs;
23878 k = -1;
23879 x = 0;
23880 for (i = first - 1; i >= 0; --i)
23882 int left, right;
23883 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23884 if (x + right > 0)
23885 k = i;
23886 x -= glyphs[i].pixel_width;
23889 return k;
23893 /* Return the index of the last glyph following glyph string S that is
23894 overwritten by S because of S's right overhang. Value is -1 if
23895 no such glyph is found. */
23897 static int
23898 right_overwritten (struct glyph_string *s)
23900 int k = -1;
23902 if (s->right_overhang)
23904 int x = 0, i;
23905 struct glyph *glyphs = s->row->glyphs[s->area];
23906 int first = (s->first_glyph - glyphs
23907 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23908 int end = s->row->used[s->area];
23910 for (i = first; i < end && s->right_overhang > x; ++i)
23911 x += glyphs[i].pixel_width;
23913 k = i;
23916 return k;
23920 /* Return the index of the last glyph following glyph string S that
23921 overwrites S because of its left overhang. Value is negative
23922 if no such glyph is found. */
23924 static int
23925 right_overwriting (struct glyph_string *s)
23927 int i, k, x;
23928 int end = s->row->used[s->area];
23929 struct glyph *glyphs = s->row->glyphs[s->area];
23930 int first = (s->first_glyph - glyphs
23931 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23933 k = -1;
23934 x = 0;
23935 for (i = first; i < end; ++i)
23937 int left, right;
23938 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23939 if (x - left < 0)
23940 k = i;
23941 x += glyphs[i].pixel_width;
23944 return k;
23948 /* Set background width of glyph string S. START is the index of the
23949 first glyph following S. LAST_X is the right-most x-position + 1
23950 in the drawing area. */
23952 static void
23953 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23955 /* If the face of this glyph string has to be drawn to the end of
23956 the drawing area, set S->extends_to_end_of_line_p. */
23958 if (start == s->row->used[s->area]
23959 && ((s->row->fill_line_p
23960 && (s->hl == DRAW_NORMAL_TEXT
23961 || s->hl == DRAW_IMAGE_RAISED
23962 || s->hl == DRAW_IMAGE_SUNKEN))
23963 || s->hl == DRAW_MOUSE_FACE))
23964 s->extends_to_end_of_line_p = 1;
23966 /* If S extends its face to the end of the line, set its
23967 background_width to the distance to the right edge of the drawing
23968 area. */
23969 if (s->extends_to_end_of_line_p)
23970 s->background_width = last_x - s->x + 1;
23971 else
23972 s->background_width = s->width;
23976 /* Compute overhangs and x-positions for glyph string S and its
23977 predecessors, or successors. X is the starting x-position for S.
23978 BACKWARD_P non-zero means process predecessors. */
23980 static void
23981 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23983 if (backward_p)
23985 while (s)
23987 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23988 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23989 x -= s->width;
23990 s->x = x;
23991 s = s->prev;
23994 else
23996 while (s)
23998 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23999 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24000 s->x = x;
24001 x += s->width;
24002 s = s->next;
24009 /* The following macros are only called from draw_glyphs below.
24010 They reference the following parameters of that function directly:
24011 `w', `row', `area', and `overlap_p'
24012 as well as the following local variables:
24013 `s', `f', and `hdc' (in W32) */
24015 #ifdef HAVE_NTGUI
24016 /* On W32, silently add local `hdc' variable to argument list of
24017 init_glyph_string. */
24018 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24019 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24020 #else
24021 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24022 init_glyph_string (s, char2b, w, row, area, start, hl)
24023 #endif
24025 /* Add a glyph string for a stretch glyph to the list of strings
24026 between HEAD and TAIL. START is the index of the stretch glyph in
24027 row area AREA of glyph row ROW. END is the index of the last glyph
24028 in that glyph row area. X is the current output position assigned
24029 to the new glyph string constructed. HL overrides that face of the
24030 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24031 is the right-most x-position of the drawing area. */
24033 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24034 and below -- keep them on one line. */
24035 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24036 do \
24038 s = alloca (sizeof *s); \
24039 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24040 START = fill_stretch_glyph_string (s, START, END); \
24041 append_glyph_string (&HEAD, &TAIL, s); \
24042 s->x = (X); \
24044 while (0)
24047 /* Add a glyph string for an image glyph to the list of strings
24048 between HEAD and TAIL. START is the index of the image glyph in
24049 row area AREA of glyph row ROW. END is the index of the last glyph
24050 in that glyph row area. X is the current output position assigned
24051 to the new glyph string constructed. HL overrides that face of the
24052 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24053 is the right-most x-position of the drawing area. */
24055 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24056 do \
24058 s = alloca (sizeof *s); \
24059 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24060 fill_image_glyph_string (s); \
24061 append_glyph_string (&HEAD, &TAIL, s); \
24062 ++START; \
24063 s->x = (X); \
24065 while (0)
24068 /* Add a glyph string for a sequence of character glyphs to the list
24069 of strings between HEAD and TAIL. START is the index of the first
24070 glyph in row area AREA of glyph row ROW that is part of the new
24071 glyph string. END is the index of the last glyph in that glyph row
24072 area. X is the current output position assigned to the new glyph
24073 string constructed. HL overrides that face of the glyph; e.g. it
24074 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24075 right-most x-position of the drawing area. */
24077 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24078 do \
24080 int face_id; \
24081 XChar2b *char2b; \
24083 face_id = (row)->glyphs[area][START].face_id; \
24085 s = alloca (sizeof *s); \
24086 char2b = alloca ((END - START) * sizeof *char2b); \
24087 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24088 append_glyph_string (&HEAD, &TAIL, s); \
24089 s->x = (X); \
24090 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24092 while (0)
24095 /* Add a glyph string for a composite sequence to the list of strings
24096 between HEAD and TAIL. START is the index of the first glyph in
24097 row area AREA of glyph row ROW that is part of the new glyph
24098 string. END is the index of the last glyph in that glyph row area.
24099 X is the current output position assigned to the new glyph string
24100 constructed. HL overrides that face of the glyph; e.g. it is
24101 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24102 x-position of the drawing area. */
24104 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24105 do { \
24106 int face_id = (row)->glyphs[area][START].face_id; \
24107 struct face *base_face = FACE_FROM_ID (f, face_id); \
24108 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24109 struct composition *cmp = composition_table[cmp_id]; \
24110 XChar2b *char2b; \
24111 struct glyph_string *first_s = NULL; \
24112 int n; \
24114 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24116 /* Make glyph_strings for each glyph sequence that is drawable by \
24117 the same face, and append them to HEAD/TAIL. */ \
24118 for (n = 0; n < cmp->glyph_len;) \
24120 s = alloca (sizeof *s); \
24121 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24122 append_glyph_string (&(HEAD), &(TAIL), s); \
24123 s->cmp = cmp; \
24124 s->cmp_from = n; \
24125 s->x = (X); \
24126 if (n == 0) \
24127 first_s = s; \
24128 n = fill_composite_glyph_string (s, base_face, overlaps); \
24131 ++START; \
24132 s = first_s; \
24133 } while (0)
24136 /* Add a glyph string for a glyph-string sequence to the list of strings
24137 between HEAD and TAIL. */
24139 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24140 do { \
24141 int face_id; \
24142 XChar2b *char2b; \
24143 Lisp_Object gstring; \
24145 face_id = (row)->glyphs[area][START].face_id; \
24146 gstring = (composition_gstring_from_id \
24147 ((row)->glyphs[area][START].u.cmp.id)); \
24148 s = alloca (sizeof *s); \
24149 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24150 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24151 append_glyph_string (&(HEAD), &(TAIL), s); \
24152 s->x = (X); \
24153 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24154 } while (0)
24157 /* Add a glyph string for a sequence of glyphless character's glyphs
24158 to the list of strings between HEAD and TAIL. The meanings of
24159 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24161 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24162 do \
24164 int face_id; \
24166 face_id = (row)->glyphs[area][START].face_id; \
24168 s = alloca (sizeof *s); \
24169 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24170 append_glyph_string (&HEAD, &TAIL, s); \
24171 s->x = (X); \
24172 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24173 overlaps); \
24175 while (0)
24178 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24179 of AREA of glyph row ROW on window W between indices START and END.
24180 HL overrides the face for drawing glyph strings, e.g. it is
24181 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24182 x-positions of the drawing area.
24184 This is an ugly monster macro construct because we must use alloca
24185 to allocate glyph strings (because draw_glyphs can be called
24186 asynchronously). */
24188 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24189 do \
24191 HEAD = TAIL = NULL; \
24192 while (START < END) \
24194 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24195 switch (first_glyph->type) \
24197 case CHAR_GLYPH: \
24198 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24199 HL, X, LAST_X); \
24200 break; \
24202 case COMPOSITE_GLYPH: \
24203 if (first_glyph->u.cmp.automatic) \
24204 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24205 HL, X, LAST_X); \
24206 else \
24207 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24208 HL, X, LAST_X); \
24209 break; \
24211 case STRETCH_GLYPH: \
24212 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24213 HL, X, LAST_X); \
24214 break; \
24216 case IMAGE_GLYPH: \
24217 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24218 HL, X, LAST_X); \
24219 break; \
24221 case GLYPHLESS_GLYPH: \
24222 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24223 HL, X, LAST_X); \
24224 break; \
24226 default: \
24227 emacs_abort (); \
24230 if (s) \
24232 set_glyph_string_background_width (s, START, LAST_X); \
24233 (X) += s->width; \
24236 } while (0)
24239 /* Draw glyphs between START and END in AREA of ROW on window W,
24240 starting at x-position X. X is relative to AREA in W. HL is a
24241 face-override with the following meaning:
24243 DRAW_NORMAL_TEXT draw normally
24244 DRAW_CURSOR draw in cursor face
24245 DRAW_MOUSE_FACE draw in mouse face.
24246 DRAW_INVERSE_VIDEO draw in mode line face
24247 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24248 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24250 If OVERLAPS is non-zero, draw only the foreground of characters and
24251 clip to the physical height of ROW. Non-zero value also defines
24252 the overlapping part to be drawn:
24254 OVERLAPS_PRED overlap with preceding rows
24255 OVERLAPS_SUCC overlap with succeeding rows
24256 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24257 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24259 Value is the x-position reached, relative to AREA of W. */
24261 static int
24262 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24263 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24264 enum draw_glyphs_face hl, int overlaps)
24266 struct glyph_string *head, *tail;
24267 struct glyph_string *s;
24268 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24269 int i, j, x_reached, last_x, area_left = 0;
24270 struct frame *f = XFRAME (WINDOW_FRAME (w));
24271 DECLARE_HDC (hdc);
24273 ALLOCATE_HDC (hdc, f);
24275 /* Let's rather be paranoid than getting a SEGV. */
24276 end = min (end, row->used[area]);
24277 start = clip_to_bounds (0, start, end);
24279 /* Translate X to frame coordinates. Set last_x to the right
24280 end of the drawing area. */
24281 if (row->full_width_p)
24283 /* X is relative to the left edge of W, without scroll bars
24284 or fringes. */
24285 area_left = WINDOW_LEFT_EDGE_X (w);
24286 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24287 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24289 else
24291 area_left = window_box_left (w, area);
24292 last_x = area_left + window_box_width (w, area);
24294 x += area_left;
24296 /* Build a doubly-linked list of glyph_string structures between
24297 head and tail from what we have to draw. Note that the macro
24298 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24299 the reason we use a separate variable `i'. */
24300 i = start;
24301 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24302 if (tail)
24303 x_reached = tail->x + tail->background_width;
24304 else
24305 x_reached = x;
24307 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24308 the row, redraw some glyphs in front or following the glyph
24309 strings built above. */
24310 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24312 struct glyph_string *h, *t;
24313 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24314 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24315 int check_mouse_face = 0;
24316 int dummy_x = 0;
24318 /* If mouse highlighting is on, we may need to draw adjacent
24319 glyphs using mouse-face highlighting. */
24320 if (area == TEXT_AREA && row->mouse_face_p
24321 && hlinfo->mouse_face_beg_row >= 0
24322 && hlinfo->mouse_face_end_row >= 0)
24324 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24326 if (row_vpos >= hlinfo->mouse_face_beg_row
24327 && row_vpos <= hlinfo->mouse_face_end_row)
24329 check_mouse_face = 1;
24330 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24331 ? hlinfo->mouse_face_beg_col : 0;
24332 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24333 ? hlinfo->mouse_face_end_col
24334 : row->used[TEXT_AREA];
24338 /* Compute overhangs for all glyph strings. */
24339 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24340 for (s = head; s; s = s->next)
24341 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24343 /* Prepend glyph strings for glyphs in front of the first glyph
24344 string that are overwritten because of the first glyph
24345 string's left overhang. The background of all strings
24346 prepended must be drawn because the first glyph string
24347 draws over it. */
24348 i = left_overwritten (head);
24349 if (i >= 0)
24351 enum draw_glyphs_face overlap_hl;
24353 /* If this row contains mouse highlighting, attempt to draw
24354 the overlapped glyphs with the correct highlight. This
24355 code fails if the overlap encompasses more than one glyph
24356 and mouse-highlight spans only some of these glyphs.
24357 However, making it work perfectly involves a lot more
24358 code, and I don't know if the pathological case occurs in
24359 practice, so we'll stick to this for now. --- cyd */
24360 if (check_mouse_face
24361 && mouse_beg_col < start && mouse_end_col > i)
24362 overlap_hl = DRAW_MOUSE_FACE;
24363 else
24364 overlap_hl = DRAW_NORMAL_TEXT;
24366 j = i;
24367 BUILD_GLYPH_STRINGS (j, start, h, t,
24368 overlap_hl, dummy_x, last_x);
24369 start = i;
24370 compute_overhangs_and_x (t, head->x, 1);
24371 prepend_glyph_string_lists (&head, &tail, h, t);
24372 clip_head = head;
24375 /* Prepend glyph strings for glyphs in front of the first glyph
24376 string that overwrite that glyph string because of their
24377 right overhang. For these strings, only the foreground must
24378 be drawn, because it draws over the glyph string at `head'.
24379 The background must not be drawn because this would overwrite
24380 right overhangs of preceding glyphs for which no glyph
24381 strings exist. */
24382 i = left_overwriting (head);
24383 if (i >= 0)
24385 enum draw_glyphs_face overlap_hl;
24387 if (check_mouse_face
24388 && mouse_beg_col < start && mouse_end_col > i)
24389 overlap_hl = DRAW_MOUSE_FACE;
24390 else
24391 overlap_hl = DRAW_NORMAL_TEXT;
24393 clip_head = head;
24394 BUILD_GLYPH_STRINGS (i, start, h, t,
24395 overlap_hl, dummy_x, last_x);
24396 for (s = h; s; s = s->next)
24397 s->background_filled_p = 1;
24398 compute_overhangs_and_x (t, head->x, 1);
24399 prepend_glyph_string_lists (&head, &tail, h, t);
24402 /* Append glyphs strings for glyphs following the last glyph
24403 string tail that are overwritten by tail. The background of
24404 these strings has to be drawn because tail's foreground draws
24405 over it. */
24406 i = right_overwritten (tail);
24407 if (i >= 0)
24409 enum draw_glyphs_face overlap_hl;
24411 if (check_mouse_face
24412 && mouse_beg_col < i && mouse_end_col > end)
24413 overlap_hl = DRAW_MOUSE_FACE;
24414 else
24415 overlap_hl = DRAW_NORMAL_TEXT;
24417 BUILD_GLYPH_STRINGS (end, i, h, t,
24418 overlap_hl, x, last_x);
24419 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24420 we don't have `end = i;' here. */
24421 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24422 append_glyph_string_lists (&head, &tail, h, t);
24423 clip_tail = tail;
24426 /* Append glyph strings for glyphs following the last glyph
24427 string tail that overwrite tail. The foreground of such
24428 glyphs has to be drawn because it writes into the background
24429 of tail. The background must not be drawn because it could
24430 paint over the foreground of following glyphs. */
24431 i = right_overwriting (tail);
24432 if (i >= 0)
24434 enum draw_glyphs_face overlap_hl;
24435 if (check_mouse_face
24436 && mouse_beg_col < i && mouse_end_col > end)
24437 overlap_hl = DRAW_MOUSE_FACE;
24438 else
24439 overlap_hl = DRAW_NORMAL_TEXT;
24441 clip_tail = tail;
24442 i++; /* We must include the Ith glyph. */
24443 BUILD_GLYPH_STRINGS (end, i, h, t,
24444 overlap_hl, x, last_x);
24445 for (s = h; s; s = s->next)
24446 s->background_filled_p = 1;
24447 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24448 append_glyph_string_lists (&head, &tail, h, t);
24450 if (clip_head || clip_tail)
24451 for (s = head; s; s = s->next)
24453 s->clip_head = clip_head;
24454 s->clip_tail = clip_tail;
24458 /* Draw all strings. */
24459 for (s = head; s; s = s->next)
24460 FRAME_RIF (f)->draw_glyph_string (s);
24462 #ifndef HAVE_NS
24463 /* When focus a sole frame and move horizontally, this sets on_p to 0
24464 causing a failure to erase prev cursor position. */
24465 if (area == TEXT_AREA
24466 && !row->full_width_p
24467 /* When drawing overlapping rows, only the glyph strings'
24468 foreground is drawn, which doesn't erase a cursor
24469 completely. */
24470 && !overlaps)
24472 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24473 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24474 : (tail ? tail->x + tail->background_width : x));
24475 x0 -= area_left;
24476 x1 -= area_left;
24478 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24479 row->y, MATRIX_ROW_BOTTOM_Y (row));
24481 #endif
24483 /* Value is the x-position up to which drawn, relative to AREA of W.
24484 This doesn't include parts drawn because of overhangs. */
24485 if (row->full_width_p)
24486 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24487 else
24488 x_reached -= area_left;
24490 RELEASE_HDC (hdc, f);
24492 return x_reached;
24495 /* Expand row matrix if too narrow. Don't expand if area
24496 is not present. */
24498 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24500 if (!it->f->fonts_changed \
24501 && (it->glyph_row->glyphs[area] \
24502 < it->glyph_row->glyphs[area + 1])) \
24504 it->w->ncols_scale_factor++; \
24505 it->f->fonts_changed = 1; \
24509 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24510 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24512 static void
24513 append_glyph (struct it *it)
24515 struct glyph *glyph;
24516 enum glyph_row_area area = it->area;
24518 eassert (it->glyph_row);
24519 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24521 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24522 if (glyph < it->glyph_row->glyphs[area + 1])
24524 /* If the glyph row is reversed, we need to prepend the glyph
24525 rather than append it. */
24526 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24528 struct glyph *g;
24530 /* Make room for the additional glyph. */
24531 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24532 g[1] = *g;
24533 glyph = it->glyph_row->glyphs[area];
24535 glyph->charpos = CHARPOS (it->position);
24536 glyph->object = it->object;
24537 if (it->pixel_width > 0)
24539 glyph->pixel_width = it->pixel_width;
24540 glyph->padding_p = 0;
24542 else
24544 /* Assure at least 1-pixel width. Otherwise, cursor can't
24545 be displayed correctly. */
24546 glyph->pixel_width = 1;
24547 glyph->padding_p = 1;
24549 glyph->ascent = it->ascent;
24550 glyph->descent = it->descent;
24551 glyph->voffset = it->voffset;
24552 glyph->type = CHAR_GLYPH;
24553 glyph->avoid_cursor_p = it->avoid_cursor_p;
24554 glyph->multibyte_p = it->multibyte_p;
24555 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24557 /* In R2L rows, the left and the right box edges need to be
24558 drawn in reverse direction. */
24559 glyph->right_box_line_p = it->start_of_box_run_p;
24560 glyph->left_box_line_p = it->end_of_box_run_p;
24562 else
24564 glyph->left_box_line_p = it->start_of_box_run_p;
24565 glyph->right_box_line_p = it->end_of_box_run_p;
24567 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24568 || it->phys_descent > it->descent);
24569 glyph->glyph_not_available_p = it->glyph_not_available_p;
24570 glyph->face_id = it->face_id;
24571 glyph->u.ch = it->char_to_display;
24572 glyph->slice.img = null_glyph_slice;
24573 glyph->font_type = FONT_TYPE_UNKNOWN;
24574 if (it->bidi_p)
24576 glyph->resolved_level = it->bidi_it.resolved_level;
24577 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24578 emacs_abort ();
24579 glyph->bidi_type = it->bidi_it.type;
24581 else
24583 glyph->resolved_level = 0;
24584 glyph->bidi_type = UNKNOWN_BT;
24586 ++it->glyph_row->used[area];
24588 else
24589 IT_EXPAND_MATRIX_WIDTH (it, area);
24592 /* Store one glyph for the composition IT->cmp_it.id in
24593 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24594 non-null. */
24596 static void
24597 append_composite_glyph (struct it *it)
24599 struct glyph *glyph;
24600 enum glyph_row_area area = it->area;
24602 eassert (it->glyph_row);
24604 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24605 if (glyph < it->glyph_row->glyphs[area + 1])
24607 /* If the glyph row is reversed, we need to prepend the glyph
24608 rather than append it. */
24609 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24611 struct glyph *g;
24613 /* Make room for the new glyph. */
24614 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24615 g[1] = *g;
24616 glyph = it->glyph_row->glyphs[it->area];
24618 glyph->charpos = it->cmp_it.charpos;
24619 glyph->object = it->object;
24620 glyph->pixel_width = it->pixel_width;
24621 glyph->ascent = it->ascent;
24622 glyph->descent = it->descent;
24623 glyph->voffset = it->voffset;
24624 glyph->type = COMPOSITE_GLYPH;
24625 if (it->cmp_it.ch < 0)
24627 glyph->u.cmp.automatic = 0;
24628 glyph->u.cmp.id = it->cmp_it.id;
24629 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24631 else
24633 glyph->u.cmp.automatic = 1;
24634 glyph->u.cmp.id = it->cmp_it.id;
24635 glyph->slice.cmp.from = it->cmp_it.from;
24636 glyph->slice.cmp.to = it->cmp_it.to - 1;
24638 glyph->avoid_cursor_p = it->avoid_cursor_p;
24639 glyph->multibyte_p = it->multibyte_p;
24640 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24642 /* In R2L rows, the left and the right box edges need to be
24643 drawn in reverse direction. */
24644 glyph->right_box_line_p = it->start_of_box_run_p;
24645 glyph->left_box_line_p = it->end_of_box_run_p;
24647 else
24649 glyph->left_box_line_p = it->start_of_box_run_p;
24650 glyph->right_box_line_p = it->end_of_box_run_p;
24652 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24653 || it->phys_descent > it->descent);
24654 glyph->padding_p = 0;
24655 glyph->glyph_not_available_p = 0;
24656 glyph->face_id = it->face_id;
24657 glyph->font_type = FONT_TYPE_UNKNOWN;
24658 if (it->bidi_p)
24660 glyph->resolved_level = it->bidi_it.resolved_level;
24661 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24662 emacs_abort ();
24663 glyph->bidi_type = it->bidi_it.type;
24665 ++it->glyph_row->used[area];
24667 else
24668 IT_EXPAND_MATRIX_WIDTH (it, area);
24672 /* Change IT->ascent and IT->height according to the setting of
24673 IT->voffset. */
24675 static void
24676 take_vertical_position_into_account (struct it *it)
24678 if (it->voffset)
24680 if (it->voffset < 0)
24681 /* Increase the ascent so that we can display the text higher
24682 in the line. */
24683 it->ascent -= it->voffset;
24684 else
24685 /* Increase the descent so that we can display the text lower
24686 in the line. */
24687 it->descent += it->voffset;
24692 /* Produce glyphs/get display metrics for the image IT is loaded with.
24693 See the description of struct display_iterator in dispextern.h for
24694 an overview of struct display_iterator. */
24696 static void
24697 produce_image_glyph (struct it *it)
24699 struct image *img;
24700 struct face *face;
24701 int glyph_ascent, crop;
24702 struct glyph_slice slice;
24704 eassert (it->what == IT_IMAGE);
24706 face = FACE_FROM_ID (it->f, it->face_id);
24707 eassert (face);
24708 /* Make sure X resources of the face is loaded. */
24709 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24711 if (it->image_id < 0)
24713 /* Fringe bitmap. */
24714 it->ascent = it->phys_ascent = 0;
24715 it->descent = it->phys_descent = 0;
24716 it->pixel_width = 0;
24717 it->nglyphs = 0;
24718 return;
24721 img = IMAGE_FROM_ID (it->f, it->image_id);
24722 eassert (img);
24723 /* Make sure X resources of the image is loaded. */
24724 prepare_image_for_display (it->f, img);
24726 slice.x = slice.y = 0;
24727 slice.width = img->width;
24728 slice.height = img->height;
24730 if (INTEGERP (it->slice.x))
24731 slice.x = XINT (it->slice.x);
24732 else if (FLOATP (it->slice.x))
24733 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24735 if (INTEGERP (it->slice.y))
24736 slice.y = XINT (it->slice.y);
24737 else if (FLOATP (it->slice.y))
24738 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24740 if (INTEGERP (it->slice.width))
24741 slice.width = XINT (it->slice.width);
24742 else if (FLOATP (it->slice.width))
24743 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24745 if (INTEGERP (it->slice.height))
24746 slice.height = XINT (it->slice.height);
24747 else if (FLOATP (it->slice.height))
24748 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24750 if (slice.x >= img->width)
24751 slice.x = img->width;
24752 if (slice.y >= img->height)
24753 slice.y = img->height;
24754 if (slice.x + slice.width >= img->width)
24755 slice.width = img->width - slice.x;
24756 if (slice.y + slice.height > img->height)
24757 slice.height = img->height - slice.y;
24759 if (slice.width == 0 || slice.height == 0)
24760 return;
24762 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24764 it->descent = slice.height - glyph_ascent;
24765 if (slice.y == 0)
24766 it->descent += img->vmargin;
24767 if (slice.y + slice.height == img->height)
24768 it->descent += img->vmargin;
24769 it->phys_descent = it->descent;
24771 it->pixel_width = slice.width;
24772 if (slice.x == 0)
24773 it->pixel_width += img->hmargin;
24774 if (slice.x + slice.width == img->width)
24775 it->pixel_width += img->hmargin;
24777 /* It's quite possible for images to have an ascent greater than
24778 their height, so don't get confused in that case. */
24779 if (it->descent < 0)
24780 it->descent = 0;
24782 it->nglyphs = 1;
24784 if (face->box != FACE_NO_BOX)
24786 if (face->box_line_width > 0)
24788 if (slice.y == 0)
24789 it->ascent += face->box_line_width;
24790 if (slice.y + slice.height == img->height)
24791 it->descent += face->box_line_width;
24794 if (it->start_of_box_run_p && slice.x == 0)
24795 it->pixel_width += eabs (face->box_line_width);
24796 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24797 it->pixel_width += eabs (face->box_line_width);
24800 take_vertical_position_into_account (it);
24802 /* Automatically crop wide image glyphs at right edge so we can
24803 draw the cursor on same display row. */
24804 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24805 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24807 it->pixel_width -= crop;
24808 slice.width -= crop;
24811 if (it->glyph_row)
24813 struct glyph *glyph;
24814 enum glyph_row_area area = it->area;
24816 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24817 if (glyph < it->glyph_row->glyphs[area + 1])
24819 glyph->charpos = CHARPOS (it->position);
24820 glyph->object = it->object;
24821 glyph->pixel_width = it->pixel_width;
24822 glyph->ascent = glyph_ascent;
24823 glyph->descent = it->descent;
24824 glyph->voffset = it->voffset;
24825 glyph->type = IMAGE_GLYPH;
24826 glyph->avoid_cursor_p = it->avoid_cursor_p;
24827 glyph->multibyte_p = it->multibyte_p;
24828 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24830 /* In R2L rows, the left and the right box edges need to be
24831 drawn in reverse direction. */
24832 glyph->right_box_line_p = it->start_of_box_run_p;
24833 glyph->left_box_line_p = it->end_of_box_run_p;
24835 else
24837 glyph->left_box_line_p = it->start_of_box_run_p;
24838 glyph->right_box_line_p = it->end_of_box_run_p;
24840 glyph->overlaps_vertically_p = 0;
24841 glyph->padding_p = 0;
24842 glyph->glyph_not_available_p = 0;
24843 glyph->face_id = it->face_id;
24844 glyph->u.img_id = img->id;
24845 glyph->slice.img = slice;
24846 glyph->font_type = FONT_TYPE_UNKNOWN;
24847 if (it->bidi_p)
24849 glyph->resolved_level = it->bidi_it.resolved_level;
24850 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24851 emacs_abort ();
24852 glyph->bidi_type = it->bidi_it.type;
24854 ++it->glyph_row->used[area];
24856 else
24857 IT_EXPAND_MATRIX_WIDTH (it, area);
24862 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24863 of the glyph, WIDTH and HEIGHT are the width and height of the
24864 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24866 static void
24867 append_stretch_glyph (struct it *it, Lisp_Object object,
24868 int width, int height, int ascent)
24870 struct glyph *glyph;
24871 enum glyph_row_area area = it->area;
24873 eassert (ascent >= 0 && ascent <= height);
24875 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24876 if (glyph < it->glyph_row->glyphs[area + 1])
24878 /* If the glyph row is reversed, we need to prepend the glyph
24879 rather than append it. */
24880 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24882 struct glyph *g;
24884 /* Make room for the additional glyph. */
24885 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24886 g[1] = *g;
24887 glyph = it->glyph_row->glyphs[area];
24889 glyph->charpos = CHARPOS (it->position);
24890 glyph->object = object;
24891 glyph->pixel_width = width;
24892 glyph->ascent = ascent;
24893 glyph->descent = height - ascent;
24894 glyph->voffset = it->voffset;
24895 glyph->type = STRETCH_GLYPH;
24896 glyph->avoid_cursor_p = it->avoid_cursor_p;
24897 glyph->multibyte_p = it->multibyte_p;
24898 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24900 /* In R2L rows, the left and the right box edges need to be
24901 drawn in reverse direction. */
24902 glyph->right_box_line_p = it->start_of_box_run_p;
24903 glyph->left_box_line_p = it->end_of_box_run_p;
24905 else
24907 glyph->left_box_line_p = it->start_of_box_run_p;
24908 glyph->right_box_line_p = it->end_of_box_run_p;
24910 glyph->overlaps_vertically_p = 0;
24911 glyph->padding_p = 0;
24912 glyph->glyph_not_available_p = 0;
24913 glyph->face_id = it->face_id;
24914 glyph->u.stretch.ascent = ascent;
24915 glyph->u.stretch.height = height;
24916 glyph->slice.img = null_glyph_slice;
24917 glyph->font_type = FONT_TYPE_UNKNOWN;
24918 if (it->bidi_p)
24920 glyph->resolved_level = it->bidi_it.resolved_level;
24921 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24922 emacs_abort ();
24923 glyph->bidi_type = it->bidi_it.type;
24925 else
24927 glyph->resolved_level = 0;
24928 glyph->bidi_type = UNKNOWN_BT;
24930 ++it->glyph_row->used[area];
24932 else
24933 IT_EXPAND_MATRIX_WIDTH (it, area);
24936 #endif /* HAVE_WINDOW_SYSTEM */
24938 /* Produce a stretch glyph for iterator IT. IT->object is the value
24939 of the glyph property displayed. The value must be a list
24940 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24941 being recognized:
24943 1. `:width WIDTH' specifies that the space should be WIDTH *
24944 canonical char width wide. WIDTH may be an integer or floating
24945 point number.
24947 2. `:relative-width FACTOR' specifies that the width of the stretch
24948 should be computed from the width of the first character having the
24949 `glyph' property, and should be FACTOR times that width.
24951 3. `:align-to HPOS' specifies that the space should be wide enough
24952 to reach HPOS, a value in canonical character units.
24954 Exactly one of the above pairs must be present.
24956 4. `:height HEIGHT' specifies that the height of the stretch produced
24957 should be HEIGHT, measured in canonical character units.
24959 5. `:relative-height FACTOR' specifies that the height of the
24960 stretch should be FACTOR times the height of the characters having
24961 the glyph property.
24963 Either none or exactly one of 4 or 5 must be present.
24965 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24966 of the stretch should be used for the ascent of the stretch.
24967 ASCENT must be in the range 0 <= ASCENT <= 100. */
24969 void
24970 produce_stretch_glyph (struct it *it)
24972 /* (space :width WIDTH :height HEIGHT ...) */
24973 Lisp_Object prop, plist;
24974 int width = 0, height = 0, align_to = -1;
24975 int zero_width_ok_p = 0;
24976 double tem;
24977 struct font *font = NULL;
24979 #ifdef HAVE_WINDOW_SYSTEM
24980 int ascent = 0;
24981 int zero_height_ok_p = 0;
24983 if (FRAME_WINDOW_P (it->f))
24985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24986 font = face->font ? face->font : FRAME_FONT (it->f);
24987 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24989 #endif
24991 /* List should start with `space'. */
24992 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24993 plist = XCDR (it->object);
24995 /* Compute the width of the stretch. */
24996 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24997 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24999 /* Absolute width `:width WIDTH' specified and valid. */
25000 zero_width_ok_p = 1;
25001 width = (int)tem;
25003 #ifdef HAVE_WINDOW_SYSTEM
25004 else if (FRAME_WINDOW_P (it->f)
25005 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25007 /* Relative width `:relative-width FACTOR' specified and valid.
25008 Compute the width of the characters having the `glyph'
25009 property. */
25010 struct it it2;
25011 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25013 it2 = *it;
25014 if (it->multibyte_p)
25015 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25016 else
25018 it2.c = it2.char_to_display = *p, it2.len = 1;
25019 if (! ASCII_CHAR_P (it2.c))
25020 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25023 it2.glyph_row = NULL;
25024 it2.what = IT_CHARACTER;
25025 x_produce_glyphs (&it2);
25026 width = NUMVAL (prop) * it2.pixel_width;
25028 #endif /* HAVE_WINDOW_SYSTEM */
25029 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25030 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25032 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25033 align_to = (align_to < 0
25035 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25036 else if (align_to < 0)
25037 align_to = window_box_left_offset (it->w, TEXT_AREA);
25038 width = max (0, (int)tem + align_to - it->current_x);
25039 zero_width_ok_p = 1;
25041 else
25042 /* Nothing specified -> width defaults to canonical char width. */
25043 width = FRAME_COLUMN_WIDTH (it->f);
25045 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25046 width = 1;
25048 #ifdef HAVE_WINDOW_SYSTEM
25049 /* Compute height. */
25050 if (FRAME_WINDOW_P (it->f))
25052 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25053 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25055 height = (int)tem;
25056 zero_height_ok_p = 1;
25058 else if (prop = Fplist_get (plist, QCrelative_height),
25059 NUMVAL (prop) > 0)
25060 height = FONT_HEIGHT (font) * NUMVAL (prop);
25061 else
25062 height = FONT_HEIGHT (font);
25064 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25065 height = 1;
25067 /* Compute percentage of height used for ascent. If
25068 `:ascent ASCENT' is present and valid, use that. Otherwise,
25069 derive the ascent from the font in use. */
25070 if (prop = Fplist_get (plist, QCascent),
25071 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25072 ascent = height * NUMVAL (prop) / 100.0;
25073 else if (!NILP (prop)
25074 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25075 ascent = min (max (0, (int)tem), height);
25076 else
25077 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25079 else
25080 #endif /* HAVE_WINDOW_SYSTEM */
25081 height = 1;
25083 if (width > 0 && it->line_wrap != TRUNCATE
25084 && it->current_x + width > it->last_visible_x)
25086 width = it->last_visible_x - it->current_x;
25087 #ifdef HAVE_WINDOW_SYSTEM
25088 /* Subtract one more pixel from the stretch width, but only on
25089 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25090 width -= FRAME_WINDOW_P (it->f);
25091 #endif
25094 if (width > 0 && height > 0 && it->glyph_row)
25096 Lisp_Object o_object = it->object;
25097 Lisp_Object object = it->stack[it->sp - 1].string;
25098 int n = width;
25100 if (!STRINGP (object))
25101 object = it->w->contents;
25102 #ifdef HAVE_WINDOW_SYSTEM
25103 if (FRAME_WINDOW_P (it->f))
25104 append_stretch_glyph (it, object, width, height, ascent);
25105 else
25106 #endif
25108 it->object = object;
25109 it->char_to_display = ' ';
25110 it->pixel_width = it->len = 1;
25111 while (n--)
25112 tty_append_glyph (it);
25113 it->object = o_object;
25117 it->pixel_width = width;
25118 #ifdef HAVE_WINDOW_SYSTEM
25119 if (FRAME_WINDOW_P (it->f))
25121 it->ascent = it->phys_ascent = ascent;
25122 it->descent = it->phys_descent = height - it->ascent;
25123 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25124 take_vertical_position_into_account (it);
25126 else
25127 #endif
25128 it->nglyphs = width;
25131 /* Get information about special display element WHAT in an
25132 environment described by IT. WHAT is one of IT_TRUNCATION or
25133 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25134 non-null glyph_row member. This function ensures that fields like
25135 face_id, c, len of IT are left untouched. */
25137 static void
25138 produce_special_glyphs (struct it *it, enum display_element_type what)
25140 struct it temp_it;
25141 Lisp_Object gc;
25142 GLYPH glyph;
25144 temp_it = *it;
25145 temp_it.object = make_number (0);
25146 memset (&temp_it.current, 0, sizeof temp_it.current);
25148 if (what == IT_CONTINUATION)
25150 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25151 if (it->bidi_it.paragraph_dir == R2L)
25152 SET_GLYPH_FROM_CHAR (glyph, '/');
25153 else
25154 SET_GLYPH_FROM_CHAR (glyph, '\\');
25155 if (it->dp
25156 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25158 /* FIXME: Should we mirror GC for R2L lines? */
25159 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25160 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25163 else if (what == IT_TRUNCATION)
25165 /* Truncation glyph. */
25166 SET_GLYPH_FROM_CHAR (glyph, '$');
25167 if (it->dp
25168 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25170 /* FIXME: Should we mirror GC for R2L lines? */
25171 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25172 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25175 else
25176 emacs_abort ();
25178 #ifdef HAVE_WINDOW_SYSTEM
25179 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25180 is turned off, we precede the truncation/continuation glyphs by a
25181 stretch glyph whose width is computed such that these special
25182 glyphs are aligned at the window margin, even when very different
25183 fonts are used in different glyph rows. */
25184 if (FRAME_WINDOW_P (temp_it.f)
25185 /* init_iterator calls this with it->glyph_row == NULL, and it
25186 wants only the pixel width of the truncation/continuation
25187 glyphs. */
25188 && temp_it.glyph_row
25189 /* insert_left_trunc_glyphs calls us at the beginning of the
25190 row, and it has its own calculation of the stretch glyph
25191 width. */
25192 && temp_it.glyph_row->used[TEXT_AREA] > 0
25193 && (temp_it.glyph_row->reversed_p
25194 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25195 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25197 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25199 if (stretch_width > 0)
25201 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25202 struct font *font =
25203 face->font ? face->font : FRAME_FONT (temp_it.f);
25204 int stretch_ascent =
25205 (((temp_it.ascent + temp_it.descent)
25206 * FONT_BASE (font)) / FONT_HEIGHT (font));
25208 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25209 temp_it.ascent + temp_it.descent,
25210 stretch_ascent);
25213 #endif
25215 temp_it.dp = NULL;
25216 temp_it.what = IT_CHARACTER;
25217 temp_it.len = 1;
25218 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25219 temp_it.face_id = GLYPH_FACE (glyph);
25220 temp_it.len = CHAR_BYTES (temp_it.c);
25222 PRODUCE_GLYPHS (&temp_it);
25223 it->pixel_width = temp_it.pixel_width;
25224 it->nglyphs = temp_it.pixel_width;
25227 #ifdef HAVE_WINDOW_SYSTEM
25229 /* Calculate line-height and line-spacing properties.
25230 An integer value specifies explicit pixel value.
25231 A float value specifies relative value to current face height.
25232 A cons (float . face-name) specifies relative value to
25233 height of specified face font.
25235 Returns height in pixels, or nil. */
25238 static Lisp_Object
25239 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25240 int boff, int override)
25242 Lisp_Object face_name = Qnil;
25243 int ascent, descent, height;
25245 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25246 return val;
25248 if (CONSP (val))
25250 face_name = XCAR (val);
25251 val = XCDR (val);
25252 if (!NUMBERP (val))
25253 val = make_number (1);
25254 if (NILP (face_name))
25256 height = it->ascent + it->descent;
25257 goto scale;
25261 if (NILP (face_name))
25263 font = FRAME_FONT (it->f);
25264 boff = FRAME_BASELINE_OFFSET (it->f);
25266 else if (EQ (face_name, Qt))
25268 override = 0;
25270 else
25272 int face_id;
25273 struct face *face;
25275 face_id = lookup_named_face (it->f, face_name, 0);
25276 if (face_id < 0)
25277 return make_number (-1);
25279 face = FACE_FROM_ID (it->f, face_id);
25280 font = face->font;
25281 if (font == NULL)
25282 return make_number (-1);
25283 boff = font->baseline_offset;
25284 if (font->vertical_centering)
25285 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25288 ascent = FONT_BASE (font) + boff;
25289 descent = FONT_DESCENT (font) - boff;
25291 if (override)
25293 it->override_ascent = ascent;
25294 it->override_descent = descent;
25295 it->override_boff = boff;
25298 height = ascent + descent;
25300 scale:
25301 if (FLOATP (val))
25302 height = (int)(XFLOAT_DATA (val) * height);
25303 else if (INTEGERP (val))
25304 height *= XINT (val);
25306 return make_number (height);
25310 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25311 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25312 and only if this is for a character for which no font was found.
25314 If the display method (it->glyphless_method) is
25315 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25316 length of the acronym or the hexadecimal string, UPPER_XOFF and
25317 UPPER_YOFF are pixel offsets for the upper part of the string,
25318 LOWER_XOFF and LOWER_YOFF are for the lower part.
25320 For the other display methods, LEN through LOWER_YOFF are zero. */
25322 static void
25323 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25324 short upper_xoff, short upper_yoff,
25325 short lower_xoff, short lower_yoff)
25327 struct glyph *glyph;
25328 enum glyph_row_area area = it->area;
25330 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25331 if (glyph < it->glyph_row->glyphs[area + 1])
25333 /* If the glyph row is reversed, we need to prepend the glyph
25334 rather than append it. */
25335 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25337 struct glyph *g;
25339 /* Make room for the additional glyph. */
25340 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25341 g[1] = *g;
25342 glyph = it->glyph_row->glyphs[area];
25344 glyph->charpos = CHARPOS (it->position);
25345 glyph->object = it->object;
25346 glyph->pixel_width = it->pixel_width;
25347 glyph->ascent = it->ascent;
25348 glyph->descent = it->descent;
25349 glyph->voffset = it->voffset;
25350 glyph->type = GLYPHLESS_GLYPH;
25351 glyph->u.glyphless.method = it->glyphless_method;
25352 glyph->u.glyphless.for_no_font = for_no_font;
25353 glyph->u.glyphless.len = len;
25354 glyph->u.glyphless.ch = it->c;
25355 glyph->slice.glyphless.upper_xoff = upper_xoff;
25356 glyph->slice.glyphless.upper_yoff = upper_yoff;
25357 glyph->slice.glyphless.lower_xoff = lower_xoff;
25358 glyph->slice.glyphless.lower_yoff = lower_yoff;
25359 glyph->avoid_cursor_p = it->avoid_cursor_p;
25360 glyph->multibyte_p = it->multibyte_p;
25361 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25363 /* In R2L rows, the left and the right box edges need to be
25364 drawn in reverse direction. */
25365 glyph->right_box_line_p = it->start_of_box_run_p;
25366 glyph->left_box_line_p = it->end_of_box_run_p;
25368 else
25370 glyph->left_box_line_p = it->start_of_box_run_p;
25371 glyph->right_box_line_p = it->end_of_box_run_p;
25373 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25374 || it->phys_descent > it->descent);
25375 glyph->padding_p = 0;
25376 glyph->glyph_not_available_p = 0;
25377 glyph->face_id = face_id;
25378 glyph->font_type = FONT_TYPE_UNKNOWN;
25379 if (it->bidi_p)
25381 glyph->resolved_level = it->bidi_it.resolved_level;
25382 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25383 emacs_abort ();
25384 glyph->bidi_type = it->bidi_it.type;
25386 ++it->glyph_row->used[area];
25388 else
25389 IT_EXPAND_MATRIX_WIDTH (it, area);
25393 /* Produce a glyph for a glyphless character for iterator IT.
25394 IT->glyphless_method specifies which method to use for displaying
25395 the character. See the description of enum
25396 glyphless_display_method in dispextern.h for the detail.
25398 FOR_NO_FONT is nonzero if and only if this is for a character for
25399 which no font was found. ACRONYM, if non-nil, is an acronym string
25400 for the character. */
25402 static void
25403 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25405 int face_id;
25406 struct face *face;
25407 struct font *font;
25408 int base_width, base_height, width, height;
25409 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25410 int len;
25412 /* Get the metrics of the base font. We always refer to the current
25413 ASCII face. */
25414 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25415 font = face->font ? face->font : FRAME_FONT (it->f);
25416 it->ascent = FONT_BASE (font) + font->baseline_offset;
25417 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25418 base_height = it->ascent + it->descent;
25419 base_width = font->average_width;
25421 face_id = merge_glyphless_glyph_face (it);
25423 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25425 it->pixel_width = THIN_SPACE_WIDTH;
25426 len = 0;
25427 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25429 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25431 width = CHAR_WIDTH (it->c);
25432 if (width == 0)
25433 width = 1;
25434 else if (width > 4)
25435 width = 4;
25436 it->pixel_width = base_width * width;
25437 len = 0;
25438 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25440 else
25442 char buf[7];
25443 const char *str;
25444 unsigned int code[6];
25445 int upper_len;
25446 int ascent, descent;
25447 struct font_metrics metrics_upper, metrics_lower;
25449 face = FACE_FROM_ID (it->f, face_id);
25450 font = face->font ? face->font : FRAME_FONT (it->f);
25451 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25453 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25455 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25456 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25457 if (CONSP (acronym))
25458 acronym = XCAR (acronym);
25459 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25461 else
25463 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25464 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25465 str = buf;
25467 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25468 code[len] = font->driver->encode_char (font, str[len]);
25469 upper_len = (len + 1) / 2;
25470 font->driver->text_extents (font, code, upper_len,
25471 &metrics_upper);
25472 font->driver->text_extents (font, code + upper_len, len - upper_len,
25473 &metrics_lower);
25477 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25478 width = max (metrics_upper.width, metrics_lower.width) + 4;
25479 upper_xoff = upper_yoff = 2; /* the typical case */
25480 if (base_width >= width)
25482 /* Align the upper to the left, the lower to the right. */
25483 it->pixel_width = base_width;
25484 lower_xoff = base_width - 2 - metrics_lower.width;
25486 else
25488 /* Center the shorter one. */
25489 it->pixel_width = width;
25490 if (metrics_upper.width >= metrics_lower.width)
25491 lower_xoff = (width - metrics_lower.width) / 2;
25492 else
25494 /* FIXME: This code doesn't look right. It formerly was
25495 missing the "lower_xoff = 0;", which couldn't have
25496 been right since it left lower_xoff uninitialized. */
25497 lower_xoff = 0;
25498 upper_xoff = (width - metrics_upper.width) / 2;
25502 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25503 top, bottom, and between upper and lower strings. */
25504 height = (metrics_upper.ascent + metrics_upper.descent
25505 + metrics_lower.ascent + metrics_lower.descent) + 5;
25506 /* Center vertically.
25507 H:base_height, D:base_descent
25508 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25510 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25511 descent = D - H/2 + h/2;
25512 lower_yoff = descent - 2 - ld;
25513 upper_yoff = lower_yoff - la - 1 - ud; */
25514 ascent = - (it->descent - (base_height + height + 1) / 2);
25515 descent = it->descent - (base_height - height) / 2;
25516 lower_yoff = descent - 2 - metrics_lower.descent;
25517 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25518 - metrics_upper.descent);
25519 /* Don't make the height shorter than the base height. */
25520 if (height > base_height)
25522 it->ascent = ascent;
25523 it->descent = descent;
25527 it->phys_ascent = it->ascent;
25528 it->phys_descent = it->descent;
25529 if (it->glyph_row)
25530 append_glyphless_glyph (it, face_id, for_no_font, len,
25531 upper_xoff, upper_yoff,
25532 lower_xoff, lower_yoff);
25533 it->nglyphs = 1;
25534 take_vertical_position_into_account (it);
25538 /* RIF:
25539 Produce glyphs/get display metrics for the display element IT is
25540 loaded with. See the description of struct it in dispextern.h
25541 for an overview of struct it. */
25543 void
25544 x_produce_glyphs (struct it *it)
25546 int extra_line_spacing = it->extra_line_spacing;
25548 it->glyph_not_available_p = 0;
25550 if (it->what == IT_CHARACTER)
25552 XChar2b char2b;
25553 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25554 struct font *font = face->font;
25555 struct font_metrics *pcm = NULL;
25556 int boff; /* Baseline offset. */
25558 if (font == NULL)
25560 /* When no suitable font is found, display this character by
25561 the method specified in the first extra slot of
25562 Vglyphless_char_display. */
25563 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25565 eassert (it->what == IT_GLYPHLESS);
25566 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25567 goto done;
25570 boff = font->baseline_offset;
25571 if (font->vertical_centering)
25572 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25574 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25576 int stretched_p;
25578 it->nglyphs = 1;
25580 if (it->override_ascent >= 0)
25582 it->ascent = it->override_ascent;
25583 it->descent = it->override_descent;
25584 boff = it->override_boff;
25586 else
25588 it->ascent = FONT_BASE (font) + boff;
25589 it->descent = FONT_DESCENT (font) - boff;
25592 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25594 pcm = get_per_char_metric (font, &char2b);
25595 if (pcm->width == 0
25596 && pcm->rbearing == 0 && pcm->lbearing == 0)
25597 pcm = NULL;
25600 if (pcm)
25602 it->phys_ascent = pcm->ascent + boff;
25603 it->phys_descent = pcm->descent - boff;
25604 it->pixel_width = pcm->width;
25606 else
25608 it->glyph_not_available_p = 1;
25609 it->phys_ascent = it->ascent;
25610 it->phys_descent = it->descent;
25611 it->pixel_width = font->space_width;
25614 if (it->constrain_row_ascent_descent_p)
25616 if (it->descent > it->max_descent)
25618 it->ascent += it->descent - it->max_descent;
25619 it->descent = it->max_descent;
25621 if (it->ascent > it->max_ascent)
25623 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25624 it->ascent = it->max_ascent;
25626 it->phys_ascent = min (it->phys_ascent, it->ascent);
25627 it->phys_descent = min (it->phys_descent, it->descent);
25628 extra_line_spacing = 0;
25631 /* If this is a space inside a region of text with
25632 `space-width' property, change its width. */
25633 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25634 if (stretched_p)
25635 it->pixel_width *= XFLOATINT (it->space_width);
25637 /* If face has a box, add the box thickness to the character
25638 height. If character has a box line to the left and/or
25639 right, add the box line width to the character's width. */
25640 if (face->box != FACE_NO_BOX)
25642 int thick = face->box_line_width;
25644 if (thick > 0)
25646 it->ascent += thick;
25647 it->descent += thick;
25649 else
25650 thick = -thick;
25652 if (it->start_of_box_run_p)
25653 it->pixel_width += thick;
25654 if (it->end_of_box_run_p)
25655 it->pixel_width += thick;
25658 /* If face has an overline, add the height of the overline
25659 (1 pixel) and a 1 pixel margin to the character height. */
25660 if (face->overline_p)
25661 it->ascent += overline_margin;
25663 if (it->constrain_row_ascent_descent_p)
25665 if (it->ascent > it->max_ascent)
25666 it->ascent = it->max_ascent;
25667 if (it->descent > it->max_descent)
25668 it->descent = it->max_descent;
25671 take_vertical_position_into_account (it);
25673 /* If we have to actually produce glyphs, do it. */
25674 if (it->glyph_row)
25676 if (stretched_p)
25678 /* Translate a space with a `space-width' property
25679 into a stretch glyph. */
25680 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25681 / FONT_HEIGHT (font));
25682 append_stretch_glyph (it, it->object, it->pixel_width,
25683 it->ascent + it->descent, ascent);
25685 else
25686 append_glyph (it);
25688 /* If characters with lbearing or rbearing are displayed
25689 in this line, record that fact in a flag of the
25690 glyph row. This is used to optimize X output code. */
25691 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25692 it->glyph_row->contains_overlapping_glyphs_p = 1;
25694 if (! stretched_p && it->pixel_width == 0)
25695 /* We assure that all visible glyphs have at least 1-pixel
25696 width. */
25697 it->pixel_width = 1;
25699 else if (it->char_to_display == '\n')
25701 /* A newline has no width, but we need the height of the
25702 line. But if previous part of the line sets a height,
25703 don't increase that height. */
25705 Lisp_Object height;
25706 Lisp_Object total_height = Qnil;
25708 it->override_ascent = -1;
25709 it->pixel_width = 0;
25710 it->nglyphs = 0;
25712 height = get_it_property (it, Qline_height);
25713 /* Split (line-height total-height) list. */
25714 if (CONSP (height)
25715 && CONSP (XCDR (height))
25716 && NILP (XCDR (XCDR (height))))
25718 total_height = XCAR (XCDR (height));
25719 height = XCAR (height);
25721 height = calc_line_height_property (it, height, font, boff, 1);
25723 if (it->override_ascent >= 0)
25725 it->ascent = it->override_ascent;
25726 it->descent = it->override_descent;
25727 boff = it->override_boff;
25729 else
25731 it->ascent = FONT_BASE (font) + boff;
25732 it->descent = FONT_DESCENT (font) - boff;
25735 if (EQ (height, Qt))
25737 if (it->descent > it->max_descent)
25739 it->ascent += it->descent - it->max_descent;
25740 it->descent = it->max_descent;
25742 if (it->ascent > it->max_ascent)
25744 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25745 it->ascent = it->max_ascent;
25747 it->phys_ascent = min (it->phys_ascent, it->ascent);
25748 it->phys_descent = min (it->phys_descent, it->descent);
25749 it->constrain_row_ascent_descent_p = 1;
25750 extra_line_spacing = 0;
25752 else
25754 Lisp_Object spacing;
25756 it->phys_ascent = it->ascent;
25757 it->phys_descent = it->descent;
25759 if ((it->max_ascent > 0 || it->max_descent > 0)
25760 && face->box != FACE_NO_BOX
25761 && face->box_line_width > 0)
25763 it->ascent += face->box_line_width;
25764 it->descent += face->box_line_width;
25766 if (!NILP (height)
25767 && XINT (height) > it->ascent + it->descent)
25768 it->ascent = XINT (height) - it->descent;
25770 if (!NILP (total_height))
25771 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25772 else
25774 spacing = get_it_property (it, Qline_spacing);
25775 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25777 if (INTEGERP (spacing))
25779 extra_line_spacing = XINT (spacing);
25780 if (!NILP (total_height))
25781 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25785 else /* i.e. (it->char_to_display == '\t') */
25787 if (font->space_width > 0)
25789 int tab_width = it->tab_width * font->space_width;
25790 int x = it->current_x + it->continuation_lines_width;
25791 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25793 /* If the distance from the current position to the next tab
25794 stop is less than a space character width, use the
25795 tab stop after that. */
25796 if (next_tab_x - x < font->space_width)
25797 next_tab_x += tab_width;
25799 it->pixel_width = next_tab_x - x;
25800 it->nglyphs = 1;
25801 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25802 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25804 if (it->glyph_row)
25806 append_stretch_glyph (it, it->object, it->pixel_width,
25807 it->ascent + it->descent, it->ascent);
25810 else
25812 it->pixel_width = 0;
25813 it->nglyphs = 1;
25817 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25819 /* A static composition.
25821 Note: A composition is represented as one glyph in the
25822 glyph matrix. There are no padding glyphs.
25824 Important note: pixel_width, ascent, and descent are the
25825 values of what is drawn by draw_glyphs (i.e. the values of
25826 the overall glyphs composed). */
25827 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25828 int boff; /* baseline offset */
25829 struct composition *cmp = composition_table[it->cmp_it.id];
25830 int glyph_len = cmp->glyph_len;
25831 struct font *font = face->font;
25833 it->nglyphs = 1;
25835 /* If we have not yet calculated pixel size data of glyphs of
25836 the composition for the current face font, calculate them
25837 now. Theoretically, we have to check all fonts for the
25838 glyphs, but that requires much time and memory space. So,
25839 here we check only the font of the first glyph. This may
25840 lead to incorrect display, but it's very rare, and C-l
25841 (recenter-top-bottom) can correct the display anyway. */
25842 if (! cmp->font || cmp->font != font)
25844 /* Ascent and descent of the font of the first character
25845 of this composition (adjusted by baseline offset).
25846 Ascent and descent of overall glyphs should not be less
25847 than these, respectively. */
25848 int font_ascent, font_descent, font_height;
25849 /* Bounding box of the overall glyphs. */
25850 int leftmost, rightmost, lowest, highest;
25851 int lbearing, rbearing;
25852 int i, width, ascent, descent;
25853 int left_padded = 0, right_padded = 0;
25854 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25855 XChar2b char2b;
25856 struct font_metrics *pcm;
25857 int font_not_found_p;
25858 ptrdiff_t pos;
25860 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25861 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25862 break;
25863 if (glyph_len < cmp->glyph_len)
25864 right_padded = 1;
25865 for (i = 0; i < glyph_len; i++)
25867 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25868 break;
25869 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25871 if (i > 0)
25872 left_padded = 1;
25874 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25875 : IT_CHARPOS (*it));
25876 /* If no suitable font is found, use the default font. */
25877 font_not_found_p = font == NULL;
25878 if (font_not_found_p)
25880 face = face->ascii_face;
25881 font = face->font;
25883 boff = font->baseline_offset;
25884 if (font->vertical_centering)
25885 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25886 font_ascent = FONT_BASE (font) + boff;
25887 font_descent = FONT_DESCENT (font) - boff;
25888 font_height = FONT_HEIGHT (font);
25890 cmp->font = font;
25892 pcm = NULL;
25893 if (! font_not_found_p)
25895 get_char_face_and_encoding (it->f, c, it->face_id,
25896 &char2b, 0);
25897 pcm = get_per_char_metric (font, &char2b);
25900 /* Initialize the bounding box. */
25901 if (pcm)
25903 width = cmp->glyph_len > 0 ? pcm->width : 0;
25904 ascent = pcm->ascent;
25905 descent = pcm->descent;
25906 lbearing = pcm->lbearing;
25907 rbearing = pcm->rbearing;
25909 else
25911 width = cmp->glyph_len > 0 ? font->space_width : 0;
25912 ascent = FONT_BASE (font);
25913 descent = FONT_DESCENT (font);
25914 lbearing = 0;
25915 rbearing = width;
25918 rightmost = width;
25919 leftmost = 0;
25920 lowest = - descent + boff;
25921 highest = ascent + boff;
25923 if (! font_not_found_p
25924 && font->default_ascent
25925 && CHAR_TABLE_P (Vuse_default_ascent)
25926 && !NILP (Faref (Vuse_default_ascent,
25927 make_number (it->char_to_display))))
25928 highest = font->default_ascent + boff;
25930 /* Draw the first glyph at the normal position. It may be
25931 shifted to right later if some other glyphs are drawn
25932 at the left. */
25933 cmp->offsets[i * 2] = 0;
25934 cmp->offsets[i * 2 + 1] = boff;
25935 cmp->lbearing = lbearing;
25936 cmp->rbearing = rbearing;
25938 /* Set cmp->offsets for the remaining glyphs. */
25939 for (i++; i < glyph_len; i++)
25941 int left, right, btm, top;
25942 int ch = COMPOSITION_GLYPH (cmp, i);
25943 int face_id;
25944 struct face *this_face;
25946 if (ch == '\t')
25947 ch = ' ';
25948 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25949 this_face = FACE_FROM_ID (it->f, face_id);
25950 font = this_face->font;
25952 if (font == NULL)
25953 pcm = NULL;
25954 else
25956 get_char_face_and_encoding (it->f, ch, face_id,
25957 &char2b, 0);
25958 pcm = get_per_char_metric (font, &char2b);
25960 if (! pcm)
25961 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25962 else
25964 width = pcm->width;
25965 ascent = pcm->ascent;
25966 descent = pcm->descent;
25967 lbearing = pcm->lbearing;
25968 rbearing = pcm->rbearing;
25969 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25971 /* Relative composition with or without
25972 alternate chars. */
25973 left = (leftmost + rightmost - width) / 2;
25974 btm = - descent + boff;
25975 if (font->relative_compose
25976 && (! CHAR_TABLE_P (Vignore_relative_composition)
25977 || NILP (Faref (Vignore_relative_composition,
25978 make_number (ch)))))
25981 if (- descent >= font->relative_compose)
25982 /* One extra pixel between two glyphs. */
25983 btm = highest + 1;
25984 else if (ascent <= 0)
25985 /* One extra pixel between two glyphs. */
25986 btm = lowest - 1 - ascent - descent;
25989 else
25991 /* A composition rule is specified by an integer
25992 value that encodes global and new reference
25993 points (GREF and NREF). GREF and NREF are
25994 specified by numbers as below:
25996 0---1---2 -- ascent
26000 9--10--11 -- center
26002 ---3---4---5--- baseline
26004 6---7---8 -- descent
26006 int rule = COMPOSITION_RULE (cmp, i);
26007 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26009 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26010 grefx = gref % 3, nrefx = nref % 3;
26011 grefy = gref / 3, nrefy = nref / 3;
26012 if (xoff)
26013 xoff = font_height * (xoff - 128) / 256;
26014 if (yoff)
26015 yoff = font_height * (yoff - 128) / 256;
26017 left = (leftmost
26018 + grefx * (rightmost - leftmost) / 2
26019 - nrefx * width / 2
26020 + xoff);
26022 btm = ((grefy == 0 ? highest
26023 : grefy == 1 ? 0
26024 : grefy == 2 ? lowest
26025 : (highest + lowest) / 2)
26026 - (nrefy == 0 ? ascent + descent
26027 : nrefy == 1 ? descent - boff
26028 : nrefy == 2 ? 0
26029 : (ascent + descent) / 2)
26030 + yoff);
26033 cmp->offsets[i * 2] = left;
26034 cmp->offsets[i * 2 + 1] = btm + descent;
26036 /* Update the bounding box of the overall glyphs. */
26037 if (width > 0)
26039 right = left + width;
26040 if (left < leftmost)
26041 leftmost = left;
26042 if (right > rightmost)
26043 rightmost = right;
26045 top = btm + descent + ascent;
26046 if (top > highest)
26047 highest = top;
26048 if (btm < lowest)
26049 lowest = btm;
26051 if (cmp->lbearing > left + lbearing)
26052 cmp->lbearing = left + lbearing;
26053 if (cmp->rbearing < left + rbearing)
26054 cmp->rbearing = left + rbearing;
26058 /* If there are glyphs whose x-offsets are negative,
26059 shift all glyphs to the right and make all x-offsets
26060 non-negative. */
26061 if (leftmost < 0)
26063 for (i = 0; i < cmp->glyph_len; i++)
26064 cmp->offsets[i * 2] -= leftmost;
26065 rightmost -= leftmost;
26066 cmp->lbearing -= leftmost;
26067 cmp->rbearing -= leftmost;
26070 if (left_padded && cmp->lbearing < 0)
26072 for (i = 0; i < cmp->glyph_len; i++)
26073 cmp->offsets[i * 2] -= cmp->lbearing;
26074 rightmost -= cmp->lbearing;
26075 cmp->rbearing -= cmp->lbearing;
26076 cmp->lbearing = 0;
26078 if (right_padded && rightmost < cmp->rbearing)
26080 rightmost = cmp->rbearing;
26083 cmp->pixel_width = rightmost;
26084 cmp->ascent = highest;
26085 cmp->descent = - lowest;
26086 if (cmp->ascent < font_ascent)
26087 cmp->ascent = font_ascent;
26088 if (cmp->descent < font_descent)
26089 cmp->descent = font_descent;
26092 if (it->glyph_row
26093 && (cmp->lbearing < 0
26094 || cmp->rbearing > cmp->pixel_width))
26095 it->glyph_row->contains_overlapping_glyphs_p = 1;
26097 it->pixel_width = cmp->pixel_width;
26098 it->ascent = it->phys_ascent = cmp->ascent;
26099 it->descent = it->phys_descent = cmp->descent;
26100 if (face->box != FACE_NO_BOX)
26102 int thick = face->box_line_width;
26104 if (thick > 0)
26106 it->ascent += thick;
26107 it->descent += thick;
26109 else
26110 thick = - thick;
26112 if (it->start_of_box_run_p)
26113 it->pixel_width += thick;
26114 if (it->end_of_box_run_p)
26115 it->pixel_width += thick;
26118 /* If face has an overline, add the height of the overline
26119 (1 pixel) and a 1 pixel margin to the character height. */
26120 if (face->overline_p)
26121 it->ascent += overline_margin;
26123 take_vertical_position_into_account (it);
26124 if (it->ascent < 0)
26125 it->ascent = 0;
26126 if (it->descent < 0)
26127 it->descent = 0;
26129 if (it->glyph_row && cmp->glyph_len > 0)
26130 append_composite_glyph (it);
26132 else if (it->what == IT_COMPOSITION)
26134 /* A dynamic (automatic) composition. */
26135 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26136 Lisp_Object gstring;
26137 struct font_metrics metrics;
26139 it->nglyphs = 1;
26141 gstring = composition_gstring_from_id (it->cmp_it.id);
26142 it->pixel_width
26143 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26144 &metrics);
26145 if (it->glyph_row
26146 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26147 it->glyph_row->contains_overlapping_glyphs_p = 1;
26148 it->ascent = it->phys_ascent = metrics.ascent;
26149 it->descent = it->phys_descent = metrics.descent;
26150 if (face->box != FACE_NO_BOX)
26152 int thick = face->box_line_width;
26154 if (thick > 0)
26156 it->ascent += thick;
26157 it->descent += thick;
26159 else
26160 thick = - thick;
26162 if (it->start_of_box_run_p)
26163 it->pixel_width += thick;
26164 if (it->end_of_box_run_p)
26165 it->pixel_width += thick;
26167 /* If face has an overline, add the height of the overline
26168 (1 pixel) and a 1 pixel margin to the character height. */
26169 if (face->overline_p)
26170 it->ascent += overline_margin;
26171 take_vertical_position_into_account (it);
26172 if (it->ascent < 0)
26173 it->ascent = 0;
26174 if (it->descent < 0)
26175 it->descent = 0;
26177 if (it->glyph_row)
26178 append_composite_glyph (it);
26180 else if (it->what == IT_GLYPHLESS)
26181 produce_glyphless_glyph (it, 0, Qnil);
26182 else if (it->what == IT_IMAGE)
26183 produce_image_glyph (it);
26184 else if (it->what == IT_STRETCH)
26185 produce_stretch_glyph (it);
26187 done:
26188 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26189 because this isn't true for images with `:ascent 100'. */
26190 eassert (it->ascent >= 0 && it->descent >= 0);
26191 if (it->area == TEXT_AREA)
26192 it->current_x += it->pixel_width;
26194 if (extra_line_spacing > 0)
26196 it->descent += extra_line_spacing;
26197 if (extra_line_spacing > it->max_extra_line_spacing)
26198 it->max_extra_line_spacing = extra_line_spacing;
26201 it->max_ascent = max (it->max_ascent, it->ascent);
26202 it->max_descent = max (it->max_descent, it->descent);
26203 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26204 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26207 /* EXPORT for RIF:
26208 Output LEN glyphs starting at START at the nominal cursor position.
26209 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26210 being updated, and UPDATED_AREA is the area of that row being updated. */
26212 void
26213 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26214 struct glyph *start, enum glyph_row_area updated_area, int len)
26216 int x, hpos, chpos = w->phys_cursor.hpos;
26218 eassert (updated_row);
26219 /* When the window is hscrolled, cursor hpos can legitimately be out
26220 of bounds, but we draw the cursor at the corresponding window
26221 margin in that case. */
26222 if (!updated_row->reversed_p && chpos < 0)
26223 chpos = 0;
26224 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26225 chpos = updated_row->used[TEXT_AREA] - 1;
26227 block_input ();
26229 /* Write glyphs. */
26231 hpos = start - updated_row->glyphs[updated_area];
26232 x = draw_glyphs (w, w->output_cursor.x,
26233 updated_row, updated_area,
26234 hpos, hpos + len,
26235 DRAW_NORMAL_TEXT, 0);
26237 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26238 if (updated_area == TEXT_AREA
26239 && w->phys_cursor_on_p
26240 && w->phys_cursor.vpos == w->output_cursor.vpos
26241 && chpos >= hpos
26242 && chpos < hpos + len)
26243 w->phys_cursor_on_p = 0;
26245 unblock_input ();
26247 /* Advance the output cursor. */
26248 w->output_cursor.hpos += len;
26249 w->output_cursor.x = x;
26253 /* EXPORT for RIF:
26254 Insert LEN glyphs from START at the nominal cursor position. */
26256 void
26257 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26258 struct glyph *start, enum glyph_row_area updated_area, int len)
26260 struct frame *f;
26261 int line_height, shift_by_width, shifted_region_width;
26262 struct glyph_row *row;
26263 struct glyph *glyph;
26264 int frame_x, frame_y;
26265 ptrdiff_t hpos;
26267 eassert (updated_row);
26268 block_input ();
26269 f = XFRAME (WINDOW_FRAME (w));
26271 /* Get the height of the line we are in. */
26272 row = updated_row;
26273 line_height = row->height;
26275 /* Get the width of the glyphs to insert. */
26276 shift_by_width = 0;
26277 for (glyph = start; glyph < start + len; ++glyph)
26278 shift_by_width += glyph->pixel_width;
26280 /* Get the width of the region to shift right. */
26281 shifted_region_width = (window_box_width (w, updated_area)
26282 - w->output_cursor.x
26283 - shift_by_width);
26285 /* Shift right. */
26286 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26287 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26289 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26290 line_height, shift_by_width);
26292 /* Write the glyphs. */
26293 hpos = start - row->glyphs[updated_area];
26294 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26295 hpos, hpos + len,
26296 DRAW_NORMAL_TEXT, 0);
26298 /* Advance the output cursor. */
26299 w->output_cursor.hpos += len;
26300 w->output_cursor.x += shift_by_width;
26301 unblock_input ();
26305 /* EXPORT for RIF:
26306 Erase the current text line from the nominal cursor position
26307 (inclusive) to pixel column TO_X (exclusive). The idea is that
26308 everything from TO_X onward is already erased.
26310 TO_X is a pixel position relative to UPDATED_AREA of currently
26311 updated window W. TO_X == -1 means clear to the end of this area. */
26313 void
26314 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26315 enum glyph_row_area updated_area, int to_x)
26317 struct frame *f;
26318 int max_x, min_y, max_y;
26319 int from_x, from_y, to_y;
26321 eassert (updated_row);
26322 f = XFRAME (w->frame);
26324 if (updated_row->full_width_p)
26325 max_x = (WINDOW_PIXEL_WIDTH (w)
26326 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26327 else
26328 max_x = window_box_width (w, updated_area);
26329 max_y = window_text_bottom_y (w);
26331 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26332 of window. For TO_X > 0, truncate to end of drawing area. */
26333 if (to_x == 0)
26334 return;
26335 else if (to_x < 0)
26336 to_x = max_x;
26337 else
26338 to_x = min (to_x, max_x);
26340 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26342 /* Notice if the cursor will be cleared by this operation. */
26343 if (!updated_row->full_width_p)
26344 notice_overwritten_cursor (w, updated_area,
26345 w->output_cursor.x, -1,
26346 updated_row->y,
26347 MATRIX_ROW_BOTTOM_Y (updated_row));
26349 from_x = w->output_cursor.x;
26351 /* Translate to frame coordinates. */
26352 if (updated_row->full_width_p)
26354 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26355 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26357 else
26359 int area_left = window_box_left (w, updated_area);
26360 from_x += area_left;
26361 to_x += area_left;
26364 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26365 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26366 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26368 /* Prevent inadvertently clearing to end of the X window. */
26369 if (to_x > from_x && to_y > from_y)
26371 block_input ();
26372 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26373 to_x - from_x, to_y - from_y);
26374 unblock_input ();
26378 #endif /* HAVE_WINDOW_SYSTEM */
26382 /***********************************************************************
26383 Cursor types
26384 ***********************************************************************/
26386 /* Value is the internal representation of the specified cursor type
26387 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26388 of the bar cursor. */
26390 static enum text_cursor_kinds
26391 get_specified_cursor_type (Lisp_Object arg, int *width)
26393 enum text_cursor_kinds type;
26395 if (NILP (arg))
26396 return NO_CURSOR;
26398 if (EQ (arg, Qbox))
26399 return FILLED_BOX_CURSOR;
26401 if (EQ (arg, Qhollow))
26402 return HOLLOW_BOX_CURSOR;
26404 if (EQ (arg, Qbar))
26406 *width = 2;
26407 return BAR_CURSOR;
26410 if (CONSP (arg)
26411 && EQ (XCAR (arg), Qbar)
26412 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26414 *width = XINT (XCDR (arg));
26415 return BAR_CURSOR;
26418 if (EQ (arg, Qhbar))
26420 *width = 2;
26421 return HBAR_CURSOR;
26424 if (CONSP (arg)
26425 && EQ (XCAR (arg), Qhbar)
26426 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26428 *width = XINT (XCDR (arg));
26429 return HBAR_CURSOR;
26432 /* Treat anything unknown as "hollow box cursor".
26433 It was bad to signal an error; people have trouble fixing
26434 .Xdefaults with Emacs, when it has something bad in it. */
26435 type = HOLLOW_BOX_CURSOR;
26437 return type;
26440 /* Set the default cursor types for specified frame. */
26441 void
26442 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26444 int width = 1;
26445 Lisp_Object tem;
26447 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26448 FRAME_CURSOR_WIDTH (f) = width;
26450 /* By default, set up the blink-off state depending on the on-state. */
26452 tem = Fassoc (arg, Vblink_cursor_alist);
26453 if (!NILP (tem))
26455 FRAME_BLINK_OFF_CURSOR (f)
26456 = get_specified_cursor_type (XCDR (tem), &width);
26457 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26459 else
26460 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26462 /* Make sure the cursor gets redrawn. */
26463 f->cursor_type_changed = 1;
26467 #ifdef HAVE_WINDOW_SYSTEM
26469 /* Return the cursor we want to be displayed in window W. Return
26470 width of bar/hbar cursor through WIDTH arg. Return with
26471 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26472 (i.e. if the `system caret' should track this cursor).
26474 In a mini-buffer window, we want the cursor only to appear if we
26475 are reading input from this window. For the selected window, we
26476 want the cursor type given by the frame parameter or buffer local
26477 setting of cursor-type. If explicitly marked off, draw no cursor.
26478 In all other cases, we want a hollow box cursor. */
26480 static enum text_cursor_kinds
26481 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26482 int *active_cursor)
26484 struct frame *f = XFRAME (w->frame);
26485 struct buffer *b = XBUFFER (w->contents);
26486 int cursor_type = DEFAULT_CURSOR;
26487 Lisp_Object alt_cursor;
26488 int non_selected = 0;
26490 *active_cursor = 1;
26492 /* Echo area */
26493 if (cursor_in_echo_area
26494 && FRAME_HAS_MINIBUF_P (f)
26495 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26497 if (w == XWINDOW (echo_area_window))
26499 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26501 *width = FRAME_CURSOR_WIDTH (f);
26502 return FRAME_DESIRED_CURSOR (f);
26504 else
26505 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26508 *active_cursor = 0;
26509 non_selected = 1;
26512 /* Detect a nonselected window or nonselected frame. */
26513 else if (w != XWINDOW (f->selected_window)
26514 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26516 *active_cursor = 0;
26518 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26519 return NO_CURSOR;
26521 non_selected = 1;
26524 /* Never display a cursor in a window in which cursor-type is nil. */
26525 if (NILP (BVAR (b, cursor_type)))
26526 return NO_CURSOR;
26528 /* Get the normal cursor type for this window. */
26529 if (EQ (BVAR (b, cursor_type), Qt))
26531 cursor_type = FRAME_DESIRED_CURSOR (f);
26532 *width = FRAME_CURSOR_WIDTH (f);
26534 else
26535 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26537 /* Use cursor-in-non-selected-windows instead
26538 for non-selected window or frame. */
26539 if (non_selected)
26541 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26542 if (!EQ (Qt, alt_cursor))
26543 return get_specified_cursor_type (alt_cursor, width);
26544 /* t means modify the normal cursor type. */
26545 if (cursor_type == FILLED_BOX_CURSOR)
26546 cursor_type = HOLLOW_BOX_CURSOR;
26547 else if (cursor_type == BAR_CURSOR && *width > 1)
26548 --*width;
26549 return cursor_type;
26552 /* Use normal cursor if not blinked off. */
26553 if (!w->cursor_off_p)
26555 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26557 if (cursor_type == FILLED_BOX_CURSOR)
26559 /* Using a block cursor on large images can be very annoying.
26560 So use a hollow cursor for "large" images.
26561 If image is not transparent (no mask), also use hollow cursor. */
26562 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26563 if (img != NULL && IMAGEP (img->spec))
26565 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26566 where N = size of default frame font size.
26567 This should cover most of the "tiny" icons people may use. */
26568 if (!img->mask
26569 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26570 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26571 cursor_type = HOLLOW_BOX_CURSOR;
26574 else if (cursor_type != NO_CURSOR)
26576 /* Display current only supports BOX and HOLLOW cursors for images.
26577 So for now, unconditionally use a HOLLOW cursor when cursor is
26578 not a solid box cursor. */
26579 cursor_type = HOLLOW_BOX_CURSOR;
26582 return cursor_type;
26585 /* Cursor is blinked off, so determine how to "toggle" it. */
26587 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26588 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26589 return get_specified_cursor_type (XCDR (alt_cursor), width);
26591 /* Then see if frame has specified a specific blink off cursor type. */
26592 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26594 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26595 return FRAME_BLINK_OFF_CURSOR (f);
26598 #if 0
26599 /* Some people liked having a permanently visible blinking cursor,
26600 while others had very strong opinions against it. So it was
26601 decided to remove it. KFS 2003-09-03 */
26603 /* Finally perform built-in cursor blinking:
26604 filled box <-> hollow box
26605 wide [h]bar <-> narrow [h]bar
26606 narrow [h]bar <-> no cursor
26607 other type <-> no cursor */
26609 if (cursor_type == FILLED_BOX_CURSOR)
26610 return HOLLOW_BOX_CURSOR;
26612 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26614 *width = 1;
26615 return cursor_type;
26617 #endif
26619 return NO_CURSOR;
26623 /* Notice when the text cursor of window W has been completely
26624 overwritten by a drawing operation that outputs glyphs in AREA
26625 starting at X0 and ending at X1 in the line starting at Y0 and
26626 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26627 the rest of the line after X0 has been written. Y coordinates
26628 are window-relative. */
26630 static void
26631 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26632 int x0, int x1, int y0, int y1)
26634 int cx0, cx1, cy0, cy1;
26635 struct glyph_row *row;
26637 if (!w->phys_cursor_on_p)
26638 return;
26639 if (area != TEXT_AREA)
26640 return;
26642 if (w->phys_cursor.vpos < 0
26643 || w->phys_cursor.vpos >= w->current_matrix->nrows
26644 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26645 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26646 return;
26648 if (row->cursor_in_fringe_p)
26650 row->cursor_in_fringe_p = 0;
26651 draw_fringe_bitmap (w, row, row->reversed_p);
26652 w->phys_cursor_on_p = 0;
26653 return;
26656 cx0 = w->phys_cursor.x;
26657 cx1 = cx0 + w->phys_cursor_width;
26658 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26659 return;
26661 /* The cursor image will be completely removed from the
26662 screen if the output area intersects the cursor area in
26663 y-direction. When we draw in [y0 y1[, and some part of
26664 the cursor is at y < y0, that part must have been drawn
26665 before. When scrolling, the cursor is erased before
26666 actually scrolling, so we don't come here. When not
26667 scrolling, the rows above the old cursor row must have
26668 changed, and in this case these rows must have written
26669 over the cursor image.
26671 Likewise if part of the cursor is below y1, with the
26672 exception of the cursor being in the first blank row at
26673 the buffer and window end because update_text_area
26674 doesn't draw that row. (Except when it does, but
26675 that's handled in update_text_area.) */
26677 cy0 = w->phys_cursor.y;
26678 cy1 = cy0 + w->phys_cursor_height;
26679 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26680 return;
26682 w->phys_cursor_on_p = 0;
26685 #endif /* HAVE_WINDOW_SYSTEM */
26688 /************************************************************************
26689 Mouse Face
26690 ************************************************************************/
26692 #ifdef HAVE_WINDOW_SYSTEM
26694 /* EXPORT for RIF:
26695 Fix the display of area AREA of overlapping row ROW in window W
26696 with respect to the overlapping part OVERLAPS. */
26698 void
26699 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26700 enum glyph_row_area area, int overlaps)
26702 int i, x;
26704 block_input ();
26706 x = 0;
26707 for (i = 0; i < row->used[area];)
26709 if (row->glyphs[area][i].overlaps_vertically_p)
26711 int start = i, start_x = x;
26715 x += row->glyphs[area][i].pixel_width;
26716 ++i;
26718 while (i < row->used[area]
26719 && row->glyphs[area][i].overlaps_vertically_p);
26721 draw_glyphs (w, start_x, row, area,
26722 start, i,
26723 DRAW_NORMAL_TEXT, overlaps);
26725 else
26727 x += row->glyphs[area][i].pixel_width;
26728 ++i;
26732 unblock_input ();
26736 /* EXPORT:
26737 Draw the cursor glyph of window W in glyph row ROW. See the
26738 comment of draw_glyphs for the meaning of HL. */
26740 void
26741 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26742 enum draw_glyphs_face hl)
26744 /* If cursor hpos is out of bounds, don't draw garbage. This can
26745 happen in mini-buffer windows when switching between echo area
26746 glyphs and mini-buffer. */
26747 if ((row->reversed_p
26748 ? (w->phys_cursor.hpos >= 0)
26749 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26751 int on_p = w->phys_cursor_on_p;
26752 int x1;
26753 int hpos = w->phys_cursor.hpos;
26755 /* When the window is hscrolled, cursor hpos can legitimately be
26756 out of bounds, but we draw the cursor at the corresponding
26757 window margin in that case. */
26758 if (!row->reversed_p && hpos < 0)
26759 hpos = 0;
26760 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26761 hpos = row->used[TEXT_AREA] - 1;
26763 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26764 hl, 0);
26765 w->phys_cursor_on_p = on_p;
26767 if (hl == DRAW_CURSOR)
26768 w->phys_cursor_width = x1 - w->phys_cursor.x;
26769 /* When we erase the cursor, and ROW is overlapped by other
26770 rows, make sure that these overlapping parts of other rows
26771 are redrawn. */
26772 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26774 w->phys_cursor_width = x1 - w->phys_cursor.x;
26776 if (row > w->current_matrix->rows
26777 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26778 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26779 OVERLAPS_ERASED_CURSOR);
26781 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26782 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26783 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26784 OVERLAPS_ERASED_CURSOR);
26790 /* Erase the image of a cursor of window W from the screen. */
26792 #ifndef HAVE_NTGUI
26793 static
26794 #endif
26795 void
26796 erase_phys_cursor (struct window *w)
26798 struct frame *f = XFRAME (w->frame);
26799 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26800 int hpos = w->phys_cursor.hpos;
26801 int vpos = w->phys_cursor.vpos;
26802 int mouse_face_here_p = 0;
26803 struct glyph_matrix *active_glyphs = w->current_matrix;
26804 struct glyph_row *cursor_row;
26805 struct glyph *cursor_glyph;
26806 enum draw_glyphs_face hl;
26808 /* No cursor displayed or row invalidated => nothing to do on the
26809 screen. */
26810 if (w->phys_cursor_type == NO_CURSOR)
26811 goto mark_cursor_off;
26813 /* VPOS >= active_glyphs->nrows means that window has been resized.
26814 Don't bother to erase the cursor. */
26815 if (vpos >= active_glyphs->nrows)
26816 goto mark_cursor_off;
26818 /* If row containing cursor is marked invalid, there is nothing we
26819 can do. */
26820 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26821 if (!cursor_row->enabled_p)
26822 goto mark_cursor_off;
26824 /* If line spacing is > 0, old cursor may only be partially visible in
26825 window after split-window. So adjust visible height. */
26826 cursor_row->visible_height = min (cursor_row->visible_height,
26827 window_text_bottom_y (w) - cursor_row->y);
26829 /* If row is completely invisible, don't attempt to delete a cursor which
26830 isn't there. This can happen if cursor is at top of a window, and
26831 we switch to a buffer with a header line in that window. */
26832 if (cursor_row->visible_height <= 0)
26833 goto mark_cursor_off;
26835 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26836 if (cursor_row->cursor_in_fringe_p)
26838 cursor_row->cursor_in_fringe_p = 0;
26839 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26840 goto mark_cursor_off;
26843 /* This can happen when the new row is shorter than the old one.
26844 In this case, either draw_glyphs or clear_end_of_line
26845 should have cleared the cursor. Note that we wouldn't be
26846 able to erase the cursor in this case because we don't have a
26847 cursor glyph at hand. */
26848 if ((cursor_row->reversed_p
26849 ? (w->phys_cursor.hpos < 0)
26850 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26851 goto mark_cursor_off;
26853 /* When the window is hscrolled, cursor hpos can legitimately be out
26854 of bounds, but we draw the cursor at the corresponding window
26855 margin in that case. */
26856 if (!cursor_row->reversed_p && hpos < 0)
26857 hpos = 0;
26858 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26859 hpos = cursor_row->used[TEXT_AREA] - 1;
26861 /* If the cursor is in the mouse face area, redisplay that when
26862 we clear the cursor. */
26863 if (! NILP (hlinfo->mouse_face_window)
26864 && coords_in_mouse_face_p (w, hpos, vpos)
26865 /* Don't redraw the cursor's spot in mouse face if it is at the
26866 end of a line (on a newline). The cursor appears there, but
26867 mouse highlighting does not. */
26868 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26869 mouse_face_here_p = 1;
26871 /* Maybe clear the display under the cursor. */
26872 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26874 int x, y, left_x;
26875 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26876 int width;
26878 cursor_glyph = get_phys_cursor_glyph (w);
26879 if (cursor_glyph == NULL)
26880 goto mark_cursor_off;
26882 width = cursor_glyph->pixel_width;
26883 left_x = window_box_left_offset (w, TEXT_AREA);
26884 x = w->phys_cursor.x;
26885 if (x < left_x)
26886 width -= left_x - x;
26887 width = min (width, window_box_width (w, TEXT_AREA) - x);
26888 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26889 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26891 if (width > 0)
26892 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26895 /* Erase the cursor by redrawing the character underneath it. */
26896 if (mouse_face_here_p)
26897 hl = DRAW_MOUSE_FACE;
26898 else
26899 hl = DRAW_NORMAL_TEXT;
26900 draw_phys_cursor_glyph (w, cursor_row, hl);
26902 mark_cursor_off:
26903 w->phys_cursor_on_p = 0;
26904 w->phys_cursor_type = NO_CURSOR;
26908 /* EXPORT:
26909 Display or clear cursor of window W. If ON is zero, clear the
26910 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26911 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26913 void
26914 display_and_set_cursor (struct window *w, bool on,
26915 int hpos, int vpos, int x, int y)
26917 struct frame *f = XFRAME (w->frame);
26918 int new_cursor_type;
26919 int new_cursor_width;
26920 int active_cursor;
26921 struct glyph_row *glyph_row;
26922 struct glyph *glyph;
26924 /* This is pointless on invisible frames, and dangerous on garbaged
26925 windows and frames; in the latter case, the frame or window may
26926 be in the midst of changing its size, and x and y may be off the
26927 window. */
26928 if (! FRAME_VISIBLE_P (f)
26929 || FRAME_GARBAGED_P (f)
26930 || vpos >= w->current_matrix->nrows
26931 || hpos >= w->current_matrix->matrix_w)
26932 return;
26934 /* If cursor is off and we want it off, return quickly. */
26935 if (!on && !w->phys_cursor_on_p)
26936 return;
26938 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26939 /* If cursor row is not enabled, we don't really know where to
26940 display the cursor. */
26941 if (!glyph_row->enabled_p)
26943 w->phys_cursor_on_p = 0;
26944 return;
26947 glyph = NULL;
26948 if (!glyph_row->exact_window_width_line_p
26949 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26950 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26952 eassert (input_blocked_p ());
26954 /* Set new_cursor_type to the cursor we want to be displayed. */
26955 new_cursor_type = get_window_cursor_type (w, glyph,
26956 &new_cursor_width, &active_cursor);
26958 /* If cursor is currently being shown and we don't want it to be or
26959 it is in the wrong place, or the cursor type is not what we want,
26960 erase it. */
26961 if (w->phys_cursor_on_p
26962 && (!on
26963 || w->phys_cursor.x != x
26964 || w->phys_cursor.y != y
26965 || new_cursor_type != w->phys_cursor_type
26966 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26967 && new_cursor_width != w->phys_cursor_width)))
26968 erase_phys_cursor (w);
26970 /* Don't check phys_cursor_on_p here because that flag is only set
26971 to zero in some cases where we know that the cursor has been
26972 completely erased, to avoid the extra work of erasing the cursor
26973 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26974 still not be visible, or it has only been partly erased. */
26975 if (on)
26977 w->phys_cursor_ascent = glyph_row->ascent;
26978 w->phys_cursor_height = glyph_row->height;
26980 /* Set phys_cursor_.* before x_draw_.* is called because some
26981 of them may need the information. */
26982 w->phys_cursor.x = x;
26983 w->phys_cursor.y = glyph_row->y;
26984 w->phys_cursor.hpos = hpos;
26985 w->phys_cursor.vpos = vpos;
26988 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26989 new_cursor_type, new_cursor_width,
26990 on, active_cursor);
26994 /* Switch the display of W's cursor on or off, according to the value
26995 of ON. */
26997 static void
26998 update_window_cursor (struct window *w, bool on)
27000 /* Don't update cursor in windows whose frame is in the process
27001 of being deleted. */
27002 if (w->current_matrix)
27004 int hpos = w->phys_cursor.hpos;
27005 int vpos = w->phys_cursor.vpos;
27006 struct glyph_row *row;
27008 if (vpos >= w->current_matrix->nrows
27009 || hpos >= w->current_matrix->matrix_w)
27010 return;
27012 row = MATRIX_ROW (w->current_matrix, vpos);
27014 /* When the window is hscrolled, cursor hpos can legitimately be
27015 out of bounds, but we draw the cursor at the corresponding
27016 window margin in that case. */
27017 if (!row->reversed_p && hpos < 0)
27018 hpos = 0;
27019 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27020 hpos = row->used[TEXT_AREA] - 1;
27022 block_input ();
27023 display_and_set_cursor (w, on, hpos, vpos,
27024 w->phys_cursor.x, w->phys_cursor.y);
27025 unblock_input ();
27030 /* Call update_window_cursor with parameter ON_P on all leaf windows
27031 in the window tree rooted at W. */
27033 static void
27034 update_cursor_in_window_tree (struct window *w, bool on_p)
27036 while (w)
27038 if (WINDOWP (w->contents))
27039 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27040 else
27041 update_window_cursor (w, on_p);
27043 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27048 /* EXPORT:
27049 Display the cursor on window W, or clear it, according to ON_P.
27050 Don't change the cursor's position. */
27052 void
27053 x_update_cursor (struct frame *f, bool on_p)
27055 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27059 /* EXPORT:
27060 Clear the cursor of window W to background color, and mark the
27061 cursor as not shown. This is used when the text where the cursor
27062 is about to be rewritten. */
27064 void
27065 x_clear_cursor (struct window *w)
27067 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27068 update_window_cursor (w, 0);
27071 #endif /* HAVE_WINDOW_SYSTEM */
27073 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27074 and MSDOS. */
27075 static void
27076 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27077 int start_hpos, int end_hpos,
27078 enum draw_glyphs_face draw)
27080 #ifdef HAVE_WINDOW_SYSTEM
27081 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27083 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27084 return;
27086 #endif
27087 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27088 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27089 #endif
27092 /* Display the active region described by mouse_face_* according to DRAW. */
27094 static void
27095 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27097 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27098 struct frame *f = XFRAME (WINDOW_FRAME (w));
27100 if (/* If window is in the process of being destroyed, don't bother
27101 to do anything. */
27102 w->current_matrix != NULL
27103 /* Don't update mouse highlight if hidden */
27104 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27105 /* Recognize when we are called to operate on rows that don't exist
27106 anymore. This can happen when a window is split. */
27107 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27109 int phys_cursor_on_p = w->phys_cursor_on_p;
27110 struct glyph_row *row, *first, *last;
27112 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27113 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27115 for (row = first; row <= last && row->enabled_p; ++row)
27117 int start_hpos, end_hpos, start_x;
27119 /* For all but the first row, the highlight starts at column 0. */
27120 if (row == first)
27122 /* R2L rows have BEG and END in reversed order, but the
27123 screen drawing geometry is always left to right. So
27124 we need to mirror the beginning and end of the
27125 highlighted area in R2L rows. */
27126 if (!row->reversed_p)
27128 start_hpos = hlinfo->mouse_face_beg_col;
27129 start_x = hlinfo->mouse_face_beg_x;
27131 else if (row == last)
27133 start_hpos = hlinfo->mouse_face_end_col;
27134 start_x = hlinfo->mouse_face_end_x;
27136 else
27138 start_hpos = 0;
27139 start_x = 0;
27142 else if (row->reversed_p && row == last)
27144 start_hpos = hlinfo->mouse_face_end_col;
27145 start_x = hlinfo->mouse_face_end_x;
27147 else
27149 start_hpos = 0;
27150 start_x = 0;
27153 if (row == last)
27155 if (!row->reversed_p)
27156 end_hpos = hlinfo->mouse_face_end_col;
27157 else if (row == first)
27158 end_hpos = hlinfo->mouse_face_beg_col;
27159 else
27161 end_hpos = row->used[TEXT_AREA];
27162 if (draw == DRAW_NORMAL_TEXT)
27163 row->fill_line_p = 1; /* Clear to end of line */
27166 else if (row->reversed_p && row == first)
27167 end_hpos = hlinfo->mouse_face_beg_col;
27168 else
27170 end_hpos = row->used[TEXT_AREA];
27171 if (draw == DRAW_NORMAL_TEXT)
27172 row->fill_line_p = 1; /* Clear to end of line */
27175 if (end_hpos > start_hpos)
27177 draw_row_with_mouse_face (w, start_x, row,
27178 start_hpos, end_hpos, draw);
27180 row->mouse_face_p
27181 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27185 #ifdef HAVE_WINDOW_SYSTEM
27186 /* When we've written over the cursor, arrange for it to
27187 be displayed again. */
27188 if (FRAME_WINDOW_P (f)
27189 && phys_cursor_on_p && !w->phys_cursor_on_p)
27191 int hpos = w->phys_cursor.hpos;
27193 /* When the window is hscrolled, cursor hpos can legitimately be
27194 out of bounds, but we draw the cursor at the corresponding
27195 window margin in that case. */
27196 if (!row->reversed_p && hpos < 0)
27197 hpos = 0;
27198 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27199 hpos = row->used[TEXT_AREA] - 1;
27201 block_input ();
27202 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27203 w->phys_cursor.x, w->phys_cursor.y);
27204 unblock_input ();
27206 #endif /* HAVE_WINDOW_SYSTEM */
27209 #ifdef HAVE_WINDOW_SYSTEM
27210 /* Change the mouse cursor. */
27211 if (FRAME_WINDOW_P (f))
27213 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27214 if (draw == DRAW_NORMAL_TEXT
27215 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27216 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27217 else
27218 #endif
27219 if (draw == DRAW_MOUSE_FACE)
27220 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27221 else
27222 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27224 #endif /* HAVE_WINDOW_SYSTEM */
27227 /* EXPORT:
27228 Clear out the mouse-highlighted active region.
27229 Redraw it un-highlighted first. Value is non-zero if mouse
27230 face was actually drawn unhighlighted. */
27233 clear_mouse_face (Mouse_HLInfo *hlinfo)
27235 int cleared = 0;
27237 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27239 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27240 cleared = 1;
27243 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27244 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27245 hlinfo->mouse_face_window = Qnil;
27246 hlinfo->mouse_face_overlay = Qnil;
27247 return cleared;
27250 /* Return true if the coordinates HPOS and VPOS on windows W are
27251 within the mouse face on that window. */
27252 static bool
27253 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27255 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27257 /* Quickly resolve the easy cases. */
27258 if (!(WINDOWP (hlinfo->mouse_face_window)
27259 && XWINDOW (hlinfo->mouse_face_window) == w))
27260 return false;
27261 if (vpos < hlinfo->mouse_face_beg_row
27262 || vpos > hlinfo->mouse_face_end_row)
27263 return false;
27264 if (vpos > hlinfo->mouse_face_beg_row
27265 && vpos < hlinfo->mouse_face_end_row)
27266 return true;
27268 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27270 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27272 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27273 return true;
27275 else if ((vpos == hlinfo->mouse_face_beg_row
27276 && hpos >= hlinfo->mouse_face_beg_col)
27277 || (vpos == hlinfo->mouse_face_end_row
27278 && hpos < hlinfo->mouse_face_end_col))
27279 return true;
27281 else
27283 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27285 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27286 return true;
27288 else if ((vpos == hlinfo->mouse_face_beg_row
27289 && hpos <= hlinfo->mouse_face_beg_col)
27290 || (vpos == hlinfo->mouse_face_end_row
27291 && hpos > hlinfo->mouse_face_end_col))
27292 return true;
27294 return false;
27298 /* EXPORT:
27299 True if physical cursor of window W is within mouse face. */
27301 bool
27302 cursor_in_mouse_face_p (struct window *w)
27304 int hpos = w->phys_cursor.hpos;
27305 int vpos = w->phys_cursor.vpos;
27306 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27308 /* When the window is hscrolled, cursor hpos can legitimately be out
27309 of bounds, but we draw the cursor at the corresponding window
27310 margin in that case. */
27311 if (!row->reversed_p && hpos < 0)
27312 hpos = 0;
27313 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27314 hpos = row->used[TEXT_AREA] - 1;
27316 return coords_in_mouse_face_p (w, hpos, vpos);
27321 /* Find the glyph rows START_ROW and END_ROW of window W that display
27322 characters between buffer positions START_CHARPOS and END_CHARPOS
27323 (excluding END_CHARPOS). DISP_STRING is a display string that
27324 covers these buffer positions. This is similar to
27325 row_containing_pos, but is more accurate when bidi reordering makes
27326 buffer positions change non-linearly with glyph rows. */
27327 static void
27328 rows_from_pos_range (struct window *w,
27329 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27330 Lisp_Object disp_string,
27331 struct glyph_row **start, struct glyph_row **end)
27333 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27334 int last_y = window_text_bottom_y (w);
27335 struct glyph_row *row;
27337 *start = NULL;
27338 *end = NULL;
27340 while (!first->enabled_p
27341 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27342 first++;
27344 /* Find the START row. */
27345 for (row = first;
27346 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27347 row++)
27349 /* A row can potentially be the START row if the range of the
27350 characters it displays intersects the range
27351 [START_CHARPOS..END_CHARPOS). */
27352 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27353 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27354 /* See the commentary in row_containing_pos, for the
27355 explanation of the complicated way to check whether
27356 some position is beyond the end of the characters
27357 displayed by a row. */
27358 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27359 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27360 && !row->ends_at_zv_p
27361 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27362 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27363 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27364 && !row->ends_at_zv_p
27365 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27367 /* Found a candidate row. Now make sure at least one of the
27368 glyphs it displays has a charpos from the range
27369 [START_CHARPOS..END_CHARPOS).
27371 This is not obvious because bidi reordering could make
27372 buffer positions of a row be 1,2,3,102,101,100, and if we
27373 want to highlight characters in [50..60), we don't want
27374 this row, even though [50..60) does intersect [1..103),
27375 the range of character positions given by the row's start
27376 and end positions. */
27377 struct glyph *g = row->glyphs[TEXT_AREA];
27378 struct glyph *e = g + row->used[TEXT_AREA];
27380 while (g < e)
27382 if (((BUFFERP (g->object) || INTEGERP (g->object))
27383 && start_charpos <= g->charpos && g->charpos < end_charpos)
27384 /* A glyph that comes from DISP_STRING is by
27385 definition to be highlighted. */
27386 || EQ (g->object, disp_string))
27387 *start = row;
27388 g++;
27390 if (*start)
27391 break;
27395 /* Find the END row. */
27396 if (!*start
27397 /* If the last row is partially visible, start looking for END
27398 from that row, instead of starting from FIRST. */
27399 && !(row->enabled_p
27400 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27401 row = first;
27402 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27404 struct glyph_row *next = row + 1;
27405 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27407 if (!next->enabled_p
27408 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27409 /* The first row >= START whose range of displayed characters
27410 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27411 is the row END + 1. */
27412 || (start_charpos < next_start
27413 && end_charpos < next_start)
27414 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27415 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27416 && !next->ends_at_zv_p
27417 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27418 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27419 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27420 && !next->ends_at_zv_p
27421 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27423 *end = row;
27424 break;
27426 else
27428 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27429 but none of the characters it displays are in the range, it is
27430 also END + 1. */
27431 struct glyph *g = next->glyphs[TEXT_AREA];
27432 struct glyph *s = g;
27433 struct glyph *e = g + next->used[TEXT_AREA];
27435 while (g < e)
27437 if (((BUFFERP (g->object) || INTEGERP (g->object))
27438 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27439 /* If the buffer position of the first glyph in
27440 the row is equal to END_CHARPOS, it means
27441 the last character to be highlighted is the
27442 newline of ROW, and we must consider NEXT as
27443 END, not END+1. */
27444 || (((!next->reversed_p && g == s)
27445 || (next->reversed_p && g == e - 1))
27446 && (g->charpos == end_charpos
27447 /* Special case for when NEXT is an
27448 empty line at ZV. */
27449 || (g->charpos == -1
27450 && !row->ends_at_zv_p
27451 && next_start == end_charpos)))))
27452 /* A glyph that comes from DISP_STRING is by
27453 definition to be highlighted. */
27454 || EQ (g->object, disp_string))
27455 break;
27456 g++;
27458 if (g == e)
27460 *end = row;
27461 break;
27463 /* The first row that ends at ZV must be the last to be
27464 highlighted. */
27465 else if (next->ends_at_zv_p)
27467 *end = next;
27468 break;
27474 /* This function sets the mouse_face_* elements of HLINFO, assuming
27475 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27476 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27477 for the overlay or run of text properties specifying the mouse
27478 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27479 before-string and after-string that must also be highlighted.
27480 DISP_STRING, if non-nil, is a display string that may cover some
27481 or all of the highlighted text. */
27483 static void
27484 mouse_face_from_buffer_pos (Lisp_Object window,
27485 Mouse_HLInfo *hlinfo,
27486 ptrdiff_t mouse_charpos,
27487 ptrdiff_t start_charpos,
27488 ptrdiff_t end_charpos,
27489 Lisp_Object before_string,
27490 Lisp_Object after_string,
27491 Lisp_Object disp_string)
27493 struct window *w = XWINDOW (window);
27494 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27495 struct glyph_row *r1, *r2;
27496 struct glyph *glyph, *end;
27497 ptrdiff_t ignore, pos;
27498 int x;
27500 eassert (NILP (disp_string) || STRINGP (disp_string));
27501 eassert (NILP (before_string) || STRINGP (before_string));
27502 eassert (NILP (after_string) || STRINGP (after_string));
27504 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27505 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27506 if (r1 == NULL)
27507 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27508 /* If the before-string or display-string contains newlines,
27509 rows_from_pos_range skips to its last row. Move back. */
27510 if (!NILP (before_string) || !NILP (disp_string))
27512 struct glyph_row *prev;
27513 while ((prev = r1 - 1, prev >= first)
27514 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27515 && prev->used[TEXT_AREA] > 0)
27517 struct glyph *beg = prev->glyphs[TEXT_AREA];
27518 glyph = beg + prev->used[TEXT_AREA];
27519 while (--glyph >= beg && INTEGERP (glyph->object));
27520 if (glyph < beg
27521 || !(EQ (glyph->object, before_string)
27522 || EQ (glyph->object, disp_string)))
27523 break;
27524 r1 = prev;
27527 if (r2 == NULL)
27529 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27530 hlinfo->mouse_face_past_end = 1;
27532 else if (!NILP (after_string))
27534 /* If the after-string has newlines, advance to its last row. */
27535 struct glyph_row *next;
27536 struct glyph_row *last
27537 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27539 for (next = r2 + 1;
27540 next <= last
27541 && next->used[TEXT_AREA] > 0
27542 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27543 ++next)
27544 r2 = next;
27546 /* The rest of the display engine assumes that mouse_face_beg_row is
27547 either above mouse_face_end_row or identical to it. But with
27548 bidi-reordered continued lines, the row for START_CHARPOS could
27549 be below the row for END_CHARPOS. If so, swap the rows and store
27550 them in correct order. */
27551 if (r1->y > r2->y)
27553 struct glyph_row *tem = r2;
27555 r2 = r1;
27556 r1 = tem;
27559 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27560 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27562 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27563 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27564 could be anywhere in the row and in any order. The strategy
27565 below is to find the leftmost and the rightmost glyph that
27566 belongs to either of these 3 strings, or whose position is
27567 between START_CHARPOS and END_CHARPOS, and highlight all the
27568 glyphs between those two. This may cover more than just the text
27569 between START_CHARPOS and END_CHARPOS if the range of characters
27570 strides the bidi level boundary, e.g. if the beginning is in R2L
27571 text while the end is in L2R text or vice versa. */
27572 if (!r1->reversed_p)
27574 /* This row is in a left to right paragraph. Scan it left to
27575 right. */
27576 glyph = r1->glyphs[TEXT_AREA];
27577 end = glyph + r1->used[TEXT_AREA];
27578 x = r1->x;
27580 /* Skip truncation glyphs at the start of the glyph row. */
27581 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27582 for (; glyph < end
27583 && INTEGERP (glyph->object)
27584 && glyph->charpos < 0;
27585 ++glyph)
27586 x += glyph->pixel_width;
27588 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27589 or DISP_STRING, and the first glyph from buffer whose
27590 position is between START_CHARPOS and END_CHARPOS. */
27591 for (; glyph < end
27592 && !INTEGERP (glyph->object)
27593 && !EQ (glyph->object, disp_string)
27594 && !(BUFFERP (glyph->object)
27595 && (glyph->charpos >= start_charpos
27596 && glyph->charpos < end_charpos));
27597 ++glyph)
27599 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27600 are present at buffer positions between START_CHARPOS and
27601 END_CHARPOS, or if they come from an overlay. */
27602 if (EQ (glyph->object, before_string))
27604 pos = string_buffer_position (before_string,
27605 start_charpos);
27606 /* If pos == 0, it means before_string came from an
27607 overlay, not from a buffer position. */
27608 if (!pos || (pos >= start_charpos && pos < end_charpos))
27609 break;
27611 else if (EQ (glyph->object, after_string))
27613 pos = string_buffer_position (after_string, end_charpos);
27614 if (!pos || (pos >= start_charpos && pos < end_charpos))
27615 break;
27617 x += glyph->pixel_width;
27619 hlinfo->mouse_face_beg_x = x;
27620 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27622 else
27624 /* This row is in a right to left paragraph. Scan it right to
27625 left. */
27626 struct glyph *g;
27628 end = r1->glyphs[TEXT_AREA] - 1;
27629 glyph = end + r1->used[TEXT_AREA];
27631 /* Skip truncation glyphs at the start of the glyph row. */
27632 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27633 for (; glyph > end
27634 && INTEGERP (glyph->object)
27635 && glyph->charpos < 0;
27636 --glyph)
27639 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27640 or DISP_STRING, and the first glyph from buffer whose
27641 position is between START_CHARPOS and END_CHARPOS. */
27642 for (; glyph > end
27643 && !INTEGERP (glyph->object)
27644 && !EQ (glyph->object, disp_string)
27645 && !(BUFFERP (glyph->object)
27646 && (glyph->charpos >= start_charpos
27647 && glyph->charpos < end_charpos));
27648 --glyph)
27650 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27651 are present at buffer positions between START_CHARPOS and
27652 END_CHARPOS, or if they come from an overlay. */
27653 if (EQ (glyph->object, before_string))
27655 pos = string_buffer_position (before_string, start_charpos);
27656 /* If pos == 0, it means before_string came from an
27657 overlay, not from a buffer position. */
27658 if (!pos || (pos >= start_charpos && pos < end_charpos))
27659 break;
27661 else if (EQ (glyph->object, after_string))
27663 pos = string_buffer_position (after_string, end_charpos);
27664 if (!pos || (pos >= start_charpos && pos < end_charpos))
27665 break;
27669 glyph++; /* first glyph to the right of the highlighted area */
27670 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27671 x += g->pixel_width;
27672 hlinfo->mouse_face_beg_x = x;
27673 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27676 /* If the highlight ends in a different row, compute GLYPH and END
27677 for the end row. Otherwise, reuse the values computed above for
27678 the row where the highlight begins. */
27679 if (r2 != r1)
27681 if (!r2->reversed_p)
27683 glyph = r2->glyphs[TEXT_AREA];
27684 end = glyph + r2->used[TEXT_AREA];
27685 x = r2->x;
27687 else
27689 end = r2->glyphs[TEXT_AREA] - 1;
27690 glyph = end + r2->used[TEXT_AREA];
27694 if (!r2->reversed_p)
27696 /* Skip truncation and continuation glyphs near the end of the
27697 row, and also blanks and stretch glyphs inserted by
27698 extend_face_to_end_of_line. */
27699 while (end > glyph
27700 && INTEGERP ((end - 1)->object))
27701 --end;
27702 /* Scan the rest of the glyph row from the end, looking for the
27703 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27704 DISP_STRING, or whose position is between START_CHARPOS
27705 and END_CHARPOS */
27706 for (--end;
27707 end > glyph
27708 && !INTEGERP (end->object)
27709 && !EQ (end->object, disp_string)
27710 && !(BUFFERP (end->object)
27711 && (end->charpos >= start_charpos
27712 && end->charpos < end_charpos));
27713 --end)
27715 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27716 are present at buffer positions between START_CHARPOS and
27717 END_CHARPOS, or if they come from an overlay. */
27718 if (EQ (end->object, before_string))
27720 pos = string_buffer_position (before_string, start_charpos);
27721 if (!pos || (pos >= start_charpos && pos < end_charpos))
27722 break;
27724 else if (EQ (end->object, after_string))
27726 pos = string_buffer_position (after_string, end_charpos);
27727 if (!pos || (pos >= start_charpos && pos < end_charpos))
27728 break;
27731 /* Find the X coordinate of the last glyph to be highlighted. */
27732 for (; glyph <= end; ++glyph)
27733 x += glyph->pixel_width;
27735 hlinfo->mouse_face_end_x = x;
27736 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27738 else
27740 /* Skip truncation and continuation glyphs near the end of the
27741 row, and also blanks and stretch glyphs inserted by
27742 extend_face_to_end_of_line. */
27743 x = r2->x;
27744 end++;
27745 while (end < glyph
27746 && INTEGERP (end->object))
27748 x += end->pixel_width;
27749 ++end;
27751 /* Scan the rest of the glyph row from the end, looking for the
27752 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27753 DISP_STRING, or whose position is between START_CHARPOS
27754 and END_CHARPOS */
27755 for ( ;
27756 end < glyph
27757 && !INTEGERP (end->object)
27758 && !EQ (end->object, disp_string)
27759 && !(BUFFERP (end->object)
27760 && (end->charpos >= start_charpos
27761 && end->charpos < end_charpos));
27762 ++end)
27764 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27765 are present at buffer positions between START_CHARPOS and
27766 END_CHARPOS, or if they come from an overlay. */
27767 if (EQ (end->object, before_string))
27769 pos = string_buffer_position (before_string, start_charpos);
27770 if (!pos || (pos >= start_charpos && pos < end_charpos))
27771 break;
27773 else if (EQ (end->object, after_string))
27775 pos = string_buffer_position (after_string, end_charpos);
27776 if (!pos || (pos >= start_charpos && pos < end_charpos))
27777 break;
27779 x += end->pixel_width;
27781 /* If we exited the above loop because we arrived at the last
27782 glyph of the row, and its buffer position is still not in
27783 range, it means the last character in range is the preceding
27784 newline. Bump the end column and x values to get past the
27785 last glyph. */
27786 if (end == glyph
27787 && BUFFERP (end->object)
27788 && (end->charpos < start_charpos
27789 || end->charpos >= end_charpos))
27791 x += end->pixel_width;
27792 ++end;
27794 hlinfo->mouse_face_end_x = x;
27795 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27798 hlinfo->mouse_face_window = window;
27799 hlinfo->mouse_face_face_id
27800 = face_at_buffer_position (w, mouse_charpos, &ignore,
27801 mouse_charpos + 1,
27802 !hlinfo->mouse_face_hidden, -1);
27803 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27806 /* The following function is not used anymore (replaced with
27807 mouse_face_from_string_pos), but I leave it here for the time
27808 being, in case someone would. */
27810 #if 0 /* not used */
27812 /* Find the position of the glyph for position POS in OBJECT in
27813 window W's current matrix, and return in *X, *Y the pixel
27814 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27816 RIGHT_P non-zero means return the position of the right edge of the
27817 glyph, RIGHT_P zero means return the left edge position.
27819 If no glyph for POS exists in the matrix, return the position of
27820 the glyph with the next smaller position that is in the matrix, if
27821 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27822 exists in the matrix, return the position of the glyph with the
27823 next larger position in OBJECT.
27825 Value is non-zero if a glyph was found. */
27827 static int
27828 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27829 int *hpos, int *vpos, int *x, int *y, int right_p)
27831 int yb = window_text_bottom_y (w);
27832 struct glyph_row *r;
27833 struct glyph *best_glyph = NULL;
27834 struct glyph_row *best_row = NULL;
27835 int best_x = 0;
27837 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27838 r->enabled_p && r->y < yb;
27839 ++r)
27841 struct glyph *g = r->glyphs[TEXT_AREA];
27842 struct glyph *e = g + r->used[TEXT_AREA];
27843 int gx;
27845 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27846 if (EQ (g->object, object))
27848 if (g->charpos == pos)
27850 best_glyph = g;
27851 best_x = gx;
27852 best_row = r;
27853 goto found;
27855 else if (best_glyph == NULL
27856 || ((eabs (g->charpos - pos)
27857 < eabs (best_glyph->charpos - pos))
27858 && (right_p
27859 ? g->charpos < pos
27860 : g->charpos > pos)))
27862 best_glyph = g;
27863 best_x = gx;
27864 best_row = r;
27869 found:
27871 if (best_glyph)
27873 *x = best_x;
27874 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27876 if (right_p)
27878 *x += best_glyph->pixel_width;
27879 ++*hpos;
27882 *y = best_row->y;
27883 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27886 return best_glyph != NULL;
27888 #endif /* not used */
27890 /* Find the positions of the first and the last glyphs in window W's
27891 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27892 (assumed to be a string), and return in HLINFO's mouse_face_*
27893 members the pixel and column/row coordinates of those glyphs. */
27895 static void
27896 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27897 Lisp_Object object,
27898 ptrdiff_t startpos, ptrdiff_t endpos)
27900 int yb = window_text_bottom_y (w);
27901 struct glyph_row *r;
27902 struct glyph *g, *e;
27903 int gx;
27904 int found = 0;
27906 /* Find the glyph row with at least one position in the range
27907 [STARTPOS..ENDPOS), and the first glyph in that row whose
27908 position belongs to that range. */
27909 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27910 r->enabled_p && r->y < yb;
27911 ++r)
27913 if (!r->reversed_p)
27915 g = r->glyphs[TEXT_AREA];
27916 e = g + r->used[TEXT_AREA];
27917 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27918 if (EQ (g->object, object)
27919 && startpos <= g->charpos && g->charpos < endpos)
27921 hlinfo->mouse_face_beg_row
27922 = MATRIX_ROW_VPOS (r, w->current_matrix);
27923 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27924 hlinfo->mouse_face_beg_x = gx;
27925 found = 1;
27926 break;
27929 else
27931 struct glyph *g1;
27933 e = r->glyphs[TEXT_AREA];
27934 g = e + r->used[TEXT_AREA];
27935 for ( ; g > e; --g)
27936 if (EQ ((g-1)->object, object)
27937 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27939 hlinfo->mouse_face_beg_row
27940 = MATRIX_ROW_VPOS (r, w->current_matrix);
27941 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27942 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27943 gx += g1->pixel_width;
27944 hlinfo->mouse_face_beg_x = gx;
27945 found = 1;
27946 break;
27949 if (found)
27950 break;
27953 if (!found)
27954 return;
27956 /* Starting with the next row, look for the first row which does NOT
27957 include any glyphs whose positions are in the range. */
27958 for (++r; r->enabled_p && r->y < yb; ++r)
27960 g = r->glyphs[TEXT_AREA];
27961 e = g + r->used[TEXT_AREA];
27962 found = 0;
27963 for ( ; g < e; ++g)
27964 if (EQ (g->object, object)
27965 && startpos <= g->charpos && g->charpos < endpos)
27967 found = 1;
27968 break;
27970 if (!found)
27971 break;
27974 /* The highlighted region ends on the previous row. */
27975 r--;
27977 /* Set the end row. */
27978 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27980 /* Compute and set the end column and the end column's horizontal
27981 pixel coordinate. */
27982 if (!r->reversed_p)
27984 g = r->glyphs[TEXT_AREA];
27985 e = g + r->used[TEXT_AREA];
27986 for ( ; e > g; --e)
27987 if (EQ ((e-1)->object, object)
27988 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27989 break;
27990 hlinfo->mouse_face_end_col = e - g;
27992 for (gx = r->x; g < e; ++g)
27993 gx += g->pixel_width;
27994 hlinfo->mouse_face_end_x = gx;
27996 else
27998 e = r->glyphs[TEXT_AREA];
27999 g = e + r->used[TEXT_AREA];
28000 for (gx = r->x ; e < g; ++e)
28002 if (EQ (e->object, object)
28003 && startpos <= e->charpos && e->charpos < endpos)
28004 break;
28005 gx += e->pixel_width;
28007 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28008 hlinfo->mouse_face_end_x = gx;
28012 #ifdef HAVE_WINDOW_SYSTEM
28014 /* See if position X, Y is within a hot-spot of an image. */
28016 static int
28017 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28019 if (!CONSP (hot_spot))
28020 return 0;
28022 if (EQ (XCAR (hot_spot), Qrect))
28024 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28025 Lisp_Object rect = XCDR (hot_spot);
28026 Lisp_Object tem;
28027 if (!CONSP (rect))
28028 return 0;
28029 if (!CONSP (XCAR (rect)))
28030 return 0;
28031 if (!CONSP (XCDR (rect)))
28032 return 0;
28033 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28034 return 0;
28035 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28036 return 0;
28037 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28038 return 0;
28039 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28040 return 0;
28041 return 1;
28043 else if (EQ (XCAR (hot_spot), Qcircle))
28045 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28046 Lisp_Object circ = XCDR (hot_spot);
28047 Lisp_Object lr, lx0, ly0;
28048 if (CONSP (circ)
28049 && CONSP (XCAR (circ))
28050 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28051 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28052 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28054 double r = XFLOATINT (lr);
28055 double dx = XINT (lx0) - x;
28056 double dy = XINT (ly0) - y;
28057 return (dx * dx + dy * dy <= r * r);
28060 else if (EQ (XCAR (hot_spot), Qpoly))
28062 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28063 if (VECTORP (XCDR (hot_spot)))
28065 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28066 Lisp_Object *poly = v->contents;
28067 ptrdiff_t n = v->header.size;
28068 ptrdiff_t i;
28069 int inside = 0;
28070 Lisp_Object lx, ly;
28071 int x0, y0;
28073 /* Need an even number of coordinates, and at least 3 edges. */
28074 if (n < 6 || n & 1)
28075 return 0;
28077 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28078 If count is odd, we are inside polygon. Pixels on edges
28079 may or may not be included depending on actual geometry of the
28080 polygon. */
28081 if ((lx = poly[n-2], !INTEGERP (lx))
28082 || (ly = poly[n-1], !INTEGERP (lx)))
28083 return 0;
28084 x0 = XINT (lx), y0 = XINT (ly);
28085 for (i = 0; i < n; i += 2)
28087 int x1 = x0, y1 = y0;
28088 if ((lx = poly[i], !INTEGERP (lx))
28089 || (ly = poly[i+1], !INTEGERP (ly)))
28090 return 0;
28091 x0 = XINT (lx), y0 = XINT (ly);
28093 /* Does this segment cross the X line? */
28094 if (x0 >= x)
28096 if (x1 >= x)
28097 continue;
28099 else if (x1 < x)
28100 continue;
28101 if (y > y0 && y > y1)
28102 continue;
28103 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28104 inside = !inside;
28106 return inside;
28109 return 0;
28112 Lisp_Object
28113 find_hot_spot (Lisp_Object map, int x, int y)
28115 while (CONSP (map))
28117 if (CONSP (XCAR (map))
28118 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28119 return XCAR (map);
28120 map = XCDR (map);
28123 return Qnil;
28126 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28127 3, 3, 0,
28128 doc: /* Lookup in image map MAP coordinates X and Y.
28129 An image map is an alist where each element has the format (AREA ID PLIST).
28130 An AREA is specified as either a rectangle, a circle, or a polygon:
28131 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28132 pixel coordinates of the upper left and bottom right corners.
28133 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28134 and the radius of the circle; r may be a float or integer.
28135 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28136 vector describes one corner in the polygon.
28137 Returns the alist element for the first matching AREA in MAP. */)
28138 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28140 if (NILP (map))
28141 return Qnil;
28143 CHECK_NUMBER (x);
28144 CHECK_NUMBER (y);
28146 return find_hot_spot (map,
28147 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28148 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28152 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28153 static void
28154 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28156 /* Do not change cursor shape while dragging mouse. */
28157 if (!NILP (do_mouse_tracking))
28158 return;
28160 if (!NILP (pointer))
28162 if (EQ (pointer, Qarrow))
28163 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28164 else if (EQ (pointer, Qhand))
28165 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28166 else if (EQ (pointer, Qtext))
28167 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28168 else if (EQ (pointer, intern ("hdrag")))
28169 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28170 else if (EQ (pointer, intern ("nhdrag")))
28171 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28172 #ifdef HAVE_X_WINDOWS
28173 else if (EQ (pointer, intern ("vdrag")))
28174 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28175 #endif
28176 else if (EQ (pointer, intern ("hourglass")))
28177 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28178 else if (EQ (pointer, Qmodeline))
28179 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28180 else
28181 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28184 if (cursor != No_Cursor)
28185 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28188 #endif /* HAVE_WINDOW_SYSTEM */
28190 /* Take proper action when mouse has moved to the mode or header line
28191 or marginal area AREA of window W, x-position X and y-position Y.
28192 X is relative to the start of the text display area of W, so the
28193 width of bitmap areas and scroll bars must be subtracted to get a
28194 position relative to the start of the mode line. */
28196 static void
28197 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28198 enum window_part area)
28200 struct window *w = XWINDOW (window);
28201 struct frame *f = XFRAME (w->frame);
28202 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28203 #ifdef HAVE_WINDOW_SYSTEM
28204 Display_Info *dpyinfo;
28205 #endif
28206 Cursor cursor = No_Cursor;
28207 Lisp_Object pointer = Qnil;
28208 int dx, dy, width, height;
28209 ptrdiff_t charpos;
28210 Lisp_Object string, object = Qnil;
28211 Lisp_Object pos IF_LINT (= Qnil), help;
28213 Lisp_Object mouse_face;
28214 int original_x_pixel = x;
28215 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28216 struct glyph_row *row IF_LINT (= 0);
28218 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28220 int x0;
28221 struct glyph *end;
28223 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28224 returns them in row/column units! */
28225 string = mode_line_string (w, area, &x, &y, &charpos,
28226 &object, &dx, &dy, &width, &height);
28228 row = (area == ON_MODE_LINE
28229 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28230 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28232 /* Find the glyph under the mouse pointer. */
28233 if (row->mode_line_p && row->enabled_p)
28235 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28236 end = glyph + row->used[TEXT_AREA];
28238 for (x0 = original_x_pixel;
28239 glyph < end && x0 >= glyph->pixel_width;
28240 ++glyph)
28241 x0 -= glyph->pixel_width;
28243 if (glyph >= end)
28244 glyph = NULL;
28247 else
28249 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28250 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28251 returns them in row/column units! */
28252 string = marginal_area_string (w, area, &x, &y, &charpos,
28253 &object, &dx, &dy, &width, &height);
28256 help = Qnil;
28258 #ifdef HAVE_WINDOW_SYSTEM
28259 if (IMAGEP (object))
28261 Lisp_Object image_map, hotspot;
28262 if ((image_map = Fplist_get (XCDR (object), QCmap),
28263 !NILP (image_map))
28264 && (hotspot = find_hot_spot (image_map, dx, dy),
28265 CONSP (hotspot))
28266 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28268 Lisp_Object plist;
28270 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28271 If so, we could look for mouse-enter, mouse-leave
28272 properties in PLIST (and do something...). */
28273 hotspot = XCDR (hotspot);
28274 if (CONSP (hotspot)
28275 && (plist = XCAR (hotspot), CONSP (plist)))
28277 pointer = Fplist_get (plist, Qpointer);
28278 if (NILP (pointer))
28279 pointer = Qhand;
28280 help = Fplist_get (plist, Qhelp_echo);
28281 if (!NILP (help))
28283 help_echo_string = help;
28284 XSETWINDOW (help_echo_window, w);
28285 help_echo_object = w->contents;
28286 help_echo_pos = charpos;
28290 if (NILP (pointer))
28291 pointer = Fplist_get (XCDR (object), QCpointer);
28293 #endif /* HAVE_WINDOW_SYSTEM */
28295 if (STRINGP (string))
28296 pos = make_number (charpos);
28298 /* Set the help text and mouse pointer. If the mouse is on a part
28299 of the mode line without any text (e.g. past the right edge of
28300 the mode line text), use the default help text and pointer. */
28301 if (STRINGP (string) || area == ON_MODE_LINE)
28303 /* Arrange to display the help by setting the global variables
28304 help_echo_string, help_echo_object, and help_echo_pos. */
28305 if (NILP (help))
28307 if (STRINGP (string))
28308 help = Fget_text_property (pos, Qhelp_echo, string);
28310 if (!NILP (help))
28312 help_echo_string = help;
28313 XSETWINDOW (help_echo_window, w);
28314 help_echo_object = string;
28315 help_echo_pos = charpos;
28317 else if (area == ON_MODE_LINE)
28319 Lisp_Object default_help
28320 = buffer_local_value_1 (Qmode_line_default_help_echo,
28321 w->contents);
28323 if (STRINGP (default_help))
28325 help_echo_string = default_help;
28326 XSETWINDOW (help_echo_window, w);
28327 help_echo_object = Qnil;
28328 help_echo_pos = -1;
28333 #ifdef HAVE_WINDOW_SYSTEM
28334 /* Change the mouse pointer according to what is under it. */
28335 if (FRAME_WINDOW_P (f))
28337 dpyinfo = FRAME_DISPLAY_INFO (f);
28338 if (STRINGP (string))
28340 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28342 if (NILP (pointer))
28343 pointer = Fget_text_property (pos, Qpointer, string);
28345 /* Change the mouse pointer according to what is under X/Y. */
28346 if (NILP (pointer)
28347 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28349 Lisp_Object map;
28350 map = Fget_text_property (pos, Qlocal_map, string);
28351 if (!KEYMAPP (map))
28352 map = Fget_text_property (pos, Qkeymap, string);
28353 if (!KEYMAPP (map))
28354 cursor = dpyinfo->vertical_scroll_bar_cursor;
28357 else
28358 /* Default mode-line pointer. */
28359 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28361 #endif
28364 /* Change the mouse face according to what is under X/Y. */
28365 if (STRINGP (string))
28367 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28368 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28369 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28370 && glyph)
28372 Lisp_Object b, e;
28374 struct glyph * tmp_glyph;
28376 int gpos;
28377 int gseq_length;
28378 int total_pixel_width;
28379 ptrdiff_t begpos, endpos, ignore;
28381 int vpos, hpos;
28383 b = Fprevious_single_property_change (make_number (charpos + 1),
28384 Qmouse_face, string, Qnil);
28385 if (NILP (b))
28386 begpos = 0;
28387 else
28388 begpos = XINT (b);
28390 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28391 if (NILP (e))
28392 endpos = SCHARS (string);
28393 else
28394 endpos = XINT (e);
28396 /* Calculate the glyph position GPOS of GLYPH in the
28397 displayed string, relative to the beginning of the
28398 highlighted part of the string.
28400 Note: GPOS is different from CHARPOS. CHARPOS is the
28401 position of GLYPH in the internal string object. A mode
28402 line string format has structures which are converted to
28403 a flattened string by the Emacs Lisp interpreter. The
28404 internal string is an element of those structures. The
28405 displayed string is the flattened string. */
28406 tmp_glyph = row_start_glyph;
28407 while (tmp_glyph < glyph
28408 && (!(EQ (tmp_glyph->object, glyph->object)
28409 && begpos <= tmp_glyph->charpos
28410 && tmp_glyph->charpos < endpos)))
28411 tmp_glyph++;
28412 gpos = glyph - tmp_glyph;
28414 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28415 the highlighted part of the displayed string to which
28416 GLYPH belongs. Note: GSEQ_LENGTH is different from
28417 SCHARS (STRING), because the latter returns the length of
28418 the internal string. */
28419 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28420 tmp_glyph > glyph
28421 && (!(EQ (tmp_glyph->object, glyph->object)
28422 && begpos <= tmp_glyph->charpos
28423 && tmp_glyph->charpos < endpos));
28424 tmp_glyph--)
28426 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28428 /* Calculate the total pixel width of all the glyphs between
28429 the beginning of the highlighted area and GLYPH. */
28430 total_pixel_width = 0;
28431 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28432 total_pixel_width += tmp_glyph->pixel_width;
28434 /* Pre calculation of re-rendering position. Note: X is in
28435 column units here, after the call to mode_line_string or
28436 marginal_area_string. */
28437 hpos = x - gpos;
28438 vpos = (area == ON_MODE_LINE
28439 ? (w->current_matrix)->nrows - 1
28440 : 0);
28442 /* If GLYPH's position is included in the region that is
28443 already drawn in mouse face, we have nothing to do. */
28444 if ( EQ (window, hlinfo->mouse_face_window)
28445 && (!row->reversed_p
28446 ? (hlinfo->mouse_face_beg_col <= hpos
28447 && hpos < hlinfo->mouse_face_end_col)
28448 /* In R2L rows we swap BEG and END, see below. */
28449 : (hlinfo->mouse_face_end_col <= hpos
28450 && hpos < hlinfo->mouse_face_beg_col))
28451 && hlinfo->mouse_face_beg_row == vpos )
28452 return;
28454 if (clear_mouse_face (hlinfo))
28455 cursor = No_Cursor;
28457 if (!row->reversed_p)
28459 hlinfo->mouse_face_beg_col = hpos;
28460 hlinfo->mouse_face_beg_x = original_x_pixel
28461 - (total_pixel_width + dx);
28462 hlinfo->mouse_face_end_col = hpos + gseq_length;
28463 hlinfo->mouse_face_end_x = 0;
28465 else
28467 /* In R2L rows, show_mouse_face expects BEG and END
28468 coordinates to be swapped. */
28469 hlinfo->mouse_face_end_col = hpos;
28470 hlinfo->mouse_face_end_x = original_x_pixel
28471 - (total_pixel_width + dx);
28472 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28473 hlinfo->mouse_face_beg_x = 0;
28476 hlinfo->mouse_face_beg_row = vpos;
28477 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28478 hlinfo->mouse_face_past_end = 0;
28479 hlinfo->mouse_face_window = window;
28481 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28482 charpos,
28483 0, &ignore,
28484 glyph->face_id,
28486 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28488 if (NILP (pointer))
28489 pointer = Qhand;
28491 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28492 clear_mouse_face (hlinfo);
28494 #ifdef HAVE_WINDOW_SYSTEM
28495 if (FRAME_WINDOW_P (f))
28496 define_frame_cursor1 (f, cursor, pointer);
28497 #endif
28501 /* EXPORT:
28502 Take proper action when the mouse has moved to position X, Y on
28503 frame F with regards to highlighting portions of display that have
28504 mouse-face properties. Also de-highlight portions of display where
28505 the mouse was before, set the mouse pointer shape as appropriate
28506 for the mouse coordinates, and activate help echo (tooltips).
28507 X and Y can be negative or out of range. */
28509 void
28510 note_mouse_highlight (struct frame *f, int x, int y)
28512 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28513 enum window_part part = ON_NOTHING;
28514 Lisp_Object window;
28515 struct window *w;
28516 Cursor cursor = No_Cursor;
28517 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28518 struct buffer *b;
28520 /* When a menu is active, don't highlight because this looks odd. */
28521 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28522 if (popup_activated ())
28523 return;
28524 #endif
28526 if (!f->glyphs_initialized_p
28527 || f->pointer_invisible)
28528 return;
28530 hlinfo->mouse_face_mouse_x = x;
28531 hlinfo->mouse_face_mouse_y = y;
28532 hlinfo->mouse_face_mouse_frame = f;
28534 if (hlinfo->mouse_face_defer)
28535 return;
28537 /* Which window is that in? */
28538 window = window_from_coordinates (f, x, y, &part, 1);
28540 /* If displaying active text in another window, clear that. */
28541 if (! EQ (window, hlinfo->mouse_face_window)
28542 /* Also clear if we move out of text area in same window. */
28543 || (!NILP (hlinfo->mouse_face_window)
28544 && !NILP (window)
28545 && part != ON_TEXT
28546 && part != ON_MODE_LINE
28547 && part != ON_HEADER_LINE))
28548 clear_mouse_face (hlinfo);
28550 /* Not on a window -> return. */
28551 if (!WINDOWP (window))
28552 return;
28554 /* Reset help_echo_string. It will get recomputed below. */
28555 help_echo_string = Qnil;
28557 /* Convert to window-relative pixel coordinates. */
28558 w = XWINDOW (window);
28559 frame_to_window_pixel_xy (w, &x, &y);
28561 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28562 /* Handle tool-bar window differently since it doesn't display a
28563 buffer. */
28564 if (EQ (window, f->tool_bar_window))
28566 note_tool_bar_highlight (f, x, y);
28567 return;
28569 #endif
28571 /* Mouse is on the mode, header line or margin? */
28572 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28573 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28575 note_mode_line_or_margin_highlight (window, x, y, part);
28577 #ifdef HAVE_WINDOW_SYSTEM
28578 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28580 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28581 /* Show non-text cursor (Bug#16647). */
28582 goto set_cursor;
28584 else
28585 #endif
28586 return;
28589 #ifdef HAVE_WINDOW_SYSTEM
28590 if (part == ON_VERTICAL_BORDER)
28592 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28593 help_echo_string = build_string ("drag-mouse-1: resize");
28595 else if (part == ON_RIGHT_DIVIDER)
28597 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28598 help_echo_string = build_string ("drag-mouse-1: resize");
28600 else if (part == ON_BOTTOM_DIVIDER)
28602 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28603 help_echo_string = build_string ("drag-mouse-1: resize");
28605 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28606 || part == ON_SCROLL_BAR)
28607 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28608 else
28609 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28610 #endif
28612 /* Are we in a window whose display is up to date?
28613 And verify the buffer's text has not changed. */
28614 b = XBUFFER (w->contents);
28615 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28617 int hpos, vpos, dx, dy, area = LAST_AREA;
28618 ptrdiff_t pos;
28619 struct glyph *glyph;
28620 Lisp_Object object;
28621 Lisp_Object mouse_face = Qnil, position;
28622 Lisp_Object *overlay_vec = NULL;
28623 ptrdiff_t i, noverlays;
28624 struct buffer *obuf;
28625 ptrdiff_t obegv, ozv;
28626 int same_region;
28628 /* Find the glyph under X/Y. */
28629 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28631 #ifdef HAVE_WINDOW_SYSTEM
28632 /* Look for :pointer property on image. */
28633 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28635 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28636 if (img != NULL && IMAGEP (img->spec))
28638 Lisp_Object image_map, hotspot;
28639 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28640 !NILP (image_map))
28641 && (hotspot = find_hot_spot (image_map,
28642 glyph->slice.img.x + dx,
28643 glyph->slice.img.y + dy),
28644 CONSP (hotspot))
28645 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28647 Lisp_Object plist;
28649 /* Could check XCAR (hotspot) to see if we enter/leave
28650 this hot-spot.
28651 If so, we could look for mouse-enter, mouse-leave
28652 properties in PLIST (and do something...). */
28653 hotspot = XCDR (hotspot);
28654 if (CONSP (hotspot)
28655 && (plist = XCAR (hotspot), CONSP (plist)))
28657 pointer = Fplist_get (plist, Qpointer);
28658 if (NILP (pointer))
28659 pointer = Qhand;
28660 help_echo_string = Fplist_get (plist, Qhelp_echo);
28661 if (!NILP (help_echo_string))
28663 help_echo_window = window;
28664 help_echo_object = glyph->object;
28665 help_echo_pos = glyph->charpos;
28669 if (NILP (pointer))
28670 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28673 #endif /* HAVE_WINDOW_SYSTEM */
28675 /* Clear mouse face if X/Y not over text. */
28676 if (glyph == NULL
28677 || area != TEXT_AREA
28678 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28679 /* Glyph's OBJECT is an integer for glyphs inserted by the
28680 display engine for its internal purposes, like truncation
28681 and continuation glyphs and blanks beyond the end of
28682 line's text on text terminals. If we are over such a
28683 glyph, we are not over any text. */
28684 || INTEGERP (glyph->object)
28685 /* R2L rows have a stretch glyph at their front, which
28686 stands for no text, whereas L2R rows have no glyphs at
28687 all beyond the end of text. Treat such stretch glyphs
28688 like we do with NULL glyphs in L2R rows. */
28689 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28690 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28691 && glyph->type == STRETCH_GLYPH
28692 && glyph->avoid_cursor_p))
28694 if (clear_mouse_face (hlinfo))
28695 cursor = No_Cursor;
28696 #ifdef HAVE_WINDOW_SYSTEM
28697 if (FRAME_WINDOW_P (f) && NILP (pointer))
28699 if (area != TEXT_AREA)
28700 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28701 else
28702 pointer = Vvoid_text_area_pointer;
28704 #endif
28705 goto set_cursor;
28708 pos = glyph->charpos;
28709 object = glyph->object;
28710 if (!STRINGP (object) && !BUFFERP (object))
28711 goto set_cursor;
28713 /* If we get an out-of-range value, return now; avoid an error. */
28714 if (BUFFERP (object) && pos > BUF_Z (b))
28715 goto set_cursor;
28717 /* Make the window's buffer temporarily current for
28718 overlays_at and compute_char_face. */
28719 obuf = current_buffer;
28720 current_buffer = b;
28721 obegv = BEGV;
28722 ozv = ZV;
28723 BEGV = BEG;
28724 ZV = Z;
28726 /* Is this char mouse-active or does it have help-echo? */
28727 position = make_number (pos);
28729 if (BUFFERP (object))
28731 /* Put all the overlays we want in a vector in overlay_vec. */
28732 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28733 /* Sort overlays into increasing priority order. */
28734 noverlays = sort_overlays (overlay_vec, noverlays, w);
28736 else
28737 noverlays = 0;
28739 if (NILP (Vmouse_highlight))
28741 clear_mouse_face (hlinfo);
28742 goto check_help_echo;
28745 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28747 if (same_region)
28748 cursor = No_Cursor;
28750 /* Check mouse-face highlighting. */
28751 if (! same_region
28752 /* If there exists an overlay with mouse-face overlapping
28753 the one we are currently highlighting, we have to
28754 check if we enter the overlapping overlay, and then
28755 highlight only that. */
28756 || (OVERLAYP (hlinfo->mouse_face_overlay)
28757 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28759 /* Find the highest priority overlay with a mouse-face. */
28760 Lisp_Object overlay = Qnil;
28761 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28763 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28764 if (!NILP (mouse_face))
28765 overlay = overlay_vec[i];
28768 /* If we're highlighting the same overlay as before, there's
28769 no need to do that again. */
28770 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28771 goto check_help_echo;
28772 hlinfo->mouse_face_overlay = overlay;
28774 /* Clear the display of the old active region, if any. */
28775 if (clear_mouse_face (hlinfo))
28776 cursor = No_Cursor;
28778 /* If no overlay applies, get a text property. */
28779 if (NILP (overlay))
28780 mouse_face = Fget_text_property (position, Qmouse_face, object);
28782 /* Next, compute the bounds of the mouse highlighting and
28783 display it. */
28784 if (!NILP (mouse_face) && STRINGP (object))
28786 /* The mouse-highlighting comes from a display string
28787 with a mouse-face. */
28788 Lisp_Object s, e;
28789 ptrdiff_t ignore;
28791 s = Fprevious_single_property_change
28792 (make_number (pos + 1), Qmouse_face, object, Qnil);
28793 e = Fnext_single_property_change
28794 (position, Qmouse_face, object, Qnil);
28795 if (NILP (s))
28796 s = make_number (0);
28797 if (NILP (e))
28798 e = make_number (SCHARS (object));
28799 mouse_face_from_string_pos (w, hlinfo, object,
28800 XINT (s), XINT (e));
28801 hlinfo->mouse_face_past_end = 0;
28802 hlinfo->mouse_face_window = window;
28803 hlinfo->mouse_face_face_id
28804 = face_at_string_position (w, object, pos, 0, &ignore,
28805 glyph->face_id, 1);
28806 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28807 cursor = No_Cursor;
28809 else
28811 /* The mouse-highlighting, if any, comes from an overlay
28812 or text property in the buffer. */
28813 Lisp_Object buffer IF_LINT (= Qnil);
28814 Lisp_Object disp_string IF_LINT (= Qnil);
28816 if (STRINGP (object))
28818 /* If we are on a display string with no mouse-face,
28819 check if the text under it has one. */
28820 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28821 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28822 pos = string_buffer_position (object, start);
28823 if (pos > 0)
28825 mouse_face = get_char_property_and_overlay
28826 (make_number (pos), Qmouse_face, w->contents, &overlay);
28827 buffer = w->contents;
28828 disp_string = object;
28831 else
28833 buffer = object;
28834 disp_string = Qnil;
28837 if (!NILP (mouse_face))
28839 Lisp_Object before, after;
28840 Lisp_Object before_string, after_string;
28841 /* To correctly find the limits of mouse highlight
28842 in a bidi-reordered buffer, we must not use the
28843 optimization of limiting the search in
28844 previous-single-property-change and
28845 next-single-property-change, because
28846 rows_from_pos_range needs the real start and end
28847 positions to DTRT in this case. That's because
28848 the first row visible in a window does not
28849 necessarily display the character whose position
28850 is the smallest. */
28851 Lisp_Object lim1
28852 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28853 ? Fmarker_position (w->start)
28854 : Qnil;
28855 Lisp_Object lim2
28856 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28857 ? make_number (BUF_Z (XBUFFER (buffer))
28858 - w->window_end_pos)
28859 : Qnil;
28861 if (NILP (overlay))
28863 /* Handle the text property case. */
28864 before = Fprevious_single_property_change
28865 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28866 after = Fnext_single_property_change
28867 (make_number (pos), Qmouse_face, buffer, lim2);
28868 before_string = after_string = Qnil;
28870 else
28872 /* Handle the overlay case. */
28873 before = Foverlay_start (overlay);
28874 after = Foverlay_end (overlay);
28875 before_string = Foverlay_get (overlay, Qbefore_string);
28876 after_string = Foverlay_get (overlay, Qafter_string);
28878 if (!STRINGP (before_string)) before_string = Qnil;
28879 if (!STRINGP (after_string)) after_string = Qnil;
28882 mouse_face_from_buffer_pos (window, hlinfo, pos,
28883 NILP (before)
28885 : XFASTINT (before),
28886 NILP (after)
28887 ? BUF_Z (XBUFFER (buffer))
28888 : XFASTINT (after),
28889 before_string, after_string,
28890 disp_string);
28891 cursor = No_Cursor;
28896 check_help_echo:
28898 /* Look for a `help-echo' property. */
28899 if (NILP (help_echo_string)) {
28900 Lisp_Object help, overlay;
28902 /* Check overlays first. */
28903 help = overlay = Qnil;
28904 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28906 overlay = overlay_vec[i];
28907 help = Foverlay_get (overlay, Qhelp_echo);
28910 if (!NILP (help))
28912 help_echo_string = help;
28913 help_echo_window = window;
28914 help_echo_object = overlay;
28915 help_echo_pos = pos;
28917 else
28919 Lisp_Object obj = glyph->object;
28920 ptrdiff_t charpos = glyph->charpos;
28922 /* Try text properties. */
28923 if (STRINGP (obj)
28924 && charpos >= 0
28925 && charpos < SCHARS (obj))
28927 help = Fget_text_property (make_number (charpos),
28928 Qhelp_echo, obj);
28929 if (NILP (help))
28931 /* If the string itself doesn't specify a help-echo,
28932 see if the buffer text ``under'' it does. */
28933 struct glyph_row *r
28934 = MATRIX_ROW (w->current_matrix, vpos);
28935 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28936 ptrdiff_t p = string_buffer_position (obj, start);
28937 if (p > 0)
28939 help = Fget_char_property (make_number (p),
28940 Qhelp_echo, w->contents);
28941 if (!NILP (help))
28943 charpos = p;
28944 obj = w->contents;
28949 else if (BUFFERP (obj)
28950 && charpos >= BEGV
28951 && charpos < ZV)
28952 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28953 obj);
28955 if (!NILP (help))
28957 help_echo_string = help;
28958 help_echo_window = window;
28959 help_echo_object = obj;
28960 help_echo_pos = charpos;
28965 #ifdef HAVE_WINDOW_SYSTEM
28966 /* Look for a `pointer' property. */
28967 if (FRAME_WINDOW_P (f) && NILP (pointer))
28969 /* Check overlays first. */
28970 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28971 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28973 if (NILP (pointer))
28975 Lisp_Object obj = glyph->object;
28976 ptrdiff_t charpos = glyph->charpos;
28978 /* Try text properties. */
28979 if (STRINGP (obj)
28980 && charpos >= 0
28981 && charpos < SCHARS (obj))
28983 pointer = Fget_text_property (make_number (charpos),
28984 Qpointer, obj);
28985 if (NILP (pointer))
28987 /* If the string itself doesn't specify a pointer,
28988 see if the buffer text ``under'' it does. */
28989 struct glyph_row *r
28990 = MATRIX_ROW (w->current_matrix, vpos);
28991 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28992 ptrdiff_t p = string_buffer_position (obj, start);
28993 if (p > 0)
28994 pointer = Fget_char_property (make_number (p),
28995 Qpointer, w->contents);
28998 else if (BUFFERP (obj)
28999 && charpos >= BEGV
29000 && charpos < ZV)
29001 pointer = Fget_text_property (make_number (charpos),
29002 Qpointer, obj);
29005 #endif /* HAVE_WINDOW_SYSTEM */
29007 BEGV = obegv;
29008 ZV = ozv;
29009 current_buffer = obuf;
29012 set_cursor:
29014 #ifdef HAVE_WINDOW_SYSTEM
29015 if (FRAME_WINDOW_P (f))
29016 define_frame_cursor1 (f, cursor, pointer);
29017 #else
29018 /* This is here to prevent a compiler error, about "label at end of
29019 compound statement". */
29020 return;
29021 #endif
29025 /* EXPORT for RIF:
29026 Clear any mouse-face on window W. This function is part of the
29027 redisplay interface, and is called from try_window_id and similar
29028 functions to ensure the mouse-highlight is off. */
29030 void
29031 x_clear_window_mouse_face (struct window *w)
29033 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29034 Lisp_Object window;
29036 block_input ();
29037 XSETWINDOW (window, w);
29038 if (EQ (window, hlinfo->mouse_face_window))
29039 clear_mouse_face (hlinfo);
29040 unblock_input ();
29044 /* EXPORT:
29045 Just discard the mouse face information for frame F, if any.
29046 This is used when the size of F is changed. */
29048 void
29049 cancel_mouse_face (struct frame *f)
29051 Lisp_Object window;
29052 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29054 window = hlinfo->mouse_face_window;
29055 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29056 reset_mouse_highlight (hlinfo);
29061 /***********************************************************************
29062 Exposure Events
29063 ***********************************************************************/
29065 #ifdef HAVE_WINDOW_SYSTEM
29067 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29068 which intersects rectangle R. R is in window-relative coordinates. */
29070 static void
29071 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29072 enum glyph_row_area area)
29074 struct glyph *first = row->glyphs[area];
29075 struct glyph *end = row->glyphs[area] + row->used[area];
29076 struct glyph *last;
29077 int first_x, start_x, x;
29079 if (area == TEXT_AREA && row->fill_line_p)
29080 /* If row extends face to end of line write the whole line. */
29081 draw_glyphs (w, 0, row, area,
29082 0, row->used[area],
29083 DRAW_NORMAL_TEXT, 0);
29084 else
29086 /* Set START_X to the window-relative start position for drawing glyphs of
29087 AREA. The first glyph of the text area can be partially visible.
29088 The first glyphs of other areas cannot. */
29089 start_x = window_box_left_offset (w, area);
29090 x = start_x;
29091 if (area == TEXT_AREA)
29092 x += row->x;
29094 /* Find the first glyph that must be redrawn. */
29095 while (first < end
29096 && x + first->pixel_width < r->x)
29098 x += first->pixel_width;
29099 ++first;
29102 /* Find the last one. */
29103 last = first;
29104 first_x = x;
29105 while (last < end
29106 && x < r->x + r->width)
29108 x += last->pixel_width;
29109 ++last;
29112 /* Repaint. */
29113 if (last > first)
29114 draw_glyphs (w, first_x - start_x, row, area,
29115 first - row->glyphs[area], last - row->glyphs[area],
29116 DRAW_NORMAL_TEXT, 0);
29121 /* Redraw the parts of the glyph row ROW on window W intersecting
29122 rectangle R. R is in window-relative coordinates. Value is
29123 non-zero if mouse-face was overwritten. */
29125 static int
29126 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29128 eassert (row->enabled_p);
29130 if (row->mode_line_p || w->pseudo_window_p)
29131 draw_glyphs (w, 0, row, TEXT_AREA,
29132 0, row->used[TEXT_AREA],
29133 DRAW_NORMAL_TEXT, 0);
29134 else
29136 if (row->used[LEFT_MARGIN_AREA])
29137 expose_area (w, row, r, LEFT_MARGIN_AREA);
29138 if (row->used[TEXT_AREA])
29139 expose_area (w, row, r, TEXT_AREA);
29140 if (row->used[RIGHT_MARGIN_AREA])
29141 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29142 draw_row_fringe_bitmaps (w, row);
29145 return row->mouse_face_p;
29149 /* Redraw those parts of glyphs rows during expose event handling that
29150 overlap other rows. Redrawing of an exposed line writes over parts
29151 of lines overlapping that exposed line; this function fixes that.
29153 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29154 row in W's current matrix that is exposed and overlaps other rows.
29155 LAST_OVERLAPPING_ROW is the last such row. */
29157 static void
29158 expose_overlaps (struct window *w,
29159 struct glyph_row *first_overlapping_row,
29160 struct glyph_row *last_overlapping_row,
29161 XRectangle *r)
29163 struct glyph_row *row;
29165 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29166 if (row->overlapping_p)
29168 eassert (row->enabled_p && !row->mode_line_p);
29170 row->clip = r;
29171 if (row->used[LEFT_MARGIN_AREA])
29172 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29174 if (row->used[TEXT_AREA])
29175 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29177 if (row->used[RIGHT_MARGIN_AREA])
29178 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29179 row->clip = NULL;
29184 /* Return non-zero if W's cursor intersects rectangle R. */
29186 static int
29187 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29189 XRectangle cr, result;
29190 struct glyph *cursor_glyph;
29191 struct glyph_row *row;
29193 if (w->phys_cursor.vpos >= 0
29194 && w->phys_cursor.vpos < w->current_matrix->nrows
29195 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29196 row->enabled_p)
29197 && row->cursor_in_fringe_p)
29199 /* Cursor is in the fringe. */
29200 cr.x = window_box_right_offset (w,
29201 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29202 ? RIGHT_MARGIN_AREA
29203 : TEXT_AREA));
29204 cr.y = row->y;
29205 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29206 cr.height = row->height;
29207 return x_intersect_rectangles (&cr, r, &result);
29210 cursor_glyph = get_phys_cursor_glyph (w);
29211 if (cursor_glyph)
29213 /* r is relative to W's box, but w->phys_cursor.x is relative
29214 to left edge of W's TEXT area. Adjust it. */
29215 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29216 cr.y = w->phys_cursor.y;
29217 cr.width = cursor_glyph->pixel_width;
29218 cr.height = w->phys_cursor_height;
29219 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29220 I assume the effect is the same -- and this is portable. */
29221 return x_intersect_rectangles (&cr, r, &result);
29223 /* If we don't understand the format, pretend we're not in the hot-spot. */
29224 return 0;
29228 /* EXPORT:
29229 Draw a vertical window border to the right of window W if W doesn't
29230 have vertical scroll bars. */
29232 void
29233 x_draw_vertical_border (struct window *w)
29235 struct frame *f = XFRAME (WINDOW_FRAME (w));
29237 /* We could do better, if we knew what type of scroll-bar the adjacent
29238 windows (on either side) have... But we don't :-(
29239 However, I think this works ok. ++KFS 2003-04-25 */
29241 /* Redraw borders between horizontally adjacent windows. Don't
29242 do it for frames with vertical scroll bars because either the
29243 right scroll bar of a window, or the left scroll bar of its
29244 neighbor will suffice as a border. */
29245 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29246 return;
29248 /* Note: It is necessary to redraw both the left and the right
29249 borders, for when only this single window W is being
29250 redisplayed. */
29251 if (!WINDOW_RIGHTMOST_P (w)
29252 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29254 int x0, x1, y0, y1;
29256 window_box_edges (w, &x0, &y0, &x1, &y1);
29257 y1 -= 1;
29259 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29260 x1 -= 1;
29262 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29265 if (!WINDOW_LEFTMOST_P (w)
29266 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29268 int x0, x1, y0, y1;
29270 window_box_edges (w, &x0, &y0, &x1, &y1);
29271 y1 -= 1;
29273 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29274 x0 -= 1;
29276 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29281 /* Draw window dividers for window W. */
29283 void
29284 x_draw_right_divider (struct window *w)
29286 struct frame *f = WINDOW_XFRAME (w);
29288 if (w->mini || w->pseudo_window_p)
29289 return;
29290 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29292 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29293 int x1 = WINDOW_RIGHT_EDGE_X (w);
29294 int y0 = WINDOW_TOP_EDGE_Y (w);
29295 /* The bottom divider prevails. */
29296 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29298 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29302 static void
29303 x_draw_bottom_divider (struct window *w)
29305 struct frame *f = XFRAME (WINDOW_FRAME (w));
29307 if (w->mini || w->pseudo_window_p)
29308 return;
29309 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29311 int x0 = WINDOW_LEFT_EDGE_X (w);
29312 int x1 = WINDOW_RIGHT_EDGE_X (w);
29313 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29314 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29316 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29320 /* Redraw the part of window W intersection rectangle FR. Pixel
29321 coordinates in FR are frame-relative. Call this function with
29322 input blocked. Value is non-zero if the exposure overwrites
29323 mouse-face. */
29325 static int
29326 expose_window (struct window *w, XRectangle *fr)
29328 struct frame *f = XFRAME (w->frame);
29329 XRectangle wr, r;
29330 int mouse_face_overwritten_p = 0;
29332 /* If window is not yet fully initialized, do nothing. This can
29333 happen when toolkit scroll bars are used and a window is split.
29334 Reconfiguring the scroll bar will generate an expose for a newly
29335 created window. */
29336 if (w->current_matrix == NULL)
29337 return 0;
29339 /* When we're currently updating the window, display and current
29340 matrix usually don't agree. Arrange for a thorough display
29341 later. */
29342 if (w->must_be_updated_p)
29344 SET_FRAME_GARBAGED (f);
29345 return 0;
29348 /* Frame-relative pixel rectangle of W. */
29349 wr.x = WINDOW_LEFT_EDGE_X (w);
29350 wr.y = WINDOW_TOP_EDGE_Y (w);
29351 wr.width = WINDOW_PIXEL_WIDTH (w);
29352 wr.height = WINDOW_PIXEL_HEIGHT (w);
29354 if (x_intersect_rectangles (fr, &wr, &r))
29356 int yb = window_text_bottom_y (w);
29357 struct glyph_row *row;
29358 int cursor_cleared_p, phys_cursor_on_p;
29359 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29361 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29362 r.x, r.y, r.width, r.height));
29364 /* Convert to window coordinates. */
29365 r.x -= WINDOW_LEFT_EDGE_X (w);
29366 r.y -= WINDOW_TOP_EDGE_Y (w);
29368 /* Turn off the cursor. */
29369 if (!w->pseudo_window_p
29370 && phys_cursor_in_rect_p (w, &r))
29372 x_clear_cursor (w);
29373 cursor_cleared_p = 1;
29375 else
29376 cursor_cleared_p = 0;
29378 /* If the row containing the cursor extends face to end of line,
29379 then expose_area might overwrite the cursor outside the
29380 rectangle and thus notice_overwritten_cursor might clear
29381 w->phys_cursor_on_p. We remember the original value and
29382 check later if it is changed. */
29383 phys_cursor_on_p = w->phys_cursor_on_p;
29385 /* Update lines intersecting rectangle R. */
29386 first_overlapping_row = last_overlapping_row = NULL;
29387 for (row = w->current_matrix->rows;
29388 row->enabled_p;
29389 ++row)
29391 int y0 = row->y;
29392 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29394 if ((y0 >= r.y && y0 < r.y + r.height)
29395 || (y1 > r.y && y1 < r.y + r.height)
29396 || (r.y >= y0 && r.y < y1)
29397 || (r.y + r.height > y0 && r.y + r.height < y1))
29399 /* A header line may be overlapping, but there is no need
29400 to fix overlapping areas for them. KFS 2005-02-12 */
29401 if (row->overlapping_p && !row->mode_line_p)
29403 if (first_overlapping_row == NULL)
29404 first_overlapping_row = row;
29405 last_overlapping_row = row;
29408 row->clip = fr;
29409 if (expose_line (w, row, &r))
29410 mouse_face_overwritten_p = 1;
29411 row->clip = NULL;
29413 else if (row->overlapping_p)
29415 /* We must redraw a row overlapping the exposed area. */
29416 if (y0 < r.y
29417 ? y0 + row->phys_height > r.y
29418 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29420 if (first_overlapping_row == NULL)
29421 first_overlapping_row = row;
29422 last_overlapping_row = row;
29426 if (y1 >= yb)
29427 break;
29430 /* Display the mode line if there is one. */
29431 if (WINDOW_WANTS_MODELINE_P (w)
29432 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29433 row->enabled_p)
29434 && row->y < r.y + r.height)
29436 if (expose_line (w, row, &r))
29437 mouse_face_overwritten_p = 1;
29440 if (!w->pseudo_window_p)
29442 /* Fix the display of overlapping rows. */
29443 if (first_overlapping_row)
29444 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29445 fr);
29447 /* Draw border between windows. */
29448 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29449 x_draw_right_divider (w);
29450 else
29451 x_draw_vertical_border (w);
29453 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29454 x_draw_bottom_divider (w);
29456 /* Turn the cursor on again. */
29457 if (cursor_cleared_p
29458 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29459 update_window_cursor (w, 1);
29463 return mouse_face_overwritten_p;
29468 /* Redraw (parts) of all windows in the window tree rooted at W that
29469 intersect R. R contains frame pixel coordinates. Value is
29470 non-zero if the exposure overwrites mouse-face. */
29472 static int
29473 expose_window_tree (struct window *w, XRectangle *r)
29475 struct frame *f = XFRAME (w->frame);
29476 int mouse_face_overwritten_p = 0;
29478 while (w && !FRAME_GARBAGED_P (f))
29480 if (WINDOWP (w->contents))
29481 mouse_face_overwritten_p
29482 |= expose_window_tree (XWINDOW (w->contents), r);
29483 else
29484 mouse_face_overwritten_p |= expose_window (w, r);
29486 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29489 return mouse_face_overwritten_p;
29493 /* EXPORT:
29494 Redisplay an exposed area of frame F. X and Y are the upper-left
29495 corner of the exposed rectangle. W and H are width and height of
29496 the exposed area. All are pixel values. W or H zero means redraw
29497 the entire frame. */
29499 void
29500 expose_frame (struct frame *f, int x, int y, int w, int h)
29502 XRectangle r;
29503 int mouse_face_overwritten_p = 0;
29505 TRACE ((stderr, "expose_frame "));
29507 /* No need to redraw if frame will be redrawn soon. */
29508 if (FRAME_GARBAGED_P (f))
29510 TRACE ((stderr, " garbaged\n"));
29511 return;
29514 /* If basic faces haven't been realized yet, there is no point in
29515 trying to redraw anything. This can happen when we get an expose
29516 event while Emacs is starting, e.g. by moving another window. */
29517 if (FRAME_FACE_CACHE (f) == NULL
29518 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29520 TRACE ((stderr, " no faces\n"));
29521 return;
29524 if (w == 0 || h == 0)
29526 r.x = r.y = 0;
29527 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29528 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29530 else
29532 r.x = x;
29533 r.y = y;
29534 r.width = w;
29535 r.height = h;
29538 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29539 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29541 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29542 if (WINDOWP (f->tool_bar_window))
29543 mouse_face_overwritten_p
29544 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29545 #endif
29547 #ifdef HAVE_X_WINDOWS
29548 #ifndef MSDOS
29549 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29550 if (WINDOWP (f->menu_bar_window))
29551 mouse_face_overwritten_p
29552 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29553 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29554 #endif
29555 #endif
29557 /* Some window managers support a focus-follows-mouse style with
29558 delayed raising of frames. Imagine a partially obscured frame,
29559 and moving the mouse into partially obscured mouse-face on that
29560 frame. The visible part of the mouse-face will be highlighted,
29561 then the WM raises the obscured frame. With at least one WM, KDE
29562 2.1, Emacs is not getting any event for the raising of the frame
29563 (even tried with SubstructureRedirectMask), only Expose events.
29564 These expose events will draw text normally, i.e. not
29565 highlighted. Which means we must redo the highlight here.
29566 Subsume it under ``we love X''. --gerd 2001-08-15 */
29567 /* Included in Windows version because Windows most likely does not
29568 do the right thing if any third party tool offers
29569 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29570 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29572 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29573 if (f == hlinfo->mouse_face_mouse_frame)
29575 int mouse_x = hlinfo->mouse_face_mouse_x;
29576 int mouse_y = hlinfo->mouse_face_mouse_y;
29577 clear_mouse_face (hlinfo);
29578 note_mouse_highlight (f, mouse_x, mouse_y);
29584 /* EXPORT:
29585 Determine the intersection of two rectangles R1 and R2. Return
29586 the intersection in *RESULT. Value is non-zero if RESULT is not
29587 empty. */
29590 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29592 XRectangle *left, *right;
29593 XRectangle *upper, *lower;
29594 int intersection_p = 0;
29596 /* Rearrange so that R1 is the left-most rectangle. */
29597 if (r1->x < r2->x)
29598 left = r1, right = r2;
29599 else
29600 left = r2, right = r1;
29602 /* X0 of the intersection is right.x0, if this is inside R1,
29603 otherwise there is no intersection. */
29604 if (right->x <= left->x + left->width)
29606 result->x = right->x;
29608 /* The right end of the intersection is the minimum of
29609 the right ends of left and right. */
29610 result->width = (min (left->x + left->width, right->x + right->width)
29611 - result->x);
29613 /* Same game for Y. */
29614 if (r1->y < r2->y)
29615 upper = r1, lower = r2;
29616 else
29617 upper = r2, lower = r1;
29619 /* The upper end of the intersection is lower.y0, if this is inside
29620 of upper. Otherwise, there is no intersection. */
29621 if (lower->y <= upper->y + upper->height)
29623 result->y = lower->y;
29625 /* The lower end of the intersection is the minimum of the lower
29626 ends of upper and lower. */
29627 result->height = (min (lower->y + lower->height,
29628 upper->y + upper->height)
29629 - result->y);
29630 intersection_p = 1;
29634 return intersection_p;
29637 #endif /* HAVE_WINDOW_SYSTEM */
29640 /***********************************************************************
29641 Initialization
29642 ***********************************************************************/
29644 void
29645 syms_of_xdisp (void)
29647 Vwith_echo_area_save_vector = Qnil;
29648 staticpro (&Vwith_echo_area_save_vector);
29650 Vmessage_stack = Qnil;
29651 staticpro (&Vmessage_stack);
29653 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29654 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29656 message_dolog_marker1 = Fmake_marker ();
29657 staticpro (&message_dolog_marker1);
29658 message_dolog_marker2 = Fmake_marker ();
29659 staticpro (&message_dolog_marker2);
29660 message_dolog_marker3 = Fmake_marker ();
29661 staticpro (&message_dolog_marker3);
29663 #ifdef GLYPH_DEBUG
29664 defsubr (&Sdump_frame_glyph_matrix);
29665 defsubr (&Sdump_glyph_matrix);
29666 defsubr (&Sdump_glyph_row);
29667 defsubr (&Sdump_tool_bar_row);
29668 defsubr (&Strace_redisplay);
29669 defsubr (&Strace_to_stderr);
29670 #endif
29671 #ifdef HAVE_WINDOW_SYSTEM
29672 defsubr (&Stool_bar_height);
29673 defsubr (&Slookup_image_map);
29674 #endif
29675 defsubr (&Sline_pixel_height);
29676 defsubr (&Sformat_mode_line);
29677 defsubr (&Sinvisible_p);
29678 defsubr (&Scurrent_bidi_paragraph_direction);
29679 defsubr (&Swindow_text_pixel_size);
29680 defsubr (&Smove_point_visually);
29682 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29683 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29684 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29685 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29686 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29687 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29688 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29689 DEFSYM (Qeval, "eval");
29690 DEFSYM (QCdata, ":data");
29691 DEFSYM (Qdisplay, "display");
29692 DEFSYM (Qspace_width, "space-width");
29693 DEFSYM (Qraise, "raise");
29694 DEFSYM (Qslice, "slice");
29695 DEFSYM (Qspace, "space");
29696 DEFSYM (Qmargin, "margin");
29697 DEFSYM (Qpointer, "pointer");
29698 DEFSYM (Qleft_margin, "left-margin");
29699 DEFSYM (Qright_margin, "right-margin");
29700 DEFSYM (Qcenter, "center");
29701 DEFSYM (Qline_height, "line-height");
29702 DEFSYM (QCalign_to, ":align-to");
29703 DEFSYM (QCrelative_width, ":relative-width");
29704 DEFSYM (QCrelative_height, ":relative-height");
29705 DEFSYM (QCeval, ":eval");
29706 DEFSYM (QCpropertize, ":propertize");
29707 DEFSYM (QCfile, ":file");
29708 DEFSYM (Qfontified, "fontified");
29709 DEFSYM (Qfontification_functions, "fontification-functions");
29710 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29711 DEFSYM (Qescape_glyph, "escape-glyph");
29712 DEFSYM (Qnobreak_space, "nobreak-space");
29713 DEFSYM (Qimage, "image");
29714 DEFSYM (Qtext, "text");
29715 DEFSYM (Qboth, "both");
29716 DEFSYM (Qboth_horiz, "both-horiz");
29717 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29718 DEFSYM (QCmap, ":map");
29719 DEFSYM (QCpointer, ":pointer");
29720 DEFSYM (Qrect, "rect");
29721 DEFSYM (Qcircle, "circle");
29722 DEFSYM (Qpoly, "poly");
29723 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29724 DEFSYM (Qgrow_only, "grow-only");
29725 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29726 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29727 DEFSYM (Qposition, "position");
29728 DEFSYM (Qbuffer_position, "buffer-position");
29729 DEFSYM (Qobject, "object");
29730 DEFSYM (Qbar, "bar");
29731 DEFSYM (Qhbar, "hbar");
29732 DEFSYM (Qbox, "box");
29733 DEFSYM (Qhollow, "hollow");
29734 DEFSYM (Qhand, "hand");
29735 DEFSYM (Qarrow, "arrow");
29736 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29738 list_of_error = list1 (list2 (intern_c_string ("error"),
29739 intern_c_string ("void-variable")));
29740 staticpro (&list_of_error);
29742 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29743 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29744 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29745 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29747 echo_buffer[0] = echo_buffer[1] = Qnil;
29748 staticpro (&echo_buffer[0]);
29749 staticpro (&echo_buffer[1]);
29751 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29752 staticpro (&echo_area_buffer[0]);
29753 staticpro (&echo_area_buffer[1]);
29755 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29756 staticpro (&Vmessages_buffer_name);
29758 mode_line_proptrans_alist = Qnil;
29759 staticpro (&mode_line_proptrans_alist);
29760 mode_line_string_list = Qnil;
29761 staticpro (&mode_line_string_list);
29762 mode_line_string_face = Qnil;
29763 staticpro (&mode_line_string_face);
29764 mode_line_string_face_prop = Qnil;
29765 staticpro (&mode_line_string_face_prop);
29766 Vmode_line_unwind_vector = Qnil;
29767 staticpro (&Vmode_line_unwind_vector);
29769 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29771 help_echo_string = Qnil;
29772 staticpro (&help_echo_string);
29773 help_echo_object = Qnil;
29774 staticpro (&help_echo_object);
29775 help_echo_window = Qnil;
29776 staticpro (&help_echo_window);
29777 previous_help_echo_string = Qnil;
29778 staticpro (&previous_help_echo_string);
29779 help_echo_pos = -1;
29781 DEFSYM (Qright_to_left, "right-to-left");
29782 DEFSYM (Qleft_to_right, "left-to-right");
29784 #ifdef HAVE_WINDOW_SYSTEM
29785 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29786 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29787 For example, if a block cursor is over a tab, it will be drawn as
29788 wide as that tab on the display. */);
29789 x_stretch_cursor_p = 0;
29790 #endif
29792 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29793 doc: /* Non-nil means highlight trailing whitespace.
29794 The face used for trailing whitespace is `trailing-whitespace'. */);
29795 Vshow_trailing_whitespace = Qnil;
29797 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29798 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29799 If the value is t, Emacs highlights non-ASCII chars which have the
29800 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29801 or `escape-glyph' face respectively.
29803 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29804 U+2011 (non-breaking hyphen) are affected.
29806 Any other non-nil value means to display these characters as a escape
29807 glyph followed by an ordinary space or hyphen.
29809 A value of nil means no special handling of these characters. */);
29810 Vnobreak_char_display = Qt;
29812 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29813 doc: /* The pointer shape to show in void text areas.
29814 A value of nil means to show the text pointer. Other options are `arrow',
29815 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29816 Vvoid_text_area_pointer = Qarrow;
29818 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29819 doc: /* Non-nil means don't actually do any redisplay.
29820 This is used for internal purposes. */);
29821 Vinhibit_redisplay = Qnil;
29823 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29824 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29825 Vglobal_mode_string = Qnil;
29827 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29828 doc: /* Marker for where to display an arrow on top of the buffer text.
29829 This must be the beginning of a line in order to work.
29830 See also `overlay-arrow-string'. */);
29831 Voverlay_arrow_position = Qnil;
29833 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29834 doc: /* String to display as an arrow in non-window frames.
29835 See also `overlay-arrow-position'. */);
29836 Voverlay_arrow_string = build_pure_c_string ("=>");
29838 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29839 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29840 The symbols on this list are examined during redisplay to determine
29841 where to display overlay arrows. */);
29842 Voverlay_arrow_variable_list
29843 = list1 (intern_c_string ("overlay-arrow-position"));
29845 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29846 doc: /* The number of lines to try scrolling a window by when point moves out.
29847 If that fails to bring point back on frame, point is centered instead.
29848 If this is zero, point is always centered after it moves off frame.
29849 If you want scrolling to always be a line at a time, you should set
29850 `scroll-conservatively' to a large value rather than set this to 1. */);
29852 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29853 doc: /* Scroll up to this many lines, to bring point back on screen.
29854 If point moves off-screen, redisplay will scroll by up to
29855 `scroll-conservatively' lines in order to bring point just barely
29856 onto the screen again. If that cannot be done, then redisplay
29857 recenters point as usual.
29859 If the value is greater than 100, redisplay will never recenter point,
29860 but will always scroll just enough text to bring point into view, even
29861 if you move far away.
29863 A value of zero means always recenter point if it moves off screen. */);
29864 scroll_conservatively = 0;
29866 DEFVAR_INT ("scroll-margin", scroll_margin,
29867 doc: /* Number of lines of margin at the top and bottom of a window.
29868 Recenter the window whenever point gets within this many lines
29869 of the top or bottom of the window. */);
29870 scroll_margin = 0;
29872 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29873 doc: /* Pixels per inch value for non-window system displays.
29874 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29875 Vdisplay_pixels_per_inch = make_float (72.0);
29877 #ifdef GLYPH_DEBUG
29878 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29879 #endif
29881 DEFVAR_LISP ("truncate-partial-width-windows",
29882 Vtruncate_partial_width_windows,
29883 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29884 For an integer value, truncate lines in each window narrower than the
29885 full frame width, provided the window width is less than that integer;
29886 otherwise, respect the value of `truncate-lines'.
29888 For any other non-nil value, truncate lines in all windows that do
29889 not span the full frame width.
29891 A value of nil means to respect the value of `truncate-lines'.
29893 If `word-wrap' is enabled, you might want to reduce this. */);
29894 Vtruncate_partial_width_windows = make_number (50);
29896 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29897 doc: /* Maximum buffer size for which line number should be displayed.
29898 If the buffer is bigger than this, the line number does not appear
29899 in the mode line. A value of nil means no limit. */);
29900 Vline_number_display_limit = Qnil;
29902 DEFVAR_INT ("line-number-display-limit-width",
29903 line_number_display_limit_width,
29904 doc: /* Maximum line width (in characters) for line number display.
29905 If the average length of the lines near point is bigger than this, then the
29906 line number may be omitted from the mode line. */);
29907 line_number_display_limit_width = 200;
29909 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29910 doc: /* Non-nil means highlight region even in nonselected windows. */);
29911 highlight_nonselected_windows = 0;
29913 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29914 doc: /* Non-nil if more than one frame is visible on this display.
29915 Minibuffer-only frames don't count, but iconified frames do.
29916 This variable is not guaranteed to be accurate except while processing
29917 `frame-title-format' and `icon-title-format'. */);
29919 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29920 doc: /* Template for displaying the title bar of visible frames.
29921 \(Assuming the window manager supports this feature.)
29923 This variable has the same structure as `mode-line-format', except that
29924 the %c and %l constructs are ignored. It is used only on frames for
29925 which no explicit name has been set \(see `modify-frame-parameters'). */);
29927 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29928 doc: /* Template for displaying the title bar of an iconified frame.
29929 \(Assuming the window manager supports this feature.)
29930 This variable has the same structure as `mode-line-format' (which see),
29931 and is used only on frames for which no explicit name has been set
29932 \(see `modify-frame-parameters'). */);
29933 Vicon_title_format
29934 = Vframe_title_format
29935 = listn (CONSTYPE_PURE, 3,
29936 intern_c_string ("multiple-frames"),
29937 build_pure_c_string ("%b"),
29938 listn (CONSTYPE_PURE, 4,
29939 empty_unibyte_string,
29940 intern_c_string ("invocation-name"),
29941 build_pure_c_string ("@"),
29942 intern_c_string ("system-name")));
29944 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29945 doc: /* Maximum number of lines to keep in the message log buffer.
29946 If nil, disable message logging. If t, log messages but don't truncate
29947 the buffer when it becomes large. */);
29948 Vmessage_log_max = make_number (1000);
29950 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29951 doc: /* Functions called before redisplay, if window sizes have changed.
29952 The value should be a list of functions that take one argument.
29953 Just before redisplay, for each frame, if any of its windows have changed
29954 size since the last redisplay, or have been split or deleted,
29955 all the functions in the list are called, with the frame as argument. */);
29956 Vwindow_size_change_functions = Qnil;
29958 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29959 doc: /* List of functions to call before redisplaying a window with scrolling.
29960 Each function is called with two arguments, the window and its new
29961 display-start position. Note that these functions are also called by
29962 `set-window-buffer'. Also note that the value of `window-end' is not
29963 valid when these functions are called.
29965 Warning: Do not use this feature to alter the way the window
29966 is scrolled. It is not designed for that, and such use probably won't
29967 work. */);
29968 Vwindow_scroll_functions = Qnil;
29970 DEFVAR_LISP ("window-text-change-functions",
29971 Vwindow_text_change_functions,
29972 doc: /* Functions to call in redisplay when text in the window might change. */);
29973 Vwindow_text_change_functions = Qnil;
29975 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29976 doc: /* Functions called when redisplay of a window reaches the end trigger.
29977 Each function is called with two arguments, the window and the end trigger value.
29978 See `set-window-redisplay-end-trigger'. */);
29979 Vredisplay_end_trigger_functions = Qnil;
29981 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29982 doc: /* Non-nil means autoselect window with mouse pointer.
29983 If nil, do not autoselect windows.
29984 A positive number means delay autoselection by that many seconds: a
29985 window is autoselected only after the mouse has remained in that
29986 window for the duration of the delay.
29987 A negative number has a similar effect, but causes windows to be
29988 autoselected only after the mouse has stopped moving. \(Because of
29989 the way Emacs compares mouse events, you will occasionally wait twice
29990 that time before the window gets selected.\)
29991 Any other value means to autoselect window instantaneously when the
29992 mouse pointer enters it.
29994 Autoselection selects the minibuffer only if it is active, and never
29995 unselects the minibuffer if it is active.
29997 When customizing this variable make sure that the actual value of
29998 `focus-follows-mouse' matches the behavior of your window manager. */);
29999 Vmouse_autoselect_window = Qnil;
30001 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30002 doc: /* Non-nil means automatically resize tool-bars.
30003 This dynamically changes the tool-bar's height to the minimum height
30004 that is needed to make all tool-bar items visible.
30005 If value is `grow-only', the tool-bar's height is only increased
30006 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30007 Vauto_resize_tool_bars = Qt;
30009 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30010 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30011 auto_raise_tool_bar_buttons_p = 1;
30013 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30014 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30015 make_cursor_line_fully_visible_p = 1;
30017 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30018 doc: /* Border below tool-bar in pixels.
30019 If an integer, use it as the height of the border.
30020 If it is one of `internal-border-width' or `border-width', use the
30021 value of the corresponding frame parameter.
30022 Otherwise, no border is added below the tool-bar. */);
30023 Vtool_bar_border = Qinternal_border_width;
30025 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30026 doc: /* Margin around tool-bar buttons in pixels.
30027 If an integer, use that for both horizontal and vertical margins.
30028 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30029 HORZ specifying the horizontal margin, and VERT specifying the
30030 vertical margin. */);
30031 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30033 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30034 doc: /* Relief thickness of tool-bar buttons. */);
30035 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30037 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30038 doc: /* Tool bar style to use.
30039 It can be one of
30040 image - show images only
30041 text - show text only
30042 both - show both, text below image
30043 both-horiz - show text to the right of the image
30044 text-image-horiz - show text to the left of the image
30045 any other - use system default or image if no system default.
30047 This variable only affects the GTK+ toolkit version of Emacs. */);
30048 Vtool_bar_style = Qnil;
30050 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30051 doc: /* Maximum number of characters a label can have to be shown.
30052 The tool bar style must also show labels for this to have any effect, see
30053 `tool-bar-style'. */);
30054 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30056 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30057 doc: /* List of functions to call to fontify regions of text.
30058 Each function is called with one argument POS. Functions must
30059 fontify a region starting at POS in the current buffer, and give
30060 fontified regions the property `fontified'. */);
30061 Vfontification_functions = Qnil;
30062 Fmake_variable_buffer_local (Qfontification_functions);
30064 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30065 unibyte_display_via_language_environment,
30066 doc: /* Non-nil means display unibyte text according to language environment.
30067 Specifically, this means that raw bytes in the range 160-255 decimal
30068 are displayed by converting them to the equivalent multibyte characters
30069 according to the current language environment. As a result, they are
30070 displayed according to the current fontset.
30072 Note that this variable affects only how these bytes are displayed,
30073 but does not change the fact they are interpreted as raw bytes. */);
30074 unibyte_display_via_language_environment = 0;
30076 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30077 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30078 If a float, it specifies a fraction of the mini-window frame's height.
30079 If an integer, it specifies a number of lines. */);
30080 Vmax_mini_window_height = make_float (0.25);
30082 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30083 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30084 A value of nil means don't automatically resize mini-windows.
30085 A value of t means resize them to fit the text displayed in them.
30086 A value of `grow-only', the default, means let mini-windows grow only;
30087 they return to their normal size when the minibuffer is closed, or the
30088 echo area becomes empty. */);
30089 Vresize_mini_windows = Qgrow_only;
30091 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30092 doc: /* Alist specifying how to blink the cursor off.
30093 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30094 `cursor-type' frame-parameter or variable equals ON-STATE,
30095 comparing using `equal', Emacs uses OFF-STATE to specify
30096 how to blink it off. ON-STATE and OFF-STATE are values for
30097 the `cursor-type' frame parameter.
30099 If a frame's ON-STATE has no entry in this list,
30100 the frame's other specifications determine how to blink the cursor off. */);
30101 Vblink_cursor_alist = Qnil;
30103 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30104 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30105 If non-nil, windows are automatically scrolled horizontally to make
30106 point visible. */);
30107 automatic_hscrolling_p = 1;
30108 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30110 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30111 doc: /* How many columns away from the window edge point is allowed to get
30112 before automatic hscrolling will horizontally scroll the window. */);
30113 hscroll_margin = 5;
30115 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30116 doc: /* How many columns to scroll the window when point gets too close to the edge.
30117 When point is less than `hscroll-margin' columns from the window
30118 edge, automatic hscrolling will scroll the window by the amount of columns
30119 determined by this variable. If its value is a positive integer, scroll that
30120 many columns. If it's a positive floating-point number, it specifies the
30121 fraction of the window's width to scroll. If it's nil or zero, point will be
30122 centered horizontally after the scroll. Any other value, including negative
30123 numbers, are treated as if the value were zero.
30125 Automatic hscrolling always moves point outside the scroll margin, so if
30126 point was more than scroll step columns inside the margin, the window will
30127 scroll more than the value given by the scroll step.
30129 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30130 and `scroll-right' overrides this variable's effect. */);
30131 Vhscroll_step = make_number (0);
30133 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30134 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30135 Bind this around calls to `message' to let it take effect. */);
30136 message_truncate_lines = 0;
30138 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30139 doc: /* Normal hook run to update the menu bar definitions.
30140 Redisplay runs this hook before it redisplays the menu bar.
30141 This is used to update menus such as Buffers, whose contents depend on
30142 various data. */);
30143 Vmenu_bar_update_hook = Qnil;
30145 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30146 doc: /* Frame for which we are updating a menu.
30147 The enable predicate for a menu binding should check this variable. */);
30148 Vmenu_updating_frame = Qnil;
30150 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30151 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30152 inhibit_menubar_update = 0;
30154 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30155 doc: /* Prefix prepended to all continuation lines at display time.
30156 The value may be a string, an image, or a stretch-glyph; it is
30157 interpreted in the same way as the value of a `display' text property.
30159 This variable is overridden by any `wrap-prefix' text or overlay
30160 property.
30162 To add a prefix to non-continuation lines, use `line-prefix'. */);
30163 Vwrap_prefix = Qnil;
30164 DEFSYM (Qwrap_prefix, "wrap-prefix");
30165 Fmake_variable_buffer_local (Qwrap_prefix);
30167 DEFVAR_LISP ("line-prefix", Vline_prefix,
30168 doc: /* Prefix prepended to all non-continuation lines at display time.
30169 The value may be a string, an image, or a stretch-glyph; it is
30170 interpreted in the same way as the value of a `display' text property.
30172 This variable is overridden by any `line-prefix' text or overlay
30173 property.
30175 To add a prefix to continuation lines, use `wrap-prefix'. */);
30176 Vline_prefix = Qnil;
30177 DEFSYM (Qline_prefix, "line-prefix");
30178 Fmake_variable_buffer_local (Qline_prefix);
30180 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30181 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30182 inhibit_eval_during_redisplay = 0;
30184 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30185 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30186 inhibit_free_realized_faces = 0;
30188 #ifdef GLYPH_DEBUG
30189 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30190 doc: /* Inhibit try_window_id display optimization. */);
30191 inhibit_try_window_id = 0;
30193 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30194 doc: /* Inhibit try_window_reusing display optimization. */);
30195 inhibit_try_window_reusing = 0;
30197 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30198 doc: /* Inhibit try_cursor_movement display optimization. */);
30199 inhibit_try_cursor_movement = 0;
30200 #endif /* GLYPH_DEBUG */
30202 DEFVAR_INT ("overline-margin", overline_margin,
30203 doc: /* Space between overline and text, in pixels.
30204 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30205 margin to the character height. */);
30206 overline_margin = 2;
30208 DEFVAR_INT ("underline-minimum-offset",
30209 underline_minimum_offset,
30210 doc: /* Minimum distance between baseline and underline.
30211 This can improve legibility of underlined text at small font sizes,
30212 particularly when using variable `x-use-underline-position-properties'
30213 with fonts that specify an UNDERLINE_POSITION relatively close to the
30214 baseline. The default value is 1. */);
30215 underline_minimum_offset = 1;
30217 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30218 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30219 This feature only works when on a window system that can change
30220 cursor shapes. */);
30221 display_hourglass_p = 1;
30223 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30224 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30225 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30227 #ifdef HAVE_WINDOW_SYSTEM
30228 hourglass_atimer = NULL;
30229 hourglass_shown_p = 0;
30230 #endif /* HAVE_WINDOW_SYSTEM */
30232 DEFSYM (Qglyphless_char, "glyphless-char");
30233 DEFSYM (Qhex_code, "hex-code");
30234 DEFSYM (Qempty_box, "empty-box");
30235 DEFSYM (Qthin_space, "thin-space");
30236 DEFSYM (Qzero_width, "zero-width");
30238 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30239 doc: /* Function run just before redisplay.
30240 It is called with one argument, which is the set of windows that are to
30241 be redisplayed. This set can be nil (meaning, only the selected window),
30242 or t (meaning all windows). */);
30243 Vpre_redisplay_function = intern ("ignore");
30245 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30246 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30248 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30249 doc: /* Char-table defining glyphless characters.
30250 Each element, if non-nil, should be one of the following:
30251 an ASCII acronym string: display this string in a box
30252 `hex-code': display the hexadecimal code of a character in a box
30253 `empty-box': display as an empty box
30254 `thin-space': display as 1-pixel width space
30255 `zero-width': don't display
30256 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30257 display method for graphical terminals and text terminals respectively.
30258 GRAPHICAL and TEXT should each have one of the values listed above.
30260 The char-table has one extra slot to control the display of a character for
30261 which no font is found. This slot only takes effect on graphical terminals.
30262 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30263 `thin-space'. The default is `empty-box'.
30265 If a character has a non-nil entry in an active display table, the
30266 display table takes effect; in this case, Emacs does not consult
30267 `glyphless-char-display' at all. */);
30268 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30269 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30270 Qempty_box);
30272 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30273 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30274 Vdebug_on_message = Qnil;
30276 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30277 doc: /* */);
30278 Vredisplay__all_windows_cause
30279 = Fmake_vector (make_number (100), make_number (0));
30281 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30282 doc: /* */);
30283 Vredisplay__mode_lines_cause
30284 = Fmake_vector (make_number (100), make_number (0));
30288 /* Initialize this module when Emacs starts. */
30290 void
30291 init_xdisp (void)
30293 CHARPOS (this_line_start_pos) = 0;
30295 if (!noninteractive)
30297 struct window *m = XWINDOW (minibuf_window);
30298 Lisp_Object frame = m->frame;
30299 struct frame *f = XFRAME (frame);
30300 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30301 struct window *r = XWINDOW (root);
30302 int i;
30304 echo_area_window = minibuf_window;
30306 r->top_line = FRAME_TOP_MARGIN (f);
30307 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30308 r->total_cols = FRAME_COLS (f);
30309 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30310 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30311 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30313 m->top_line = FRAME_LINES (f) - 1;
30314 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30315 m->total_cols = FRAME_COLS (f);
30316 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30317 m->total_lines = 1;
30318 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30320 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30321 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30322 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30324 /* The default ellipsis glyphs `...'. */
30325 for (i = 0; i < 3; ++i)
30326 default_invis_vector[i] = make_number ('.');
30330 /* Allocate the buffer for frame titles.
30331 Also used for `format-mode-line'. */
30332 int size = 100;
30333 mode_line_noprop_buf = xmalloc (size);
30334 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30335 mode_line_noprop_ptr = mode_line_noprop_buf;
30336 mode_line_target = MODE_LINE_DISPLAY;
30339 help_echo_showing_p = 0;
30342 #ifdef HAVE_WINDOW_SYSTEM
30344 /* Platform-independent portion of hourglass implementation. */
30346 /* Cancel a currently active hourglass timer, and start a new one. */
30347 void
30348 start_hourglass (void)
30350 struct timespec delay;
30352 cancel_hourglass ();
30354 if (INTEGERP (Vhourglass_delay)
30355 && XINT (Vhourglass_delay) > 0)
30356 delay = make_timespec (min (XINT (Vhourglass_delay),
30357 TYPE_MAXIMUM (time_t)),
30359 else if (FLOATP (Vhourglass_delay)
30360 && XFLOAT_DATA (Vhourglass_delay) > 0)
30361 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30362 else
30363 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30365 #ifdef HAVE_NTGUI
30367 extern void w32_note_current_window (void);
30368 w32_note_current_window ();
30370 #endif /* HAVE_NTGUI */
30372 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30373 show_hourglass, NULL);
30377 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30378 shown. */
30379 void
30380 cancel_hourglass (void)
30382 if (hourglass_atimer)
30384 cancel_atimer (hourglass_atimer);
30385 hourglass_atimer = NULL;
30388 if (hourglass_shown_p)
30389 hide_hourglass ();
30392 #endif /* HAVE_WINDOW_SYSTEM */