Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / xdisp.c
blob8e2ac437ca3a3e3e291b6fe35f26bc82a5789eb5
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_max_ascent, 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 pixels = w->pixel_width;
1024 if (!w->pseudo_window_p)
1026 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 pixels -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1038 return pixels;
1042 /* Return the pixel height of the display area of window W, not
1043 including mode lines of W, if any. */
1046 window_box_height (struct window *w)
1048 struct frame *f = XFRAME (w->frame);
1049 int height = WINDOW_PIXEL_HEIGHT (w);
1051 eassert (height >= 0);
1053 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1061 if (WINDOW_WANTS_MODELINE_P (w))
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1097 int x;
1099 if (w->pseudo_window_p)
1100 return 0;
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1118 return x;
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1137 window_box_left (struct window *w, enum glyph_row_area area)
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1148 return x;
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1157 window_box_right (struct window *w, enum glyph_row_area area)
1159 return window_box_left (w, area) + window_box_width (w, area);
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1187 #ifdef HAVE_WINDOW_SYSTEM
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1206 #endif /* HAVE_WINDOW_SYSTEM */
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1216 line_bottom_y (struct it *it)
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1221 if (line_height == 0)
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1232 else
1234 struct glyph_row *row = it->glyph_row;
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1247 return line_top_y + line_height;
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1283 default_line_pixel_height (struct window *w)
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1303 if (addon >= 0)
1304 height += addon;
1307 else
1308 height += f->extra_line_spacing;
1311 return height;
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1319 if (CONSP (spec))
1321 while (CONSP (spec))
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1328 else if (VECTORP (spec))
1330 ptrdiff_t i;
1332 for (i = 0; i < ASIZE (spec); i++)
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1337 return Qnil;
1340 return spec;
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1357 return window_hscroll;
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1379 if (XBUFFER (w->contents) != current_buffer)
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = true;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1455 it = save_it;
1457 if (visible_p)
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1505 else if (IT_CHARPOS (it) != charpos)
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1638 while (EQ ((g - 1)->object, string))
1640 --g;
1641 top_x -= g->pixel_width;
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1658 else
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1670 visible_p = true;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1687 bidi_unshelve_cache (itdata, 0);
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1706 return visible_p;
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1718 int c;
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1727 return c;
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1738 eassert (STRINGP (string) && nchars >= 0);
1740 if (STRING_MULTIBYTE (string))
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1745 while (nchars--)
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1756 return pos;
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1781 struct text_pos pos;
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1786 if (multibyte_p)
1788 int len;
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1802 return pos;
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1812 ptrdiff_t nchars;
1814 if (multibyte_p)
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1820 for (nchars = 0; rest > 0; ++nchars)
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1826 else
1827 nchars = strlen (s);
1829 return nchars;
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1876 return height;
1878 #endif
1880 return 1;
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1913 /* PXW: Should we clip pixels before converting to columns/lines? */
1914 if (!noclip)
1916 if (pix_x < 0)
1917 pix_x = 0;
1918 else if (pix_x > FRAME_TOTAL_COLS (f))
1919 pix_x = FRAME_TOTAL_COLS (f);
1921 if (pix_y < 0)
1922 pix_y = 0;
1923 else if (pix_y > FRAME_LINES (f))
1924 pix_y = FRAME_LINES (f);
1927 #endif
1929 *x = pix_x;
1930 *y = pix_y;
1934 /* Find the glyph under window-relative coordinates X/Y in window W.
1935 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1936 strings. Return in *HPOS and *VPOS the row and column number of
1937 the glyph found. Return in *AREA the glyph area containing X.
1938 Value is a pointer to the glyph found or null if X/Y is not on
1939 text, or we can't tell because W's current matrix is not up to
1940 date. */
1942 static struct glyph *
1943 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1944 int *dx, int *dy, int *area)
1946 struct glyph *glyph, *end;
1947 struct glyph_row *row = NULL;
1948 int x0, i;
1950 /* Find row containing Y. Give up if some row is not enabled. */
1951 for (i = 0; i < w->current_matrix->nrows; ++i)
1953 row = MATRIX_ROW (w->current_matrix, i);
1954 if (!row->enabled_p)
1955 return NULL;
1956 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1957 break;
1960 *vpos = i;
1961 *hpos = 0;
1963 /* Give up if Y is not in the window. */
1964 if (i == w->current_matrix->nrows)
1965 return NULL;
1967 /* Get the glyph area containing X. */
1968 if (w->pseudo_window_p)
1970 *area = TEXT_AREA;
1971 x0 = 0;
1973 else
1975 if (x < window_box_left_offset (w, TEXT_AREA))
1977 *area = LEFT_MARGIN_AREA;
1978 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1980 else if (x < window_box_right_offset (w, TEXT_AREA))
1982 *area = TEXT_AREA;
1983 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1985 else
1987 *area = RIGHT_MARGIN_AREA;
1988 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1992 /* Find glyph containing X. */
1993 glyph = row->glyphs[*area];
1994 end = glyph + row->used[*area];
1995 x -= x0;
1996 while (glyph < end && x >= glyph->pixel_width)
1998 x -= glyph->pixel_width;
1999 ++glyph;
2002 if (glyph == end)
2003 return NULL;
2005 if (dx)
2007 *dx = x;
2008 *dy = y - (row->y + row->ascent - glyph->ascent);
2011 *hpos = glyph - row->glyphs[*area];
2012 return glyph;
2015 /* Convert frame-relative x/y to coordinates relative to window W.
2016 Takes pseudo-windows into account. */
2018 static void
2019 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2021 if (w->pseudo_window_p)
2023 /* A pseudo-window is always full-width, and starts at the
2024 left edge of the frame, plus a frame border. */
2025 struct frame *f = XFRAME (w->frame);
2026 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2027 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2029 else
2031 *x -= WINDOW_LEFT_EDGE_X (w);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2036 #ifdef HAVE_WINDOW_SYSTEM
2038 /* EXPORT:
2039 Return in RECTS[] at most N clipping rectangles for glyph string S.
2040 Return the number of stored rectangles. */
2043 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2045 XRectangle r;
2047 if (n <= 0)
2048 return 0;
2050 if (s->row->full_width_p)
2052 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2053 r.x = WINDOW_LEFT_EDGE_X (s->w);
2054 if (s->row->mode_line_p)
2055 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2056 else
2057 r.width = WINDOW_PIXEL_WIDTH (s->w);
2059 /* Unless displaying a mode or menu bar line, which are always
2060 fully visible, clip to the visible part of the row. */
2061 if (s->w->pseudo_window_p)
2062 r.height = s->row->visible_height;
2063 else
2064 r.height = s->height;
2066 else
2068 /* This is a text line that may be partially visible. */
2069 r.x = window_box_left (s->w, s->area);
2070 r.width = window_box_width (s->w, s->area);
2071 r.height = s->row->visible_height;
2074 if (s->clip_head)
2075 if (r.x < s->clip_head->x)
2077 if (r.width >= s->clip_head->x - r.x)
2078 r.width -= s->clip_head->x - r.x;
2079 else
2080 r.width = 0;
2081 r.x = s->clip_head->x;
2083 if (s->clip_tail)
2084 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2086 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2087 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2088 else
2089 r.width = 0;
2092 /* If S draws overlapping rows, it's sufficient to use the top and
2093 bottom of the window for clipping because this glyph string
2094 intentionally draws over other lines. */
2095 if (s->for_overlaps)
2097 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2098 r.height = window_text_bottom_y (s->w) - r.y;
2100 /* Alas, the above simple strategy does not work for the
2101 environments with anti-aliased text: if the same text is
2102 drawn onto the same place multiple times, it gets thicker.
2103 If the overlap we are processing is for the erased cursor, we
2104 take the intersection with the rectangle of the cursor. */
2105 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2107 XRectangle rc, r_save = r;
2109 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2110 rc.y = s->w->phys_cursor.y;
2111 rc.width = s->w->phys_cursor_width;
2112 rc.height = s->w->phys_cursor_height;
2114 x_intersect_rectangles (&r_save, &rc, &r);
2117 else
2119 /* Don't use S->y for clipping because it doesn't take partially
2120 visible lines into account. For example, it can be negative for
2121 partially visible lines at the top of a window. */
2122 if (!s->row->full_width_p
2123 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2124 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2125 else
2126 r.y = max (0, s->row->y);
2129 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2131 /* If drawing the cursor, don't let glyph draw outside its
2132 advertised boundaries. Cleartype does this under some circumstances. */
2133 if (s->hl == DRAW_CURSOR)
2135 struct glyph *glyph = s->first_glyph;
2136 int height, max_y;
2138 if (s->x > r.x)
2140 r.width -= s->x - r.x;
2141 r.x = s->x;
2143 r.width = min (r.width, glyph->pixel_width);
2145 /* If r.y is below window bottom, ensure that we still see a cursor. */
2146 height = min (glyph->ascent + glyph->descent,
2147 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2148 max_y = window_text_bottom_y (s->w) - height;
2149 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2150 if (s->ybase - glyph->ascent > max_y)
2152 r.y = max_y;
2153 r.height = height;
2155 else
2157 /* Don't draw cursor glyph taller than our actual glyph. */
2158 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2159 if (height < r.height)
2161 max_y = r.y + r.height;
2162 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2163 r.height = min (max_y - r.y, height);
2168 if (s->row->clip)
2170 XRectangle r_save = r;
2172 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2173 r.width = 0;
2176 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2177 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2179 #ifdef CONVERT_FROM_XRECT
2180 CONVERT_FROM_XRECT (r, *rects);
2181 #else
2182 *rects = r;
2183 #endif
2184 return 1;
2186 else
2188 /* If we are processing overlapping and allowed to return
2189 multiple clipping rectangles, we exclude the row of the glyph
2190 string from the clipping rectangle. This is to avoid drawing
2191 the same text on the environment with anti-aliasing. */
2192 #ifdef CONVERT_FROM_XRECT
2193 XRectangle rs[2];
2194 #else
2195 XRectangle *rs = rects;
2196 #endif
2197 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2199 if (s->for_overlaps & OVERLAPS_PRED)
2201 rs[i] = r;
2202 if (r.y + r.height > row_y)
2204 if (r.y < row_y)
2205 rs[i].height = row_y - r.y;
2206 else
2207 rs[i].height = 0;
2209 i++;
2211 if (s->for_overlaps & OVERLAPS_SUCC)
2213 rs[i] = r;
2214 if (r.y < row_y + s->row->visible_height)
2216 if (r.y + r.height > row_y + s->row->visible_height)
2218 rs[i].y = row_y + s->row->visible_height;
2219 rs[i].height = r.y + r.height - rs[i].y;
2221 else
2222 rs[i].height = 0;
2224 i++;
2227 n = i;
2228 #ifdef CONVERT_FROM_XRECT
2229 for (i = 0; i < n; i++)
2230 CONVERT_FROM_XRECT (rs[i], rects[i]);
2231 #endif
2232 return n;
2236 /* EXPORT:
2237 Return in *NR the clipping rectangle for glyph string S. */
2239 void
2240 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2242 get_glyph_string_clip_rects (s, nr, 1);
2246 /* EXPORT:
2247 Return the position and height of the phys cursor in window W.
2248 Set w->phys_cursor_width to width of phys cursor.
2251 void
2252 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2253 struct glyph *glyph, int *xp, int *yp, int *heightp)
2255 struct frame *f = XFRAME (WINDOW_FRAME (w));
2256 int x, y, wd, h, h0, y0;
2258 /* Compute the width of the rectangle to draw. If on a stretch
2259 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2260 rectangle as wide as the glyph, but use a canonical character
2261 width instead. */
2262 wd = glyph->pixel_width - 1;
2263 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2264 wd++; /* Why? */
2265 #endif
2267 x = w->phys_cursor.x;
2268 if (x < 0)
2270 wd += x;
2271 x = 0;
2274 if (glyph->type == STRETCH_GLYPH
2275 && !x_stretch_cursor_p)
2276 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2277 w->phys_cursor_width = wd;
2279 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2281 /* If y is below window bottom, ensure that we still see a cursor. */
2282 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2284 h = max (h0, glyph->ascent + glyph->descent);
2285 h0 = min (h0, glyph->ascent + glyph->descent);
2287 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2288 if (y < y0)
2290 h = max (h - (y0 - y) + 1, h0);
2291 y = y0 - 1;
2293 else
2295 y0 = window_text_bottom_y (w) - h0;
2296 if (y > y0)
2298 h += y - y0;
2299 y = y0;
2303 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2304 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2305 *heightp = h;
2309 * Remember which glyph the mouse is over.
2312 void
2313 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2315 Lisp_Object window;
2316 struct window *w;
2317 struct glyph_row *r, *gr, *end_row;
2318 enum window_part part;
2319 enum glyph_row_area area;
2320 int x, y, width, height;
2322 /* Try to determine frame pixel position and size of the glyph under
2323 frame pixel coordinates X/Y on frame F. */
2325 if (window_resize_pixelwise)
2327 width = height = 1;
2328 goto virtual_glyph;
2330 else if (!f->glyphs_initialized_p
2331 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2332 NILP (window)))
2334 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2335 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2336 goto virtual_glyph;
2339 w = XWINDOW (window);
2340 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2341 height = WINDOW_FRAME_LINE_HEIGHT (w);
2343 x = window_relative_x_coord (w, part, gx);
2344 y = gy - WINDOW_TOP_EDGE_Y (w);
2346 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2347 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2349 if (w->pseudo_window_p)
2351 area = TEXT_AREA;
2352 part = ON_MODE_LINE; /* Don't adjust margin. */
2353 goto text_glyph;
2356 switch (part)
2358 case ON_LEFT_MARGIN:
2359 area = LEFT_MARGIN_AREA;
2360 goto text_glyph;
2362 case ON_RIGHT_MARGIN:
2363 area = RIGHT_MARGIN_AREA;
2364 goto text_glyph;
2366 case ON_HEADER_LINE:
2367 case ON_MODE_LINE:
2368 gr = (part == ON_HEADER_LINE
2369 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2370 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2371 gy = gr->y;
2372 area = TEXT_AREA;
2373 goto text_glyph_row_found;
2375 case ON_TEXT:
2376 area = TEXT_AREA;
2378 text_glyph:
2379 gr = 0; gy = 0;
2380 for (; r <= end_row && r->enabled_p; ++r)
2381 if (r->y + r->height > y)
2383 gr = r; gy = r->y;
2384 break;
2387 text_glyph_row_found:
2388 if (gr && gy <= y)
2390 struct glyph *g = gr->glyphs[area];
2391 struct glyph *end = g + gr->used[area];
2393 height = gr->height;
2394 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2395 if (gx + g->pixel_width > x)
2396 break;
2398 if (g < end)
2400 if (g->type == IMAGE_GLYPH)
2402 /* Don't remember when mouse is over image, as
2403 image may have hot-spots. */
2404 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2405 return;
2407 width = g->pixel_width;
2409 else
2411 /* Use nominal char spacing at end of line. */
2412 x -= gx;
2413 gx += (x / width) * width;
2416 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2417 gx += window_box_left_offset (w, area);
2419 else
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2426 break;
2428 case ON_LEFT_FRINGE:
2429 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2431 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2432 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2433 goto row_glyph;
2435 case ON_RIGHT_FRINGE:
2436 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 : window_box_right_offset (w, TEXT_AREA));
2439 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2440 goto row_glyph;
2442 case ON_SCROLL_BAR:
2443 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2445 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2446 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2447 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2448 : 0)));
2449 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2451 row_glyph:
2452 gr = 0, gy = 0;
2453 for (; r <= end_row && r->enabled_p; ++r)
2454 if (r->y + r->height > y)
2456 gr = r; gy = r->y;
2457 break;
2460 if (gr && gy <= y)
2461 height = gr->height;
2462 else
2464 /* Use nominal line height at end of window. */
2465 y -= gy;
2466 gy += (y / height) * height;
2468 break;
2470 default:
2472 virtual_glyph:
2473 /* If there is no glyph under the mouse, then we divide the screen
2474 into a grid of the smallest glyph in the frame, and use that
2475 as our "glyph". */
2477 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2478 round down even for negative values. */
2479 if (gx < 0)
2480 gx -= width - 1;
2481 if (gy < 0)
2482 gy -= height - 1;
2484 gx = (gx / width) * width;
2485 gy = (gy / height) * height;
2487 goto store_rect;
2490 gx += WINDOW_LEFT_EDGE_X (w);
2491 gy += WINDOW_TOP_EDGE_Y (w);
2493 store_rect:
2494 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2496 /* Visible feedback for debugging. */
2497 #if 0
2498 #if HAVE_X_WINDOWS
2499 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2500 f->output_data.x->normal_gc,
2501 gx, gy, width, height);
2502 #endif
2503 #endif
2507 #endif /* HAVE_WINDOW_SYSTEM */
2509 static void
2510 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2512 eassert (w);
2513 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2514 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2515 w->window_end_vpos
2516 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2519 /***********************************************************************
2520 Lisp form evaluation
2521 ***********************************************************************/
2523 /* Error handler for safe_eval and safe_call. */
2525 static Lisp_Object
2526 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2528 add_to_log ("Error during redisplay: %S signaled %S",
2529 Flist (nargs, args), arg);
2530 return Qnil;
2533 /* Call function FUNC with the rest of NARGS - 1 arguments
2534 following. Return the result, or nil if something went
2535 wrong. Prevent redisplay during the evaluation. */
2537 Lisp_Object
2538 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2540 Lisp_Object val;
2542 if (inhibit_eval_during_redisplay)
2543 val = Qnil;
2544 else
2546 va_list ap;
2547 ptrdiff_t i;
2548 ptrdiff_t count = SPECPDL_INDEX ();
2549 struct gcpro gcpro1;
2550 Lisp_Object *args = alloca (nargs * word_size);
2552 args[0] = func;
2553 va_start (ap, func);
2554 for (i = 1; i < nargs; i++)
2555 args[i] = va_arg (ap, Lisp_Object);
2556 va_end (ap);
2558 GCPRO1 (args[0]);
2559 gcpro1.nvars = nargs;
2560 specbind (Qinhibit_redisplay, Qt);
2561 /* Use Qt to ensure debugger does not run,
2562 so there is no possibility of wanting to redisplay. */
2563 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2564 safe_eval_handler);
2565 UNGCPRO;
2566 val = unbind_to (count, val);
2569 return val;
2573 /* Call function FN with one argument ARG.
2574 Return the result, or nil if something went wrong. */
2576 Lisp_Object
2577 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2579 return safe_call (2, fn, arg);
2582 static Lisp_Object Qeval;
2584 Lisp_Object
2585 safe_eval (Lisp_Object sexpr)
2587 return safe_call1 (Qeval, sexpr);
2590 /* Call function FN with two arguments ARG1 and ARG2.
2591 Return the result, or nil if something went wrong. */
2593 Lisp_Object
2594 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2596 return safe_call (3, fn, arg1, arg2);
2601 /***********************************************************************
2602 Debugging
2603 ***********************************************************************/
2605 #if 0
2607 /* Define CHECK_IT to perform sanity checks on iterators.
2608 This is for debugging. It is too slow to do unconditionally. */
2610 static void
2611 check_it (struct it *it)
2613 if (it->method == GET_FROM_STRING)
2615 eassert (STRINGP (it->string));
2616 eassert (IT_STRING_CHARPOS (*it) >= 0);
2618 else
2620 eassert (IT_STRING_CHARPOS (*it) < 0);
2621 if (it->method == GET_FROM_BUFFER)
2623 /* Check that character and byte positions agree. */
2624 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2628 if (it->dpvec)
2629 eassert (it->current.dpvec_index >= 0);
2630 else
2631 eassert (it->current.dpvec_index < 0);
2634 #define CHECK_IT(IT) check_it ((IT))
2636 #else /* not 0 */
2638 #define CHECK_IT(IT) (void) 0
2640 #endif /* not 0 */
2643 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2645 /* Check that the window end of window W is what we expect it
2646 to be---the last row in the current matrix displaying text. */
2648 static void
2649 check_window_end (struct window *w)
2651 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2653 struct glyph_row *row;
2654 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2655 !row->enabled_p
2656 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2657 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2661 #define CHECK_WINDOW_END(W) check_window_end ((W))
2663 #else
2665 #define CHECK_WINDOW_END(W) (void) 0
2667 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2669 /***********************************************************************
2670 Iterator initialization
2671 ***********************************************************************/
2673 /* Initialize IT for displaying current_buffer in window W, starting
2674 at character position CHARPOS. CHARPOS < 0 means that no buffer
2675 position is specified which is useful when the iterator is assigned
2676 a position later. BYTEPOS is the byte position corresponding to
2677 CHARPOS.
2679 If ROW is not null, calls to produce_glyphs with IT as parameter
2680 will produce glyphs in that row.
2682 BASE_FACE_ID is the id of a base face to use. It must be one of
2683 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2684 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2685 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2687 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2688 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2689 will be initialized to use the corresponding mode line glyph row of
2690 the desired matrix of W. */
2692 void
2693 init_iterator (struct it *it, struct window *w,
2694 ptrdiff_t charpos, ptrdiff_t bytepos,
2695 struct glyph_row *row, enum face_id base_face_id)
2697 enum face_id remapped_base_face_id = base_face_id;
2699 /* Some precondition checks. */
2700 eassert (w != NULL && it != NULL);
2701 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2702 && charpos <= ZV));
2704 /* If face attributes have been changed since the last redisplay,
2705 free realized faces now because they depend on face definitions
2706 that might have changed. Don't free faces while there might be
2707 desired matrices pending which reference these faces. */
2708 if (face_change_count && !inhibit_free_realized_faces)
2710 face_change_count = 0;
2711 free_all_realized_faces (Qnil);
2714 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2715 if (! NILP (Vface_remapping_alist))
2716 remapped_base_face_id
2717 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2719 /* Use one of the mode line rows of W's desired matrix if
2720 appropriate. */
2721 if (row == NULL)
2723 if (base_face_id == MODE_LINE_FACE_ID
2724 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2725 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2726 else if (base_face_id == HEADER_LINE_FACE_ID)
2727 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2730 /* Clear IT. */
2731 memset (it, 0, sizeof *it);
2732 it->current.overlay_string_index = -1;
2733 it->current.dpvec_index = -1;
2734 it->base_face_id = remapped_base_face_id;
2735 it->string = Qnil;
2736 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2737 it->paragraph_embedding = L2R;
2738 it->bidi_it.string.lstring = Qnil;
2739 it->bidi_it.string.s = NULL;
2740 it->bidi_it.string.bufpos = 0;
2741 it->bidi_it.w = w;
2743 /* The window in which we iterate over current_buffer: */
2744 XSETWINDOW (it->window, w);
2745 it->w = w;
2746 it->f = XFRAME (w->frame);
2748 it->cmp_it.id = -1;
2750 /* Extra space between lines (on window systems only). */
2751 if (base_face_id == DEFAULT_FACE_ID
2752 && FRAME_WINDOW_P (it->f))
2754 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2755 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2756 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2757 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2758 * FRAME_LINE_HEIGHT (it->f));
2759 else if (it->f->extra_line_spacing > 0)
2760 it->extra_line_spacing = it->f->extra_line_spacing;
2761 it->max_extra_line_spacing = 0;
2764 /* If realized faces have been removed, e.g. because of face
2765 attribute changes of named faces, recompute them. When running
2766 in batch mode, the face cache of the initial frame is null. If
2767 we happen to get called, make a dummy face cache. */
2768 if (FRAME_FACE_CACHE (it->f) == NULL)
2769 init_frame_faces (it->f);
2770 if (FRAME_FACE_CACHE (it->f)->used == 0)
2771 recompute_basic_faces (it->f);
2773 /* Current value of the `slice', `space-width', and 'height' properties. */
2774 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2775 it->space_width = Qnil;
2776 it->font_height = Qnil;
2777 it->override_ascent = -1;
2779 /* Are control characters displayed as `^C'? */
2780 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2782 /* -1 means everything between a CR and the following line end
2783 is invisible. >0 means lines indented more than this value are
2784 invisible. */
2785 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2786 ? (clip_to_bounds
2787 (-1, XINT (BVAR (current_buffer, selective_display)),
2788 PTRDIFF_MAX))
2789 : (!NILP (BVAR (current_buffer, selective_display))
2790 ? -1 : 0));
2791 it->selective_display_ellipsis_p
2792 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2794 /* Display table to use. */
2795 it->dp = window_display_table (w);
2797 /* Are multibyte characters enabled in current_buffer? */
2798 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2800 /* Get the position at which the redisplay_end_trigger hook should
2801 be run, if it is to be run at all. */
2802 if (MARKERP (w->redisplay_end_trigger)
2803 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2804 it->redisplay_end_trigger_charpos
2805 = marker_position (w->redisplay_end_trigger);
2806 else if (INTEGERP (w->redisplay_end_trigger))
2807 it->redisplay_end_trigger_charpos =
2808 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2810 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2812 /* Are lines in the display truncated? */
2813 if (base_face_id != DEFAULT_FACE_ID
2814 || it->w->hscroll
2815 || (! WINDOW_FULL_WIDTH_P (it->w)
2816 && ((!NILP (Vtruncate_partial_width_windows)
2817 && !INTEGERP (Vtruncate_partial_width_windows))
2818 || (INTEGERP (Vtruncate_partial_width_windows)
2819 /* PXW: Shall we do something about this? */
2820 && (WINDOW_TOTAL_COLS (it->w)
2821 < XINT (Vtruncate_partial_width_windows))))))
2822 it->line_wrap = TRUNCATE;
2823 else if (NILP (BVAR (current_buffer, truncate_lines)))
2824 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2825 ? WINDOW_WRAP : WORD_WRAP;
2826 else
2827 it->line_wrap = TRUNCATE;
2829 /* Get dimensions of truncation and continuation glyphs. These are
2830 displayed as fringe bitmaps under X, but we need them for such
2831 frames when the fringes are turned off. But leave the dimensions
2832 zero for tooltip frames, as these glyphs look ugly there and also
2833 sabotage calculations of tooltip dimensions in x-show-tip. */
2834 #ifdef HAVE_WINDOW_SYSTEM
2835 if (!(FRAME_WINDOW_P (it->f)
2836 && FRAMEP (tip_frame)
2837 && it->f == XFRAME (tip_frame)))
2838 #endif
2840 if (it->line_wrap == TRUNCATE)
2842 /* We will need the truncation glyph. */
2843 eassert (it->glyph_row == NULL);
2844 produce_special_glyphs (it, IT_TRUNCATION);
2845 it->truncation_pixel_width = it->pixel_width;
2847 else
2849 /* We will need the continuation glyph. */
2850 eassert (it->glyph_row == NULL);
2851 produce_special_glyphs (it, IT_CONTINUATION);
2852 it->continuation_pixel_width = it->pixel_width;
2856 /* Reset these values to zero because the produce_special_glyphs
2857 above has changed them. */
2858 it->pixel_width = it->ascent = it->descent = 0;
2859 it->phys_ascent = it->phys_descent = 0;
2861 /* Set this after getting the dimensions of truncation and
2862 continuation glyphs, so that we don't produce glyphs when calling
2863 produce_special_glyphs, above. */
2864 it->glyph_row = row;
2865 it->area = TEXT_AREA;
2867 /* Forget any previous info about this row being reversed. */
2868 if (it->glyph_row)
2869 it->glyph_row->reversed_p = 0;
2871 /* Get the dimensions of the display area. The display area
2872 consists of the visible window area plus a horizontally scrolled
2873 part to the left of the window. All x-values are relative to the
2874 start of this total display area. */
2875 if (base_face_id != DEFAULT_FACE_ID)
2877 /* Mode lines, menu bar in terminal frames. */
2878 it->first_visible_x = 0;
2879 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2881 else
2883 it->first_visible_x
2884 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2885 it->last_visible_x = (it->first_visible_x
2886 + window_box_width (w, TEXT_AREA));
2888 /* If we truncate lines, leave room for the truncation glyph(s) at
2889 the right margin. Otherwise, leave room for the continuation
2890 glyph(s). Done only if the window has no fringes. Since we
2891 don't know at this point whether there will be any R2L lines in
2892 the window, we reserve space for truncation/continuation glyphs
2893 even if only one of the fringes is absent. */
2894 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2895 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2897 if (it->line_wrap == TRUNCATE)
2898 it->last_visible_x -= it->truncation_pixel_width;
2899 else
2900 it->last_visible_x -= it->continuation_pixel_width;
2903 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2904 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2907 /* Leave room for a border glyph. */
2908 if (!FRAME_WINDOW_P (it->f)
2909 && !WINDOW_RIGHTMOST_P (it->w))
2910 it->last_visible_x -= 1;
2912 it->last_visible_y = window_text_bottom_y (w);
2914 /* For mode lines and alike, arrange for the first glyph having a
2915 left box line if the face specifies a box. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2918 struct face *face;
2920 it->face_id = remapped_base_face_id;
2922 /* If we have a boxed mode line, make the first character appear
2923 with a left box line. */
2924 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2925 if (face->box != FACE_NO_BOX)
2926 it->start_of_box_run_p = true;
2929 /* If a buffer position was specified, set the iterator there,
2930 getting overlays and face properties from that position. */
2931 if (charpos >= BUF_BEG (current_buffer))
2933 it->end_charpos = ZV;
2934 eassert (charpos == BYTE_TO_CHAR (bytepos));
2935 IT_CHARPOS (*it) = charpos;
2936 IT_BYTEPOS (*it) = bytepos;
2938 /* We will rely on `reseat' to set this up properly, via
2939 handle_face_prop. */
2940 it->face_id = it->base_face_id;
2942 it->start = it->current;
2943 /* Do we need to reorder bidirectional text? Not if this is a
2944 unibyte buffer: by definition, none of the single-byte
2945 characters are strong R2L, so no reordering is needed. And
2946 bidi.c doesn't support unibyte buffers anyway. Also, don't
2947 reorder while we are loading loadup.el, since the tables of
2948 character properties needed for reordering are not yet
2949 available. */
2950 it->bidi_p =
2951 NILP (Vpurify_flag)
2952 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2953 && it->multibyte_p;
2955 /* If we are to reorder bidirectional text, init the bidi
2956 iterator. */
2957 if (it->bidi_p)
2959 /* Note the paragraph direction that this buffer wants to
2960 use. */
2961 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qleft_to_right))
2963 it->paragraph_embedding = L2R;
2964 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2965 Qright_to_left))
2966 it->paragraph_embedding = R2L;
2967 else
2968 it->paragraph_embedding = NEUTRAL_DIR;
2969 bidi_unshelve_cache (NULL, 0);
2970 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2971 &it->bidi_it);
2974 /* Compute faces etc. */
2975 reseat (it, it->current.pos, 1);
2978 CHECK_IT (it);
2982 /* Initialize IT for the display of window W with window start POS. */
2984 void
2985 start_display (struct it *it, struct window *w, struct text_pos pos)
2987 struct glyph_row *row;
2988 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2990 row = w->desired_matrix->rows + first_vpos;
2991 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2992 it->first_vpos = first_vpos;
2994 /* Don't reseat to previous visible line start if current start
2995 position is in a string or image. */
2996 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2998 int start_at_line_beg_p;
2999 int first_y = it->current_y;
3001 /* If window start is not at a line start, skip forward to POS to
3002 get the correct continuation lines width. */
3003 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3004 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3005 if (!start_at_line_beg_p)
3007 int new_x;
3009 reseat_at_previous_visible_line_start (it);
3010 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3012 new_x = it->current_x + it->pixel_width;
3014 /* If lines are continued, this line may end in the middle
3015 of a multi-glyph character (e.g. a control character
3016 displayed as \003, or in the middle of an overlay
3017 string). In this case move_it_to above will not have
3018 taken us to the start of the continuation line but to the
3019 end of the continued line. */
3020 if (it->current_x > 0
3021 && it->line_wrap != TRUNCATE /* Lines are continued. */
3022 && (/* And glyph doesn't fit on the line. */
3023 new_x > it->last_visible_x
3024 /* Or it fits exactly and we're on a window
3025 system frame. */
3026 || (new_x == it->last_visible_x
3027 && FRAME_WINDOW_P (it->f)
3028 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3029 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3030 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3032 if ((it->current.dpvec_index >= 0
3033 || it->current.overlay_string_index >= 0)
3034 /* If we are on a newline from a display vector or
3035 overlay string, then we are already at the end of
3036 a screen line; no need to go to the next line in
3037 that case, as this line is not really continued.
3038 (If we do go to the next line, C-e will not DTRT.) */
3039 && it->c != '\n')
3041 set_iterator_to_next (it, 1);
3042 move_it_in_display_line_to (it, -1, -1, 0);
3045 it->continuation_lines_width += it->current_x;
3047 /* If the character at POS is displayed via a display
3048 vector, move_it_to above stops at the final glyph of
3049 IT->dpvec. To make the caller redisplay that character
3050 again (a.k.a. start at POS), we need to reset the
3051 dpvec_index to the beginning of IT->dpvec. */
3052 else if (it->current.dpvec_index >= 0)
3053 it->current.dpvec_index = 0;
3055 /* We're starting a new display line, not affected by the
3056 height of the continued line, so clear the appropriate
3057 fields in the iterator structure. */
3058 it->max_ascent = it->max_descent = 0;
3059 it->max_phys_ascent = it->max_phys_descent = 0;
3061 it->current_y = first_y;
3062 it->vpos = 0;
3063 it->current_x = it->hpos = 0;
3069 /* Return 1 if POS is a position in ellipses displayed for invisible
3070 text. W is the window we display, for text property lookup. */
3072 static int
3073 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3075 Lisp_Object prop, window;
3076 int ellipses_p = 0;
3077 ptrdiff_t charpos = CHARPOS (pos->pos);
3079 /* If POS specifies a position in a display vector, this might
3080 be for an ellipsis displayed for invisible text. We won't
3081 get the iterator set up for delivering that ellipsis unless
3082 we make sure that it gets aware of the invisible text. */
3083 if (pos->dpvec_index >= 0
3084 && pos->overlay_string_index < 0
3085 && CHARPOS (pos->string_pos) < 0
3086 && charpos > BEGV
3087 && (XSETWINDOW (window, w),
3088 prop = Fget_char_property (make_number (charpos),
3089 Qinvisible, window),
3090 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3092 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3093 window);
3094 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3097 return ellipses_p;
3101 /* Initialize IT for stepping through current_buffer in window W,
3102 starting at position POS that includes overlay string and display
3103 vector/ control character translation position information. Value
3104 is zero if there are overlay strings with newlines at POS. */
3106 static int
3107 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3109 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3110 int i, overlay_strings_with_newlines = 0;
3112 /* If POS specifies a position in a display vector, this might
3113 be for an ellipsis displayed for invisible text. We won't
3114 get the iterator set up for delivering that ellipsis unless
3115 we make sure that it gets aware of the invisible text. */
3116 if (in_ellipses_for_invisible_text_p (pos, w))
3118 --charpos;
3119 bytepos = 0;
3122 /* Keep in mind: the call to reseat in init_iterator skips invisible
3123 text, so we might end up at a position different from POS. This
3124 is only a problem when POS is a row start after a newline and an
3125 overlay starts there with an after-string, and the overlay has an
3126 invisible property. Since we don't skip invisible text in
3127 display_line and elsewhere immediately after consuming the
3128 newline before the row start, such a POS will not be in a string,
3129 but the call to init_iterator below will move us to the
3130 after-string. */
3131 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3133 /* This only scans the current chunk -- it should scan all chunks.
3134 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3135 to 16 in 22.1 to make this a lesser problem. */
3136 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3138 const char *s = SSDATA (it->overlay_strings[i]);
3139 const char *e = s + SBYTES (it->overlay_strings[i]);
3141 while (s < e && *s != '\n')
3142 ++s;
3144 if (s < e)
3146 overlay_strings_with_newlines = 1;
3147 break;
3151 /* If position is within an overlay string, set up IT to the right
3152 overlay string. */
3153 if (pos->overlay_string_index >= 0)
3155 int relative_index;
3157 /* If the first overlay string happens to have a `display'
3158 property for an image, the iterator will be set up for that
3159 image, and we have to undo that setup first before we can
3160 correct the overlay string index. */
3161 if (it->method == GET_FROM_IMAGE)
3162 pop_it (it);
3164 /* We already have the first chunk of overlay strings in
3165 IT->overlay_strings. Load more until the one for
3166 pos->overlay_string_index is in IT->overlay_strings. */
3167 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3169 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3170 it->current.overlay_string_index = 0;
3171 while (n--)
3173 load_overlay_strings (it, 0);
3174 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3178 it->current.overlay_string_index = pos->overlay_string_index;
3179 relative_index = (it->current.overlay_string_index
3180 % OVERLAY_STRING_CHUNK_SIZE);
3181 it->string = it->overlay_strings[relative_index];
3182 eassert (STRINGP (it->string));
3183 it->current.string_pos = pos->string_pos;
3184 it->method = GET_FROM_STRING;
3185 it->end_charpos = SCHARS (it->string);
3186 /* Set up the bidi iterator for this overlay string. */
3187 if (it->bidi_p)
3189 it->bidi_it.string.lstring = it->string;
3190 it->bidi_it.string.s = NULL;
3191 it->bidi_it.string.schars = SCHARS (it->string);
3192 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3193 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3194 it->bidi_it.string.unibyte = !it->multibyte_p;
3195 it->bidi_it.w = it->w;
3196 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3197 FRAME_WINDOW_P (it->f), &it->bidi_it);
3199 /* Synchronize the state of the bidi iterator with
3200 pos->string_pos. For any string position other than
3201 zero, this will be done automagically when we resume
3202 iteration over the string and get_visually_first_element
3203 is called. But if string_pos is zero, and the string is
3204 to be reordered for display, we need to resync manually,
3205 since it could be that the iteration state recorded in
3206 pos ended at string_pos of 0 moving backwards in string. */
3207 if (CHARPOS (pos->string_pos) == 0)
3209 get_visually_first_element (it);
3210 if (IT_STRING_CHARPOS (*it) != 0)
3211 do {
3212 /* Paranoia. */
3213 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3214 bidi_move_to_visually_next (&it->bidi_it);
3215 } while (it->bidi_it.charpos != 0);
3217 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3218 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3222 if (CHARPOS (pos->string_pos) >= 0)
3224 /* Recorded position is not in an overlay string, but in another
3225 string. This can only be a string from a `display' property.
3226 IT should already be filled with that string. */
3227 it->current.string_pos = pos->string_pos;
3228 eassert (STRINGP (it->string));
3229 if (it->bidi_p)
3230 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3231 FRAME_WINDOW_P (it->f), &it->bidi_it);
3234 /* Restore position in display vector translations, control
3235 character translations or ellipses. */
3236 if (pos->dpvec_index >= 0)
3238 if (it->dpvec == NULL)
3239 get_next_display_element (it);
3240 eassert (it->dpvec && it->current.dpvec_index == 0);
3241 it->current.dpvec_index = pos->dpvec_index;
3244 CHECK_IT (it);
3245 return !overlay_strings_with_newlines;
3249 /* Initialize IT for stepping through current_buffer in window W
3250 starting at ROW->start. */
3252 static void
3253 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3255 init_from_display_pos (it, w, &row->start);
3256 it->start = row->start;
3257 it->continuation_lines_width = row->continuation_lines_width;
3258 CHECK_IT (it);
3262 /* Initialize IT for stepping through current_buffer in window W
3263 starting in the line following ROW, i.e. starting at ROW->end.
3264 Value is zero if there are overlay strings with newlines at ROW's
3265 end position. */
3267 static int
3268 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3270 int success = 0;
3272 if (init_from_display_pos (it, w, &row->end))
3274 if (row->continued_p)
3275 it->continuation_lines_width
3276 = row->continuation_lines_width + row->pixel_width;
3277 CHECK_IT (it);
3278 success = 1;
3281 return success;
3287 /***********************************************************************
3288 Text properties
3289 ***********************************************************************/
3291 /* Called when IT reaches IT->stop_charpos. Handle text property and
3292 overlay changes. Set IT->stop_charpos to the next position where
3293 to stop. */
3295 static void
3296 handle_stop (struct it *it)
3298 enum prop_handled handled;
3299 int handle_overlay_change_p;
3300 struct props *p;
3302 it->dpvec = NULL;
3303 it->current.dpvec_index = -1;
3304 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3305 it->ignore_overlay_strings_at_pos_p = 0;
3306 it->ellipsis_p = 0;
3308 /* Use face of preceding text for ellipsis (if invisible) */
3309 if (it->selective_display_ellipsis_p)
3310 it->saved_face_id = it->face_id;
3314 handled = HANDLED_NORMALLY;
3316 /* Call text property handlers. */
3317 for (p = it_props; p->handler; ++p)
3319 handled = p->handler (it);
3321 if (handled == HANDLED_RECOMPUTE_PROPS)
3322 break;
3323 else if (handled == HANDLED_RETURN)
3325 /* We still want to show before and after strings from
3326 overlays even if the actual buffer text is replaced. */
3327 if (!handle_overlay_change_p
3328 || it->sp > 1
3329 /* Don't call get_overlay_strings_1 if we already
3330 have overlay strings loaded, because doing so
3331 will load them again and push the iterator state
3332 onto the stack one more time, which is not
3333 expected by the rest of the code that processes
3334 overlay strings. */
3335 || (it->current.overlay_string_index < 0
3336 ? !get_overlay_strings_1 (it, 0, 0)
3337 : 0))
3339 if (it->ellipsis_p)
3340 setup_for_ellipsis (it, 0);
3341 /* When handling a display spec, we might load an
3342 empty string. In that case, discard it here. We
3343 used to discard it in handle_single_display_spec,
3344 but that causes get_overlay_strings_1, above, to
3345 ignore overlay strings that we must check. */
3346 if (STRINGP (it->string) && !SCHARS (it->string))
3347 pop_it (it);
3348 return;
3350 else if (STRINGP (it->string) && !SCHARS (it->string))
3351 pop_it (it);
3352 else
3354 it->ignore_overlay_strings_at_pos_p = true;
3355 it->string_from_display_prop_p = 0;
3356 it->from_disp_prop_p = 0;
3357 handle_overlay_change_p = 0;
3359 handled = HANDLED_RECOMPUTE_PROPS;
3360 break;
3362 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3363 handle_overlay_change_p = 0;
3366 if (handled != HANDLED_RECOMPUTE_PROPS)
3368 /* Don't check for overlay strings below when set to deliver
3369 characters from a display vector. */
3370 if (it->method == GET_FROM_DISPLAY_VECTOR)
3371 handle_overlay_change_p = 0;
3373 /* Handle overlay changes.
3374 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3375 if it finds overlays. */
3376 if (handle_overlay_change_p)
3377 handled = handle_overlay_change (it);
3380 if (it->ellipsis_p)
3382 setup_for_ellipsis (it, 0);
3383 break;
3386 while (handled == HANDLED_RECOMPUTE_PROPS);
3388 /* Determine where to stop next. */
3389 if (handled == HANDLED_NORMALLY)
3390 compute_stop_pos (it);
3394 /* Compute IT->stop_charpos from text property and overlay change
3395 information for IT's current position. */
3397 static void
3398 compute_stop_pos (struct it *it)
3400 register INTERVAL iv, next_iv;
3401 Lisp_Object object, limit, position;
3402 ptrdiff_t charpos, bytepos;
3404 if (STRINGP (it->string))
3406 /* Strings are usually short, so don't limit the search for
3407 properties. */
3408 it->stop_charpos = it->end_charpos;
3409 object = it->string;
3410 limit = Qnil;
3411 charpos = IT_STRING_CHARPOS (*it);
3412 bytepos = IT_STRING_BYTEPOS (*it);
3414 else
3416 ptrdiff_t pos;
3418 /* If end_charpos is out of range for some reason, such as a
3419 misbehaving display function, rationalize it (Bug#5984). */
3420 if (it->end_charpos > ZV)
3421 it->end_charpos = ZV;
3422 it->stop_charpos = it->end_charpos;
3424 /* If next overlay change is in front of the current stop pos
3425 (which is IT->end_charpos), stop there. Note: value of
3426 next_overlay_change is point-max if no overlay change
3427 follows. */
3428 charpos = IT_CHARPOS (*it);
3429 bytepos = IT_BYTEPOS (*it);
3430 pos = next_overlay_change (charpos);
3431 if (pos < it->stop_charpos)
3432 it->stop_charpos = pos;
3434 /* Set up variables for computing the stop position from text
3435 property changes. */
3436 XSETBUFFER (object, current_buffer);
3437 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3440 /* Get the interval containing IT's position. Value is a null
3441 interval if there isn't such an interval. */
3442 position = make_number (charpos);
3443 iv = validate_interval_range (object, &position, &position, 0);
3444 if (iv)
3446 Lisp_Object values_here[LAST_PROP_IDX];
3447 struct props *p;
3449 /* Get properties here. */
3450 for (p = it_props; p->handler; ++p)
3451 values_here[p->idx] = textget (iv->plist, *p->name);
3453 /* Look for an interval following iv that has different
3454 properties. */
3455 for (next_iv = next_interval (iv);
3456 (next_iv
3457 && (NILP (limit)
3458 || XFASTINT (limit) > next_iv->position));
3459 next_iv = next_interval (next_iv))
3461 for (p = it_props; p->handler; ++p)
3463 Lisp_Object new_value;
3465 new_value = textget (next_iv->plist, *p->name);
3466 if (!EQ (values_here[p->idx], new_value))
3467 break;
3470 if (p->handler)
3471 break;
3474 if (next_iv)
3476 if (INTEGERP (limit)
3477 && next_iv->position >= XFASTINT (limit))
3478 /* No text property change up to limit. */
3479 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3480 else
3481 /* Text properties change in next_iv. */
3482 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3486 if (it->cmp_it.id < 0)
3488 ptrdiff_t stoppos = it->end_charpos;
3490 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3491 stoppos = -1;
3492 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3493 stoppos, it->string);
3496 eassert (STRINGP (it->string)
3497 || (it->stop_charpos >= BEGV
3498 && it->stop_charpos >= IT_CHARPOS (*it)));
3502 /* Return the position of the next overlay change after POS in
3503 current_buffer. Value is point-max if no overlay change
3504 follows. This is like `next-overlay-change' but doesn't use
3505 xmalloc. */
3507 static ptrdiff_t
3508 next_overlay_change (ptrdiff_t pos)
3510 ptrdiff_t i, noverlays;
3511 ptrdiff_t endpos;
3512 Lisp_Object *overlays;
3514 /* Get all overlays at the given position. */
3515 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3517 /* If any of these overlays ends before endpos,
3518 use its ending point instead. */
3519 for (i = 0; i < noverlays; ++i)
3521 Lisp_Object oend;
3522 ptrdiff_t oendpos;
3524 oend = OVERLAY_END (overlays[i]);
3525 oendpos = OVERLAY_POSITION (oend);
3526 endpos = min (endpos, oendpos);
3529 return endpos;
3532 /* How many characters forward to search for a display property or
3533 display string. Searching too far forward makes the bidi display
3534 sluggish, especially in small windows. */
3535 #define MAX_DISP_SCAN 250
3537 /* Return the character position of a display string at or after
3538 position specified by POSITION. If no display string exists at or
3539 after POSITION, return ZV. A display string is either an overlay
3540 with `display' property whose value is a string, or a `display'
3541 text property whose value is a string. STRING is data about the
3542 string to iterate; if STRING->lstring is nil, we are iterating a
3543 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3544 on a GUI frame. DISP_PROP is set to zero if we searched
3545 MAX_DISP_SCAN characters forward without finding any display
3546 strings, non-zero otherwise. It is set to 2 if the display string
3547 uses any kind of `(space ...)' spec that will produce a stretch of
3548 white space in the text area. */
3549 ptrdiff_t
3550 compute_display_string_pos (struct text_pos *position,
3551 struct bidi_string_data *string,
3552 struct window *w,
3553 int frame_window_p, int *disp_prop)
3555 /* OBJECT = nil means current buffer. */
3556 Lisp_Object object, object1;
3557 Lisp_Object pos, spec, limpos;
3558 int string_p = (string && (STRINGP (string->lstring) || string->s));
3559 ptrdiff_t eob = string_p ? string->schars : ZV;
3560 ptrdiff_t begb = string_p ? 0 : BEGV;
3561 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3562 ptrdiff_t lim =
3563 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3564 struct text_pos tpos;
3565 int rv = 0;
3567 if (string && STRINGP (string->lstring))
3568 object1 = object = string->lstring;
3569 else if (w && !string_p)
3571 XSETWINDOW (object, w);
3572 object1 = Qnil;
3574 else
3575 object1 = object = Qnil;
3577 *disp_prop = 1;
3579 if (charpos >= eob
3580 /* We don't support display properties whose values are strings
3581 that have display string properties. */
3582 || string->from_disp_str
3583 /* C strings cannot have display properties. */
3584 || (string->s && !STRINGP (object)))
3586 *disp_prop = 0;
3587 return eob;
3590 /* If the character at CHARPOS is where the display string begins,
3591 return CHARPOS. */
3592 pos = make_number (charpos);
3593 if (STRINGP (object))
3594 bufpos = string->bufpos;
3595 else
3596 bufpos = charpos;
3597 tpos = *position;
3598 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3599 && (charpos <= begb
3600 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3601 object),
3602 spec))
3603 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3604 frame_window_p)))
3606 if (rv == 2)
3607 *disp_prop = 2;
3608 return charpos;
3611 /* Look forward for the first character with a `display' property
3612 that will replace the underlying text when displayed. */
3613 limpos = make_number (lim);
3614 do {
3615 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3616 CHARPOS (tpos) = XFASTINT (pos);
3617 if (CHARPOS (tpos) >= lim)
3619 *disp_prop = 0;
3620 break;
3622 if (STRINGP (object))
3623 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3624 else
3625 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3626 spec = Fget_char_property (pos, Qdisplay, object);
3627 if (!STRINGP (object))
3628 bufpos = CHARPOS (tpos);
3629 } while (NILP (spec)
3630 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3631 bufpos, frame_window_p)));
3632 if (rv == 2)
3633 *disp_prop = 2;
3635 return CHARPOS (tpos);
3638 /* Return the character position of the end of the display string that
3639 started at CHARPOS. If there's no display string at CHARPOS,
3640 return -1. A display string is either an overlay with `display'
3641 property whose value is a string or a `display' text property whose
3642 value is a string. */
3643 ptrdiff_t
3644 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3646 /* OBJECT = nil means current buffer. */
3647 Lisp_Object object =
3648 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3649 Lisp_Object pos = make_number (charpos);
3650 ptrdiff_t eob =
3651 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3653 if (charpos >= eob || (string->s && !STRINGP (object)))
3654 return eob;
3656 /* It could happen that the display property or overlay was removed
3657 since we found it in compute_display_string_pos above. One way
3658 this can happen is if JIT font-lock was called (through
3659 handle_fontified_prop), and jit-lock-functions remove text
3660 properties or overlays from the portion of buffer that includes
3661 CHARPOS. Muse mode is known to do that, for example. In this
3662 case, we return -1 to the caller, to signal that no display
3663 string is actually present at CHARPOS. See bidi_fetch_char for
3664 how this is handled.
3666 An alternative would be to never look for display properties past
3667 it->stop_charpos. But neither compute_display_string_pos nor
3668 bidi_fetch_char that calls it know or care where the next
3669 stop_charpos is. */
3670 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3671 return -1;
3673 /* Look forward for the first character where the `display' property
3674 changes. */
3675 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3677 return XFASTINT (pos);
3682 /***********************************************************************
3683 Fontification
3684 ***********************************************************************/
3686 /* Handle changes in the `fontified' property of the current buffer by
3687 calling hook functions from Qfontification_functions to fontify
3688 regions of text. */
3690 static enum prop_handled
3691 handle_fontified_prop (struct it *it)
3693 Lisp_Object prop, pos;
3694 enum prop_handled handled = HANDLED_NORMALLY;
3696 if (!NILP (Vmemory_full))
3697 return handled;
3699 /* Get the value of the `fontified' property at IT's current buffer
3700 position. (The `fontified' property doesn't have a special
3701 meaning in strings.) If the value is nil, call functions from
3702 Qfontification_functions. */
3703 if (!STRINGP (it->string)
3704 && it->s == NULL
3705 && !NILP (Vfontification_functions)
3706 && !NILP (Vrun_hooks)
3707 && (pos = make_number (IT_CHARPOS (*it)),
3708 prop = Fget_char_property (pos, Qfontified, Qnil),
3709 /* Ignore the special cased nil value always present at EOB since
3710 no amount of fontifying will be able to change it. */
3711 NILP (prop) && IT_CHARPOS (*it) < Z))
3713 ptrdiff_t count = SPECPDL_INDEX ();
3714 Lisp_Object val;
3715 struct buffer *obuf = current_buffer;
3716 ptrdiff_t begv = BEGV, zv = ZV;
3717 bool old_clip_changed = current_buffer->clip_changed;
3719 val = Vfontification_functions;
3720 specbind (Qfontification_functions, Qnil);
3722 eassert (it->end_charpos == ZV);
3724 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3725 safe_call1 (val, pos);
3726 else
3728 Lisp_Object fns, fn;
3729 struct gcpro gcpro1, gcpro2;
3731 fns = Qnil;
3732 GCPRO2 (val, fns);
3734 for (; CONSP (val); val = XCDR (val))
3736 fn = XCAR (val);
3738 if (EQ (fn, Qt))
3740 /* A value of t indicates this hook has a local
3741 binding; it means to run the global binding too.
3742 In a global value, t should not occur. If it
3743 does, we must ignore it to avoid an endless
3744 loop. */
3745 for (fns = Fdefault_value (Qfontification_functions);
3746 CONSP (fns);
3747 fns = XCDR (fns))
3749 fn = XCAR (fns);
3750 if (!EQ (fn, Qt))
3751 safe_call1 (fn, pos);
3754 else
3755 safe_call1 (fn, pos);
3758 UNGCPRO;
3761 unbind_to (count, Qnil);
3763 /* Fontification functions routinely call `save-restriction'.
3764 Normally, this tags clip_changed, which can confuse redisplay
3765 (see discussion in Bug#6671). Since we don't perform any
3766 special handling of fontification changes in the case where
3767 `save-restriction' isn't called, there's no point doing so in
3768 this case either. So, if the buffer's restrictions are
3769 actually left unchanged, reset clip_changed. */
3770 if (obuf == current_buffer)
3772 if (begv == BEGV && zv == ZV)
3773 current_buffer->clip_changed = old_clip_changed;
3775 /* There isn't much we can reasonably do to protect against
3776 misbehaving fontification, but here's a fig leaf. */
3777 else if (BUFFER_LIVE_P (obuf))
3778 set_buffer_internal_1 (obuf);
3780 /* The fontification code may have added/removed text.
3781 It could do even a lot worse, but let's at least protect against
3782 the most obvious case where only the text past `pos' gets changed',
3783 as is/was done in grep.el where some escapes sequences are turned
3784 into face properties (bug#7876). */
3785 it->end_charpos = ZV;
3787 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3788 something. This avoids an endless loop if they failed to
3789 fontify the text for which reason ever. */
3790 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3791 handled = HANDLED_RECOMPUTE_PROPS;
3794 return handled;
3799 /***********************************************************************
3800 Faces
3801 ***********************************************************************/
3803 /* Set up iterator IT from face properties at its current position.
3804 Called from handle_stop. */
3806 static enum prop_handled
3807 handle_face_prop (struct it *it)
3809 int new_face_id;
3810 ptrdiff_t next_stop;
3812 if (!STRINGP (it->string))
3814 new_face_id
3815 = face_at_buffer_position (it->w,
3816 IT_CHARPOS (*it),
3817 &next_stop,
3818 (IT_CHARPOS (*it)
3819 + TEXT_PROP_DISTANCE_LIMIT),
3820 0, it->base_face_id);
3822 /* Is this a start of a run of characters with box face?
3823 Caveat: this can be called for a freshly initialized
3824 iterator; face_id is -1 in this case. We know that the new
3825 face will not change until limit, i.e. if the new face has a
3826 box, all characters up to limit will have one. But, as
3827 usual, we don't know whether limit is really the end. */
3828 if (new_face_id != it->face_id)
3830 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3831 /* If it->face_id is -1, old_face below will be NULL, see
3832 the definition of FACE_FROM_ID. This will happen if this
3833 is the initial call that gets the face. */
3834 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3836 /* If the value of face_id of the iterator is -1, we have to
3837 look in front of IT's position and see whether there is a
3838 face there that's different from new_face_id. */
3839 if (!old_face && IT_CHARPOS (*it) > BEG)
3841 int prev_face_id = face_before_it_pos (it);
3843 old_face = FACE_FROM_ID (it->f, prev_face_id);
3846 /* If the new face has a box, but the old face does not,
3847 this is the start of a run of characters with box face,
3848 i.e. this character has a shadow on the left side. */
3849 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3850 && (old_face == NULL || !old_face->box));
3851 it->face_box_p = new_face->box != FACE_NO_BOX;
3854 else
3856 int base_face_id;
3857 ptrdiff_t bufpos;
3858 int i;
3859 Lisp_Object from_overlay
3860 = (it->current.overlay_string_index >= 0
3861 ? it->string_overlays[it->current.overlay_string_index
3862 % OVERLAY_STRING_CHUNK_SIZE]
3863 : Qnil);
3865 /* See if we got to this string directly or indirectly from
3866 an overlay property. That includes the before-string or
3867 after-string of an overlay, strings in display properties
3868 provided by an overlay, their text properties, etc.
3870 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3871 if (! NILP (from_overlay))
3872 for (i = it->sp - 1; i >= 0; i--)
3874 if (it->stack[i].current.overlay_string_index >= 0)
3875 from_overlay
3876 = it->string_overlays[it->stack[i].current.overlay_string_index
3877 % OVERLAY_STRING_CHUNK_SIZE];
3878 else if (! NILP (it->stack[i].from_overlay))
3879 from_overlay = it->stack[i].from_overlay;
3881 if (!NILP (from_overlay))
3882 break;
3885 if (! NILP (from_overlay))
3887 bufpos = IT_CHARPOS (*it);
3888 /* For a string from an overlay, the base face depends
3889 only on text properties and ignores overlays. */
3890 base_face_id
3891 = face_for_overlay_string (it->w,
3892 IT_CHARPOS (*it),
3893 &next_stop,
3894 (IT_CHARPOS (*it)
3895 + TEXT_PROP_DISTANCE_LIMIT),
3897 from_overlay);
3899 else
3901 bufpos = 0;
3903 /* For strings from a `display' property, use the face at
3904 IT's current buffer position as the base face to merge
3905 with, so that overlay strings appear in the same face as
3906 surrounding text, unless they specify their own faces.
3907 For strings from wrap-prefix and line-prefix properties,
3908 use the default face, possibly remapped via
3909 Vface_remapping_alist. */
3910 base_face_id = it->string_from_prefix_prop_p
3911 ? (!NILP (Vface_remapping_alist)
3912 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3913 : DEFAULT_FACE_ID)
3914 : underlying_face_id (it);
3917 new_face_id = face_at_string_position (it->w,
3918 it->string,
3919 IT_STRING_CHARPOS (*it),
3920 bufpos,
3921 &next_stop,
3922 base_face_id, 0);
3924 /* Is this a start of a run of characters with box? Caveat:
3925 this can be called for a freshly allocated iterator; face_id
3926 is -1 is this case. We know that the new face will not
3927 change until the next check pos, i.e. if the new face has a
3928 box, all characters up to that position will have a
3929 box. But, as usual, we don't know whether that position
3930 is really the end. */
3931 if (new_face_id != it->face_id)
3933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3936 /* If new face has a box but old face hasn't, this is the
3937 start of a run of characters with box, i.e. it has a
3938 shadow on the left side. */
3939 it->start_of_box_run_p
3940 = new_face->box && (old_face == NULL || !old_face->box);
3941 it->face_box_p = new_face->box != FACE_NO_BOX;
3945 it->face_id = new_face_id;
3946 return HANDLED_NORMALLY;
3950 /* Return the ID of the face ``underlying'' IT's current position,
3951 which is in a string. If the iterator is associated with a
3952 buffer, return the face at IT's current buffer position.
3953 Otherwise, use the iterator's base_face_id. */
3955 static int
3956 underlying_face_id (struct it *it)
3958 int face_id = it->base_face_id, i;
3960 eassert (STRINGP (it->string));
3962 for (i = it->sp - 1; i >= 0; --i)
3963 if (NILP (it->stack[i].string))
3964 face_id = it->stack[i].face_id;
3966 return face_id;
3970 /* Compute the face one character before or after the current position
3971 of IT, in the visual order. BEFORE_P non-zero means get the face
3972 in front (to the left in L2R paragraphs, to the right in R2L
3973 paragraphs) of IT's screen position. Value is the ID of the face. */
3975 static int
3976 face_before_or_after_it_pos (struct it *it, int before_p)
3978 int face_id, limit;
3979 ptrdiff_t next_check_charpos;
3980 struct it it_copy;
3981 void *it_copy_data = NULL;
3983 eassert (it->s == NULL);
3985 if (STRINGP (it->string))
3987 ptrdiff_t bufpos, charpos;
3988 int base_face_id;
3990 /* No face change past the end of the string (for the case
3991 we are padding with spaces). No face change before the
3992 string start. */
3993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3995 return it->face_id;
3997 if (!it->bidi_p)
3999 /* Set charpos to the position before or after IT's current
4000 position, in the logical order, which in the non-bidi
4001 case is the same as the visual order. */
4002 if (before_p)
4003 charpos = IT_STRING_CHARPOS (*it) - 1;
4004 else if (it->what == IT_COMPOSITION)
4005 /* For composition, we must check the character after the
4006 composition. */
4007 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4008 else
4009 charpos = IT_STRING_CHARPOS (*it) + 1;
4011 else
4013 if (before_p)
4015 /* With bidi iteration, the character before the current
4016 in the visual order cannot be found by simple
4017 iteration, because "reverse" reordering is not
4018 supported. Instead, we need to use the move_it_*
4019 family of functions. */
4020 /* Ignore face changes before the first visible
4021 character on this display line. */
4022 if (it->current_x <= it->first_visible_x)
4023 return it->face_id;
4024 SAVE_IT (it_copy, *it, it_copy_data);
4025 /* Implementation note: Since move_it_in_display_line
4026 works in the iterator geometry, and thinks the first
4027 character is always the leftmost, even in R2L lines,
4028 we don't need to distinguish between the R2L and L2R
4029 cases here. */
4030 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4031 it_copy.current_x - 1, MOVE_TO_X);
4032 charpos = IT_STRING_CHARPOS (it_copy);
4033 RESTORE_IT (it, it, it_copy_data);
4035 else
4037 /* Set charpos to the string position of the character
4038 that comes after IT's current position in the visual
4039 order. */
4040 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4042 it_copy = *it;
4043 while (n--)
4044 bidi_move_to_visually_next (&it_copy.bidi_it);
4046 charpos = it_copy.bidi_it.charpos;
4049 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4051 if (it->current.overlay_string_index >= 0)
4052 bufpos = IT_CHARPOS (*it);
4053 else
4054 bufpos = 0;
4056 base_face_id = underlying_face_id (it);
4058 /* Get the face for ASCII, or unibyte. */
4059 face_id = face_at_string_position (it->w,
4060 it->string,
4061 charpos,
4062 bufpos,
4063 &next_check_charpos,
4064 base_face_id, 0);
4066 /* Correct the face for charsets different from ASCII. Do it
4067 for the multibyte case only. The face returned above is
4068 suitable for unibyte text if IT->string is unibyte. */
4069 if (STRING_MULTIBYTE (it->string))
4071 struct text_pos pos1 = string_pos (charpos, it->string);
4072 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4073 int c, len;
4074 struct face *face = FACE_FROM_ID (it->f, face_id);
4076 c = string_char_and_length (p, &len);
4077 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4080 else
4082 struct text_pos pos;
4084 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4085 || (IT_CHARPOS (*it) <= BEGV && before_p))
4086 return it->face_id;
4088 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4089 pos = it->current.pos;
4091 if (!it->bidi_p)
4093 if (before_p)
4094 DEC_TEXT_POS (pos, it->multibyte_p);
4095 else
4097 if (it->what == IT_COMPOSITION)
4099 /* For composition, we must check the position after
4100 the composition. */
4101 pos.charpos += it->cmp_it.nchars;
4102 pos.bytepos += it->len;
4104 else
4105 INC_TEXT_POS (pos, it->multibyte_p);
4108 else
4110 if (before_p)
4112 /* With bidi iteration, the character before the current
4113 in the visual order cannot be found by simple
4114 iteration, because "reverse" reordering is not
4115 supported. Instead, we need to use the move_it_*
4116 family of functions. */
4117 /* Ignore face changes before the first visible
4118 character on this display line. */
4119 if (it->current_x <= it->first_visible_x)
4120 return it->face_id;
4121 SAVE_IT (it_copy, *it, it_copy_data);
4122 /* Implementation note: Since move_it_in_display_line
4123 works in the iterator geometry, and thinks the first
4124 character is always the leftmost, even in R2L lines,
4125 we don't need to distinguish between the R2L and L2R
4126 cases here. */
4127 move_it_in_display_line (&it_copy, ZV,
4128 it_copy.current_x - 1, MOVE_TO_X);
4129 pos = it_copy.current.pos;
4130 RESTORE_IT (it, it, it_copy_data);
4132 else
4134 /* Set charpos to the buffer position of the character
4135 that comes after IT's current position in the visual
4136 order. */
4137 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4139 it_copy = *it;
4140 while (n--)
4141 bidi_move_to_visually_next (&it_copy.bidi_it);
4143 SET_TEXT_POS (pos,
4144 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4147 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4149 /* Determine face for CHARSET_ASCII, or unibyte. */
4150 face_id = face_at_buffer_position (it->w,
4151 CHARPOS (pos),
4152 &next_check_charpos,
4153 limit, 0, -1);
4155 /* Correct the face for charsets different from ASCII. Do it
4156 for the multibyte case only. The face returned above is
4157 suitable for unibyte text if current_buffer is unibyte. */
4158 if (it->multibyte_p)
4160 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4161 struct face *face = FACE_FROM_ID (it->f, face_id);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4166 return face_id;
4171 /***********************************************************************
4172 Invisible text
4173 ***********************************************************************/
4175 /* Set up iterator IT from invisible properties at its current
4176 position. Called from handle_stop. */
4178 static enum prop_handled
4179 handle_invisible_prop (struct it *it)
4181 enum prop_handled handled = HANDLED_NORMALLY;
4182 int invis_p;
4183 Lisp_Object prop;
4185 if (STRINGP (it->string))
4187 Lisp_Object end_charpos, limit, charpos;
4189 /* Get the value of the invisible text property at the
4190 current position. Value will be nil if there is no such
4191 property. */
4192 charpos = make_number (IT_STRING_CHARPOS (*it));
4193 prop = Fget_text_property (charpos, Qinvisible, it->string);
4194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4196 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4198 /* Record whether we have to display an ellipsis for the
4199 invisible text. */
4200 int display_ellipsis_p = (invis_p == 2);
4201 ptrdiff_t len, endpos;
4203 handled = HANDLED_RECOMPUTE_PROPS;
4205 /* Get the position at which the next visible text can be
4206 found in IT->string, if any. */
4207 endpos = len = SCHARS (it->string);
4208 XSETINT (limit, len);
4211 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4212 it->string, limit);
4213 if (INTEGERP (end_charpos))
4215 endpos = XFASTINT (end_charpos);
4216 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p == 2)
4219 display_ellipsis_p = true;
4222 while (invis_p && endpos < len);
4224 if (display_ellipsis_p)
4225 it->ellipsis_p = true;
4227 if (endpos < len)
4229 /* Text at END_CHARPOS is visible. Move IT there. */
4230 struct text_pos old;
4231 ptrdiff_t oldpos;
4233 old = it->current.string_pos;
4234 oldpos = CHARPOS (old);
4235 if (it->bidi_p)
4237 if (it->bidi_it.first_elt
4238 && it->bidi_it.charpos < SCHARS (it->string))
4239 bidi_paragraph_init (it->paragraph_embedding,
4240 &it->bidi_it, 1);
4241 /* Bidi-iterate out of the invisible text. */
4244 bidi_move_to_visually_next (&it->bidi_it);
4246 while (oldpos <= it->bidi_it.charpos
4247 && it->bidi_it.charpos < endpos);
4249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4251 if (IT_CHARPOS (*it) >= endpos)
4252 it->prev_stop = endpos;
4254 else
4256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4257 compute_string_pos (&it->current.string_pos, old, it->string);
4260 else
4262 /* The rest of the string is invisible. If this is an
4263 overlay string, proceed with the next overlay string
4264 or whatever comes and return a character from there. */
4265 if (it->current.overlay_string_index >= 0
4266 && !display_ellipsis_p)
4268 next_overlay_string (it);
4269 /* Don't check for overlay strings when we just
4270 finished processing them. */
4271 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4273 else
4275 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4276 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4281 else
4283 ptrdiff_t newpos, next_stop, start_charpos, tem;
4284 Lisp_Object pos, overlay;
4286 /* First of all, is there invisible text at this position? */
4287 tem = start_charpos = IT_CHARPOS (*it);
4288 pos = make_number (tem);
4289 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4290 &overlay);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4293 /* If we are on invisible text, skip over it. */
4294 if (invis_p && start_charpos < it->end_charpos)
4296 /* Record whether we have to display an ellipsis for the
4297 invisible text. */
4298 int display_ellipsis_p = invis_p == 2;
4300 handled = HANDLED_RECOMPUTE_PROPS;
4302 /* Loop skipping over invisible text. The loop is left at
4303 ZV or with IT on the first char being visible again. */
4306 /* Try to skip some invisible text. Return value is the
4307 position reached which can be equal to where we start
4308 if there is nothing invisible there. This skips both
4309 over invisible text properties and overlays with
4310 invisible property. */
4311 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4313 /* If we skipped nothing at all we weren't at invisible
4314 text in the first place. If everything to the end of
4315 the buffer was skipped, end the loop. */
4316 if (newpos == tem || newpos >= ZV)
4317 invis_p = 0;
4318 else
4320 /* We skipped some characters but not necessarily
4321 all there are. Check if we ended up on visible
4322 text. Fget_char_property returns the property of
4323 the char before the given position, i.e. if we
4324 get invis_p = 0, this means that the char at
4325 newpos is visible. */
4326 pos = make_number (newpos);
4327 prop = Fget_char_property (pos, Qinvisible, it->window);
4328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4331 /* If we ended up on invisible text, proceed to
4332 skip starting with next_stop. */
4333 if (invis_p)
4334 tem = next_stop;
4336 /* If there are adjacent invisible texts, don't lose the
4337 second one's ellipsis. */
4338 if (invis_p == 2)
4339 display_ellipsis_p = true;
4341 while (invis_p);
4343 /* The position newpos is now either ZV or on visible text. */
4344 if (it->bidi_p)
4346 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4347 int on_newline
4348 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4349 int after_newline
4350 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4352 /* If the invisible text ends on a newline or on a
4353 character after a newline, we can avoid the costly,
4354 character by character, bidi iteration to NEWPOS, and
4355 instead simply reseat the iterator there. That's
4356 because all bidi reordering information is tossed at
4357 the newline. This is a big win for modes that hide
4358 complete lines, like Outline, Org, etc. */
4359 if (on_newline || after_newline)
4361 struct text_pos tpos;
4362 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4364 SET_TEXT_POS (tpos, newpos, bpos);
4365 reseat_1 (it, tpos, 0);
4366 /* If we reseat on a newline/ZV, we need to prep the
4367 bidi iterator for advancing to the next character
4368 after the newline/EOB, keeping the current paragraph
4369 direction (so that PRODUCE_GLYPHS does TRT wrt
4370 prepending/appending glyphs to a glyph row). */
4371 if (on_newline)
4373 it->bidi_it.first_elt = 0;
4374 it->bidi_it.paragraph_dir = pdir;
4375 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4376 it->bidi_it.nchars = 1;
4377 it->bidi_it.ch_len = 1;
4380 else /* Must use the slow method. */
4382 /* With bidi iteration, the region of invisible text
4383 could start and/or end in the middle of a
4384 non-base embedding level. Therefore, we need to
4385 skip invisible text using the bidi iterator,
4386 starting at IT's current position, until we find
4387 ourselves outside of the invisible text.
4388 Skipping invisible text _after_ bidi iteration
4389 avoids affecting the visual order of the
4390 displayed text when invisible properties are
4391 added or removed. */
4392 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4394 /* If we were `reseat'ed to a new paragraph,
4395 determine the paragraph base direction. We
4396 need to do it now because
4397 next_element_from_buffer may not have a
4398 chance to do it, if we are going to skip any
4399 text at the beginning, which resets the
4400 FIRST_ELT flag. */
4401 bidi_paragraph_init (it->paragraph_embedding,
4402 &it->bidi_it, 1);
4406 bidi_move_to_visually_next (&it->bidi_it);
4408 while (it->stop_charpos <= it->bidi_it.charpos
4409 && it->bidi_it.charpos < newpos);
4410 IT_CHARPOS (*it) = it->bidi_it.charpos;
4411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4412 /* If we overstepped NEWPOS, record its position in
4413 the iterator, so that we skip invisible text if
4414 later the bidi iteration lands us in the
4415 invisible region again. */
4416 if (IT_CHARPOS (*it) >= newpos)
4417 it->prev_stop = newpos;
4420 else
4422 IT_CHARPOS (*it) = newpos;
4423 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4426 /* If there are before-strings at the start of invisible
4427 text, and the text is invisible because of a text
4428 property, arrange to show before-strings because 20.x did
4429 it that way. (If the text is invisible because of an
4430 overlay property instead of a text property, this is
4431 already handled in the overlay code.) */
4432 if (NILP (overlay)
4433 && get_overlay_strings (it, it->stop_charpos))
4435 handled = HANDLED_RECOMPUTE_PROPS;
4436 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4438 else if (display_ellipsis_p)
4440 /* Make sure that the glyphs of the ellipsis will get
4441 correct `charpos' values. If we would not update
4442 it->position here, the glyphs would belong to the
4443 last visible character _before_ the invisible
4444 text, which confuses `set_cursor_from_row'.
4446 We use the last invisible position instead of the
4447 first because this way the cursor is always drawn on
4448 the first "." of the ellipsis, whenever PT is inside
4449 the invisible text. Otherwise the cursor would be
4450 placed _after_ the ellipsis when the point is after the
4451 first invisible character. */
4452 if (!STRINGP (it->object))
4454 it->position.charpos = newpos - 1;
4455 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4457 it->ellipsis_p = true;
4458 /* Let the ellipsis display before
4459 considering any properties of the following char.
4460 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4461 handled = HANDLED_RETURN;
4466 return handled;
4470 /* Make iterator IT return `...' next.
4471 Replaces LEN characters from buffer. */
4473 static void
4474 setup_for_ellipsis (struct it *it, int len)
4476 /* Use the display table definition for `...'. Invalid glyphs
4477 will be handled by the method returning elements from dpvec. */
4478 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4481 it->dpvec = v->contents;
4482 it->dpend = v->contents + v->header.size;
4484 else
4486 /* Default `...'. */
4487 it->dpvec = default_invis_vector;
4488 it->dpend = default_invis_vector + 3;
4491 it->dpvec_char_len = len;
4492 it->current.dpvec_index = 0;
4493 it->dpvec_face_id = -1;
4495 /* Remember the current face id in case glyphs specify faces.
4496 IT's face is restored in set_iterator_to_next.
4497 saved_face_id was set to preceding char's face in handle_stop. */
4498 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4499 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4501 it->method = GET_FROM_DISPLAY_VECTOR;
4502 it->ellipsis_p = true;
4507 /***********************************************************************
4508 'display' property
4509 ***********************************************************************/
4511 /* Set up iterator IT from `display' property at its current position.
4512 Called from handle_stop.
4513 We return HANDLED_RETURN if some part of the display property
4514 overrides the display of the buffer text itself.
4515 Otherwise we return HANDLED_NORMALLY. */
4517 static enum prop_handled
4518 handle_display_prop (struct it *it)
4520 Lisp_Object propval, object, overlay;
4521 struct text_pos *position;
4522 ptrdiff_t bufpos;
4523 /* Nonzero if some property replaces the display of the text itself. */
4524 int display_replaced_p = 0;
4526 if (STRINGP (it->string))
4528 object = it->string;
4529 position = &it->current.string_pos;
4530 bufpos = CHARPOS (it->current.pos);
4532 else
4534 XSETWINDOW (object, it->w);
4535 position = &it->current.pos;
4536 bufpos = CHARPOS (*position);
4539 /* Reset those iterator values set from display property values. */
4540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4541 it->space_width = Qnil;
4542 it->font_height = Qnil;
4543 it->voffset = 0;
4545 /* We don't support recursive `display' properties, i.e. string
4546 values that have a string `display' property, that have a string
4547 `display' property etc. */
4548 if (!it->string_from_display_prop_p)
4549 it->area = TEXT_AREA;
4551 propval = get_char_property_and_overlay (make_number (position->charpos),
4552 Qdisplay, object, &overlay);
4553 if (NILP (propval))
4554 return HANDLED_NORMALLY;
4555 /* Now OVERLAY is the overlay that gave us this property, or nil
4556 if it was a text property. */
4558 if (!STRINGP (it->string))
4559 object = it->w->contents;
4561 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4562 position, bufpos,
4563 FRAME_WINDOW_P (it->f));
4565 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4568 /* Subroutine of handle_display_prop. Returns non-zero if the display
4569 specification in SPEC is a replacing specification, i.e. it would
4570 replace the text covered by `display' property with something else,
4571 such as an image or a display string. If SPEC includes any kind or
4572 `(space ...) specification, the value is 2; this is used by
4573 compute_display_string_pos, which see.
4575 See handle_single_display_spec for documentation of arguments.
4576 frame_window_p is non-zero if the window being redisplayed is on a
4577 GUI frame; this argument is used only if IT is NULL, see below.
4579 IT can be NULL, if this is called by the bidi reordering code
4580 through compute_display_string_pos, which see. In that case, this
4581 function only examines SPEC, but does not otherwise "handle" it, in
4582 the sense that it doesn't set up members of IT from the display
4583 spec. */
4584 static int
4585 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4586 Lisp_Object overlay, struct text_pos *position,
4587 ptrdiff_t bufpos, int frame_window_p)
4589 int replacing_p = 0;
4590 int rv;
4592 if (CONSP (spec)
4593 /* Simple specifications. */
4594 && !EQ (XCAR (spec), Qimage)
4595 && !EQ (XCAR (spec), Qspace)
4596 && !EQ (XCAR (spec), Qwhen)
4597 && !EQ (XCAR (spec), Qslice)
4598 && !EQ (XCAR (spec), Qspace_width)
4599 && !EQ (XCAR (spec), Qheight)
4600 && !EQ (XCAR (spec), Qraise)
4601 /* Marginal area specifications. */
4602 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4603 && !EQ (XCAR (spec), Qleft_fringe)
4604 && !EQ (XCAR (spec), Qright_fringe)
4605 && !NILP (XCAR (spec)))
4607 for (; CONSP (spec); spec = XCDR (spec))
4609 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4610 overlay, position, bufpos,
4611 replacing_p, frame_window_p)))
4613 replacing_p = rv;
4614 /* If some text in a string is replaced, `position' no
4615 longer points to the position of `object'. */
4616 if (!it || STRINGP (object))
4617 break;
4621 else if (VECTORP (spec))
4623 ptrdiff_t i;
4624 for (i = 0; i < ASIZE (spec); ++i)
4625 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4636 else
4638 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4639 position, bufpos, 0,
4640 frame_window_p)))
4641 replacing_p = rv;
4644 return replacing_p;
4647 /* Value is the position of the end of the `display' property starting
4648 at START_POS in OBJECT. */
4650 static struct text_pos
4651 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4653 Lisp_Object end;
4654 struct text_pos end_pos;
4656 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4657 Qdisplay, object, Qnil);
4658 CHARPOS (end_pos) = XFASTINT (end);
4659 if (STRINGP (object))
4660 compute_string_pos (&end_pos, start_pos, it->string);
4661 else
4662 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4664 return end_pos;
4668 /* Set up IT from a single `display' property specification SPEC. OBJECT
4669 is the object in which the `display' property was found. *POSITION
4670 is the position in OBJECT at which the `display' property was found.
4671 BUFPOS is the buffer position of OBJECT (different from POSITION if
4672 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4673 previously saw a display specification which already replaced text
4674 display with something else, for example an image; we ignore such
4675 properties after the first one has been processed.
4677 OVERLAY is the overlay this `display' property came from,
4678 or nil if it was a text property.
4680 If SPEC is a `space' or `image' specification, and in some other
4681 cases too, set *POSITION to the position where the `display'
4682 property ends.
4684 If IT is NULL, only examine the property specification in SPEC, but
4685 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4686 is intended to be displayed in a window on a GUI frame.
4688 Value is non-zero if something was found which replaces the display
4689 of buffer or string text. */
4691 static int
4692 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, int display_replaced_p,
4695 int frame_window_p)
4697 Lisp_Object form;
4698 Lisp_Object location, value;
4699 struct text_pos start_pos = *position;
4700 int valid_p;
4702 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4703 If the result is non-nil, use VALUE instead of SPEC. */
4704 form = Qt;
4705 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4707 spec = XCDR (spec);
4708 if (!CONSP (spec))
4709 return 0;
4710 form = XCAR (spec);
4711 spec = XCDR (spec);
4714 if (!NILP (form) && !EQ (form, Qt))
4716 ptrdiff_t count = SPECPDL_INDEX ();
4717 struct gcpro gcpro1;
4719 /* Bind `object' to the object having the `display' property, a
4720 buffer or string. Bind `position' to the position in the
4721 object where the property was found, and `buffer-position'
4722 to the current position in the buffer. */
4724 if (NILP (object))
4725 XSETBUFFER (object, current_buffer);
4726 specbind (Qobject, object);
4727 specbind (Qposition, make_number (CHARPOS (*position)));
4728 specbind (Qbuffer_position, make_number (bufpos));
4729 GCPRO1 (form);
4730 form = safe_eval (form);
4731 UNGCPRO;
4732 unbind_to (count, Qnil);
4735 if (NILP (form))
4736 return 0;
4738 /* Handle `(height HEIGHT)' specifications. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qheight)
4741 && CONSP (XCDR (spec)))
4743 if (it)
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4748 it->font_height = XCAR (XCDR (spec));
4749 if (!NILP (it->font_height))
4751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4752 int new_height = -1;
4754 if (CONSP (it->font_height)
4755 && (EQ (XCAR (it->font_height), Qplus)
4756 || EQ (XCAR (it->font_height), Qminus))
4757 && CONSP (XCDR (it->font_height))
4758 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4760 /* `(+ N)' or `(- N)' where N is an integer. */
4761 int steps = XINT (XCAR (XCDR (it->font_height)));
4762 if (EQ (XCAR (it->font_height), Qplus))
4763 steps = - steps;
4764 it->face_id = smaller_face (it->f, it->face_id, steps);
4766 else if (FUNCTIONP (it->font_height))
4768 /* Call function with current height as argument.
4769 Value is the new height. */
4770 Lisp_Object height;
4771 height = safe_call1 (it->font_height,
4772 face->lface[LFACE_HEIGHT_INDEX]);
4773 if (NUMBERP (height))
4774 new_height = XFLOATINT (height);
4776 else if (NUMBERP (it->font_height))
4778 /* Value is a multiple of the canonical char height. */
4779 struct face *f;
4781 f = FACE_FROM_ID (it->f,
4782 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4783 new_height = (XFLOATINT (it->font_height)
4784 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4786 else
4788 /* Evaluate IT->font_height with `height' bound to the
4789 current specified height to get the new height. */
4790 ptrdiff_t count = SPECPDL_INDEX ();
4792 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4793 value = safe_eval (it->font_height);
4794 unbind_to (count, Qnil);
4796 if (NUMBERP (value))
4797 new_height = XFLOATINT (value);
4800 if (new_height > 0)
4801 it->face_id = face_with_height (it->f, it->face_id, new_height);
4805 return 0;
4808 /* Handle `(space-width WIDTH)'. */
4809 if (CONSP (spec)
4810 && EQ (XCAR (spec), Qspace_width)
4811 && CONSP (XCDR (spec)))
4813 if (it)
4815 if (!FRAME_WINDOW_P (it->f))
4816 return 0;
4818 value = XCAR (XCDR (spec));
4819 if (NUMBERP (value) && XFLOATINT (value) > 0)
4820 it->space_width = value;
4823 return 0;
4826 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4827 if (CONSP (spec)
4828 && EQ (XCAR (spec), Qslice))
4830 Lisp_Object tem;
4832 if (it)
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4837 if (tem = XCDR (spec), CONSP (tem))
4839 it->slice.x = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4842 it->slice.y = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4845 it->slice.width = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 it->slice.height = XCAR (tem);
4853 return 0;
4856 /* Handle `(raise FACTOR)'. */
4857 if (CONSP (spec)
4858 && EQ (XCAR (spec), Qraise)
4859 && CONSP (XCDR (spec)))
4861 if (it)
4863 if (!FRAME_WINDOW_P (it->f))
4864 return 0;
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (NUMBERP (value))
4870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4871 it->voffset = - (XFLOATINT (value)
4872 * (FONT_HEIGHT (face->font)));
4874 #endif /* HAVE_WINDOW_SYSTEM */
4877 return 0;
4880 /* Don't handle the other kinds of display specifications
4881 inside a string that we got from a `display' property. */
4882 if (it && it->string_from_display_prop_p)
4883 return 0;
4885 /* Characters having this form of property are not displayed, so
4886 we have to find the end of the property. */
4887 if (it)
4889 start_pos = *position;
4890 *position = display_prop_end (it, object, start_pos);
4892 value = Qnil;
4894 /* Stop the scan at that end position--we assume that all
4895 text properties change there. */
4896 if (it)
4897 it->stop_charpos = position->charpos;
4899 /* Handle `(left-fringe BITMAP [FACE])'
4900 and `(right-fringe BITMAP [FACE])'. */
4901 if (CONSP (spec)
4902 && (EQ (XCAR (spec), Qleft_fringe)
4903 || EQ (XCAR (spec), Qright_fringe))
4904 && CONSP (XCDR (spec)))
4906 int fringe_bitmap;
4908 if (it)
4910 if (!FRAME_WINDOW_P (it->f))
4911 /* If we return here, POSITION has been advanced
4912 across the text with this property. */
4914 /* Synchronize the bidi iterator with POSITION. This is
4915 needed because we are not going to push the iterator
4916 on behalf of this display property, so there will be
4917 no pop_it call to do this synchronization for us. */
4918 if (it->bidi_p)
4920 it->position = *position;
4921 iterate_out_of_display_property (it);
4922 *position = it->position;
4924 return 1;
4927 else if (!frame_window_p)
4928 return 1;
4930 #ifdef HAVE_WINDOW_SYSTEM
4931 value = XCAR (XCDR (spec));
4932 if (!SYMBOLP (value)
4933 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4934 /* If we return here, POSITION has been advanced
4935 across the text with this property. */
4937 if (it && it->bidi_p)
4939 it->position = *position;
4940 iterate_out_of_display_property (it);
4941 *position = it->position;
4943 return 1;
4946 if (it)
4948 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4950 if (CONSP (XCDR (XCDR (spec))))
4952 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4953 int face_id2 = lookup_derived_face (it->f, face_name,
4954 FRINGE_FACE_ID, 0);
4955 if (face_id2 >= 0)
4956 face_id = face_id2;
4959 /* Save current settings of IT so that we can restore them
4960 when we are finished with the glyph property value. */
4961 push_it (it, position);
4963 it->area = TEXT_AREA;
4964 it->what = IT_IMAGE;
4965 it->image_id = -1; /* no image */
4966 it->position = start_pos;
4967 it->object = NILP (object) ? it->w->contents : object;
4968 it->method = GET_FROM_IMAGE;
4969 it->from_overlay = Qnil;
4970 it->face_id = face_id;
4971 it->from_disp_prop_p = true;
4973 /* Say that we haven't consumed the characters with
4974 `display' property yet. The call to pop_it in
4975 set_iterator_to_next will clean this up. */
4976 *position = start_pos;
4978 if (EQ (XCAR (spec), Qleft_fringe))
4980 it->left_user_fringe_bitmap = fringe_bitmap;
4981 it->left_user_fringe_face_id = face_id;
4983 else
4985 it->right_user_fringe_bitmap = fringe_bitmap;
4986 it->right_user_fringe_face_id = face_id;
4989 #endif /* HAVE_WINDOW_SYSTEM */
4990 return 1;
4993 /* Prepare to handle `((margin left-margin) ...)',
4994 `((margin right-margin) ...)' and `((margin nil) ...)'
4995 prefixes for display specifications. */
4996 location = Qunbound;
4997 if (CONSP (spec) && CONSP (XCAR (spec)))
4999 Lisp_Object tem;
5001 value = XCDR (spec);
5002 if (CONSP (value))
5003 value = XCAR (value);
5005 tem = XCAR (spec);
5006 if (EQ (XCAR (tem), Qmargin)
5007 && (tem = XCDR (tem),
5008 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5009 (NILP (tem)
5010 || EQ (tem, Qleft_margin)
5011 || EQ (tem, Qright_margin))))
5012 location = tem;
5015 if (EQ (location, Qunbound))
5017 location = Qnil;
5018 value = spec;
5021 /* After this point, VALUE is the property after any
5022 margin prefix has been stripped. It must be a string,
5023 an image specification, or `(space ...)'.
5025 LOCATION specifies where to display: `left-margin',
5026 `right-margin' or nil. */
5028 valid_p = (STRINGP (value)
5029 #ifdef HAVE_WINDOW_SYSTEM
5030 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5031 && valid_image_p (value))
5032 #endif /* not HAVE_WINDOW_SYSTEM */
5033 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5035 if (valid_p && !display_replaced_p)
5037 int retval = 1;
5039 if (!it)
5041 /* Callers need to know whether the display spec is any kind
5042 of `(space ...)' spec that is about to affect text-area
5043 display. */
5044 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5045 retval = 2;
5046 return retval;
5049 /* Save current settings of IT so that we can restore them
5050 when we are finished with the glyph property value. */
5051 push_it (it, position);
5052 it->from_overlay = overlay;
5053 it->from_disp_prop_p = true;
5055 if (NILP (location))
5056 it->area = TEXT_AREA;
5057 else if (EQ (location, Qleft_margin))
5058 it->area = LEFT_MARGIN_AREA;
5059 else
5060 it->area = RIGHT_MARGIN_AREA;
5062 if (STRINGP (value))
5064 it->string = value;
5065 it->multibyte_p = STRING_MULTIBYTE (it->string);
5066 it->current.overlay_string_index = -1;
5067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5068 it->end_charpos = it->string_nchars = SCHARS (it->string);
5069 it->method = GET_FROM_STRING;
5070 it->stop_charpos = 0;
5071 it->prev_stop = 0;
5072 it->base_level_stop = 0;
5073 it->string_from_display_prop_p = true;
5074 /* Say that we haven't consumed the characters with
5075 `display' property yet. The call to pop_it in
5076 set_iterator_to_next will clean this up. */
5077 if (BUFFERP (object))
5078 *position = start_pos;
5080 /* Force paragraph direction to be that of the parent
5081 object. If the parent object's paragraph direction is
5082 not yet determined, default to L2R. */
5083 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5084 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5085 else
5086 it->paragraph_embedding = L2R;
5088 /* Set up the bidi iterator for this display string. */
5089 if (it->bidi_p)
5091 it->bidi_it.string.lstring = it->string;
5092 it->bidi_it.string.s = NULL;
5093 it->bidi_it.string.schars = it->end_charpos;
5094 it->bidi_it.string.bufpos = bufpos;
5095 it->bidi_it.string.from_disp_str = 1;
5096 it->bidi_it.string.unibyte = !it->multibyte_p;
5097 it->bidi_it.w = it->w;
5098 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5101 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5103 it->method = GET_FROM_STRETCH;
5104 it->object = value;
5105 *position = it->position = start_pos;
5106 retval = 1 + (it->area == TEXT_AREA);
5108 #ifdef HAVE_WINDOW_SYSTEM
5109 else
5111 it->what = IT_IMAGE;
5112 it->image_id = lookup_image (it->f, value);
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5122 #endif /* HAVE_WINDOW_SYSTEM */
5124 return retval;
5127 /* Invalid property or property not supported. Restore
5128 POSITION to what it was before. */
5129 *position = start_pos;
5130 return 0;
5133 /* Check if PROP is a display property value whose text should be
5134 treated as intangible. OVERLAY is the overlay from which PROP
5135 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5136 specify the buffer position covered by PROP. */
5139 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5140 ptrdiff_t charpos, ptrdiff_t bytepos)
5142 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5143 struct text_pos position;
5145 SET_TEXT_POS (position, charpos, bytepos);
5146 return handle_display_spec (NULL, prop, Qnil, overlay,
5147 &position, charpos, frame_window_p);
5151 /* Return 1 if PROP is a display sub-property value containing STRING.
5153 Implementation note: this and the following function are really
5154 special cases of handle_display_spec and
5155 handle_single_display_spec, and should ideally use the same code.
5156 Until they do, these two pairs must be consistent and must be
5157 modified in sync. */
5159 static int
5160 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5162 if (EQ (string, prop))
5163 return 1;
5165 /* Skip over `when FORM'. */
5166 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5168 prop = XCDR (prop);
5169 if (!CONSP (prop))
5170 return 0;
5171 /* Actually, the condition following `when' should be eval'ed,
5172 like handle_single_display_spec does, and we should return
5173 zero if it evaluates to nil. However, this function is
5174 called only when the buffer was already displayed and some
5175 glyph in the glyph matrix was found to come from a display
5176 string. Therefore, the condition was already evaluated, and
5177 the result was non-nil, otherwise the display string wouldn't
5178 have been displayed and we would have never been called for
5179 this property. Thus, we can skip the evaluation and assume
5180 its result is non-nil. */
5181 prop = XCDR (prop);
5184 if (CONSP (prop))
5185 /* Skip over `margin LOCATION'. */
5186 if (EQ (XCAR (prop), Qmargin))
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5197 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5201 /* Return 1 if STRING appears in the `display' property PROP. */
5203 static int
5204 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5206 if (CONSP (prop)
5207 && !EQ (XCAR (prop), Qwhen)
5208 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5210 /* A list of sub-properties. */
5211 while (CONSP (prop))
5213 if (single_display_spec_string_p (XCAR (prop), string))
5214 return 1;
5215 prop = XCDR (prop);
5218 else if (VECTORP (prop))
5220 /* A vector of sub-properties. */
5221 ptrdiff_t i;
5222 for (i = 0; i < ASIZE (prop); ++i)
5223 if (single_display_spec_string_p (AREF (prop, i), string))
5224 return 1;
5226 else
5227 return single_display_spec_string_p (prop, string);
5229 return 0;
5232 /* Look for STRING in overlays and text properties in the current
5233 buffer, between character positions FROM and TO (excluding TO).
5234 BACK_P non-zero means look back (in this case, TO is supposed to be
5235 less than FROM).
5236 Value is the first character position where STRING was found, or
5237 zero if it wasn't found before hitting TO.
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5242 static ptrdiff_t
5243 string_buffer_position_lim (Lisp_Object string,
5244 ptrdiff_t from, ptrdiff_t to, int back_p)
5246 Lisp_Object limit, prop, pos;
5247 int found = 0;
5249 pos = make_number (max (from, BEGV));
5251 if (!back_p) /* looking forward */
5253 limit = make_number (min (to, ZV));
5254 while (!found && !EQ (pos, limit))
5256 prop = Fget_char_property (pos, Qdisplay, Qnil);
5257 if (!NILP (prop) && display_prop_string_p (prop, string))
5258 found = 1;
5259 else
5260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5261 limit);
5264 else /* looking back */
5266 limit = make_number (max (to, BEGV));
5267 while (!found && !EQ (pos, limit))
5269 prop = Fget_char_property (pos, Qdisplay, Qnil);
5270 if (!NILP (prop) && display_prop_string_p (prop, string))
5271 found = 1;
5272 else
5273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5274 limit);
5278 return found ? XINT (pos) : 0;
5281 /* Determine which buffer position in current buffer STRING comes from.
5282 AROUND_CHARPOS is an approximate position where it could come from.
5283 Value is the buffer position or 0 if it couldn't be determined.
5285 This function is necessary because we don't record buffer positions
5286 in glyphs generated from strings (to keep struct glyph small).
5287 This function may only use code that doesn't eval because it is
5288 called asynchronously from note_mouse_highlight. */
5290 static ptrdiff_t
5291 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5293 const int MAX_DISTANCE = 1000;
5294 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos + MAX_DISTANCE,
5298 if (!found)
5299 found = string_buffer_position_lim (string, around_charpos,
5300 around_charpos - MAX_DISTANCE, 1);
5301 return found;
5306 /***********************************************************************
5307 `composition' property
5308 ***********************************************************************/
5310 /* Set up iterator IT from `composition' property at its current
5311 position. Called from handle_stop. */
5313 static enum prop_handled
5314 handle_composition_prop (struct it *it)
5316 Lisp_Object prop, string;
5317 ptrdiff_t pos, pos_byte, start, end;
5319 if (STRINGP (it->string))
5321 unsigned char *s;
5323 pos = IT_STRING_CHARPOS (*it);
5324 pos_byte = IT_STRING_BYTEPOS (*it);
5325 string = it->string;
5326 s = SDATA (string) + pos_byte;
5327 it->c = STRING_CHAR (s);
5329 else
5331 pos = IT_CHARPOS (*it);
5332 pos_byte = IT_BYTEPOS (*it);
5333 string = Qnil;
5334 it->c = FETCH_CHAR (pos_byte);
5337 /* If there's a valid composition and point is not inside of the
5338 composition (in the case that the composition is from the current
5339 buffer), draw a glyph composed from the composition components. */
5340 if (find_composition (pos, -1, &start, &end, &prop, string)
5341 && composition_valid_p (start, end, prop)
5342 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5344 if (start < pos)
5345 /* As we can't handle this situation (perhaps font-lock added
5346 a new composition), we just return here hoping that next
5347 redisplay will detect this composition much earlier. */
5348 return HANDLED_NORMALLY;
5349 if (start != pos)
5351 if (STRINGP (it->string))
5352 pos_byte = string_char_to_byte (it->string, start);
5353 else
5354 pos_byte = CHAR_TO_BYTE (start);
5356 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5357 prop, string);
5359 if (it->cmp_it.id >= 0)
5361 it->cmp_it.ch = -1;
5362 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5363 it->cmp_it.nglyphs = -1;
5367 return HANDLED_NORMALLY;
5372 /***********************************************************************
5373 Overlay strings
5374 ***********************************************************************/
5376 /* The following structure is used to record overlay strings for
5377 later sorting in load_overlay_strings. */
5379 struct overlay_entry
5381 Lisp_Object overlay;
5382 Lisp_Object string;
5383 EMACS_INT priority;
5384 int after_string_p;
5388 /* Set up iterator IT from overlay strings at its current position.
5389 Called from handle_stop. */
5391 static enum prop_handled
5392 handle_overlay_change (struct it *it)
5394 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5395 return HANDLED_RECOMPUTE_PROPS;
5396 else
5397 return HANDLED_NORMALLY;
5401 /* Set up the next overlay string for delivery by IT, if there is an
5402 overlay string to deliver. Called by set_iterator_to_next when the
5403 end of the current overlay string is reached. If there are more
5404 overlay strings to display, IT->string and
5405 IT->current.overlay_string_index are set appropriately here.
5406 Otherwise IT->string is set to nil. */
5408 static void
5409 next_overlay_string (struct it *it)
5411 ++it->current.overlay_string_index;
5412 if (it->current.overlay_string_index == it->n_overlay_strings)
5414 /* No more overlay strings. Restore IT's settings to what
5415 they were before overlay strings were processed, and
5416 continue to deliver from current_buffer. */
5418 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5419 pop_it (it);
5420 eassert (it->sp > 0
5421 || (NILP (it->string)
5422 && it->method == GET_FROM_BUFFER
5423 && it->stop_charpos >= BEGV
5424 && it->stop_charpos <= it->end_charpos));
5425 it->current.overlay_string_index = -1;
5426 it->n_overlay_strings = 0;
5427 it->overlay_strings_charpos = -1;
5428 /* If there's an empty display string on the stack, pop the
5429 stack, to resync the bidi iterator with IT's position. Such
5430 empty strings are pushed onto the stack in
5431 get_overlay_strings_1. */
5432 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5433 pop_it (it);
5435 /* If we're at the end of the buffer, record that we have
5436 processed the overlay strings there already, so that
5437 next_element_from_buffer doesn't try it again. */
5438 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5439 it->overlay_strings_at_end_processed_p = true;
5441 else
5443 /* There are more overlay strings to process. If
5444 IT->current.overlay_string_index has advanced to a position
5445 where we must load IT->overlay_strings with more strings, do
5446 it. We must load at the IT->overlay_strings_charpos where
5447 IT->n_overlay_strings was originally computed; when invisible
5448 text is present, this might not be IT_CHARPOS (Bug#7016). */
5449 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5451 if (it->current.overlay_string_index && i == 0)
5452 load_overlay_strings (it, it->overlay_strings_charpos);
5454 /* Initialize IT to deliver display elements from the overlay
5455 string. */
5456 it->string = it->overlay_strings[i];
5457 it->multibyte_p = STRING_MULTIBYTE (it->string);
5458 SET_TEXT_POS (it->current.string_pos, 0, 0);
5459 it->method = GET_FROM_STRING;
5460 it->stop_charpos = 0;
5461 it->end_charpos = SCHARS (it->string);
5462 if (it->cmp_it.stop_pos >= 0)
5463 it->cmp_it.stop_pos = 0;
5464 it->prev_stop = 0;
5465 it->base_level_stop = 0;
5467 /* Set up the bidi iterator for this overlay string. */
5468 if (it->bidi_p)
5470 it->bidi_it.string.lstring = it->string;
5471 it->bidi_it.string.s = NULL;
5472 it->bidi_it.string.schars = SCHARS (it->string);
5473 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5474 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5475 it->bidi_it.string.unibyte = !it->multibyte_p;
5476 it->bidi_it.w = it->w;
5477 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5481 CHECK_IT (it);
5485 /* Compare two overlay_entry structures E1 and E2. Used as a
5486 comparison function for qsort in load_overlay_strings. Overlay
5487 strings for the same position are sorted so that
5489 1. All after-strings come in front of before-strings, except
5490 when they come from the same overlay.
5492 2. Within after-strings, strings are sorted so that overlay strings
5493 from overlays with higher priorities come first.
5495 2. Within before-strings, strings are sorted so that overlay
5496 strings from overlays with higher priorities come last.
5498 Value is analogous to strcmp. */
5501 static int
5502 compare_overlay_entries (const void *e1, const void *e2)
5504 struct overlay_entry const *entry1 = e1;
5505 struct overlay_entry const *entry2 = e2;
5506 int result;
5508 if (entry1->after_string_p != entry2->after_string_p)
5510 /* Let after-strings appear in front of before-strings if
5511 they come from different overlays. */
5512 if (EQ (entry1->overlay, entry2->overlay))
5513 result = entry1->after_string_p ? 1 : -1;
5514 else
5515 result = entry1->after_string_p ? -1 : 1;
5517 else if (entry1->priority != entry2->priority)
5519 if (entry1->after_string_p)
5520 /* After-strings sorted in order of decreasing priority. */
5521 result = entry2->priority < entry1->priority ? -1 : 1;
5522 else
5523 /* Before-strings sorted in order of increasing priority. */
5524 result = entry1->priority < entry2->priority ? -1 : 1;
5526 else
5527 result = 0;
5529 return result;
5533 /* Load the vector IT->overlay_strings with overlay strings from IT's
5534 current buffer position, or from CHARPOS if that is > 0. Set
5535 IT->n_overlays to the total number of overlay strings found.
5537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5538 a time. On entry into load_overlay_strings,
5539 IT->current.overlay_string_index gives the number of overlay
5540 strings that have already been loaded by previous calls to this
5541 function.
5543 IT->add_overlay_start contains an additional overlay start
5544 position to consider for taking overlay strings from, if non-zero.
5545 This position comes into play when the overlay has an `invisible'
5546 property, and both before and after-strings. When we've skipped to
5547 the end of the overlay, because of its `invisible' property, we
5548 nevertheless want its before-string to appear.
5549 IT->add_overlay_start will contain the overlay start position
5550 in this case.
5552 Overlay strings are sorted so that after-string strings come in
5553 front of before-string strings. Within before and after-strings,
5554 strings are sorted by overlay priority. See also function
5555 compare_overlay_entries. */
5557 static void
5558 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5560 Lisp_Object overlay, window, str, invisible;
5561 struct Lisp_Overlay *ov;
5562 ptrdiff_t start, end;
5563 ptrdiff_t size = 20;
5564 ptrdiff_t n = 0, i, j;
5565 int invis_p;
5566 struct overlay_entry *entries = alloca (size * sizeof *entries);
5567 USE_SAFE_ALLOCA;
5569 if (charpos <= 0)
5570 charpos = IT_CHARPOS (*it);
5572 /* Append the overlay string STRING of overlay OVERLAY to vector
5573 `entries' which has size `size' and currently contains `n'
5574 elements. AFTER_P non-zero means STRING is an after-string of
5575 OVERLAY. */
5576 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5577 do \
5579 Lisp_Object priority; \
5581 if (n == size) \
5583 struct overlay_entry *old = entries; \
5584 SAFE_NALLOCA (entries, 2, size); \
5585 memcpy (entries, old, size * sizeof *entries); \
5586 size *= 2; \
5589 entries[n].string = (STRING); \
5590 entries[n].overlay = (OVERLAY); \
5591 priority = Foverlay_get ((OVERLAY), Qpriority); \
5592 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5593 entries[n].after_string_p = (AFTER_P); \
5594 ++n; \
5596 while (0)
5598 /* Process overlay before the overlay center. */
5599 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5601 XSETMISC (overlay, ov);
5602 eassert (OVERLAYP (overlay));
5603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5606 if (end < charpos)
5607 break;
5609 /* Skip this overlay if it doesn't start or end at IT's current
5610 position. */
5611 if (end != charpos && start != charpos)
5612 continue;
5614 /* Skip this overlay if it doesn't apply to IT->w. */
5615 window = Foverlay_get (overlay, Qwindow);
5616 if (WINDOWP (window) && XWINDOW (window) != it->w)
5617 continue;
5619 /* If the text ``under'' the overlay is invisible, both before-
5620 and after-strings from this overlay are visible; start and
5621 end position are indistinguishable. */
5622 invisible = Foverlay_get (overlay, Qinvisible);
5623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5625 /* If overlay has a non-empty before-string, record it. */
5626 if ((start == charpos || (end == charpos && invis_p))
5627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5628 && SCHARS (str))
5629 RECORD_OVERLAY_STRING (overlay, str, 0);
5631 /* If overlay has a non-empty after-string, record it. */
5632 if ((end == charpos || (start == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 1);
5638 /* Process overlays after the overlay center. */
5639 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5641 XSETMISC (overlay, ov);
5642 eassert (OVERLAYP (overlay));
5643 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5644 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5646 if (start > charpos)
5647 break;
5649 /* Skip this overlay if it doesn't start or end at IT's current
5650 position. */
5651 if (end != charpos && start != charpos)
5652 continue;
5654 /* Skip this overlay if it doesn't apply to IT->w. */
5655 window = Foverlay_get (overlay, Qwindow);
5656 if (WINDOWP (window) && XWINDOW (window) != it->w)
5657 continue;
5659 /* If the text ``under'' the overlay is invisible, it has a zero
5660 dimension, and both before- and after-strings apply. */
5661 invisible = Foverlay_get (overlay, Qinvisible);
5662 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5664 /* If overlay has a non-empty before-string, record it. */
5665 if ((start == charpos || (end == charpos && invis_p))
5666 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5667 && SCHARS (str))
5668 RECORD_OVERLAY_STRING (overlay, str, 0);
5670 /* If overlay has a non-empty after-string, record it. */
5671 if ((end == charpos || (start == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 1);
5677 #undef RECORD_OVERLAY_STRING
5679 /* Sort entries. */
5680 if (n > 1)
5681 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5683 /* Record number of overlay strings, and where we computed it. */
5684 it->n_overlay_strings = n;
5685 it->overlay_strings_charpos = charpos;
5687 /* IT->current.overlay_string_index is the number of overlay strings
5688 that have already been consumed by IT. Copy some of the
5689 remaining overlay strings to IT->overlay_strings. */
5690 i = 0;
5691 j = it->current.overlay_string_index;
5692 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5694 it->overlay_strings[i] = entries[j].string;
5695 it->string_overlays[i++] = entries[j++].overlay;
5698 CHECK_IT (it);
5699 SAFE_FREE ();
5703 /* Get the first chunk of overlay strings at IT's current buffer
5704 position, or at CHARPOS if that is > 0. Value is non-zero if at
5705 least one overlay string was found. */
5707 static int
5708 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5710 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5711 process. This fills IT->overlay_strings with strings, and sets
5712 IT->n_overlay_strings to the total number of strings to process.
5713 IT->pos.overlay_string_index has to be set temporarily to zero
5714 because load_overlay_strings needs this; it must be set to -1
5715 when no overlay strings are found because a zero value would
5716 indicate a position in the first overlay string. */
5717 it->current.overlay_string_index = 0;
5718 load_overlay_strings (it, charpos);
5720 /* If we found overlay strings, set up IT to deliver display
5721 elements from the first one. Otherwise set up IT to deliver
5722 from current_buffer. */
5723 if (it->n_overlay_strings)
5725 /* Make sure we know settings in current_buffer, so that we can
5726 restore meaningful values when we're done with the overlay
5727 strings. */
5728 if (compute_stop_p)
5729 compute_stop_pos (it);
5730 eassert (it->face_id >= 0);
5732 /* Save IT's settings. They are restored after all overlay
5733 strings have been processed. */
5734 eassert (!compute_stop_p || it->sp == 0);
5736 /* When called from handle_stop, there might be an empty display
5737 string loaded. In that case, don't bother saving it. But
5738 don't use this optimization with the bidi iterator, since we
5739 need the corresponding pop_it call to resync the bidi
5740 iterator's position with IT's position, after we are done
5741 with the overlay strings. (The corresponding call to pop_it
5742 in case of an empty display string is in
5743 next_overlay_string.) */
5744 if (!(!it->bidi_p
5745 && STRINGP (it->string) && !SCHARS (it->string)))
5746 push_it (it, NULL);
5748 /* Set up IT to deliver display elements from the first overlay
5749 string. */
5750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5751 it->string = it->overlay_strings[0];
5752 it->from_overlay = Qnil;
5753 it->stop_charpos = 0;
5754 eassert (STRINGP (it->string));
5755 it->end_charpos = SCHARS (it->string);
5756 it->prev_stop = 0;
5757 it->base_level_stop = 0;
5758 it->multibyte_p = STRING_MULTIBYTE (it->string);
5759 it->method = GET_FROM_STRING;
5760 it->from_disp_prop_p = 0;
5762 /* Force paragraph direction to be that of the parent
5763 buffer. */
5764 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5765 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5766 else
5767 it->paragraph_embedding = L2R;
5769 /* Set up the bidi iterator for this overlay string. */
5770 if (it->bidi_p)
5772 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5774 it->bidi_it.string.lstring = it->string;
5775 it->bidi_it.string.s = NULL;
5776 it->bidi_it.string.schars = SCHARS (it->string);
5777 it->bidi_it.string.bufpos = pos;
5778 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5779 it->bidi_it.string.unibyte = !it->multibyte_p;
5780 it->bidi_it.w = it->w;
5781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5783 return 1;
5786 it->current.overlay_string_index = -1;
5787 return 0;
5790 static int
5791 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5793 it->string = Qnil;
5794 it->method = GET_FROM_BUFFER;
5796 (void) get_overlay_strings_1 (it, charpos, 1);
5798 CHECK_IT (it);
5800 /* Value is non-zero if we found at least one overlay string. */
5801 return STRINGP (it->string);
5806 /***********************************************************************
5807 Saving and restoring state
5808 ***********************************************************************/
5810 /* Save current settings of IT on IT->stack. Called, for example,
5811 before setting up IT for an overlay string, to be able to restore
5812 IT's settings to what they were after the overlay string has been
5813 processed. If POSITION is non-NULL, it is the position to save on
5814 the stack instead of IT->position. */
5816 static void
5817 push_it (struct it *it, struct text_pos *position)
5819 struct iterator_stack_entry *p;
5821 eassert (it->sp < IT_STACK_SIZE);
5822 p = it->stack + it->sp;
5824 p->stop_charpos = it->stop_charpos;
5825 p->prev_stop = it->prev_stop;
5826 p->base_level_stop = it->base_level_stop;
5827 p->cmp_it = it->cmp_it;
5828 eassert (it->face_id >= 0);
5829 p->face_id = it->face_id;
5830 p->string = it->string;
5831 p->method = it->method;
5832 p->from_overlay = it->from_overlay;
5833 switch (p->method)
5835 case GET_FROM_IMAGE:
5836 p->u.image.object = it->object;
5837 p->u.image.image_id = it->image_id;
5838 p->u.image.slice = it->slice;
5839 break;
5840 case GET_FROM_STRETCH:
5841 p->u.stretch.object = it->object;
5842 break;
5844 p->position = position ? *position : it->position;
5845 p->current = it->current;
5846 p->end_charpos = it->end_charpos;
5847 p->string_nchars = it->string_nchars;
5848 p->area = it->area;
5849 p->multibyte_p = it->multibyte_p;
5850 p->avoid_cursor_p = it->avoid_cursor_p;
5851 p->space_width = it->space_width;
5852 p->font_height = it->font_height;
5853 p->voffset = it->voffset;
5854 p->string_from_display_prop_p = it->string_from_display_prop_p;
5855 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5856 p->display_ellipsis_p = 0;
5857 p->line_wrap = it->line_wrap;
5858 p->bidi_p = it->bidi_p;
5859 p->paragraph_embedding = it->paragraph_embedding;
5860 p->from_disp_prop_p = it->from_disp_prop_p;
5861 ++it->sp;
5863 /* Save the state of the bidi iterator as well. */
5864 if (it->bidi_p)
5865 bidi_push_it (&it->bidi_it);
5868 static void
5869 iterate_out_of_display_property (struct it *it)
5871 int buffer_p = !STRINGP (it->string);
5872 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5873 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5875 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5877 /* Maybe initialize paragraph direction. If we are at the beginning
5878 of a new paragraph, next_element_from_buffer may not have a
5879 chance to do that. */
5880 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5882 /* prev_stop can be zero, so check against BEGV as well. */
5883 while (it->bidi_it.charpos >= bob
5884 && it->prev_stop <= it->bidi_it.charpos
5885 && it->bidi_it.charpos < CHARPOS (it->position)
5886 && it->bidi_it.charpos < eob)
5887 bidi_move_to_visually_next (&it->bidi_it);
5888 /* Record the stop_pos we just crossed, for when we cross it
5889 back, maybe. */
5890 if (it->bidi_it.charpos > CHARPOS (it->position))
5891 it->prev_stop = CHARPOS (it->position);
5892 /* If we ended up not where pop_it put us, resync IT's
5893 positional members with the bidi iterator. */
5894 if (it->bidi_it.charpos != CHARPOS (it->position))
5895 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5896 if (buffer_p)
5897 it->current.pos = it->position;
5898 else
5899 it->current.string_pos = it->position;
5902 /* Restore IT's settings from IT->stack. Called, for example, when no
5903 more overlay strings must be processed, and we return to delivering
5904 display elements from a buffer, or when the end of a string from a
5905 `display' property is reached and we return to delivering display
5906 elements from an overlay string, or from a buffer. */
5908 static void
5909 pop_it (struct it *it)
5911 struct iterator_stack_entry *p;
5912 int from_display_prop = it->from_disp_prop_p;
5914 eassert (it->sp > 0);
5915 --it->sp;
5916 p = it->stack + it->sp;
5917 it->stop_charpos = p->stop_charpos;
5918 it->prev_stop = p->prev_stop;
5919 it->base_level_stop = p->base_level_stop;
5920 it->cmp_it = p->cmp_it;
5921 it->face_id = p->face_id;
5922 it->current = p->current;
5923 it->position = p->position;
5924 it->string = p->string;
5925 it->from_overlay = p->from_overlay;
5926 if (NILP (it->string))
5927 SET_TEXT_POS (it->current.string_pos, -1, -1);
5928 it->method = p->method;
5929 switch (it->method)
5931 case GET_FROM_IMAGE:
5932 it->image_id = p->u.image.image_id;
5933 it->object = p->u.image.object;
5934 it->slice = p->u.image.slice;
5935 break;
5936 case GET_FROM_STRETCH:
5937 it->object = p->u.stretch.object;
5938 break;
5939 case GET_FROM_BUFFER:
5940 it->object = it->w->contents;
5941 break;
5942 case GET_FROM_STRING:
5943 it->object = it->string;
5944 break;
5945 case GET_FROM_DISPLAY_VECTOR:
5946 if (it->s)
5947 it->method = GET_FROM_C_STRING;
5948 else if (STRINGP (it->string))
5949 it->method = GET_FROM_STRING;
5950 else
5952 it->method = GET_FROM_BUFFER;
5953 it->object = it->w->contents;
5956 it->end_charpos = p->end_charpos;
5957 it->string_nchars = p->string_nchars;
5958 it->area = p->area;
5959 it->multibyte_p = p->multibyte_p;
5960 it->avoid_cursor_p = p->avoid_cursor_p;
5961 it->space_width = p->space_width;
5962 it->font_height = p->font_height;
5963 it->voffset = p->voffset;
5964 it->string_from_display_prop_p = p->string_from_display_prop_p;
5965 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5966 it->line_wrap = p->line_wrap;
5967 it->bidi_p = p->bidi_p;
5968 it->paragraph_embedding = p->paragraph_embedding;
5969 it->from_disp_prop_p = p->from_disp_prop_p;
5970 if (it->bidi_p)
5972 bidi_pop_it (&it->bidi_it);
5973 /* Bidi-iterate until we get out of the portion of text, if any,
5974 covered by a `display' text property or by an overlay with
5975 `display' property. (We cannot just jump there, because the
5976 internal coherency of the bidi iterator state can not be
5977 preserved across such jumps.) We also must determine the
5978 paragraph base direction if the overlay we just processed is
5979 at the beginning of a new paragraph. */
5980 if (from_display_prop
5981 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5982 iterate_out_of_display_property (it);
5984 eassert ((BUFFERP (it->object)
5985 && IT_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (STRINGP (it->object)
5988 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5996 /***********************************************************************
5997 Moving over lines
5998 ***********************************************************************/
6000 /* Set IT's current position to the previous line start. */
6002 static void
6003 back_to_previous_line_start (struct it *it)
6005 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6007 DEC_BOTH (cp, bp);
6008 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6012 /* Move IT to the next line start.
6014 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6015 we skipped over part of the text (as opposed to moving the iterator
6016 continuously over the text). Otherwise, don't change the value
6017 of *SKIPPED_P.
6019 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6020 iterator on the newline, if it was found.
6022 Newlines may come from buffer text, overlay strings, or strings
6023 displayed via the `display' property. That's the reason we can't
6024 simply use find_newline_no_quit.
6026 Note that this function may not skip over invisible text that is so
6027 because of text properties and immediately follows a newline. If
6028 it would, function reseat_at_next_visible_line_start, when called
6029 from set_iterator_to_next, would effectively make invisible
6030 characters following a newline part of the wrong glyph row, which
6031 leads to wrong cursor motion. */
6033 static int
6034 forward_to_next_line_start (struct it *it, int *skipped_p,
6035 struct bidi_it *bidi_it_prev)
6037 ptrdiff_t old_selective;
6038 int newline_found_p, n;
6039 const int MAX_NEWLINE_DISTANCE = 500;
6041 /* If already on a newline, just consume it to avoid unintended
6042 skipping over invisible text below. */
6043 if (it->what == IT_CHARACTER
6044 && it->c == '\n'
6045 && CHARPOS (it->position) == IT_CHARPOS (*it))
6047 if (it->bidi_p && bidi_it_prev)
6048 *bidi_it_prev = it->bidi_it;
6049 set_iterator_to_next (it, 0);
6050 it->c = 0;
6051 return 1;
6054 /* Don't handle selective display in the following. It's (a)
6055 unnecessary because it's done by the caller, and (b) leads to an
6056 infinite recursion because next_element_from_ellipsis indirectly
6057 calls this function. */
6058 old_selective = it->selective;
6059 it->selective = 0;
6061 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6062 from buffer text. */
6063 for (n = newline_found_p = 0;
6064 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6065 n += STRINGP (it->string) ? 0 : 1)
6067 if (!get_next_display_element (it))
6068 return 0;
6069 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6070 if (newline_found_p && it->bidi_p && bidi_it_prev)
6071 *bidi_it_prev = it->bidi_it;
6072 set_iterator_to_next (it, 0);
6075 /* If we didn't find a newline near enough, see if we can use a
6076 short-cut. */
6077 if (!newline_found_p)
6079 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6080 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6081 1, &bytepos);
6082 Lisp_Object pos;
6084 eassert (!STRINGP (it->string));
6086 /* If there isn't any `display' property in sight, and no
6087 overlays, we can just use the position of the newline in
6088 buffer text. */
6089 if (it->stop_charpos >= limit
6090 || ((pos = Fnext_single_property_change (make_number (start),
6091 Qdisplay, Qnil,
6092 make_number (limit)),
6093 NILP (pos))
6094 && next_overlay_change (start) == ZV))
6096 if (!it->bidi_p)
6098 IT_CHARPOS (*it) = limit;
6099 IT_BYTEPOS (*it) = bytepos;
6101 else
6103 struct bidi_it bprev;
6105 /* Help bidi.c avoid expensive searches for display
6106 properties and overlays, by telling it that there are
6107 none up to `limit'. */
6108 if (it->bidi_it.disp_pos < limit)
6110 it->bidi_it.disp_pos = limit;
6111 it->bidi_it.disp_prop = 0;
6113 do {
6114 bprev = it->bidi_it;
6115 bidi_move_to_visually_next (&it->bidi_it);
6116 } while (it->bidi_it.charpos != limit);
6117 IT_CHARPOS (*it) = limit;
6118 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6119 if (bidi_it_prev)
6120 *bidi_it_prev = bprev;
6122 *skipped_p = newline_found_p = true;
6124 else
6126 while (get_next_display_element (it)
6127 && !newline_found_p)
6129 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, 0);
6137 it->selective = old_selective;
6138 return newline_found_p;
6142 /* Set IT's current position to the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. Caution: this does not change IT->current_x and
6145 IT->hpos. */
6147 static void
6148 back_to_previous_visible_line_start (struct it *it)
6150 while (IT_CHARPOS (*it) > BEGV)
6152 back_to_previous_line_start (it);
6154 if (IT_CHARPOS (*it) <= BEGV)
6155 break;
6157 /* If selective > 0, then lines indented more than its value are
6158 invisible. */
6159 if (it->selective > 0
6160 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6161 it->selective))
6162 continue;
6164 /* Check the newline before point for invisibility. */
6166 Lisp_Object prop;
6167 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6168 Qinvisible, it->window);
6169 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6170 continue;
6173 if (IT_CHARPOS (*it) <= BEGV)
6174 break;
6177 struct it it2;
6178 void *it2data = NULL;
6179 ptrdiff_t pos;
6180 ptrdiff_t beg, end;
6181 Lisp_Object val, overlay;
6183 SAVE_IT (it2, *it, it2data);
6185 /* If newline is part of a composition, continue from start of composition */
6186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6187 && beg < IT_CHARPOS (*it))
6188 goto replaced;
6190 /* If newline is replaced by a display property, find start of overlay
6191 or interval and continue search from that point. */
6192 pos = --IT_CHARPOS (it2);
6193 --IT_BYTEPOS (it2);
6194 it2.sp = 0;
6195 bidi_unshelve_cache (NULL, 0);
6196 it2.string_from_display_prop_p = 0;
6197 it2.from_disp_prop_p = 0;
6198 if (handle_display_prop (&it2) == HANDLED_RETURN
6199 && !NILP (val = get_char_property_and_overlay
6200 (make_number (pos), Qdisplay, Qnil, &overlay))
6201 && (OVERLAYP (overlay)
6202 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6203 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6205 RESTORE_IT (it, it, it2data);
6206 goto replaced;
6209 /* Newline is not replaced by anything -- so we are done. */
6210 RESTORE_IT (it, it, it2data);
6211 break;
6213 replaced:
6214 if (beg < BEGV)
6215 beg = BEGV;
6216 IT_CHARPOS (*it) = beg;
6217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6221 it->continuation_lines_width = 0;
6223 eassert (IT_CHARPOS (*it) >= BEGV);
6224 eassert (IT_CHARPOS (*it) == BEGV
6225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6226 CHECK_IT (it);
6230 /* Reseat iterator IT at the previous visible line start. Skip
6231 invisible text that is so either due to text properties or due to
6232 selective display. At the end, update IT's overlay information,
6233 face information etc. */
6235 void
6236 reseat_at_previous_visible_line_start (struct it *it)
6238 back_to_previous_visible_line_start (it);
6239 reseat (it, it->current.pos, 1);
6240 CHECK_IT (it);
6244 /* Reseat iterator IT on the next visible line start in the current
6245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6246 preceding the line start. Skip over invisible text that is so
6247 because of selective display. Compute faces, overlays etc at the
6248 new position. Note that this function does not skip over text that
6249 is invisible because of text properties. */
6251 static void
6252 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6254 int newline_found_p, skipped_p = 0;
6255 struct bidi_it bidi_it_prev;
6257 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6259 /* Skip over lines that are invisible because they are indented
6260 more than the value of IT->selective. */
6261 if (it->selective > 0)
6262 while (IT_CHARPOS (*it) < ZV
6263 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6264 it->selective))
6266 eassert (IT_BYTEPOS (*it) == BEGV
6267 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6268 newline_found_p =
6269 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6272 /* Position on the newline if that's what's requested. */
6273 if (on_newline_p && newline_found_p)
6275 if (STRINGP (it->string))
6277 if (IT_STRING_CHARPOS (*it) > 0)
6279 if (!it->bidi_p)
6281 --IT_STRING_CHARPOS (*it);
6282 --IT_STRING_BYTEPOS (*it);
6284 else
6286 /* We need to restore the bidi iterator to the state
6287 it had on the newline, and resync the IT's
6288 position with that. */
6289 it->bidi_it = bidi_it_prev;
6290 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6291 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6295 else if (IT_CHARPOS (*it) > BEGV)
6297 if (!it->bidi_p)
6299 --IT_CHARPOS (*it);
6300 --IT_BYTEPOS (*it);
6302 else
6304 /* We need to restore the bidi iterator to the state it
6305 had on the newline and resync IT with that. */
6306 it->bidi_it = bidi_it_prev;
6307 IT_CHARPOS (*it) = it->bidi_it.charpos;
6308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6310 reseat (it, it->current.pos, 0);
6313 else if (skipped_p)
6314 reseat (it, it->current.pos, 0);
6316 CHECK_IT (it);
6321 /***********************************************************************
6322 Changing an iterator's position
6323 ***********************************************************************/
6325 /* Change IT's current position to POS in current_buffer. If FORCE_P
6326 is non-zero, always check for text properties at the new position.
6327 Otherwise, text properties are only looked up if POS >=
6328 IT->check_charpos of a property. */
6330 static void
6331 reseat (struct it *it, struct text_pos pos, int force_p)
6333 ptrdiff_t original_pos = IT_CHARPOS (*it);
6335 reseat_1 (it, pos, 0);
6337 /* Determine where to check text properties. Avoid doing it
6338 where possible because text property lookup is very expensive. */
6339 if (force_p
6340 || CHARPOS (pos) > it->stop_charpos
6341 || CHARPOS (pos) < original_pos)
6343 if (it->bidi_p)
6345 /* For bidi iteration, we need to prime prev_stop and
6346 base_level_stop with our best estimations. */
6347 /* Implementation note: Of course, POS is not necessarily a
6348 stop position, so assigning prev_pos to it is a lie; we
6349 should have called compute_stop_backwards. However, if
6350 the current buffer does not include any R2L characters,
6351 that call would be a waste of cycles, because the
6352 iterator will never move back, and thus never cross this
6353 "fake" stop position. So we delay that backward search
6354 until the time we really need it, in next_element_from_buffer. */
6355 if (CHARPOS (pos) != it->prev_stop)
6356 it->prev_stop = CHARPOS (pos);
6357 if (CHARPOS (pos) < it->base_level_stop)
6358 it->base_level_stop = 0; /* meaning it's unknown */
6359 handle_stop (it);
6361 else
6363 handle_stop (it);
6364 it->prev_stop = it->base_level_stop = 0;
6369 CHECK_IT (it);
6373 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6374 IT->stop_pos to POS, also. */
6376 static void
6377 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6379 /* Don't call this function when scanning a C string. */
6380 eassert (it->s == NULL);
6382 /* POS must be a reasonable value. */
6383 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6385 it->current.pos = it->position = pos;
6386 it->end_charpos = ZV;
6387 it->dpvec = NULL;
6388 it->current.dpvec_index = -1;
6389 it->current.overlay_string_index = -1;
6390 IT_STRING_CHARPOS (*it) = -1;
6391 IT_STRING_BYTEPOS (*it) = -1;
6392 it->string = Qnil;
6393 it->method = GET_FROM_BUFFER;
6394 it->object = it->w->contents;
6395 it->area = TEXT_AREA;
6396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6397 it->sp = 0;
6398 it->string_from_display_prop_p = 0;
6399 it->string_from_prefix_prop_p = 0;
6401 it->from_disp_prop_p = 0;
6402 it->face_before_selective_p = 0;
6403 if (it->bidi_p)
6405 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6406 &it->bidi_it);
6407 bidi_unshelve_cache (NULL, 0);
6408 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.lstring = Qnil;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.unibyte = 0;
6413 it->bidi_it.w = it->w;
6416 if (set_stop_p)
6418 it->stop_charpos = CHARPOS (pos);
6419 it->base_level_stop = CHARPOS (pos);
6421 /* This make the information stored in it->cmp_it invalidate. */
6422 it->cmp_it.id = -1;
6426 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6427 If S is non-null, it is a C string to iterate over. Otherwise,
6428 STRING gives a Lisp string to iterate over.
6430 If PRECISION > 0, don't return more then PRECISION number of
6431 characters from the string.
6433 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6434 characters have been returned. FIELD_WIDTH < 0 means an infinite
6435 field width.
6437 MULTIBYTE = 0 means disable processing of multibyte characters,
6438 MULTIBYTE > 0 means enable it,
6439 MULTIBYTE < 0 means use IT->multibyte_p.
6441 IT must be initialized via a prior call to init_iterator before
6442 calling this function. */
6444 static void
6445 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6446 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6447 int multibyte)
6449 /* No text property checks performed by default, but see below. */
6450 it->stop_charpos = -1;
6452 /* Set iterator position and end position. */
6453 memset (&it->current, 0, sizeof it->current);
6454 it->current.overlay_string_index = -1;
6455 it->current.dpvec_index = -1;
6456 eassert (charpos >= 0);
6458 /* If STRING is specified, use its multibyteness, otherwise use the
6459 setting of MULTIBYTE, if specified. */
6460 if (multibyte >= 0)
6461 it->multibyte_p = multibyte > 0;
6463 /* Bidirectional reordering of strings is controlled by the default
6464 value of bidi-display-reordering. Don't try to reorder while
6465 loading loadup.el, as the necessary character property tables are
6466 not yet available. */
6467 it->bidi_p =
6468 NILP (Vpurify_flag)
6469 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6471 if (s == NULL)
6473 eassert (STRINGP (string));
6474 it->string = string;
6475 it->s = NULL;
6476 it->end_charpos = it->string_nchars = SCHARS (string);
6477 it->method = GET_FROM_STRING;
6478 it->current.string_pos = string_pos (charpos, string);
6480 if (it->bidi_p)
6482 it->bidi_it.string.lstring = string;
6483 it->bidi_it.string.s = NULL;
6484 it->bidi_it.string.schars = it->end_charpos;
6485 it->bidi_it.string.bufpos = 0;
6486 it->bidi_it.string.from_disp_str = 0;
6487 it->bidi_it.string.unibyte = !it->multibyte_p;
6488 it->bidi_it.w = it->w;
6489 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6490 FRAME_WINDOW_P (it->f), &it->bidi_it);
6493 else
6495 it->s = (const unsigned char *) s;
6496 it->string = Qnil;
6498 /* Note that we use IT->current.pos, not it->current.string_pos,
6499 for displaying C strings. */
6500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6501 if (it->multibyte_p)
6503 it->current.pos = c_string_pos (charpos, s, 1);
6504 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6506 else
6508 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6509 it->end_charpos = it->string_nchars = strlen (s);
6512 if (it->bidi_p)
6514 it->bidi_it.string.lstring = Qnil;
6515 it->bidi_it.string.s = (const unsigned char *) s;
6516 it->bidi_it.string.schars = it->end_charpos;
6517 it->bidi_it.string.bufpos = 0;
6518 it->bidi_it.string.from_disp_str = 0;
6519 it->bidi_it.string.unibyte = !it->multibyte_p;
6520 it->bidi_it.w = it->w;
6521 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6522 &it->bidi_it);
6524 it->method = GET_FROM_C_STRING;
6527 /* PRECISION > 0 means don't return more than PRECISION characters
6528 from the string. */
6529 if (precision > 0 && it->end_charpos - charpos > precision)
6531 it->end_charpos = it->string_nchars = charpos + precision;
6532 if (it->bidi_p)
6533 it->bidi_it.string.schars = it->end_charpos;
6536 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6537 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6538 FIELD_WIDTH < 0 means infinite field width. This is useful for
6539 padding with `-' at the end of a mode line. */
6540 if (field_width < 0)
6541 field_width = INFINITY;
6542 /* Implementation note: We deliberately don't enlarge
6543 it->bidi_it.string.schars here to fit it->end_charpos, because
6544 the bidi iterator cannot produce characters out of thin air. */
6545 if (field_width > it->end_charpos - charpos)
6546 it->end_charpos = charpos + field_width;
6548 /* Use the standard display table for displaying strings. */
6549 if (DISP_TABLE_P (Vstandard_display_table))
6550 it->dp = XCHAR_TABLE (Vstandard_display_table);
6552 it->stop_charpos = charpos;
6553 it->prev_stop = charpos;
6554 it->base_level_stop = 0;
6555 if (it->bidi_p)
6557 it->bidi_it.first_elt = 1;
6558 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6559 it->bidi_it.disp_pos = -1;
6561 if (s == NULL && it->multibyte_p)
6563 ptrdiff_t endpos = SCHARS (it->string);
6564 if (endpos > it->end_charpos)
6565 endpos = it->end_charpos;
6566 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6567 it->string);
6569 CHECK_IT (it);
6574 /***********************************************************************
6575 Iteration
6576 ***********************************************************************/
6578 /* Map enum it_method value to corresponding next_element_from_* function. */
6580 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6582 next_element_from_buffer,
6583 next_element_from_display_vector,
6584 next_element_from_string,
6585 next_element_from_c_string,
6586 next_element_from_image,
6587 next_element_from_stretch
6590 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6593 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6594 (possibly with the following characters). */
6596 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6597 ((IT)->cmp_it.id >= 0 \
6598 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6599 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6600 END_CHARPOS, (IT)->w, \
6601 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6602 (IT)->string)))
6605 /* Lookup the char-table Vglyphless_char_display for character C (-1
6606 if we want information for no-font case), and return the display
6607 method symbol. By side-effect, update it->what and
6608 it->glyphless_method. This function is called from
6609 get_next_display_element for each character element, and from
6610 x_produce_glyphs when no suitable font was found. */
6612 Lisp_Object
6613 lookup_glyphless_char_display (int c, struct it *it)
6615 Lisp_Object glyphless_method = Qnil;
6617 if (CHAR_TABLE_P (Vglyphless_char_display)
6618 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6620 if (c >= 0)
6622 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6623 if (CONSP (glyphless_method))
6624 glyphless_method = FRAME_WINDOW_P (it->f)
6625 ? XCAR (glyphless_method)
6626 : XCDR (glyphless_method);
6628 else
6629 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6632 retry:
6633 if (NILP (glyphless_method))
6635 if (c >= 0)
6636 /* The default is to display the character by a proper font. */
6637 return Qnil;
6638 /* The default for the no-font case is to display an empty box. */
6639 glyphless_method = Qempty_box;
6641 if (EQ (glyphless_method, Qzero_width))
6643 if (c >= 0)
6644 return glyphless_method;
6645 /* This method can't be used for the no-font case. */
6646 glyphless_method = Qempty_box;
6648 if (EQ (glyphless_method, Qthin_space))
6649 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6650 else if (EQ (glyphless_method, Qempty_box))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6652 else if (EQ (glyphless_method, Qhex_code))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6654 else if (STRINGP (glyphless_method))
6655 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6656 else
6658 /* Invalid value. We use the default method. */
6659 glyphless_method = Qnil;
6660 goto retry;
6662 it->what = IT_GLYPHLESS;
6663 return glyphless_method;
6666 /* Merge escape glyph face and cache the result. */
6668 static struct frame *last_escape_glyph_frame = NULL;
6669 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6670 static int last_escape_glyph_merged_face_id = 0;
6672 static int
6673 merge_escape_glyph_face (struct it *it)
6675 int face_id;
6677 if (it->f == last_escape_glyph_frame
6678 && it->face_id == last_escape_glyph_face_id)
6679 face_id = last_escape_glyph_merged_face_id;
6680 else
6682 /* Merge the `escape-glyph' face into the current face. */
6683 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6688 return face_id;
6691 /* Likewise for glyphless glyph face. */
6693 static struct frame *last_glyphless_glyph_frame = NULL;
6694 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6695 static int last_glyphless_glyph_merged_face_id = 0;
6698 merge_glyphless_glyph_face (struct it *it)
6700 int face_id;
6702 if (it->f == last_glyphless_glyph_frame
6703 && it->face_id == last_glyphless_glyph_face_id)
6704 face_id = last_glyphless_glyph_merged_face_id;
6705 else
6707 /* Merge the `glyphless-char' face into the current face. */
6708 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6709 last_glyphless_glyph_frame = it->f;
6710 last_glyphless_glyph_face_id = it->face_id;
6711 last_glyphless_glyph_merged_face_id = face_id;
6713 return face_id;
6716 /* Load IT's display element fields with information about the next
6717 display element from the current position of IT. Value is zero if
6718 end of buffer (or C string) is reached. */
6720 static int
6721 get_next_display_element (struct it *it)
6723 /* Non-zero means that we found a display element. Zero means that
6724 we hit the end of what we iterate over. Performance note: the
6725 function pointer `method' used here turns out to be faster than
6726 using a sequence of if-statements. */
6727 int success_p;
6729 get_next:
6730 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6732 if (it->what == IT_CHARACTER)
6734 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6735 and only if (a) the resolved directionality of that character
6736 is R..." */
6737 /* FIXME: Do we need an exception for characters from display
6738 tables? */
6739 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6740 it->c = bidi_mirror_char (it->c);
6741 /* Map via display table or translate control characters.
6742 IT->c, IT->len etc. have been set to the next character by
6743 the function call above. If we have a display table, and it
6744 contains an entry for IT->c, translate it. Don't do this if
6745 IT->c itself comes from a display table, otherwise we could
6746 end up in an infinite recursion. (An alternative could be to
6747 count the recursion depth of this function and signal an
6748 error when a certain maximum depth is reached.) Is it worth
6749 it? */
6750 if (success_p && it->dpvec == NULL)
6752 Lisp_Object dv;
6753 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6754 int nonascii_space_p = 0;
6755 int nonascii_hyphen_p = 0;
6756 int c = it->c; /* This is the character to display. */
6758 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6760 eassert (SINGLE_BYTE_CHAR_P (c));
6761 if (unibyte_display_via_language_environment)
6763 c = DECODE_CHAR (unibyte, c);
6764 if (c < 0)
6765 c = BYTE8_TO_CHAR (it->c);
6767 else
6768 c = BYTE8_TO_CHAR (it->c);
6771 if (it->dp
6772 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6773 VECTORP (dv)))
6775 struct Lisp_Vector *v = XVECTOR (dv);
6777 /* Return the first character from the display table
6778 entry, if not empty. If empty, don't display the
6779 current character. */
6780 if (v->header.size)
6782 it->dpvec_char_len = it->len;
6783 it->dpvec = v->contents;
6784 it->dpend = v->contents + v->header.size;
6785 it->current.dpvec_index = 0;
6786 it->dpvec_face_id = -1;
6787 it->saved_face_id = it->face_id;
6788 it->method = GET_FROM_DISPLAY_VECTOR;
6789 it->ellipsis_p = 0;
6791 else
6793 set_iterator_to_next (it, 0);
6795 goto get_next;
6798 if (! NILP (lookup_glyphless_char_display (c, it)))
6800 if (it->what == IT_GLYPHLESS)
6801 goto done;
6802 /* Don't display this character. */
6803 set_iterator_to_next (it, 0);
6804 goto get_next;
6807 /* If `nobreak-char-display' is non-nil, we display
6808 non-ASCII spaces and hyphens specially. */
6809 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6811 if (c == 0xA0)
6812 nonascii_space_p = true;
6813 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6814 nonascii_hyphen_p = true;
6817 /* Translate control characters into `\003' or `^C' form.
6818 Control characters coming from a display table entry are
6819 currently not translated because we use IT->dpvec to hold
6820 the translation. This could easily be changed but I
6821 don't believe that it is worth doing.
6823 The characters handled by `nobreak-char-display' must be
6824 translated too.
6826 Non-printable characters and raw-byte characters are also
6827 translated to octal form. */
6828 if (((c < ' ' || c == 127) /* ASCII control chars. */
6829 ? (it->area != TEXT_AREA
6830 /* In mode line, treat \n, \t like other crl chars. */
6831 || (c != '\t'
6832 && it->glyph_row
6833 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6834 || (c != '\n' && c != '\t'))
6835 : (nonascii_space_p
6836 || nonascii_hyphen_p
6837 || CHAR_BYTE8_P (c)
6838 || ! CHAR_PRINTABLE_P (c))))
6840 /* C is a control character, non-ASCII space/hyphen,
6841 raw-byte, or a non-printable character which must be
6842 displayed either as '\003' or as `^C' where the '\\'
6843 and '^' can be defined in the display table. Fill
6844 IT->ctl_chars with glyphs for what we have to
6845 display. Then, set IT->dpvec to these glyphs. */
6846 Lisp_Object gc;
6847 int ctl_len;
6848 int face_id;
6849 int lface_id = 0;
6850 int escape_glyph;
6852 /* Handle control characters with ^. */
6854 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6856 int g;
6858 g = '^'; /* default glyph for Control */
6859 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6860 if (it->dp
6861 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6863 g = GLYPH_CODE_CHAR (gc);
6864 lface_id = GLYPH_CODE_FACE (gc);
6867 face_id = (lface_id
6868 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6869 : merge_escape_glyph_face (it));
6871 XSETINT (it->ctl_chars[0], g);
6872 XSETINT (it->ctl_chars[1], c ^ 0100);
6873 ctl_len = 2;
6874 goto display_control;
6877 /* Handle non-ascii space in the mode where it only gets
6878 highlighting. */
6880 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6882 /* Merge `nobreak-space' into the current face. */
6883 face_id = merge_faces (it->f, Qnobreak_space, 0,
6884 it->face_id);
6885 XSETINT (it->ctl_chars[0], ' ');
6886 ctl_len = 1;
6887 goto display_control;
6890 /* Handle sequences that start with the "escape glyph". */
6892 /* the default escape glyph is \. */
6893 escape_glyph = '\\';
6895 if (it->dp
6896 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6898 escape_glyph = GLYPH_CODE_CHAR (gc);
6899 lface_id = GLYPH_CODE_FACE (gc);
6902 face_id = (lface_id
6903 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6904 : merge_escape_glyph_face (it));
6906 /* Draw non-ASCII hyphen with just highlighting: */
6908 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6910 XSETINT (it->ctl_chars[0], '-');
6911 ctl_len = 1;
6912 goto display_control;
6915 /* Draw non-ASCII space/hyphen with escape glyph: */
6917 if (nonascii_space_p || nonascii_hyphen_p)
6919 XSETINT (it->ctl_chars[0], escape_glyph);
6920 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6921 ctl_len = 2;
6922 goto display_control;
6926 char str[10];
6927 int len, i;
6929 if (CHAR_BYTE8_P (c))
6930 /* Display \200 instead of \17777600. */
6931 c = CHAR_TO_BYTE8 (c);
6932 len = sprintf (str, "%03o", c);
6934 XSETINT (it->ctl_chars[0], escape_glyph);
6935 for (i = 0; i < len; i++)
6936 XSETINT (it->ctl_chars[i + 1], str[i]);
6937 ctl_len = len + 1;
6940 display_control:
6941 /* Set up IT->dpvec and return first character from it. */
6942 it->dpvec_char_len = it->len;
6943 it->dpvec = it->ctl_chars;
6944 it->dpend = it->dpvec + ctl_len;
6945 it->current.dpvec_index = 0;
6946 it->dpvec_face_id = face_id;
6947 it->saved_face_id = it->face_id;
6948 it->method = GET_FROM_DISPLAY_VECTOR;
6949 it->ellipsis_p = 0;
6950 goto get_next;
6952 it->char_to_display = c;
6954 else if (success_p)
6956 it->char_to_display = it->c;
6960 #ifdef HAVE_WINDOW_SYSTEM
6961 /* Adjust face id for a multibyte character. There are no multibyte
6962 character in unibyte text. */
6963 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6964 && it->multibyte_p
6965 && success_p
6966 && FRAME_WINDOW_P (it->f))
6968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6970 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6972 /* Automatic composition with glyph-string. */
6973 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6975 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6977 else
6979 ptrdiff_t pos = (it->s ? -1
6980 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6981 : IT_CHARPOS (*it));
6982 int c;
6984 if (it->what == IT_CHARACTER)
6985 c = it->char_to_display;
6986 else
6988 struct composition *cmp = composition_table[it->cmp_it.id];
6989 int i;
6991 c = ' ';
6992 for (i = 0; i < cmp->glyph_len; i++)
6993 /* TAB in a composition means display glyphs with
6994 padding space on the left or right. */
6995 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6996 break;
6998 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 #endif /* HAVE_WINDOW_SYSTEM */
7003 done:
7004 /* Is this character the last one of a run of characters with
7005 box? If yes, set IT->end_of_box_run_p to 1. */
7006 if (it->face_box_p
7007 && it->s == NULL)
7009 if (it->method == GET_FROM_STRING && it->sp)
7011 int face_id = underlying_face_id (it);
7012 struct face *face = FACE_FROM_ID (it->f, face_id);
7014 if (face)
7016 if (face->box == FACE_NO_BOX)
7018 /* If the box comes from face properties in a
7019 display string, check faces in that string. */
7020 int string_face_id = face_after_it_pos (it);
7021 it->end_of_box_run_p
7022 = (FACE_FROM_ID (it->f, string_face_id)->box
7023 == FACE_NO_BOX);
7025 /* Otherwise, the box comes from the underlying face.
7026 If this is the last string character displayed, check
7027 the next buffer location. */
7028 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7029 && (it->current.overlay_string_index
7030 == it->n_overlay_strings - 1))
7032 ptrdiff_t ignore;
7033 int next_face_id;
7034 struct text_pos pos = it->current.pos;
7035 INC_TEXT_POS (pos, it->multibyte_p);
7037 next_face_id = face_at_buffer_position
7038 (it->w, CHARPOS (pos), &ignore,
7039 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7040 -1);
7041 it->end_of_box_run_p
7042 = (FACE_FROM_ID (it->f, next_face_id)->box
7043 == FACE_NO_BOX);
7047 /* next_element_from_display_vector sets this flag according to
7048 faces of the display vector glyphs, see there. */
7049 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7073 /* Move IT to the next display element.
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7096 switch (it->method)
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7109 if (! it->bidi_p)
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7115 it->cmp_it.from = it->cmp_it.to;
7117 else
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7125 else if (! it->cmp_it.reversed_p)
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7141 else
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7154 else
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7169 else
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7183 else
7185 eassert (it->len != 0);
7187 if (!it->bidi_p)
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7192 else
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7215 break;
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7229 else
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7235 break;
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7250 int recheck_faces = it->ellipsis_p;
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = true;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7278 /* Maybe recheck faces after display vector. */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7282 break;
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7300 else
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos. */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7307 if (it->cmp_it.id >= 0)
7309 int i;
7311 if (! it->bidi_p)
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7326 else if (! it->cmp_it.reversed_p)
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7346 else
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7366 else
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7379 else
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7388 ptrdiff_t stop = it->end_charpos;
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7400 consider_string_end:
7402 if (it->current.overlay_string_index >= 0)
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7414 else
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7428 break;
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7460 static int
7461 next_element_from_display_vector (struct it *it)
7463 Lisp_Object gc;
7464 int prev_face_id = it->face_id;
7465 int next_face_id;
7467 /* Precondition. */
7468 eassert (it->dpvec && it->current.dpvec_index >= 0);
7470 it->face_id = it->saved_face_id;
7472 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7473 That seemed totally bogus - so I changed it... */
7474 gc = it->dpvec[it->current.dpvec_index];
7476 if (GLYPH_CODE_P (gc))
7478 struct face *this_face, *prev_face, *next_face;
7480 it->c = GLYPH_CODE_CHAR (gc);
7481 it->len = CHAR_BYTES (it->c);
7483 /* The entry may contain a face id to use. Such a face id is
7484 the id of a Lisp face, not a realized face. A face id of
7485 zero means no face is specified. */
7486 if (it->dpvec_face_id >= 0)
7487 it->face_id = it->dpvec_face_id;
7488 else
7490 int lface_id = GLYPH_CODE_FACE (gc);
7491 if (lface_id > 0)
7492 it->face_id = merge_faces (it->f, Qt, lface_id,
7493 it->saved_face_id);
7496 /* Glyphs in the display vector could have the box face, so we
7497 need to set the related flags in the iterator, as
7498 appropriate. */
7499 this_face = FACE_FROM_ID (it->f, it->face_id);
7500 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7502 /* Is this character the first character of a box-face run? */
7503 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7504 && (!prev_face
7505 || prev_face->box == FACE_NO_BOX));
7507 /* For the last character of the box-face run, we need to look
7508 either at the next glyph from the display vector, or at the
7509 face we saw before the display vector. */
7510 next_face_id = it->saved_face_id;
7511 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7513 if (it->dpvec_face_id >= 0)
7514 next_face_id = it->dpvec_face_id;
7515 else
7517 int lface_id =
7518 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7520 if (lface_id > 0)
7521 next_face_id = merge_faces (it->f, Qt, lface_id,
7522 it->saved_face_id);
7525 next_face = FACE_FROM_ID (it->f, next_face_id);
7526 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7527 && (!next_face
7528 || next_face->box == FACE_NO_BOX));
7529 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7531 else
7532 /* Display table entry is invalid. Return a space. */
7533 it->c = ' ', it->len = 1;
7535 /* Don't change position and object of the iterator here. They are
7536 still the values of the character that had this display table
7537 entry or was translated, and that's what we want. */
7538 it->what = IT_CHARACTER;
7539 return 1;
7542 /* Get the first element of string/buffer in the visual order, after
7543 being reseated to a new position in a string or a buffer. */
7544 static void
7545 get_visually_first_element (struct it *it)
7547 int string_p = STRINGP (it->string) || it->s;
7548 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7549 ptrdiff_t bob = (string_p ? 0 : BEGV);
7551 if (STRINGP (it->string))
7553 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7554 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7556 else
7558 it->bidi_it.charpos = IT_CHARPOS (*it);
7559 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7562 if (it->bidi_it.charpos == eob)
7564 /* Nothing to do, but reset the FIRST_ELT flag, like
7565 bidi_paragraph_init does, because we are not going to
7566 call it. */
7567 it->bidi_it.first_elt = 0;
7569 else if (it->bidi_it.charpos == bob
7570 || (!string_p
7571 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7572 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7574 /* If we are at the beginning of a line/string, we can produce
7575 the next element right away. */
7576 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7577 bidi_move_to_visually_next (&it->bidi_it);
7579 else
7581 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7583 /* We need to prime the bidi iterator starting at the line's or
7584 string's beginning, before we will be able to produce the
7585 next element. */
7586 if (string_p)
7587 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7588 else
7589 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7590 IT_BYTEPOS (*it), -1,
7591 &it->bidi_it.bytepos);
7592 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7595 /* Now return to buffer/string position where we were asked
7596 to get the next display element, and produce that. */
7597 bidi_move_to_visually_next (&it->bidi_it);
7599 while (it->bidi_it.bytepos != orig_bytepos
7600 && it->bidi_it.charpos < eob);
7603 /* Adjust IT's position information to where we ended up. */
7604 if (STRINGP (it->string))
7606 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7607 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7609 else
7611 IT_CHARPOS (*it) = it->bidi_it.charpos;
7612 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 if (STRINGP (it->string) || !it->s)
7617 ptrdiff_t stop, charpos, bytepos;
7619 if (STRINGP (it->string))
7621 eassert (!it->s);
7622 stop = SCHARS (it->string);
7623 if (stop > it->end_charpos)
7624 stop = it->end_charpos;
7625 charpos = IT_STRING_CHARPOS (*it);
7626 bytepos = IT_STRING_BYTEPOS (*it);
7628 else
7630 stop = it->end_charpos;
7631 charpos = IT_CHARPOS (*it);
7632 bytepos = IT_BYTEPOS (*it);
7634 if (it->bidi_it.scan_dir < 0)
7635 stop = -1;
7636 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7637 it->string);
7641 /* Load IT with the next display element from Lisp string IT->string.
7642 IT->current.string_pos is the current position within the string.
7643 If IT->current.overlay_string_index >= 0, the Lisp string is an
7644 overlay string. */
7646 static int
7647 next_element_from_string (struct it *it)
7649 struct text_pos position;
7651 eassert (STRINGP (it->string));
7652 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7653 eassert (IT_STRING_CHARPOS (*it) >= 0);
7654 position = it->current.string_pos;
7656 /* With bidi reordering, the character to display might not be the
7657 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7658 that we were reseat()ed to a new string, whose paragraph
7659 direction is not known. */
7660 if (it->bidi_p && it->bidi_it.first_elt)
7662 get_visually_first_element (it);
7663 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7666 /* Time to check for invisible text? */
7667 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7669 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7671 if (!(!it->bidi_p
7672 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7673 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7675 /* With bidi non-linear iteration, we could find
7676 ourselves far beyond the last computed stop_charpos,
7677 with several other stop positions in between that we
7678 missed. Scan them all now, in buffer's logical
7679 order, until we find and handle the last stop_charpos
7680 that precedes our current position. */
7681 handle_stop_backwards (it, it->stop_charpos);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7684 else
7686 if (it->bidi_p)
7688 /* Take note of the stop position we just moved
7689 across, for when we will move back across it. */
7690 it->prev_stop = it->stop_charpos;
7691 /* If we are at base paragraph embedding level, take
7692 note of the last stop position seen at this
7693 level. */
7694 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7695 it->base_level_stop = it->stop_charpos;
7697 handle_stop (it);
7699 /* Since a handler may have changed IT->method, we must
7700 recurse here. */
7701 return GET_NEXT_DISPLAY_ELEMENT (it);
7704 else if (it->bidi_p
7705 /* If we are before prev_stop, we may have overstepped
7706 on our way backwards a stop_pos, and if so, we need
7707 to handle that stop_pos. */
7708 && IT_STRING_CHARPOS (*it) < it->prev_stop
7709 /* We can sometimes back up for reasons that have nothing
7710 to do with bidi reordering. E.g., compositions. The
7711 code below is only needed when we are above the base
7712 embedding level, so test for that explicitly. */
7713 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7715 /* If we lost track of base_level_stop, we have no better
7716 place for handle_stop_backwards to start from than string
7717 beginning. This happens, e.g., when we were reseated to
7718 the previous screenful of text by vertical-motion. */
7719 if (it->base_level_stop <= 0
7720 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7721 it->base_level_stop = 0;
7722 handle_stop_backwards (it, it->base_level_stop);
7723 return GET_NEXT_DISPLAY_ELEMENT (it);
7727 if (it->current.overlay_string_index >= 0)
7729 /* Get the next character from an overlay string. In overlay
7730 strings, there is no field width or padding with spaces to
7731 do. */
7732 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7734 it->what = IT_EOB;
7735 return 0;
7737 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7738 IT_STRING_BYTEPOS (*it),
7739 it->bidi_it.scan_dir < 0
7740 ? -1
7741 : SCHARS (it->string))
7742 && next_element_from_composition (it))
7744 return 1;
7746 else if (STRING_MULTIBYTE (it->string))
7748 const unsigned char *s = (SDATA (it->string)
7749 + IT_STRING_BYTEPOS (*it));
7750 it->c = string_char_and_length (s, &it->len);
7752 else
7754 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7755 it->len = 1;
7758 else
7760 /* Get the next character from a Lisp string that is not an
7761 overlay string. Such strings come from the mode line, for
7762 example. We may have to pad with spaces, or truncate the
7763 string. See also next_element_from_c_string. */
7764 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7766 it->what = IT_EOB;
7767 return 0;
7769 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7771 /* Pad with spaces. */
7772 it->c = ' ', it->len = 1;
7773 CHARPOS (position) = BYTEPOS (position) = -1;
7775 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7776 IT_STRING_BYTEPOS (*it),
7777 it->bidi_it.scan_dir < 0
7778 ? -1
7779 : it->string_nchars)
7780 && next_element_from_composition (it))
7782 return 1;
7784 else if (STRING_MULTIBYTE (it->string))
7786 const unsigned char *s = (SDATA (it->string)
7787 + IT_STRING_BYTEPOS (*it));
7788 it->c = string_char_and_length (s, &it->len);
7790 else
7792 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7793 it->len = 1;
7797 /* Record what we have and where it came from. */
7798 it->what = IT_CHARACTER;
7799 it->object = it->string;
7800 it->position = position;
7801 return 1;
7805 /* Load IT with next display element from C string IT->s.
7806 IT->string_nchars is the maximum number of characters to return
7807 from the string. IT->end_charpos may be greater than
7808 IT->string_nchars when this function is called, in which case we
7809 may have to return padding spaces. Value is zero if end of string
7810 reached, including padding spaces. */
7812 static int
7813 next_element_from_c_string (struct it *it)
7815 bool success_p = true;
7817 eassert (it->s);
7818 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7819 it->what = IT_CHARACTER;
7820 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7821 it->object = Qnil;
7823 /* With bidi reordering, the character to display might not be the
7824 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7825 we were reseated to a new string, whose paragraph direction is
7826 not known. */
7827 if (it->bidi_p && it->bidi_it.first_elt)
7828 get_visually_first_element (it);
7830 /* IT's position can be greater than IT->string_nchars in case a
7831 field width or precision has been specified when the iterator was
7832 initialized. */
7833 if (IT_CHARPOS (*it) >= it->end_charpos)
7835 /* End of the game. */
7836 it->what = IT_EOB;
7837 success_p = 0;
7839 else if (IT_CHARPOS (*it) >= it->string_nchars)
7841 /* Pad with spaces. */
7842 it->c = ' ', it->len = 1;
7843 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7845 else if (it->multibyte_p)
7846 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7847 else
7848 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7850 return success_p;
7854 /* Set up IT to return characters from an ellipsis, if appropriate.
7855 The definition of the ellipsis glyphs may come from a display table
7856 entry. This function fills IT with the first glyph from the
7857 ellipsis if an ellipsis is to be displayed. */
7859 static int
7860 next_element_from_ellipsis (struct it *it)
7862 if (it->selective_display_ellipsis_p)
7863 setup_for_ellipsis (it, it->len);
7864 else
7866 /* The face at the current position may be different from the
7867 face we find after the invisible text. Remember what it
7868 was in IT->saved_face_id, and signal that it's there by
7869 setting face_before_selective_p. */
7870 it->saved_face_id = it->face_id;
7871 it->method = GET_FROM_BUFFER;
7872 it->object = it->w->contents;
7873 reseat_at_next_visible_line_start (it, 1);
7874 it->face_before_selective_p = true;
7877 return GET_NEXT_DISPLAY_ELEMENT (it);
7881 /* Deliver an image display element. The iterator IT is already
7882 filled with image information (done in handle_display_prop). Value
7883 is always 1. */
7886 static int
7887 next_element_from_image (struct it *it)
7889 it->what = IT_IMAGE;
7890 it->ignore_overlay_strings_at_pos_p = 0;
7891 return 1;
7895 /* Fill iterator IT with next display element from a stretch glyph
7896 property. IT->object is the value of the text property. Value is
7897 always 1. */
7899 static int
7900 next_element_from_stretch (struct it *it)
7902 it->what = IT_STRETCH;
7903 return 1;
7906 /* Scan backwards from IT's current position until we find a stop
7907 position, or until BEGV. This is called when we find ourself
7908 before both the last known prev_stop and base_level_stop while
7909 reordering bidirectional text. */
7911 static void
7912 compute_stop_pos_backwards (struct it *it)
7914 const int SCAN_BACK_LIMIT = 1000;
7915 struct text_pos pos;
7916 struct display_pos save_current = it->current;
7917 struct text_pos save_position = it->position;
7918 ptrdiff_t charpos = IT_CHARPOS (*it);
7919 ptrdiff_t where_we_are = charpos;
7920 ptrdiff_t save_stop_pos = it->stop_charpos;
7921 ptrdiff_t save_end_pos = it->end_charpos;
7923 eassert (NILP (it->string) && !it->s);
7924 eassert (it->bidi_p);
7925 it->bidi_p = 0;
7928 it->end_charpos = min (charpos + 1, ZV);
7929 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7930 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7931 reseat_1 (it, pos, 0);
7932 compute_stop_pos (it);
7933 /* We must advance forward, right? */
7934 if (it->stop_charpos <= charpos)
7935 emacs_abort ();
7937 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7939 if (it->stop_charpos <= where_we_are)
7940 it->prev_stop = it->stop_charpos;
7941 else
7942 it->prev_stop = BEGV;
7943 it->bidi_p = true;
7944 it->current = save_current;
7945 it->position = save_position;
7946 it->stop_charpos = save_stop_pos;
7947 it->end_charpos = save_end_pos;
7950 /* Scan forward from CHARPOS in the current buffer/string, until we
7951 find a stop position > current IT's position. Then handle the stop
7952 position before that. This is called when we bump into a stop
7953 position while reordering bidirectional text. CHARPOS should be
7954 the last previously processed stop_pos (or BEGV/0, if none were
7955 processed yet) whose position is less that IT's current
7956 position. */
7958 static void
7959 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7961 int bufp = !STRINGP (it->string);
7962 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7963 struct display_pos save_current = it->current;
7964 struct text_pos save_position = it->position;
7965 struct text_pos pos1;
7966 ptrdiff_t next_stop;
7968 /* Scan in strict logical order. */
7969 eassert (it->bidi_p);
7970 it->bidi_p = 0;
7973 it->prev_stop = charpos;
7974 if (bufp)
7976 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7977 reseat_1 (it, pos1, 0);
7979 else
7980 it->current.string_pos = string_pos (charpos, it->string);
7981 compute_stop_pos (it);
7982 /* We must advance forward, right? */
7983 if (it->stop_charpos <= it->prev_stop)
7984 emacs_abort ();
7985 charpos = it->stop_charpos;
7987 while (charpos <= where_we_are);
7989 it->bidi_p = true;
7990 it->current = save_current;
7991 it->position = save_position;
7992 next_stop = it->stop_charpos;
7993 it->stop_charpos = it->prev_stop;
7994 handle_stop (it);
7995 it->stop_charpos = next_stop;
7998 /* Load IT with the next display element from current_buffer. Value
7999 is zero if end of buffer reached. IT->stop_charpos is the next
8000 position at which to stop and check for text properties or buffer
8001 end. */
8003 static int
8004 next_element_from_buffer (struct it *it)
8006 bool success_p = true;
8008 eassert (IT_CHARPOS (*it) >= BEGV);
8009 eassert (NILP (it->string) && !it->s);
8010 eassert (!it->bidi_p
8011 || (EQ (it->bidi_it.string.lstring, Qnil)
8012 && it->bidi_it.string.s == NULL));
8014 /* With bidi reordering, the character to display might not be the
8015 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8016 we were reseat()ed to a new buffer position, which is potentially
8017 a different paragraph. */
8018 if (it->bidi_p && it->bidi_it.first_elt)
8020 get_visually_first_element (it);
8021 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8024 if (IT_CHARPOS (*it) >= it->stop_charpos)
8026 if (IT_CHARPOS (*it) >= it->end_charpos)
8028 int overlay_strings_follow_p;
8030 /* End of the game, except when overlay strings follow that
8031 haven't been returned yet. */
8032 if (it->overlay_strings_at_end_processed_p)
8033 overlay_strings_follow_p = 0;
8034 else
8036 it->overlay_strings_at_end_processed_p = true;
8037 overlay_strings_follow_p = get_overlay_strings (it, 0);
8040 if (overlay_strings_follow_p)
8041 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8042 else
8044 it->what = IT_EOB;
8045 it->position = it->current.pos;
8046 success_p = 0;
8049 else if (!(!it->bidi_p
8050 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8051 || IT_CHARPOS (*it) == it->stop_charpos))
8053 /* With bidi non-linear iteration, we could find ourselves
8054 far beyond the last computed stop_charpos, with several
8055 other stop positions in between that we missed. Scan
8056 them all now, in buffer's logical order, until we find
8057 and handle the last stop_charpos that precedes our
8058 current position. */
8059 handle_stop_backwards (it, it->stop_charpos);
8060 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 else
8064 if (it->bidi_p)
8066 /* Take note of the stop position we just moved across,
8067 for when we will move back across it. */
8068 it->prev_stop = it->stop_charpos;
8069 /* If we are at base paragraph embedding level, take
8070 note of the last stop position seen at this
8071 level. */
8072 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8073 it->base_level_stop = it->stop_charpos;
8075 handle_stop (it);
8076 return GET_NEXT_DISPLAY_ELEMENT (it);
8079 else if (it->bidi_p
8080 /* If we are before prev_stop, we may have overstepped on
8081 our way backwards a stop_pos, and if so, we need to
8082 handle that stop_pos. */
8083 && IT_CHARPOS (*it) < it->prev_stop
8084 /* We can sometimes back up for reasons that have nothing
8085 to do with bidi reordering. E.g., compositions. The
8086 code below is only needed when we are above the base
8087 embedding level, so test for that explicitly. */
8088 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8090 if (it->base_level_stop <= 0
8091 || IT_CHARPOS (*it) < it->base_level_stop)
8093 /* If we lost track of base_level_stop, we need to find
8094 prev_stop by looking backwards. This happens, e.g., when
8095 we were reseated to the previous screenful of text by
8096 vertical-motion. */
8097 it->base_level_stop = BEGV;
8098 compute_stop_pos_backwards (it);
8099 handle_stop_backwards (it, it->prev_stop);
8101 else
8102 handle_stop_backwards (it, it->base_level_stop);
8103 return GET_NEXT_DISPLAY_ELEMENT (it);
8105 else
8107 /* No face changes, overlays etc. in sight, so just return a
8108 character from current_buffer. */
8109 unsigned char *p;
8110 ptrdiff_t stop;
8112 /* Maybe run the redisplay end trigger hook. Performance note:
8113 This doesn't seem to cost measurable time. */
8114 if (it->redisplay_end_trigger_charpos
8115 && it->glyph_row
8116 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8117 run_redisplay_end_trigger_hook (it);
8119 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8120 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8121 stop)
8122 && next_element_from_composition (it))
8124 return 1;
8127 /* Get the next character, maybe multibyte. */
8128 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8129 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8130 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8131 else
8132 it->c = *p, it->len = 1;
8134 /* Record what we have and where it came from. */
8135 it->what = IT_CHARACTER;
8136 it->object = it->w->contents;
8137 it->position = it->current.pos;
8139 /* Normally we return the character found above, except when we
8140 really want to return an ellipsis for selective display. */
8141 if (it->selective)
8143 if (it->c == '\n')
8145 /* A value of selective > 0 means hide lines indented more
8146 than that number of columns. */
8147 if (it->selective > 0
8148 && IT_CHARPOS (*it) + 1 < ZV
8149 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8150 IT_BYTEPOS (*it) + 1,
8151 it->selective))
8153 success_p = next_element_from_ellipsis (it);
8154 it->dpvec_char_len = -1;
8157 else if (it->c == '\r' && it->selective == -1)
8159 /* A value of selective == -1 means that everything from the
8160 CR to the end of the line is invisible, with maybe an
8161 ellipsis displayed for it. */
8162 success_p = next_element_from_ellipsis (it);
8163 it->dpvec_char_len = -1;
8168 /* Value is zero if end of buffer reached. */
8169 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8170 return success_p;
8174 /* Run the redisplay end trigger hook for IT. */
8176 static void
8177 run_redisplay_end_trigger_hook (struct it *it)
8179 Lisp_Object args[3];
8181 /* IT->glyph_row should be non-null, i.e. we should be actually
8182 displaying something, or otherwise we should not run the hook. */
8183 eassert (it->glyph_row);
8185 /* Set up hook arguments. */
8186 args[0] = Qredisplay_end_trigger_functions;
8187 args[1] = it->window;
8188 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8189 it->redisplay_end_trigger_charpos = 0;
8191 /* Since we are *trying* to run these functions, don't try to run
8192 them again, even if they get an error. */
8193 wset_redisplay_end_trigger (it->w, Qnil);
8194 Frun_hook_with_args (3, args);
8196 /* Notice if it changed the face of the character we are on. */
8197 handle_face_prop (it);
8201 /* Deliver a composition display element. Unlike the other
8202 next_element_from_XXX, this function is not registered in the array
8203 get_next_element[]. It is called from next_element_from_buffer and
8204 next_element_from_string when necessary. */
8206 static int
8207 next_element_from_composition (struct it *it)
8209 it->what = IT_COMPOSITION;
8210 it->len = it->cmp_it.nbytes;
8211 if (STRINGP (it->string))
8213 if (it->c < 0)
8215 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8216 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8217 return 0;
8219 it->position = it->current.string_pos;
8220 it->object = it->string;
8221 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8222 IT_STRING_BYTEPOS (*it), it->string);
8224 else
8226 if (it->c < 0)
8228 IT_CHARPOS (*it) += it->cmp_it.nchars;
8229 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8230 if (it->bidi_p)
8232 if (it->bidi_it.new_paragraph)
8233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8234 /* Resync the bidi iterator with IT's new position.
8235 FIXME: this doesn't support bidirectional text. */
8236 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8237 bidi_move_to_visually_next (&it->bidi_it);
8239 return 0;
8241 it->position = it->current.pos;
8242 it->object = it->w->contents;
8243 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8244 IT_BYTEPOS (*it), Qnil);
8246 return 1;
8251 /***********************************************************************
8252 Moving an iterator without producing glyphs
8253 ***********************************************************************/
8255 /* Check if iterator is at a position corresponding to a valid buffer
8256 position after some move_it_ call. */
8258 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8259 ((it)->method == GET_FROM_STRING \
8260 ? IT_STRING_CHARPOS (*it) == 0 \
8261 : 1)
8264 /* Move iterator IT to a specified buffer or X position within one
8265 line on the display without producing glyphs.
8267 OP should be a bit mask including some or all of these bits:
8268 MOVE_TO_X: Stop upon reaching x-position TO_X.
8269 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8270 Regardless of OP's value, stop upon reaching the end of the display line.
8272 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8273 This means, in particular, that TO_X includes window's horizontal
8274 scroll amount.
8276 The return value has several possible values that
8277 say what condition caused the scan to stop:
8279 MOVE_POS_MATCH_OR_ZV
8280 - when TO_POS or ZV was reached.
8282 MOVE_X_REACHED
8283 -when TO_X was reached before TO_POS or ZV were reached.
8285 MOVE_LINE_CONTINUED
8286 - when we reached the end of the display area and the line must
8287 be continued.
8289 MOVE_LINE_TRUNCATED
8290 - when we reached the end of the display area and the line is
8291 truncated.
8293 MOVE_NEWLINE_OR_CR
8294 - when we stopped at a line end, i.e. a newline or a CR and selective
8295 display is on. */
8297 static enum move_it_result
8298 move_it_in_display_line_to (struct it *it,
8299 ptrdiff_t to_charpos, int to_x,
8300 enum move_operation_enum op)
8302 enum move_it_result result = MOVE_UNDEFINED;
8303 struct glyph_row *saved_glyph_row;
8304 struct it wrap_it, atpos_it, atx_it, ppos_it;
8305 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8306 void *ppos_data = NULL;
8307 int may_wrap = 0;
8308 enum it_method prev_method = it->method;
8309 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8310 int saw_smaller_pos = prev_pos < to_charpos;
8312 /* Don't produce glyphs in produce_glyphs. */
8313 saved_glyph_row = it->glyph_row;
8314 it->glyph_row = NULL;
8316 /* Use wrap_it to save a copy of IT wherever a word wrap could
8317 occur. Use atpos_it to save a copy of IT at the desired buffer
8318 position, if found, so that we can scan ahead and check if the
8319 word later overshoots the window edge. Use atx_it similarly, for
8320 pixel positions. */
8321 wrap_it.sp = -1;
8322 atpos_it.sp = -1;
8323 atx_it.sp = -1;
8325 /* Use ppos_it under bidi reordering to save a copy of IT for the
8326 position > CHARPOS that is the closest to CHARPOS. We restore
8327 that position in IT when we have scanned the entire display line
8328 without finding a match for CHARPOS and all the character
8329 positions are greater than CHARPOS. */
8330 if (it->bidi_p)
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8334 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8335 SAVE_IT (ppos_it, *it, ppos_data);
8338 #define BUFFER_POS_REACHED_P() \
8339 ((op & MOVE_TO_POS) != 0 \
8340 && BUFFERP (it->object) \
8341 && (IT_CHARPOS (*it) == to_charpos \
8342 || ((!it->bidi_p \
8343 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8344 && IT_CHARPOS (*it) > to_charpos) \
8345 || (it->what == IT_COMPOSITION \
8346 && ((IT_CHARPOS (*it) > to_charpos \
8347 && to_charpos >= it->cmp_it.charpos) \
8348 || (IT_CHARPOS (*it) < to_charpos \
8349 && to_charpos <= it->cmp_it.charpos)))) \
8350 && (it->method == GET_FROM_BUFFER \
8351 || (it->method == GET_FROM_DISPLAY_VECTOR \
8352 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8354 /* If there's a line-/wrap-prefix, handle it. */
8355 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8356 && it->current_y < it->last_visible_y)
8357 handle_line_prefix (it);
8359 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8360 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8362 while (1)
8364 int x, i, ascent = 0, descent = 0;
8366 /* Utility macro to reset an iterator with x, ascent, and descent. */
8367 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8368 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8369 (IT)->max_descent = descent)
8371 /* Stop if we move beyond TO_CHARPOS (after an image or a
8372 display string or stretch glyph). */
8373 if ((op & MOVE_TO_POS) != 0
8374 && BUFFERP (it->object)
8375 && it->method == GET_FROM_BUFFER
8376 && (((!it->bidi_p
8377 /* When the iterator is at base embedding level, we
8378 are guaranteed that characters are delivered for
8379 display in strictly increasing order of their
8380 buffer positions. */
8381 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8382 && IT_CHARPOS (*it) > to_charpos)
8383 || (it->bidi_p
8384 && (prev_method == GET_FROM_IMAGE
8385 || prev_method == GET_FROM_STRETCH
8386 || prev_method == GET_FROM_STRING)
8387 /* Passed TO_CHARPOS from left to right. */
8388 && ((prev_pos < to_charpos
8389 && IT_CHARPOS (*it) > to_charpos)
8390 /* Passed TO_CHARPOS from right to left. */
8391 || (prev_pos > to_charpos
8392 && IT_CHARPOS (*it) < to_charpos)))))
8394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 break;
8399 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8400 /* If wrap_it is valid, the current position might be in a
8401 word that is wrapped. So, save the iterator in
8402 atpos_it and continue to see if wrapping happens. */
8403 SAVE_IT (atpos_it, *it, atpos_data);
8406 /* Stop when ZV reached.
8407 We used to stop here when TO_CHARPOS reached as well, but that is
8408 too soon if this glyph does not fit on this line. So we handle it
8409 explicitly below. */
8410 if (!get_next_display_element (it))
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 break;
8416 if (it->line_wrap == TRUNCATE)
8418 if (BUFFER_POS_REACHED_P ())
8420 result = MOVE_POS_MATCH_OR_ZV;
8421 break;
8424 else
8426 if (it->line_wrap == WORD_WRAP)
8428 if (IT_DISPLAYING_WHITESPACE (it))
8429 may_wrap = 1;
8430 else if (may_wrap)
8432 /* We have reached a glyph that follows one or more
8433 whitespace characters. If the position is
8434 already found, we are done. */
8435 if (atpos_it.sp >= 0)
8437 RESTORE_IT (it, &atpos_it, atpos_data);
8438 result = MOVE_POS_MATCH_OR_ZV;
8439 goto done;
8441 if (atx_it.sp >= 0)
8443 RESTORE_IT (it, &atx_it, atx_data);
8444 result = MOVE_X_REACHED;
8445 goto done;
8447 /* Otherwise, we can wrap here. */
8448 SAVE_IT (wrap_it, *it, wrap_data);
8449 may_wrap = 0;
8454 /* Remember the line height for the current line, in case
8455 the next element doesn't fit on the line. */
8456 ascent = it->max_ascent;
8457 descent = it->max_descent;
8459 /* The call to produce_glyphs will get the metrics of the
8460 display element IT is loaded with. Record the x-position
8461 before this display element, in case it doesn't fit on the
8462 line. */
8463 x = it->current_x;
8465 PRODUCE_GLYPHS (it);
8467 if (it->area != TEXT_AREA)
8469 prev_method = it->method;
8470 if (it->method == GET_FROM_BUFFER)
8471 prev_pos = IT_CHARPOS (*it);
8472 set_iterator_to_next (it, 1);
8473 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8474 SET_TEXT_POS (this_line_min_pos,
8475 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8476 if (it->bidi_p
8477 && (op & MOVE_TO_POS)
8478 && IT_CHARPOS (*it) > to_charpos
8479 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8480 SAVE_IT (ppos_it, *it, ppos_data);
8481 continue;
8484 /* The number of glyphs we get back in IT->nglyphs will normally
8485 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8486 character on a terminal frame, or (iii) a line end. For the
8487 second case, IT->nglyphs - 1 padding glyphs will be present.
8488 (On X frames, there is only one glyph produced for a
8489 composite character.)
8491 The behavior implemented below means, for continuation lines,
8492 that as many spaces of a TAB as fit on the current line are
8493 displayed there. For terminal frames, as many glyphs of a
8494 multi-glyph character are displayed in the current line, too.
8495 This is what the old redisplay code did, and we keep it that
8496 way. Under X, the whole shape of a complex character must
8497 fit on the line or it will be completely displayed in the
8498 next line.
8500 Note that both for tabs and padding glyphs, all glyphs have
8501 the same width. */
8502 if (it->nglyphs)
8504 /* More than one glyph or glyph doesn't fit on line. All
8505 glyphs have the same width. */
8506 int single_glyph_width = it->pixel_width / it->nglyphs;
8507 int new_x;
8508 int x_before_this_char = x;
8509 int hpos_before_this_char = it->hpos;
8511 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8513 new_x = x + single_glyph_width;
8515 /* We want to leave anything reaching TO_X to the caller. */
8516 if ((op & MOVE_TO_X) && new_x > to_x)
8518 if (BUFFER_POS_REACHED_P ())
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 goto buffer_pos_reached;
8522 if (atpos_it.sp < 0)
8524 SAVE_IT (atpos_it, *it, atpos_data);
8525 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8528 else
8530 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8532 it->current_x = x;
8533 result = MOVE_X_REACHED;
8534 break;
8536 if (atx_it.sp < 0)
8538 SAVE_IT (atx_it, *it, atx_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8544 if (/* Lines are continued. */
8545 it->line_wrap != TRUNCATE
8546 && (/* And glyph doesn't fit on the line. */
8547 new_x > it->last_visible_x
8548 /* Or it fits exactly and we're on a window
8549 system frame. */
8550 || (new_x == it->last_visible_x
8551 && FRAME_WINDOW_P (it->f)
8552 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8553 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8554 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8556 if (/* IT->hpos == 0 means the very first glyph
8557 doesn't fit on the line, e.g. a wide image. */
8558 it->hpos == 0
8559 || (new_x == it->last_visible_x
8560 && FRAME_WINDOW_P (it->f)))
8562 ++it->hpos;
8563 it->current_x = new_x;
8565 /* The character's last glyph just barely fits
8566 in this row. */
8567 if (i == it->nglyphs - 1)
8569 /* If this is the destination position,
8570 return a position *before* it in this row,
8571 now that we know it fits in this row. */
8572 if (BUFFER_POS_REACHED_P ())
8574 if (it->line_wrap != WORD_WRAP
8575 || wrap_it.sp < 0)
8577 it->hpos = hpos_before_this_char;
8578 it->current_x = x_before_this_char;
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 break;
8582 if (it->line_wrap == WORD_WRAP
8583 && atpos_it.sp < 0)
8585 SAVE_IT (atpos_it, *it, atpos_data);
8586 atpos_it.current_x = x_before_this_char;
8587 atpos_it.hpos = hpos_before_this_char;
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 set_iterator_to_next (it, 1);
8595 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8596 SET_TEXT_POS (this_line_min_pos,
8597 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8598 /* On graphical terminals, newlines may
8599 "overflow" into the fringe if
8600 overflow-newline-into-fringe is non-nil.
8601 On text terminals, and on graphical
8602 terminals with no right margin, newlines
8603 may overflow into the last glyph on the
8604 display line.*/
8605 if (!FRAME_WINDOW_P (it->f)
8606 || ((it->bidi_p
8607 && it->bidi_it.paragraph_dir == R2L)
8608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8610 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8612 if (!get_next_display_element (it))
8614 result = MOVE_POS_MATCH_OR_ZV;
8615 break;
8617 if (BUFFER_POS_REACHED_P ())
8619 if (ITERATOR_AT_END_OF_LINE_P (it))
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 else
8622 result = MOVE_LINE_CONTINUED;
8623 break;
8625 if (ITERATOR_AT_END_OF_LINE_P (it)
8626 && (it->line_wrap != WORD_WRAP
8627 || wrap_it.sp < 0))
8629 result = MOVE_NEWLINE_OR_CR;
8630 break;
8635 else
8636 IT_RESET_X_ASCENT_DESCENT (it);
8638 if (wrap_it.sp >= 0)
8640 RESTORE_IT (it, &wrap_it, wrap_data);
8641 atpos_it.sp = -1;
8642 atx_it.sp = -1;
8645 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8646 IT_CHARPOS (*it)));
8647 result = MOVE_LINE_CONTINUED;
8648 break;
8651 if (BUFFER_POS_REACHED_P ())
8653 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8654 goto buffer_pos_reached;
8655 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8657 SAVE_IT (atpos_it, *it, atpos_data);
8658 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8662 if (new_x > it->first_visible_x)
8664 /* Glyph is visible. Increment number of glyphs that
8665 would be displayed. */
8666 ++it->hpos;
8670 if (result != MOVE_UNDEFINED)
8671 break;
8673 else if (BUFFER_POS_REACHED_P ())
8675 buffer_pos_reached:
8676 IT_RESET_X_ASCENT_DESCENT (it);
8677 result = MOVE_POS_MATCH_OR_ZV;
8678 break;
8680 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8682 /* Stop when TO_X specified and reached. This check is
8683 necessary here because of lines consisting of a line end,
8684 only. The line end will not produce any glyphs and we
8685 would never get MOVE_X_REACHED. */
8686 eassert (it->nglyphs == 0);
8687 result = MOVE_X_REACHED;
8688 break;
8691 /* Is this a line end? If yes, we're done. */
8692 if (ITERATOR_AT_END_OF_LINE_P (it))
8694 /* If we are past TO_CHARPOS, but never saw any character
8695 positions smaller than TO_CHARPOS, return
8696 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8697 did. */
8698 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8700 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8702 if (IT_CHARPOS (ppos_it) < ZV)
8704 RESTORE_IT (it, &ppos_it, ppos_data);
8705 result = MOVE_POS_MATCH_OR_ZV;
8707 else
8708 goto buffer_pos_reached;
8710 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8711 && IT_CHARPOS (*it) > to_charpos)
8712 goto buffer_pos_reached;
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8716 else
8717 result = MOVE_NEWLINE_OR_CR;
8718 break;
8721 prev_method = it->method;
8722 if (it->method == GET_FROM_BUFFER)
8723 prev_pos = IT_CHARPOS (*it);
8724 /* The current display element has been consumed. Advance
8725 to the next. */
8726 set_iterator_to_next (it, 1);
8727 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8728 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8729 if (IT_CHARPOS (*it) < to_charpos)
8730 saw_smaller_pos = 1;
8731 if (it->bidi_p
8732 && (op & MOVE_TO_POS)
8733 && IT_CHARPOS (*it) >= to_charpos
8734 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8735 SAVE_IT (ppos_it, *it, ppos_data);
8737 /* Stop if lines are truncated and IT's current x-position is
8738 past the right edge of the window now. */
8739 if (it->line_wrap == TRUNCATE
8740 && it->current_x >= it->last_visible_x)
8742 if (!FRAME_WINDOW_P (it->f)
8743 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8744 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8745 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8746 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8748 int at_eob_p = 0;
8750 if ((at_eob_p = !get_next_display_element (it))
8751 || BUFFER_POS_REACHED_P ()
8752 /* If we are past TO_CHARPOS, but never saw any
8753 character positions smaller than TO_CHARPOS,
8754 return MOVE_POS_MATCH_OR_ZV, like the
8755 unidirectional display did. */
8756 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8757 && !saw_smaller_pos
8758 && IT_CHARPOS (*it) > to_charpos))
8760 if (it->bidi_p
8761 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8762 RESTORE_IT (it, &ppos_it, ppos_data);
8763 result = MOVE_POS_MATCH_OR_ZV;
8764 break;
8766 if (ITERATOR_AT_END_OF_LINE_P (it))
8768 result = MOVE_NEWLINE_OR_CR;
8769 break;
8772 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8773 && !saw_smaller_pos
8774 && IT_CHARPOS (*it) > to_charpos)
8776 if (IT_CHARPOS (ppos_it) < ZV)
8777 RESTORE_IT (it, &ppos_it, ppos_data);
8778 result = MOVE_POS_MATCH_OR_ZV;
8779 break;
8781 result = MOVE_LINE_TRUNCATED;
8782 break;
8784 #undef IT_RESET_X_ASCENT_DESCENT
8787 #undef BUFFER_POS_REACHED_P
8789 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8790 restore the saved iterator. */
8791 if (atpos_it.sp >= 0)
8792 RESTORE_IT (it, &atpos_it, atpos_data);
8793 else if (atx_it.sp >= 0)
8794 RESTORE_IT (it, &atx_it, atx_data);
8796 done:
8798 if (atpos_data)
8799 bidi_unshelve_cache (atpos_data, 1);
8800 if (atx_data)
8801 bidi_unshelve_cache (atx_data, 1);
8802 if (wrap_data)
8803 bidi_unshelve_cache (wrap_data, 1);
8804 if (ppos_data)
8805 bidi_unshelve_cache (ppos_data, 1);
8807 /* Restore the iterator settings altered at the beginning of this
8808 function. */
8809 it->glyph_row = saved_glyph_row;
8810 return result;
8813 /* For external use. */
8814 void
8815 move_it_in_display_line (struct it *it,
8816 ptrdiff_t to_charpos, int to_x,
8817 enum move_operation_enum op)
8819 if (it->line_wrap == WORD_WRAP
8820 && (op & MOVE_TO_X))
8822 struct it save_it;
8823 void *save_data = NULL;
8824 int skip;
8826 SAVE_IT (save_it, *it, save_data);
8827 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8828 /* When word-wrap is on, TO_X may lie past the end
8829 of a wrapped line. Then it->current is the
8830 character on the next line, so backtrack to the
8831 space before the wrap point. */
8832 if (skip == MOVE_LINE_CONTINUED)
8834 int prev_x = max (it->current_x - 1, 0);
8835 RESTORE_IT (it, &save_it, save_data);
8836 move_it_in_display_line_to
8837 (it, -1, prev_x, MOVE_TO_X);
8839 else
8840 bidi_unshelve_cache (save_data, 1);
8842 else
8843 move_it_in_display_line_to (it, to_charpos, to_x, op);
8847 /* Move IT forward until it satisfies one or more of the criteria in
8848 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8850 OP is a bit-mask that specifies where to stop, and in particular,
8851 which of those four position arguments makes a difference. See the
8852 description of enum move_operation_enum.
8854 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8855 screen line, this function will set IT to the next position that is
8856 displayed to the right of TO_CHARPOS on the screen.
8858 Return the maximum pixel length of any line scanned but never more
8859 than it.last_visible_x. */
8862 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8864 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8865 int line_height, line_start_x = 0, reached = 0;
8866 int max_current_x = 0;
8867 void *backup_data = NULL;
8869 for (;;)
8871 if (op & MOVE_TO_VPOS)
8873 /* If no TO_CHARPOS and no TO_X specified, stop at the
8874 start of the line TO_VPOS. */
8875 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8877 if (it->vpos == to_vpos)
8879 reached = 1;
8880 break;
8882 else
8883 skip = move_it_in_display_line_to (it, -1, -1, 0);
8885 else
8887 /* TO_VPOS >= 0 means stop at TO_X in the line at
8888 TO_VPOS, or at TO_POS, whichever comes first. */
8889 if (it->vpos == to_vpos)
8891 reached = 2;
8892 break;
8895 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8897 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8899 reached = 3;
8900 break;
8902 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8904 /* We have reached TO_X but not in the line we want. */
8905 skip = move_it_in_display_line_to (it, to_charpos,
8906 -1, MOVE_TO_POS);
8907 if (skip == MOVE_POS_MATCH_OR_ZV)
8909 reached = 4;
8910 break;
8915 else if (op & MOVE_TO_Y)
8917 struct it it_backup;
8919 if (it->line_wrap == WORD_WRAP)
8920 SAVE_IT (it_backup, *it, backup_data);
8922 /* TO_Y specified means stop at TO_X in the line containing
8923 TO_Y---or at TO_CHARPOS if this is reached first. The
8924 problem is that we can't really tell whether the line
8925 contains TO_Y before we have completely scanned it, and
8926 this may skip past TO_X. What we do is to first scan to
8927 TO_X.
8929 If TO_X is not specified, use a TO_X of zero. The reason
8930 is to make the outcome of this function more predictable.
8931 If we didn't use TO_X == 0, we would stop at the end of
8932 the line which is probably not what a caller would expect
8933 to happen. */
8934 skip = move_it_in_display_line_to
8935 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8936 (MOVE_TO_X | (op & MOVE_TO_POS)));
8938 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 5;
8941 else if (skip == MOVE_X_REACHED)
8943 /* If TO_X was reached, we want to know whether TO_Y is
8944 in the line. We know this is the case if the already
8945 scanned glyphs make the line tall enough. Otherwise,
8946 we must check by scanning the rest of the line. */
8947 line_height = it->max_ascent + it->max_descent;
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8951 reached = 6;
8952 break;
8954 SAVE_IT (it_backup, *it, backup_data);
8955 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8956 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8957 op & MOVE_TO_POS);
8958 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8959 line_height = it->max_ascent + it->max_descent;
8960 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8962 if (to_y >= it->current_y
8963 && to_y < it->current_y + line_height)
8965 /* If TO_Y is in this line and TO_X was reached
8966 above, we scanned too far. We have to restore
8967 IT's settings to the ones before skipping. But
8968 keep the more accurate values of max_ascent and
8969 max_descent we've found while skipping the rest
8970 of the line, for the sake of callers, such as
8971 pos_visible_p, that need to know the line
8972 height. */
8973 int max_ascent = it->max_ascent;
8974 int max_descent = it->max_descent;
8976 RESTORE_IT (it, &it_backup, backup_data);
8977 it->max_ascent = max_ascent;
8978 it->max_descent = max_descent;
8979 reached = 6;
8981 else
8983 skip = skip2;
8984 if (skip == MOVE_POS_MATCH_OR_ZV)
8985 reached = 7;
8988 else
8990 /* Check whether TO_Y is in this line. */
8991 line_height = it->max_ascent + it->max_descent;
8992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8994 if (to_y >= it->current_y
8995 && to_y < it->current_y + line_height)
8997 if (to_y > it->current_y)
8998 max_current_x = max (it->current_x, max_current_x);
9000 /* When word-wrap is on, TO_X may lie past the end
9001 of a wrapped line. Then it->current is the
9002 character on the next line, so backtrack to the
9003 space before the wrap point. */
9004 if (skip == MOVE_LINE_CONTINUED
9005 && it->line_wrap == WORD_WRAP)
9007 int prev_x = max (it->current_x - 1, 0);
9008 RESTORE_IT (it, &it_backup, backup_data);
9009 skip = move_it_in_display_line_to
9010 (it, -1, prev_x, MOVE_TO_X);
9013 reached = 6;
9017 if (reached)
9019 max_current_x = max (it->current_x, max_current_x);
9020 break;
9023 else if (BUFFERP (it->object)
9024 && (it->method == GET_FROM_BUFFER
9025 || it->method == GET_FROM_STRETCH)
9026 && IT_CHARPOS (*it) >= to_charpos
9027 /* Under bidi iteration, a call to set_iterator_to_next
9028 can scan far beyond to_charpos if the initial
9029 portion of the next line needs to be reordered. In
9030 that case, give move_it_in_display_line_to another
9031 chance below. */
9032 && !(it->bidi_p
9033 && it->bidi_it.scan_dir == -1))
9034 skip = MOVE_POS_MATCH_OR_ZV;
9035 else
9036 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9038 switch (skip)
9040 case MOVE_POS_MATCH_OR_ZV:
9041 max_current_x = max (it->current_x, max_current_x);
9042 reached = 8;
9043 goto out;
9045 case MOVE_NEWLINE_OR_CR:
9046 max_current_x = max (it->current_x, max_current_x);
9047 set_iterator_to_next (it, 1);
9048 it->continuation_lines_width = 0;
9049 break;
9051 case MOVE_LINE_TRUNCATED:
9052 max_current_x = it->last_visible_x;
9053 it->continuation_lines_width = 0;
9054 reseat_at_next_visible_line_start (it, 0);
9055 if ((op & MOVE_TO_POS) != 0
9056 && IT_CHARPOS (*it) > to_charpos)
9058 reached = 9;
9059 goto out;
9061 break;
9063 case MOVE_LINE_CONTINUED:
9064 max_current_x = it->last_visible_x;
9065 /* For continued lines ending in a tab, some of the glyphs
9066 associated with the tab are displayed on the current
9067 line. Since it->current_x does not include these glyphs,
9068 we use it->last_visible_x instead. */
9069 if (it->c == '\t')
9071 it->continuation_lines_width += it->last_visible_x;
9072 /* When moving by vpos, ensure that the iterator really
9073 advances to the next line (bug#847, bug#969). Fixme:
9074 do we need to do this in other circumstances? */
9075 if (it->current_x != it->last_visible_x
9076 && (op & MOVE_TO_VPOS)
9077 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9079 line_start_x = it->current_x + it->pixel_width
9080 - it->last_visible_x;
9081 set_iterator_to_next (it, 0);
9084 else
9085 it->continuation_lines_width += it->current_x;
9086 break;
9088 default:
9089 emacs_abort ();
9092 /* Reset/increment for the next run. */
9093 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9094 it->current_x = line_start_x;
9095 line_start_x = 0;
9096 it->hpos = 0;
9097 it->current_y += it->max_ascent + it->max_descent;
9098 ++it->vpos;
9099 last_height = it->max_ascent + it->max_descent;
9100 last_max_ascent = it->max_ascent;
9101 it->max_ascent = it->max_descent = 0;
9104 out:
9106 /* On text terminals, we may stop at the end of a line in the middle
9107 of a multi-character glyph. If the glyph itself is continued,
9108 i.e. it is actually displayed on the next line, don't treat this
9109 stopping point as valid; move to the next line instead (unless
9110 that brings us offscreen). */
9111 if (!FRAME_WINDOW_P (it->f)
9112 && op & MOVE_TO_POS
9113 && IT_CHARPOS (*it) == to_charpos
9114 && it->what == IT_CHARACTER
9115 && it->nglyphs > 1
9116 && it->line_wrap == WINDOW_WRAP
9117 && it->current_x == it->last_visible_x - 1
9118 && it->c != '\n'
9119 && it->c != '\t'
9120 && it->vpos < it->w->window_end_vpos)
9122 it->continuation_lines_width += it->current_x;
9123 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9124 it->current_y += it->max_ascent + it->max_descent;
9125 ++it->vpos;
9126 last_height = it->max_ascent + it->max_descent;
9127 last_max_ascent = it->max_ascent;
9130 if (backup_data)
9131 bidi_unshelve_cache (backup_data, 1);
9133 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9135 return max_current_x;
9139 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9141 If DY > 0, move IT backward at least that many pixels. DY = 0
9142 means move IT backward to the preceding line start or BEGV. This
9143 function may move over more than DY pixels if IT->current_y - DY
9144 ends up in the middle of a line; in this case IT->current_y will be
9145 set to the top of the line moved to. */
9147 void
9148 move_it_vertically_backward (struct it *it, int dy)
9150 int nlines, h;
9151 struct it it2, it3;
9152 void *it2data = NULL, *it3data = NULL;
9153 ptrdiff_t start_pos;
9154 int nchars_per_row
9155 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9156 ptrdiff_t pos_limit;
9158 move_further_back:
9159 eassert (dy >= 0);
9161 start_pos = IT_CHARPOS (*it);
9163 /* Estimate how many newlines we must move back. */
9164 nlines = max (1, dy / default_line_pixel_height (it->w));
9165 if (it->line_wrap == TRUNCATE)
9166 pos_limit = BEGV;
9167 else
9168 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9170 /* Set the iterator's position that many lines back. But don't go
9171 back more than NLINES full screen lines -- this wins a day with
9172 buffers which have very long lines. */
9173 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9174 back_to_previous_visible_line_start (it);
9176 /* Reseat the iterator here. When moving backward, we don't want
9177 reseat to skip forward over invisible text, set up the iterator
9178 to deliver from overlay strings at the new position etc. So,
9179 use reseat_1 here. */
9180 reseat_1 (it, it->current.pos, 1);
9182 /* We are now surely at a line start. */
9183 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9184 reordering is in effect. */
9185 it->continuation_lines_width = 0;
9187 /* Move forward and see what y-distance we moved. First move to the
9188 start of the next line so that we get its height. We need this
9189 height to be able to tell whether we reached the specified
9190 y-distance. */
9191 SAVE_IT (it2, *it, it2data);
9192 it2.max_ascent = it2.max_descent = 0;
9195 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9196 MOVE_TO_POS | MOVE_TO_VPOS);
9198 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9199 /* If we are in a display string which starts at START_POS,
9200 and that display string includes a newline, and we are
9201 right after that newline (i.e. at the beginning of a
9202 display line), exit the loop, because otherwise we will
9203 infloop, since move_it_to will see that it is already at
9204 START_POS and will not move. */
9205 || (it2.method == GET_FROM_STRING
9206 && IT_CHARPOS (it2) == start_pos
9207 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9208 eassert (IT_CHARPOS (*it) >= BEGV);
9209 SAVE_IT (it3, it2, it3data);
9211 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9212 eassert (IT_CHARPOS (*it) >= BEGV);
9213 /* H is the actual vertical distance from the position in *IT
9214 and the starting position. */
9215 h = it2.current_y - it->current_y;
9216 /* NLINES is the distance in number of lines. */
9217 nlines = it2.vpos - it->vpos;
9219 /* Correct IT's y and vpos position
9220 so that they are relative to the starting point. */
9221 it->vpos -= nlines;
9222 it->current_y -= h;
9224 if (dy == 0)
9226 /* DY == 0 means move to the start of the screen line. The
9227 value of nlines is > 0 if continuation lines were involved,
9228 or if the original IT position was at start of a line. */
9229 RESTORE_IT (it, it, it2data);
9230 if (nlines > 0)
9231 move_it_by_lines (it, nlines);
9232 /* The above code moves us to some position NLINES down,
9233 usually to its first glyph (leftmost in an L2R line), but
9234 that's not necessarily the start of the line, under bidi
9235 reordering. We want to get to the character position
9236 that is immediately after the newline of the previous
9237 line. */
9238 if (it->bidi_p
9239 && !it->continuation_lines_width
9240 && !STRINGP (it->string)
9241 && IT_CHARPOS (*it) > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9244 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9246 DEC_BOTH (cp, bp);
9247 cp = find_newline_no_quit (cp, bp, -1, NULL);
9248 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9250 bidi_unshelve_cache (it3data, 1);
9252 else
9254 /* The y-position we try to reach, relative to *IT.
9255 Note that H has been subtracted in front of the if-statement. */
9256 int target_y = it->current_y + h - dy;
9257 int y0 = it3.current_y;
9258 int y1;
9259 int line_height;
9261 RESTORE_IT (&it3, &it3, it3data);
9262 y1 = line_bottom_y (&it3);
9263 line_height = y1 - y0;
9264 RESTORE_IT (it, it, it2data);
9265 /* If we did not reach target_y, try to move further backward if
9266 we can. If we moved too far backward, try to move forward. */
9267 if (target_y < it->current_y
9268 /* This is heuristic. In a window that's 3 lines high, with
9269 a line height of 13 pixels each, recentering with point
9270 on the bottom line will try to move -39/2 = 19 pixels
9271 backward. Try to avoid moving into the first line. */
9272 && (it->current_y - target_y
9273 > min (window_box_height (it->w), line_height * 2 / 3))
9274 && IT_CHARPOS (*it) > BEGV)
9276 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9277 target_y - it->current_y));
9278 dy = it->current_y - target_y;
9279 goto move_further_back;
9281 else if (target_y >= it->current_y + line_height
9282 && IT_CHARPOS (*it) < ZV)
9284 /* Should move forward by at least one line, maybe more.
9286 Note: Calling move_it_by_lines can be expensive on
9287 terminal frames, where compute_motion is used (via
9288 vmotion) to do the job, when there are very long lines
9289 and truncate-lines is nil. That's the reason for
9290 treating terminal frames specially here. */
9292 if (!FRAME_WINDOW_P (it->f))
9293 move_it_vertically (it, target_y - (it->current_y + line_height));
9294 else
9298 move_it_by_lines (it, 1);
9300 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9307 /* Move IT by a specified amount of pixel lines DY. DY negative means
9308 move backwards. DY = 0 means move to start of screen line. At the
9309 end, IT will be on the start of a screen line. */
9311 void
9312 move_it_vertically (struct it *it, int dy)
9314 if (dy <= 0)
9315 move_it_vertically_backward (it, -dy);
9316 else
9318 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9319 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9320 MOVE_TO_POS | MOVE_TO_Y);
9321 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9323 /* If buffer ends in ZV without a newline, move to the start of
9324 the line to satisfy the post-condition. */
9325 if (IT_CHARPOS (*it) == ZV
9326 && ZV > BEGV
9327 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9328 move_it_by_lines (it, 0);
9333 /* Move iterator IT past the end of the text line it is in. */
9335 void
9336 move_it_past_eol (struct it *it)
9338 enum move_it_result rc;
9340 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9341 if (rc == MOVE_NEWLINE_OR_CR)
9342 set_iterator_to_next (it, 0);
9346 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9347 negative means move up. DVPOS == 0 means move to the start of the
9348 screen line.
9350 Optimization idea: If we would know that IT->f doesn't use
9351 a face with proportional font, we could be faster for
9352 truncate-lines nil. */
9354 void
9355 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9358 /* The commented-out optimization uses vmotion on terminals. This
9359 gives bad results, because elements like it->what, on which
9360 callers such as pos_visible_p rely, aren't updated. */
9361 /* struct position pos;
9362 if (!FRAME_WINDOW_P (it->f))
9364 struct text_pos textpos;
9366 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9367 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9368 reseat (it, textpos, 1);
9369 it->vpos += pos.vpos;
9370 it->current_y += pos.vpos;
9372 else */
9374 if (dvpos == 0)
9376 /* DVPOS == 0 means move to the start of the screen line. */
9377 move_it_vertically_backward (it, 0);
9378 /* Let next call to line_bottom_y calculate real line height. */
9379 last_height = 0;
9381 else if (dvpos > 0)
9383 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9384 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9386 /* Only move to the next buffer position if we ended up in a
9387 string from display property, not in an overlay string
9388 (before-string or after-string). That is because the
9389 latter don't conceal the underlying buffer position, so
9390 we can ask to move the iterator to the exact position we
9391 are interested in. Note that, even if we are already at
9392 IT_CHARPOS (*it), the call below is not a no-op, as it
9393 will detect that we are at the end of the string, pop the
9394 iterator, and compute it->current_x and it->hpos
9395 correctly. */
9396 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9397 -1, -1, -1, MOVE_TO_POS);
9400 else
9402 struct it it2;
9403 void *it2data = NULL;
9404 ptrdiff_t start_charpos, i;
9405 int nchars_per_row
9406 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9407 ptrdiff_t pos_limit;
9409 /* Start at the beginning of the screen line containing IT's
9410 position. This may actually move vertically backwards,
9411 in case of overlays, so adjust dvpos accordingly. */
9412 dvpos += it->vpos;
9413 move_it_vertically_backward (it, 0);
9414 dvpos -= it->vpos;
9416 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9417 screen lines, and reseat the iterator there. */
9418 start_charpos = IT_CHARPOS (*it);
9419 if (it->line_wrap == TRUNCATE)
9420 pos_limit = BEGV;
9421 else
9422 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9423 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9424 back_to_previous_visible_line_start (it);
9425 reseat (it, it->current.pos, 1);
9427 /* Move further back if we end up in a string or an image. */
9428 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9430 /* First try to move to start of display line. */
9431 dvpos += it->vpos;
9432 move_it_vertically_backward (it, 0);
9433 dvpos -= it->vpos;
9434 if (IT_POS_VALID_AFTER_MOVE_P (it))
9435 break;
9436 /* If start of line is still in string or image,
9437 move further back. */
9438 back_to_previous_visible_line_start (it);
9439 reseat (it, it->current.pos, 1);
9440 dvpos--;
9443 it->current_x = it->hpos = 0;
9445 /* Above call may have moved too far if continuation lines
9446 are involved. Scan forward and see if it did. */
9447 SAVE_IT (it2, *it, it2data);
9448 it2.vpos = it2.current_y = 0;
9449 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9450 it->vpos -= it2.vpos;
9451 it->current_y -= it2.current_y;
9452 it->current_x = it->hpos = 0;
9454 /* If we moved too far back, move IT some lines forward. */
9455 if (it2.vpos > -dvpos)
9457 int delta = it2.vpos + dvpos;
9459 RESTORE_IT (&it2, &it2, it2data);
9460 SAVE_IT (it2, *it, it2data);
9461 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9462 /* Move back again if we got too far ahead. */
9463 if (IT_CHARPOS (*it) >= start_charpos)
9464 RESTORE_IT (it, &it2, it2data);
9465 else
9466 bidi_unshelve_cache (it2data, 1);
9468 else
9469 RESTORE_IT (it, it, it2data);
9473 /* Return true if IT points into the middle of a display vector. */
9475 bool
9476 in_display_vector_p (struct it *it)
9478 return (it->method == GET_FROM_DISPLAY_VECTOR
9479 && it->current.dpvec_index > 0
9480 && it->dpvec + it->current.dpvec_index != it->dpend);
9483 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9484 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9485 WINDOW must be a live window and defaults to the selected one. The
9486 return value is a cons of the maximum pixel-width of any text line and
9487 the maximum pixel-height of all text lines.
9489 The optional argument FROM, if non-nil, specifies the first text
9490 position and defaults to the minimum accessible position of the buffer.
9491 If FROM is t, use the minimum accessible position that is not a newline
9492 character. TO, if non-nil, specifies the last text position and
9493 defaults to the maximum accessible position of the buffer. If TO is t,
9494 use the maximum accessible position that is not a newline character.
9496 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9497 width that can be returned. X_LIMIT nil or omitted, means to use the
9498 pixel-width of WINDOW's body; use this if you do not intend to change
9499 the width of WINDOW. Use the maximum width WINDOW may assume if you
9500 intend to change WINDOW's width.
9502 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9503 height that can be returned. Text lines whose y-coordinate is beyond
9504 Y_LIMIT are ignored. Since calculating the text height of a large
9505 buffer can take some time, it makes sense to specify this argument if
9506 the size of the buffer is unknown.
9508 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9509 include the height of the mode- or header-line of WINDOW in the return
9510 value. If it is either the symbol `mode-line' or `header-line', include
9511 only the height of that line, if present, in the return value. If t,
9512 include the height of any of these lines in the return value. */)
9513 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9514 Lisp_Object mode_and_header_line)
9516 struct window *w = decode_live_window (window);
9517 Lisp_Object buf;
9518 struct buffer *b;
9519 struct it it;
9520 struct buffer *old_buffer = NULL;
9521 ptrdiff_t start, end, pos;
9522 struct text_pos startp;
9523 void *itdata = NULL;
9524 int c, max_y = -1, x = 0, y = 0;
9526 buf = w->contents;
9527 CHECK_BUFFER (buf);
9528 b = XBUFFER (buf);
9530 if (b != current_buffer)
9532 old_buffer = current_buffer;
9533 set_buffer_internal (b);
9536 if (NILP (from))
9537 start = BEGV;
9538 else if (EQ (from, Qt))
9540 start = pos = BEGV;
9541 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9542 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9543 start = pos;
9544 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9545 start = pos;
9547 else
9549 CHECK_NUMBER_COERCE_MARKER (from);
9550 start = min (max (XINT (from), BEGV), ZV);
9553 if (NILP (to))
9554 end = ZV;
9555 else if (EQ (to, Qt))
9557 end = pos = ZV;
9558 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9559 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9560 end = pos;
9561 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9562 end = pos;
9564 else
9566 CHECK_NUMBER_COERCE_MARKER (to);
9567 end = max (start, min (XINT (to), ZV));
9570 if (!NILP (y_limit))
9572 CHECK_NUMBER (y_limit);
9573 max_y = min (XINT (y_limit), INT_MAX);
9576 itdata = bidi_shelve_cache ();
9577 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9578 start_display (&it, w, startp);
9580 /** move_it_vertically_backward (&it, 0); **/
9581 if (NILP (x_limit))
9582 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9583 else
9585 CHECK_NUMBER (x_limit);
9586 it.last_visible_x = min (XINT (x_limit), INFINITY);
9587 /* Actually, we never want move_it_to stop at to_x. But to make
9588 sure that move_it_in_display_line_to always moves far enough,
9589 we set it to INT_MAX and specify MOVE_TO_X. */
9590 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9591 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9594 if (start == end)
9595 y = it.current_y;
9596 else
9598 /* Count last line. */
9599 last_height = 0;
9600 y = line_bottom_y (&it); /* - y; */
9603 if (!EQ (mode_and_header_line, Qheader_line)
9604 && !EQ (mode_and_header_line, Qt))
9605 /* Do not count the header-line which was counted automatically by
9606 start_display. */
9607 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9609 if (EQ (mode_and_header_line, Qmode_line)
9610 || EQ (mode_and_header_line, Qt))
9611 /* Do count the mode-line which is not included automatically by
9612 start_display. */
9613 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9615 bidi_unshelve_cache (itdata, 0);
9617 if (old_buffer)
9618 set_buffer_internal (old_buffer);
9620 return Fcons (make_number (x), make_number (y));
9623 /***********************************************************************
9624 Messages
9625 ***********************************************************************/
9628 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9629 to *Messages*. */
9631 void
9632 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9634 Lisp_Object args[3];
9635 Lisp_Object msg, fmt;
9636 char *buffer;
9637 ptrdiff_t len;
9638 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9639 USE_SAFE_ALLOCA;
9641 fmt = msg = Qnil;
9642 GCPRO4 (fmt, msg, arg1, arg2);
9644 args[0] = fmt = build_string (format);
9645 args[1] = arg1;
9646 args[2] = arg2;
9647 msg = Fformat (3, args);
9649 len = SBYTES (msg) + 1;
9650 buffer = SAFE_ALLOCA (len);
9651 memcpy (buffer, SDATA (msg), len);
9653 message_dolog (buffer, len - 1, 1, 0);
9654 SAFE_FREE ();
9656 UNGCPRO;
9660 /* Output a newline in the *Messages* buffer if "needs" one. */
9662 void
9663 message_log_maybe_newline (void)
9665 if (message_log_need_newline)
9666 message_dolog ("", 0, 1, 0);
9670 /* Add a string M of length NBYTES to the message log, optionally
9671 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9672 true, means interpret the contents of M as multibyte. This
9673 function calls low-level routines in order to bypass text property
9674 hooks, etc. which might not be safe to run.
9676 This may GC (insert may run before/after change hooks),
9677 so the buffer M must NOT point to a Lisp string. */
9679 void
9680 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9682 const unsigned char *msg = (const unsigned char *) m;
9684 if (!NILP (Vmemory_full))
9685 return;
9687 if (!NILP (Vmessage_log_max))
9689 struct buffer *oldbuf;
9690 Lisp_Object oldpoint, oldbegv, oldzv;
9691 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9692 ptrdiff_t point_at_end = 0;
9693 ptrdiff_t zv_at_end = 0;
9694 Lisp_Object old_deactivate_mark;
9695 struct gcpro gcpro1;
9697 old_deactivate_mark = Vdeactivate_mark;
9698 oldbuf = current_buffer;
9700 /* Ensure the Messages buffer exists, and switch to it.
9701 If we created it, set the major-mode. */
9703 int newbuffer = 0;
9704 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9706 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9708 if (newbuffer
9709 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9710 call0 (intern ("messages-buffer-mode"));
9713 bset_undo_list (current_buffer, Qt);
9714 bset_cache_long_scans (current_buffer, Qnil);
9716 oldpoint = message_dolog_marker1;
9717 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9718 oldbegv = message_dolog_marker2;
9719 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9720 oldzv = message_dolog_marker3;
9721 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9722 GCPRO1 (old_deactivate_mark);
9724 if (PT == Z)
9725 point_at_end = 1;
9726 if (ZV == Z)
9727 zv_at_end = 1;
9729 BEGV = BEG;
9730 BEGV_BYTE = BEG_BYTE;
9731 ZV = Z;
9732 ZV_BYTE = Z_BYTE;
9733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9735 /* Insert the string--maybe converting multibyte to single byte
9736 or vice versa, so that all the text fits the buffer. */
9737 if (multibyte
9738 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9740 ptrdiff_t i;
9741 int c, char_bytes;
9742 char work[1];
9744 /* Convert a multibyte string to single-byte
9745 for the *Message* buffer. */
9746 for (i = 0; i < nbytes; i += char_bytes)
9748 c = string_char_and_length (msg + i, &char_bytes);
9749 work[0] = (ASCII_CHAR_P (c)
9751 : multibyte_char_to_unibyte (c));
9752 insert_1_both (work, 1, 1, 1, 0, 0);
9755 else if (! multibyte
9756 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9758 ptrdiff_t i;
9759 int c, char_bytes;
9760 unsigned char str[MAX_MULTIBYTE_LENGTH];
9761 /* Convert a single-byte string to multibyte
9762 for the *Message* buffer. */
9763 for (i = 0; i < nbytes; i++)
9765 c = msg[i];
9766 MAKE_CHAR_MULTIBYTE (c);
9767 char_bytes = CHAR_STRING (c, str);
9768 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9771 else if (nbytes)
9772 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9774 if (nlflag)
9776 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9777 printmax_t dups;
9779 insert_1_both ("\n", 1, 1, 1, 0, 0);
9781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9782 this_bol = PT;
9783 this_bol_byte = PT_BYTE;
9785 /* See if this line duplicates the previous one.
9786 If so, combine duplicates. */
9787 if (this_bol > BEG)
9789 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9790 prev_bol = PT;
9791 prev_bol_byte = PT_BYTE;
9793 dups = message_log_check_duplicate (prev_bol_byte,
9794 this_bol_byte);
9795 if (dups)
9797 del_range_both (prev_bol, prev_bol_byte,
9798 this_bol, this_bol_byte, 0);
9799 if (dups > 1)
9801 char dupstr[sizeof " [ times]"
9802 + INT_STRLEN_BOUND (printmax_t)];
9804 /* If you change this format, don't forget to also
9805 change message_log_check_duplicate. */
9806 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9807 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9808 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9813 /* If we have more than the desired maximum number of lines
9814 in the *Messages* buffer now, delete the oldest ones.
9815 This is safe because we don't have undo in this buffer. */
9817 if (NATNUMP (Vmessage_log_max))
9819 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9820 -XFASTINT (Vmessage_log_max) - 1, 0);
9821 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9824 BEGV = marker_position (oldbegv);
9825 BEGV_BYTE = marker_byte_position (oldbegv);
9827 if (zv_at_end)
9829 ZV = Z;
9830 ZV_BYTE = Z_BYTE;
9832 else
9834 ZV = marker_position (oldzv);
9835 ZV_BYTE = marker_byte_position (oldzv);
9838 if (point_at_end)
9839 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9840 else
9841 /* We can't do Fgoto_char (oldpoint) because it will run some
9842 Lisp code. */
9843 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9844 marker_byte_position (oldpoint));
9846 UNGCPRO;
9847 unchain_marker (XMARKER (oldpoint));
9848 unchain_marker (XMARKER (oldbegv));
9849 unchain_marker (XMARKER (oldzv));
9851 /* We called insert_1_both above with its 5th argument (PREPARE)
9852 zero, which prevents insert_1_both from calling
9853 prepare_to_modify_buffer, which in turns prevents us from
9854 incrementing windows_or_buffers_changed even if *Messages* is
9855 shown in some window. So we must manually set
9856 windows_or_buffers_changed here to make up for that. */
9857 windows_or_buffers_changed = old_windows_or_buffers_changed;
9858 bset_redisplay (current_buffer);
9860 set_buffer_internal (oldbuf);
9862 message_log_need_newline = !nlflag;
9863 Vdeactivate_mark = old_deactivate_mark;
9868 /* We are at the end of the buffer after just having inserted a newline.
9869 (Note: We depend on the fact we won't be crossing the gap.)
9870 Check to see if the most recent message looks a lot like the previous one.
9871 Return 0 if different, 1 if the new one should just replace it, or a
9872 value N > 1 if we should also append " [N times]". */
9874 static intmax_t
9875 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9877 ptrdiff_t i;
9878 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9879 int seen_dots = 0;
9880 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9881 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9883 for (i = 0; i < len; i++)
9885 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9886 seen_dots = 1;
9887 if (p1[i] != p2[i])
9888 return seen_dots;
9890 p1 += len;
9891 if (*p1 == '\n')
9892 return 2;
9893 if (*p1++ == ' ' && *p1++ == '[')
9895 char *pend;
9896 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9897 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9898 return n + 1;
9900 return 0;
9904 /* Display an echo area message M with a specified length of NBYTES
9905 bytes. The string may include null characters. If M is not a
9906 string, clear out any existing message, and let the mini-buffer
9907 text show through.
9909 This function cancels echoing. */
9911 void
9912 message3 (Lisp_Object m)
9914 struct gcpro gcpro1;
9916 GCPRO1 (m);
9917 clear_message (true, true);
9918 cancel_echoing ();
9920 /* First flush out any partial line written with print. */
9921 message_log_maybe_newline ();
9922 if (STRINGP (m))
9924 ptrdiff_t nbytes = SBYTES (m);
9925 bool multibyte = STRING_MULTIBYTE (m);
9926 USE_SAFE_ALLOCA;
9927 char *buffer = SAFE_ALLOCA (nbytes);
9928 memcpy (buffer, SDATA (m), nbytes);
9929 message_dolog (buffer, nbytes, 1, multibyte);
9930 SAFE_FREE ();
9932 message3_nolog (m);
9934 UNGCPRO;
9938 /* The non-logging version of message3.
9939 This does not cancel echoing, because it is used for echoing.
9940 Perhaps we need to make a separate function for echoing
9941 and make this cancel echoing. */
9943 void
9944 message3_nolog (Lisp_Object m)
9946 struct frame *sf = SELECTED_FRAME ();
9948 if (FRAME_INITIAL_P (sf))
9950 if (noninteractive_need_newline)
9951 putc ('\n', stderr);
9952 noninteractive_need_newline = 0;
9953 if (STRINGP (m))
9955 Lisp_Object s = ENCODE_SYSTEM (m);
9957 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9959 if (cursor_in_echo_area == 0)
9960 fprintf (stderr, "\n");
9961 fflush (stderr);
9963 /* Error messages get reported properly by cmd_error, so this must be just an
9964 informative message; if the frame hasn't really been initialized yet, just
9965 toss it. */
9966 else if (INTERACTIVE && sf->glyphs_initialized_p)
9968 /* Get the frame containing the mini-buffer
9969 that the selected frame is using. */
9970 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9971 Lisp_Object frame = XWINDOW (mini_window)->frame;
9972 struct frame *f = XFRAME (frame);
9974 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9975 Fmake_frame_visible (frame);
9977 if (STRINGP (m) && SCHARS (m) > 0)
9979 set_message (m);
9980 if (minibuffer_auto_raise)
9981 Fraise_frame (frame);
9982 /* Assume we are not echoing.
9983 (If we are, echo_now will override this.) */
9984 echo_message_buffer = Qnil;
9986 else
9987 clear_message (true, true);
9989 do_pending_window_change (0);
9990 echo_area_display (1);
9991 do_pending_window_change (0);
9992 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9993 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9998 /* Display a null-terminated echo area message M. If M is 0, clear
9999 out any existing message, and let the mini-buffer text show through.
10001 The buffer M must continue to exist until after the echo area gets
10002 cleared or some other message gets displayed there. Do not pass
10003 text that is stored in a Lisp string. Do not pass text in a buffer
10004 that was alloca'd. */
10006 void
10007 message1 (const char *m)
10009 message3 (m ? build_unibyte_string (m) : Qnil);
10013 /* The non-logging counterpart of message1. */
10015 void
10016 message1_nolog (const char *m)
10018 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10021 /* Display a message M which contains a single %s
10022 which gets replaced with STRING. */
10024 void
10025 message_with_string (const char *m, Lisp_Object string, int log)
10027 CHECK_STRING (string);
10029 if (noninteractive)
10031 if (m)
10033 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10034 String whose data pointer might be passed to us in M. So
10035 we use a local copy. */
10036 char *fmt = xstrdup (m);
10038 if (noninteractive_need_newline)
10039 putc ('\n', stderr);
10040 noninteractive_need_newline = 0;
10041 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10042 if (!cursor_in_echo_area)
10043 fprintf (stderr, "\n");
10044 fflush (stderr);
10045 xfree (fmt);
10048 else if (INTERACTIVE)
10050 /* The frame whose minibuffer we're going to display the message on.
10051 It may be larger than the selected frame, so we need
10052 to use its buffer, not the selected frame's buffer. */
10053 Lisp_Object mini_window;
10054 struct frame *f, *sf = SELECTED_FRAME ();
10056 /* Get the frame containing the minibuffer
10057 that the selected frame is using. */
10058 mini_window = FRAME_MINIBUF_WINDOW (sf);
10059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10061 /* Error messages get reported properly by cmd_error, so this must be
10062 just an informative message; if the frame hasn't really been
10063 initialized yet, just toss it. */
10064 if (f->glyphs_initialized_p)
10066 Lisp_Object args[2], msg;
10067 struct gcpro gcpro1, gcpro2;
10069 args[0] = build_string (m);
10070 args[1] = msg = string;
10071 GCPRO2 (args[0], msg);
10072 gcpro1.nvars = 2;
10074 msg = Fformat (2, args);
10076 if (log)
10077 message3 (msg);
10078 else
10079 message3_nolog (msg);
10081 UNGCPRO;
10083 /* Print should start at the beginning of the message
10084 buffer next time. */
10085 message_buf_print = 0;
10091 /* Dump an informative message to the minibuf. If M is 0, clear out
10092 any existing message, and let the mini-buffer text show through. */
10094 static void
10095 vmessage (const char *m, va_list ap)
10097 if (noninteractive)
10099 if (m)
10101 if (noninteractive_need_newline)
10102 putc ('\n', stderr);
10103 noninteractive_need_newline = 0;
10104 vfprintf (stderr, m, ap);
10105 if (cursor_in_echo_area == 0)
10106 fprintf (stderr, "\n");
10107 fflush (stderr);
10110 else if (INTERACTIVE)
10112 /* The frame whose mini-buffer we're going to display the message
10113 on. It may be larger than the selected frame, so we need to
10114 use its buffer, not the selected frame's buffer. */
10115 Lisp_Object mini_window;
10116 struct frame *f, *sf = SELECTED_FRAME ();
10118 /* Get the frame containing the mini-buffer
10119 that the selected frame is using. */
10120 mini_window = FRAME_MINIBUF_WINDOW (sf);
10121 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10123 /* Error messages get reported properly by cmd_error, so this must be
10124 just an informative message; if the frame hasn't really been
10125 initialized yet, just toss it. */
10126 if (f->glyphs_initialized_p)
10128 if (m)
10130 ptrdiff_t len;
10131 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10132 char *message_buf = alloca (maxsize + 1);
10134 len = doprnt (message_buf, maxsize, m, 0, ap);
10136 message3 (make_string (message_buf, len));
10138 else
10139 message1 (0);
10141 /* Print should start at the beginning of the message
10142 buffer next time. */
10143 message_buf_print = 0;
10148 void
10149 message (const char *m, ...)
10151 va_list ap;
10152 va_start (ap, m);
10153 vmessage (m, ap);
10154 va_end (ap);
10158 #if 0
10159 /* The non-logging version of message. */
10161 void
10162 message_nolog (const char *m, ...)
10164 Lisp_Object old_log_max;
10165 va_list ap;
10166 va_start (ap, m);
10167 old_log_max = Vmessage_log_max;
10168 Vmessage_log_max = Qnil;
10169 vmessage (m, ap);
10170 Vmessage_log_max = old_log_max;
10171 va_end (ap);
10173 #endif
10176 /* Display the current message in the current mini-buffer. This is
10177 only called from error handlers in process.c, and is not time
10178 critical. */
10180 void
10181 update_echo_area (void)
10183 if (!NILP (echo_area_buffer[0]))
10185 Lisp_Object string;
10186 string = Fcurrent_message ();
10187 message3 (string);
10192 /* Make sure echo area buffers in `echo_buffers' are live.
10193 If they aren't, make new ones. */
10195 static void
10196 ensure_echo_area_buffers (void)
10198 int i;
10200 for (i = 0; i < 2; ++i)
10201 if (!BUFFERP (echo_buffer[i])
10202 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10204 char name[30];
10205 Lisp_Object old_buffer;
10206 int j;
10208 old_buffer = echo_buffer[i];
10209 echo_buffer[i] = Fget_buffer_create
10210 (make_formatted_string (name, " *Echo Area %d*", i));
10211 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10212 /* to force word wrap in echo area -
10213 it was decided to postpone this*/
10214 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10216 for (j = 0; j < 2; ++j)
10217 if (EQ (old_buffer, echo_area_buffer[j]))
10218 echo_area_buffer[j] = echo_buffer[i];
10223 /* Call FN with args A1..A2 with either the current or last displayed
10224 echo_area_buffer as current buffer.
10226 WHICH zero means use the current message buffer
10227 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10228 from echo_buffer[] and clear it.
10230 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10231 suitable buffer from echo_buffer[] and clear it.
10233 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10234 that the current message becomes the last displayed one, make
10235 choose a suitable buffer for echo_area_buffer[0], and clear it.
10237 Value is what FN returns. */
10239 static int
10240 with_echo_area_buffer (struct window *w, int which,
10241 int (*fn) (ptrdiff_t, Lisp_Object),
10242 ptrdiff_t a1, Lisp_Object a2)
10244 Lisp_Object buffer;
10245 int this_one, the_other, clear_buffer_p, rc;
10246 ptrdiff_t count = SPECPDL_INDEX ();
10248 /* If buffers aren't live, make new ones. */
10249 ensure_echo_area_buffers ();
10251 clear_buffer_p = 0;
10253 if (which == 0)
10254 this_one = 0, the_other = 1;
10255 else if (which > 0)
10256 this_one = 1, the_other = 0;
10257 else
10259 this_one = 0, the_other = 1;
10260 clear_buffer_p = true;
10262 /* We need a fresh one in case the current echo buffer equals
10263 the one containing the last displayed echo area message. */
10264 if (!NILP (echo_area_buffer[this_one])
10265 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10266 echo_area_buffer[this_one] = Qnil;
10269 /* Choose a suitable buffer from echo_buffer[] is we don't
10270 have one. */
10271 if (NILP (echo_area_buffer[this_one]))
10273 echo_area_buffer[this_one]
10274 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10275 ? echo_buffer[the_other]
10276 : echo_buffer[this_one]);
10277 clear_buffer_p = true;
10280 buffer = echo_area_buffer[this_one];
10282 /* Don't get confused by reusing the buffer used for echoing
10283 for a different purpose. */
10284 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10285 cancel_echoing ();
10287 record_unwind_protect (unwind_with_echo_area_buffer,
10288 with_echo_area_buffer_unwind_data (w));
10290 /* Make the echo area buffer current. Note that for display
10291 purposes, it is not necessary that the displayed window's buffer
10292 == current_buffer, except for text property lookup. So, let's
10293 only set that buffer temporarily here without doing a full
10294 Fset_window_buffer. We must also change w->pointm, though,
10295 because otherwise an assertions in unshow_buffer fails, and Emacs
10296 aborts. */
10297 set_buffer_internal_1 (XBUFFER (buffer));
10298 if (w)
10300 wset_buffer (w, buffer);
10301 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10304 bset_undo_list (current_buffer, Qt);
10305 bset_read_only (current_buffer, Qnil);
10306 specbind (Qinhibit_read_only, Qt);
10307 specbind (Qinhibit_modification_hooks, Qt);
10309 if (clear_buffer_p && Z > BEG)
10310 del_range (BEG, Z);
10312 eassert (BEGV >= BEG);
10313 eassert (ZV <= Z && ZV >= BEGV);
10315 rc = fn (a1, a2);
10317 eassert (BEGV >= BEG);
10318 eassert (ZV <= Z && ZV >= BEGV);
10320 unbind_to (count, Qnil);
10321 return rc;
10325 /* Save state that should be preserved around the call to the function
10326 FN called in with_echo_area_buffer. */
10328 static Lisp_Object
10329 with_echo_area_buffer_unwind_data (struct window *w)
10331 int i = 0;
10332 Lisp_Object vector, tmp;
10334 /* Reduce consing by keeping one vector in
10335 Vwith_echo_area_save_vector. */
10336 vector = Vwith_echo_area_save_vector;
10337 Vwith_echo_area_save_vector = Qnil;
10339 if (NILP (vector))
10340 vector = Fmake_vector (make_number (9), Qnil);
10342 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10343 ASET (vector, i, Vdeactivate_mark); ++i;
10344 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10346 if (w)
10348 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10349 ASET (vector, i, w->contents); ++i;
10350 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10351 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10352 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10353 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10355 else
10357 int end = i + 6;
10358 for (; i < end; ++i)
10359 ASET (vector, i, Qnil);
10362 eassert (i == ASIZE (vector));
10363 return vector;
10367 /* Restore global state from VECTOR which was created by
10368 with_echo_area_buffer_unwind_data. */
10370 static void
10371 unwind_with_echo_area_buffer (Lisp_Object vector)
10373 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10374 Vdeactivate_mark = AREF (vector, 1);
10375 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10377 if (WINDOWP (AREF (vector, 3)))
10379 struct window *w;
10380 Lisp_Object buffer;
10382 w = XWINDOW (AREF (vector, 3));
10383 buffer = AREF (vector, 4);
10385 wset_buffer (w, buffer);
10386 set_marker_both (w->pointm, buffer,
10387 XFASTINT (AREF (vector, 5)),
10388 XFASTINT (AREF (vector, 6)));
10389 set_marker_both (w->start, buffer,
10390 XFASTINT (AREF (vector, 7)),
10391 XFASTINT (AREF (vector, 8)));
10394 Vwith_echo_area_save_vector = vector;
10398 /* Set up the echo area for use by print functions. MULTIBYTE_P
10399 non-zero means we will print multibyte. */
10401 void
10402 setup_echo_area_for_printing (int multibyte_p)
10404 /* If we can't find an echo area any more, exit. */
10405 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10406 Fkill_emacs (Qnil);
10408 ensure_echo_area_buffers ();
10410 if (!message_buf_print)
10412 /* A message has been output since the last time we printed.
10413 Choose a fresh echo area buffer. */
10414 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10415 echo_area_buffer[0] = echo_buffer[1];
10416 else
10417 echo_area_buffer[0] = echo_buffer[0];
10419 /* Switch to that buffer and clear it. */
10420 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10421 bset_truncate_lines (current_buffer, Qnil);
10423 if (Z > BEG)
10425 ptrdiff_t count = SPECPDL_INDEX ();
10426 specbind (Qinhibit_read_only, Qt);
10427 /* Note that undo recording is always disabled. */
10428 del_range (BEG, Z);
10429 unbind_to (count, Qnil);
10431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10433 /* Set up the buffer for the multibyteness we need. */
10434 if (multibyte_p
10435 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10438 /* Raise the frame containing the echo area. */
10439 if (minibuffer_auto_raise)
10441 struct frame *sf = SELECTED_FRAME ();
10442 Lisp_Object mini_window;
10443 mini_window = FRAME_MINIBUF_WINDOW (sf);
10444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10447 message_log_maybe_newline ();
10448 message_buf_print = 1;
10450 else
10452 if (NILP (echo_area_buffer[0]))
10454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10455 echo_area_buffer[0] = echo_buffer[1];
10456 else
10457 echo_area_buffer[0] = echo_buffer[0];
10460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10462 /* Someone switched buffers between print requests. */
10463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10464 bset_truncate_lines (current_buffer, Qnil);
10470 /* Display an echo area message in window W. Value is non-zero if W's
10471 height is changed. If display_last_displayed_message_p is
10472 non-zero, display the message that was last displayed, otherwise
10473 display the current message. */
10475 static int
10476 display_echo_area (struct window *w)
10478 int i, no_message_p, window_height_changed_p;
10480 /* Temporarily disable garbage collections while displaying the echo
10481 area. This is done because a GC can print a message itself.
10482 That message would modify the echo area buffer's contents while a
10483 redisplay of the buffer is going on, and seriously confuse
10484 redisplay. */
10485 ptrdiff_t count = inhibit_garbage_collection ();
10487 /* If there is no message, we must call display_echo_area_1
10488 nevertheless because it resizes the window. But we will have to
10489 reset the echo_area_buffer in question to nil at the end because
10490 with_echo_area_buffer will sets it to an empty buffer. */
10491 i = display_last_displayed_message_p ? 1 : 0;
10492 no_message_p = NILP (echo_area_buffer[i]);
10494 window_height_changed_p
10495 = with_echo_area_buffer (w, display_last_displayed_message_p,
10496 display_echo_area_1,
10497 (intptr_t) w, Qnil);
10499 if (no_message_p)
10500 echo_area_buffer[i] = Qnil;
10502 unbind_to (count, Qnil);
10503 return window_height_changed_p;
10507 /* Helper for display_echo_area. Display the current buffer which
10508 contains the current echo area message in window W, a mini-window,
10509 a pointer to which is passed in A1. A2..A4 are currently not used.
10510 Change the height of W so that all of the message is displayed.
10511 Value is non-zero if height of W was changed. */
10513 static int
10514 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10516 intptr_t i1 = a1;
10517 struct window *w = (struct window *) i1;
10518 Lisp_Object window;
10519 struct text_pos start;
10520 int window_height_changed_p = 0;
10522 /* Do this before displaying, so that we have a large enough glyph
10523 matrix for the display. If we can't get enough space for the
10524 whole text, display the last N lines. That works by setting w->start. */
10525 window_height_changed_p = resize_mini_window (w, 0);
10527 /* Use the starting position chosen by resize_mini_window. */
10528 SET_TEXT_POS_FROM_MARKER (start, w->start);
10530 /* Display. */
10531 clear_glyph_matrix (w->desired_matrix);
10532 XSETWINDOW (window, w);
10533 try_window (window, start, 0);
10535 return window_height_changed_p;
10539 /* Resize the echo area window to exactly the size needed for the
10540 currently displayed message, if there is one. If a mini-buffer
10541 is active, don't shrink it. */
10543 void
10544 resize_echo_area_exactly (void)
10546 if (BUFFERP (echo_area_buffer[0])
10547 && WINDOWP (echo_area_window))
10549 struct window *w = XWINDOW (echo_area_window);
10550 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10551 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10552 (intptr_t) w, resize_exactly);
10553 if (resized_p)
10555 windows_or_buffers_changed = 42;
10556 update_mode_lines = 30;
10557 redisplay_internal ();
10563 /* Callback function for with_echo_area_buffer, when used from
10564 resize_echo_area_exactly. A1 contains a pointer to the window to
10565 resize, EXACTLY non-nil means resize the mini-window exactly to the
10566 size of the text displayed. A3 and A4 are not used. Value is what
10567 resize_mini_window returns. */
10569 static int
10570 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10572 intptr_t i1 = a1;
10573 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10577 /* Resize mini-window W to fit the size of its contents. EXACT_P
10578 means size the window exactly to the size needed. Otherwise, it's
10579 only enlarged until W's buffer is empty.
10581 Set W->start to the right place to begin display. If the whole
10582 contents fit, start at the beginning. Otherwise, start so as
10583 to make the end of the contents appear. This is particularly
10584 important for y-or-n-p, but seems desirable generally.
10586 Value is non-zero if the window height has been changed. */
10589 resize_mini_window (struct window *w, int exact_p)
10591 struct frame *f = XFRAME (w->frame);
10592 int window_height_changed_p = 0;
10594 eassert (MINI_WINDOW_P (w));
10596 /* By default, start display at the beginning. */
10597 set_marker_both (w->start, w->contents,
10598 BUF_BEGV (XBUFFER (w->contents)),
10599 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10601 /* Don't resize windows while redisplaying a window; it would
10602 confuse redisplay functions when the size of the window they are
10603 displaying changes from under them. Such a resizing can happen,
10604 for instance, when which-func prints a long message while
10605 we are running fontification-functions. We're running these
10606 functions with safe_call which binds inhibit-redisplay to t. */
10607 if (!NILP (Vinhibit_redisplay))
10608 return 0;
10610 /* Nil means don't try to resize. */
10611 if (NILP (Vresize_mini_windows)
10612 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10613 return 0;
10615 if (!FRAME_MINIBUF_ONLY_P (f))
10617 struct it it;
10618 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10619 + WINDOW_PIXEL_HEIGHT (w));
10620 int unit = FRAME_LINE_HEIGHT (f);
10621 int height, max_height;
10622 struct text_pos start;
10623 struct buffer *old_current_buffer = NULL;
10625 if (current_buffer != XBUFFER (w->contents))
10627 old_current_buffer = current_buffer;
10628 set_buffer_internal (XBUFFER (w->contents));
10631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10633 /* Compute the max. number of lines specified by the user. */
10634 if (FLOATP (Vmax_mini_window_height))
10635 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10636 else if (INTEGERP (Vmax_mini_window_height))
10637 max_height = XINT (Vmax_mini_window_height) * unit;
10638 else
10639 max_height = total_height / 4;
10641 /* Correct that max. height if it's bogus. */
10642 max_height = clip_to_bounds (unit, max_height, total_height);
10644 /* Find out the height of the text in the window. */
10645 if (it.line_wrap == TRUNCATE)
10646 height = unit;
10647 else
10649 last_height = 0;
10650 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10651 if (it.max_ascent == 0 && it.max_descent == 0)
10652 height = it.current_y + last_height;
10653 else
10654 height = it.current_y + it.max_ascent + it.max_descent;
10655 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10658 /* Compute a suitable window start. */
10659 if (height > max_height)
10661 height = max_height;
10662 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10663 move_it_vertically_backward (&it, height);
10664 start = it.current.pos;
10666 else
10667 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10668 SET_MARKER_FROM_TEXT_POS (w->start, start);
10670 if (EQ (Vresize_mini_windows, Qgrow_only))
10672 /* Let it grow only, until we display an empty message, in which
10673 case the window shrinks again. */
10674 if (height > WINDOW_PIXEL_HEIGHT (w))
10676 int old_height = WINDOW_PIXEL_HEIGHT (w);
10678 FRAME_WINDOWS_FROZEN (f) = 1;
10679 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10680 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10682 else if (height < WINDOW_PIXEL_HEIGHT (w)
10683 && (exact_p || BEGV == ZV))
10685 int old_height = WINDOW_PIXEL_HEIGHT (w);
10687 FRAME_WINDOWS_FROZEN (f) = 0;
10688 shrink_mini_window (w, 1);
10689 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10692 else
10694 /* Always resize to exact size needed. */
10695 if (height > WINDOW_PIXEL_HEIGHT (w))
10697 int old_height = WINDOW_PIXEL_HEIGHT (w);
10699 FRAME_WINDOWS_FROZEN (f) = 1;
10700 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10701 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10703 else if (height < WINDOW_PIXEL_HEIGHT (w))
10705 int old_height = WINDOW_PIXEL_HEIGHT (w);
10707 FRAME_WINDOWS_FROZEN (f) = 0;
10708 shrink_mini_window (w, 1);
10710 if (height)
10712 FRAME_WINDOWS_FROZEN (f) = 1;
10713 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10716 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10720 if (old_current_buffer)
10721 set_buffer_internal (old_current_buffer);
10724 return window_height_changed_p;
10728 /* Value is the current message, a string, or nil if there is no
10729 current message. */
10731 Lisp_Object
10732 current_message (void)
10734 Lisp_Object msg;
10736 if (!BUFFERP (echo_area_buffer[0]))
10737 msg = Qnil;
10738 else
10740 with_echo_area_buffer (0, 0, current_message_1,
10741 (intptr_t) &msg, Qnil);
10742 if (NILP (msg))
10743 echo_area_buffer[0] = Qnil;
10746 return msg;
10750 static int
10751 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10753 intptr_t i1 = a1;
10754 Lisp_Object *msg = (Lisp_Object *) i1;
10756 if (Z > BEG)
10757 *msg = make_buffer_string (BEG, Z, 1);
10758 else
10759 *msg = Qnil;
10760 return 0;
10764 /* Push the current message on Vmessage_stack for later restoration
10765 by restore_message. Value is non-zero if the current message isn't
10766 empty. This is a relatively infrequent operation, so it's not
10767 worth optimizing. */
10769 bool
10770 push_message (void)
10772 Lisp_Object msg = current_message ();
10773 Vmessage_stack = Fcons (msg, Vmessage_stack);
10774 return STRINGP (msg);
10778 /* Restore message display from the top of Vmessage_stack. */
10780 void
10781 restore_message (void)
10783 eassert (CONSP (Vmessage_stack));
10784 message3_nolog (XCAR (Vmessage_stack));
10788 /* Handler for unwind-protect calling pop_message. */
10790 void
10791 pop_message_unwind (void)
10793 /* Pop the top-most entry off Vmessage_stack. */
10794 eassert (CONSP (Vmessage_stack));
10795 Vmessage_stack = XCDR (Vmessage_stack);
10799 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10800 exits. If the stack is not empty, we have a missing pop_message
10801 somewhere. */
10803 void
10804 check_message_stack (void)
10806 if (!NILP (Vmessage_stack))
10807 emacs_abort ();
10811 /* Truncate to NCHARS what will be displayed in the echo area the next
10812 time we display it---but don't redisplay it now. */
10814 void
10815 truncate_echo_area (ptrdiff_t nchars)
10817 if (nchars == 0)
10818 echo_area_buffer[0] = Qnil;
10819 else if (!noninteractive
10820 && INTERACTIVE
10821 && !NILP (echo_area_buffer[0]))
10823 struct frame *sf = SELECTED_FRAME ();
10824 /* Error messages get reported properly by cmd_error, so this must be
10825 just an informative message; if the frame hasn't really been
10826 initialized yet, just toss it. */
10827 if (sf->glyphs_initialized_p)
10828 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10833 /* Helper function for truncate_echo_area. Truncate the current
10834 message to at most NCHARS characters. */
10836 static int
10837 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10839 if (BEG + nchars < Z)
10840 del_range (BEG + nchars, Z);
10841 if (Z == BEG)
10842 echo_area_buffer[0] = Qnil;
10843 return 0;
10846 /* Set the current message to STRING. */
10848 static void
10849 set_message (Lisp_Object string)
10851 eassert (STRINGP (string));
10853 message_enable_multibyte = STRING_MULTIBYTE (string);
10855 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10856 message_buf_print = 0;
10857 help_echo_showing_p = 0;
10859 if (STRINGP (Vdebug_on_message)
10860 && STRINGP (string)
10861 && fast_string_match (Vdebug_on_message, string) >= 0)
10862 call_debugger (list2 (Qerror, string));
10866 /* Helper function for set_message. First argument is ignored and second
10867 argument has the same meaning as for set_message.
10868 This function is called with the echo area buffer being current. */
10870 static int
10871 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10873 eassert (STRINGP (string));
10875 /* Change multibyteness of the echo buffer appropriately. */
10876 if (message_enable_multibyte
10877 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10878 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10880 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10881 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10882 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10884 /* Insert new message at BEG. */
10885 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10887 /* This function takes care of single/multibyte conversion.
10888 We just have to ensure that the echo area buffer has the right
10889 setting of enable_multibyte_characters. */
10890 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10892 return 0;
10896 /* Clear messages. CURRENT_P non-zero means clear the current
10897 message. LAST_DISPLAYED_P non-zero means clear the message
10898 last displayed. */
10900 void
10901 clear_message (bool current_p, bool last_displayed_p)
10903 if (current_p)
10905 echo_area_buffer[0] = Qnil;
10906 message_cleared_p = true;
10909 if (last_displayed_p)
10910 echo_area_buffer[1] = Qnil;
10912 message_buf_print = 0;
10915 /* Clear garbaged frames.
10917 This function is used where the old redisplay called
10918 redraw_garbaged_frames which in turn called redraw_frame which in
10919 turn called clear_frame. The call to clear_frame was a source of
10920 flickering. I believe a clear_frame is not necessary. It should
10921 suffice in the new redisplay to invalidate all current matrices,
10922 and ensure a complete redisplay of all windows. */
10924 static void
10925 clear_garbaged_frames (void)
10927 if (frame_garbaged)
10929 Lisp_Object tail, frame;
10931 FOR_EACH_FRAME (tail, frame)
10933 struct frame *f = XFRAME (frame);
10935 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10937 if (f->resized_p)
10938 redraw_frame (f);
10939 else
10940 clear_current_matrices (f);
10941 fset_redisplay (f);
10942 f->garbaged = false;
10943 f->resized_p = false;
10947 frame_garbaged = false;
10952 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10953 is non-zero update selected_frame. Value is non-zero if the
10954 mini-windows height has been changed. */
10956 static int
10957 echo_area_display (int update_frame_p)
10959 Lisp_Object mini_window;
10960 struct window *w;
10961 struct frame *f;
10962 int window_height_changed_p = 0;
10963 struct frame *sf = SELECTED_FRAME ();
10965 mini_window = FRAME_MINIBUF_WINDOW (sf);
10966 w = XWINDOW (mini_window);
10967 f = XFRAME (WINDOW_FRAME (w));
10969 /* Don't display if frame is invisible or not yet initialized. */
10970 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10971 return 0;
10973 #ifdef HAVE_WINDOW_SYSTEM
10974 /* When Emacs starts, selected_frame may be the initial terminal
10975 frame. If we let this through, a message would be displayed on
10976 the terminal. */
10977 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10978 return 0;
10979 #endif /* HAVE_WINDOW_SYSTEM */
10981 /* Redraw garbaged frames. */
10982 clear_garbaged_frames ();
10984 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10986 echo_area_window = mini_window;
10987 window_height_changed_p = display_echo_area (w);
10988 w->must_be_updated_p = true;
10990 /* Update the display, unless called from redisplay_internal.
10991 Also don't update the screen during redisplay itself. The
10992 update will happen at the end of redisplay, and an update
10993 here could cause confusion. */
10994 if (update_frame_p && !redisplaying_p)
10996 int n = 0;
10998 /* If the display update has been interrupted by pending
10999 input, update mode lines in the frame. Due to the
11000 pending input, it might have been that redisplay hasn't
11001 been called, so that mode lines above the echo area are
11002 garbaged. This looks odd, so we prevent it here. */
11003 if (!display_completed)
11004 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11006 if (window_height_changed_p
11007 /* Don't do this if Emacs is shutting down. Redisplay
11008 needs to run hooks. */
11009 && !NILP (Vrun_hooks))
11011 /* Must update other windows. Likewise as in other
11012 cases, don't let this update be interrupted by
11013 pending input. */
11014 ptrdiff_t count = SPECPDL_INDEX ();
11015 specbind (Qredisplay_dont_pause, Qt);
11016 windows_or_buffers_changed = 44;
11017 redisplay_internal ();
11018 unbind_to (count, Qnil);
11020 else if (FRAME_WINDOW_P (f) && n == 0)
11022 /* Window configuration is the same as before.
11023 Can do with a display update of the echo area,
11024 unless we displayed some mode lines. */
11025 update_single_window (w, 1);
11026 flush_frame (f);
11028 else
11029 update_frame (f, 1, 1);
11031 /* If cursor is in the echo area, make sure that the next
11032 redisplay displays the minibuffer, so that the cursor will
11033 be replaced with what the minibuffer wants. */
11034 if (cursor_in_echo_area)
11035 wset_redisplay (XWINDOW (mini_window));
11038 else if (!EQ (mini_window, selected_window))
11039 wset_redisplay (XWINDOW (mini_window));
11041 /* Last displayed message is now the current message. */
11042 echo_area_buffer[1] = echo_area_buffer[0];
11043 /* Inform read_char that we're not echoing. */
11044 echo_message_buffer = Qnil;
11046 /* Prevent redisplay optimization in redisplay_internal by resetting
11047 this_line_start_pos. This is done because the mini-buffer now
11048 displays the message instead of its buffer text. */
11049 if (EQ (mini_window, selected_window))
11050 CHARPOS (this_line_start_pos) = 0;
11052 return window_height_changed_p;
11055 /* Nonzero if W's buffer was changed but not saved. */
11057 static int
11058 window_buffer_changed (struct window *w)
11060 struct buffer *b = XBUFFER (w->contents);
11062 eassert (BUFFER_LIVE_P (b));
11064 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11067 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11069 static int
11070 mode_line_update_needed (struct window *w)
11072 return (w->column_number_displayed != -1
11073 && !(PT == w->last_point && !window_outdated (w))
11074 && (w->column_number_displayed != current_column ()));
11077 /* Nonzero if window start of W is frozen and may not be changed during
11078 redisplay. */
11080 static bool
11081 window_frozen_p (struct window *w)
11083 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11085 Lisp_Object window;
11087 XSETWINDOW (window, w);
11088 if (MINI_WINDOW_P (w))
11089 return 0;
11090 else if (EQ (window, selected_window))
11091 return 0;
11092 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11093 && EQ (window, Vminibuf_scroll_window))
11094 /* This special window can't be frozen too. */
11095 return 0;
11096 else
11097 return 1;
11099 return 0;
11102 /***********************************************************************
11103 Mode Lines and Frame Titles
11104 ***********************************************************************/
11106 /* A buffer for constructing non-propertized mode-line strings and
11107 frame titles in it; allocated from the heap in init_xdisp and
11108 resized as needed in store_mode_line_noprop_char. */
11110 static char *mode_line_noprop_buf;
11112 /* The buffer's end, and a current output position in it. */
11114 static char *mode_line_noprop_buf_end;
11115 static char *mode_line_noprop_ptr;
11117 #define MODE_LINE_NOPROP_LEN(start) \
11118 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11120 static enum {
11121 MODE_LINE_DISPLAY = 0,
11122 MODE_LINE_TITLE,
11123 MODE_LINE_NOPROP,
11124 MODE_LINE_STRING
11125 } mode_line_target;
11127 /* Alist that caches the results of :propertize.
11128 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11129 static Lisp_Object mode_line_proptrans_alist;
11131 /* List of strings making up the mode-line. */
11132 static Lisp_Object mode_line_string_list;
11134 /* Base face property when building propertized mode line string. */
11135 static Lisp_Object mode_line_string_face;
11136 static Lisp_Object mode_line_string_face_prop;
11139 /* Unwind data for mode line strings */
11141 static Lisp_Object Vmode_line_unwind_vector;
11143 static Lisp_Object
11144 format_mode_line_unwind_data (struct frame *target_frame,
11145 struct buffer *obuf,
11146 Lisp_Object owin,
11147 int save_proptrans)
11149 Lisp_Object vector, tmp;
11151 /* Reduce consing by keeping one vector in
11152 Vwith_echo_area_save_vector. */
11153 vector = Vmode_line_unwind_vector;
11154 Vmode_line_unwind_vector = Qnil;
11156 if (NILP (vector))
11157 vector = Fmake_vector (make_number (10), Qnil);
11159 ASET (vector, 0, make_number (mode_line_target));
11160 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11161 ASET (vector, 2, mode_line_string_list);
11162 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11163 ASET (vector, 4, mode_line_string_face);
11164 ASET (vector, 5, mode_line_string_face_prop);
11166 if (obuf)
11167 XSETBUFFER (tmp, obuf);
11168 else
11169 tmp = Qnil;
11170 ASET (vector, 6, tmp);
11171 ASET (vector, 7, owin);
11172 if (target_frame)
11174 /* Similarly to `with-selected-window', if the operation selects
11175 a window on another frame, we must restore that frame's
11176 selected window, and (for a tty) the top-frame. */
11177 ASET (vector, 8, target_frame->selected_window);
11178 if (FRAME_TERMCAP_P (target_frame))
11179 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11182 return vector;
11185 static void
11186 unwind_format_mode_line (Lisp_Object vector)
11188 Lisp_Object old_window = AREF (vector, 7);
11189 Lisp_Object target_frame_window = AREF (vector, 8);
11190 Lisp_Object old_top_frame = AREF (vector, 9);
11192 mode_line_target = XINT (AREF (vector, 0));
11193 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11194 mode_line_string_list = AREF (vector, 2);
11195 if (! EQ (AREF (vector, 3), Qt))
11196 mode_line_proptrans_alist = AREF (vector, 3);
11197 mode_line_string_face = AREF (vector, 4);
11198 mode_line_string_face_prop = AREF (vector, 5);
11200 /* Select window before buffer, since it may change the buffer. */
11201 if (!NILP (old_window))
11203 /* If the operation that we are unwinding had selected a window
11204 on a different frame, reset its frame-selected-window. For a
11205 text terminal, reset its top-frame if necessary. */
11206 if (!NILP (target_frame_window))
11208 Lisp_Object frame
11209 = WINDOW_FRAME (XWINDOW (target_frame_window));
11211 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11212 Fselect_window (target_frame_window, Qt);
11214 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11215 Fselect_frame (old_top_frame, Qt);
11218 Fselect_window (old_window, Qt);
11221 if (!NILP (AREF (vector, 6)))
11223 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11224 ASET (vector, 6, Qnil);
11227 Vmode_line_unwind_vector = vector;
11231 /* Store a single character C for the frame title in mode_line_noprop_buf.
11232 Re-allocate mode_line_noprop_buf if necessary. */
11234 static void
11235 store_mode_line_noprop_char (char c)
11237 /* If output position has reached the end of the allocated buffer,
11238 increase the buffer's size. */
11239 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11241 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11242 ptrdiff_t size = len;
11243 mode_line_noprop_buf =
11244 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11245 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11246 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11249 *mode_line_noprop_ptr++ = c;
11253 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11254 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11255 characters that yield more columns than PRECISION; PRECISION <= 0
11256 means copy the whole string. Pad with spaces until FIELD_WIDTH
11257 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11258 pad. Called from display_mode_element when it is used to build a
11259 frame title. */
11261 static int
11262 store_mode_line_noprop (const char *string, int field_width, int precision)
11264 const unsigned char *str = (const unsigned char *) string;
11265 int n = 0;
11266 ptrdiff_t dummy, nbytes;
11268 /* Copy at most PRECISION chars from STR. */
11269 nbytes = strlen (string);
11270 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11271 while (nbytes--)
11272 store_mode_line_noprop_char (*str++);
11274 /* Fill up with spaces until FIELD_WIDTH reached. */
11275 while (field_width > 0
11276 && n < field_width)
11278 store_mode_line_noprop_char (' ');
11279 ++n;
11282 return n;
11285 /***********************************************************************
11286 Frame Titles
11287 ***********************************************************************/
11289 #ifdef HAVE_WINDOW_SYSTEM
11291 /* Set the title of FRAME, if it has changed. The title format is
11292 Vicon_title_format if FRAME is iconified, otherwise it is
11293 frame_title_format. */
11295 static void
11296 x_consider_frame_title (Lisp_Object frame)
11298 struct frame *f = XFRAME (frame);
11300 if (FRAME_WINDOW_P (f)
11301 || FRAME_MINIBUF_ONLY_P (f)
11302 || f->explicit_name)
11304 /* Do we have more than one visible frame on this X display? */
11305 Lisp_Object tail, other_frame, fmt;
11306 ptrdiff_t title_start;
11307 char *title;
11308 ptrdiff_t len;
11309 struct it it;
11310 ptrdiff_t count = SPECPDL_INDEX ();
11312 FOR_EACH_FRAME (tail, other_frame)
11314 struct frame *tf = XFRAME (other_frame);
11316 if (tf != f
11317 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11318 && !FRAME_MINIBUF_ONLY_P (tf)
11319 && !EQ (other_frame, tip_frame)
11320 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11321 break;
11324 /* Set global variable indicating that multiple frames exist. */
11325 multiple_frames = CONSP (tail);
11327 /* Switch to the buffer of selected window of the frame. Set up
11328 mode_line_target so that display_mode_element will output into
11329 mode_line_noprop_buf; then display the title. */
11330 record_unwind_protect (unwind_format_mode_line,
11331 format_mode_line_unwind_data
11332 (f, current_buffer, selected_window, 0));
11334 Fselect_window (f->selected_window, Qt);
11335 set_buffer_internal_1
11336 (XBUFFER (XWINDOW (f->selected_window)->contents));
11337 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11339 mode_line_target = MODE_LINE_TITLE;
11340 title_start = MODE_LINE_NOPROP_LEN (0);
11341 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11342 NULL, DEFAULT_FACE_ID);
11343 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11344 len = MODE_LINE_NOPROP_LEN (title_start);
11345 title = mode_line_noprop_buf + title_start;
11346 unbind_to (count, Qnil);
11348 /* Set the title only if it's changed. This avoids consing in
11349 the common case where it hasn't. (If it turns out that we've
11350 already wasted too much time by walking through the list with
11351 display_mode_element, then we might need to optimize at a
11352 higher level than this.) */
11353 if (! STRINGP (f->name)
11354 || SBYTES (f->name) != len
11355 || memcmp (title, SDATA (f->name), len) != 0)
11356 x_implicitly_set_name (f, make_string (title, len), Qnil);
11360 #endif /* not HAVE_WINDOW_SYSTEM */
11363 /***********************************************************************
11364 Menu Bars
11365 ***********************************************************************/
11367 /* Non-zero if we will not redisplay all visible windows. */
11368 #define REDISPLAY_SOME_P() \
11369 ((windows_or_buffers_changed == 0 \
11370 || windows_or_buffers_changed == REDISPLAY_SOME) \
11371 && (update_mode_lines == 0 \
11372 || update_mode_lines == REDISPLAY_SOME))
11374 /* Prepare for redisplay by updating menu-bar item lists when
11375 appropriate. This can call eval. */
11377 static void
11378 prepare_menu_bars (void)
11380 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11381 bool some_windows = REDISPLAY_SOME_P ();
11382 struct gcpro gcpro1, gcpro2;
11383 Lisp_Object tooltip_frame;
11385 #ifdef HAVE_WINDOW_SYSTEM
11386 tooltip_frame = tip_frame;
11387 #else
11388 tooltip_frame = Qnil;
11389 #endif
11391 if (FUNCTIONP (Vpre_redisplay_function))
11393 Lisp_Object windows = all_windows ? Qt : Qnil;
11394 if (all_windows && some_windows)
11396 Lisp_Object ws = window_list ();
11397 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11399 Lisp_Object this = XCAR (ws);
11400 struct window *w = XWINDOW (this);
11401 if (w->redisplay
11402 || XFRAME (w->frame)->redisplay
11403 || XBUFFER (w->contents)->text->redisplay)
11405 windows = Fcons (this, windows);
11409 safe_call1 (Vpre_redisplay_function, windows);
11412 /* Update all frame titles based on their buffer names, etc. We do
11413 this before the menu bars so that the buffer-menu will show the
11414 up-to-date frame titles. */
11415 #ifdef HAVE_WINDOW_SYSTEM
11416 if (all_windows)
11418 Lisp_Object tail, frame;
11420 FOR_EACH_FRAME (tail, frame)
11422 struct frame *f = XFRAME (frame);
11423 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11424 if (some_windows
11425 && !f->redisplay
11426 && !w->redisplay
11427 && !XBUFFER (w->contents)->text->redisplay)
11428 continue;
11430 if (!EQ (frame, tooltip_frame)
11431 && (FRAME_ICONIFIED_P (f)
11432 || FRAME_VISIBLE_P (f) == 1
11433 /* Exclude TTY frames that are obscured because they
11434 are not the top frame on their console. This is
11435 because x_consider_frame_title actually switches
11436 to the frame, which for TTY frames means it is
11437 marked as garbaged, and will be completely
11438 redrawn on the next redisplay cycle. This causes
11439 TTY frames to be completely redrawn, when there
11440 are more than one of them, even though nothing
11441 should be changed on display. */
11442 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11443 x_consider_frame_title (frame);
11446 #endif /* HAVE_WINDOW_SYSTEM */
11448 /* Update the menu bar item lists, if appropriate. This has to be
11449 done before any actual redisplay or generation of display lines. */
11451 if (all_windows)
11453 Lisp_Object tail, frame;
11454 ptrdiff_t count = SPECPDL_INDEX ();
11455 /* 1 means that update_menu_bar has run its hooks
11456 so any further calls to update_menu_bar shouldn't do so again. */
11457 int menu_bar_hooks_run = 0;
11459 record_unwind_save_match_data ();
11461 FOR_EACH_FRAME (tail, frame)
11463 struct frame *f = XFRAME (frame);
11464 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11466 /* Ignore tooltip frame. */
11467 if (EQ (frame, tooltip_frame))
11468 continue;
11470 if (some_windows
11471 && !f->redisplay
11472 && !w->redisplay
11473 && !XBUFFER (w->contents)->text->redisplay)
11474 continue;
11476 /* If a window on this frame changed size, report that to
11477 the user and clear the size-change flag. */
11478 if (FRAME_WINDOW_SIZES_CHANGED (f))
11480 Lisp_Object functions;
11482 /* Clear flag first in case we get an error below. */
11483 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11484 functions = Vwindow_size_change_functions;
11485 GCPRO2 (tail, functions);
11487 while (CONSP (functions))
11489 if (!EQ (XCAR (functions), Qt))
11490 call1 (XCAR (functions), frame);
11491 functions = XCDR (functions);
11493 UNGCPRO;
11496 GCPRO1 (tail);
11497 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11498 #ifdef HAVE_WINDOW_SYSTEM
11499 update_tool_bar (f, 0);
11500 #endif
11501 #ifdef HAVE_NS
11502 if (windows_or_buffers_changed
11503 && FRAME_NS_P (f))
11504 ns_set_doc_edited
11505 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11506 #endif
11507 UNGCPRO;
11510 unbind_to (count, Qnil);
11512 else
11514 struct frame *sf = SELECTED_FRAME ();
11515 update_menu_bar (sf, 1, 0);
11516 #ifdef HAVE_WINDOW_SYSTEM
11517 update_tool_bar (sf, 1);
11518 #endif
11523 /* Update the menu bar item list for frame F. This has to be done
11524 before we start to fill in any display lines, because it can call
11525 eval.
11527 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11529 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11530 already ran the menu bar hooks for this redisplay, so there
11531 is no need to run them again. The return value is the
11532 updated value of this flag, to pass to the next call. */
11534 static int
11535 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11537 Lisp_Object window;
11538 register struct window *w;
11540 /* If called recursively during a menu update, do nothing. This can
11541 happen when, for instance, an activate-menubar-hook causes a
11542 redisplay. */
11543 if (inhibit_menubar_update)
11544 return hooks_run;
11546 window = FRAME_SELECTED_WINDOW (f);
11547 w = XWINDOW (window);
11549 if (FRAME_WINDOW_P (f)
11551 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11552 || defined (HAVE_NS) || defined (USE_GTK)
11553 FRAME_EXTERNAL_MENU_BAR (f)
11554 #else
11555 FRAME_MENU_BAR_LINES (f) > 0
11556 #endif
11557 : FRAME_MENU_BAR_LINES (f) > 0)
11559 /* If the user has switched buffers or windows, we need to
11560 recompute to reflect the new bindings. But we'll
11561 recompute when update_mode_lines is set too; that means
11562 that people can use force-mode-line-update to request
11563 that the menu bar be recomputed. The adverse effect on
11564 the rest of the redisplay algorithm is about the same as
11565 windows_or_buffers_changed anyway. */
11566 if (windows_or_buffers_changed
11567 /* This used to test w->update_mode_line, but we believe
11568 there is no need to recompute the menu in that case. */
11569 || update_mode_lines
11570 || window_buffer_changed (w))
11572 struct buffer *prev = current_buffer;
11573 ptrdiff_t count = SPECPDL_INDEX ();
11575 specbind (Qinhibit_menubar_update, Qt);
11577 set_buffer_internal_1 (XBUFFER (w->contents));
11578 if (save_match_data)
11579 record_unwind_save_match_data ();
11580 if (NILP (Voverriding_local_map_menu_flag))
11582 specbind (Qoverriding_terminal_local_map, Qnil);
11583 specbind (Qoverriding_local_map, Qnil);
11586 if (!hooks_run)
11588 /* Run the Lucid hook. */
11589 safe_run_hooks (Qactivate_menubar_hook);
11591 /* If it has changed current-menubar from previous value,
11592 really recompute the menu-bar from the value. */
11593 if (! NILP (Vlucid_menu_bar_dirty_flag))
11594 call0 (Qrecompute_lucid_menubar);
11596 safe_run_hooks (Qmenu_bar_update_hook);
11598 hooks_run = 1;
11601 XSETFRAME (Vmenu_updating_frame, f);
11602 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11604 /* Redisplay the menu bar in case we changed it. */
11605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11606 || defined (HAVE_NS) || defined (USE_GTK)
11607 if (FRAME_WINDOW_P (f))
11609 #if defined (HAVE_NS)
11610 /* All frames on Mac OS share the same menubar. So only
11611 the selected frame should be allowed to set it. */
11612 if (f == SELECTED_FRAME ())
11613 #endif
11614 set_frame_menubar (f, 0, 0);
11616 else
11617 /* On a terminal screen, the menu bar is an ordinary screen
11618 line, and this makes it get updated. */
11619 w->update_mode_line = 1;
11620 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11621 /* In the non-toolkit version, the menu bar is an ordinary screen
11622 line, and this makes it get updated. */
11623 w->update_mode_line = 1;
11624 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11626 unbind_to (count, Qnil);
11627 set_buffer_internal_1 (prev);
11631 return hooks_run;
11634 /***********************************************************************
11635 Tool-bars
11636 ***********************************************************************/
11638 #ifdef HAVE_WINDOW_SYSTEM
11640 /* Tool-bar item index of the item on which a mouse button was pressed
11641 or -1. */
11643 int last_tool_bar_item;
11645 /* Select `frame' temporarily without running all the code in
11646 do_switch_frame.
11647 FIXME: Maybe do_switch_frame should be trimmed down similarly
11648 when `norecord' is set. */
11649 static void
11650 fast_set_selected_frame (Lisp_Object frame)
11652 if (!EQ (selected_frame, frame))
11654 selected_frame = frame;
11655 selected_window = XFRAME (frame)->selected_window;
11659 /* Update the tool-bar item list for frame F. This has to be done
11660 before we start to fill in any display lines. Called from
11661 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11662 and restore it here. */
11664 static void
11665 update_tool_bar (struct frame *f, int save_match_data)
11667 #if defined (USE_GTK) || defined (HAVE_NS)
11668 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11669 #else
11670 int do_update = (WINDOWP (f->tool_bar_window)
11671 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11672 #endif
11674 if (do_update)
11676 Lisp_Object window;
11677 struct window *w;
11679 window = FRAME_SELECTED_WINDOW (f);
11680 w = XWINDOW (window);
11682 /* If the user has switched buffers or windows, we need to
11683 recompute to reflect the new bindings. But we'll
11684 recompute when update_mode_lines is set too; that means
11685 that people can use force-mode-line-update to request
11686 that the menu bar be recomputed. The adverse effect on
11687 the rest of the redisplay algorithm is about the same as
11688 windows_or_buffers_changed anyway. */
11689 if (windows_or_buffers_changed
11690 || w->update_mode_line
11691 || update_mode_lines
11692 || window_buffer_changed (w))
11694 struct buffer *prev = current_buffer;
11695 ptrdiff_t count = SPECPDL_INDEX ();
11696 Lisp_Object frame, new_tool_bar;
11697 int new_n_tool_bar;
11698 struct gcpro gcpro1;
11700 /* Set current_buffer to the buffer of the selected
11701 window of the frame, so that we get the right local
11702 keymaps. */
11703 set_buffer_internal_1 (XBUFFER (w->contents));
11705 /* Save match data, if we must. */
11706 if (save_match_data)
11707 record_unwind_save_match_data ();
11709 /* Make sure that we don't accidentally use bogus keymaps. */
11710 if (NILP (Voverriding_local_map_menu_flag))
11712 specbind (Qoverriding_terminal_local_map, Qnil);
11713 specbind (Qoverriding_local_map, Qnil);
11716 GCPRO1 (new_tool_bar);
11718 /* We must temporarily set the selected frame to this frame
11719 before calling tool_bar_items, because the calculation of
11720 the tool-bar keymap uses the selected frame (see
11721 `tool-bar-make-keymap' in tool-bar.el). */
11722 eassert (EQ (selected_window,
11723 /* Since we only explicitly preserve selected_frame,
11724 check that selected_window would be redundant. */
11725 XFRAME (selected_frame)->selected_window));
11726 record_unwind_protect (fast_set_selected_frame, selected_frame);
11727 XSETFRAME (frame, f);
11728 fast_set_selected_frame (frame);
11730 /* Build desired tool-bar items from keymaps. */
11731 new_tool_bar
11732 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11733 &new_n_tool_bar);
11735 /* Redisplay the tool-bar if we changed it. */
11736 if (new_n_tool_bar != f->n_tool_bar_items
11737 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11739 /* Redisplay that happens asynchronously due to an expose event
11740 may access f->tool_bar_items. Make sure we update both
11741 variables within BLOCK_INPUT so no such event interrupts. */
11742 block_input ();
11743 fset_tool_bar_items (f, new_tool_bar);
11744 f->n_tool_bar_items = new_n_tool_bar;
11745 w->update_mode_line = 1;
11746 unblock_input ();
11749 UNGCPRO;
11751 unbind_to (count, Qnil);
11752 set_buffer_internal_1 (prev);
11757 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11759 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11760 F's desired tool-bar contents. F->tool_bar_items must have
11761 been set up previously by calling prepare_menu_bars. */
11763 static void
11764 build_desired_tool_bar_string (struct frame *f)
11766 int i, size, size_needed;
11767 struct gcpro gcpro1, gcpro2, gcpro3;
11768 Lisp_Object image, plist, props;
11770 image = plist = props = Qnil;
11771 GCPRO3 (image, plist, props);
11773 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11774 Otherwise, make a new string. */
11776 /* The size of the string we might be able to reuse. */
11777 size = (STRINGP (f->desired_tool_bar_string)
11778 ? SCHARS (f->desired_tool_bar_string)
11779 : 0);
11781 /* We need one space in the string for each image. */
11782 size_needed = f->n_tool_bar_items;
11784 /* Reuse f->desired_tool_bar_string, if possible. */
11785 if (size < size_needed || NILP (f->desired_tool_bar_string))
11786 fset_desired_tool_bar_string
11787 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11788 else
11790 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11791 Fremove_text_properties (make_number (0), make_number (size),
11792 props, f->desired_tool_bar_string);
11795 /* Put a `display' property on the string for the images to display,
11796 put a `menu_item' property on tool-bar items with a value that
11797 is the index of the item in F's tool-bar item vector. */
11798 for (i = 0; i < f->n_tool_bar_items; ++i)
11800 #define PROP(IDX) \
11801 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11803 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11804 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11805 int hmargin, vmargin, relief, idx, end;
11807 /* If image is a vector, choose the image according to the
11808 button state. */
11809 image = PROP (TOOL_BAR_ITEM_IMAGES);
11810 if (VECTORP (image))
11812 if (enabled_p)
11813 idx = (selected_p
11814 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11815 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11816 else
11817 idx = (selected_p
11818 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11819 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11821 eassert (ASIZE (image) >= idx);
11822 image = AREF (image, idx);
11824 else
11825 idx = -1;
11827 /* Ignore invalid image specifications. */
11828 if (!valid_image_p (image))
11829 continue;
11831 /* Display the tool-bar button pressed, or depressed. */
11832 plist = Fcopy_sequence (XCDR (image));
11834 /* Compute margin and relief to draw. */
11835 relief = (tool_bar_button_relief >= 0
11836 ? tool_bar_button_relief
11837 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11838 hmargin = vmargin = relief;
11840 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11841 INT_MAX - max (hmargin, vmargin)))
11843 hmargin += XFASTINT (Vtool_bar_button_margin);
11844 vmargin += XFASTINT (Vtool_bar_button_margin);
11846 else if (CONSP (Vtool_bar_button_margin))
11848 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11849 INT_MAX - hmargin))
11850 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11852 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11853 INT_MAX - vmargin))
11854 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11857 if (auto_raise_tool_bar_buttons_p)
11859 /* Add a `:relief' property to the image spec if the item is
11860 selected. */
11861 if (selected_p)
11863 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11864 hmargin -= relief;
11865 vmargin -= relief;
11868 else
11870 /* If image is selected, display it pressed, i.e. with a
11871 negative relief. If it's not selected, display it with a
11872 raised relief. */
11873 plist = Fplist_put (plist, QCrelief,
11874 (selected_p
11875 ? make_number (-relief)
11876 : make_number (relief)));
11877 hmargin -= relief;
11878 vmargin -= relief;
11881 /* Put a margin around the image. */
11882 if (hmargin || vmargin)
11884 if (hmargin == vmargin)
11885 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11886 else
11887 plist = Fplist_put (plist, QCmargin,
11888 Fcons (make_number (hmargin),
11889 make_number (vmargin)));
11892 /* If button is not enabled, and we don't have special images
11893 for the disabled state, make the image appear disabled by
11894 applying an appropriate algorithm to it. */
11895 if (!enabled_p && idx < 0)
11896 plist = Fplist_put (plist, QCconversion, Qdisabled);
11898 /* Put a `display' text property on the string for the image to
11899 display. Put a `menu-item' property on the string that gives
11900 the start of this item's properties in the tool-bar items
11901 vector. */
11902 image = Fcons (Qimage, plist);
11903 props = list4 (Qdisplay, image,
11904 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11906 /* Let the last image hide all remaining spaces in the tool bar
11907 string. The string can be longer than needed when we reuse a
11908 previous string. */
11909 if (i + 1 == f->n_tool_bar_items)
11910 end = SCHARS (f->desired_tool_bar_string);
11911 else
11912 end = i + 1;
11913 Fadd_text_properties (make_number (i), make_number (end),
11914 props, f->desired_tool_bar_string);
11915 #undef PROP
11918 UNGCPRO;
11922 /* Display one line of the tool-bar of frame IT->f.
11924 HEIGHT specifies the desired height of the tool-bar line.
11925 If the actual height of the glyph row is less than HEIGHT, the
11926 row's height is increased to HEIGHT, and the icons are centered
11927 vertically in the new height.
11929 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11930 count a final empty row in case the tool-bar width exactly matches
11931 the window width.
11934 static void
11935 display_tool_bar_line (struct it *it, int height)
11937 struct glyph_row *row = it->glyph_row;
11938 int max_x = it->last_visible_x;
11939 struct glyph *last;
11941 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11942 clear_glyph_row (row);
11943 row->enabled_p = true;
11944 row->y = it->current_y;
11946 /* Note that this isn't made use of if the face hasn't a box,
11947 so there's no need to check the face here. */
11948 it->start_of_box_run_p = 1;
11950 while (it->current_x < max_x)
11952 int x, n_glyphs_before, i, nglyphs;
11953 struct it it_before;
11955 /* Get the next display element. */
11956 if (!get_next_display_element (it))
11958 /* Don't count empty row if we are counting needed tool-bar lines. */
11959 if (height < 0 && !it->hpos)
11960 return;
11961 break;
11964 /* Produce glyphs. */
11965 n_glyphs_before = row->used[TEXT_AREA];
11966 it_before = *it;
11968 PRODUCE_GLYPHS (it);
11970 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11971 i = 0;
11972 x = it_before.current_x;
11973 while (i < nglyphs)
11975 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11977 if (x + glyph->pixel_width > max_x)
11979 /* Glyph doesn't fit on line. Backtrack. */
11980 row->used[TEXT_AREA] = n_glyphs_before;
11981 *it = it_before;
11982 /* If this is the only glyph on this line, it will never fit on the
11983 tool-bar, so skip it. But ensure there is at least one glyph,
11984 so we don't accidentally disable the tool-bar. */
11985 if (n_glyphs_before == 0
11986 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11987 break;
11988 goto out;
11991 ++it->hpos;
11992 x += glyph->pixel_width;
11993 ++i;
11996 /* Stop at line end. */
11997 if (ITERATOR_AT_END_OF_LINE_P (it))
11998 break;
12000 set_iterator_to_next (it, 1);
12003 out:;
12005 row->displays_text_p = row->used[TEXT_AREA] != 0;
12007 /* Use default face for the border below the tool bar.
12009 FIXME: When auto-resize-tool-bars is grow-only, there is
12010 no additional border below the possibly empty tool-bar lines.
12011 So to make the extra empty lines look "normal", we have to
12012 use the tool-bar face for the border too. */
12013 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12014 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12015 it->face_id = DEFAULT_FACE_ID;
12017 extend_face_to_end_of_line (it);
12018 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12019 last->right_box_line_p = 1;
12020 if (last == row->glyphs[TEXT_AREA])
12021 last->left_box_line_p = 1;
12023 /* Make line the desired height and center it vertically. */
12024 if ((height -= it->max_ascent + it->max_descent) > 0)
12026 /* Don't add more than one line height. */
12027 height %= FRAME_LINE_HEIGHT (it->f);
12028 it->max_ascent += height / 2;
12029 it->max_descent += (height + 1) / 2;
12032 compute_line_metrics (it);
12034 /* If line is empty, make it occupy the rest of the tool-bar. */
12035 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12037 row->height = row->phys_height = it->last_visible_y - row->y;
12038 row->visible_height = row->height;
12039 row->ascent = row->phys_ascent = 0;
12040 row->extra_line_spacing = 0;
12043 row->full_width_p = 1;
12044 row->continued_p = 0;
12045 row->truncated_on_left_p = 0;
12046 row->truncated_on_right_p = 0;
12048 it->current_x = it->hpos = 0;
12049 it->current_y += row->height;
12050 ++it->vpos;
12051 ++it->glyph_row;
12055 /* Max tool-bar height. Basically, this is what makes all other windows
12056 disappear when the frame gets too small. Rethink this! */
12058 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12059 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12061 /* Value is the number of pixels needed to make all tool-bar items of
12062 frame F visible. The actual number of glyph rows needed is
12063 returned in *N_ROWS if non-NULL. */
12065 static int
12066 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12068 struct window *w = XWINDOW (f->tool_bar_window);
12069 struct it it;
12070 /* tool_bar_height is called from redisplay_tool_bar after building
12071 the desired matrix, so use (unused) mode-line row as temporary row to
12072 avoid destroying the first tool-bar row. */
12073 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12075 /* Initialize an iterator for iteration over
12076 F->desired_tool_bar_string in the tool-bar window of frame F. */
12077 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12078 it.first_visible_x = 0;
12079 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12080 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12081 it.paragraph_embedding = L2R;
12083 while (!ITERATOR_AT_END_P (&it))
12085 clear_glyph_row (temp_row);
12086 it.glyph_row = temp_row;
12087 display_tool_bar_line (&it, -1);
12089 clear_glyph_row (temp_row);
12091 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12092 if (n_rows)
12093 *n_rows = it.vpos > 0 ? it.vpos : -1;
12095 if (pixelwise)
12096 return it.current_y;
12097 else
12098 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12101 #endif /* !USE_GTK && !HAVE_NS */
12103 #if defined USE_GTK || defined HAVE_NS
12104 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12105 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12106 #endif
12108 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12109 0, 2, 0,
12110 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12111 If FRAME is nil or omitted, use the selected frame. Optional argument
12112 PIXELWISE non-nil means return the height of the tool bar inpixels. */)
12113 (Lisp_Object frame, Lisp_Object pixelwise)
12115 int height = 0;
12117 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12118 struct frame *f = decode_any_frame (frame);
12120 if (WINDOWP (f->tool_bar_window)
12121 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12123 update_tool_bar (f, 1);
12124 if (f->n_tool_bar_items)
12126 build_desired_tool_bar_string (f);
12127 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12130 #endif
12132 return make_number (height);
12136 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12137 height should be changed. */
12139 static int
12140 redisplay_tool_bar (struct frame *f)
12142 #if defined (USE_GTK) || defined (HAVE_NS)
12144 if (FRAME_EXTERNAL_TOOL_BAR (f))
12145 update_frame_tool_bar (f);
12146 return 0;
12148 #else /* !USE_GTK && !HAVE_NS */
12150 struct window *w;
12151 struct it it;
12152 struct glyph_row *row;
12154 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12155 do anything. This means you must start with tool-bar-lines
12156 non-zero to get the auto-sizing effect. Or in other words, you
12157 can turn off tool-bars by specifying tool-bar-lines zero. */
12158 if (!WINDOWP (f->tool_bar_window)
12159 || (w = XWINDOW (f->tool_bar_window),
12160 WINDOW_PIXEL_HEIGHT (w) == 0))
12161 return 0;
12163 /* Set up an iterator for the tool-bar window. */
12164 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12165 it.first_visible_x = 0;
12166 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12167 row = it.glyph_row;
12169 /* Build a string that represents the contents of the tool-bar. */
12170 build_desired_tool_bar_string (f);
12171 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12172 /* FIXME: This should be controlled by a user option. But it
12173 doesn't make sense to have an R2L tool bar if the menu bar cannot
12174 be drawn also R2L, and making the menu bar R2L is tricky due
12175 toolkit-specific code that implements it. If an R2L tool bar is
12176 ever supported, display_tool_bar_line should also be augmented to
12177 call unproduce_glyphs like display_line and display_string
12178 do. */
12179 it.paragraph_embedding = L2R;
12181 if (f->n_tool_bar_rows == 0)
12183 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12185 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12187 Lisp_Object frame;
12188 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12189 / FRAME_LINE_HEIGHT (f));
12191 XSETFRAME (frame, f);
12192 Fmodify_frame_parameters (frame,
12193 list1 (Fcons (Qtool_bar_lines,
12194 make_number (new_lines))));
12195 /* Always do that now. */
12196 clear_glyph_matrix (w->desired_matrix);
12197 f->fonts_changed = 1;
12198 return 1;
12202 /* Display as many lines as needed to display all tool-bar items. */
12204 if (f->n_tool_bar_rows > 0)
12206 int border, rows, height, extra;
12208 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12209 border = XINT (Vtool_bar_border);
12210 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12211 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12212 else if (EQ (Vtool_bar_border, Qborder_width))
12213 border = f->border_width;
12214 else
12215 border = 0;
12216 if (border < 0)
12217 border = 0;
12219 rows = f->n_tool_bar_rows;
12220 height = max (1, (it.last_visible_y - border) / rows);
12221 extra = it.last_visible_y - border - height * rows;
12223 while (it.current_y < it.last_visible_y)
12225 int h = 0;
12226 if (extra > 0 && rows-- > 0)
12228 h = (extra + rows - 1) / rows;
12229 extra -= h;
12231 display_tool_bar_line (&it, height + h);
12234 else
12236 while (it.current_y < it.last_visible_y)
12237 display_tool_bar_line (&it, 0);
12240 /* It doesn't make much sense to try scrolling in the tool-bar
12241 window, so don't do it. */
12242 w->desired_matrix->no_scrolling_p = 1;
12243 w->must_be_updated_p = 1;
12245 if (!NILP (Vauto_resize_tool_bars))
12247 /* Do we really allow the toolbar to occupy the whole frame? */
12248 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12249 int change_height_p = 0;
12251 /* If we couldn't display everything, change the tool-bar's
12252 height if there is room for more. */
12253 if (IT_STRING_CHARPOS (it) < it.end_charpos
12254 && it.current_y < max_tool_bar_height)
12255 change_height_p = 1;
12257 /* We subtract 1 because display_tool_bar_line advances the
12258 glyph_row pointer before returning to its caller. We want to
12259 examine the last glyph row produced by
12260 display_tool_bar_line. */
12261 row = it.glyph_row - 1;
12263 /* If there are blank lines at the end, except for a partially
12264 visible blank line at the end that is smaller than
12265 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12266 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12267 && row->height >= FRAME_LINE_HEIGHT (f))
12268 change_height_p = 1;
12270 /* If row displays tool-bar items, but is partially visible,
12271 change the tool-bar's height. */
12272 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12273 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12274 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12275 change_height_p = 1;
12277 /* Resize windows as needed by changing the `tool-bar-lines'
12278 frame parameter. */
12279 if (change_height_p)
12281 Lisp_Object frame;
12282 int nrows;
12283 int new_height = tool_bar_height (f, &nrows, 1);
12285 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12286 && !f->minimize_tool_bar_window_p)
12287 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12288 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12289 f->minimize_tool_bar_window_p = 0;
12291 if (change_height_p)
12293 /* Current size of the tool-bar window in canonical line
12294 units. */
12295 int old_lines = WINDOW_TOTAL_LINES (w);
12296 /* Required size of the tool-bar window in canonical
12297 line units. */
12298 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12299 / FRAME_LINE_HEIGHT (f));
12300 /* Maximum size of the tool-bar window in canonical line
12301 units that this frame can allow. */
12302 int max_lines =
12303 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12305 /* Don't try to change the tool-bar window size and set
12306 the fonts_changed flag unless really necessary. That
12307 flag causes redisplay to give up and retry
12308 redisplaying the frame from scratch, so setting it
12309 unnecessarily can lead to nasty redisplay loops. */
12310 if (new_lines <= max_lines
12311 && eabs (new_lines - old_lines) >= 1)
12313 XSETFRAME (frame, f);
12314 Fmodify_frame_parameters (frame,
12315 list1 (Fcons (Qtool_bar_lines,
12316 make_number (new_lines))));
12317 clear_glyph_matrix (w->desired_matrix);
12318 f->n_tool_bar_rows = nrows;
12319 f->fonts_changed = 1;
12320 return 1;
12326 f->minimize_tool_bar_window_p = 0;
12327 return 0;
12329 #endif /* USE_GTK || HAVE_NS */
12332 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12334 /* Get information about the tool-bar item which is displayed in GLYPH
12335 on frame F. Return in *PROP_IDX the index where tool-bar item
12336 properties start in F->tool_bar_items. Value is zero if
12337 GLYPH doesn't display a tool-bar item. */
12339 static int
12340 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12342 Lisp_Object prop;
12343 int success_p;
12344 int charpos;
12346 /* This function can be called asynchronously, which means we must
12347 exclude any possibility that Fget_text_property signals an
12348 error. */
12349 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12350 charpos = max (0, charpos);
12352 /* Get the text property `menu-item' at pos. The value of that
12353 property is the start index of this item's properties in
12354 F->tool_bar_items. */
12355 prop = Fget_text_property (make_number (charpos),
12356 Qmenu_item, f->current_tool_bar_string);
12357 if (INTEGERP (prop))
12359 *prop_idx = XINT (prop);
12360 success_p = 1;
12362 else
12363 success_p = 0;
12365 return success_p;
12369 /* Get information about the tool-bar item at position X/Y on frame F.
12370 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12371 the current matrix of the tool-bar window of F, or NULL if not
12372 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12373 item in F->tool_bar_items. Value is
12375 -1 if X/Y is not on a tool-bar item
12376 0 if X/Y is on the same item that was highlighted before.
12377 1 otherwise. */
12379 static int
12380 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12381 int *hpos, int *vpos, int *prop_idx)
12383 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12384 struct window *w = XWINDOW (f->tool_bar_window);
12385 int area;
12387 /* Find the glyph under X/Y. */
12388 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12389 if (*glyph == NULL)
12390 return -1;
12392 /* Get the start of this tool-bar item's properties in
12393 f->tool_bar_items. */
12394 if (!tool_bar_item_info (f, *glyph, prop_idx))
12395 return -1;
12397 /* Is mouse on the highlighted item? */
12398 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12399 && *vpos >= hlinfo->mouse_face_beg_row
12400 && *vpos <= hlinfo->mouse_face_end_row
12401 && (*vpos > hlinfo->mouse_face_beg_row
12402 || *hpos >= hlinfo->mouse_face_beg_col)
12403 && (*vpos < hlinfo->mouse_face_end_row
12404 || *hpos < hlinfo->mouse_face_end_col
12405 || hlinfo->mouse_face_past_end))
12406 return 0;
12408 return 1;
12412 /* EXPORT:
12413 Handle mouse button event on the tool-bar of frame F, at
12414 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12415 0 for button release. MODIFIERS is event modifiers for button
12416 release. */
12418 void
12419 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12420 int modifiers)
12422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12423 struct window *w = XWINDOW (f->tool_bar_window);
12424 int hpos, vpos, prop_idx;
12425 struct glyph *glyph;
12426 Lisp_Object enabled_p;
12427 int ts;
12429 /* If not on the highlighted tool-bar item, and mouse-highlight is
12430 non-nil, return. This is so we generate the tool-bar button
12431 click only when the mouse button is released on the same item as
12432 where it was pressed. However, when mouse-highlight is disabled,
12433 generate the click when the button is released regardless of the
12434 highlight, since tool-bar items are not highlighted in that
12435 case. */
12436 frame_to_window_pixel_xy (w, &x, &y);
12437 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12438 if (ts == -1
12439 || (ts != 0 && !NILP (Vmouse_highlight)))
12440 return;
12442 /* When mouse-highlight is off, generate the click for the item
12443 where the button was pressed, disregarding where it was
12444 released. */
12445 if (NILP (Vmouse_highlight) && !down_p)
12446 prop_idx = last_tool_bar_item;
12448 /* If item is disabled, do nothing. */
12449 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12450 if (NILP (enabled_p))
12451 return;
12453 if (down_p)
12455 /* Show item in pressed state. */
12456 if (!NILP (Vmouse_highlight))
12457 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12458 last_tool_bar_item = prop_idx;
12460 else
12462 Lisp_Object key, frame;
12463 struct input_event event;
12464 EVENT_INIT (event);
12466 /* Show item in released state. */
12467 if (!NILP (Vmouse_highlight))
12468 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12470 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12472 XSETFRAME (frame, f);
12473 event.kind = TOOL_BAR_EVENT;
12474 event.frame_or_window = frame;
12475 event.arg = frame;
12476 kbd_buffer_store_event (&event);
12478 event.kind = TOOL_BAR_EVENT;
12479 event.frame_or_window = frame;
12480 event.arg = key;
12481 event.modifiers = modifiers;
12482 kbd_buffer_store_event (&event);
12483 last_tool_bar_item = -1;
12488 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12489 tool-bar window-relative coordinates X/Y. Called from
12490 note_mouse_highlight. */
12492 static void
12493 note_tool_bar_highlight (struct frame *f, int x, int y)
12495 Lisp_Object window = f->tool_bar_window;
12496 struct window *w = XWINDOW (window);
12497 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12498 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12499 int hpos, vpos;
12500 struct glyph *glyph;
12501 struct glyph_row *row;
12502 int i;
12503 Lisp_Object enabled_p;
12504 int prop_idx;
12505 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12506 int mouse_down_p, rc;
12508 /* Function note_mouse_highlight is called with negative X/Y
12509 values when mouse moves outside of the frame. */
12510 if (x <= 0 || y <= 0)
12512 clear_mouse_face (hlinfo);
12513 return;
12516 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12517 if (rc < 0)
12519 /* Not on tool-bar item. */
12520 clear_mouse_face (hlinfo);
12521 return;
12523 else if (rc == 0)
12524 /* On same tool-bar item as before. */
12525 goto set_help_echo;
12527 clear_mouse_face (hlinfo);
12529 /* Mouse is down, but on different tool-bar item? */
12530 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12531 && f == dpyinfo->last_mouse_frame);
12533 if (mouse_down_p
12534 && last_tool_bar_item != prop_idx)
12535 return;
12537 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12539 /* If tool-bar item is not enabled, don't highlight it. */
12540 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12541 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12543 /* Compute the x-position of the glyph. In front and past the
12544 image is a space. We include this in the highlighted area. */
12545 row = MATRIX_ROW (w->current_matrix, vpos);
12546 for (i = x = 0; i < hpos; ++i)
12547 x += row->glyphs[TEXT_AREA][i].pixel_width;
12549 /* Record this as the current active region. */
12550 hlinfo->mouse_face_beg_col = hpos;
12551 hlinfo->mouse_face_beg_row = vpos;
12552 hlinfo->mouse_face_beg_x = x;
12553 hlinfo->mouse_face_past_end = 0;
12555 hlinfo->mouse_face_end_col = hpos + 1;
12556 hlinfo->mouse_face_end_row = vpos;
12557 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12558 hlinfo->mouse_face_window = window;
12559 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12561 /* Display it as active. */
12562 show_mouse_face (hlinfo, draw);
12565 set_help_echo:
12567 /* Set help_echo_string to a help string to display for this tool-bar item.
12568 XTread_socket does the rest. */
12569 help_echo_object = help_echo_window = Qnil;
12570 help_echo_pos = -1;
12571 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12572 if (NILP (help_echo_string))
12573 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12576 #endif /* !USE_GTK && !HAVE_NS */
12578 #endif /* HAVE_WINDOW_SYSTEM */
12582 /************************************************************************
12583 Horizontal scrolling
12584 ************************************************************************/
12586 static int hscroll_window_tree (Lisp_Object);
12587 static int hscroll_windows (Lisp_Object);
12589 /* For all leaf windows in the window tree rooted at WINDOW, set their
12590 hscroll value so that PT is (i) visible in the window, and (ii) so
12591 that it is not within a certain margin at the window's left and
12592 right border. Value is non-zero if any window's hscroll has been
12593 changed. */
12595 static int
12596 hscroll_window_tree (Lisp_Object window)
12598 int hscrolled_p = 0;
12599 int hscroll_relative_p = FLOATP (Vhscroll_step);
12600 int hscroll_step_abs = 0;
12601 double hscroll_step_rel = 0;
12603 if (hscroll_relative_p)
12605 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12606 if (hscroll_step_rel < 0)
12608 hscroll_relative_p = 0;
12609 hscroll_step_abs = 0;
12612 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12614 hscroll_step_abs = XINT (Vhscroll_step);
12615 if (hscroll_step_abs < 0)
12616 hscroll_step_abs = 0;
12618 else
12619 hscroll_step_abs = 0;
12621 while (WINDOWP (window))
12623 struct window *w = XWINDOW (window);
12625 if (WINDOWP (w->contents))
12626 hscrolled_p |= hscroll_window_tree (w->contents);
12627 else if (w->cursor.vpos >= 0)
12629 int h_margin;
12630 int text_area_width;
12631 struct glyph_row *cursor_row;
12632 struct glyph_row *bottom_row;
12633 int row_r2l_p;
12635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12636 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12637 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12638 else
12639 cursor_row = bottom_row - 1;
12641 if (!cursor_row->enabled_p)
12643 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12644 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12645 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12646 else
12647 cursor_row = bottom_row - 1;
12649 row_r2l_p = cursor_row->reversed_p;
12651 text_area_width = window_box_width (w, TEXT_AREA);
12653 /* Scroll when cursor is inside this scroll margin. */
12654 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12656 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12657 /* For left-to-right rows, hscroll when cursor is either
12658 (i) inside the right hscroll margin, or (ii) if it is
12659 inside the left margin and the window is already
12660 hscrolled. */
12661 && ((!row_r2l_p
12662 && ((w->hscroll
12663 && w->cursor.x <= h_margin)
12664 || (cursor_row->enabled_p
12665 && cursor_row->truncated_on_right_p
12666 && (w->cursor.x >= text_area_width - h_margin))))
12667 /* For right-to-left rows, the logic is similar,
12668 except that rules for scrolling to left and right
12669 are reversed. E.g., if cursor.x <= h_margin, we
12670 need to hscroll "to the right" unconditionally,
12671 and that will scroll the screen to the left so as
12672 to reveal the next portion of the row. */
12673 || (row_r2l_p
12674 && ((cursor_row->enabled_p
12675 /* FIXME: It is confusing to set the
12676 truncated_on_right_p flag when R2L rows
12677 are actually truncated on the left. */
12678 && cursor_row->truncated_on_right_p
12679 && w->cursor.x <= h_margin)
12680 || (w->hscroll
12681 && (w->cursor.x >= text_area_width - h_margin))))))
12683 struct it it;
12684 ptrdiff_t hscroll;
12685 struct buffer *saved_current_buffer;
12686 ptrdiff_t pt;
12687 int wanted_x;
12689 /* Find point in a display of infinite width. */
12690 saved_current_buffer = current_buffer;
12691 current_buffer = XBUFFER (w->contents);
12693 if (w == XWINDOW (selected_window))
12694 pt = PT;
12695 else
12696 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12698 /* Move iterator to pt starting at cursor_row->start in
12699 a line with infinite width. */
12700 init_to_row_start (&it, w, cursor_row);
12701 it.last_visible_x = INFINITY;
12702 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12703 current_buffer = saved_current_buffer;
12705 /* Position cursor in window. */
12706 if (!hscroll_relative_p && hscroll_step_abs == 0)
12707 hscroll = max (0, (it.current_x
12708 - (ITERATOR_AT_END_OF_LINE_P (&it)
12709 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12710 : (text_area_width / 2))))
12711 / FRAME_COLUMN_WIDTH (it.f);
12712 else if ((!row_r2l_p
12713 && w->cursor.x >= text_area_width - h_margin)
12714 || (row_r2l_p && w->cursor.x <= h_margin))
12716 if (hscroll_relative_p)
12717 wanted_x = text_area_width * (1 - hscroll_step_rel)
12718 - h_margin;
12719 else
12720 wanted_x = text_area_width
12721 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12722 - h_margin;
12723 hscroll
12724 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12726 else
12728 if (hscroll_relative_p)
12729 wanted_x = text_area_width * hscroll_step_rel
12730 + h_margin;
12731 else
12732 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12733 + h_margin;
12734 hscroll
12735 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12737 hscroll = max (hscroll, w->min_hscroll);
12739 /* Don't prevent redisplay optimizations if hscroll
12740 hasn't changed, as it will unnecessarily slow down
12741 redisplay. */
12742 if (w->hscroll != hscroll)
12744 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12745 w->hscroll = hscroll;
12746 hscrolled_p = 1;
12751 window = w->next;
12754 /* Value is non-zero if hscroll of any leaf window has been changed. */
12755 return hscrolled_p;
12759 /* Set hscroll so that cursor is visible and not inside horizontal
12760 scroll margins for all windows in the tree rooted at WINDOW. See
12761 also hscroll_window_tree above. Value is non-zero if any window's
12762 hscroll has been changed. If it has, desired matrices on the frame
12763 of WINDOW are cleared. */
12765 static int
12766 hscroll_windows (Lisp_Object window)
12768 int hscrolled_p = hscroll_window_tree (window);
12769 if (hscrolled_p)
12770 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12771 return hscrolled_p;
12776 /************************************************************************
12777 Redisplay
12778 ************************************************************************/
12780 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12781 to a non-zero value. This is sometimes handy to have in a debugger
12782 session. */
12784 #ifdef GLYPH_DEBUG
12786 /* First and last unchanged row for try_window_id. */
12788 static int debug_first_unchanged_at_end_vpos;
12789 static int debug_last_unchanged_at_beg_vpos;
12791 /* Delta vpos and y. */
12793 static int debug_dvpos, debug_dy;
12795 /* Delta in characters and bytes for try_window_id. */
12797 static ptrdiff_t debug_delta, debug_delta_bytes;
12799 /* Values of window_end_pos and window_end_vpos at the end of
12800 try_window_id. */
12802 static ptrdiff_t debug_end_vpos;
12804 /* Append a string to W->desired_matrix->method. FMT is a printf
12805 format string. If trace_redisplay_p is true also printf the
12806 resulting string to stderr. */
12808 static void debug_method_add (struct window *, char const *, ...)
12809 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12811 static void
12812 debug_method_add (struct window *w, char const *fmt, ...)
12814 void *ptr = w;
12815 char *method = w->desired_matrix->method;
12816 int len = strlen (method);
12817 int size = sizeof w->desired_matrix->method;
12818 int remaining = size - len - 1;
12819 va_list ap;
12821 if (len && remaining)
12823 method[len] = '|';
12824 --remaining, ++len;
12827 va_start (ap, fmt);
12828 vsnprintf (method + len, remaining + 1, fmt, ap);
12829 va_end (ap);
12831 if (trace_redisplay_p)
12832 fprintf (stderr, "%p (%s): %s\n",
12833 ptr,
12834 ((BUFFERP (w->contents)
12835 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12836 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12837 : "no buffer"),
12838 method + len);
12841 #endif /* GLYPH_DEBUG */
12844 /* Value is non-zero if all changes in window W, which displays
12845 current_buffer, are in the text between START and END. START is a
12846 buffer position, END is given as a distance from Z. Used in
12847 redisplay_internal for display optimization. */
12849 static int
12850 text_outside_line_unchanged_p (struct window *w,
12851 ptrdiff_t start, ptrdiff_t end)
12853 int unchanged_p = 1;
12855 /* If text or overlays have changed, see where. */
12856 if (window_outdated (w))
12858 /* Gap in the line? */
12859 if (GPT < start || Z - GPT < end)
12860 unchanged_p = 0;
12862 /* Changes start in front of the line, or end after it? */
12863 if (unchanged_p
12864 && (BEG_UNCHANGED < start - 1
12865 || END_UNCHANGED < end))
12866 unchanged_p = 0;
12868 /* If selective display, can't optimize if changes start at the
12869 beginning of the line. */
12870 if (unchanged_p
12871 && INTEGERP (BVAR (current_buffer, selective_display))
12872 && XINT (BVAR (current_buffer, selective_display)) > 0
12873 && (BEG_UNCHANGED < start || GPT <= start))
12874 unchanged_p = 0;
12876 /* If there are overlays at the start or end of the line, these
12877 may have overlay strings with newlines in them. A change at
12878 START, for instance, may actually concern the display of such
12879 overlay strings as well, and they are displayed on different
12880 lines. So, quickly rule out this case. (For the future, it
12881 might be desirable to implement something more telling than
12882 just BEG/END_UNCHANGED.) */
12883 if (unchanged_p)
12885 if (BEG + BEG_UNCHANGED == start
12886 && overlay_touches_p (start))
12887 unchanged_p = 0;
12888 if (END_UNCHANGED == end
12889 && overlay_touches_p (Z - end))
12890 unchanged_p = 0;
12893 /* Under bidi reordering, adding or deleting a character in the
12894 beginning of a paragraph, before the first strong directional
12895 character, can change the base direction of the paragraph (unless
12896 the buffer specifies a fixed paragraph direction), which will
12897 require to redisplay the whole paragraph. It might be worthwhile
12898 to find the paragraph limits and widen the range of redisplayed
12899 lines to that, but for now just give up this optimization. */
12900 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12901 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12902 unchanged_p = 0;
12905 return unchanged_p;
12909 /* Do a frame update, taking possible shortcuts into account. This is
12910 the main external entry point for redisplay.
12912 If the last redisplay displayed an echo area message and that message
12913 is no longer requested, we clear the echo area or bring back the
12914 mini-buffer if that is in use. */
12916 void
12917 redisplay (void)
12919 redisplay_internal ();
12923 static Lisp_Object
12924 overlay_arrow_string_or_property (Lisp_Object var)
12926 Lisp_Object val;
12928 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12929 return val;
12931 return Voverlay_arrow_string;
12934 /* Return 1 if there are any overlay-arrows in current_buffer. */
12935 static int
12936 overlay_arrow_in_current_buffer_p (void)
12938 Lisp_Object vlist;
12940 for (vlist = Voverlay_arrow_variable_list;
12941 CONSP (vlist);
12942 vlist = XCDR (vlist))
12944 Lisp_Object var = XCAR (vlist);
12945 Lisp_Object val;
12947 if (!SYMBOLP (var))
12948 continue;
12949 val = find_symbol_value (var);
12950 if (MARKERP (val)
12951 && current_buffer == XMARKER (val)->buffer)
12952 return 1;
12954 return 0;
12958 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12959 has changed. */
12961 static int
12962 overlay_arrows_changed_p (void)
12964 Lisp_Object vlist;
12966 for (vlist = Voverlay_arrow_variable_list;
12967 CONSP (vlist);
12968 vlist = XCDR (vlist))
12970 Lisp_Object var = XCAR (vlist);
12971 Lisp_Object val, pstr;
12973 if (!SYMBOLP (var))
12974 continue;
12975 val = find_symbol_value (var);
12976 if (!MARKERP (val))
12977 continue;
12978 if (! EQ (COERCE_MARKER (val),
12979 Fget (var, Qlast_arrow_position))
12980 || ! (pstr = overlay_arrow_string_or_property (var),
12981 EQ (pstr, Fget (var, Qlast_arrow_string))))
12982 return 1;
12984 return 0;
12987 /* Mark overlay arrows to be updated on next redisplay. */
12989 static void
12990 update_overlay_arrows (int up_to_date)
12992 Lisp_Object vlist;
12994 for (vlist = Voverlay_arrow_variable_list;
12995 CONSP (vlist);
12996 vlist = XCDR (vlist))
12998 Lisp_Object var = XCAR (vlist);
13000 if (!SYMBOLP (var))
13001 continue;
13003 if (up_to_date > 0)
13005 Lisp_Object val = find_symbol_value (var);
13006 Fput (var, Qlast_arrow_position,
13007 COERCE_MARKER (val));
13008 Fput (var, Qlast_arrow_string,
13009 overlay_arrow_string_or_property (var));
13011 else if (up_to_date < 0
13012 || !NILP (Fget (var, Qlast_arrow_position)))
13014 Fput (var, Qlast_arrow_position, Qt);
13015 Fput (var, Qlast_arrow_string, Qt);
13021 /* Return overlay arrow string to display at row.
13022 Return integer (bitmap number) for arrow bitmap in left fringe.
13023 Return nil if no overlay arrow. */
13025 static Lisp_Object
13026 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13028 Lisp_Object vlist;
13030 for (vlist = Voverlay_arrow_variable_list;
13031 CONSP (vlist);
13032 vlist = XCDR (vlist))
13034 Lisp_Object var = XCAR (vlist);
13035 Lisp_Object val;
13037 if (!SYMBOLP (var))
13038 continue;
13040 val = find_symbol_value (var);
13042 if (MARKERP (val)
13043 && current_buffer == XMARKER (val)->buffer
13044 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13046 if (FRAME_WINDOW_P (it->f)
13047 /* FIXME: if ROW->reversed_p is set, this should test
13048 the right fringe, not the left one. */
13049 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13051 #ifdef HAVE_WINDOW_SYSTEM
13052 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13054 int fringe_bitmap;
13055 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13056 return make_number (fringe_bitmap);
13058 #endif
13059 return make_number (-1); /* Use default arrow bitmap. */
13061 return overlay_arrow_string_or_property (var);
13065 return Qnil;
13068 /* Return 1 if point moved out of or into a composition. Otherwise
13069 return 0. PREV_BUF and PREV_PT are the last point buffer and
13070 position. BUF and PT are the current point buffer and position. */
13072 static int
13073 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13074 struct buffer *buf, ptrdiff_t pt)
13076 ptrdiff_t start, end;
13077 Lisp_Object prop;
13078 Lisp_Object buffer;
13080 XSETBUFFER (buffer, buf);
13081 /* Check a composition at the last point if point moved within the
13082 same buffer. */
13083 if (prev_buf == buf)
13085 if (prev_pt == pt)
13086 /* Point didn't move. */
13087 return 0;
13089 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13090 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13091 && composition_valid_p (start, end, prop)
13092 && start < prev_pt && end > prev_pt)
13093 /* The last point was within the composition. Return 1 iff
13094 point moved out of the composition. */
13095 return (pt <= start || pt >= end);
13098 /* Check a composition at the current point. */
13099 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13100 && find_composition (pt, -1, &start, &end, &prop, buffer)
13101 && composition_valid_p (start, end, prop)
13102 && start < pt && end > pt);
13105 /* Reconsider the clip changes of buffer which is displayed in W. */
13107 static void
13108 reconsider_clip_changes (struct window *w)
13110 struct buffer *b = XBUFFER (w->contents);
13112 if (b->clip_changed
13113 && w->window_end_valid
13114 && w->current_matrix->buffer == b
13115 && w->current_matrix->zv == BUF_ZV (b)
13116 && w->current_matrix->begv == BUF_BEGV (b))
13117 b->clip_changed = 0;
13119 /* If display wasn't paused, and W is not a tool bar window, see if
13120 point has been moved into or out of a composition. In that case,
13121 we set b->clip_changed to 1 to force updating the screen. If
13122 b->clip_changed has already been set to 1, we can skip this
13123 check. */
13124 if (!b->clip_changed && w->window_end_valid)
13126 ptrdiff_t pt = (w == XWINDOW (selected_window)
13127 ? PT : marker_position (w->pointm));
13129 if ((w->current_matrix->buffer != b || pt != w->last_point)
13130 && check_point_in_composition (w->current_matrix->buffer,
13131 w->last_point, b, pt))
13132 b->clip_changed = 1;
13136 static void
13137 propagate_buffer_redisplay (void)
13138 { /* Resetting b->text->redisplay is problematic!
13139 We can't just reset it in the case that some window that displays
13140 it has not been redisplayed; and such a window can stay
13141 unredisplayed for a long time if it's currently invisible.
13142 But we do want to reset it at the end of redisplay otherwise
13143 its displayed windows will keep being redisplayed over and over
13144 again.
13145 So we copy all b->text->redisplay flags up to their windows here,
13146 such that mark_window_display_accurate can safely reset
13147 b->text->redisplay. */
13148 Lisp_Object ws = window_list ();
13149 for (; CONSP (ws); ws = XCDR (ws))
13151 struct window *thisw = XWINDOW (XCAR (ws));
13152 struct buffer *thisb = XBUFFER (thisw->contents);
13153 if (thisb->text->redisplay)
13154 thisw->redisplay = true;
13158 #define STOP_POLLING \
13159 do { if (! polling_stopped_here) stop_polling (); \
13160 polling_stopped_here = 1; } while (0)
13162 #define RESUME_POLLING \
13163 do { if (polling_stopped_here) start_polling (); \
13164 polling_stopped_here = 0; } while (0)
13167 /* Perhaps in the future avoid recentering windows if it
13168 is not necessary; currently that causes some problems. */
13170 static void
13171 redisplay_internal (void)
13173 struct window *w = XWINDOW (selected_window);
13174 struct window *sw;
13175 struct frame *fr;
13176 int pending;
13177 bool must_finish = 0, match_p;
13178 struct text_pos tlbufpos, tlendpos;
13179 int number_of_visible_frames;
13180 ptrdiff_t count;
13181 struct frame *sf;
13182 int polling_stopped_here = 0;
13183 Lisp_Object tail, frame;
13185 /* True means redisplay has to consider all windows on all
13186 frames. False, only selected_window is considered. */
13187 bool consider_all_windows_p;
13189 /* True means redisplay has to redisplay the miniwindow. */
13190 bool update_miniwindow_p = false;
13192 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13194 /* No redisplay if running in batch mode or frame is not yet fully
13195 initialized, or redisplay is explicitly turned off by setting
13196 Vinhibit_redisplay. */
13197 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13198 || !NILP (Vinhibit_redisplay))
13199 return;
13201 /* Don't examine these until after testing Vinhibit_redisplay.
13202 When Emacs is shutting down, perhaps because its connection to
13203 X has dropped, we should not look at them at all. */
13204 fr = XFRAME (w->frame);
13205 sf = SELECTED_FRAME ();
13207 if (!fr->glyphs_initialized_p)
13208 return;
13210 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13211 if (popup_activated ())
13212 return;
13213 #endif
13215 /* I don't think this happens but let's be paranoid. */
13216 if (redisplaying_p)
13217 return;
13219 /* Record a function that clears redisplaying_p
13220 when we leave this function. */
13221 count = SPECPDL_INDEX ();
13222 record_unwind_protect_void (unwind_redisplay);
13223 redisplaying_p = 1;
13224 specbind (Qinhibit_free_realized_faces, Qnil);
13226 /* Record this function, so it appears on the profiler's backtraces. */
13227 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13229 FOR_EACH_FRAME (tail, frame)
13230 XFRAME (frame)->already_hscrolled_p = 0;
13232 retry:
13233 /* Remember the currently selected window. */
13234 sw = w;
13236 pending = 0;
13237 last_escape_glyph_frame = NULL;
13238 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13239 last_glyphless_glyph_frame = NULL;
13240 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13242 /* If face_change_count is non-zero, init_iterator will free all
13243 realized faces, which includes the faces referenced from current
13244 matrices. So, we can't reuse current matrices in this case. */
13245 if (face_change_count)
13246 windows_or_buffers_changed = 47;
13248 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13249 && FRAME_TTY (sf)->previous_frame != sf)
13251 /* Since frames on a single ASCII terminal share the same
13252 display area, displaying a different frame means redisplay
13253 the whole thing. */
13254 SET_FRAME_GARBAGED (sf);
13255 #ifndef DOS_NT
13256 set_tty_color_mode (FRAME_TTY (sf), sf);
13257 #endif
13258 FRAME_TTY (sf)->previous_frame = sf;
13261 /* Set the visible flags for all frames. Do this before checking for
13262 resized or garbaged frames; they want to know if their frames are
13263 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13264 number_of_visible_frames = 0;
13266 FOR_EACH_FRAME (tail, frame)
13268 struct frame *f = XFRAME (frame);
13270 if (FRAME_VISIBLE_P (f))
13272 ++number_of_visible_frames;
13273 /* Adjust matrices for visible frames only. */
13274 if (f->fonts_changed)
13276 adjust_frame_glyphs (f);
13277 f->fonts_changed = 0;
13279 /* If cursor type has been changed on the frame
13280 other than selected, consider all frames. */
13281 if (f != sf && f->cursor_type_changed)
13282 update_mode_lines = 31;
13284 clear_desired_matrices (f);
13287 /* Notice any pending interrupt request to change frame size. */
13288 do_pending_window_change (1);
13290 /* do_pending_window_change could change the selected_window due to
13291 frame resizing which makes the selected window too small. */
13292 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13293 sw = w;
13295 /* Clear frames marked as garbaged. */
13296 clear_garbaged_frames ();
13298 /* Build menubar and tool-bar items. */
13299 if (NILP (Vmemory_full))
13300 prepare_menu_bars ();
13302 reconsider_clip_changes (w);
13304 /* In most cases selected window displays current buffer. */
13305 match_p = XBUFFER (w->contents) == current_buffer;
13306 if (match_p)
13308 /* Detect case that we need to write or remove a star in the mode line. */
13309 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13310 w->update_mode_line = 1;
13312 if (mode_line_update_needed (w))
13313 w->update_mode_line = 1;
13316 /* Normally the message* functions will have already displayed and
13317 updated the echo area, but the frame may have been trashed, or
13318 the update may have been preempted, so display the echo area
13319 again here. Checking message_cleared_p captures the case that
13320 the echo area should be cleared. */
13321 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13322 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13323 || (message_cleared_p
13324 && minibuf_level == 0
13325 /* If the mini-window is currently selected, this means the
13326 echo-area doesn't show through. */
13327 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13329 int window_height_changed_p = echo_area_display (0);
13331 if (message_cleared_p)
13332 update_miniwindow_p = true;
13334 must_finish = 1;
13336 /* If we don't display the current message, don't clear the
13337 message_cleared_p flag, because, if we did, we wouldn't clear
13338 the echo area in the next redisplay which doesn't preserve
13339 the echo area. */
13340 if (!display_last_displayed_message_p)
13341 message_cleared_p = 0;
13343 if (window_height_changed_p)
13345 windows_or_buffers_changed = 50;
13347 /* If window configuration was changed, frames may have been
13348 marked garbaged. Clear them or we will experience
13349 surprises wrt scrolling. */
13350 clear_garbaged_frames ();
13353 else if (EQ (selected_window, minibuf_window)
13354 && (current_buffer->clip_changed || window_outdated (w))
13355 && resize_mini_window (w, 0))
13357 /* Resized active mini-window to fit the size of what it is
13358 showing if its contents might have changed. */
13359 must_finish = 1;
13361 /* If window configuration was changed, frames may have been
13362 marked garbaged. Clear them or we will experience
13363 surprises wrt scrolling. */
13364 clear_garbaged_frames ();
13367 if (windows_or_buffers_changed && !update_mode_lines)
13368 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13369 only the windows's contents needs to be refreshed, or whether the
13370 mode-lines also need a refresh. */
13371 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13372 ? REDISPLAY_SOME : 32);
13374 /* If specs for an arrow have changed, do thorough redisplay
13375 to ensure we remove any arrow that should no longer exist. */
13376 if (overlay_arrows_changed_p ())
13377 /* Apparently, this is the only case where we update other windows,
13378 without updating other mode-lines. */
13379 windows_or_buffers_changed = 49;
13381 consider_all_windows_p = (update_mode_lines
13382 || windows_or_buffers_changed);
13384 #define AINC(a,i) \
13385 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13386 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13388 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13389 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13391 /* Optimize the case that only the line containing the cursor in the
13392 selected window has changed. Variables starting with this_ are
13393 set in display_line and record information about the line
13394 containing the cursor. */
13395 tlbufpos = this_line_start_pos;
13396 tlendpos = this_line_end_pos;
13397 if (!consider_all_windows_p
13398 && CHARPOS (tlbufpos) > 0
13399 && !w->update_mode_line
13400 && !current_buffer->clip_changed
13401 && !current_buffer->prevent_redisplay_optimizations_p
13402 && FRAME_VISIBLE_P (XFRAME (w->frame))
13403 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13404 && !XFRAME (w->frame)->cursor_type_changed
13405 /* Make sure recorded data applies to current buffer, etc. */
13406 && this_line_buffer == current_buffer
13407 && match_p
13408 && !w->force_start
13409 && !w->optional_new_start
13410 /* Point must be on the line that we have info recorded about. */
13411 && PT >= CHARPOS (tlbufpos)
13412 && PT <= Z - CHARPOS (tlendpos)
13413 /* All text outside that line, including its final newline,
13414 must be unchanged. */
13415 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13416 CHARPOS (tlendpos)))
13418 if (CHARPOS (tlbufpos) > BEGV
13419 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13420 && (CHARPOS (tlbufpos) == ZV
13421 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13422 /* Former continuation line has disappeared by becoming empty. */
13423 goto cancel;
13424 else if (window_outdated (w) || MINI_WINDOW_P (w))
13426 /* We have to handle the case of continuation around a
13427 wide-column character (see the comment in indent.c around
13428 line 1340).
13430 For instance, in the following case:
13432 -------- Insert --------
13433 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13434 J_I_ ==> J_I_ `^^' are cursors.
13435 ^^ ^^
13436 -------- --------
13438 As we have to redraw the line above, we cannot use this
13439 optimization. */
13441 struct it it;
13442 int line_height_before = this_line_pixel_height;
13444 /* Note that start_display will handle the case that the
13445 line starting at tlbufpos is a continuation line. */
13446 start_display (&it, w, tlbufpos);
13448 /* Implementation note: It this still necessary? */
13449 if (it.current_x != this_line_start_x)
13450 goto cancel;
13452 TRACE ((stderr, "trying display optimization 1\n"));
13453 w->cursor.vpos = -1;
13454 overlay_arrow_seen = 0;
13455 it.vpos = this_line_vpos;
13456 it.current_y = this_line_y;
13457 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13458 display_line (&it);
13460 /* If line contains point, is not continued,
13461 and ends at same distance from eob as before, we win. */
13462 if (w->cursor.vpos >= 0
13463 /* Line is not continued, otherwise this_line_start_pos
13464 would have been set to 0 in display_line. */
13465 && CHARPOS (this_line_start_pos)
13466 /* Line ends as before. */
13467 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13468 /* Line has same height as before. Otherwise other lines
13469 would have to be shifted up or down. */
13470 && this_line_pixel_height == line_height_before)
13472 /* If this is not the window's last line, we must adjust
13473 the charstarts of the lines below. */
13474 if (it.current_y < it.last_visible_y)
13476 struct glyph_row *row
13477 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13478 ptrdiff_t delta, delta_bytes;
13480 /* We used to distinguish between two cases here,
13481 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13482 when the line ends in a newline or the end of the
13483 buffer's accessible portion. But both cases did
13484 the same, so they were collapsed. */
13485 delta = (Z
13486 - CHARPOS (tlendpos)
13487 - MATRIX_ROW_START_CHARPOS (row));
13488 delta_bytes = (Z_BYTE
13489 - BYTEPOS (tlendpos)
13490 - MATRIX_ROW_START_BYTEPOS (row));
13492 increment_matrix_positions (w->current_matrix,
13493 this_line_vpos + 1,
13494 w->current_matrix->nrows,
13495 delta, delta_bytes);
13498 /* If this row displays text now but previously didn't,
13499 or vice versa, w->window_end_vpos may have to be
13500 adjusted. */
13501 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13503 if (w->window_end_vpos < this_line_vpos)
13504 w->window_end_vpos = this_line_vpos;
13506 else if (w->window_end_vpos == this_line_vpos
13507 && this_line_vpos > 0)
13508 w->window_end_vpos = this_line_vpos - 1;
13509 w->window_end_valid = 0;
13511 /* Update hint: No need to try to scroll in update_window. */
13512 w->desired_matrix->no_scrolling_p = 1;
13514 #ifdef GLYPH_DEBUG
13515 *w->desired_matrix->method = 0;
13516 debug_method_add (w, "optimization 1");
13517 #endif
13518 #ifdef HAVE_WINDOW_SYSTEM
13519 update_window_fringes (w, 0);
13520 #endif
13521 goto update;
13523 else
13524 goto cancel;
13526 else if (/* Cursor position hasn't changed. */
13527 PT == w->last_point
13528 /* Make sure the cursor was last displayed
13529 in this window. Otherwise we have to reposition it. */
13531 /* PXW: Must be converted to pixels, probably. */
13532 && 0 <= w->cursor.vpos
13533 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13535 if (!must_finish)
13537 do_pending_window_change (1);
13538 /* If selected_window changed, redisplay again. */
13539 if (WINDOWP (selected_window)
13540 && (w = XWINDOW (selected_window)) != sw)
13541 goto retry;
13543 /* We used to always goto end_of_redisplay here, but this
13544 isn't enough if we have a blinking cursor. */
13545 if (w->cursor_off_p == w->last_cursor_off_p)
13546 goto end_of_redisplay;
13548 goto update;
13550 /* If highlighting the region, or if the cursor is in the echo area,
13551 then we can't just move the cursor. */
13552 else if (NILP (Vshow_trailing_whitespace)
13553 && !cursor_in_echo_area)
13555 struct it it;
13556 struct glyph_row *row;
13558 /* Skip from tlbufpos to PT and see where it is. Note that
13559 PT may be in invisible text. If so, we will end at the
13560 next visible position. */
13561 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13562 NULL, DEFAULT_FACE_ID);
13563 it.current_x = this_line_start_x;
13564 it.current_y = this_line_y;
13565 it.vpos = this_line_vpos;
13567 /* The call to move_it_to stops in front of PT, but
13568 moves over before-strings. */
13569 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13571 if (it.vpos == this_line_vpos
13572 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13573 row->enabled_p))
13575 eassert (this_line_vpos == it.vpos);
13576 eassert (this_line_y == it.current_y);
13577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13578 #ifdef GLYPH_DEBUG
13579 *w->desired_matrix->method = 0;
13580 debug_method_add (w, "optimization 3");
13581 #endif
13582 goto update;
13584 else
13585 goto cancel;
13588 cancel:
13589 /* Text changed drastically or point moved off of line. */
13590 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13593 CHARPOS (this_line_start_pos) = 0;
13594 ++clear_face_cache_count;
13595 #ifdef HAVE_WINDOW_SYSTEM
13596 ++clear_image_cache_count;
13597 #endif
13599 /* Build desired matrices, and update the display. If
13600 consider_all_windows_p is non-zero, do it for all windows on all
13601 frames. Otherwise do it for selected_window, only. */
13603 if (consider_all_windows_p)
13605 FOR_EACH_FRAME (tail, frame)
13606 XFRAME (frame)->updated_p = 0;
13608 propagate_buffer_redisplay ();
13610 FOR_EACH_FRAME (tail, frame)
13612 struct frame *f = XFRAME (frame);
13614 /* We don't have to do anything for unselected terminal
13615 frames. */
13616 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13617 && !EQ (FRAME_TTY (f)->top_frame, frame))
13618 continue;
13620 retry_frame:
13622 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13624 bool gcscrollbars
13625 /* Only GC scrollbars when we redisplay the whole frame. */
13626 = f->redisplay || !REDISPLAY_SOME_P ();
13627 /* Mark all the scroll bars to be removed; we'll redeem
13628 the ones we want when we redisplay their windows. */
13629 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13630 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13632 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13633 redisplay_windows (FRAME_ROOT_WINDOW (f));
13634 /* Remember that the invisible frames need to be redisplayed next
13635 time they're visible. */
13636 else if (!REDISPLAY_SOME_P ())
13637 f->redisplay = true;
13639 /* The X error handler may have deleted that frame. */
13640 if (!FRAME_LIVE_P (f))
13641 continue;
13643 /* Any scroll bars which redisplay_windows should have
13644 nuked should now go away. */
13645 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13646 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13648 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13650 /* If fonts changed on visible frame, display again. */
13651 if (f->fonts_changed)
13653 adjust_frame_glyphs (f);
13654 f->fonts_changed = 0;
13655 goto retry_frame;
13658 /* See if we have to hscroll. */
13659 if (!f->already_hscrolled_p)
13661 f->already_hscrolled_p = 1;
13662 if (hscroll_windows (f->root_window))
13663 goto retry_frame;
13666 /* Prevent various kinds of signals during display
13667 update. stdio is not robust about handling
13668 signals, which can cause an apparent I/O error. */
13669 if (interrupt_input)
13670 unrequest_sigio ();
13671 STOP_POLLING;
13673 pending |= update_frame (f, 0, 0);
13674 f->cursor_type_changed = 0;
13675 f->updated_p = 1;
13680 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13682 if (!pending)
13684 /* Do the mark_window_display_accurate after all windows have
13685 been redisplayed because this call resets flags in buffers
13686 which are needed for proper redisplay. */
13687 FOR_EACH_FRAME (tail, frame)
13689 struct frame *f = XFRAME (frame);
13690 if (f->updated_p)
13692 f->redisplay = false;
13693 mark_window_display_accurate (f->root_window, 1);
13694 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13695 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13700 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13702 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13703 struct frame *mini_frame;
13705 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13706 /* Use list_of_error, not Qerror, so that
13707 we catch only errors and don't run the debugger. */
13708 internal_condition_case_1 (redisplay_window_1, selected_window,
13709 list_of_error,
13710 redisplay_window_error);
13711 if (update_miniwindow_p)
13712 internal_condition_case_1 (redisplay_window_1, mini_window,
13713 list_of_error,
13714 redisplay_window_error);
13716 /* Compare desired and current matrices, perform output. */
13718 update:
13719 /* If fonts changed, display again. */
13720 if (sf->fonts_changed)
13721 goto retry;
13723 /* Prevent various kinds of signals during display update.
13724 stdio is not robust about handling signals,
13725 which can cause an apparent I/O error. */
13726 if (interrupt_input)
13727 unrequest_sigio ();
13728 STOP_POLLING;
13730 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13732 if (hscroll_windows (selected_window))
13733 goto retry;
13735 XWINDOW (selected_window)->must_be_updated_p = true;
13736 pending = update_frame (sf, 0, 0);
13737 sf->cursor_type_changed = 0;
13740 /* We may have called echo_area_display at the top of this
13741 function. If the echo area is on another frame, that may
13742 have put text on a frame other than the selected one, so the
13743 above call to update_frame would not have caught it. Catch
13744 it here. */
13745 mini_window = FRAME_MINIBUF_WINDOW (sf);
13746 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13748 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13750 XWINDOW (mini_window)->must_be_updated_p = true;
13751 pending |= update_frame (mini_frame, 0, 0);
13752 mini_frame->cursor_type_changed = 0;
13753 if (!pending && hscroll_windows (mini_window))
13754 goto retry;
13758 /* If display was paused because of pending input, make sure we do a
13759 thorough update the next time. */
13760 if (pending)
13762 /* Prevent the optimization at the beginning of
13763 redisplay_internal that tries a single-line update of the
13764 line containing the cursor in the selected window. */
13765 CHARPOS (this_line_start_pos) = 0;
13767 /* Let the overlay arrow be updated the next time. */
13768 update_overlay_arrows (0);
13770 /* If we pause after scrolling, some rows in the current
13771 matrices of some windows are not valid. */
13772 if (!WINDOW_FULL_WIDTH_P (w)
13773 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13774 update_mode_lines = 36;
13776 else
13778 if (!consider_all_windows_p)
13780 /* This has already been done above if
13781 consider_all_windows_p is set. */
13782 if (XBUFFER (w->contents)->text->redisplay
13783 && buffer_window_count (XBUFFER (w->contents)) > 1)
13784 /* This can happen if b->text->redisplay was set during
13785 jit-lock. */
13786 propagate_buffer_redisplay ();
13787 mark_window_display_accurate_1 (w, 1);
13789 /* Say overlay arrows are up to date. */
13790 update_overlay_arrows (1);
13792 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13793 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13796 update_mode_lines = 0;
13797 windows_or_buffers_changed = 0;
13800 /* Start SIGIO interrupts coming again. Having them off during the
13801 code above makes it less likely one will discard output, but not
13802 impossible, since there might be stuff in the system buffer here.
13803 But it is much hairier to try to do anything about that. */
13804 if (interrupt_input)
13805 request_sigio ();
13806 RESUME_POLLING;
13808 /* If a frame has become visible which was not before, redisplay
13809 again, so that we display it. Expose events for such a frame
13810 (which it gets when becoming visible) don't call the parts of
13811 redisplay constructing glyphs, so simply exposing a frame won't
13812 display anything in this case. So, we have to display these
13813 frames here explicitly. */
13814 if (!pending)
13816 int new_count = 0;
13818 FOR_EACH_FRAME (tail, frame)
13820 if (XFRAME (frame)->visible)
13821 new_count++;
13824 if (new_count != number_of_visible_frames)
13825 windows_or_buffers_changed = 52;
13828 /* Change frame size now if a change is pending. */
13829 do_pending_window_change (1);
13831 /* If we just did a pending size change, or have additional
13832 visible frames, or selected_window changed, redisplay again. */
13833 if ((windows_or_buffers_changed && !pending)
13834 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13835 goto retry;
13837 /* Clear the face and image caches.
13839 We used to do this only if consider_all_windows_p. But the cache
13840 needs to be cleared if a timer creates images in the current
13841 buffer (e.g. the test case in Bug#6230). */
13843 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13845 clear_face_cache (0);
13846 clear_face_cache_count = 0;
13849 #ifdef HAVE_WINDOW_SYSTEM
13850 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13852 clear_image_caches (Qnil);
13853 clear_image_cache_count = 0;
13855 #endif /* HAVE_WINDOW_SYSTEM */
13857 end_of_redisplay:
13858 if (interrupt_input && interrupts_deferred)
13859 request_sigio ();
13861 unbind_to (count, Qnil);
13862 RESUME_POLLING;
13866 /* Redisplay, but leave alone any recent echo area message unless
13867 another message has been requested in its place.
13869 This is useful in situations where you need to redisplay but no
13870 user action has occurred, making it inappropriate for the message
13871 area to be cleared. See tracking_off and
13872 wait_reading_process_output for examples of these situations.
13874 FROM_WHERE is an integer saying from where this function was
13875 called. This is useful for debugging. */
13877 void
13878 redisplay_preserve_echo_area (int from_where)
13880 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13882 if (!NILP (echo_area_buffer[1]))
13884 /* We have a previously displayed message, but no current
13885 message. Redisplay the previous message. */
13886 display_last_displayed_message_p = 1;
13887 redisplay_internal ();
13888 display_last_displayed_message_p = 0;
13890 else
13891 redisplay_internal ();
13893 flush_frame (SELECTED_FRAME ());
13897 /* Function registered with record_unwind_protect in redisplay_internal. */
13899 static void
13900 unwind_redisplay (void)
13902 redisplaying_p = 0;
13906 /* Mark the display of leaf window W as accurate or inaccurate.
13907 If ACCURATE_P is non-zero mark display of W as accurate. If
13908 ACCURATE_P is zero, arrange for W to be redisplayed the next
13909 time redisplay_internal is called. */
13911 static void
13912 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13914 struct buffer *b = XBUFFER (w->contents);
13916 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13917 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13918 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13920 if (accurate_p)
13922 b->clip_changed = false;
13923 b->prevent_redisplay_optimizations_p = false;
13924 eassert (buffer_window_count (b) > 0);
13925 /* Resetting b->text->redisplay is problematic!
13926 In order to make it safer to do it here, redisplay_internal must
13927 have copied all b->text->redisplay to their respective windows. */
13928 b->text->redisplay = false;
13930 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13931 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13932 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13933 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13935 w->current_matrix->buffer = b;
13936 w->current_matrix->begv = BUF_BEGV (b);
13937 w->current_matrix->zv = BUF_ZV (b);
13939 w->last_cursor_vpos = w->cursor.vpos;
13940 w->last_cursor_off_p = w->cursor_off_p;
13942 if (w == XWINDOW (selected_window))
13943 w->last_point = BUF_PT (b);
13944 else
13945 w->last_point = marker_position (w->pointm);
13947 w->window_end_valid = true;
13948 w->update_mode_line = false;
13951 w->redisplay = !accurate_p;
13955 /* Mark the display of windows in the window tree rooted at WINDOW as
13956 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13957 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13958 be redisplayed the next time redisplay_internal is called. */
13960 void
13961 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13963 struct window *w;
13965 for (; !NILP (window); window = w->next)
13967 w = XWINDOW (window);
13968 if (WINDOWP (w->contents))
13969 mark_window_display_accurate (w->contents, accurate_p);
13970 else
13971 mark_window_display_accurate_1 (w, accurate_p);
13974 if (accurate_p)
13975 update_overlay_arrows (1);
13976 else
13977 /* Force a thorough redisplay the next time by setting
13978 last_arrow_position and last_arrow_string to t, which is
13979 unequal to any useful value of Voverlay_arrow_... */
13980 update_overlay_arrows (-1);
13984 /* Return value in display table DP (Lisp_Char_Table *) for character
13985 C. Since a display table doesn't have any parent, we don't have to
13986 follow parent. Do not call this function directly but use the
13987 macro DISP_CHAR_VECTOR. */
13989 Lisp_Object
13990 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13992 Lisp_Object val;
13994 if (ASCII_CHAR_P (c))
13996 val = dp->ascii;
13997 if (SUB_CHAR_TABLE_P (val))
13998 val = XSUB_CHAR_TABLE (val)->contents[c];
14000 else
14002 Lisp_Object table;
14004 XSETCHAR_TABLE (table, dp);
14005 val = char_table_ref (table, c);
14007 if (NILP (val))
14008 val = dp->defalt;
14009 return val;
14014 /***********************************************************************
14015 Window Redisplay
14016 ***********************************************************************/
14018 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14020 static void
14021 redisplay_windows (Lisp_Object window)
14023 while (!NILP (window))
14025 struct window *w = XWINDOW (window);
14027 if (WINDOWP (w->contents))
14028 redisplay_windows (w->contents);
14029 else if (BUFFERP (w->contents))
14031 displayed_buffer = XBUFFER (w->contents);
14032 /* Use list_of_error, not Qerror, so that
14033 we catch only errors and don't run the debugger. */
14034 internal_condition_case_1 (redisplay_window_0, window,
14035 list_of_error,
14036 redisplay_window_error);
14039 window = w->next;
14043 static Lisp_Object
14044 redisplay_window_error (Lisp_Object ignore)
14046 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14047 return Qnil;
14050 static Lisp_Object
14051 redisplay_window_0 (Lisp_Object window)
14053 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14054 redisplay_window (window, false);
14055 return Qnil;
14058 static Lisp_Object
14059 redisplay_window_1 (Lisp_Object window)
14061 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14062 redisplay_window (window, true);
14063 return Qnil;
14067 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14068 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14069 which positions recorded in ROW differ from current buffer
14070 positions.
14072 Return 0 if cursor is not on this row, 1 otherwise. */
14074 static int
14075 set_cursor_from_row (struct window *w, struct glyph_row *row,
14076 struct glyph_matrix *matrix,
14077 ptrdiff_t delta, ptrdiff_t delta_bytes,
14078 int dy, int dvpos)
14080 struct glyph *glyph = row->glyphs[TEXT_AREA];
14081 struct glyph *end = glyph + row->used[TEXT_AREA];
14082 struct glyph *cursor = NULL;
14083 /* The last known character position in row. */
14084 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14085 int x = row->x;
14086 ptrdiff_t pt_old = PT - delta;
14087 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14088 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14089 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14090 /* A glyph beyond the edge of TEXT_AREA which we should never
14091 touch. */
14092 struct glyph *glyphs_end = end;
14093 /* Non-zero means we've found a match for cursor position, but that
14094 glyph has the avoid_cursor_p flag set. */
14095 int match_with_avoid_cursor = 0;
14096 /* Non-zero means we've seen at least one glyph that came from a
14097 display string. */
14098 int string_seen = 0;
14099 /* Largest and smallest buffer positions seen so far during scan of
14100 glyph row. */
14101 ptrdiff_t bpos_max = pos_before;
14102 ptrdiff_t bpos_min = pos_after;
14103 /* Last buffer position covered by an overlay string with an integer
14104 `cursor' property. */
14105 ptrdiff_t bpos_covered = 0;
14106 /* Non-zero means the display string on which to display the cursor
14107 comes from a text property, not from an overlay. */
14108 int string_from_text_prop = 0;
14110 /* Don't even try doing anything if called for a mode-line or
14111 header-line row, since the rest of the code isn't prepared to
14112 deal with such calamities. */
14113 eassert (!row->mode_line_p);
14114 if (row->mode_line_p)
14115 return 0;
14117 /* Skip over glyphs not having an object at the start and the end of
14118 the row. These are special glyphs like truncation marks on
14119 terminal frames. */
14120 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14122 if (!row->reversed_p)
14124 while (glyph < end
14125 && INTEGERP (glyph->object)
14126 && glyph->charpos < 0)
14128 x += glyph->pixel_width;
14129 ++glyph;
14131 while (end > glyph
14132 && INTEGERP ((end - 1)->object)
14133 /* CHARPOS is zero for blanks and stretch glyphs
14134 inserted by extend_face_to_end_of_line. */
14135 && (end - 1)->charpos <= 0)
14136 --end;
14137 glyph_before = glyph - 1;
14138 glyph_after = end;
14140 else
14142 struct glyph *g;
14144 /* If the glyph row is reversed, we need to process it from back
14145 to front, so swap the edge pointers. */
14146 glyphs_end = end = glyph - 1;
14147 glyph += row->used[TEXT_AREA] - 1;
14149 while (glyph > end + 1
14150 && INTEGERP (glyph->object)
14151 && glyph->charpos < 0)
14153 --glyph;
14154 x -= glyph->pixel_width;
14156 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14157 --glyph;
14158 /* By default, in reversed rows we put the cursor on the
14159 rightmost (first in the reading order) glyph. */
14160 for (g = end + 1; g < glyph; g++)
14161 x += g->pixel_width;
14162 while (end < glyph
14163 && INTEGERP ((end + 1)->object)
14164 && (end + 1)->charpos <= 0)
14165 ++end;
14166 glyph_before = glyph + 1;
14167 glyph_after = end;
14170 else if (row->reversed_p)
14172 /* In R2L rows that don't display text, put the cursor on the
14173 rightmost glyph. Case in point: an empty last line that is
14174 part of an R2L paragraph. */
14175 cursor = end - 1;
14176 /* Avoid placing the cursor on the last glyph of the row, where
14177 on terminal frames we hold the vertical border between
14178 adjacent windows. */
14179 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14180 && !WINDOW_RIGHTMOST_P (w)
14181 && cursor == row->glyphs[LAST_AREA] - 1)
14182 cursor--;
14183 x = -1; /* will be computed below, at label compute_x */
14186 /* Step 1: Try to find the glyph whose character position
14187 corresponds to point. If that's not possible, find 2 glyphs
14188 whose character positions are the closest to point, one before
14189 point, the other after it. */
14190 if (!row->reversed_p)
14191 while (/* not marched to end of glyph row */
14192 glyph < end
14193 /* glyph was not inserted by redisplay for internal purposes */
14194 && !INTEGERP (glyph->object))
14196 if (BUFFERP (glyph->object))
14198 ptrdiff_t dpos = glyph->charpos - pt_old;
14200 if (glyph->charpos > bpos_max)
14201 bpos_max = glyph->charpos;
14202 if (glyph->charpos < bpos_min)
14203 bpos_min = glyph->charpos;
14204 if (!glyph->avoid_cursor_p)
14206 /* If we hit point, we've found the glyph on which to
14207 display the cursor. */
14208 if (dpos == 0)
14210 match_with_avoid_cursor = 0;
14211 break;
14213 /* See if we've found a better approximation to
14214 POS_BEFORE or to POS_AFTER. */
14215 if (0 > dpos && dpos > pos_before - pt_old)
14217 pos_before = glyph->charpos;
14218 glyph_before = glyph;
14220 else if (0 < dpos && dpos < pos_after - pt_old)
14222 pos_after = glyph->charpos;
14223 glyph_after = glyph;
14226 else if (dpos == 0)
14227 match_with_avoid_cursor = 1;
14229 else if (STRINGP (glyph->object))
14231 Lisp_Object chprop;
14232 ptrdiff_t glyph_pos = glyph->charpos;
14234 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14235 glyph->object);
14236 if (!NILP (chprop))
14238 /* If the string came from a `display' text property,
14239 look up the buffer position of that property and
14240 use that position to update bpos_max, as if we
14241 actually saw such a position in one of the row's
14242 glyphs. This helps with supporting integer values
14243 of `cursor' property on the display string in
14244 situations where most or all of the row's buffer
14245 text is completely covered by display properties,
14246 so that no glyph with valid buffer positions is
14247 ever seen in the row. */
14248 ptrdiff_t prop_pos =
14249 string_buffer_position_lim (glyph->object, pos_before,
14250 pos_after, 0);
14252 if (prop_pos >= pos_before)
14253 bpos_max = prop_pos - 1;
14255 if (INTEGERP (chprop))
14257 bpos_covered = bpos_max + XINT (chprop);
14258 /* If the `cursor' property covers buffer positions up
14259 to and including point, we should display cursor on
14260 this glyph. Note that, if a `cursor' property on one
14261 of the string's characters has an integer value, we
14262 will break out of the loop below _before_ we get to
14263 the position match above. IOW, integer values of
14264 the `cursor' property override the "exact match for
14265 point" strategy of positioning the cursor. */
14266 /* Implementation note: bpos_max == pt_old when, e.g.,
14267 we are in an empty line, where bpos_max is set to
14268 MATRIX_ROW_START_CHARPOS, see above. */
14269 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14271 cursor = glyph;
14272 break;
14276 string_seen = 1;
14278 x += glyph->pixel_width;
14279 ++glyph;
14281 else if (glyph > end) /* row is reversed */
14282 while (!INTEGERP (glyph->object))
14284 if (BUFFERP (glyph->object))
14286 ptrdiff_t dpos = glyph->charpos - pt_old;
14288 if (glyph->charpos > bpos_max)
14289 bpos_max = glyph->charpos;
14290 if (glyph->charpos < bpos_min)
14291 bpos_min = glyph->charpos;
14292 if (!glyph->avoid_cursor_p)
14294 if (dpos == 0)
14296 match_with_avoid_cursor = 0;
14297 break;
14299 if (0 > dpos && dpos > pos_before - pt_old)
14301 pos_before = glyph->charpos;
14302 glyph_before = glyph;
14304 else if (0 < dpos && dpos < pos_after - pt_old)
14306 pos_after = glyph->charpos;
14307 glyph_after = glyph;
14310 else if (dpos == 0)
14311 match_with_avoid_cursor = 1;
14313 else if (STRINGP (glyph->object))
14315 Lisp_Object chprop;
14316 ptrdiff_t glyph_pos = glyph->charpos;
14318 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14319 glyph->object);
14320 if (!NILP (chprop))
14322 ptrdiff_t prop_pos =
14323 string_buffer_position_lim (glyph->object, pos_before,
14324 pos_after, 0);
14326 if (prop_pos >= pos_before)
14327 bpos_max = prop_pos - 1;
14329 if (INTEGERP (chprop))
14331 bpos_covered = bpos_max + XINT (chprop);
14332 /* If the `cursor' property covers buffer positions up
14333 to and including point, we should display cursor on
14334 this glyph. */
14335 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14337 cursor = glyph;
14338 break;
14341 string_seen = 1;
14343 --glyph;
14344 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14346 x--; /* can't use any pixel_width */
14347 break;
14349 x -= glyph->pixel_width;
14352 /* Step 2: If we didn't find an exact match for point, we need to
14353 look for a proper place to put the cursor among glyphs between
14354 GLYPH_BEFORE and GLYPH_AFTER. */
14355 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14356 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14357 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14359 /* An empty line has a single glyph whose OBJECT is zero and
14360 whose CHARPOS is the position of a newline on that line.
14361 Note that on a TTY, there are more glyphs after that, which
14362 were produced by extend_face_to_end_of_line, but their
14363 CHARPOS is zero or negative. */
14364 int empty_line_p =
14365 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14366 && INTEGERP (glyph->object) && glyph->charpos > 0
14367 /* On a TTY, continued and truncated rows also have a glyph at
14368 their end whose OBJECT is zero and whose CHARPOS is
14369 positive (the continuation and truncation glyphs), but such
14370 rows are obviously not "empty". */
14371 && !(row->continued_p || row->truncated_on_right_p);
14373 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14375 ptrdiff_t ellipsis_pos;
14377 /* Scan back over the ellipsis glyphs. */
14378 if (!row->reversed_p)
14380 ellipsis_pos = (glyph - 1)->charpos;
14381 while (glyph > row->glyphs[TEXT_AREA]
14382 && (glyph - 1)->charpos == ellipsis_pos)
14383 glyph--, x -= glyph->pixel_width;
14384 /* That loop always goes one position too far, including
14385 the glyph before the ellipsis. So scan forward over
14386 that one. */
14387 x += glyph->pixel_width;
14388 glyph++;
14390 else /* row is reversed */
14392 ellipsis_pos = (glyph + 1)->charpos;
14393 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14394 && (glyph + 1)->charpos == ellipsis_pos)
14395 glyph++, x += glyph->pixel_width;
14396 x -= glyph->pixel_width;
14397 glyph--;
14400 else if (match_with_avoid_cursor)
14402 cursor = glyph_after;
14403 x = -1;
14405 else if (string_seen)
14407 int incr = row->reversed_p ? -1 : +1;
14409 /* Need to find the glyph that came out of a string which is
14410 present at point. That glyph is somewhere between
14411 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14412 positioned between POS_BEFORE and POS_AFTER in the
14413 buffer. */
14414 struct glyph *start, *stop;
14415 ptrdiff_t pos = pos_before;
14417 x = -1;
14419 /* If the row ends in a newline from a display string,
14420 reordering could have moved the glyphs belonging to the
14421 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14422 in this case we extend the search to the last glyph in
14423 the row that was not inserted by redisplay. */
14424 if (row->ends_in_newline_from_string_p)
14426 glyph_after = end;
14427 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14430 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14431 correspond to POS_BEFORE and POS_AFTER, respectively. We
14432 need START and STOP in the order that corresponds to the
14433 row's direction as given by its reversed_p flag. If the
14434 directionality of characters between POS_BEFORE and
14435 POS_AFTER is the opposite of the row's base direction,
14436 these characters will have been reordered for display,
14437 and we need to reverse START and STOP. */
14438 if (!row->reversed_p)
14440 start = min (glyph_before, glyph_after);
14441 stop = max (glyph_before, glyph_after);
14443 else
14445 start = max (glyph_before, glyph_after);
14446 stop = min (glyph_before, glyph_after);
14448 for (glyph = start + incr;
14449 row->reversed_p ? glyph > stop : glyph < stop; )
14452 /* Any glyphs that come from the buffer are here because
14453 of bidi reordering. Skip them, and only pay
14454 attention to glyphs that came from some string. */
14455 if (STRINGP (glyph->object))
14457 Lisp_Object str;
14458 ptrdiff_t tem;
14459 /* If the display property covers the newline, we
14460 need to search for it one position farther. */
14461 ptrdiff_t lim = pos_after
14462 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14464 string_from_text_prop = 0;
14465 str = glyph->object;
14466 tem = string_buffer_position_lim (str, pos, lim, 0);
14467 if (tem == 0 /* from overlay */
14468 || pos <= tem)
14470 /* If the string from which this glyph came is
14471 found in the buffer at point, or at position
14472 that is closer to point than pos_after, then
14473 we've found the glyph we've been looking for.
14474 If it comes from an overlay (tem == 0), and
14475 it has the `cursor' property on one of its
14476 glyphs, record that glyph as a candidate for
14477 displaying the cursor. (As in the
14478 unidirectional version, we will display the
14479 cursor on the last candidate we find.) */
14480 if (tem == 0
14481 || tem == pt_old
14482 || (tem - pt_old > 0 && tem < pos_after))
14484 /* The glyphs from this string could have
14485 been reordered. Find the one with the
14486 smallest string position. Or there could
14487 be a character in the string with the
14488 `cursor' property, which means display
14489 cursor on that character's glyph. */
14490 ptrdiff_t strpos = glyph->charpos;
14492 if (tem)
14494 cursor = glyph;
14495 string_from_text_prop = 1;
14497 for ( ;
14498 (row->reversed_p ? glyph > stop : glyph < stop)
14499 && EQ (glyph->object, str);
14500 glyph += incr)
14502 Lisp_Object cprop;
14503 ptrdiff_t gpos = glyph->charpos;
14505 cprop = Fget_char_property (make_number (gpos),
14506 Qcursor,
14507 glyph->object);
14508 if (!NILP (cprop))
14510 cursor = glyph;
14511 break;
14513 if (tem && glyph->charpos < strpos)
14515 strpos = glyph->charpos;
14516 cursor = glyph;
14520 if (tem == pt_old
14521 || (tem - pt_old > 0 && tem < pos_after))
14522 goto compute_x;
14524 if (tem)
14525 pos = tem + 1; /* don't find previous instances */
14527 /* This string is not what we want; skip all of the
14528 glyphs that came from it. */
14529 while ((row->reversed_p ? glyph > stop : glyph < stop)
14530 && EQ (glyph->object, str))
14531 glyph += incr;
14533 else
14534 glyph += incr;
14537 /* If we reached the end of the line, and END was from a string,
14538 the cursor is not on this line. */
14539 if (cursor == NULL
14540 && (row->reversed_p ? glyph <= end : glyph >= end)
14541 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14542 && STRINGP (end->object)
14543 && row->continued_p)
14544 return 0;
14546 /* A truncated row may not include PT among its character positions.
14547 Setting the cursor inside the scroll margin will trigger
14548 recalculation of hscroll in hscroll_window_tree. But if a
14549 display string covers point, defer to the string-handling
14550 code below to figure this out. */
14551 else if (row->truncated_on_left_p && pt_old < bpos_min)
14553 cursor = glyph_before;
14554 x = -1;
14556 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14557 /* Zero-width characters produce no glyphs. */
14558 || (!empty_line_p
14559 && (row->reversed_p
14560 ? glyph_after > glyphs_end
14561 : glyph_after < glyphs_end)))
14563 cursor = glyph_after;
14564 x = -1;
14568 compute_x:
14569 if (cursor != NULL)
14570 glyph = cursor;
14571 else if (glyph == glyphs_end
14572 && pos_before == pos_after
14573 && STRINGP ((row->reversed_p
14574 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14575 : row->glyphs[TEXT_AREA])->object))
14577 /* If all the glyphs of this row came from strings, put the
14578 cursor on the first glyph of the row. This avoids having the
14579 cursor outside of the text area in this very rare and hard
14580 use case. */
14581 glyph =
14582 row->reversed_p
14583 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14584 : row->glyphs[TEXT_AREA];
14586 if (x < 0)
14588 struct glyph *g;
14590 /* Need to compute x that corresponds to GLYPH. */
14591 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14593 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14594 emacs_abort ();
14595 x += g->pixel_width;
14599 /* ROW could be part of a continued line, which, under bidi
14600 reordering, might have other rows whose start and end charpos
14601 occlude point. Only set w->cursor if we found a better
14602 approximation to the cursor position than we have from previously
14603 examined candidate rows belonging to the same continued line. */
14604 if (/* We already have a candidate row. */
14605 w->cursor.vpos >= 0
14606 /* That candidate is not the row we are processing. */
14607 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14608 /* Make sure cursor.vpos specifies a row whose start and end
14609 charpos occlude point, and it is valid candidate for being a
14610 cursor-row. This is because some callers of this function
14611 leave cursor.vpos at the row where the cursor was displayed
14612 during the last redisplay cycle. */
14613 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14614 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14615 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14617 struct glyph *g1
14618 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14620 /* Don't consider glyphs that are outside TEXT_AREA. */
14621 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14622 return 0;
14623 /* Keep the candidate whose buffer position is the closest to
14624 point or has the `cursor' property. */
14625 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14626 w->cursor.hpos >= 0
14627 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14628 && ((BUFFERP (g1->object)
14629 && (g1->charpos == pt_old /* An exact match always wins. */
14630 || (BUFFERP (glyph->object)
14631 && eabs (g1->charpos - pt_old)
14632 < eabs (glyph->charpos - pt_old))))
14633 /* Previous candidate is a glyph from a string that has
14634 a non-nil `cursor' property. */
14635 || (STRINGP (g1->object)
14636 && (!NILP (Fget_char_property (make_number (g1->charpos),
14637 Qcursor, g1->object))
14638 /* Previous candidate is from the same display
14639 string as this one, and the display string
14640 came from a text property. */
14641 || (EQ (g1->object, glyph->object)
14642 && string_from_text_prop)
14643 /* this candidate is from newline and its
14644 position is not an exact match */
14645 || (INTEGERP (glyph->object)
14646 && glyph->charpos != pt_old)))))
14647 return 0;
14648 /* If this candidate gives an exact match, use that. */
14649 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14650 /* If this candidate is a glyph created for the
14651 terminating newline of a line, and point is on that
14652 newline, it wins because it's an exact match. */
14653 || (!row->continued_p
14654 && INTEGERP (glyph->object)
14655 && glyph->charpos == 0
14656 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14657 /* Otherwise, keep the candidate that comes from a row
14658 spanning less buffer positions. This may win when one or
14659 both candidate positions are on glyphs that came from
14660 display strings, for which we cannot compare buffer
14661 positions. */
14662 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14663 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14664 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14665 return 0;
14667 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14668 w->cursor.x = x;
14669 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14670 w->cursor.y = row->y + dy;
14672 if (w == XWINDOW (selected_window))
14674 if (!row->continued_p
14675 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14676 && row->x == 0)
14678 this_line_buffer = XBUFFER (w->contents);
14680 CHARPOS (this_line_start_pos)
14681 = MATRIX_ROW_START_CHARPOS (row) + delta;
14682 BYTEPOS (this_line_start_pos)
14683 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14685 CHARPOS (this_line_end_pos)
14686 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14687 BYTEPOS (this_line_end_pos)
14688 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14690 this_line_y = w->cursor.y;
14691 this_line_pixel_height = row->height;
14692 this_line_vpos = w->cursor.vpos;
14693 this_line_start_x = row->x;
14695 else
14696 CHARPOS (this_line_start_pos) = 0;
14699 return 1;
14703 /* Run window scroll functions, if any, for WINDOW with new window
14704 start STARTP. Sets the window start of WINDOW to that position.
14706 We assume that the window's buffer is really current. */
14708 static struct text_pos
14709 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14711 struct window *w = XWINDOW (window);
14712 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14714 eassert (current_buffer == XBUFFER (w->contents));
14716 if (!NILP (Vwindow_scroll_functions))
14718 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14719 make_number (CHARPOS (startp)));
14720 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14721 /* In case the hook functions switch buffers. */
14722 set_buffer_internal (XBUFFER (w->contents));
14725 return startp;
14729 /* Make sure the line containing the cursor is fully visible.
14730 A value of 1 means there is nothing to be done.
14731 (Either the line is fully visible, or it cannot be made so,
14732 or we cannot tell.)
14734 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14735 is higher than window.
14737 A value of 0 means the caller should do scrolling
14738 as if point had gone off the screen. */
14740 static int
14741 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14743 struct glyph_matrix *matrix;
14744 struct glyph_row *row;
14745 int window_height;
14747 if (!make_cursor_line_fully_visible_p)
14748 return 1;
14750 /* It's not always possible to find the cursor, e.g, when a window
14751 is full of overlay strings. Don't do anything in that case. */
14752 if (w->cursor.vpos < 0)
14753 return 1;
14755 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14756 row = MATRIX_ROW (matrix, w->cursor.vpos);
14758 /* If the cursor row is not partially visible, there's nothing to do. */
14759 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14760 return 1;
14762 /* If the row the cursor is in is taller than the window's height,
14763 it's not clear what to do, so do nothing. */
14764 window_height = window_box_height (w);
14765 if (row->height >= window_height)
14767 if (!force_p || MINI_WINDOW_P (w)
14768 || w->vscroll || w->cursor.vpos == 0)
14769 return 1;
14771 return 0;
14775 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14776 non-zero means only WINDOW is redisplayed in redisplay_internal.
14777 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14778 in redisplay_window to bring a partially visible line into view in
14779 the case that only the cursor has moved.
14781 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14782 last screen line's vertical height extends past the end of the screen.
14784 Value is
14786 1 if scrolling succeeded
14788 0 if scrolling didn't find point.
14790 -1 if new fonts have been loaded so that we must interrupt
14791 redisplay, adjust glyph matrices, and try again. */
14793 enum
14795 SCROLLING_SUCCESS,
14796 SCROLLING_FAILED,
14797 SCROLLING_NEED_LARGER_MATRICES
14800 /* If scroll-conservatively is more than this, never recenter.
14802 If you change this, don't forget to update the doc string of
14803 `scroll-conservatively' and the Emacs manual. */
14804 #define SCROLL_LIMIT 100
14806 static int
14807 try_scrolling (Lisp_Object window, int just_this_one_p,
14808 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14809 int temp_scroll_step, int last_line_misfit)
14811 struct window *w = XWINDOW (window);
14812 struct frame *f = XFRAME (w->frame);
14813 struct text_pos pos, startp;
14814 struct it it;
14815 int this_scroll_margin, scroll_max, rc, height;
14816 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14817 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14818 Lisp_Object aggressive;
14819 /* We will never try scrolling more than this number of lines. */
14820 int scroll_limit = SCROLL_LIMIT;
14821 int frame_line_height = default_line_pixel_height (w);
14822 int window_total_lines
14823 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14825 #ifdef GLYPH_DEBUG
14826 debug_method_add (w, "try_scrolling");
14827 #endif
14829 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14831 /* Compute scroll margin height in pixels. We scroll when point is
14832 within this distance from the top or bottom of the window. */
14833 if (scroll_margin > 0)
14834 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14835 * frame_line_height;
14836 else
14837 this_scroll_margin = 0;
14839 /* Force arg_scroll_conservatively to have a reasonable value, to
14840 avoid scrolling too far away with slow move_it_* functions. Note
14841 that the user can supply scroll-conservatively equal to
14842 `most-positive-fixnum', which can be larger than INT_MAX. */
14843 if (arg_scroll_conservatively > scroll_limit)
14845 arg_scroll_conservatively = scroll_limit + 1;
14846 scroll_max = scroll_limit * frame_line_height;
14848 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14849 /* Compute how much we should try to scroll maximally to bring
14850 point into view. */
14851 scroll_max = (max (scroll_step,
14852 max (arg_scroll_conservatively, temp_scroll_step))
14853 * frame_line_height);
14854 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14855 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14856 /* We're trying to scroll because of aggressive scrolling but no
14857 scroll_step is set. Choose an arbitrary one. */
14858 scroll_max = 10 * frame_line_height;
14859 else
14860 scroll_max = 0;
14862 too_near_end:
14864 /* Decide whether to scroll down. */
14865 if (PT > CHARPOS (startp))
14867 int scroll_margin_y;
14869 /* Compute the pixel ypos of the scroll margin, then move IT to
14870 either that ypos or PT, whichever comes first. */
14871 start_display (&it, w, startp);
14872 scroll_margin_y = it.last_visible_y - this_scroll_margin
14873 - frame_line_height * extra_scroll_margin_lines;
14874 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14875 (MOVE_TO_POS | MOVE_TO_Y));
14877 if (PT > CHARPOS (it.current.pos))
14879 int y0 = line_bottom_y (&it);
14880 /* Compute how many pixels below window bottom to stop searching
14881 for PT. This avoids costly search for PT that is far away if
14882 the user limited scrolling by a small number of lines, but
14883 always finds PT if scroll_conservatively is set to a large
14884 number, such as most-positive-fixnum. */
14885 int slack = max (scroll_max, 10 * frame_line_height);
14886 int y_to_move = it.last_visible_y + slack;
14888 /* Compute the distance from the scroll margin to PT or to
14889 the scroll limit, whichever comes first. This should
14890 include the height of the cursor line, to make that line
14891 fully visible. */
14892 move_it_to (&it, PT, -1, y_to_move,
14893 -1, MOVE_TO_POS | MOVE_TO_Y);
14894 dy = line_bottom_y (&it) - y0;
14896 if (dy > scroll_max)
14897 return SCROLLING_FAILED;
14899 if (dy > 0)
14900 scroll_down_p = 1;
14904 if (scroll_down_p)
14906 /* Point is in or below the bottom scroll margin, so move the
14907 window start down. If scrolling conservatively, move it just
14908 enough down to make point visible. If scroll_step is set,
14909 move it down by scroll_step. */
14910 if (arg_scroll_conservatively)
14911 amount_to_scroll
14912 = min (max (dy, frame_line_height),
14913 frame_line_height * arg_scroll_conservatively);
14914 else if (scroll_step || temp_scroll_step)
14915 amount_to_scroll = scroll_max;
14916 else
14918 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14919 height = WINDOW_BOX_TEXT_HEIGHT (w);
14920 if (NUMBERP (aggressive))
14922 double float_amount = XFLOATINT (aggressive) * height;
14923 int aggressive_scroll = float_amount;
14924 if (aggressive_scroll == 0 && float_amount > 0)
14925 aggressive_scroll = 1;
14926 /* Don't let point enter the scroll margin near top of
14927 the window. This could happen if the value of
14928 scroll_up_aggressively is too large and there are
14929 non-zero margins, because scroll_up_aggressively
14930 means put point that fraction of window height
14931 _from_the_bottom_margin_. */
14932 if (aggressive_scroll + 2*this_scroll_margin > height)
14933 aggressive_scroll = height - 2*this_scroll_margin;
14934 amount_to_scroll = dy + aggressive_scroll;
14938 if (amount_to_scroll <= 0)
14939 return SCROLLING_FAILED;
14941 start_display (&it, w, startp);
14942 if (arg_scroll_conservatively <= scroll_limit)
14943 move_it_vertically (&it, amount_to_scroll);
14944 else
14946 /* Extra precision for users who set scroll-conservatively
14947 to a large number: make sure the amount we scroll
14948 the window start is never less than amount_to_scroll,
14949 which was computed as distance from window bottom to
14950 point. This matters when lines at window top and lines
14951 below window bottom have different height. */
14952 struct it it1;
14953 void *it1data = NULL;
14954 /* We use a temporary it1 because line_bottom_y can modify
14955 its argument, if it moves one line down; see there. */
14956 int start_y;
14958 SAVE_IT (it1, it, it1data);
14959 start_y = line_bottom_y (&it1);
14960 do {
14961 RESTORE_IT (&it, &it, it1data);
14962 move_it_by_lines (&it, 1);
14963 SAVE_IT (it1, it, it1data);
14964 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14967 /* If STARTP is unchanged, move it down another screen line. */
14968 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14969 move_it_by_lines (&it, 1);
14970 startp = it.current.pos;
14972 else
14974 struct text_pos scroll_margin_pos = startp;
14975 int y_offset = 0;
14977 /* See if point is inside the scroll margin at the top of the
14978 window. */
14979 if (this_scroll_margin)
14981 int y_start;
14983 start_display (&it, w, startp);
14984 y_start = it.current_y;
14985 move_it_vertically (&it, this_scroll_margin);
14986 scroll_margin_pos = it.current.pos;
14987 /* If we didn't move enough before hitting ZV, request
14988 additional amount of scroll, to move point out of the
14989 scroll margin. */
14990 if (IT_CHARPOS (it) == ZV
14991 && it.current_y - y_start < this_scroll_margin)
14992 y_offset = this_scroll_margin - (it.current_y - y_start);
14995 if (PT < CHARPOS (scroll_margin_pos))
14997 /* Point is in the scroll margin at the top of the window or
14998 above what is displayed in the window. */
14999 int y0, y_to_move;
15001 /* Compute the vertical distance from PT to the scroll
15002 margin position. Move as far as scroll_max allows, or
15003 one screenful, or 10 screen lines, whichever is largest.
15004 Give up if distance is greater than scroll_max or if we
15005 didn't reach the scroll margin position. */
15006 SET_TEXT_POS (pos, PT, PT_BYTE);
15007 start_display (&it, w, pos);
15008 y0 = it.current_y;
15009 y_to_move = max (it.last_visible_y,
15010 max (scroll_max, 10 * frame_line_height));
15011 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15012 y_to_move, -1,
15013 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15014 dy = it.current_y - y0;
15015 if (dy > scroll_max
15016 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15017 return SCROLLING_FAILED;
15019 /* Additional scroll for when ZV was too close to point. */
15020 dy += y_offset;
15022 /* Compute new window start. */
15023 start_display (&it, w, startp);
15025 if (arg_scroll_conservatively)
15026 amount_to_scroll = max (dy, frame_line_height *
15027 max (scroll_step, temp_scroll_step));
15028 else if (scroll_step || temp_scroll_step)
15029 amount_to_scroll = scroll_max;
15030 else
15032 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15033 height = WINDOW_BOX_TEXT_HEIGHT (w);
15034 if (NUMBERP (aggressive))
15036 double float_amount = XFLOATINT (aggressive) * height;
15037 int aggressive_scroll = float_amount;
15038 if (aggressive_scroll == 0 && float_amount > 0)
15039 aggressive_scroll = 1;
15040 /* Don't let point enter the scroll margin near
15041 bottom of the window, if the value of
15042 scroll_down_aggressively happens to be too
15043 large. */
15044 if (aggressive_scroll + 2*this_scroll_margin > height)
15045 aggressive_scroll = height - 2*this_scroll_margin;
15046 amount_to_scroll = dy + aggressive_scroll;
15050 if (amount_to_scroll <= 0)
15051 return SCROLLING_FAILED;
15053 move_it_vertically_backward (&it, amount_to_scroll);
15054 startp = it.current.pos;
15058 /* Run window scroll functions. */
15059 startp = run_window_scroll_functions (window, startp);
15061 /* Display the window. Give up if new fonts are loaded, or if point
15062 doesn't appear. */
15063 if (!try_window (window, startp, 0))
15064 rc = SCROLLING_NEED_LARGER_MATRICES;
15065 else if (w->cursor.vpos < 0)
15067 clear_glyph_matrix (w->desired_matrix);
15068 rc = SCROLLING_FAILED;
15070 else
15072 /* Maybe forget recorded base line for line number display. */
15073 if (!just_this_one_p
15074 || current_buffer->clip_changed
15075 || BEG_UNCHANGED < CHARPOS (startp))
15076 w->base_line_number = 0;
15078 /* If cursor ends up on a partially visible line,
15079 treat that as being off the bottom of the screen. */
15080 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15081 /* It's possible that the cursor is on the first line of the
15082 buffer, which is partially obscured due to a vscroll
15083 (Bug#7537). In that case, avoid looping forever. */
15084 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15086 clear_glyph_matrix (w->desired_matrix);
15087 ++extra_scroll_margin_lines;
15088 goto too_near_end;
15090 rc = SCROLLING_SUCCESS;
15093 return rc;
15097 /* Compute a suitable window start for window W if display of W starts
15098 on a continuation line. Value is non-zero if a new window start
15099 was computed.
15101 The new window start will be computed, based on W's width, starting
15102 from the start of the continued line. It is the start of the
15103 screen line with the minimum distance from the old start W->start. */
15105 static int
15106 compute_window_start_on_continuation_line (struct window *w)
15108 struct text_pos pos, start_pos;
15109 int window_start_changed_p = 0;
15111 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15113 /* If window start is on a continuation line... Window start may be
15114 < BEGV in case there's invisible text at the start of the
15115 buffer (M-x rmail, for example). */
15116 if (CHARPOS (start_pos) > BEGV
15117 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15119 struct it it;
15120 struct glyph_row *row;
15122 /* Handle the case that the window start is out of range. */
15123 if (CHARPOS (start_pos) < BEGV)
15124 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15125 else if (CHARPOS (start_pos) > ZV)
15126 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15128 /* Find the start of the continued line. This should be fast
15129 because find_newline is fast (newline cache). */
15130 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15131 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15132 row, DEFAULT_FACE_ID);
15133 reseat_at_previous_visible_line_start (&it);
15135 /* If the line start is "too far" away from the window start,
15136 say it takes too much time to compute a new window start. */
15137 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15138 /* PXW: Do we need upper bounds here? */
15139 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15141 int min_distance, distance;
15143 /* Move forward by display lines to find the new window
15144 start. If window width was enlarged, the new start can
15145 be expected to be > the old start. If window width was
15146 decreased, the new window start will be < the old start.
15147 So, we're looking for the display line start with the
15148 minimum distance from the old window start. */
15149 pos = it.current.pos;
15150 min_distance = INFINITY;
15151 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15152 distance < min_distance)
15154 min_distance = distance;
15155 pos = it.current.pos;
15156 if (it.line_wrap == WORD_WRAP)
15158 /* Under WORD_WRAP, move_it_by_lines is likely to
15159 overshoot and stop not at the first, but the
15160 second character from the left margin. So in
15161 that case, we need a more tight control on the X
15162 coordinate of the iterator than move_it_by_lines
15163 promises in its contract. The method is to first
15164 go to the last (rightmost) visible character of a
15165 line, then move to the leftmost character on the
15166 next line in a separate call. */
15167 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15169 move_it_to (&it, ZV, 0,
15170 it.current_y + it.max_ascent + it.max_descent, -1,
15171 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15173 else
15174 move_it_by_lines (&it, 1);
15177 /* Set the window start there. */
15178 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15179 window_start_changed_p = 1;
15183 return window_start_changed_p;
15187 /* Try cursor movement in case text has not changed in window WINDOW,
15188 with window start STARTP. Value is
15190 CURSOR_MOVEMENT_SUCCESS if successful
15192 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15194 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15195 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15196 we want to scroll as if scroll-step were set to 1. See the code.
15198 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15199 which case we have to abort this redisplay, and adjust matrices
15200 first. */
15202 enum
15204 CURSOR_MOVEMENT_SUCCESS,
15205 CURSOR_MOVEMENT_CANNOT_BE_USED,
15206 CURSOR_MOVEMENT_MUST_SCROLL,
15207 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15210 static int
15211 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15213 struct window *w = XWINDOW (window);
15214 struct frame *f = XFRAME (w->frame);
15215 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15217 #ifdef GLYPH_DEBUG
15218 if (inhibit_try_cursor_movement)
15219 return rc;
15220 #endif
15222 /* Previously, there was a check for Lisp integer in the
15223 if-statement below. Now, this field is converted to
15224 ptrdiff_t, thus zero means invalid position in a buffer. */
15225 eassert (w->last_point > 0);
15226 /* Likewise there was a check whether window_end_vpos is nil or larger
15227 than the window. Now window_end_vpos is int and so never nil, but
15228 let's leave eassert to check whether it fits in the window. */
15229 eassert (w->window_end_vpos < w->current_matrix->nrows);
15231 /* Handle case where text has not changed, only point, and it has
15232 not moved off the frame. */
15233 if (/* Point may be in this window. */
15234 PT >= CHARPOS (startp)
15235 /* Selective display hasn't changed. */
15236 && !current_buffer->clip_changed
15237 /* Function force-mode-line-update is used to force a thorough
15238 redisplay. It sets either windows_or_buffers_changed or
15239 update_mode_lines. So don't take a shortcut here for these
15240 cases. */
15241 && !update_mode_lines
15242 && !windows_or_buffers_changed
15243 && !f->cursor_type_changed
15244 && NILP (Vshow_trailing_whitespace)
15245 /* This code is not used for mini-buffer for the sake of the case
15246 of redisplaying to replace an echo area message; since in
15247 that case the mini-buffer contents per se are usually
15248 unchanged. This code is of no real use in the mini-buffer
15249 since the handling of this_line_start_pos, etc., in redisplay
15250 handles the same cases. */
15251 && !EQ (window, minibuf_window)
15252 && (FRAME_WINDOW_P (f)
15253 || !overlay_arrow_in_current_buffer_p ()))
15255 int this_scroll_margin, top_scroll_margin;
15256 struct glyph_row *row = NULL;
15257 int frame_line_height = default_line_pixel_height (w);
15258 int window_total_lines
15259 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15261 #ifdef GLYPH_DEBUG
15262 debug_method_add (w, "cursor movement");
15263 #endif
15265 /* Scroll if point within this distance from the top or bottom
15266 of the window. This is a pixel value. */
15267 if (scroll_margin > 0)
15269 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15270 this_scroll_margin *= frame_line_height;
15272 else
15273 this_scroll_margin = 0;
15275 top_scroll_margin = this_scroll_margin;
15276 if (WINDOW_WANTS_HEADER_LINE_P (w))
15277 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15279 /* Start with the row the cursor was displayed during the last
15280 not paused redisplay. Give up if that row is not valid. */
15281 if (w->last_cursor_vpos < 0
15282 || w->last_cursor_vpos >= w->current_matrix->nrows)
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else
15286 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15287 if (row->mode_line_p)
15288 ++row;
15289 if (!row->enabled_p)
15290 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15293 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15295 int scroll_p = 0, must_scroll = 0;
15296 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15298 if (PT > w->last_point)
15300 /* Point has moved forward. */
15301 while (MATRIX_ROW_END_CHARPOS (row) < PT
15302 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15304 eassert (row->enabled_p);
15305 ++row;
15308 /* If the end position of a row equals the start
15309 position of the next row, and PT is at that position,
15310 we would rather display cursor in the next line. */
15311 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15312 && MATRIX_ROW_END_CHARPOS (row) == PT
15313 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15314 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15315 && !cursor_row_p (row))
15316 ++row;
15318 /* If within the scroll margin, scroll. Note that
15319 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15320 the next line would be drawn, and that
15321 this_scroll_margin can be zero. */
15322 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15323 || PT > MATRIX_ROW_END_CHARPOS (row)
15324 /* Line is completely visible last line in window
15325 and PT is to be set in the next line. */
15326 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15327 && PT == MATRIX_ROW_END_CHARPOS (row)
15328 && !row->ends_at_zv_p
15329 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15330 scroll_p = 1;
15332 else if (PT < w->last_point)
15334 /* Cursor has to be moved backward. Note that PT >=
15335 CHARPOS (startp) because of the outer if-statement. */
15336 while (!row->mode_line_p
15337 && (MATRIX_ROW_START_CHARPOS (row) > PT
15338 || (MATRIX_ROW_START_CHARPOS (row) == PT
15339 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15340 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15341 row > w->current_matrix->rows
15342 && (row-1)->ends_in_newline_from_string_p))))
15343 && (row->y > top_scroll_margin
15344 || CHARPOS (startp) == BEGV))
15346 eassert (row->enabled_p);
15347 --row;
15350 /* Consider the following case: Window starts at BEGV,
15351 there is invisible, intangible text at BEGV, so that
15352 display starts at some point START > BEGV. It can
15353 happen that we are called with PT somewhere between
15354 BEGV and START. Try to handle that case. */
15355 if (row < w->current_matrix->rows
15356 || row->mode_line_p)
15358 row = w->current_matrix->rows;
15359 if (row->mode_line_p)
15360 ++row;
15363 /* Due to newlines in overlay strings, we may have to
15364 skip forward over overlay strings. */
15365 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15366 && MATRIX_ROW_END_CHARPOS (row) == PT
15367 && !cursor_row_p (row))
15368 ++row;
15370 /* If within the scroll margin, scroll. */
15371 if (row->y < top_scroll_margin
15372 && CHARPOS (startp) != BEGV)
15373 scroll_p = 1;
15375 else
15377 /* Cursor did not move. So don't scroll even if cursor line
15378 is partially visible, as it was so before. */
15379 rc = CURSOR_MOVEMENT_SUCCESS;
15382 if (PT < MATRIX_ROW_START_CHARPOS (row)
15383 || PT > MATRIX_ROW_END_CHARPOS (row))
15385 /* if PT is not in the glyph row, give up. */
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 must_scroll = 1;
15389 else if (rc != CURSOR_MOVEMENT_SUCCESS
15390 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15392 struct glyph_row *row1;
15394 /* If rows are bidi-reordered and point moved, back up
15395 until we find a row that does not belong to a
15396 continuation line. This is because we must consider
15397 all rows of a continued line as candidates for the
15398 new cursor positioning, since row start and end
15399 positions change non-linearly with vertical position
15400 in such rows. */
15401 /* FIXME: Revisit this when glyph ``spilling'' in
15402 continuation lines' rows is implemented for
15403 bidi-reordered rows. */
15404 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15405 MATRIX_ROW_CONTINUATION_LINE_P (row);
15406 --row)
15408 /* If we hit the beginning of the displayed portion
15409 without finding the first row of a continued
15410 line, give up. */
15411 if (row <= row1)
15413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15414 break;
15416 eassert (row->enabled_p);
15419 if (must_scroll)
15421 else if (rc != CURSOR_MOVEMENT_SUCCESS
15422 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15423 /* Make sure this isn't a header line by any chance, since
15424 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15425 && !row->mode_line_p
15426 && make_cursor_line_fully_visible_p)
15428 if (PT == MATRIX_ROW_END_CHARPOS (row)
15429 && !row->ends_at_zv_p
15430 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 else if (row->height > window_box_height (w))
15434 /* If we end up in a partially visible line, let's
15435 make it fully visible, except when it's taller
15436 than the window, in which case we can't do much
15437 about it. */
15438 *scroll_step = 1;
15439 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15441 else
15443 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15444 if (!cursor_row_fully_visible_p (w, 0, 1))
15445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15446 else
15447 rc = CURSOR_MOVEMENT_SUCCESS;
15450 else if (scroll_p)
15451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15452 else if (rc != CURSOR_MOVEMENT_SUCCESS
15453 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15455 /* With bidi-reordered rows, there could be more than
15456 one candidate row whose start and end positions
15457 occlude point. We need to let set_cursor_from_row
15458 find the best candidate. */
15459 /* FIXME: Revisit this when glyph ``spilling'' in
15460 continuation lines' rows is implemented for
15461 bidi-reordered rows. */
15462 int rv = 0;
15466 int at_zv_p = 0, exact_match_p = 0;
15468 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15469 && PT <= MATRIX_ROW_END_CHARPOS (row)
15470 && cursor_row_p (row))
15471 rv |= set_cursor_from_row (w, row, w->current_matrix,
15472 0, 0, 0, 0);
15473 /* As soon as we've found the exact match for point,
15474 or the first suitable row whose ends_at_zv_p flag
15475 is set, we are done. */
15476 at_zv_p =
15477 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15478 if (rv && !at_zv_p
15479 && w->cursor.hpos >= 0
15480 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15481 w->cursor.vpos))
15483 struct glyph_row *candidate =
15484 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15485 struct glyph *g =
15486 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15487 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15489 exact_match_p =
15490 (BUFFERP (g->object) && g->charpos == PT)
15491 || (INTEGERP (g->object)
15492 && (g->charpos == PT
15493 || (g->charpos == 0 && endpos - 1 == PT)));
15495 if (rv && (at_zv_p || exact_match_p))
15497 rc = CURSOR_MOVEMENT_SUCCESS;
15498 break;
15500 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15501 break;
15502 ++row;
15504 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15505 || row->continued_p)
15506 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15507 || (MATRIX_ROW_START_CHARPOS (row) == PT
15508 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15509 /* If we didn't find any candidate rows, or exited the
15510 loop before all the candidates were examined, signal
15511 to the caller that this method failed. */
15512 if (rc != CURSOR_MOVEMENT_SUCCESS
15513 && !(rv
15514 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15515 && !row->continued_p))
15516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15517 else if (rv)
15518 rc = CURSOR_MOVEMENT_SUCCESS;
15520 else
15524 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15526 rc = CURSOR_MOVEMENT_SUCCESS;
15527 break;
15529 ++row;
15531 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15532 && MATRIX_ROW_START_CHARPOS (row) == PT
15533 && cursor_row_p (row));
15538 return rc;
15541 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15542 static
15543 #endif
15544 void
15545 set_vertical_scroll_bar (struct window *w)
15547 ptrdiff_t start, end, whole;
15549 /* Calculate the start and end positions for the current window.
15550 At some point, it would be nice to choose between scrollbars
15551 which reflect the whole buffer size, with special markers
15552 indicating narrowing, and scrollbars which reflect only the
15553 visible region.
15555 Note that mini-buffers sometimes aren't displaying any text. */
15556 if (!MINI_WINDOW_P (w)
15557 || (w == XWINDOW (minibuf_window)
15558 && NILP (echo_area_buffer[0])))
15560 struct buffer *buf = XBUFFER (w->contents);
15561 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15562 start = marker_position (w->start) - BUF_BEGV (buf);
15563 /* I don't think this is guaranteed to be right. For the
15564 moment, we'll pretend it is. */
15565 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15567 if (end < start)
15568 end = start;
15569 if (whole < (end - start))
15570 whole = end - start;
15572 else
15573 start = end = whole = 0;
15575 /* Indicate what this scroll bar ought to be displaying now. */
15576 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15577 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15578 (w, end - start, whole, start);
15582 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15583 selected_window is redisplayed.
15585 We can return without actually redisplaying the window if fonts has been
15586 changed on window's frame. In that case, redisplay_internal will retry. */
15588 static void
15589 redisplay_window (Lisp_Object window, bool just_this_one_p)
15591 struct window *w = XWINDOW (window);
15592 struct frame *f = XFRAME (w->frame);
15593 struct buffer *buffer = XBUFFER (w->contents);
15594 struct buffer *old = current_buffer;
15595 struct text_pos lpoint, opoint, startp;
15596 int update_mode_line;
15597 int tem;
15598 struct it it;
15599 /* Record it now because it's overwritten. */
15600 bool current_matrix_up_to_date_p = false;
15601 bool used_current_matrix_p = false;
15602 /* This is less strict than current_matrix_up_to_date_p.
15603 It indicates that the buffer contents and narrowing are unchanged. */
15604 bool buffer_unchanged_p = false;
15605 int temp_scroll_step = 0;
15606 ptrdiff_t count = SPECPDL_INDEX ();
15607 int rc;
15608 int centering_position = -1;
15609 int last_line_misfit = 0;
15610 ptrdiff_t beg_unchanged, end_unchanged;
15611 int frame_line_height;
15613 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15614 opoint = lpoint;
15616 #ifdef GLYPH_DEBUG
15617 *w->desired_matrix->method = 0;
15618 #endif
15620 if (!just_this_one_p
15621 && REDISPLAY_SOME_P ()
15622 && !w->redisplay
15623 && !f->redisplay
15624 && !buffer->text->redisplay)
15625 return;
15627 /* Make sure that both W's markers are valid. */
15628 eassert (XMARKER (w->start)->buffer == buffer);
15629 eassert (XMARKER (w->pointm)->buffer == buffer);
15631 restart:
15632 reconsider_clip_changes (w);
15633 frame_line_height = default_line_pixel_height (w);
15635 /* Has the mode line to be updated? */
15636 update_mode_line = (w->update_mode_line
15637 || update_mode_lines
15638 || buffer->clip_changed
15639 || buffer->prevent_redisplay_optimizations_p);
15641 if (!just_this_one_p)
15642 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15643 cleverly elsewhere. */
15644 w->must_be_updated_p = true;
15646 if (MINI_WINDOW_P (w))
15648 if (w == XWINDOW (echo_area_window)
15649 && !NILP (echo_area_buffer[0]))
15651 if (update_mode_line)
15652 /* We may have to update a tty frame's menu bar or a
15653 tool-bar. Example `M-x C-h C-h C-g'. */
15654 goto finish_menu_bars;
15655 else
15656 /* We've already displayed the echo area glyphs in this window. */
15657 goto finish_scroll_bars;
15659 else if ((w != XWINDOW (minibuf_window)
15660 || minibuf_level == 0)
15661 /* When buffer is nonempty, redisplay window normally. */
15662 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15663 /* Quail displays non-mini buffers in minibuffer window.
15664 In that case, redisplay the window normally. */
15665 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15667 /* W is a mini-buffer window, but it's not active, so clear
15668 it. */
15669 int yb = window_text_bottom_y (w);
15670 struct glyph_row *row;
15671 int y;
15673 for (y = 0, row = w->desired_matrix->rows;
15674 y < yb;
15675 y += row->height, ++row)
15676 blank_row (w, row, y);
15677 goto finish_scroll_bars;
15680 clear_glyph_matrix (w->desired_matrix);
15683 /* Otherwise set up data on this window; select its buffer and point
15684 value. */
15685 /* Really select the buffer, for the sake of buffer-local
15686 variables. */
15687 set_buffer_internal_1 (XBUFFER (w->contents));
15689 current_matrix_up_to_date_p
15690 = (w->window_end_valid
15691 && !current_buffer->clip_changed
15692 && !current_buffer->prevent_redisplay_optimizations_p
15693 && !window_outdated (w));
15695 /* Run the window-bottom-change-functions
15696 if it is possible that the text on the screen has changed
15697 (either due to modification of the text, or any other reason). */
15698 if (!current_matrix_up_to_date_p
15699 && !NILP (Vwindow_text_change_functions))
15701 safe_run_hooks (Qwindow_text_change_functions);
15702 goto restart;
15705 beg_unchanged = BEG_UNCHANGED;
15706 end_unchanged = END_UNCHANGED;
15708 SET_TEXT_POS (opoint, PT, PT_BYTE);
15710 specbind (Qinhibit_point_motion_hooks, Qt);
15712 buffer_unchanged_p
15713 = (w->window_end_valid
15714 && !current_buffer->clip_changed
15715 && !window_outdated (w));
15717 /* When windows_or_buffers_changed is non-zero, we can't rely
15718 on the window end being valid, so set it to zero there. */
15719 if (windows_or_buffers_changed)
15721 /* If window starts on a continuation line, maybe adjust the
15722 window start in case the window's width changed. */
15723 if (XMARKER (w->start)->buffer == current_buffer)
15724 compute_window_start_on_continuation_line (w);
15726 w->window_end_valid = false;
15727 /* If so, we also can't rely on current matrix
15728 and should not fool try_cursor_movement below. */
15729 current_matrix_up_to_date_p = false;
15732 /* Some sanity checks. */
15733 CHECK_WINDOW_END (w);
15734 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15735 emacs_abort ();
15736 if (BYTEPOS (opoint) < CHARPOS (opoint))
15737 emacs_abort ();
15739 if (mode_line_update_needed (w))
15740 update_mode_line = 1;
15742 /* Point refers normally to the selected window. For any other
15743 window, set up appropriate value. */
15744 if (!EQ (window, selected_window))
15746 ptrdiff_t new_pt = marker_position (w->pointm);
15747 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15748 if (new_pt < BEGV)
15750 new_pt = BEGV;
15751 new_pt_byte = BEGV_BYTE;
15752 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15754 else if (new_pt > (ZV - 1))
15756 new_pt = ZV;
15757 new_pt_byte = ZV_BYTE;
15758 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15761 /* We don't use SET_PT so that the point-motion hooks don't run. */
15762 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15765 /* If any of the character widths specified in the display table
15766 have changed, invalidate the width run cache. It's true that
15767 this may be a bit late to catch such changes, but the rest of
15768 redisplay goes (non-fatally) haywire when the display table is
15769 changed, so why should we worry about doing any better? */
15770 if (current_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 invalidate_region_cache (current_buffer,
15778 current_buffer->width_run_cache,
15779 BEG, Z);
15780 recompute_width_table (current_buffer, disptab);
15784 /* If window-start is screwed up, choose a new one. */
15785 if (XMARKER (w->start)->buffer != current_buffer)
15786 goto recenter;
15788 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15790 /* If someone specified a new starting point but did not insist,
15791 check whether it can be used. */
15792 if (w->optional_new_start
15793 && CHARPOS (startp) >= BEGV
15794 && CHARPOS (startp) <= ZV)
15796 w->optional_new_start = 0;
15797 start_display (&it, w, startp);
15798 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15799 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15800 if (IT_CHARPOS (it) == PT)
15801 w->force_start = 1;
15802 /* IT may overshoot PT if text at PT is invisible. */
15803 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15804 w->force_start = 1;
15807 force_start:
15809 /* Handle case where place to start displaying has been specified,
15810 unless the specified location is outside the accessible range. */
15811 if (w->force_start || window_frozen_p (w))
15813 /* We set this later on if we have to adjust point. */
15814 int new_vpos = -1;
15816 w->force_start = 0;
15817 w->vscroll = 0;
15818 w->window_end_valid = 0;
15820 /* Forget any recorded base line for line number display. */
15821 if (!buffer_unchanged_p)
15822 w->base_line_number = 0;
15824 /* Redisplay the mode line. Select the buffer properly for that.
15825 Also, run the hook window-scroll-functions
15826 because we have scrolled. */
15827 /* Note, we do this after clearing force_start because
15828 if there's an error, it is better to forget about force_start
15829 than to get into an infinite loop calling the hook functions
15830 and having them get more errors. */
15831 if (!update_mode_line
15832 || ! NILP (Vwindow_scroll_functions))
15834 update_mode_line = 1;
15835 w->update_mode_line = 1;
15836 startp = run_window_scroll_functions (window, startp);
15839 if (CHARPOS (startp) < BEGV)
15840 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15841 else if (CHARPOS (startp) > ZV)
15842 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15844 /* Redisplay, then check if cursor has been set during the
15845 redisplay. Give up if new fonts were loaded. */
15846 /* We used to issue a CHECK_MARGINS argument to try_window here,
15847 but this causes scrolling to fail when point begins inside
15848 the scroll margin (bug#148) -- cyd */
15849 if (!try_window (window, startp, 0))
15851 w->force_start = 1;
15852 clear_glyph_matrix (w->desired_matrix);
15853 goto need_larger_matrices;
15856 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15858 /* If point does not appear, try to move point so it does
15859 appear. The desired matrix has been built above, so we
15860 can use it here. */
15861 new_vpos = window_box_height (w) / 2;
15864 if (!cursor_row_fully_visible_p (w, 0, 0))
15866 /* Point does appear, but on a line partly visible at end of window.
15867 Move it back to a fully-visible line. */
15868 new_vpos = window_box_height (w);
15870 else if (w->cursor.vpos >= 0)
15872 /* Some people insist on not letting point enter the scroll
15873 margin, even though this part handles windows that didn't
15874 scroll at all. */
15875 int window_total_lines
15876 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15877 int margin = min (scroll_margin, window_total_lines / 4);
15878 int pixel_margin = margin * frame_line_height;
15879 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15881 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15882 below, which finds the row to move point to, advances by
15883 the Y coordinate of the _next_ row, see the definition of
15884 MATRIX_ROW_BOTTOM_Y. */
15885 if (w->cursor.vpos < margin + header_line)
15887 w->cursor.vpos = -1;
15888 clear_glyph_matrix (w->desired_matrix);
15889 goto try_to_scroll;
15891 else
15893 int window_height = window_box_height (w);
15895 if (header_line)
15896 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15897 if (w->cursor.y >= window_height - pixel_margin)
15899 w->cursor.vpos = -1;
15900 clear_glyph_matrix (w->desired_matrix);
15901 goto try_to_scroll;
15906 /* If we need to move point for either of the above reasons,
15907 now actually do it. */
15908 if (new_vpos >= 0)
15910 struct glyph_row *row;
15912 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15913 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15914 ++row;
15916 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15917 MATRIX_ROW_START_BYTEPOS (row));
15919 if (w != XWINDOW (selected_window))
15920 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15921 else if (current_buffer == old)
15922 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15924 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15926 /* If we are highlighting the region, then we just changed
15927 the region, so redisplay to show it. */
15928 /* FIXME: We need to (re)run pre-redisplay-function! */
15929 /* if (markpos_of_region () >= 0)
15931 clear_glyph_matrix (w->desired_matrix);
15932 if (!try_window (window, startp, 0))
15933 goto need_larger_matrices;
15938 #ifdef GLYPH_DEBUG
15939 debug_method_add (w, "forced window start");
15940 #endif
15941 goto done;
15944 /* Handle case where text has not changed, only point, and it has
15945 not moved off the frame, and we are not retrying after hscroll.
15946 (current_matrix_up_to_date_p is nonzero when retrying.) */
15947 if (current_matrix_up_to_date_p
15948 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15949 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15951 switch (rc)
15953 case CURSOR_MOVEMENT_SUCCESS:
15954 used_current_matrix_p = 1;
15955 goto done;
15957 case CURSOR_MOVEMENT_MUST_SCROLL:
15958 goto try_to_scroll;
15960 default:
15961 emacs_abort ();
15964 /* If current starting point was originally the beginning of a line
15965 but no longer is, find a new starting point. */
15966 else if (w->start_at_line_beg
15967 && !(CHARPOS (startp) <= BEGV
15968 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15970 #ifdef GLYPH_DEBUG
15971 debug_method_add (w, "recenter 1");
15972 #endif
15973 goto recenter;
15976 /* Try scrolling with try_window_id. Value is > 0 if update has
15977 been done, it is -1 if we know that the same window start will
15978 not work. It is 0 if unsuccessful for some other reason. */
15979 else if ((tem = try_window_id (w)) != 0)
15981 #ifdef GLYPH_DEBUG
15982 debug_method_add (w, "try_window_id %d", tem);
15983 #endif
15985 if (f->fonts_changed)
15986 goto need_larger_matrices;
15987 if (tem > 0)
15988 goto done;
15990 /* Otherwise try_window_id has returned -1 which means that we
15991 don't want the alternative below this comment to execute. */
15993 else if (CHARPOS (startp) >= BEGV
15994 && CHARPOS (startp) <= ZV
15995 && PT >= CHARPOS (startp)
15996 && (CHARPOS (startp) < ZV
15997 /* Avoid starting at end of buffer. */
15998 || CHARPOS (startp) == BEGV
15999 || !window_outdated (w)))
16001 int d1, d2, d3, d4, d5, d6;
16003 /* If first window line is a continuation line, and window start
16004 is inside the modified region, but the first change is before
16005 current window start, we must select a new window start.
16007 However, if this is the result of a down-mouse event (e.g. by
16008 extending the mouse-drag-overlay), we don't want to select a
16009 new window start, since that would change the position under
16010 the mouse, resulting in an unwanted mouse-movement rather
16011 than a simple mouse-click. */
16012 if (!w->start_at_line_beg
16013 && NILP (do_mouse_tracking)
16014 && CHARPOS (startp) > BEGV
16015 && CHARPOS (startp) > BEG + beg_unchanged
16016 && CHARPOS (startp) <= Z - end_unchanged
16017 /* Even if w->start_at_line_beg is nil, a new window may
16018 start at a line_beg, since that's how set_buffer_window
16019 sets it. So, we need to check the return value of
16020 compute_window_start_on_continuation_line. (See also
16021 bug#197). */
16022 && XMARKER (w->start)->buffer == current_buffer
16023 && compute_window_start_on_continuation_line (w)
16024 /* It doesn't make sense to force the window start like we
16025 do at label force_start if it is already known that point
16026 will not be visible in the resulting window, because
16027 doing so will move point from its correct position
16028 instead of scrolling the window to bring point into view.
16029 See bug#9324. */
16030 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16032 w->force_start = 1;
16033 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16034 goto force_start;
16037 #ifdef GLYPH_DEBUG
16038 debug_method_add (w, "same window start");
16039 #endif
16041 /* Try to redisplay starting at same place as before.
16042 If point has not moved off frame, accept the results. */
16043 if (!current_matrix_up_to_date_p
16044 /* Don't use try_window_reusing_current_matrix in this case
16045 because a window scroll function can have changed the
16046 buffer. */
16047 || !NILP (Vwindow_scroll_functions)
16048 || MINI_WINDOW_P (w)
16049 || !(used_current_matrix_p
16050 = try_window_reusing_current_matrix (w)))
16052 IF_DEBUG (debug_method_add (w, "1"));
16053 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16054 /* -1 means we need to scroll.
16055 0 means we need new matrices, but fonts_changed
16056 is set in that case, so we will detect it below. */
16057 goto try_to_scroll;
16060 if (f->fonts_changed)
16061 goto need_larger_matrices;
16063 if (w->cursor.vpos >= 0)
16065 if (!just_this_one_p
16066 || current_buffer->clip_changed
16067 || BEG_UNCHANGED < CHARPOS (startp))
16068 /* Forget any recorded base line for line number display. */
16069 w->base_line_number = 0;
16071 if (!cursor_row_fully_visible_p (w, 1, 0))
16073 clear_glyph_matrix (w->desired_matrix);
16074 last_line_misfit = 1;
16076 /* Drop through and scroll. */
16077 else
16078 goto done;
16080 else
16081 clear_glyph_matrix (w->desired_matrix);
16084 try_to_scroll:
16086 /* Redisplay the mode line. Select the buffer properly for that. */
16087 if (!update_mode_line)
16089 update_mode_line = 1;
16090 w->update_mode_line = 1;
16093 /* Try to scroll by specified few lines. */
16094 if ((scroll_conservatively
16095 || emacs_scroll_step
16096 || temp_scroll_step
16097 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16098 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16099 && CHARPOS (startp) >= BEGV
16100 && CHARPOS (startp) <= ZV)
16102 /* The function returns -1 if new fonts were loaded, 1 if
16103 successful, 0 if not successful. */
16104 int ss = try_scrolling (window, just_this_one_p,
16105 scroll_conservatively,
16106 emacs_scroll_step,
16107 temp_scroll_step, last_line_misfit);
16108 switch (ss)
16110 case SCROLLING_SUCCESS:
16111 goto done;
16113 case SCROLLING_NEED_LARGER_MATRICES:
16114 goto need_larger_matrices;
16116 case SCROLLING_FAILED:
16117 break;
16119 default:
16120 emacs_abort ();
16124 /* Finally, just choose a place to start which positions point
16125 according to user preferences. */
16127 recenter:
16129 #ifdef GLYPH_DEBUG
16130 debug_method_add (w, "recenter");
16131 #endif
16133 /* Forget any previously recorded base line for line number display. */
16134 if (!buffer_unchanged_p)
16135 w->base_line_number = 0;
16137 /* Determine the window start relative to point. */
16138 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16139 it.current_y = it.last_visible_y;
16140 if (centering_position < 0)
16142 int window_total_lines
16143 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16144 int margin =
16145 scroll_margin > 0
16146 ? min (scroll_margin, window_total_lines / 4)
16147 : 0;
16148 ptrdiff_t margin_pos = CHARPOS (startp);
16149 Lisp_Object aggressive;
16150 int scrolling_up;
16152 /* If there is a scroll margin at the top of the window, find
16153 its character position. */
16154 if (margin
16155 /* Cannot call start_display if startp is not in the
16156 accessible region of the buffer. This can happen when we
16157 have just switched to a different buffer and/or changed
16158 its restriction. In that case, startp is initialized to
16159 the character position 1 (BEGV) because we did not yet
16160 have chance to display the buffer even once. */
16161 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16163 struct it it1;
16164 void *it1data = NULL;
16166 SAVE_IT (it1, it, it1data);
16167 start_display (&it1, w, startp);
16168 move_it_vertically (&it1, margin * frame_line_height);
16169 margin_pos = IT_CHARPOS (it1);
16170 RESTORE_IT (&it, &it, it1data);
16172 scrolling_up = PT > margin_pos;
16173 aggressive =
16174 scrolling_up
16175 ? BVAR (current_buffer, scroll_up_aggressively)
16176 : BVAR (current_buffer, scroll_down_aggressively);
16178 if (!MINI_WINDOW_P (w)
16179 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16181 int pt_offset = 0;
16183 /* Setting scroll-conservatively overrides
16184 scroll-*-aggressively. */
16185 if (!scroll_conservatively && NUMBERP (aggressive))
16187 double float_amount = XFLOATINT (aggressive);
16189 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16190 if (pt_offset == 0 && float_amount > 0)
16191 pt_offset = 1;
16192 if (pt_offset && margin > 0)
16193 margin -= 1;
16195 /* Compute how much to move the window start backward from
16196 point so that point will be displayed where the user
16197 wants it. */
16198 if (scrolling_up)
16200 centering_position = it.last_visible_y;
16201 if (pt_offset)
16202 centering_position -= pt_offset;
16203 centering_position -=
16204 frame_line_height * (1 + margin + (last_line_misfit != 0))
16205 + WINDOW_HEADER_LINE_HEIGHT (w);
16206 /* Don't let point enter the scroll margin near top of
16207 the window. */
16208 if (centering_position < margin * frame_line_height)
16209 centering_position = margin * frame_line_height;
16211 else
16212 centering_position = margin * frame_line_height + pt_offset;
16214 else
16215 /* Set the window start half the height of the window backward
16216 from point. */
16217 centering_position = window_box_height (w) / 2;
16219 move_it_vertically_backward (&it, centering_position);
16221 eassert (IT_CHARPOS (it) >= BEGV);
16223 /* The function move_it_vertically_backward may move over more
16224 than the specified y-distance. If it->w is small, e.g. a
16225 mini-buffer window, we may end up in front of the window's
16226 display area. Start displaying at the start of the line
16227 containing PT in this case. */
16228 if (it.current_y <= 0)
16230 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16231 move_it_vertically_backward (&it, 0);
16232 it.current_y = 0;
16235 it.current_x = it.hpos = 0;
16237 /* Set the window start position here explicitly, to avoid an
16238 infinite loop in case the functions in window-scroll-functions
16239 get errors. */
16240 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16242 /* Run scroll hooks. */
16243 startp = run_window_scroll_functions (window, it.current.pos);
16245 /* Redisplay the window. */
16246 if (!current_matrix_up_to_date_p
16247 || windows_or_buffers_changed
16248 || f->cursor_type_changed
16249 /* Don't use try_window_reusing_current_matrix in this case
16250 because it can have changed the buffer. */
16251 || !NILP (Vwindow_scroll_functions)
16252 || !just_this_one_p
16253 || MINI_WINDOW_P (w)
16254 || !(used_current_matrix_p
16255 = try_window_reusing_current_matrix (w)))
16256 try_window (window, startp, 0);
16258 /* If new fonts have been loaded (due to fontsets), give up. We
16259 have to start a new redisplay since we need to re-adjust glyph
16260 matrices. */
16261 if (f->fonts_changed)
16262 goto need_larger_matrices;
16264 /* If cursor did not appear assume that the middle of the window is
16265 in the first line of the window. Do it again with the next line.
16266 (Imagine a window of height 100, displaying two lines of height
16267 60. Moving back 50 from it->last_visible_y will end in the first
16268 line.) */
16269 if (w->cursor.vpos < 0)
16271 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16273 clear_glyph_matrix (w->desired_matrix);
16274 move_it_by_lines (&it, 1);
16275 try_window (window, it.current.pos, 0);
16277 else if (PT < IT_CHARPOS (it))
16279 clear_glyph_matrix (w->desired_matrix);
16280 move_it_by_lines (&it, -1);
16281 try_window (window, it.current.pos, 0);
16283 else
16285 /* Not much we can do about it. */
16289 /* Consider the following case: Window starts at BEGV, there is
16290 invisible, intangible text at BEGV, so that display starts at
16291 some point START > BEGV. It can happen that we are called with
16292 PT somewhere between BEGV and START. Try to handle that case. */
16293 if (w->cursor.vpos < 0)
16295 struct glyph_row *row = w->current_matrix->rows;
16296 if (row->mode_line_p)
16297 ++row;
16298 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16301 if (!cursor_row_fully_visible_p (w, 0, 0))
16303 /* If vscroll is enabled, disable it and try again. */
16304 if (w->vscroll)
16306 w->vscroll = 0;
16307 clear_glyph_matrix (w->desired_matrix);
16308 goto recenter;
16311 /* Users who set scroll-conservatively to a large number want
16312 point just above/below the scroll margin. If we ended up
16313 with point's row partially visible, move the window start to
16314 make that row fully visible and out of the margin. */
16315 if (scroll_conservatively > SCROLL_LIMIT)
16317 int window_total_lines
16318 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16319 int margin =
16320 scroll_margin > 0
16321 ? min (scroll_margin, window_total_lines / 4)
16322 : 0;
16323 int move_down = w->cursor.vpos >= window_total_lines / 2;
16325 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16326 clear_glyph_matrix (w->desired_matrix);
16327 if (1 == try_window (window, it.current.pos,
16328 TRY_WINDOW_CHECK_MARGINS))
16329 goto done;
16332 /* If centering point failed to make the whole line visible,
16333 put point at the top instead. That has to make the whole line
16334 visible, if it can be done. */
16335 if (centering_position == 0)
16336 goto done;
16338 clear_glyph_matrix (w->desired_matrix);
16339 centering_position = 0;
16340 goto recenter;
16343 done:
16345 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16346 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16347 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16349 /* Display the mode line, if we must. */
16350 if ((update_mode_line
16351 /* If window not full width, must redo its mode line
16352 if (a) the window to its side is being redone and
16353 (b) we do a frame-based redisplay. This is a consequence
16354 of how inverted lines are drawn in frame-based redisplay. */
16355 || (!just_this_one_p
16356 && !FRAME_WINDOW_P (f)
16357 && !WINDOW_FULL_WIDTH_P (w))
16358 /* Line number to display. */
16359 || w->base_line_pos > 0
16360 /* Column number is displayed and different from the one displayed. */
16361 || (w->column_number_displayed != -1
16362 && (w->column_number_displayed != current_column ())))
16363 /* This means that the window has a mode line. */
16364 && (WINDOW_WANTS_MODELINE_P (w)
16365 || WINDOW_WANTS_HEADER_LINE_P (w)))
16368 display_mode_lines (w);
16370 /* If mode line height has changed, arrange for a thorough
16371 immediate redisplay using the correct mode line height. */
16372 if (WINDOW_WANTS_MODELINE_P (w)
16373 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16375 f->fonts_changed = 1;
16376 w->mode_line_height = -1;
16377 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16378 = DESIRED_MODE_LINE_HEIGHT (w);
16381 /* If header line height has changed, arrange for a thorough
16382 immediate redisplay using the correct header line height. */
16383 if (WINDOW_WANTS_HEADER_LINE_P (w)
16384 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16386 f->fonts_changed = 1;
16387 w->header_line_height = -1;
16388 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16389 = DESIRED_HEADER_LINE_HEIGHT (w);
16392 if (f->fonts_changed)
16393 goto need_larger_matrices;
16396 if (!line_number_displayed && w->base_line_pos != -1)
16398 w->base_line_pos = 0;
16399 w->base_line_number = 0;
16402 finish_menu_bars:
16404 /* When we reach a frame's selected window, redo the frame's menu bar. */
16405 if (update_mode_line
16406 && EQ (FRAME_SELECTED_WINDOW (f), window))
16408 int redisplay_menu_p = 0;
16410 if (FRAME_WINDOW_P (f))
16412 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16413 || defined (HAVE_NS) || defined (USE_GTK)
16414 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16415 #else
16416 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16417 #endif
16419 else
16420 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16422 if (redisplay_menu_p)
16423 display_menu_bar (w);
16425 #ifdef HAVE_WINDOW_SYSTEM
16426 if (FRAME_WINDOW_P (f))
16428 #if defined (USE_GTK) || defined (HAVE_NS)
16429 if (FRAME_EXTERNAL_TOOL_BAR (f))
16430 redisplay_tool_bar (f);
16431 #else
16432 if (WINDOWP (f->tool_bar_window)
16433 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16434 || !NILP (Vauto_resize_tool_bars))
16435 && redisplay_tool_bar (f))
16436 ignore_mouse_drag_p = 1;
16437 #endif
16439 #endif
16442 #ifdef HAVE_WINDOW_SYSTEM
16443 if (FRAME_WINDOW_P (f)
16444 && update_window_fringes (w, (just_this_one_p
16445 || (!used_current_matrix_p && !overlay_arrow_seen)
16446 || w->pseudo_window_p)))
16448 update_begin (f);
16449 block_input ();
16450 if (draw_window_fringes (w, 1))
16452 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16453 x_draw_right_divider (w);
16454 else
16455 x_draw_vertical_border (w);
16457 unblock_input ();
16458 update_end (f);
16461 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16462 x_draw_bottom_divider (w);
16463 #endif /* HAVE_WINDOW_SYSTEM */
16465 /* We go to this label, with fonts_changed set, if it is
16466 necessary to try again using larger glyph matrices.
16467 We have to redeem the scroll bar even in this case,
16468 because the loop in redisplay_internal expects that. */
16469 need_larger_matrices:
16471 finish_scroll_bars:
16473 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16475 /* Set the thumb's position and size. */
16476 set_vertical_scroll_bar (w);
16478 /* Note that we actually used the scroll bar attached to this
16479 window, so it shouldn't be deleted at the end of redisplay. */
16480 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16481 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16484 /* Restore current_buffer and value of point in it. The window
16485 update may have changed the buffer, so first make sure `opoint'
16486 is still valid (Bug#6177). */
16487 if (CHARPOS (opoint) < BEGV)
16488 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16489 else if (CHARPOS (opoint) > ZV)
16490 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16491 else
16492 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16494 set_buffer_internal_1 (old);
16495 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16496 shorter. This can be caused by log truncation in *Messages*. */
16497 if (CHARPOS (lpoint) <= ZV)
16498 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16500 unbind_to (count, Qnil);
16504 /* Build the complete desired matrix of WINDOW with a window start
16505 buffer position POS.
16507 Value is 1 if successful. It is zero if fonts were loaded during
16508 redisplay which makes re-adjusting glyph matrices necessary, and -1
16509 if point would appear in the scroll margins.
16510 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16511 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16512 set in FLAGS.) */
16515 try_window (Lisp_Object window, struct text_pos pos, int flags)
16517 struct window *w = XWINDOW (window);
16518 struct it it;
16519 struct glyph_row *last_text_row = NULL;
16520 struct frame *f = XFRAME (w->frame);
16521 int frame_line_height = default_line_pixel_height (w);
16523 /* Make POS the new window start. */
16524 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16526 /* Mark cursor position as unknown. No overlay arrow seen. */
16527 w->cursor.vpos = -1;
16528 overlay_arrow_seen = 0;
16530 /* Initialize iterator and info to start at POS. */
16531 start_display (&it, w, pos);
16533 /* Display all lines of W. */
16534 while (it.current_y < it.last_visible_y)
16536 if (display_line (&it))
16537 last_text_row = it.glyph_row - 1;
16538 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16539 return 0;
16542 /* Don't let the cursor end in the scroll margins. */
16543 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16544 && !MINI_WINDOW_P (w))
16546 int this_scroll_margin;
16547 int window_total_lines
16548 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16550 if (scroll_margin > 0)
16552 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16553 this_scroll_margin *= frame_line_height;
16555 else
16556 this_scroll_margin = 0;
16558 if ((w->cursor.y >= 0 /* not vscrolled */
16559 && w->cursor.y < this_scroll_margin
16560 && CHARPOS (pos) > BEGV
16561 && IT_CHARPOS (it) < ZV)
16562 /* rms: considering make_cursor_line_fully_visible_p here
16563 seems to give wrong results. We don't want to recenter
16564 when the last line is partly visible, we want to allow
16565 that case to be handled in the usual way. */
16566 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16568 w->cursor.vpos = -1;
16569 clear_glyph_matrix (w->desired_matrix);
16570 return -1;
16574 /* If bottom moved off end of frame, change mode line percentage. */
16575 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16576 w->update_mode_line = 1;
16578 /* Set window_end_pos to the offset of the last character displayed
16579 on the window from the end of current_buffer. Set
16580 window_end_vpos to its row number. */
16581 if (last_text_row)
16583 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16584 adjust_window_ends (w, last_text_row, 0);
16585 eassert
16586 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16587 w->window_end_vpos)));
16589 else
16591 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16592 w->window_end_pos = Z - ZV;
16593 w->window_end_vpos = 0;
16596 /* But that is not valid info until redisplay finishes. */
16597 w->window_end_valid = 0;
16598 return 1;
16603 /************************************************************************
16604 Window redisplay reusing current matrix when buffer has not changed
16605 ************************************************************************/
16607 /* Try redisplay of window W showing an unchanged buffer with a
16608 different window start than the last time it was displayed by
16609 reusing its current matrix. Value is non-zero if successful.
16610 W->start is the new window start. */
16612 static int
16613 try_window_reusing_current_matrix (struct window *w)
16615 struct frame *f = XFRAME (w->frame);
16616 struct glyph_row *bottom_row;
16617 struct it it;
16618 struct run run;
16619 struct text_pos start, new_start;
16620 int nrows_scrolled, i;
16621 struct glyph_row *last_text_row;
16622 struct glyph_row *last_reused_text_row;
16623 struct glyph_row *start_row;
16624 int start_vpos, min_y, max_y;
16626 #ifdef GLYPH_DEBUG
16627 if (inhibit_try_window_reusing)
16628 return 0;
16629 #endif
16631 if (/* This function doesn't handle terminal frames. */
16632 !FRAME_WINDOW_P (f)
16633 /* Don't try to reuse the display if windows have been split
16634 or such. */
16635 || windows_or_buffers_changed
16636 || f->cursor_type_changed)
16637 return 0;
16639 /* Can't do this if showing trailing whitespace. */
16640 if (!NILP (Vshow_trailing_whitespace))
16641 return 0;
16643 /* If top-line visibility has changed, give up. */
16644 if (WINDOW_WANTS_HEADER_LINE_P (w)
16645 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16646 return 0;
16648 /* Give up if old or new display is scrolled vertically. We could
16649 make this function handle this, but right now it doesn't. */
16650 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16651 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16652 return 0;
16654 /* The variable new_start now holds the new window start. The old
16655 start `start' can be determined from the current matrix. */
16656 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16657 start = start_row->minpos;
16658 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16660 /* Clear the desired matrix for the display below. */
16661 clear_glyph_matrix (w->desired_matrix);
16663 if (CHARPOS (new_start) <= CHARPOS (start))
16665 /* Don't use this method if the display starts with an ellipsis
16666 displayed for invisible text. It's not easy to handle that case
16667 below, and it's certainly not worth the effort since this is
16668 not a frequent case. */
16669 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16670 return 0;
16672 IF_DEBUG (debug_method_add (w, "twu1"));
16674 /* Display up to a row that can be reused. The variable
16675 last_text_row is set to the last row displayed that displays
16676 text. Note that it.vpos == 0 if or if not there is a
16677 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16678 start_display (&it, w, new_start);
16679 w->cursor.vpos = -1;
16680 last_text_row = last_reused_text_row = NULL;
16682 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16684 /* If we have reached into the characters in the START row,
16685 that means the line boundaries have changed. So we
16686 can't start copying with the row START. Maybe it will
16687 work to start copying with the following row. */
16688 while (IT_CHARPOS (it) > CHARPOS (start))
16690 /* Advance to the next row as the "start". */
16691 start_row++;
16692 start = start_row->minpos;
16693 /* If there are no more rows to try, or just one, give up. */
16694 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16695 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16696 || CHARPOS (start) == ZV)
16698 clear_glyph_matrix (w->desired_matrix);
16699 return 0;
16702 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16704 /* If we have reached alignment, we can copy the rest of the
16705 rows. */
16706 if (IT_CHARPOS (it) == CHARPOS (start)
16707 /* Don't accept "alignment" inside a display vector,
16708 since start_row could have started in the middle of
16709 that same display vector (thus their character
16710 positions match), and we have no way of telling if
16711 that is the case. */
16712 && it.current.dpvec_index < 0)
16713 break;
16715 if (display_line (&it))
16716 last_text_row = it.glyph_row - 1;
16720 /* A value of current_y < last_visible_y means that we stopped
16721 at the previous window start, which in turn means that we
16722 have at least one reusable row. */
16723 if (it.current_y < it.last_visible_y)
16725 struct glyph_row *row;
16727 /* IT.vpos always starts from 0; it counts text lines. */
16728 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16730 /* Find PT if not already found in the lines displayed. */
16731 if (w->cursor.vpos < 0)
16733 int dy = it.current_y - start_row->y;
16735 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16736 row = row_containing_pos (w, PT, row, NULL, dy);
16737 if (row)
16738 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16739 dy, nrows_scrolled);
16740 else
16742 clear_glyph_matrix (w->desired_matrix);
16743 return 0;
16747 /* Scroll the display. Do it before the current matrix is
16748 changed. The problem here is that update has not yet
16749 run, i.e. part of the current matrix is not up to date.
16750 scroll_run_hook will clear the cursor, and use the
16751 current matrix to get the height of the row the cursor is
16752 in. */
16753 run.current_y = start_row->y;
16754 run.desired_y = it.current_y;
16755 run.height = it.last_visible_y - it.current_y;
16757 if (run.height > 0 && run.current_y != run.desired_y)
16759 update_begin (f);
16760 FRAME_RIF (f)->update_window_begin_hook (w);
16761 FRAME_RIF (f)->clear_window_mouse_face (w);
16762 FRAME_RIF (f)->scroll_run_hook (w, &run);
16763 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16764 update_end (f);
16767 /* Shift current matrix down by nrows_scrolled lines. */
16768 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16769 rotate_matrix (w->current_matrix,
16770 start_vpos,
16771 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16772 nrows_scrolled);
16774 /* Disable lines that must be updated. */
16775 for (i = 0; i < nrows_scrolled; ++i)
16776 (start_row + i)->enabled_p = false;
16778 /* Re-compute Y positions. */
16779 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16780 max_y = it.last_visible_y;
16781 for (row = start_row + nrows_scrolled;
16782 row < bottom_row;
16783 ++row)
16785 row->y = it.current_y;
16786 row->visible_height = row->height;
16788 if (row->y < min_y)
16789 row->visible_height -= min_y - row->y;
16790 if (row->y + row->height > max_y)
16791 row->visible_height -= row->y + row->height - max_y;
16792 if (row->fringe_bitmap_periodic_p)
16793 row->redraw_fringe_bitmaps_p = 1;
16795 it.current_y += row->height;
16797 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16798 last_reused_text_row = row;
16799 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16800 break;
16803 /* Disable lines in the current matrix which are now
16804 below the window. */
16805 for (++row; row < bottom_row; ++row)
16806 row->enabled_p = row->mode_line_p = 0;
16809 /* Update window_end_pos etc.; last_reused_text_row is the last
16810 reused row from the current matrix containing text, if any.
16811 The value of last_text_row is the last displayed line
16812 containing text. */
16813 if (last_reused_text_row)
16814 adjust_window_ends (w, last_reused_text_row, 1);
16815 else if (last_text_row)
16816 adjust_window_ends (w, last_text_row, 0);
16817 else
16819 /* This window must be completely empty. */
16820 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16821 w->window_end_pos = Z - ZV;
16822 w->window_end_vpos = 0;
16824 w->window_end_valid = 0;
16826 /* Update hint: don't try scrolling again in update_window. */
16827 w->desired_matrix->no_scrolling_p = 1;
16829 #ifdef GLYPH_DEBUG
16830 debug_method_add (w, "try_window_reusing_current_matrix 1");
16831 #endif
16832 return 1;
16834 else if (CHARPOS (new_start) > CHARPOS (start))
16836 struct glyph_row *pt_row, *row;
16837 struct glyph_row *first_reusable_row;
16838 struct glyph_row *first_row_to_display;
16839 int dy;
16840 int yb = window_text_bottom_y (w);
16842 /* Find the row starting at new_start, if there is one. Don't
16843 reuse a partially visible line at the end. */
16844 first_reusable_row = start_row;
16845 while (first_reusable_row->enabled_p
16846 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16847 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16848 < CHARPOS (new_start)))
16849 ++first_reusable_row;
16851 /* Give up if there is no row to reuse. */
16852 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16853 || !first_reusable_row->enabled_p
16854 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16855 != CHARPOS (new_start)))
16856 return 0;
16858 /* We can reuse fully visible rows beginning with
16859 first_reusable_row to the end of the window. Set
16860 first_row_to_display to the first row that cannot be reused.
16861 Set pt_row to the row containing point, if there is any. */
16862 pt_row = NULL;
16863 for (first_row_to_display = first_reusable_row;
16864 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16865 ++first_row_to_display)
16867 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16868 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16869 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16870 && first_row_to_display->ends_at_zv_p
16871 && pt_row == NULL)))
16872 pt_row = first_row_to_display;
16875 /* Start displaying at the start of first_row_to_display. */
16876 eassert (first_row_to_display->y < yb);
16877 init_to_row_start (&it, w, first_row_to_display);
16879 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16880 - start_vpos);
16881 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16882 - nrows_scrolled);
16883 it.current_y = (first_row_to_display->y - first_reusable_row->y
16884 + WINDOW_HEADER_LINE_HEIGHT (w));
16886 /* Display lines beginning with first_row_to_display in the
16887 desired matrix. Set last_text_row to the last row displayed
16888 that displays text. */
16889 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16890 if (pt_row == NULL)
16891 w->cursor.vpos = -1;
16892 last_text_row = NULL;
16893 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16894 if (display_line (&it))
16895 last_text_row = it.glyph_row - 1;
16897 /* If point is in a reused row, adjust y and vpos of the cursor
16898 position. */
16899 if (pt_row)
16901 w->cursor.vpos -= nrows_scrolled;
16902 w->cursor.y -= first_reusable_row->y - start_row->y;
16905 /* Give up if point isn't in a row displayed or reused. (This
16906 also handles the case where w->cursor.vpos < nrows_scrolled
16907 after the calls to display_line, which can happen with scroll
16908 margins. See bug#1295.) */
16909 if (w->cursor.vpos < 0)
16911 clear_glyph_matrix (w->desired_matrix);
16912 return 0;
16915 /* Scroll the display. */
16916 run.current_y = first_reusable_row->y;
16917 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16918 run.height = it.last_visible_y - run.current_y;
16919 dy = run.current_y - run.desired_y;
16921 if (run.height)
16923 update_begin (f);
16924 FRAME_RIF (f)->update_window_begin_hook (w);
16925 FRAME_RIF (f)->clear_window_mouse_face (w);
16926 FRAME_RIF (f)->scroll_run_hook (w, &run);
16927 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16928 update_end (f);
16931 /* Adjust Y positions of reused rows. */
16932 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16933 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16934 max_y = it.last_visible_y;
16935 for (row = first_reusable_row; row < first_row_to_display; ++row)
16937 row->y -= dy;
16938 row->visible_height = row->height;
16939 if (row->y < min_y)
16940 row->visible_height -= min_y - row->y;
16941 if (row->y + row->height > max_y)
16942 row->visible_height -= row->y + row->height - max_y;
16943 if (row->fringe_bitmap_periodic_p)
16944 row->redraw_fringe_bitmaps_p = 1;
16947 /* Scroll the current matrix. */
16948 eassert (nrows_scrolled > 0);
16949 rotate_matrix (w->current_matrix,
16950 start_vpos,
16951 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16952 -nrows_scrolled);
16954 /* Disable rows not reused. */
16955 for (row -= nrows_scrolled; row < bottom_row; ++row)
16956 row->enabled_p = false;
16958 /* Point may have moved to a different line, so we cannot assume that
16959 the previous cursor position is valid; locate the correct row. */
16960 if (pt_row)
16962 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16963 row < bottom_row
16964 && PT >= MATRIX_ROW_END_CHARPOS (row)
16965 && !row->ends_at_zv_p;
16966 row++)
16968 w->cursor.vpos++;
16969 w->cursor.y = row->y;
16971 if (row < bottom_row)
16973 /* Can't simply scan the row for point with
16974 bidi-reordered glyph rows. Let set_cursor_from_row
16975 figure out where to put the cursor, and if it fails,
16976 give up. */
16977 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16979 if (!set_cursor_from_row (w, row, w->current_matrix,
16980 0, 0, 0, 0))
16982 clear_glyph_matrix (w->desired_matrix);
16983 return 0;
16986 else
16988 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16989 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16991 for (; glyph < end
16992 && (!BUFFERP (glyph->object)
16993 || glyph->charpos < PT);
16994 glyph++)
16996 w->cursor.hpos++;
16997 w->cursor.x += glyph->pixel_width;
17003 /* Adjust window end. A null value of last_text_row means that
17004 the window end is in reused rows which in turn means that
17005 only its vpos can have changed. */
17006 if (last_text_row)
17007 adjust_window_ends (w, last_text_row, 0);
17008 else
17009 w->window_end_vpos -= nrows_scrolled;
17011 w->window_end_valid = 0;
17012 w->desired_matrix->no_scrolling_p = 1;
17014 #ifdef GLYPH_DEBUG
17015 debug_method_add (w, "try_window_reusing_current_matrix 2");
17016 #endif
17017 return 1;
17020 return 0;
17025 /************************************************************************
17026 Window redisplay reusing current matrix when buffer has changed
17027 ************************************************************************/
17029 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17030 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17031 ptrdiff_t *, ptrdiff_t *);
17032 static struct glyph_row *
17033 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17034 struct glyph_row *);
17037 /* Return the last row in MATRIX displaying text. If row START is
17038 non-null, start searching with that row. IT gives the dimensions
17039 of the display. Value is null if matrix is empty; otherwise it is
17040 a pointer to the row found. */
17042 static struct glyph_row *
17043 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17044 struct glyph_row *start)
17046 struct glyph_row *row, *row_found;
17048 /* Set row_found to the last row in IT->w's current matrix
17049 displaying text. The loop looks funny but think of partially
17050 visible lines. */
17051 row_found = NULL;
17052 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17053 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17055 eassert (row->enabled_p);
17056 row_found = row;
17057 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17058 break;
17059 ++row;
17062 return row_found;
17066 /* Return the last row in the current matrix of W that is not affected
17067 by changes at the start of current_buffer that occurred since W's
17068 current matrix was built. Value is null if no such row exists.
17070 BEG_UNCHANGED us the number of characters unchanged at the start of
17071 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17072 first changed character in current_buffer. Characters at positions <
17073 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17074 when the current matrix was built. */
17076 static struct glyph_row *
17077 find_last_unchanged_at_beg_row (struct window *w)
17079 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17080 struct glyph_row *row;
17081 struct glyph_row *row_found = NULL;
17082 int yb = window_text_bottom_y (w);
17084 /* Find the last row displaying unchanged text. */
17085 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17086 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17087 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17088 ++row)
17090 if (/* If row ends before first_changed_pos, it is unchanged,
17091 except in some case. */
17092 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17093 /* When row ends in ZV and we write at ZV it is not
17094 unchanged. */
17095 && !row->ends_at_zv_p
17096 /* When first_changed_pos is the end of a continued line,
17097 row is not unchanged because it may be no longer
17098 continued. */
17099 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17100 && (row->continued_p
17101 || row->exact_window_width_line_p))
17102 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17103 needs to be recomputed, so don't consider this row as
17104 unchanged. This happens when the last line was
17105 bidi-reordered and was killed immediately before this
17106 redisplay cycle. In that case, ROW->end stores the
17107 buffer position of the first visual-order character of
17108 the killed text, which is now beyond ZV. */
17109 && CHARPOS (row->end.pos) <= ZV)
17110 row_found = row;
17112 /* Stop if last visible row. */
17113 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17114 break;
17117 return row_found;
17121 /* Find the first glyph row in the current matrix of W that is not
17122 affected by changes at the end of current_buffer since the
17123 time W's current matrix was built.
17125 Return in *DELTA the number of chars by which buffer positions in
17126 unchanged text at the end of current_buffer must be adjusted.
17128 Return in *DELTA_BYTES the corresponding number of bytes.
17130 Value is null if no such row exists, i.e. all rows are affected by
17131 changes. */
17133 static struct glyph_row *
17134 find_first_unchanged_at_end_row (struct window *w,
17135 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17137 struct glyph_row *row;
17138 struct glyph_row *row_found = NULL;
17140 *delta = *delta_bytes = 0;
17142 /* Display must not have been paused, otherwise the current matrix
17143 is not up to date. */
17144 eassert (w->window_end_valid);
17146 /* A value of window_end_pos >= END_UNCHANGED means that the window
17147 end is in the range of changed text. If so, there is no
17148 unchanged row at the end of W's current matrix. */
17149 if (w->window_end_pos >= END_UNCHANGED)
17150 return NULL;
17152 /* Set row to the last row in W's current matrix displaying text. */
17153 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17155 /* If matrix is entirely empty, no unchanged row exists. */
17156 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17158 /* The value of row is the last glyph row in the matrix having a
17159 meaningful buffer position in it. The end position of row
17160 corresponds to window_end_pos. This allows us to translate
17161 buffer positions in the current matrix to current buffer
17162 positions for characters not in changed text. */
17163 ptrdiff_t Z_old =
17164 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17165 ptrdiff_t Z_BYTE_old =
17166 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17167 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17168 struct glyph_row *first_text_row
17169 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17171 *delta = Z - Z_old;
17172 *delta_bytes = Z_BYTE - Z_BYTE_old;
17174 /* Set last_unchanged_pos to the buffer position of the last
17175 character in the buffer that has not been changed. Z is the
17176 index + 1 of the last character in current_buffer, i.e. by
17177 subtracting END_UNCHANGED we get the index of the last
17178 unchanged character, and we have to add BEG to get its buffer
17179 position. */
17180 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17181 last_unchanged_pos_old = last_unchanged_pos - *delta;
17183 /* Search backward from ROW for a row displaying a line that
17184 starts at a minimum position >= last_unchanged_pos_old. */
17185 for (; row > first_text_row; --row)
17187 /* This used to abort, but it can happen.
17188 It is ok to just stop the search instead here. KFS. */
17189 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17190 break;
17192 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17193 row_found = row;
17197 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17199 return row_found;
17203 /* Make sure that glyph rows in the current matrix of window W
17204 reference the same glyph memory as corresponding rows in the
17205 frame's frame matrix. This function is called after scrolling W's
17206 current matrix on a terminal frame in try_window_id and
17207 try_window_reusing_current_matrix. */
17209 static void
17210 sync_frame_with_window_matrix_rows (struct window *w)
17212 struct frame *f = XFRAME (w->frame);
17213 struct glyph_row *window_row, *window_row_end, *frame_row;
17215 /* Preconditions: W must be a leaf window and full-width. Its frame
17216 must have a frame matrix. */
17217 eassert (BUFFERP (w->contents));
17218 eassert (WINDOW_FULL_WIDTH_P (w));
17219 eassert (!FRAME_WINDOW_P (f));
17221 /* If W is a full-width window, glyph pointers in W's current matrix
17222 have, by definition, to be the same as glyph pointers in the
17223 corresponding frame matrix. Note that frame matrices have no
17224 marginal areas (see build_frame_matrix). */
17225 window_row = w->current_matrix->rows;
17226 window_row_end = window_row + w->current_matrix->nrows;
17227 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17228 while (window_row < window_row_end)
17230 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17231 struct glyph *end = window_row->glyphs[LAST_AREA];
17233 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17234 frame_row->glyphs[TEXT_AREA] = start;
17235 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17236 frame_row->glyphs[LAST_AREA] = end;
17238 /* Disable frame rows whose corresponding window rows have
17239 been disabled in try_window_id. */
17240 if (!window_row->enabled_p)
17241 frame_row->enabled_p = false;
17243 ++window_row, ++frame_row;
17248 /* Find the glyph row in window W containing CHARPOS. Consider all
17249 rows between START and END (not inclusive). END null means search
17250 all rows to the end of the display area of W. Value is the row
17251 containing CHARPOS or null. */
17253 struct glyph_row *
17254 row_containing_pos (struct window *w, ptrdiff_t charpos,
17255 struct glyph_row *start, struct glyph_row *end, int dy)
17257 struct glyph_row *row = start;
17258 struct glyph_row *best_row = NULL;
17259 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17260 int last_y;
17262 /* If we happen to start on a header-line, skip that. */
17263 if (row->mode_line_p)
17264 ++row;
17266 if ((end && row >= end) || !row->enabled_p)
17267 return NULL;
17269 last_y = window_text_bottom_y (w) - dy;
17271 while (1)
17273 /* Give up if we have gone too far. */
17274 if (end && row >= end)
17275 return NULL;
17276 /* This formerly returned if they were equal.
17277 I think that both quantities are of a "last plus one" type;
17278 if so, when they are equal, the row is within the screen. -- rms. */
17279 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17280 return NULL;
17282 /* If it is in this row, return this row. */
17283 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17284 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17285 /* The end position of a row equals the start
17286 position of the next row. If CHARPOS is there, we
17287 would rather consider it displayed in the next
17288 line, except when this line ends in ZV. */
17289 && !row_for_charpos_p (row, charpos)))
17290 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17292 struct glyph *g;
17294 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17295 || (!best_row && !row->continued_p))
17296 return row;
17297 /* In bidi-reordered rows, there could be several rows whose
17298 edges surround CHARPOS, all of these rows belonging to
17299 the same continued line. We need to find the row which
17300 fits CHARPOS the best. */
17301 for (g = row->glyphs[TEXT_AREA];
17302 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17303 g++)
17305 if (!STRINGP (g->object))
17307 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17309 mindif = eabs (g->charpos - charpos);
17310 best_row = row;
17311 /* Exact match always wins. */
17312 if (mindif == 0)
17313 return best_row;
17318 else if (best_row && !row->continued_p)
17319 return best_row;
17320 ++row;
17325 /* Try to redisplay window W by reusing its existing display. W's
17326 current matrix must be up to date when this function is called,
17327 i.e. window_end_valid must be nonzero.
17329 Value is
17331 1 if display has been updated
17332 0 if otherwise unsuccessful
17333 -1 if redisplay with same window start is known not to succeed
17335 The following steps are performed:
17337 1. Find the last row in the current matrix of W that is not
17338 affected by changes at the start of current_buffer. If no such row
17339 is found, give up.
17341 2. Find the first row in W's current matrix that is not affected by
17342 changes at the end of current_buffer. Maybe there is no such row.
17344 3. Display lines beginning with the row + 1 found in step 1 to the
17345 row found in step 2 or, if step 2 didn't find a row, to the end of
17346 the window.
17348 4. If cursor is not known to appear on the window, give up.
17350 5. If display stopped at the row found in step 2, scroll the
17351 display and current matrix as needed.
17353 6. Maybe display some lines at the end of W, if we must. This can
17354 happen under various circumstances, like a partially visible line
17355 becoming fully visible, or because newly displayed lines are displayed
17356 in smaller font sizes.
17358 7. Update W's window end information. */
17360 static int
17361 try_window_id (struct window *w)
17363 struct frame *f = XFRAME (w->frame);
17364 struct glyph_matrix *current_matrix = w->current_matrix;
17365 struct glyph_matrix *desired_matrix = w->desired_matrix;
17366 struct glyph_row *last_unchanged_at_beg_row;
17367 struct glyph_row *first_unchanged_at_end_row;
17368 struct glyph_row *row;
17369 struct glyph_row *bottom_row;
17370 int bottom_vpos;
17371 struct it it;
17372 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17373 int dvpos, dy;
17374 struct text_pos start_pos;
17375 struct run run;
17376 int first_unchanged_at_end_vpos = 0;
17377 struct glyph_row *last_text_row, *last_text_row_at_end;
17378 struct text_pos start;
17379 ptrdiff_t first_changed_charpos, last_changed_charpos;
17381 #ifdef GLYPH_DEBUG
17382 if (inhibit_try_window_id)
17383 return 0;
17384 #endif
17386 /* This is handy for debugging. */
17387 #if 0
17388 #define GIVE_UP(X) \
17389 do { \
17390 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17391 return 0; \
17392 } while (0)
17393 #else
17394 #define GIVE_UP(X) return 0
17395 #endif
17397 SET_TEXT_POS_FROM_MARKER (start, w->start);
17399 /* Don't use this for mini-windows because these can show
17400 messages and mini-buffers, and we don't handle that here. */
17401 if (MINI_WINDOW_P (w))
17402 GIVE_UP (1);
17404 /* This flag is used to prevent redisplay optimizations. */
17405 if (windows_or_buffers_changed || f->cursor_type_changed)
17406 GIVE_UP (2);
17408 /* Verify that narrowing has not changed.
17409 Also verify that we were not told to prevent redisplay optimizations.
17410 It would be nice to further
17411 reduce the number of cases where this prevents try_window_id. */
17412 if (current_buffer->clip_changed
17413 || current_buffer->prevent_redisplay_optimizations_p)
17414 GIVE_UP (3);
17416 /* Window must either use window-based redisplay or be full width. */
17417 if (!FRAME_WINDOW_P (f)
17418 && (!FRAME_LINE_INS_DEL_OK (f)
17419 || !WINDOW_FULL_WIDTH_P (w)))
17420 GIVE_UP (4);
17422 /* Give up if point is known NOT to appear in W. */
17423 if (PT < CHARPOS (start))
17424 GIVE_UP (5);
17426 /* Another way to prevent redisplay optimizations. */
17427 if (w->last_modified == 0)
17428 GIVE_UP (6);
17430 /* Verify that window is not hscrolled. */
17431 if (w->hscroll != 0)
17432 GIVE_UP (7);
17434 /* Verify that display wasn't paused. */
17435 if (!w->window_end_valid)
17436 GIVE_UP (8);
17438 /* Likewise if highlighting trailing whitespace. */
17439 if (!NILP (Vshow_trailing_whitespace))
17440 GIVE_UP (11);
17442 /* Can't use this if overlay arrow position and/or string have
17443 changed. */
17444 if (overlay_arrows_changed_p ())
17445 GIVE_UP (12);
17447 /* When word-wrap is on, adding a space to the first word of a
17448 wrapped line can change the wrap position, altering the line
17449 above it. It might be worthwhile to handle this more
17450 intelligently, but for now just redisplay from scratch. */
17451 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17452 GIVE_UP (21);
17454 /* Under bidi reordering, adding or deleting a character in the
17455 beginning of a paragraph, before the first strong directional
17456 character, can change the base direction of the paragraph (unless
17457 the buffer specifies a fixed paragraph direction), which will
17458 require to redisplay the whole paragraph. It might be worthwhile
17459 to find the paragraph limits and widen the range of redisplayed
17460 lines to that, but for now just give up this optimization and
17461 redisplay from scratch. */
17462 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17463 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17464 GIVE_UP (22);
17466 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17467 only if buffer has really changed. The reason is that the gap is
17468 initially at Z for freshly visited files. The code below would
17469 set end_unchanged to 0 in that case. */
17470 if (MODIFF > SAVE_MODIFF
17471 /* This seems to happen sometimes after saving a buffer. */
17472 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17474 if (GPT - BEG < BEG_UNCHANGED)
17475 BEG_UNCHANGED = GPT - BEG;
17476 if (Z - GPT < END_UNCHANGED)
17477 END_UNCHANGED = Z - GPT;
17480 /* The position of the first and last character that has been changed. */
17481 first_changed_charpos = BEG + BEG_UNCHANGED;
17482 last_changed_charpos = Z - END_UNCHANGED;
17484 /* If window starts after a line end, and the last change is in
17485 front of that newline, then changes don't affect the display.
17486 This case happens with stealth-fontification. Note that although
17487 the display is unchanged, glyph positions in the matrix have to
17488 be adjusted, of course. */
17489 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17490 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17491 && ((last_changed_charpos < CHARPOS (start)
17492 && CHARPOS (start) == BEGV)
17493 || (last_changed_charpos < CHARPOS (start) - 1
17494 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17496 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17497 struct glyph_row *r0;
17499 /* Compute how many chars/bytes have been added to or removed
17500 from the buffer. */
17501 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17502 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17503 Z_delta = Z - Z_old;
17504 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17506 /* Give up if PT is not in the window. Note that it already has
17507 been checked at the start of try_window_id that PT is not in
17508 front of the window start. */
17509 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17510 GIVE_UP (13);
17512 /* If window start is unchanged, we can reuse the whole matrix
17513 as is, after adjusting glyph positions. No need to compute
17514 the window end again, since its offset from Z hasn't changed. */
17515 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17516 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17517 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17518 /* PT must not be in a partially visible line. */
17519 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17520 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17522 /* Adjust positions in the glyph matrix. */
17523 if (Z_delta || Z_delta_bytes)
17525 struct glyph_row *r1
17526 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17527 increment_matrix_positions (w->current_matrix,
17528 MATRIX_ROW_VPOS (r0, current_matrix),
17529 MATRIX_ROW_VPOS (r1, current_matrix),
17530 Z_delta, Z_delta_bytes);
17533 /* Set the cursor. */
17534 row = row_containing_pos (w, PT, r0, NULL, 0);
17535 if (row)
17536 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17537 return 1;
17541 /* Handle the case that changes are all below what is displayed in
17542 the window, and that PT is in the window. This shortcut cannot
17543 be taken if ZV is visible in the window, and text has been added
17544 there that is visible in the window. */
17545 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17546 /* ZV is not visible in the window, or there are no
17547 changes at ZV, actually. */
17548 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17549 || first_changed_charpos == last_changed_charpos))
17551 struct glyph_row *r0;
17553 /* Give up if PT is not in the window. Note that it already has
17554 been checked at the start of try_window_id that PT is not in
17555 front of the window start. */
17556 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17557 GIVE_UP (14);
17559 /* If window start is unchanged, we can reuse the whole matrix
17560 as is, without changing glyph positions since no text has
17561 been added/removed in front of the window end. */
17562 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17563 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17564 /* PT must not be in a partially visible line. */
17565 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17566 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17568 /* We have to compute the window end anew since text
17569 could have been added/removed after it. */
17570 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17571 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17573 /* Set the cursor. */
17574 row = row_containing_pos (w, PT, r0, NULL, 0);
17575 if (row)
17576 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17577 return 2;
17581 /* Give up if window start is in the changed area.
17583 The condition used to read
17585 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17587 but why that was tested escapes me at the moment. */
17588 if (CHARPOS (start) >= first_changed_charpos
17589 && CHARPOS (start) <= last_changed_charpos)
17590 GIVE_UP (15);
17592 /* Check that window start agrees with the start of the first glyph
17593 row in its current matrix. Check this after we know the window
17594 start is not in changed text, otherwise positions would not be
17595 comparable. */
17596 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17597 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17598 GIVE_UP (16);
17600 /* Give up if the window ends in strings. Overlay strings
17601 at the end are difficult to handle, so don't try. */
17602 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17603 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17604 GIVE_UP (20);
17606 /* Compute the position at which we have to start displaying new
17607 lines. Some of the lines at the top of the window might be
17608 reusable because they are not displaying changed text. Find the
17609 last row in W's current matrix not affected by changes at the
17610 start of current_buffer. Value is null if changes start in the
17611 first line of window. */
17612 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17613 if (last_unchanged_at_beg_row)
17615 /* Avoid starting to display in the middle of a character, a TAB
17616 for instance. This is easier than to set up the iterator
17617 exactly, and it's not a frequent case, so the additional
17618 effort wouldn't really pay off. */
17619 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17620 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17621 && last_unchanged_at_beg_row > w->current_matrix->rows)
17622 --last_unchanged_at_beg_row;
17624 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17625 GIVE_UP (17);
17627 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17628 GIVE_UP (18);
17629 start_pos = it.current.pos;
17631 /* Start displaying new lines in the desired matrix at the same
17632 vpos we would use in the current matrix, i.e. below
17633 last_unchanged_at_beg_row. */
17634 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17635 current_matrix);
17636 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17637 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17639 eassert (it.hpos == 0 && it.current_x == 0);
17641 else
17643 /* There are no reusable lines at the start of the window.
17644 Start displaying in the first text line. */
17645 start_display (&it, w, start);
17646 it.vpos = it.first_vpos;
17647 start_pos = it.current.pos;
17650 /* Find the first row that is not affected by changes at the end of
17651 the buffer. Value will be null if there is no unchanged row, in
17652 which case we must redisplay to the end of the window. delta
17653 will be set to the value by which buffer positions beginning with
17654 first_unchanged_at_end_row have to be adjusted due to text
17655 changes. */
17656 first_unchanged_at_end_row
17657 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17658 IF_DEBUG (debug_delta = delta);
17659 IF_DEBUG (debug_delta_bytes = delta_bytes);
17661 /* Set stop_pos to the buffer position up to which we will have to
17662 display new lines. If first_unchanged_at_end_row != NULL, this
17663 is the buffer position of the start of the line displayed in that
17664 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17665 that we don't stop at a buffer position. */
17666 stop_pos = 0;
17667 if (first_unchanged_at_end_row)
17669 eassert (last_unchanged_at_beg_row == NULL
17670 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17672 /* If this is a continuation line, move forward to the next one
17673 that isn't. Changes in lines above affect this line.
17674 Caution: this may move first_unchanged_at_end_row to a row
17675 not displaying text. */
17676 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17677 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17678 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17679 < it.last_visible_y))
17680 ++first_unchanged_at_end_row;
17682 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17683 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17684 >= it.last_visible_y))
17685 first_unchanged_at_end_row = NULL;
17686 else
17688 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17689 + delta);
17690 first_unchanged_at_end_vpos
17691 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17692 eassert (stop_pos >= Z - END_UNCHANGED);
17695 else if (last_unchanged_at_beg_row == NULL)
17696 GIVE_UP (19);
17699 #ifdef GLYPH_DEBUG
17701 /* Either there is no unchanged row at the end, or the one we have
17702 now displays text. This is a necessary condition for the window
17703 end pos calculation at the end of this function. */
17704 eassert (first_unchanged_at_end_row == NULL
17705 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17707 debug_last_unchanged_at_beg_vpos
17708 = (last_unchanged_at_beg_row
17709 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17710 : -1);
17711 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17713 #endif /* GLYPH_DEBUG */
17716 /* Display new lines. Set last_text_row to the last new line
17717 displayed which has text on it, i.e. might end up as being the
17718 line where the window_end_vpos is. */
17719 w->cursor.vpos = -1;
17720 last_text_row = NULL;
17721 overlay_arrow_seen = 0;
17722 while (it.current_y < it.last_visible_y
17723 && !f->fonts_changed
17724 && (first_unchanged_at_end_row == NULL
17725 || IT_CHARPOS (it) < stop_pos))
17727 if (display_line (&it))
17728 last_text_row = it.glyph_row - 1;
17731 if (f->fonts_changed)
17732 return -1;
17735 /* Compute differences in buffer positions, y-positions etc. for
17736 lines reused at the bottom of the window. Compute what we can
17737 scroll. */
17738 if (first_unchanged_at_end_row
17739 /* No lines reused because we displayed everything up to the
17740 bottom of the window. */
17741 && it.current_y < it.last_visible_y)
17743 dvpos = (it.vpos
17744 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17745 current_matrix));
17746 dy = it.current_y - first_unchanged_at_end_row->y;
17747 run.current_y = first_unchanged_at_end_row->y;
17748 run.desired_y = run.current_y + dy;
17749 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17751 else
17753 delta = delta_bytes = dvpos = dy
17754 = run.current_y = run.desired_y = run.height = 0;
17755 first_unchanged_at_end_row = NULL;
17757 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17760 /* Find the cursor if not already found. We have to decide whether
17761 PT will appear on this window (it sometimes doesn't, but this is
17762 not a very frequent case.) This decision has to be made before
17763 the current matrix is altered. A value of cursor.vpos < 0 means
17764 that PT is either in one of the lines beginning at
17765 first_unchanged_at_end_row or below the window. Don't care for
17766 lines that might be displayed later at the window end; as
17767 mentioned, this is not a frequent case. */
17768 if (w->cursor.vpos < 0)
17770 /* Cursor in unchanged rows at the top? */
17771 if (PT < CHARPOS (start_pos)
17772 && last_unchanged_at_beg_row)
17774 row = row_containing_pos (w, PT,
17775 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17776 last_unchanged_at_beg_row + 1, 0);
17777 if (row)
17778 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17781 /* Start from first_unchanged_at_end_row looking for PT. */
17782 else if (first_unchanged_at_end_row)
17784 row = row_containing_pos (w, PT - delta,
17785 first_unchanged_at_end_row, NULL, 0);
17786 if (row)
17787 set_cursor_from_row (w, row, w->current_matrix, delta,
17788 delta_bytes, dy, dvpos);
17791 /* Give up if cursor was not found. */
17792 if (w->cursor.vpos < 0)
17794 clear_glyph_matrix (w->desired_matrix);
17795 return -1;
17799 /* Don't let the cursor end in the scroll margins. */
17801 int this_scroll_margin, cursor_height;
17802 int frame_line_height = default_line_pixel_height (w);
17803 int window_total_lines
17804 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17806 this_scroll_margin =
17807 max (0, min (scroll_margin, window_total_lines / 4));
17808 this_scroll_margin *= frame_line_height;
17809 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17811 if ((w->cursor.y < this_scroll_margin
17812 && CHARPOS (start) > BEGV)
17813 /* Old redisplay didn't take scroll margin into account at the bottom,
17814 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17815 || (w->cursor.y + (make_cursor_line_fully_visible_p
17816 ? cursor_height + this_scroll_margin
17817 : 1)) > it.last_visible_y)
17819 w->cursor.vpos = -1;
17820 clear_glyph_matrix (w->desired_matrix);
17821 return -1;
17825 /* Scroll the display. Do it before changing the current matrix so
17826 that xterm.c doesn't get confused about where the cursor glyph is
17827 found. */
17828 if (dy && run.height)
17830 update_begin (f);
17832 if (FRAME_WINDOW_P (f))
17834 FRAME_RIF (f)->update_window_begin_hook (w);
17835 FRAME_RIF (f)->clear_window_mouse_face (w);
17836 FRAME_RIF (f)->scroll_run_hook (w, &run);
17837 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17839 else
17841 /* Terminal frame. In this case, dvpos gives the number of
17842 lines to scroll by; dvpos < 0 means scroll up. */
17843 int from_vpos
17844 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17845 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17846 int end = (WINDOW_TOP_EDGE_LINE (w)
17847 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17848 + window_internal_height (w));
17850 #if defined (HAVE_GPM) || defined (MSDOS)
17851 x_clear_window_mouse_face (w);
17852 #endif
17853 /* Perform the operation on the screen. */
17854 if (dvpos > 0)
17856 /* Scroll last_unchanged_at_beg_row to the end of the
17857 window down dvpos lines. */
17858 set_terminal_window (f, end);
17860 /* On dumb terminals delete dvpos lines at the end
17861 before inserting dvpos empty lines. */
17862 if (!FRAME_SCROLL_REGION_OK (f))
17863 ins_del_lines (f, end - dvpos, -dvpos);
17865 /* Insert dvpos empty lines in front of
17866 last_unchanged_at_beg_row. */
17867 ins_del_lines (f, from, dvpos);
17869 else if (dvpos < 0)
17871 /* Scroll up last_unchanged_at_beg_vpos to the end of
17872 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17873 set_terminal_window (f, end);
17875 /* Delete dvpos lines in front of
17876 last_unchanged_at_beg_vpos. ins_del_lines will set
17877 the cursor to the given vpos and emit |dvpos| delete
17878 line sequences. */
17879 ins_del_lines (f, from + dvpos, dvpos);
17881 /* On a dumb terminal insert dvpos empty lines at the
17882 end. */
17883 if (!FRAME_SCROLL_REGION_OK (f))
17884 ins_del_lines (f, end + dvpos, -dvpos);
17887 set_terminal_window (f, 0);
17890 update_end (f);
17893 /* Shift reused rows of the current matrix to the right position.
17894 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17895 text. */
17896 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17897 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17898 if (dvpos < 0)
17900 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17901 bottom_vpos, dvpos);
17902 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17903 bottom_vpos);
17905 else if (dvpos > 0)
17907 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17908 bottom_vpos, dvpos);
17909 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17910 first_unchanged_at_end_vpos + dvpos);
17913 /* For frame-based redisplay, make sure that current frame and window
17914 matrix are in sync with respect to glyph memory. */
17915 if (!FRAME_WINDOW_P (f))
17916 sync_frame_with_window_matrix_rows (w);
17918 /* Adjust buffer positions in reused rows. */
17919 if (delta || delta_bytes)
17920 increment_matrix_positions (current_matrix,
17921 first_unchanged_at_end_vpos + dvpos,
17922 bottom_vpos, delta, delta_bytes);
17924 /* Adjust Y positions. */
17925 if (dy)
17926 shift_glyph_matrix (w, current_matrix,
17927 first_unchanged_at_end_vpos + dvpos,
17928 bottom_vpos, dy);
17930 if (first_unchanged_at_end_row)
17932 first_unchanged_at_end_row += dvpos;
17933 if (first_unchanged_at_end_row->y >= it.last_visible_y
17934 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17935 first_unchanged_at_end_row = NULL;
17938 /* If scrolling up, there may be some lines to display at the end of
17939 the window. */
17940 last_text_row_at_end = NULL;
17941 if (dy < 0)
17943 /* Scrolling up can leave for example a partially visible line
17944 at the end of the window to be redisplayed. */
17945 /* Set last_row to the glyph row in the current matrix where the
17946 window end line is found. It has been moved up or down in
17947 the matrix by dvpos. */
17948 int last_vpos = w->window_end_vpos + dvpos;
17949 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17951 /* If last_row is the window end line, it should display text. */
17952 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17954 /* If window end line was partially visible before, begin
17955 displaying at that line. Otherwise begin displaying with the
17956 line following it. */
17957 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17959 init_to_row_start (&it, w, last_row);
17960 it.vpos = last_vpos;
17961 it.current_y = last_row->y;
17963 else
17965 init_to_row_end (&it, w, last_row);
17966 it.vpos = 1 + last_vpos;
17967 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17968 ++last_row;
17971 /* We may start in a continuation line. If so, we have to
17972 get the right continuation_lines_width and current_x. */
17973 it.continuation_lines_width = last_row->continuation_lines_width;
17974 it.hpos = it.current_x = 0;
17976 /* Display the rest of the lines at the window end. */
17977 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17978 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17980 /* Is it always sure that the display agrees with lines in
17981 the current matrix? I don't think so, so we mark rows
17982 displayed invalid in the current matrix by setting their
17983 enabled_p flag to zero. */
17984 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
17985 if (display_line (&it))
17986 last_text_row_at_end = it.glyph_row - 1;
17990 /* Update window_end_pos and window_end_vpos. */
17991 if (first_unchanged_at_end_row && !last_text_row_at_end)
17993 /* Window end line if one of the preserved rows from the current
17994 matrix. Set row to the last row displaying text in current
17995 matrix starting at first_unchanged_at_end_row, after
17996 scrolling. */
17997 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17998 row = find_last_row_displaying_text (w->current_matrix, &it,
17999 first_unchanged_at_end_row);
18000 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18001 adjust_window_ends (w, row, 1);
18002 eassert (w->window_end_bytepos >= 0);
18003 IF_DEBUG (debug_method_add (w, "A"));
18005 else if (last_text_row_at_end)
18007 adjust_window_ends (w, last_text_row_at_end, 0);
18008 eassert (w->window_end_bytepos >= 0);
18009 IF_DEBUG (debug_method_add (w, "B"));
18011 else if (last_text_row)
18013 /* We have displayed either to the end of the window or at the
18014 end of the window, i.e. the last row with text is to be found
18015 in the desired matrix. */
18016 adjust_window_ends (w, last_text_row, 0);
18017 eassert (w->window_end_bytepos >= 0);
18019 else if (first_unchanged_at_end_row == NULL
18020 && last_text_row == NULL
18021 && last_text_row_at_end == NULL)
18023 /* Displayed to end of window, but no line containing text was
18024 displayed. Lines were deleted at the end of the window. */
18025 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18026 int vpos = w->window_end_vpos;
18027 struct glyph_row *current_row = current_matrix->rows + vpos;
18028 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18030 for (row = NULL;
18031 row == NULL && vpos >= first_vpos;
18032 --vpos, --current_row, --desired_row)
18034 if (desired_row->enabled_p)
18036 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18037 row = desired_row;
18039 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18040 row = current_row;
18043 eassert (row != NULL);
18044 w->window_end_vpos = vpos + 1;
18045 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18046 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18047 eassert (w->window_end_bytepos >= 0);
18048 IF_DEBUG (debug_method_add (w, "C"));
18050 else
18051 emacs_abort ();
18053 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18054 debug_end_vpos = w->window_end_vpos));
18056 /* Record that display has not been completed. */
18057 w->window_end_valid = 0;
18058 w->desired_matrix->no_scrolling_p = 1;
18059 return 3;
18061 #undef GIVE_UP
18066 /***********************************************************************
18067 More debugging support
18068 ***********************************************************************/
18070 #ifdef GLYPH_DEBUG
18072 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18073 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18074 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18077 /* Dump the contents of glyph matrix MATRIX on stderr.
18079 GLYPHS 0 means don't show glyph contents.
18080 GLYPHS 1 means show glyphs in short form
18081 GLYPHS > 1 means show glyphs in long form. */
18083 void
18084 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18086 int i;
18087 for (i = 0; i < matrix->nrows; ++i)
18088 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18092 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18093 the glyph row and area where the glyph comes from. */
18095 void
18096 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18098 if (glyph->type == CHAR_GLYPH
18099 || glyph->type == GLYPHLESS_GLYPH)
18101 fprintf (stderr,
18102 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18103 glyph - row->glyphs[TEXT_AREA],
18104 (glyph->type == CHAR_GLYPH
18105 ? 'C'
18106 : 'G'),
18107 glyph->charpos,
18108 (BUFFERP (glyph->object)
18109 ? 'B'
18110 : (STRINGP (glyph->object)
18111 ? 'S'
18112 : (INTEGERP (glyph->object)
18113 ? '0'
18114 : '-'))),
18115 glyph->pixel_width,
18116 glyph->u.ch,
18117 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18118 ? glyph->u.ch
18119 : '.'),
18120 glyph->face_id,
18121 glyph->left_box_line_p,
18122 glyph->right_box_line_p);
18124 else if (glyph->type == STRETCH_GLYPH)
18126 fprintf (stderr,
18127 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18128 glyph - row->glyphs[TEXT_AREA],
18129 'S',
18130 glyph->charpos,
18131 (BUFFERP (glyph->object)
18132 ? 'B'
18133 : (STRINGP (glyph->object)
18134 ? 'S'
18135 : (INTEGERP (glyph->object)
18136 ? '0'
18137 : '-'))),
18138 glyph->pixel_width,
18140 ' ',
18141 glyph->face_id,
18142 glyph->left_box_line_p,
18143 glyph->right_box_line_p);
18145 else if (glyph->type == IMAGE_GLYPH)
18147 fprintf (stderr,
18148 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18149 glyph - row->glyphs[TEXT_AREA],
18150 'I',
18151 glyph->charpos,
18152 (BUFFERP (glyph->object)
18153 ? 'B'
18154 : (STRINGP (glyph->object)
18155 ? 'S'
18156 : (INTEGERP (glyph->object)
18157 ? '0'
18158 : '-'))),
18159 glyph->pixel_width,
18160 glyph->u.img_id,
18161 '.',
18162 glyph->face_id,
18163 glyph->left_box_line_p,
18164 glyph->right_box_line_p);
18166 else if (glyph->type == COMPOSITE_GLYPH)
18168 fprintf (stderr,
18169 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18170 glyph - row->glyphs[TEXT_AREA],
18171 '+',
18172 glyph->charpos,
18173 (BUFFERP (glyph->object)
18174 ? 'B'
18175 : (STRINGP (glyph->object)
18176 ? 'S'
18177 : (INTEGERP (glyph->object)
18178 ? '0'
18179 : '-'))),
18180 glyph->pixel_width,
18181 glyph->u.cmp.id);
18182 if (glyph->u.cmp.automatic)
18183 fprintf (stderr,
18184 "[%d-%d]",
18185 glyph->slice.cmp.from, glyph->slice.cmp.to);
18186 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18187 glyph->face_id,
18188 glyph->left_box_line_p,
18189 glyph->right_box_line_p);
18194 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18195 GLYPHS 0 means don't show glyph contents.
18196 GLYPHS 1 means show glyphs in short form
18197 GLYPHS > 1 means show glyphs in long form. */
18199 void
18200 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18202 if (glyphs != 1)
18204 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18205 fprintf (stderr, "==============================================================================\n");
18207 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18208 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18209 vpos,
18210 MATRIX_ROW_START_CHARPOS (row),
18211 MATRIX_ROW_END_CHARPOS (row),
18212 row->used[TEXT_AREA],
18213 row->contains_overlapping_glyphs_p,
18214 row->enabled_p,
18215 row->truncated_on_left_p,
18216 row->truncated_on_right_p,
18217 row->continued_p,
18218 MATRIX_ROW_CONTINUATION_LINE_P (row),
18219 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18220 row->ends_at_zv_p,
18221 row->fill_line_p,
18222 row->ends_in_middle_of_char_p,
18223 row->starts_in_middle_of_char_p,
18224 row->mouse_face_p,
18225 row->x,
18226 row->y,
18227 row->pixel_width,
18228 row->height,
18229 row->visible_height,
18230 row->ascent,
18231 row->phys_ascent);
18232 /* The next 3 lines should align to "Start" in the header. */
18233 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18234 row->end.overlay_string_index,
18235 row->continuation_lines_width);
18236 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18237 CHARPOS (row->start.string_pos),
18238 CHARPOS (row->end.string_pos));
18239 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18240 row->end.dpvec_index);
18243 if (glyphs > 1)
18245 int area;
18247 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18249 struct glyph *glyph = row->glyphs[area];
18250 struct glyph *glyph_end = glyph + row->used[area];
18252 /* Glyph for a line end in text. */
18253 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18254 ++glyph_end;
18256 if (glyph < glyph_end)
18257 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18259 for (; glyph < glyph_end; ++glyph)
18260 dump_glyph (row, glyph, area);
18263 else if (glyphs == 1)
18265 int area;
18267 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18269 char *s = alloca (row->used[area] + 4);
18270 int i;
18272 for (i = 0; i < row->used[area]; ++i)
18274 struct glyph *glyph = row->glyphs[area] + i;
18275 if (i == row->used[area] - 1
18276 && area == TEXT_AREA
18277 && INTEGERP (glyph->object)
18278 && glyph->type == CHAR_GLYPH
18279 && glyph->u.ch == ' ')
18281 strcpy (&s[i], "[\\n]");
18282 i += 4;
18284 else if (glyph->type == CHAR_GLYPH
18285 && glyph->u.ch < 0x80
18286 && glyph->u.ch >= ' ')
18287 s[i] = glyph->u.ch;
18288 else
18289 s[i] = '.';
18292 s[i] = '\0';
18293 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18299 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18300 Sdump_glyph_matrix, 0, 1, "p",
18301 doc: /* Dump the current matrix of the selected window to stderr.
18302 Shows contents of glyph row structures. With non-nil
18303 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18304 glyphs in short form, otherwise show glyphs in long form. */)
18305 (Lisp_Object glyphs)
18307 struct window *w = XWINDOW (selected_window);
18308 struct buffer *buffer = XBUFFER (w->contents);
18310 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18311 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18312 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18313 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18314 fprintf (stderr, "=============================================\n");
18315 dump_glyph_matrix (w->current_matrix,
18316 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18317 return Qnil;
18321 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18322 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18323 (void)
18325 struct frame *f = XFRAME (selected_frame);
18326 dump_glyph_matrix (f->current_matrix, 1);
18327 return Qnil;
18331 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18332 doc: /* Dump glyph row ROW to stderr.
18333 GLYPH 0 means don't dump glyphs.
18334 GLYPH 1 means dump glyphs in short form.
18335 GLYPH > 1 or omitted means dump glyphs in long form. */)
18336 (Lisp_Object row, Lisp_Object glyphs)
18338 struct glyph_matrix *matrix;
18339 EMACS_INT vpos;
18341 CHECK_NUMBER (row);
18342 matrix = XWINDOW (selected_window)->current_matrix;
18343 vpos = XINT (row);
18344 if (vpos >= 0 && vpos < matrix->nrows)
18345 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18346 vpos,
18347 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18348 return Qnil;
18352 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18353 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18354 GLYPH 0 means don't dump glyphs.
18355 GLYPH 1 means dump glyphs in short form.
18356 GLYPH > 1 or omitted means dump glyphs in long form.
18358 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18359 do nothing. */)
18360 (Lisp_Object row, Lisp_Object glyphs)
18362 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18363 struct frame *sf = SELECTED_FRAME ();
18364 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18365 EMACS_INT vpos;
18367 CHECK_NUMBER (row);
18368 vpos = XINT (row);
18369 if (vpos >= 0 && vpos < m->nrows)
18370 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18371 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18372 #endif
18373 return Qnil;
18377 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18378 doc: /* Toggle tracing of redisplay.
18379 With ARG, turn tracing on if and only if ARG is positive. */)
18380 (Lisp_Object arg)
18382 if (NILP (arg))
18383 trace_redisplay_p = !trace_redisplay_p;
18384 else
18386 arg = Fprefix_numeric_value (arg);
18387 trace_redisplay_p = XINT (arg) > 0;
18390 return Qnil;
18394 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18395 doc: /* Like `format', but print result to stderr.
18396 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18397 (ptrdiff_t nargs, Lisp_Object *args)
18399 Lisp_Object s = Fformat (nargs, args);
18400 fprintf (stderr, "%s", SDATA (s));
18401 return Qnil;
18404 #endif /* GLYPH_DEBUG */
18408 /***********************************************************************
18409 Building Desired Matrix Rows
18410 ***********************************************************************/
18412 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18413 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18415 static struct glyph_row *
18416 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18418 struct frame *f = XFRAME (WINDOW_FRAME (w));
18419 struct buffer *buffer = XBUFFER (w->contents);
18420 struct buffer *old = current_buffer;
18421 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18422 int arrow_len = SCHARS (overlay_arrow_string);
18423 const unsigned char *arrow_end = arrow_string + arrow_len;
18424 const unsigned char *p;
18425 struct it it;
18426 bool multibyte_p;
18427 int n_glyphs_before;
18429 set_buffer_temp (buffer);
18430 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18431 it.glyph_row->used[TEXT_AREA] = 0;
18432 SET_TEXT_POS (it.position, 0, 0);
18434 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18435 p = arrow_string;
18436 while (p < arrow_end)
18438 Lisp_Object face, ilisp;
18440 /* Get the next character. */
18441 if (multibyte_p)
18442 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18443 else
18445 it.c = it.char_to_display = *p, it.len = 1;
18446 if (! ASCII_CHAR_P (it.c))
18447 it.char_to_display = BYTE8_TO_CHAR (it.c);
18449 p += it.len;
18451 /* Get its face. */
18452 ilisp = make_number (p - arrow_string);
18453 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18454 it.face_id = compute_char_face (f, it.char_to_display, face);
18456 /* Compute its width, get its glyphs. */
18457 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18458 SET_TEXT_POS (it.position, -1, -1);
18459 PRODUCE_GLYPHS (&it);
18461 /* If this character doesn't fit any more in the line, we have
18462 to remove some glyphs. */
18463 if (it.current_x > it.last_visible_x)
18465 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18466 break;
18470 set_buffer_temp (old);
18471 return it.glyph_row;
18475 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18476 glyphs to insert is determined by produce_special_glyphs. */
18478 static void
18479 insert_left_trunc_glyphs (struct it *it)
18481 struct it truncate_it;
18482 struct glyph *from, *end, *to, *toend;
18484 eassert (!FRAME_WINDOW_P (it->f)
18485 || (!it->glyph_row->reversed_p
18486 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18487 || (it->glyph_row->reversed_p
18488 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18490 /* Get the truncation glyphs. */
18491 truncate_it = *it;
18492 truncate_it.current_x = 0;
18493 truncate_it.face_id = DEFAULT_FACE_ID;
18494 truncate_it.glyph_row = &scratch_glyph_row;
18495 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18496 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18497 truncate_it.object = make_number (0);
18498 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18500 /* Overwrite glyphs from IT with truncation glyphs. */
18501 if (!it->glyph_row->reversed_p)
18503 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18505 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18506 end = from + tused;
18507 to = it->glyph_row->glyphs[TEXT_AREA];
18508 toend = to + it->glyph_row->used[TEXT_AREA];
18509 if (FRAME_WINDOW_P (it->f))
18511 /* On GUI frames, when variable-size fonts are displayed,
18512 the truncation glyphs may need more pixels than the row's
18513 glyphs they overwrite. We overwrite more glyphs to free
18514 enough screen real estate, and enlarge the stretch glyph
18515 on the right (see display_line), if there is one, to
18516 preserve the screen position of the truncation glyphs on
18517 the right. */
18518 int w = 0;
18519 struct glyph *g = to;
18520 short used;
18522 /* The first glyph could be partially visible, in which case
18523 it->glyph_row->x will be negative. But we want the left
18524 truncation glyphs to be aligned at the left margin of the
18525 window, so we override the x coordinate at which the row
18526 will begin. */
18527 it->glyph_row->x = 0;
18528 while (g < toend && w < it->truncation_pixel_width)
18530 w += g->pixel_width;
18531 ++g;
18533 if (g - to - tused > 0)
18535 memmove (to + tused, g, (toend - g) * sizeof(*g));
18536 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18538 used = it->glyph_row->used[TEXT_AREA];
18539 if (it->glyph_row->truncated_on_right_p
18540 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18541 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18542 == STRETCH_GLYPH)
18544 int extra = w - it->truncation_pixel_width;
18546 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18550 while (from < end)
18551 *to++ = *from++;
18553 /* There may be padding glyphs left over. Overwrite them too. */
18554 if (!FRAME_WINDOW_P (it->f))
18556 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18558 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18559 while (from < end)
18560 *to++ = *from++;
18564 if (to > toend)
18565 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18567 else
18569 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18571 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18572 that back to front. */
18573 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18574 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18575 toend = it->glyph_row->glyphs[TEXT_AREA];
18576 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18577 if (FRAME_WINDOW_P (it->f))
18579 int w = 0;
18580 struct glyph *g = to;
18582 while (g >= toend && w < it->truncation_pixel_width)
18584 w += g->pixel_width;
18585 --g;
18587 if (to - g - tused > 0)
18588 to = g + tused;
18589 if (it->glyph_row->truncated_on_right_p
18590 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18591 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18593 int extra = w - it->truncation_pixel_width;
18595 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18599 while (from >= end && to >= toend)
18600 *to-- = *from--;
18601 if (!FRAME_WINDOW_P (it->f))
18603 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18605 from =
18606 truncate_it.glyph_row->glyphs[TEXT_AREA]
18607 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18608 while (from >= end && to >= toend)
18609 *to-- = *from--;
18612 if (from >= end)
18614 /* Need to free some room before prepending additional
18615 glyphs. */
18616 int move_by = from - end + 1;
18617 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18618 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18620 for ( ; g >= g0; g--)
18621 g[move_by] = *g;
18622 while (from >= end)
18623 *to-- = *from--;
18624 it->glyph_row->used[TEXT_AREA] += move_by;
18629 /* Compute the hash code for ROW. */
18630 unsigned
18631 row_hash (struct glyph_row *row)
18633 int area, k;
18634 unsigned hashval = 0;
18636 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18637 for (k = 0; k < row->used[area]; ++k)
18638 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18639 + row->glyphs[area][k].u.val
18640 + row->glyphs[area][k].face_id
18641 + row->glyphs[area][k].padding_p
18642 + (row->glyphs[area][k].type << 2));
18644 return hashval;
18647 /* Compute the pixel height and width of IT->glyph_row.
18649 Most of the time, ascent and height of a display line will be equal
18650 to the max_ascent and max_height values of the display iterator
18651 structure. This is not the case if
18653 1. We hit ZV without displaying anything. In this case, max_ascent
18654 and max_height will be zero.
18656 2. We have some glyphs that don't contribute to the line height.
18657 (The glyph row flag contributes_to_line_height_p is for future
18658 pixmap extensions).
18660 The first case is easily covered by using default values because in
18661 these cases, the line height does not really matter, except that it
18662 must not be zero. */
18664 static void
18665 compute_line_metrics (struct it *it)
18667 struct glyph_row *row = it->glyph_row;
18669 if (FRAME_WINDOW_P (it->f))
18671 int i, min_y, max_y;
18673 /* The line may consist of one space only, that was added to
18674 place the cursor on it. If so, the row's height hasn't been
18675 computed yet. */
18676 if (row->height == 0)
18678 if (it->max_ascent + it->max_descent == 0)
18679 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18680 row->ascent = it->max_ascent;
18681 row->height = it->max_ascent + it->max_descent;
18682 row->phys_ascent = it->max_phys_ascent;
18683 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18684 row->extra_line_spacing = it->max_extra_line_spacing;
18687 /* Compute the width of this line. */
18688 row->pixel_width = row->x;
18689 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18690 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18692 eassert (row->pixel_width >= 0);
18693 eassert (row->ascent >= 0 && row->height > 0);
18695 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18696 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18698 /* If first line's physical ascent is larger than its logical
18699 ascent, use the physical ascent, and make the row taller.
18700 This makes accented characters fully visible. */
18701 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18702 && row->phys_ascent > row->ascent)
18704 row->height += row->phys_ascent - row->ascent;
18705 row->ascent = row->phys_ascent;
18708 /* Compute how much of the line is visible. */
18709 row->visible_height = row->height;
18711 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18712 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18714 if (row->y < min_y)
18715 row->visible_height -= min_y - row->y;
18716 if (row->y + row->height > max_y)
18717 row->visible_height -= row->y + row->height - max_y;
18719 else
18721 row->pixel_width = row->used[TEXT_AREA];
18722 if (row->continued_p)
18723 row->pixel_width -= it->continuation_pixel_width;
18724 else if (row->truncated_on_right_p)
18725 row->pixel_width -= it->truncation_pixel_width;
18726 row->ascent = row->phys_ascent = 0;
18727 row->height = row->phys_height = row->visible_height = 1;
18728 row->extra_line_spacing = 0;
18731 /* Compute a hash code for this row. */
18732 row->hash = row_hash (row);
18734 it->max_ascent = it->max_descent = 0;
18735 it->max_phys_ascent = it->max_phys_descent = 0;
18739 /* Append one space to the glyph row of iterator IT if doing a
18740 window-based redisplay. The space has the same face as
18741 IT->face_id. Value is non-zero if a space was added.
18743 This function is called to make sure that there is always one glyph
18744 at the end of a glyph row that the cursor can be set on under
18745 window-systems. (If there weren't such a glyph we would not know
18746 how wide and tall a box cursor should be displayed).
18748 At the same time this space let's a nicely handle clearing to the
18749 end of the line if the row ends in italic text. */
18751 static int
18752 append_space_for_newline (struct it *it, int default_face_p)
18754 if (FRAME_WINDOW_P (it->f))
18756 int n = it->glyph_row->used[TEXT_AREA];
18758 if (it->glyph_row->glyphs[TEXT_AREA] + n
18759 < it->glyph_row->glyphs[1 + TEXT_AREA])
18761 /* Save some values that must not be changed.
18762 Must save IT->c and IT->len because otherwise
18763 ITERATOR_AT_END_P wouldn't work anymore after
18764 append_space_for_newline has been called. */
18765 enum display_element_type saved_what = it->what;
18766 int saved_c = it->c, saved_len = it->len;
18767 int saved_char_to_display = it->char_to_display;
18768 int saved_x = it->current_x;
18769 int saved_face_id = it->face_id;
18770 int saved_box_end = it->end_of_box_run_p;
18771 struct text_pos saved_pos;
18772 Lisp_Object saved_object;
18773 struct face *face;
18775 saved_object = it->object;
18776 saved_pos = it->position;
18778 it->what = IT_CHARACTER;
18779 memset (&it->position, 0, sizeof it->position);
18780 it->object = make_number (0);
18781 it->c = it->char_to_display = ' ';
18782 it->len = 1;
18784 /* If the default face was remapped, be sure to use the
18785 remapped face for the appended newline. */
18786 if (default_face_p)
18787 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18788 else if (it->face_before_selective_p)
18789 it->face_id = it->saved_face_id;
18790 face = FACE_FROM_ID (it->f, it->face_id);
18791 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18792 /* In R2L rows, we will prepend a stretch glyph that will
18793 have the end_of_box_run_p flag set for it, so there's no
18794 need for the appended newline glyph to have that flag
18795 set. */
18796 if (it->glyph_row->reversed_p
18797 /* But if the appended newline glyph goes all the way to
18798 the end of the row, there will be no stretch glyph,
18799 so leave the box flag set. */
18800 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18801 it->end_of_box_run_p = 0;
18803 PRODUCE_GLYPHS (it);
18805 it->override_ascent = -1;
18806 it->constrain_row_ascent_descent_p = 0;
18807 it->current_x = saved_x;
18808 it->object = saved_object;
18809 it->position = saved_pos;
18810 it->what = saved_what;
18811 it->face_id = saved_face_id;
18812 it->len = saved_len;
18813 it->c = saved_c;
18814 it->char_to_display = saved_char_to_display;
18815 it->end_of_box_run_p = saved_box_end;
18816 return 1;
18820 return 0;
18824 /* Extend the face of the last glyph in the text area of IT->glyph_row
18825 to the end of the display line. Called from display_line. If the
18826 glyph row is empty, add a space glyph to it so that we know the
18827 face to draw. Set the glyph row flag fill_line_p. If the glyph
18828 row is R2L, prepend a stretch glyph to cover the empty space to the
18829 left of the leftmost glyph. */
18831 static void
18832 extend_face_to_end_of_line (struct it *it)
18834 struct face *face, *default_face;
18835 struct frame *f = it->f;
18837 /* If line is already filled, do nothing. Non window-system frames
18838 get a grace of one more ``pixel'' because their characters are
18839 1-``pixel'' wide, so they hit the equality too early. This grace
18840 is needed only for R2L rows that are not continued, to produce
18841 one extra blank where we could display the cursor. */
18842 if ((it->current_x >= it->last_visible_x
18843 + (!FRAME_WINDOW_P (f)
18844 && it->glyph_row->reversed_p
18845 && !it->glyph_row->continued_p))
18846 /* If the window has display margins, we will need to extend
18847 their face even if the text area is filled. */
18848 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18849 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18850 return;
18852 /* The default face, possibly remapped. */
18853 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18855 /* Face extension extends the background and box of IT->face_id
18856 to the end of the line. If the background equals the background
18857 of the frame, we don't have to do anything. */
18858 if (it->face_before_selective_p)
18859 face = FACE_FROM_ID (f, it->saved_face_id);
18860 else
18861 face = FACE_FROM_ID (f, it->face_id);
18863 if (FRAME_WINDOW_P (f)
18864 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18865 && face->box == FACE_NO_BOX
18866 && face->background == FRAME_BACKGROUND_PIXEL (f)
18867 #ifdef HAVE_WINDOW_SYSTEM
18868 && !face->stipple
18869 #endif
18870 && !it->glyph_row->reversed_p)
18871 return;
18873 /* Set the glyph row flag indicating that the face of the last glyph
18874 in the text area has to be drawn to the end of the text area. */
18875 it->glyph_row->fill_line_p = 1;
18877 /* If current character of IT is not ASCII, make sure we have the
18878 ASCII face. This will be automatically undone the next time
18879 get_next_display_element returns a multibyte character. Note
18880 that the character will always be single byte in unibyte
18881 text. */
18882 if (!ASCII_CHAR_P (it->c))
18884 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18887 if (FRAME_WINDOW_P (f))
18889 /* If the row is empty, add a space with the current face of IT,
18890 so that we know which face to draw. */
18891 if (it->glyph_row->used[TEXT_AREA] == 0)
18893 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18894 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18895 it->glyph_row->used[TEXT_AREA] = 1;
18897 /* Mode line and the header line don't have margins, and
18898 likewise the frame's tool-bar window, if there is any. */
18899 if (!(it->glyph_row->mode_line_p
18900 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18901 || (WINDOWP (f->tool_bar_window)
18902 && it->w == XWINDOW (f->tool_bar_window))
18903 #endif
18906 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18907 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18909 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18910 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18911 default_face->id;
18912 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18914 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18915 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18917 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18918 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18919 default_face->id;
18920 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18923 #ifdef HAVE_WINDOW_SYSTEM
18924 if (it->glyph_row->reversed_p)
18926 /* Prepend a stretch glyph to the row, such that the
18927 rightmost glyph will be drawn flushed all the way to the
18928 right margin of the window. The stretch glyph that will
18929 occupy the empty space, if any, to the left of the
18930 glyphs. */
18931 struct font *font = face->font ? face->font : FRAME_FONT (f);
18932 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18933 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18934 struct glyph *g;
18935 int row_width, stretch_ascent, stretch_width;
18936 struct text_pos saved_pos;
18937 int saved_face_id, saved_avoid_cursor, saved_box_start;
18939 for (row_width = 0, g = row_start; g < row_end; g++)
18940 row_width += g->pixel_width;
18941 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18942 if (stretch_width > 0)
18944 stretch_ascent =
18945 (((it->ascent + it->descent)
18946 * FONT_BASE (font)) / FONT_HEIGHT (font));
18947 saved_pos = it->position;
18948 memset (&it->position, 0, sizeof it->position);
18949 saved_avoid_cursor = it->avoid_cursor_p;
18950 it->avoid_cursor_p = 1;
18951 saved_face_id = it->face_id;
18952 saved_box_start = it->start_of_box_run_p;
18953 /* The last row's stretch glyph should get the default
18954 face, to avoid painting the rest of the window with
18955 the region face, if the region ends at ZV. */
18956 if (it->glyph_row->ends_at_zv_p)
18957 it->face_id = default_face->id;
18958 else
18959 it->face_id = face->id;
18960 it->start_of_box_run_p = 0;
18961 append_stretch_glyph (it, make_number (0), stretch_width,
18962 it->ascent + it->descent, stretch_ascent);
18963 it->position = saved_pos;
18964 it->avoid_cursor_p = saved_avoid_cursor;
18965 it->face_id = saved_face_id;
18966 it->start_of_box_run_p = saved_box_start;
18969 #endif /* HAVE_WINDOW_SYSTEM */
18971 else
18973 /* Save some values that must not be changed. */
18974 int saved_x = it->current_x;
18975 struct text_pos saved_pos;
18976 Lisp_Object saved_object;
18977 enum display_element_type saved_what = it->what;
18978 int saved_face_id = it->face_id;
18980 saved_object = it->object;
18981 saved_pos = it->position;
18983 it->what = IT_CHARACTER;
18984 memset (&it->position, 0, sizeof it->position);
18985 it->object = make_number (0);
18986 it->c = it->char_to_display = ' ';
18987 it->len = 1;
18989 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18990 && (it->glyph_row->used[LEFT_MARGIN_AREA]
18991 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
18992 && !it->glyph_row->mode_line_p
18993 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
18995 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
18996 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
18998 for (it->current_x = 0; g < e; g++)
18999 it->current_x += g->pixel_width;
19001 it->area = LEFT_MARGIN_AREA;
19002 it->face_id = default_face->id;
19003 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19004 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19006 PRODUCE_GLYPHS (it);
19007 /* term.c:produce_glyphs advances it->current_x only for
19008 TEXT_AREA. */
19009 it->current_x += it->pixel_width;
19012 it->current_x = saved_x;
19013 it->area = TEXT_AREA;
19016 /* The last row's blank glyphs should get the default face, to
19017 avoid painting the rest of the window with the region face,
19018 if the region ends at ZV. */
19019 if (it->glyph_row->ends_at_zv_p)
19020 it->face_id = default_face->id;
19021 else
19022 it->face_id = face->id;
19023 PRODUCE_GLYPHS (it);
19025 while (it->current_x <= it->last_visible_x)
19026 PRODUCE_GLYPHS (it);
19028 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19029 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19030 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19031 && !it->glyph_row->mode_line_p
19032 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19034 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19035 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19037 for ( ; g < e; g++)
19038 it->current_x += g->pixel_width;
19040 it->area = RIGHT_MARGIN_AREA;
19041 it->face_id = default_face->id;
19042 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19043 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19045 PRODUCE_GLYPHS (it);
19046 it->current_x += it->pixel_width;
19049 it->area = TEXT_AREA;
19052 /* Don't count these blanks really. It would let us insert a left
19053 truncation glyph below and make us set the cursor on them, maybe. */
19054 it->current_x = saved_x;
19055 it->object = saved_object;
19056 it->position = saved_pos;
19057 it->what = saved_what;
19058 it->face_id = saved_face_id;
19063 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19064 trailing whitespace. */
19066 static int
19067 trailing_whitespace_p (ptrdiff_t charpos)
19069 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19070 int c = 0;
19072 while (bytepos < ZV_BYTE
19073 && (c = FETCH_CHAR (bytepos),
19074 c == ' ' || c == '\t'))
19075 ++bytepos;
19077 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19079 if (bytepos != PT_BYTE)
19080 return 1;
19082 return 0;
19086 /* Highlight trailing whitespace, if any, in ROW. */
19088 static void
19089 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19091 int used = row->used[TEXT_AREA];
19093 if (used)
19095 struct glyph *start = row->glyphs[TEXT_AREA];
19096 struct glyph *glyph = start + used - 1;
19098 if (row->reversed_p)
19100 /* Right-to-left rows need to be processed in the opposite
19101 direction, so swap the edge pointers. */
19102 glyph = start;
19103 start = row->glyphs[TEXT_AREA] + used - 1;
19106 /* Skip over glyphs inserted to display the cursor at the
19107 end of a line, for extending the face of the last glyph
19108 to the end of the line on terminals, and for truncation
19109 and continuation glyphs. */
19110 if (!row->reversed_p)
19112 while (glyph >= start
19113 && glyph->type == CHAR_GLYPH
19114 && INTEGERP (glyph->object))
19115 --glyph;
19117 else
19119 while (glyph <= start
19120 && glyph->type == CHAR_GLYPH
19121 && INTEGERP (glyph->object))
19122 ++glyph;
19125 /* If last glyph is a space or stretch, and it's trailing
19126 whitespace, set the face of all trailing whitespace glyphs in
19127 IT->glyph_row to `trailing-whitespace'. */
19128 if ((row->reversed_p ? glyph <= start : glyph >= start)
19129 && BUFFERP (glyph->object)
19130 && (glyph->type == STRETCH_GLYPH
19131 || (glyph->type == CHAR_GLYPH
19132 && glyph->u.ch == ' '))
19133 && trailing_whitespace_p (glyph->charpos))
19135 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19136 if (face_id < 0)
19137 return;
19139 if (!row->reversed_p)
19141 while (glyph >= start
19142 && BUFFERP (glyph->object)
19143 && (glyph->type == STRETCH_GLYPH
19144 || (glyph->type == CHAR_GLYPH
19145 && glyph->u.ch == ' ')))
19146 (glyph--)->face_id = face_id;
19148 else
19150 while (glyph <= start
19151 && BUFFERP (glyph->object)
19152 && (glyph->type == STRETCH_GLYPH
19153 || (glyph->type == CHAR_GLYPH
19154 && glyph->u.ch == ' ')))
19155 (glyph++)->face_id = face_id;
19162 /* Value is non-zero if glyph row ROW should be
19163 considered to hold the buffer position CHARPOS. */
19165 static int
19166 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19168 int result = 1;
19170 if (charpos == CHARPOS (row->end.pos)
19171 || charpos == MATRIX_ROW_END_CHARPOS (row))
19173 /* Suppose the row ends on a string.
19174 Unless the row is continued, that means it ends on a newline
19175 in the string. If it's anything other than a display string
19176 (e.g., a before-string from an overlay), we don't want the
19177 cursor there. (This heuristic seems to give the optimal
19178 behavior for the various types of multi-line strings.)
19179 One exception: if the string has `cursor' property on one of
19180 its characters, we _do_ want the cursor there. */
19181 if (CHARPOS (row->end.string_pos) >= 0)
19183 if (row->continued_p)
19184 result = 1;
19185 else
19187 /* Check for `display' property. */
19188 struct glyph *beg = row->glyphs[TEXT_AREA];
19189 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19190 struct glyph *glyph;
19192 result = 0;
19193 for (glyph = end; glyph >= beg; --glyph)
19194 if (STRINGP (glyph->object))
19196 Lisp_Object prop
19197 = Fget_char_property (make_number (charpos),
19198 Qdisplay, Qnil);
19199 result =
19200 (!NILP (prop)
19201 && display_prop_string_p (prop, glyph->object));
19202 /* If there's a `cursor' property on one of the
19203 string's characters, this row is a cursor row,
19204 even though this is not a display string. */
19205 if (!result)
19207 Lisp_Object s = glyph->object;
19209 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19211 ptrdiff_t gpos = glyph->charpos;
19213 if (!NILP (Fget_char_property (make_number (gpos),
19214 Qcursor, s)))
19216 result = 1;
19217 break;
19221 break;
19225 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19227 /* If the row ends in middle of a real character,
19228 and the line is continued, we want the cursor here.
19229 That's because CHARPOS (ROW->end.pos) would equal
19230 PT if PT is before the character. */
19231 if (!row->ends_in_ellipsis_p)
19232 result = row->continued_p;
19233 else
19234 /* If the row ends in an ellipsis, then
19235 CHARPOS (ROW->end.pos) will equal point after the
19236 invisible text. We want that position to be displayed
19237 after the ellipsis. */
19238 result = 0;
19240 /* If the row ends at ZV, display the cursor at the end of that
19241 row instead of at the start of the row below. */
19242 else if (row->ends_at_zv_p)
19243 result = 1;
19244 else
19245 result = 0;
19248 return result;
19251 /* Value is non-zero if glyph row ROW should be
19252 used to hold the cursor. */
19254 static int
19255 cursor_row_p (struct glyph_row *row)
19257 return row_for_charpos_p (row, PT);
19262 /* Push the property PROP so that it will be rendered at the current
19263 position in IT. Return 1 if PROP was successfully pushed, 0
19264 otherwise. Called from handle_line_prefix to handle the
19265 `line-prefix' and `wrap-prefix' properties. */
19267 static int
19268 push_prefix_prop (struct it *it, Lisp_Object prop)
19270 struct text_pos pos =
19271 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19273 eassert (it->method == GET_FROM_BUFFER
19274 || it->method == GET_FROM_DISPLAY_VECTOR
19275 || it->method == GET_FROM_STRING);
19277 /* We need to save the current buffer/string position, so it will be
19278 restored by pop_it, because iterate_out_of_display_property
19279 depends on that being set correctly, but some situations leave
19280 it->position not yet set when this function is called. */
19281 push_it (it, &pos);
19283 if (STRINGP (prop))
19285 if (SCHARS (prop) == 0)
19287 pop_it (it);
19288 return 0;
19291 it->string = prop;
19292 it->string_from_prefix_prop_p = 1;
19293 it->multibyte_p = STRING_MULTIBYTE (it->string);
19294 it->current.overlay_string_index = -1;
19295 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19296 it->end_charpos = it->string_nchars = SCHARS (it->string);
19297 it->method = GET_FROM_STRING;
19298 it->stop_charpos = 0;
19299 it->prev_stop = 0;
19300 it->base_level_stop = 0;
19302 /* Force paragraph direction to be that of the parent
19303 buffer/string. */
19304 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19305 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19306 else
19307 it->paragraph_embedding = L2R;
19309 /* Set up the bidi iterator for this display string. */
19310 if (it->bidi_p)
19312 it->bidi_it.string.lstring = it->string;
19313 it->bidi_it.string.s = NULL;
19314 it->bidi_it.string.schars = it->end_charpos;
19315 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19316 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19317 it->bidi_it.string.unibyte = !it->multibyte_p;
19318 it->bidi_it.w = it->w;
19319 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19322 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19324 it->method = GET_FROM_STRETCH;
19325 it->object = prop;
19327 #ifdef HAVE_WINDOW_SYSTEM
19328 else if (IMAGEP (prop))
19330 it->what = IT_IMAGE;
19331 it->image_id = lookup_image (it->f, prop);
19332 it->method = GET_FROM_IMAGE;
19334 #endif /* HAVE_WINDOW_SYSTEM */
19335 else
19337 pop_it (it); /* bogus display property, give up */
19338 return 0;
19341 return 1;
19344 /* Return the character-property PROP at the current position in IT. */
19346 static Lisp_Object
19347 get_it_property (struct it *it, Lisp_Object prop)
19349 Lisp_Object position, object = it->object;
19351 if (STRINGP (object))
19352 position = make_number (IT_STRING_CHARPOS (*it));
19353 else if (BUFFERP (object))
19355 position = make_number (IT_CHARPOS (*it));
19356 object = it->window;
19358 else
19359 return Qnil;
19361 return Fget_char_property (position, prop, object);
19364 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19366 static void
19367 handle_line_prefix (struct it *it)
19369 Lisp_Object prefix;
19371 if (it->continuation_lines_width > 0)
19373 prefix = get_it_property (it, Qwrap_prefix);
19374 if (NILP (prefix))
19375 prefix = Vwrap_prefix;
19377 else
19379 prefix = get_it_property (it, Qline_prefix);
19380 if (NILP (prefix))
19381 prefix = Vline_prefix;
19383 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19385 /* If the prefix is wider than the window, and we try to wrap
19386 it, it would acquire its own wrap prefix, and so on till the
19387 iterator stack overflows. So, don't wrap the prefix. */
19388 it->line_wrap = TRUNCATE;
19389 it->avoid_cursor_p = 1;
19395 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19396 only for R2L lines from display_line and display_string, when they
19397 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19398 the line/string needs to be continued on the next glyph row. */
19399 static void
19400 unproduce_glyphs (struct it *it, int n)
19402 struct glyph *glyph, *end;
19404 eassert (it->glyph_row);
19405 eassert (it->glyph_row->reversed_p);
19406 eassert (it->area == TEXT_AREA);
19407 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19409 if (n > it->glyph_row->used[TEXT_AREA])
19410 n = it->glyph_row->used[TEXT_AREA];
19411 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19412 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19413 for ( ; glyph < end; glyph++)
19414 glyph[-n] = *glyph;
19417 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19418 and ROW->maxpos. */
19419 static void
19420 find_row_edges (struct it *it, struct glyph_row *row,
19421 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19422 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19424 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19425 lines' rows is implemented for bidi-reordered rows. */
19427 /* ROW->minpos is the value of min_pos, the minimal buffer position
19428 we have in ROW, or ROW->start.pos if that is smaller. */
19429 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19430 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19431 else
19432 /* We didn't find buffer positions smaller than ROW->start, or
19433 didn't find _any_ valid buffer positions in any of the glyphs,
19434 so we must trust the iterator's computed positions. */
19435 row->minpos = row->start.pos;
19436 if (max_pos <= 0)
19438 max_pos = CHARPOS (it->current.pos);
19439 max_bpos = BYTEPOS (it->current.pos);
19442 /* Here are the various use-cases for ending the row, and the
19443 corresponding values for ROW->maxpos:
19445 Line ends in a newline from buffer eol_pos + 1
19446 Line is continued from buffer max_pos + 1
19447 Line is truncated on right it->current.pos
19448 Line ends in a newline from string max_pos + 1(*)
19449 (*) + 1 only when line ends in a forward scan
19450 Line is continued from string max_pos
19451 Line is continued from display vector max_pos
19452 Line is entirely from a string min_pos == max_pos
19453 Line is entirely from a display vector min_pos == max_pos
19454 Line that ends at ZV ZV
19456 If you discover other use-cases, please add them here as
19457 appropriate. */
19458 if (row->ends_at_zv_p)
19459 row->maxpos = it->current.pos;
19460 else if (row->used[TEXT_AREA])
19462 int seen_this_string = 0;
19463 struct glyph_row *r1 = row - 1;
19465 /* Did we see the same display string on the previous row? */
19466 if (STRINGP (it->object)
19467 /* this is not the first row */
19468 && row > it->w->desired_matrix->rows
19469 /* previous row is not the header line */
19470 && !r1->mode_line_p
19471 /* previous row also ends in a newline from a string */
19472 && r1->ends_in_newline_from_string_p)
19474 struct glyph *start, *end;
19476 /* Search for the last glyph of the previous row that came
19477 from buffer or string. Depending on whether the row is
19478 L2R or R2L, we need to process it front to back or the
19479 other way round. */
19480 if (!r1->reversed_p)
19482 start = r1->glyphs[TEXT_AREA];
19483 end = start + r1->used[TEXT_AREA];
19484 /* Glyphs inserted by redisplay have an integer (zero)
19485 as their object. */
19486 while (end > start
19487 && INTEGERP ((end - 1)->object)
19488 && (end - 1)->charpos <= 0)
19489 --end;
19490 if (end > start)
19492 if (EQ ((end - 1)->object, it->object))
19493 seen_this_string = 1;
19495 else
19496 /* If all the glyphs of the previous row were inserted
19497 by redisplay, it means the previous row was
19498 produced from a single newline, which is only
19499 possible if that newline came from the same string
19500 as the one which produced this ROW. */
19501 seen_this_string = 1;
19503 else
19505 end = r1->glyphs[TEXT_AREA] - 1;
19506 start = end + r1->used[TEXT_AREA];
19507 while (end < start
19508 && INTEGERP ((end + 1)->object)
19509 && (end + 1)->charpos <= 0)
19510 ++end;
19511 if (end < start)
19513 if (EQ ((end + 1)->object, it->object))
19514 seen_this_string = 1;
19516 else
19517 seen_this_string = 1;
19520 /* Take note of each display string that covers a newline only
19521 once, the first time we see it. This is for when a display
19522 string includes more than one newline in it. */
19523 if (row->ends_in_newline_from_string_p && !seen_this_string)
19525 /* If we were scanning the buffer forward when we displayed
19526 the string, we want to account for at least one buffer
19527 position that belongs to this row (position covered by
19528 the display string), so that cursor positioning will
19529 consider this row as a candidate when point is at the end
19530 of the visual line represented by this row. This is not
19531 required when scanning back, because max_pos will already
19532 have a much larger value. */
19533 if (CHARPOS (row->end.pos) > max_pos)
19534 INC_BOTH (max_pos, max_bpos);
19535 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19537 else if (CHARPOS (it->eol_pos) > 0)
19538 SET_TEXT_POS (row->maxpos,
19539 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19540 else if (row->continued_p)
19542 /* If max_pos is different from IT's current position, it
19543 means IT->method does not belong to the display element
19544 at max_pos. However, it also means that the display
19545 element at max_pos was displayed in its entirety on this
19546 line, which is equivalent to saying that the next line
19547 starts at the next buffer position. */
19548 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19549 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19550 else
19552 INC_BOTH (max_pos, max_bpos);
19553 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19556 else if (row->truncated_on_right_p)
19557 /* display_line already called reseat_at_next_visible_line_start,
19558 which puts the iterator at the beginning of the next line, in
19559 the logical order. */
19560 row->maxpos = it->current.pos;
19561 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19562 /* A line that is entirely from a string/image/stretch... */
19563 row->maxpos = row->minpos;
19564 else
19565 emacs_abort ();
19567 else
19568 row->maxpos = it->current.pos;
19571 /* Construct the glyph row IT->glyph_row in the desired matrix of
19572 IT->w from text at the current position of IT. See dispextern.h
19573 for an overview of struct it. Value is non-zero if
19574 IT->glyph_row displays text, as opposed to a line displaying ZV
19575 only. */
19577 static int
19578 display_line (struct it *it)
19580 struct glyph_row *row = it->glyph_row;
19581 Lisp_Object overlay_arrow_string;
19582 struct it wrap_it;
19583 void *wrap_data = NULL;
19584 int may_wrap = 0, wrap_x IF_LINT (= 0);
19585 int wrap_row_used = -1;
19586 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19587 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19588 int wrap_row_extra_line_spacing IF_LINT (= 0);
19589 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19590 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19591 int cvpos;
19592 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19593 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19595 /* We always start displaying at hpos zero even if hscrolled. */
19596 eassert (it->hpos == 0 && it->current_x == 0);
19598 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19599 >= it->w->desired_matrix->nrows)
19601 it->w->nrows_scale_factor++;
19602 it->f->fonts_changed = 1;
19603 return 0;
19606 /* Clear the result glyph row and enable it. */
19607 prepare_desired_row (row);
19609 row->y = it->current_y;
19610 row->start = it->start;
19611 row->continuation_lines_width = it->continuation_lines_width;
19612 row->displays_text_p = 1;
19613 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19614 it->starts_in_middle_of_char_p = 0;
19616 /* Arrange the overlays nicely for our purposes. Usually, we call
19617 display_line on only one line at a time, in which case this
19618 can't really hurt too much, or we call it on lines which appear
19619 one after another in the buffer, in which case all calls to
19620 recenter_overlay_lists but the first will be pretty cheap. */
19621 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19623 /* Move over display elements that are not visible because we are
19624 hscrolled. This may stop at an x-position < IT->first_visible_x
19625 if the first glyph is partially visible or if we hit a line end. */
19626 if (it->current_x < it->first_visible_x)
19628 enum move_it_result move_result;
19630 this_line_min_pos = row->start.pos;
19631 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19632 MOVE_TO_POS | MOVE_TO_X);
19633 /* If we are under a large hscroll, move_it_in_display_line_to
19634 could hit the end of the line without reaching
19635 it->first_visible_x. Pretend that we did reach it. This is
19636 especially important on a TTY, where we will call
19637 extend_face_to_end_of_line, which needs to know how many
19638 blank glyphs to produce. */
19639 if (it->current_x < it->first_visible_x
19640 && (move_result == MOVE_NEWLINE_OR_CR
19641 || move_result == MOVE_POS_MATCH_OR_ZV))
19642 it->current_x = it->first_visible_x;
19644 /* Record the smallest positions seen while we moved over
19645 display elements that are not visible. This is needed by
19646 redisplay_internal for optimizing the case where the cursor
19647 stays inside the same line. The rest of this function only
19648 considers positions that are actually displayed, so
19649 RECORD_MAX_MIN_POS will not otherwise record positions that
19650 are hscrolled to the left of the left edge of the window. */
19651 min_pos = CHARPOS (this_line_min_pos);
19652 min_bpos = BYTEPOS (this_line_min_pos);
19654 else
19656 /* We only do this when not calling `move_it_in_display_line_to'
19657 above, because move_it_in_display_line_to calls
19658 handle_line_prefix itself. */
19659 handle_line_prefix (it);
19662 /* Get the initial row height. This is either the height of the
19663 text hscrolled, if there is any, or zero. */
19664 row->ascent = it->max_ascent;
19665 row->height = it->max_ascent + it->max_descent;
19666 row->phys_ascent = it->max_phys_ascent;
19667 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19668 row->extra_line_spacing = it->max_extra_line_spacing;
19670 /* Utility macro to record max and min buffer positions seen until now. */
19671 #define RECORD_MAX_MIN_POS(IT) \
19672 do \
19674 int composition_p = !STRINGP ((IT)->string) \
19675 && ((IT)->what == IT_COMPOSITION); \
19676 ptrdiff_t current_pos = \
19677 composition_p ? (IT)->cmp_it.charpos \
19678 : IT_CHARPOS (*(IT)); \
19679 ptrdiff_t current_bpos = \
19680 composition_p ? CHAR_TO_BYTE (current_pos) \
19681 : IT_BYTEPOS (*(IT)); \
19682 if (current_pos < min_pos) \
19684 min_pos = current_pos; \
19685 min_bpos = current_bpos; \
19687 if (IT_CHARPOS (*it) > max_pos) \
19689 max_pos = IT_CHARPOS (*it); \
19690 max_bpos = IT_BYTEPOS (*it); \
19693 while (0)
19695 /* Loop generating characters. The loop is left with IT on the next
19696 character to display. */
19697 while (1)
19699 int n_glyphs_before, hpos_before, x_before;
19700 int x, nglyphs;
19701 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19703 /* Retrieve the next thing to display. Value is zero if end of
19704 buffer reached. */
19705 if (!get_next_display_element (it))
19707 /* Maybe add a space at the end of this line that is used to
19708 display the cursor there under X. Set the charpos of the
19709 first glyph of blank lines not corresponding to any text
19710 to -1. */
19711 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19712 row->exact_window_width_line_p = 1;
19713 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19714 || row->used[TEXT_AREA] == 0)
19716 row->glyphs[TEXT_AREA]->charpos = -1;
19717 row->displays_text_p = 0;
19719 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19720 && (!MINI_WINDOW_P (it->w)
19721 || (minibuf_level && EQ (it->window, minibuf_window))))
19722 row->indicate_empty_line_p = 1;
19725 it->continuation_lines_width = 0;
19726 row->ends_at_zv_p = 1;
19727 /* A row that displays right-to-left text must always have
19728 its last face extended all the way to the end of line,
19729 even if this row ends in ZV, because we still write to
19730 the screen left to right. We also need to extend the
19731 last face if the default face is remapped to some
19732 different face, otherwise the functions that clear
19733 portions of the screen will clear with the default face's
19734 background color. */
19735 if (row->reversed_p
19736 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19737 extend_face_to_end_of_line (it);
19738 break;
19741 /* Now, get the metrics of what we want to display. This also
19742 generates glyphs in `row' (which is IT->glyph_row). */
19743 n_glyphs_before = row->used[TEXT_AREA];
19744 x = it->current_x;
19746 /* Remember the line height so far in case the next element doesn't
19747 fit on the line. */
19748 if (it->line_wrap != TRUNCATE)
19750 ascent = it->max_ascent;
19751 descent = it->max_descent;
19752 phys_ascent = it->max_phys_ascent;
19753 phys_descent = it->max_phys_descent;
19755 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19757 if (IT_DISPLAYING_WHITESPACE (it))
19758 may_wrap = 1;
19759 else if (may_wrap)
19761 SAVE_IT (wrap_it, *it, wrap_data);
19762 wrap_x = x;
19763 wrap_row_used = row->used[TEXT_AREA];
19764 wrap_row_ascent = row->ascent;
19765 wrap_row_height = row->height;
19766 wrap_row_phys_ascent = row->phys_ascent;
19767 wrap_row_phys_height = row->phys_height;
19768 wrap_row_extra_line_spacing = row->extra_line_spacing;
19769 wrap_row_min_pos = min_pos;
19770 wrap_row_min_bpos = min_bpos;
19771 wrap_row_max_pos = max_pos;
19772 wrap_row_max_bpos = max_bpos;
19773 may_wrap = 0;
19778 PRODUCE_GLYPHS (it);
19780 /* If this display element was in marginal areas, continue with
19781 the next one. */
19782 if (it->area != TEXT_AREA)
19784 row->ascent = max (row->ascent, it->max_ascent);
19785 row->height = max (row->height, it->max_ascent + it->max_descent);
19786 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19787 row->phys_height = max (row->phys_height,
19788 it->max_phys_ascent + it->max_phys_descent);
19789 row->extra_line_spacing = max (row->extra_line_spacing,
19790 it->max_extra_line_spacing);
19791 set_iterator_to_next (it, 1);
19792 continue;
19795 /* Does the display element fit on the line? If we truncate
19796 lines, we should draw past the right edge of the window. If
19797 we don't truncate, we want to stop so that we can display the
19798 continuation glyph before the right margin. If lines are
19799 continued, there are two possible strategies for characters
19800 resulting in more than 1 glyph (e.g. tabs): Display as many
19801 glyphs as possible in this line and leave the rest for the
19802 continuation line, or display the whole element in the next
19803 line. Original redisplay did the former, so we do it also. */
19804 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19805 hpos_before = it->hpos;
19806 x_before = x;
19808 if (/* Not a newline. */
19809 nglyphs > 0
19810 /* Glyphs produced fit entirely in the line. */
19811 && it->current_x < it->last_visible_x)
19813 it->hpos += nglyphs;
19814 row->ascent = max (row->ascent, it->max_ascent);
19815 row->height = max (row->height, it->max_ascent + it->max_descent);
19816 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19817 row->phys_height = max (row->phys_height,
19818 it->max_phys_ascent + it->max_phys_descent);
19819 row->extra_line_spacing = max (row->extra_line_spacing,
19820 it->max_extra_line_spacing);
19821 if (it->current_x - it->pixel_width < it->first_visible_x)
19822 row->x = x - it->first_visible_x;
19823 /* Record the maximum and minimum buffer positions seen so
19824 far in glyphs that will be displayed by this row. */
19825 if (it->bidi_p)
19826 RECORD_MAX_MIN_POS (it);
19828 else
19830 int i, new_x;
19831 struct glyph *glyph;
19833 for (i = 0; i < nglyphs; ++i, x = new_x)
19835 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19836 new_x = x + glyph->pixel_width;
19838 if (/* Lines are continued. */
19839 it->line_wrap != TRUNCATE
19840 && (/* Glyph doesn't fit on the line. */
19841 new_x > it->last_visible_x
19842 /* Or it fits exactly on a window system frame. */
19843 || (new_x == it->last_visible_x
19844 && FRAME_WINDOW_P (it->f)
19845 && (row->reversed_p
19846 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19847 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19849 /* End of a continued line. */
19851 if (it->hpos == 0
19852 || (new_x == it->last_visible_x
19853 && FRAME_WINDOW_P (it->f)
19854 && (row->reversed_p
19855 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19856 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19858 /* Current glyph is the only one on the line or
19859 fits exactly on the line. We must continue
19860 the line because we can't draw the cursor
19861 after the glyph. */
19862 row->continued_p = 1;
19863 it->current_x = new_x;
19864 it->continuation_lines_width += new_x;
19865 ++it->hpos;
19866 if (i == nglyphs - 1)
19868 /* If line-wrap is on, check if a previous
19869 wrap point was found. */
19870 if (wrap_row_used > 0
19871 /* Even if there is a previous wrap
19872 point, continue the line here as
19873 usual, if (i) the previous character
19874 was a space or tab AND (ii) the
19875 current character is not. */
19876 && (!may_wrap
19877 || IT_DISPLAYING_WHITESPACE (it)))
19878 goto back_to_wrap;
19880 /* Record the maximum and minimum buffer
19881 positions seen so far in glyphs that will be
19882 displayed by this row. */
19883 if (it->bidi_p)
19884 RECORD_MAX_MIN_POS (it);
19885 set_iterator_to_next (it, 1);
19886 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19888 if (!get_next_display_element (it))
19890 row->exact_window_width_line_p = 1;
19891 it->continuation_lines_width = 0;
19892 row->continued_p = 0;
19893 row->ends_at_zv_p = 1;
19895 else if (ITERATOR_AT_END_OF_LINE_P (it))
19897 row->continued_p = 0;
19898 row->exact_window_width_line_p = 1;
19902 else if (it->bidi_p)
19903 RECORD_MAX_MIN_POS (it);
19904 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19905 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19906 extend_face_to_end_of_line (it);
19908 else if (CHAR_GLYPH_PADDING_P (*glyph)
19909 && !FRAME_WINDOW_P (it->f))
19911 /* A padding glyph that doesn't fit on this line.
19912 This means the whole character doesn't fit
19913 on the line. */
19914 if (row->reversed_p)
19915 unproduce_glyphs (it, row->used[TEXT_AREA]
19916 - n_glyphs_before);
19917 row->used[TEXT_AREA] = n_glyphs_before;
19919 /* Fill the rest of the row with continuation
19920 glyphs like in 20.x. */
19921 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19922 < row->glyphs[1 + TEXT_AREA])
19923 produce_special_glyphs (it, IT_CONTINUATION);
19925 row->continued_p = 1;
19926 it->current_x = x_before;
19927 it->continuation_lines_width += x_before;
19929 /* Restore the height to what it was before the
19930 element not fitting on the line. */
19931 it->max_ascent = ascent;
19932 it->max_descent = descent;
19933 it->max_phys_ascent = phys_ascent;
19934 it->max_phys_descent = phys_descent;
19935 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19936 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19937 extend_face_to_end_of_line (it);
19939 else if (wrap_row_used > 0)
19941 back_to_wrap:
19942 if (row->reversed_p)
19943 unproduce_glyphs (it,
19944 row->used[TEXT_AREA] - wrap_row_used);
19945 RESTORE_IT (it, &wrap_it, wrap_data);
19946 it->continuation_lines_width += wrap_x;
19947 row->used[TEXT_AREA] = wrap_row_used;
19948 row->ascent = wrap_row_ascent;
19949 row->height = wrap_row_height;
19950 row->phys_ascent = wrap_row_phys_ascent;
19951 row->phys_height = wrap_row_phys_height;
19952 row->extra_line_spacing = wrap_row_extra_line_spacing;
19953 min_pos = wrap_row_min_pos;
19954 min_bpos = wrap_row_min_bpos;
19955 max_pos = wrap_row_max_pos;
19956 max_bpos = wrap_row_max_bpos;
19957 row->continued_p = 1;
19958 row->ends_at_zv_p = 0;
19959 row->exact_window_width_line_p = 0;
19960 it->continuation_lines_width += x;
19962 /* Make sure that a non-default face is extended
19963 up to the right margin of the window. */
19964 extend_face_to_end_of_line (it);
19966 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19968 /* A TAB that extends past the right edge of the
19969 window. This produces a single glyph on
19970 window system frames. We leave the glyph in
19971 this row and let it fill the row, but don't
19972 consume the TAB. */
19973 if ((row->reversed_p
19974 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19975 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19976 produce_special_glyphs (it, IT_CONTINUATION);
19977 it->continuation_lines_width += it->last_visible_x;
19978 row->ends_in_middle_of_char_p = 1;
19979 row->continued_p = 1;
19980 glyph->pixel_width = it->last_visible_x - x;
19981 it->starts_in_middle_of_char_p = 1;
19982 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19983 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19984 extend_face_to_end_of_line (it);
19986 else
19988 /* Something other than a TAB that draws past
19989 the right edge of the window. Restore
19990 positions to values before the element. */
19991 if (row->reversed_p)
19992 unproduce_glyphs (it, row->used[TEXT_AREA]
19993 - (n_glyphs_before + i));
19994 row->used[TEXT_AREA] = n_glyphs_before + i;
19996 /* Display continuation glyphs. */
19997 it->current_x = x_before;
19998 it->continuation_lines_width += x;
19999 if (!FRAME_WINDOW_P (it->f)
20000 || (row->reversed_p
20001 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20002 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20003 produce_special_glyphs (it, IT_CONTINUATION);
20004 row->continued_p = 1;
20006 extend_face_to_end_of_line (it);
20008 if (nglyphs > 1 && i > 0)
20010 row->ends_in_middle_of_char_p = 1;
20011 it->starts_in_middle_of_char_p = 1;
20014 /* Restore the height to what it was before the
20015 element not fitting on the line. */
20016 it->max_ascent = ascent;
20017 it->max_descent = descent;
20018 it->max_phys_ascent = phys_ascent;
20019 it->max_phys_descent = phys_descent;
20022 break;
20024 else if (new_x > it->first_visible_x)
20026 /* Increment number of glyphs actually displayed. */
20027 ++it->hpos;
20029 /* Record the maximum and minimum buffer positions
20030 seen so far in glyphs that will be displayed by
20031 this row. */
20032 if (it->bidi_p)
20033 RECORD_MAX_MIN_POS (it);
20035 if (x < it->first_visible_x)
20036 /* Glyph is partially visible, i.e. row starts at
20037 negative X position. */
20038 row->x = x - it->first_visible_x;
20040 else
20042 /* Glyph is completely off the left margin of the
20043 window. This should not happen because of the
20044 move_it_in_display_line at the start of this
20045 function, unless the text display area of the
20046 window is empty. */
20047 eassert (it->first_visible_x <= it->last_visible_x);
20050 /* Even if this display element produced no glyphs at all,
20051 we want to record its position. */
20052 if (it->bidi_p && nglyphs == 0)
20053 RECORD_MAX_MIN_POS (it);
20055 row->ascent = max (row->ascent, it->max_ascent);
20056 row->height = max (row->height, it->max_ascent + it->max_descent);
20057 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20058 row->phys_height = max (row->phys_height,
20059 it->max_phys_ascent + it->max_phys_descent);
20060 row->extra_line_spacing = max (row->extra_line_spacing,
20061 it->max_extra_line_spacing);
20063 /* End of this display line if row is continued. */
20064 if (row->continued_p || row->ends_at_zv_p)
20065 break;
20068 at_end_of_line:
20069 /* Is this a line end? If yes, we're also done, after making
20070 sure that a non-default face is extended up to the right
20071 margin of the window. */
20072 if (ITERATOR_AT_END_OF_LINE_P (it))
20074 int used_before = row->used[TEXT_AREA];
20076 row->ends_in_newline_from_string_p = STRINGP (it->object);
20078 /* Add a space at the end of the line that is used to
20079 display the cursor there. */
20080 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20081 append_space_for_newline (it, 0);
20083 /* Extend the face to the end of the line. */
20084 extend_face_to_end_of_line (it);
20086 /* Make sure we have the position. */
20087 if (used_before == 0)
20088 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20090 /* Record the position of the newline, for use in
20091 find_row_edges. */
20092 it->eol_pos = it->current.pos;
20094 /* Consume the line end. This skips over invisible lines. */
20095 set_iterator_to_next (it, 1);
20096 it->continuation_lines_width = 0;
20097 break;
20100 /* Proceed with next display element. Note that this skips
20101 over lines invisible because of selective display. */
20102 set_iterator_to_next (it, 1);
20104 /* If we truncate lines, we are done when the last displayed
20105 glyphs reach past the right margin of the window. */
20106 if (it->line_wrap == TRUNCATE
20107 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20108 ? (it->current_x >= it->last_visible_x)
20109 : (it->current_x > it->last_visible_x)))
20111 /* Maybe add truncation glyphs. */
20112 if (!FRAME_WINDOW_P (it->f)
20113 || (row->reversed_p
20114 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20115 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20117 int i, n;
20119 if (!row->reversed_p)
20121 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20122 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20123 break;
20125 else
20127 for (i = 0; i < row->used[TEXT_AREA]; i++)
20128 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20129 break;
20130 /* Remove any padding glyphs at the front of ROW, to
20131 make room for the truncation glyphs we will be
20132 adding below. The loop below always inserts at
20133 least one truncation glyph, so also remove the
20134 last glyph added to ROW. */
20135 unproduce_glyphs (it, i + 1);
20136 /* Adjust i for the loop below. */
20137 i = row->used[TEXT_AREA] - (i + 1);
20140 it->current_x = x_before;
20141 if (!FRAME_WINDOW_P (it->f))
20143 for (n = row->used[TEXT_AREA]; i < n; ++i)
20145 row->used[TEXT_AREA] = i;
20146 produce_special_glyphs (it, IT_TRUNCATION);
20149 else
20151 row->used[TEXT_AREA] = i;
20152 produce_special_glyphs (it, IT_TRUNCATION);
20155 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20157 /* Don't truncate if we can overflow newline into fringe. */
20158 if (!get_next_display_element (it))
20160 it->continuation_lines_width = 0;
20161 row->ends_at_zv_p = 1;
20162 row->exact_window_width_line_p = 1;
20163 break;
20165 if (ITERATOR_AT_END_OF_LINE_P (it))
20167 row->exact_window_width_line_p = 1;
20168 goto at_end_of_line;
20170 it->current_x = x_before;
20173 row->truncated_on_right_p = 1;
20174 it->continuation_lines_width = 0;
20175 reseat_at_next_visible_line_start (it, 0);
20176 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20177 it->hpos = hpos_before;
20178 break;
20182 if (wrap_data)
20183 bidi_unshelve_cache (wrap_data, 1);
20185 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20186 at the left window margin. */
20187 if (it->first_visible_x
20188 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20190 if (!FRAME_WINDOW_P (it->f)
20191 || (row->reversed_p
20192 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20193 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20194 insert_left_trunc_glyphs (it);
20195 row->truncated_on_left_p = 1;
20198 /* Remember the position at which this line ends.
20200 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20201 cannot be before the call to find_row_edges below, since that is
20202 where these positions are determined. */
20203 row->end = it->current;
20204 if (!it->bidi_p)
20206 row->minpos = row->start.pos;
20207 row->maxpos = row->end.pos;
20209 else
20211 /* ROW->minpos and ROW->maxpos must be the smallest and
20212 `1 + the largest' buffer positions in ROW. But if ROW was
20213 bidi-reordered, these two positions can be anywhere in the
20214 row, so we must determine them now. */
20215 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20218 /* If the start of this line is the overlay arrow-position, then
20219 mark this glyph row as the one containing the overlay arrow.
20220 This is clearly a mess with variable size fonts. It would be
20221 better to let it be displayed like cursors under X. */
20222 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20223 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20224 !NILP (overlay_arrow_string)))
20226 /* Overlay arrow in window redisplay is a fringe bitmap. */
20227 if (STRINGP (overlay_arrow_string))
20229 struct glyph_row *arrow_row
20230 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20231 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20232 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20233 struct glyph *p = row->glyphs[TEXT_AREA];
20234 struct glyph *p2, *end;
20236 /* Copy the arrow glyphs. */
20237 while (glyph < arrow_end)
20238 *p++ = *glyph++;
20240 /* Throw away padding glyphs. */
20241 p2 = p;
20242 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20243 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20244 ++p2;
20245 if (p2 > p)
20247 while (p2 < end)
20248 *p++ = *p2++;
20249 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20252 else
20254 eassert (INTEGERP (overlay_arrow_string));
20255 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20257 overlay_arrow_seen = 1;
20260 /* Highlight trailing whitespace. */
20261 if (!NILP (Vshow_trailing_whitespace))
20262 highlight_trailing_whitespace (it->f, it->glyph_row);
20264 /* Compute pixel dimensions of this line. */
20265 compute_line_metrics (it);
20267 /* Implementation note: No changes in the glyphs of ROW or in their
20268 faces can be done past this point, because compute_line_metrics
20269 computes ROW's hash value and stores it within the glyph_row
20270 structure. */
20272 /* Record whether this row ends inside an ellipsis. */
20273 row->ends_in_ellipsis_p
20274 = (it->method == GET_FROM_DISPLAY_VECTOR
20275 && it->ellipsis_p);
20277 /* Save fringe bitmaps in this row. */
20278 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20279 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20280 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20281 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20283 it->left_user_fringe_bitmap = 0;
20284 it->left_user_fringe_face_id = 0;
20285 it->right_user_fringe_bitmap = 0;
20286 it->right_user_fringe_face_id = 0;
20288 /* Maybe set the cursor. */
20289 cvpos = it->w->cursor.vpos;
20290 if ((cvpos < 0
20291 /* In bidi-reordered rows, keep checking for proper cursor
20292 position even if one has been found already, because buffer
20293 positions in such rows change non-linearly with ROW->VPOS,
20294 when a line is continued. One exception: when we are at ZV,
20295 display cursor on the first suitable glyph row, since all
20296 the empty rows after that also have their position set to ZV. */
20297 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20298 lines' rows is implemented for bidi-reordered rows. */
20299 || (it->bidi_p
20300 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20301 && PT >= MATRIX_ROW_START_CHARPOS (row)
20302 && PT <= MATRIX_ROW_END_CHARPOS (row)
20303 && cursor_row_p (row))
20304 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20306 /* Prepare for the next line. This line starts horizontally at (X
20307 HPOS) = (0 0). Vertical positions are incremented. As a
20308 convenience for the caller, IT->glyph_row is set to the next
20309 row to be used. */
20310 it->current_x = it->hpos = 0;
20311 it->current_y += row->height;
20312 SET_TEXT_POS (it->eol_pos, 0, 0);
20313 ++it->vpos;
20314 ++it->glyph_row;
20315 /* The next row should by default use the same value of the
20316 reversed_p flag as this one. set_iterator_to_next decides when
20317 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20318 the flag accordingly. */
20319 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20320 it->glyph_row->reversed_p = row->reversed_p;
20321 it->start = row->end;
20322 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20324 #undef RECORD_MAX_MIN_POS
20327 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20328 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20329 doc: /* Return paragraph direction at point in BUFFER.
20330 Value is either `left-to-right' or `right-to-left'.
20331 If BUFFER is omitted or nil, it defaults to the current buffer.
20333 Paragraph direction determines how the text in the paragraph is displayed.
20334 In left-to-right paragraphs, text begins at the left margin of the window
20335 and the reading direction is generally left to right. In right-to-left
20336 paragraphs, text begins at the right margin and is read from right to left.
20338 See also `bidi-paragraph-direction'. */)
20339 (Lisp_Object buffer)
20341 struct buffer *buf = current_buffer;
20342 struct buffer *old = buf;
20344 if (! NILP (buffer))
20346 CHECK_BUFFER (buffer);
20347 buf = XBUFFER (buffer);
20350 if (NILP (BVAR (buf, bidi_display_reordering))
20351 || NILP (BVAR (buf, enable_multibyte_characters))
20352 /* When we are loading loadup.el, the character property tables
20353 needed for bidi iteration are not yet available. */
20354 || !NILP (Vpurify_flag))
20355 return Qleft_to_right;
20356 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20357 return BVAR (buf, bidi_paragraph_direction);
20358 else
20360 /* Determine the direction from buffer text. We could try to
20361 use current_matrix if it is up to date, but this seems fast
20362 enough as it is. */
20363 struct bidi_it itb;
20364 ptrdiff_t pos = BUF_PT (buf);
20365 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20366 int c;
20367 void *itb_data = bidi_shelve_cache ();
20369 set_buffer_temp (buf);
20370 /* bidi_paragraph_init finds the base direction of the paragraph
20371 by searching forward from paragraph start. We need the base
20372 direction of the current or _previous_ paragraph, so we need
20373 to make sure we are within that paragraph. To that end, find
20374 the previous non-empty line. */
20375 if (pos >= ZV && pos > BEGV)
20376 DEC_BOTH (pos, bytepos);
20377 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20378 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20380 while ((c = FETCH_BYTE (bytepos)) == '\n'
20381 || c == ' ' || c == '\t' || c == '\f')
20383 if (bytepos <= BEGV_BYTE)
20384 break;
20385 bytepos--;
20386 pos--;
20388 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20389 bytepos--;
20391 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20392 itb.paragraph_dir = NEUTRAL_DIR;
20393 itb.string.s = NULL;
20394 itb.string.lstring = Qnil;
20395 itb.string.bufpos = 0;
20396 itb.string.unibyte = 0;
20397 /* We have no window to use here for ignoring window-specific
20398 overlays. Using NULL for window pointer will cause
20399 compute_display_string_pos to use the current buffer. */
20400 itb.w = NULL;
20401 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20402 bidi_unshelve_cache (itb_data, 0);
20403 set_buffer_temp (old);
20404 switch (itb.paragraph_dir)
20406 case L2R:
20407 return Qleft_to_right;
20408 break;
20409 case R2L:
20410 return Qright_to_left;
20411 break;
20412 default:
20413 emacs_abort ();
20418 DEFUN ("move-point-visually", Fmove_point_visually,
20419 Smove_point_visually, 1, 1, 0,
20420 doc: /* Move point in the visual order in the specified DIRECTION.
20421 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20422 left.
20424 Value is the new character position of point. */)
20425 (Lisp_Object direction)
20427 struct window *w = XWINDOW (selected_window);
20428 struct buffer *b = XBUFFER (w->contents);
20429 struct glyph_row *row;
20430 int dir;
20431 Lisp_Object paragraph_dir;
20433 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20434 (!(ROW)->continued_p \
20435 && INTEGERP ((GLYPH)->object) \
20436 && (GLYPH)->type == CHAR_GLYPH \
20437 && (GLYPH)->u.ch == ' ' \
20438 && (GLYPH)->charpos >= 0 \
20439 && !(GLYPH)->avoid_cursor_p)
20441 CHECK_NUMBER (direction);
20442 dir = XINT (direction);
20443 if (dir > 0)
20444 dir = 1;
20445 else
20446 dir = -1;
20448 /* If current matrix is up-to-date, we can use the information
20449 recorded in the glyphs, at least as long as the goal is on the
20450 screen. */
20451 if (w->window_end_valid
20452 && !windows_or_buffers_changed
20453 && b
20454 && !b->clip_changed
20455 && !b->prevent_redisplay_optimizations_p
20456 && !window_outdated (w)
20457 && w->cursor.vpos >= 0
20458 && w->cursor.vpos < w->current_matrix->nrows
20459 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20461 struct glyph *g = row->glyphs[TEXT_AREA];
20462 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20463 struct glyph *gpt = g + w->cursor.hpos;
20465 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20467 if (BUFFERP (g->object) && g->charpos != PT)
20469 SET_PT (g->charpos);
20470 w->cursor.vpos = -1;
20471 return make_number (PT);
20473 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20475 ptrdiff_t new_pos;
20477 if (BUFFERP (gpt->object))
20479 new_pos = PT;
20480 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20481 new_pos += (row->reversed_p ? -dir : dir);
20482 else
20483 new_pos -= (row->reversed_p ? -dir : dir);;
20485 else if (BUFFERP (g->object))
20486 new_pos = g->charpos;
20487 else
20488 break;
20489 SET_PT (new_pos);
20490 w->cursor.vpos = -1;
20491 return make_number (PT);
20493 else if (ROW_GLYPH_NEWLINE_P (row, g))
20495 /* Glyphs inserted at the end of a non-empty line for
20496 positioning the cursor have zero charpos, so we must
20497 deduce the value of point by other means. */
20498 if (g->charpos > 0)
20499 SET_PT (g->charpos);
20500 else if (row->ends_at_zv_p && PT != ZV)
20501 SET_PT (ZV);
20502 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20503 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20504 else
20505 break;
20506 w->cursor.vpos = -1;
20507 return make_number (PT);
20510 if (g == e || INTEGERP (g->object))
20512 if (row->truncated_on_left_p || row->truncated_on_right_p)
20513 goto simulate_display;
20514 if (!row->reversed_p)
20515 row += dir;
20516 else
20517 row -= dir;
20518 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20519 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20520 goto simulate_display;
20522 if (dir > 0)
20524 if (row->reversed_p && !row->continued_p)
20526 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20527 w->cursor.vpos = -1;
20528 return make_number (PT);
20530 g = row->glyphs[TEXT_AREA];
20531 e = g + row->used[TEXT_AREA];
20532 for ( ; g < e; g++)
20534 if (BUFFERP (g->object)
20535 /* Empty lines have only one glyph, which stands
20536 for the newline, and whose charpos is the
20537 buffer position of the newline. */
20538 || ROW_GLYPH_NEWLINE_P (row, g)
20539 /* When the buffer ends in a newline, the line at
20540 EOB also has one glyph, but its charpos is -1. */
20541 || (row->ends_at_zv_p
20542 && !row->reversed_p
20543 && INTEGERP (g->object)
20544 && g->type == CHAR_GLYPH
20545 && g->u.ch == ' '))
20547 if (g->charpos > 0)
20548 SET_PT (g->charpos);
20549 else if (!row->reversed_p
20550 && row->ends_at_zv_p
20551 && PT != ZV)
20552 SET_PT (ZV);
20553 else
20554 continue;
20555 w->cursor.vpos = -1;
20556 return make_number (PT);
20560 else
20562 if (!row->reversed_p && !row->continued_p)
20564 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20565 w->cursor.vpos = -1;
20566 return make_number (PT);
20568 e = row->glyphs[TEXT_AREA];
20569 g = e + row->used[TEXT_AREA] - 1;
20570 for ( ; g >= e; g--)
20572 if (BUFFERP (g->object)
20573 || (ROW_GLYPH_NEWLINE_P (row, g)
20574 && g->charpos > 0)
20575 /* Empty R2L lines on GUI frames have the buffer
20576 position of the newline stored in the stretch
20577 glyph. */
20578 || g->type == STRETCH_GLYPH
20579 || (row->ends_at_zv_p
20580 && row->reversed_p
20581 && INTEGERP (g->object)
20582 && g->type == CHAR_GLYPH
20583 && g->u.ch == ' '))
20585 if (g->charpos > 0)
20586 SET_PT (g->charpos);
20587 else if (row->reversed_p
20588 && row->ends_at_zv_p
20589 && PT != ZV)
20590 SET_PT (ZV);
20591 else
20592 continue;
20593 w->cursor.vpos = -1;
20594 return make_number (PT);
20601 simulate_display:
20603 /* If we wind up here, we failed to move by using the glyphs, so we
20604 need to simulate display instead. */
20606 if (b)
20607 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20608 else
20609 paragraph_dir = Qleft_to_right;
20610 if (EQ (paragraph_dir, Qright_to_left))
20611 dir = -dir;
20612 if (PT <= BEGV && dir < 0)
20613 xsignal0 (Qbeginning_of_buffer);
20614 else if (PT >= ZV && dir > 0)
20615 xsignal0 (Qend_of_buffer);
20616 else
20618 struct text_pos pt;
20619 struct it it;
20620 int pt_x, target_x, pixel_width, pt_vpos;
20621 bool at_eol_p;
20622 bool overshoot_expected = false;
20623 bool target_is_eol_p = false;
20625 /* Setup the arena. */
20626 SET_TEXT_POS (pt, PT, PT_BYTE);
20627 start_display (&it, w, pt);
20629 if (it.cmp_it.id < 0
20630 && it.method == GET_FROM_STRING
20631 && it.area == TEXT_AREA
20632 && it.string_from_display_prop_p
20633 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20634 overshoot_expected = true;
20636 /* Find the X coordinate of point. We start from the beginning
20637 of this or previous line to make sure we are before point in
20638 the logical order (since the move_it_* functions can only
20639 move forward). */
20640 reseat:
20641 reseat_at_previous_visible_line_start (&it);
20642 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20643 if (IT_CHARPOS (it) != PT)
20645 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20646 -1, -1, -1, MOVE_TO_POS);
20647 /* If we missed point because the character there is
20648 displayed out of a display vector that has more than one
20649 glyph, retry expecting overshoot. */
20650 if (it.method == GET_FROM_DISPLAY_VECTOR
20651 && it.current.dpvec_index > 0
20652 && !overshoot_expected)
20654 overshoot_expected = true;
20655 goto reseat;
20657 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20658 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20660 pt_x = it.current_x;
20661 pt_vpos = it.vpos;
20662 if (dir > 0 || overshoot_expected)
20664 struct glyph_row *row = it.glyph_row;
20666 /* When point is at beginning of line, we don't have
20667 information about the glyph there loaded into struct
20668 it. Calling get_next_display_element fixes that. */
20669 if (pt_x == 0)
20670 get_next_display_element (&it);
20671 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20672 it.glyph_row = NULL;
20673 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20674 it.glyph_row = row;
20675 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20676 it, lest it will become out of sync with it's buffer
20677 position. */
20678 it.current_x = pt_x;
20680 else
20681 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20682 pixel_width = it.pixel_width;
20683 if (overshoot_expected && at_eol_p)
20684 pixel_width = 0;
20685 else if (pixel_width <= 0)
20686 pixel_width = 1;
20688 /* If there's a display string (or something similar) at point,
20689 we are actually at the glyph to the left of point, so we need
20690 to correct the X coordinate. */
20691 if (overshoot_expected)
20693 if (it.bidi_p)
20694 pt_x += pixel_width * it.bidi_it.scan_dir;
20695 else
20696 pt_x += pixel_width;
20699 /* Compute target X coordinate, either to the left or to the
20700 right of point. On TTY frames, all characters have the same
20701 pixel width of 1, so we can use that. On GUI frames we don't
20702 have an easy way of getting at the pixel width of the
20703 character to the left of point, so we use a different method
20704 of getting to that place. */
20705 if (dir > 0)
20706 target_x = pt_x + pixel_width;
20707 else
20708 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20710 /* Target X coordinate could be one line above or below the line
20711 of point, in which case we need to adjust the target X
20712 coordinate. Also, if moving to the left, we need to begin at
20713 the left edge of the point's screen line. */
20714 if (dir < 0)
20716 if (pt_x > 0)
20718 start_display (&it, w, pt);
20719 reseat_at_previous_visible_line_start (&it);
20720 it.current_x = it.current_y = it.hpos = 0;
20721 if (pt_vpos != 0)
20722 move_it_by_lines (&it, pt_vpos);
20724 else
20726 move_it_by_lines (&it, -1);
20727 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20728 target_is_eol_p = true;
20731 else
20733 if (at_eol_p
20734 || (target_x >= it.last_visible_x
20735 && it.line_wrap != TRUNCATE))
20737 if (pt_x > 0)
20738 move_it_by_lines (&it, 0);
20739 move_it_by_lines (&it, 1);
20740 target_x = 0;
20744 /* Move to the target X coordinate. */
20745 #ifdef HAVE_WINDOW_SYSTEM
20746 /* On GUI frames, as we don't know the X coordinate of the
20747 character to the left of point, moving point to the left
20748 requires walking, one grapheme cluster at a time, until we
20749 find ourself at a place immediately to the left of the
20750 character at point. */
20751 if (FRAME_WINDOW_P (it.f) && dir < 0)
20753 struct text_pos new_pos;
20754 enum move_it_result rc = MOVE_X_REACHED;
20756 if (it.current_x == 0)
20757 get_next_display_element (&it);
20758 if (it.what == IT_COMPOSITION)
20760 new_pos.charpos = it.cmp_it.charpos;
20761 new_pos.bytepos = -1;
20763 else
20764 new_pos = it.current.pos;
20766 while (it.current_x + it.pixel_width <= target_x
20767 && rc == MOVE_X_REACHED)
20769 int new_x = it.current_x + it.pixel_width;
20771 /* For composed characters, we want the position of the
20772 first character in the grapheme cluster (usually, the
20773 composition's base character), whereas it.current
20774 might give us the position of the _last_ one, e.g. if
20775 the composition is rendered in reverse due to bidi
20776 reordering. */
20777 if (it.what == IT_COMPOSITION)
20779 new_pos.charpos = it.cmp_it.charpos;
20780 new_pos.bytepos = -1;
20782 else
20783 new_pos = it.current.pos;
20784 if (new_x == it.current_x)
20785 new_x++;
20786 rc = move_it_in_display_line_to (&it, ZV, new_x,
20787 MOVE_TO_POS | MOVE_TO_X);
20788 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20789 break;
20791 /* The previous position we saw in the loop is the one we
20792 want. */
20793 if (new_pos.bytepos == -1)
20794 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20795 it.current.pos = new_pos;
20797 else
20798 #endif
20799 if (it.current_x != target_x)
20800 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20802 /* When lines are truncated, the above loop will stop at the
20803 window edge. But we want to get to the end of line, even if
20804 it is beyond the window edge; automatic hscroll will then
20805 scroll the window to show point as appropriate. */
20806 if (target_is_eol_p && it.line_wrap == TRUNCATE
20807 && get_next_display_element (&it))
20809 struct text_pos new_pos = it.current.pos;
20811 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20813 set_iterator_to_next (&it, 0);
20814 if (it.method == GET_FROM_BUFFER)
20815 new_pos = it.current.pos;
20816 if (!get_next_display_element (&it))
20817 break;
20820 it.current.pos = new_pos;
20823 /* If we ended up in a display string that covers point, move to
20824 buffer position to the right in the visual order. */
20825 if (dir > 0)
20827 while (IT_CHARPOS (it) == PT)
20829 set_iterator_to_next (&it, 0);
20830 if (!get_next_display_element (&it))
20831 break;
20835 /* Move point to that position. */
20836 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20839 return make_number (PT);
20841 #undef ROW_GLYPH_NEWLINE_P
20845 /***********************************************************************
20846 Menu Bar
20847 ***********************************************************************/
20849 /* Redisplay the menu bar in the frame for window W.
20851 The menu bar of X frames that don't have X toolkit support is
20852 displayed in a special window W->frame->menu_bar_window.
20854 The menu bar of terminal frames is treated specially as far as
20855 glyph matrices are concerned. Menu bar lines are not part of
20856 windows, so the update is done directly on the frame matrix rows
20857 for the menu bar. */
20859 static void
20860 display_menu_bar (struct window *w)
20862 struct frame *f = XFRAME (WINDOW_FRAME (w));
20863 struct it it;
20864 Lisp_Object items;
20865 int i;
20867 /* Don't do all this for graphical frames. */
20868 #ifdef HAVE_NTGUI
20869 if (FRAME_W32_P (f))
20870 return;
20871 #endif
20872 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20873 if (FRAME_X_P (f))
20874 return;
20875 #endif
20877 #ifdef HAVE_NS
20878 if (FRAME_NS_P (f))
20879 return;
20880 #endif /* HAVE_NS */
20882 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20883 eassert (!FRAME_WINDOW_P (f));
20884 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20885 it.first_visible_x = 0;
20886 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20887 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20888 if (FRAME_WINDOW_P (f))
20890 /* Menu bar lines are displayed in the desired matrix of the
20891 dummy window menu_bar_window. */
20892 struct window *menu_w;
20893 menu_w = XWINDOW (f->menu_bar_window);
20894 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20895 MENU_FACE_ID);
20896 it.first_visible_x = 0;
20897 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20899 else
20900 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20902 /* This is a TTY frame, i.e. character hpos/vpos are used as
20903 pixel x/y. */
20904 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20905 MENU_FACE_ID);
20906 it.first_visible_x = 0;
20907 it.last_visible_x = FRAME_COLS (f);
20910 /* FIXME: This should be controlled by a user option. See the
20911 comments in redisplay_tool_bar and display_mode_line about
20912 this. */
20913 it.paragraph_embedding = L2R;
20915 /* Clear all rows of the menu bar. */
20916 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20918 struct glyph_row *row = it.glyph_row + i;
20919 clear_glyph_row (row);
20920 row->enabled_p = true;
20921 row->full_width_p = 1;
20924 /* Display all items of the menu bar. */
20925 items = FRAME_MENU_BAR_ITEMS (it.f);
20926 for (i = 0; i < ASIZE (items); i += 4)
20928 Lisp_Object string;
20930 /* Stop at nil string. */
20931 string = AREF (items, i + 1);
20932 if (NILP (string))
20933 break;
20935 /* Remember where item was displayed. */
20936 ASET (items, i + 3, make_number (it.hpos));
20938 /* Display the item, pad with one space. */
20939 if (it.current_x < it.last_visible_x)
20940 display_string (NULL, string, Qnil, 0, 0, &it,
20941 SCHARS (string) + 1, 0, 0, -1);
20944 /* Fill out the line with spaces. */
20945 if (it.current_x < it.last_visible_x)
20946 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20948 /* Compute the total height of the lines. */
20949 compute_line_metrics (&it);
20952 /* Deep copy of a glyph row, including the glyphs. */
20953 static void
20954 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20956 struct glyph *pointers[1 + LAST_AREA];
20957 int to_used = to->used[TEXT_AREA];
20959 /* Save glyph pointers of TO. */
20960 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20962 /* Do a structure assignment. */
20963 *to = *from;
20965 /* Restore original glyph pointers of TO. */
20966 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20968 /* Copy the glyphs. */
20969 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20970 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20972 /* If we filled only part of the TO row, fill the rest with
20973 space_glyph (which will display as empty space). */
20974 if (to_used > from->used[TEXT_AREA])
20975 fill_up_frame_row_with_spaces (to, to_used);
20978 /* Display one menu item on a TTY, by overwriting the glyphs in the
20979 frame F's desired glyph matrix with glyphs produced from the menu
20980 item text. Called from term.c to display TTY drop-down menus one
20981 item at a time.
20983 ITEM_TEXT is the menu item text as a C string.
20985 FACE_ID is the face ID to be used for this menu item. FACE_ID
20986 could specify one of 3 faces: a face for an enabled item, a face
20987 for a disabled item, or a face for a selected item.
20989 X and Y are coordinates of the first glyph in the frame's desired
20990 matrix to be overwritten by the menu item. Since this is a TTY, Y
20991 is the zero-based number of the glyph row and X is the zero-based
20992 glyph number in the row, starting from left, where to start
20993 displaying the item.
20995 SUBMENU non-zero means this menu item drops down a submenu, which
20996 should be indicated by displaying a proper visual cue after the
20997 item text. */
20999 void
21000 display_tty_menu_item (const char *item_text, int width, int face_id,
21001 int x, int y, int submenu)
21003 struct it it;
21004 struct frame *f = SELECTED_FRAME ();
21005 struct window *w = XWINDOW (f->selected_window);
21006 int saved_used, saved_truncated, saved_width, saved_reversed;
21007 struct glyph_row *row;
21008 size_t item_len = strlen (item_text);
21010 eassert (FRAME_TERMCAP_P (f));
21012 /* Don't write beyond the matrix's last row. This can happen for
21013 TTY screens that are not high enough to show the entire menu.
21014 (This is actually a bit of defensive programming, as
21015 tty_menu_display already limits the number of menu items to one
21016 less than the number of screen lines.) */
21017 if (y >= f->desired_matrix->nrows)
21018 return;
21020 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21021 it.first_visible_x = 0;
21022 it.last_visible_x = FRAME_COLS (f) - 1;
21023 row = it.glyph_row;
21024 /* Start with the row contents from the current matrix. */
21025 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21026 saved_width = row->full_width_p;
21027 row->full_width_p = 1;
21028 saved_reversed = row->reversed_p;
21029 row->reversed_p = 0;
21030 row->enabled_p = true;
21032 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21033 desired face. */
21034 eassert (x < f->desired_matrix->matrix_w);
21035 it.current_x = it.hpos = x;
21036 it.current_y = it.vpos = y;
21037 saved_used = row->used[TEXT_AREA];
21038 saved_truncated = row->truncated_on_right_p;
21039 row->used[TEXT_AREA] = x;
21040 it.face_id = face_id;
21041 it.line_wrap = TRUNCATE;
21043 /* FIXME: This should be controlled by a user option. See the
21044 comments in redisplay_tool_bar and display_mode_line about this.
21045 Also, if paragraph_embedding could ever be R2L, changes will be
21046 needed to avoid shifting to the right the row characters in
21047 term.c:append_glyph. */
21048 it.paragraph_embedding = L2R;
21050 /* Pad with a space on the left. */
21051 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21052 width--;
21053 /* Display the menu item, pad with spaces to WIDTH. */
21054 if (submenu)
21056 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21057 item_len, 0, FRAME_COLS (f) - 1, -1);
21058 width -= item_len;
21059 /* Indicate with " >" that there's a submenu. */
21060 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21061 FRAME_COLS (f) - 1, -1);
21063 else
21064 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21065 width, 0, FRAME_COLS (f) - 1, -1);
21067 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21068 row->truncated_on_right_p = saved_truncated;
21069 row->hash = row_hash (row);
21070 row->full_width_p = saved_width;
21071 row->reversed_p = saved_reversed;
21074 /***********************************************************************
21075 Mode Line
21076 ***********************************************************************/
21078 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21079 FORCE is non-zero, redisplay mode lines unconditionally.
21080 Otherwise, redisplay only mode lines that are garbaged. Value is
21081 the number of windows whose mode lines were redisplayed. */
21083 static int
21084 redisplay_mode_lines (Lisp_Object window, bool force)
21086 int nwindows = 0;
21088 while (!NILP (window))
21090 struct window *w = XWINDOW (window);
21092 if (WINDOWP (w->contents))
21093 nwindows += redisplay_mode_lines (w->contents, force);
21094 else if (force
21095 || FRAME_GARBAGED_P (XFRAME (w->frame))
21096 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21098 struct text_pos lpoint;
21099 struct buffer *old = current_buffer;
21101 /* Set the window's buffer for the mode line display. */
21102 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21103 set_buffer_internal_1 (XBUFFER (w->contents));
21105 /* Point refers normally to the selected window. For any
21106 other window, set up appropriate value. */
21107 if (!EQ (window, selected_window))
21109 struct text_pos pt;
21111 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21112 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21115 /* Display mode lines. */
21116 clear_glyph_matrix (w->desired_matrix);
21117 if (display_mode_lines (w))
21118 ++nwindows;
21120 /* Restore old settings. */
21121 set_buffer_internal_1 (old);
21122 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21125 window = w->next;
21128 return nwindows;
21132 /* Display the mode and/or header line of window W. Value is the
21133 sum number of mode lines and header lines displayed. */
21135 static int
21136 display_mode_lines (struct window *w)
21138 Lisp_Object old_selected_window = selected_window;
21139 Lisp_Object old_selected_frame = selected_frame;
21140 Lisp_Object new_frame = w->frame;
21141 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21142 int n = 0;
21144 selected_frame = new_frame;
21145 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21146 or window's point, then we'd need select_window_1 here as well. */
21147 XSETWINDOW (selected_window, w);
21148 XFRAME (new_frame)->selected_window = selected_window;
21150 /* These will be set while the mode line specs are processed. */
21151 line_number_displayed = 0;
21152 w->column_number_displayed = -1;
21154 if (WINDOW_WANTS_MODELINE_P (w))
21156 struct window *sel_w = XWINDOW (old_selected_window);
21158 /* Select mode line face based on the real selected window. */
21159 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21160 BVAR (current_buffer, mode_line_format));
21161 ++n;
21164 if (WINDOW_WANTS_HEADER_LINE_P (w))
21166 display_mode_line (w, HEADER_LINE_FACE_ID,
21167 BVAR (current_buffer, header_line_format));
21168 ++n;
21171 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21172 selected_frame = old_selected_frame;
21173 selected_window = old_selected_window;
21174 if (n > 0)
21175 w->must_be_updated_p = true;
21176 return n;
21180 /* Display mode or header line of window W. FACE_ID specifies which
21181 line to display; it is either MODE_LINE_FACE_ID or
21182 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21183 display. Value is the pixel height of the mode/header line
21184 displayed. */
21186 static int
21187 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21189 struct it it;
21190 struct face *face;
21191 ptrdiff_t count = SPECPDL_INDEX ();
21193 init_iterator (&it, w, -1, -1, NULL, face_id);
21194 /* Don't extend on a previously drawn mode-line.
21195 This may happen if called from pos_visible_p. */
21196 it.glyph_row->enabled_p = false;
21197 prepare_desired_row (it.glyph_row);
21199 it.glyph_row->mode_line_p = 1;
21201 /* FIXME: This should be controlled by a user option. But
21202 supporting such an option is not trivial, since the mode line is
21203 made up of many separate strings. */
21204 it.paragraph_embedding = L2R;
21206 record_unwind_protect (unwind_format_mode_line,
21207 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21209 mode_line_target = MODE_LINE_DISPLAY;
21211 /* Temporarily make frame's keyboard the current kboard so that
21212 kboard-local variables in the mode_line_format will get the right
21213 values. */
21214 push_kboard (FRAME_KBOARD (it.f));
21215 record_unwind_save_match_data ();
21216 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21217 pop_kboard ();
21219 unbind_to (count, Qnil);
21221 /* Fill up with spaces. */
21222 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21224 compute_line_metrics (&it);
21225 it.glyph_row->full_width_p = 1;
21226 it.glyph_row->continued_p = 0;
21227 it.glyph_row->truncated_on_left_p = 0;
21228 it.glyph_row->truncated_on_right_p = 0;
21230 /* Make a 3D mode-line have a shadow at its right end. */
21231 face = FACE_FROM_ID (it.f, face_id);
21232 extend_face_to_end_of_line (&it);
21233 if (face->box != FACE_NO_BOX)
21235 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21236 + it.glyph_row->used[TEXT_AREA] - 1);
21237 last->right_box_line_p = 1;
21240 return it.glyph_row->height;
21243 /* Move element ELT in LIST to the front of LIST.
21244 Return the updated list. */
21246 static Lisp_Object
21247 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21249 register Lisp_Object tail, prev;
21250 register Lisp_Object tem;
21252 tail = list;
21253 prev = Qnil;
21254 while (CONSP (tail))
21256 tem = XCAR (tail);
21258 if (EQ (elt, tem))
21260 /* Splice out the link TAIL. */
21261 if (NILP (prev))
21262 list = XCDR (tail);
21263 else
21264 Fsetcdr (prev, XCDR (tail));
21266 /* Now make it the first. */
21267 Fsetcdr (tail, list);
21268 return tail;
21270 else
21271 prev = tail;
21272 tail = XCDR (tail);
21273 QUIT;
21276 /* Not found--return unchanged LIST. */
21277 return list;
21280 /* Contribute ELT to the mode line for window IT->w. How it
21281 translates into text depends on its data type.
21283 IT describes the display environment in which we display, as usual.
21285 DEPTH is the depth in recursion. It is used to prevent
21286 infinite recursion here.
21288 FIELD_WIDTH is the number of characters the display of ELT should
21289 occupy in the mode line, and PRECISION is the maximum number of
21290 characters to display from ELT's representation. See
21291 display_string for details.
21293 Returns the hpos of the end of the text generated by ELT.
21295 PROPS is a property list to add to any string we encounter.
21297 If RISKY is nonzero, remove (disregard) any properties in any string
21298 we encounter, and ignore :eval and :propertize.
21300 The global variable `mode_line_target' determines whether the
21301 output is passed to `store_mode_line_noprop',
21302 `store_mode_line_string', or `display_string'. */
21304 static int
21305 display_mode_element (struct it *it, int depth, int field_width, int precision,
21306 Lisp_Object elt, Lisp_Object props, int risky)
21308 int n = 0, field, prec;
21309 int literal = 0;
21311 tail_recurse:
21312 if (depth > 100)
21313 elt = build_string ("*too-deep*");
21315 depth++;
21317 switch (XTYPE (elt))
21319 case Lisp_String:
21321 /* A string: output it and check for %-constructs within it. */
21322 unsigned char c;
21323 ptrdiff_t offset = 0;
21325 if (SCHARS (elt) > 0
21326 && (!NILP (props) || risky))
21328 Lisp_Object oprops, aelt;
21329 oprops = Ftext_properties_at (make_number (0), elt);
21331 /* If the starting string's properties are not what
21332 we want, translate the string. Also, if the string
21333 is risky, do that anyway. */
21335 if (NILP (Fequal (props, oprops)) || risky)
21337 /* If the starting string has properties,
21338 merge the specified ones onto the existing ones. */
21339 if (! NILP (oprops) && !risky)
21341 Lisp_Object tem;
21343 oprops = Fcopy_sequence (oprops);
21344 tem = props;
21345 while (CONSP (tem))
21347 oprops = Fplist_put (oprops, XCAR (tem),
21348 XCAR (XCDR (tem)));
21349 tem = XCDR (XCDR (tem));
21351 props = oprops;
21354 aelt = Fassoc (elt, mode_line_proptrans_alist);
21355 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21357 /* AELT is what we want. Move it to the front
21358 without consing. */
21359 elt = XCAR (aelt);
21360 mode_line_proptrans_alist
21361 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21363 else
21365 Lisp_Object tem;
21367 /* If AELT has the wrong props, it is useless.
21368 so get rid of it. */
21369 if (! NILP (aelt))
21370 mode_line_proptrans_alist
21371 = Fdelq (aelt, mode_line_proptrans_alist);
21373 elt = Fcopy_sequence (elt);
21374 Fset_text_properties (make_number (0), Flength (elt),
21375 props, elt);
21376 /* Add this item to mode_line_proptrans_alist. */
21377 mode_line_proptrans_alist
21378 = Fcons (Fcons (elt, props),
21379 mode_line_proptrans_alist);
21380 /* Truncate mode_line_proptrans_alist
21381 to at most 50 elements. */
21382 tem = Fnthcdr (make_number (50),
21383 mode_line_proptrans_alist);
21384 if (! NILP (tem))
21385 XSETCDR (tem, Qnil);
21390 offset = 0;
21392 if (literal)
21394 prec = precision - n;
21395 switch (mode_line_target)
21397 case MODE_LINE_NOPROP:
21398 case MODE_LINE_TITLE:
21399 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21400 break;
21401 case MODE_LINE_STRING:
21402 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21403 break;
21404 case MODE_LINE_DISPLAY:
21405 n += display_string (NULL, elt, Qnil, 0, 0, it,
21406 0, prec, 0, STRING_MULTIBYTE (elt));
21407 break;
21410 break;
21413 /* Handle the non-literal case. */
21415 while ((precision <= 0 || n < precision)
21416 && SREF (elt, offset) != 0
21417 && (mode_line_target != MODE_LINE_DISPLAY
21418 || it->current_x < it->last_visible_x))
21420 ptrdiff_t last_offset = offset;
21422 /* Advance to end of string or next format specifier. */
21423 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21426 if (offset - 1 != last_offset)
21428 ptrdiff_t nchars, nbytes;
21430 /* Output to end of string or up to '%'. Field width
21431 is length of string. Don't output more than
21432 PRECISION allows us. */
21433 offset--;
21435 prec = c_string_width (SDATA (elt) + last_offset,
21436 offset - last_offset, precision - n,
21437 &nchars, &nbytes);
21439 switch (mode_line_target)
21441 case MODE_LINE_NOPROP:
21442 case MODE_LINE_TITLE:
21443 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21444 break;
21445 case MODE_LINE_STRING:
21447 ptrdiff_t bytepos = last_offset;
21448 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21449 ptrdiff_t endpos = (precision <= 0
21450 ? string_byte_to_char (elt, offset)
21451 : charpos + nchars);
21453 n += store_mode_line_string (NULL,
21454 Fsubstring (elt, make_number (charpos),
21455 make_number (endpos)),
21456 0, 0, 0, Qnil);
21458 break;
21459 case MODE_LINE_DISPLAY:
21461 ptrdiff_t bytepos = last_offset;
21462 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21464 if (precision <= 0)
21465 nchars = string_byte_to_char (elt, offset) - charpos;
21466 n += display_string (NULL, elt, Qnil, 0, charpos,
21467 it, 0, nchars, 0,
21468 STRING_MULTIBYTE (elt));
21470 break;
21473 else /* c == '%' */
21475 ptrdiff_t percent_position = offset;
21477 /* Get the specified minimum width. Zero means
21478 don't pad. */
21479 field = 0;
21480 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21481 field = field * 10 + c - '0';
21483 /* Don't pad beyond the total padding allowed. */
21484 if (field_width - n > 0 && field > field_width - n)
21485 field = field_width - n;
21487 /* Note that either PRECISION <= 0 or N < PRECISION. */
21488 prec = precision - n;
21490 if (c == 'M')
21491 n += display_mode_element (it, depth, field, prec,
21492 Vglobal_mode_string, props,
21493 risky);
21494 else if (c != 0)
21496 bool multibyte;
21497 ptrdiff_t bytepos, charpos;
21498 const char *spec;
21499 Lisp_Object string;
21501 bytepos = percent_position;
21502 charpos = (STRING_MULTIBYTE (elt)
21503 ? string_byte_to_char (elt, bytepos)
21504 : bytepos);
21505 spec = decode_mode_spec (it->w, c, field, &string);
21506 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21508 switch (mode_line_target)
21510 case MODE_LINE_NOPROP:
21511 case MODE_LINE_TITLE:
21512 n += store_mode_line_noprop (spec, field, prec);
21513 break;
21514 case MODE_LINE_STRING:
21516 Lisp_Object tem = build_string (spec);
21517 props = Ftext_properties_at (make_number (charpos), elt);
21518 /* Should only keep face property in props */
21519 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21521 break;
21522 case MODE_LINE_DISPLAY:
21524 int nglyphs_before, nwritten;
21526 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21527 nwritten = display_string (spec, string, elt,
21528 charpos, 0, it,
21529 field, prec, 0,
21530 multibyte);
21532 /* Assign to the glyphs written above the
21533 string where the `%x' came from, position
21534 of the `%'. */
21535 if (nwritten > 0)
21537 struct glyph *glyph
21538 = (it->glyph_row->glyphs[TEXT_AREA]
21539 + nglyphs_before);
21540 int i;
21542 for (i = 0; i < nwritten; ++i)
21544 glyph[i].object = elt;
21545 glyph[i].charpos = charpos;
21548 n += nwritten;
21551 break;
21554 else /* c == 0 */
21555 break;
21559 break;
21561 case Lisp_Symbol:
21562 /* A symbol: process the value of the symbol recursively
21563 as if it appeared here directly. Avoid error if symbol void.
21564 Special case: if value of symbol is a string, output the string
21565 literally. */
21567 register Lisp_Object tem;
21569 /* If the variable is not marked as risky to set
21570 then its contents are risky to use. */
21571 if (NILP (Fget (elt, Qrisky_local_variable)))
21572 risky = 1;
21574 tem = Fboundp (elt);
21575 if (!NILP (tem))
21577 tem = Fsymbol_value (elt);
21578 /* If value is a string, output that string literally:
21579 don't check for % within it. */
21580 if (STRINGP (tem))
21581 literal = 1;
21583 if (!EQ (tem, elt))
21585 /* Give up right away for nil or t. */
21586 elt = tem;
21587 goto tail_recurse;
21591 break;
21593 case Lisp_Cons:
21595 register Lisp_Object car, tem;
21597 /* A cons cell: five distinct cases.
21598 If first element is :eval or :propertize, do something special.
21599 If first element is a string or a cons, process all the elements
21600 and effectively concatenate them.
21601 If first element is a negative number, truncate displaying cdr to
21602 at most that many characters. If positive, pad (with spaces)
21603 to at least that many characters.
21604 If first element is a symbol, process the cadr or caddr recursively
21605 according to whether the symbol's value is non-nil or nil. */
21606 car = XCAR (elt);
21607 if (EQ (car, QCeval))
21609 /* An element of the form (:eval FORM) means evaluate FORM
21610 and use the result as mode line elements. */
21612 if (risky)
21613 break;
21615 if (CONSP (XCDR (elt)))
21617 Lisp_Object spec;
21618 spec = safe_eval (XCAR (XCDR (elt)));
21619 n += display_mode_element (it, depth, field_width - n,
21620 precision - n, spec, props,
21621 risky);
21624 else if (EQ (car, QCpropertize))
21626 /* An element of the form (:propertize ELT PROPS...)
21627 means display ELT but applying properties PROPS. */
21629 if (risky)
21630 break;
21632 if (CONSP (XCDR (elt)))
21633 n += display_mode_element (it, depth, field_width - n,
21634 precision - n, XCAR (XCDR (elt)),
21635 XCDR (XCDR (elt)), risky);
21637 else if (SYMBOLP (car))
21639 tem = Fboundp (car);
21640 elt = XCDR (elt);
21641 if (!CONSP (elt))
21642 goto invalid;
21643 /* elt is now the cdr, and we know it is a cons cell.
21644 Use its car if CAR has a non-nil value. */
21645 if (!NILP (tem))
21647 tem = Fsymbol_value (car);
21648 if (!NILP (tem))
21650 elt = XCAR (elt);
21651 goto tail_recurse;
21654 /* Symbol's value is nil (or symbol is unbound)
21655 Get the cddr of the original list
21656 and if possible find the caddr and use that. */
21657 elt = XCDR (elt);
21658 if (NILP (elt))
21659 break;
21660 else if (!CONSP (elt))
21661 goto invalid;
21662 elt = XCAR (elt);
21663 goto tail_recurse;
21665 else if (INTEGERP (car))
21667 register int lim = XINT (car);
21668 elt = XCDR (elt);
21669 if (lim < 0)
21671 /* Negative int means reduce maximum width. */
21672 if (precision <= 0)
21673 precision = -lim;
21674 else
21675 precision = min (precision, -lim);
21677 else if (lim > 0)
21679 /* Padding specified. Don't let it be more than
21680 current maximum. */
21681 if (precision > 0)
21682 lim = min (precision, lim);
21684 /* If that's more padding than already wanted, queue it.
21685 But don't reduce padding already specified even if
21686 that is beyond the current truncation point. */
21687 field_width = max (lim, field_width);
21689 goto tail_recurse;
21691 else if (STRINGP (car) || CONSP (car))
21693 Lisp_Object halftail = elt;
21694 int len = 0;
21696 while (CONSP (elt)
21697 && (precision <= 0 || n < precision))
21699 n += display_mode_element (it, depth,
21700 /* Do padding only after the last
21701 element in the list. */
21702 (! CONSP (XCDR (elt))
21703 ? field_width - n
21704 : 0),
21705 precision - n, XCAR (elt),
21706 props, risky);
21707 elt = XCDR (elt);
21708 len++;
21709 if ((len & 1) == 0)
21710 halftail = XCDR (halftail);
21711 /* Check for cycle. */
21712 if (EQ (halftail, elt))
21713 break;
21717 break;
21719 default:
21720 invalid:
21721 elt = build_string ("*invalid*");
21722 goto tail_recurse;
21725 /* Pad to FIELD_WIDTH. */
21726 if (field_width > 0 && n < field_width)
21728 switch (mode_line_target)
21730 case MODE_LINE_NOPROP:
21731 case MODE_LINE_TITLE:
21732 n += store_mode_line_noprop ("", field_width - n, 0);
21733 break;
21734 case MODE_LINE_STRING:
21735 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21736 break;
21737 case MODE_LINE_DISPLAY:
21738 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21739 0, 0, 0);
21740 break;
21744 return n;
21747 /* Store a mode-line string element in mode_line_string_list.
21749 If STRING is non-null, display that C string. Otherwise, the Lisp
21750 string LISP_STRING is displayed.
21752 FIELD_WIDTH is the minimum number of output glyphs to produce.
21753 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21754 with spaces. FIELD_WIDTH <= 0 means don't pad.
21756 PRECISION is the maximum number of characters to output from
21757 STRING. PRECISION <= 0 means don't truncate the string.
21759 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21760 properties to the string.
21762 PROPS are the properties to add to the string.
21763 The mode_line_string_face face property is always added to the string.
21766 static int
21767 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21768 int field_width, int precision, Lisp_Object props)
21770 ptrdiff_t len;
21771 int n = 0;
21773 if (string != NULL)
21775 len = strlen (string);
21776 if (precision > 0 && len > precision)
21777 len = precision;
21778 lisp_string = make_string (string, len);
21779 if (NILP (props))
21780 props = mode_line_string_face_prop;
21781 else if (!NILP (mode_line_string_face))
21783 Lisp_Object face = Fplist_get (props, Qface);
21784 props = Fcopy_sequence (props);
21785 if (NILP (face))
21786 face = mode_line_string_face;
21787 else
21788 face = list2 (face, mode_line_string_face);
21789 props = Fplist_put (props, Qface, face);
21791 Fadd_text_properties (make_number (0), make_number (len),
21792 props, lisp_string);
21794 else
21796 len = XFASTINT (Flength (lisp_string));
21797 if (precision > 0 && len > precision)
21799 len = precision;
21800 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21801 precision = -1;
21803 if (!NILP (mode_line_string_face))
21805 Lisp_Object face;
21806 if (NILP (props))
21807 props = Ftext_properties_at (make_number (0), lisp_string);
21808 face = Fplist_get (props, Qface);
21809 if (NILP (face))
21810 face = mode_line_string_face;
21811 else
21812 face = list2 (face, mode_line_string_face);
21813 props = list2 (Qface, face);
21814 if (copy_string)
21815 lisp_string = Fcopy_sequence (lisp_string);
21817 if (!NILP (props))
21818 Fadd_text_properties (make_number (0), make_number (len),
21819 props, lisp_string);
21822 if (len > 0)
21824 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21825 n += len;
21828 if (field_width > len)
21830 field_width -= len;
21831 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21832 if (!NILP (props))
21833 Fadd_text_properties (make_number (0), make_number (field_width),
21834 props, lisp_string);
21835 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21836 n += field_width;
21839 return n;
21843 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21844 1, 4, 0,
21845 doc: /* Format a string out of a mode line format specification.
21846 First arg FORMAT specifies the mode line format (see `mode-line-format'
21847 for details) to use.
21849 By default, the format is evaluated for the currently selected window.
21851 Optional second arg FACE specifies the face property to put on all
21852 characters for which no face is specified. The value nil means the
21853 default face. The value t means whatever face the window's mode line
21854 currently uses (either `mode-line' or `mode-line-inactive',
21855 depending on whether the window is the selected window or not).
21856 An integer value means the value string has no text
21857 properties.
21859 Optional third and fourth args WINDOW and BUFFER specify the window
21860 and buffer to use as the context for the formatting (defaults
21861 are the selected window and the WINDOW's buffer). */)
21862 (Lisp_Object format, Lisp_Object face,
21863 Lisp_Object window, Lisp_Object buffer)
21865 struct it it;
21866 int len;
21867 struct window *w;
21868 struct buffer *old_buffer = NULL;
21869 int face_id;
21870 int no_props = INTEGERP (face);
21871 ptrdiff_t count = SPECPDL_INDEX ();
21872 Lisp_Object str;
21873 int string_start = 0;
21875 w = decode_any_window (window);
21876 XSETWINDOW (window, w);
21878 if (NILP (buffer))
21879 buffer = w->contents;
21880 CHECK_BUFFER (buffer);
21882 /* Make formatting the modeline a non-op when noninteractive, otherwise
21883 there will be problems later caused by a partially initialized frame. */
21884 if (NILP (format) || noninteractive)
21885 return empty_unibyte_string;
21887 if (no_props)
21888 face = Qnil;
21890 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21891 : EQ (face, Qt) ? (EQ (window, selected_window)
21892 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21893 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21894 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21895 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21896 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21897 : DEFAULT_FACE_ID;
21899 old_buffer = current_buffer;
21901 /* Save things including mode_line_proptrans_alist,
21902 and set that to nil so that we don't alter the outer value. */
21903 record_unwind_protect (unwind_format_mode_line,
21904 format_mode_line_unwind_data
21905 (XFRAME (WINDOW_FRAME (w)),
21906 old_buffer, selected_window, 1));
21907 mode_line_proptrans_alist = Qnil;
21909 Fselect_window (window, Qt);
21910 set_buffer_internal_1 (XBUFFER (buffer));
21912 init_iterator (&it, w, -1, -1, NULL, face_id);
21914 if (no_props)
21916 mode_line_target = MODE_LINE_NOPROP;
21917 mode_line_string_face_prop = Qnil;
21918 mode_line_string_list = Qnil;
21919 string_start = MODE_LINE_NOPROP_LEN (0);
21921 else
21923 mode_line_target = MODE_LINE_STRING;
21924 mode_line_string_list = Qnil;
21925 mode_line_string_face = face;
21926 mode_line_string_face_prop
21927 = NILP (face) ? Qnil : list2 (Qface, face);
21930 push_kboard (FRAME_KBOARD (it.f));
21931 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21932 pop_kboard ();
21934 if (no_props)
21936 len = MODE_LINE_NOPROP_LEN (string_start);
21937 str = make_string (mode_line_noprop_buf + string_start, len);
21939 else
21941 mode_line_string_list = Fnreverse (mode_line_string_list);
21942 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21943 empty_unibyte_string);
21946 unbind_to (count, Qnil);
21947 return str;
21950 /* Write a null-terminated, right justified decimal representation of
21951 the positive integer D to BUF using a minimal field width WIDTH. */
21953 static void
21954 pint2str (register char *buf, register int width, register ptrdiff_t d)
21956 register char *p = buf;
21958 if (d <= 0)
21959 *p++ = '0';
21960 else
21962 while (d > 0)
21964 *p++ = d % 10 + '0';
21965 d /= 10;
21969 for (width -= (int) (p - buf); width > 0; --width)
21970 *p++ = ' ';
21971 *p-- = '\0';
21972 while (p > buf)
21974 d = *buf;
21975 *buf++ = *p;
21976 *p-- = d;
21980 /* Write a null-terminated, right justified decimal and "human
21981 readable" representation of the nonnegative integer D to BUF using
21982 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21984 static const char power_letter[] =
21986 0, /* no letter */
21987 'k', /* kilo */
21988 'M', /* mega */
21989 'G', /* giga */
21990 'T', /* tera */
21991 'P', /* peta */
21992 'E', /* exa */
21993 'Z', /* zetta */
21994 'Y' /* yotta */
21997 static void
21998 pint2hrstr (char *buf, int width, ptrdiff_t d)
22000 /* We aim to represent the nonnegative integer D as
22001 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22002 ptrdiff_t quotient = d;
22003 int remainder = 0;
22004 /* -1 means: do not use TENTHS. */
22005 int tenths = -1;
22006 int exponent = 0;
22008 /* Length of QUOTIENT.TENTHS as a string. */
22009 int length;
22011 char * psuffix;
22012 char * p;
22014 if (quotient >= 1000)
22016 /* Scale to the appropriate EXPONENT. */
22019 remainder = quotient % 1000;
22020 quotient /= 1000;
22021 exponent++;
22023 while (quotient >= 1000);
22025 /* Round to nearest and decide whether to use TENTHS or not. */
22026 if (quotient <= 9)
22028 tenths = remainder / 100;
22029 if (remainder % 100 >= 50)
22031 if (tenths < 9)
22032 tenths++;
22033 else
22035 quotient++;
22036 if (quotient == 10)
22037 tenths = -1;
22038 else
22039 tenths = 0;
22043 else
22044 if (remainder >= 500)
22046 if (quotient < 999)
22047 quotient++;
22048 else
22050 quotient = 1;
22051 exponent++;
22052 tenths = 0;
22057 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22058 if (tenths == -1 && quotient <= 99)
22059 if (quotient <= 9)
22060 length = 1;
22061 else
22062 length = 2;
22063 else
22064 length = 3;
22065 p = psuffix = buf + max (width, length);
22067 /* Print EXPONENT. */
22068 *psuffix++ = power_letter[exponent];
22069 *psuffix = '\0';
22071 /* Print TENTHS. */
22072 if (tenths >= 0)
22074 *--p = '0' + tenths;
22075 *--p = '.';
22078 /* Print QUOTIENT. */
22081 int digit = quotient % 10;
22082 *--p = '0' + digit;
22084 while ((quotient /= 10) != 0);
22086 /* Print leading spaces. */
22087 while (buf < p)
22088 *--p = ' ';
22091 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22092 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22093 type of CODING_SYSTEM. Return updated pointer into BUF. */
22095 static unsigned char invalid_eol_type[] = "(*invalid*)";
22097 static char *
22098 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22100 Lisp_Object val;
22101 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22102 const unsigned char *eol_str;
22103 int eol_str_len;
22104 /* The EOL conversion we are using. */
22105 Lisp_Object eoltype;
22107 val = CODING_SYSTEM_SPEC (coding_system);
22108 eoltype = Qnil;
22110 if (!VECTORP (val)) /* Not yet decided. */
22112 *buf++ = multibyte ? '-' : ' ';
22113 if (eol_flag)
22114 eoltype = eol_mnemonic_undecided;
22115 /* Don't mention EOL conversion if it isn't decided. */
22117 else
22119 Lisp_Object attrs;
22120 Lisp_Object eolvalue;
22122 attrs = AREF (val, 0);
22123 eolvalue = AREF (val, 2);
22125 *buf++ = multibyte
22126 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22127 : ' ';
22129 if (eol_flag)
22131 /* The EOL conversion that is normal on this system. */
22133 if (NILP (eolvalue)) /* Not yet decided. */
22134 eoltype = eol_mnemonic_undecided;
22135 else if (VECTORP (eolvalue)) /* Not yet decided. */
22136 eoltype = eol_mnemonic_undecided;
22137 else /* eolvalue is Qunix, Qdos, or Qmac. */
22138 eoltype = (EQ (eolvalue, Qunix)
22139 ? eol_mnemonic_unix
22140 : (EQ (eolvalue, Qdos) == 1
22141 ? eol_mnemonic_dos : eol_mnemonic_mac));
22145 if (eol_flag)
22147 /* Mention the EOL conversion if it is not the usual one. */
22148 if (STRINGP (eoltype))
22150 eol_str = SDATA (eoltype);
22151 eol_str_len = SBYTES (eoltype);
22153 else if (CHARACTERP (eoltype))
22155 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22156 int c = XFASTINT (eoltype);
22157 eol_str_len = CHAR_STRING (c, tmp);
22158 eol_str = tmp;
22160 else
22162 eol_str = invalid_eol_type;
22163 eol_str_len = sizeof (invalid_eol_type) - 1;
22165 memcpy (buf, eol_str, eol_str_len);
22166 buf += eol_str_len;
22169 return buf;
22172 /* Return a string for the output of a mode line %-spec for window W,
22173 generated by character C. FIELD_WIDTH > 0 means pad the string
22174 returned with spaces to that value. Return a Lisp string in
22175 *STRING if the resulting string is taken from that Lisp string.
22177 Note we operate on the current buffer for most purposes. */
22179 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22181 static const char *
22182 decode_mode_spec (struct window *w, register int c, int field_width,
22183 Lisp_Object *string)
22185 Lisp_Object obj;
22186 struct frame *f = XFRAME (WINDOW_FRAME (w));
22187 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22188 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22189 produce strings from numerical values, so limit preposterously
22190 large values of FIELD_WIDTH to avoid overrunning the buffer's
22191 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22192 bytes plus the terminating null. */
22193 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22194 struct buffer *b = current_buffer;
22196 obj = Qnil;
22197 *string = Qnil;
22199 switch (c)
22201 case '*':
22202 if (!NILP (BVAR (b, read_only)))
22203 return "%";
22204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22205 return "*";
22206 return "-";
22208 case '+':
22209 /* This differs from %* only for a modified read-only buffer. */
22210 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22211 return "*";
22212 if (!NILP (BVAR (b, read_only)))
22213 return "%";
22214 return "-";
22216 case '&':
22217 /* This differs from %* in ignoring read-only-ness. */
22218 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22219 return "*";
22220 return "-";
22222 case '%':
22223 return "%";
22225 case '[':
22227 int i;
22228 char *p;
22230 if (command_loop_level > 5)
22231 return "[[[... ";
22232 p = decode_mode_spec_buf;
22233 for (i = 0; i < command_loop_level; i++)
22234 *p++ = '[';
22235 *p = 0;
22236 return decode_mode_spec_buf;
22239 case ']':
22241 int i;
22242 char *p;
22244 if (command_loop_level > 5)
22245 return " ...]]]";
22246 p = decode_mode_spec_buf;
22247 for (i = 0; i < command_loop_level; i++)
22248 *p++ = ']';
22249 *p = 0;
22250 return decode_mode_spec_buf;
22253 case '-':
22255 register int i;
22257 /* Let lots_of_dashes be a string of infinite length. */
22258 if (mode_line_target == MODE_LINE_NOPROP
22259 || mode_line_target == MODE_LINE_STRING)
22260 return "--";
22261 if (field_width <= 0
22262 || field_width > sizeof (lots_of_dashes))
22264 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22265 decode_mode_spec_buf[i] = '-';
22266 decode_mode_spec_buf[i] = '\0';
22267 return decode_mode_spec_buf;
22269 else
22270 return lots_of_dashes;
22273 case 'b':
22274 obj = BVAR (b, name);
22275 break;
22277 case 'c':
22278 /* %c and %l are ignored in `frame-title-format'.
22279 (In redisplay_internal, the frame title is drawn _before_ the
22280 windows are updated, so the stuff which depends on actual
22281 window contents (such as %l) may fail to render properly, or
22282 even crash emacs.) */
22283 if (mode_line_target == MODE_LINE_TITLE)
22284 return "";
22285 else
22287 ptrdiff_t col = current_column ();
22288 w->column_number_displayed = col;
22289 pint2str (decode_mode_spec_buf, width, col);
22290 return decode_mode_spec_buf;
22293 case 'e':
22294 #ifndef SYSTEM_MALLOC
22296 if (NILP (Vmemory_full))
22297 return "";
22298 else
22299 return "!MEM FULL! ";
22301 #else
22302 return "";
22303 #endif
22305 case 'F':
22306 /* %F displays the frame name. */
22307 if (!NILP (f->title))
22308 return SSDATA (f->title);
22309 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22310 return SSDATA (f->name);
22311 return "Emacs";
22313 case 'f':
22314 obj = BVAR (b, filename);
22315 break;
22317 case 'i':
22319 ptrdiff_t size = ZV - BEGV;
22320 pint2str (decode_mode_spec_buf, width, size);
22321 return decode_mode_spec_buf;
22324 case 'I':
22326 ptrdiff_t size = ZV - BEGV;
22327 pint2hrstr (decode_mode_spec_buf, width, size);
22328 return decode_mode_spec_buf;
22331 case 'l':
22333 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22334 ptrdiff_t topline, nlines, height;
22335 ptrdiff_t junk;
22337 /* %c and %l are ignored in `frame-title-format'. */
22338 if (mode_line_target == MODE_LINE_TITLE)
22339 return "";
22341 startpos = marker_position (w->start);
22342 startpos_byte = marker_byte_position (w->start);
22343 height = WINDOW_TOTAL_LINES (w);
22345 /* If we decided that this buffer isn't suitable for line numbers,
22346 don't forget that too fast. */
22347 if (w->base_line_pos == -1)
22348 goto no_value;
22350 /* If the buffer is very big, don't waste time. */
22351 if (INTEGERP (Vline_number_display_limit)
22352 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22354 w->base_line_pos = 0;
22355 w->base_line_number = 0;
22356 goto no_value;
22359 if (w->base_line_number > 0
22360 && w->base_line_pos > 0
22361 && w->base_line_pos <= startpos)
22363 line = w->base_line_number;
22364 linepos = w->base_line_pos;
22365 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22367 else
22369 line = 1;
22370 linepos = BUF_BEGV (b);
22371 linepos_byte = BUF_BEGV_BYTE (b);
22374 /* Count lines from base line to window start position. */
22375 nlines = display_count_lines (linepos_byte,
22376 startpos_byte,
22377 startpos, &junk);
22379 topline = nlines + line;
22381 /* Determine a new base line, if the old one is too close
22382 or too far away, or if we did not have one.
22383 "Too close" means it's plausible a scroll-down would
22384 go back past it. */
22385 if (startpos == BUF_BEGV (b))
22387 w->base_line_number = topline;
22388 w->base_line_pos = BUF_BEGV (b);
22390 else if (nlines < height + 25 || nlines > height * 3 + 50
22391 || linepos == BUF_BEGV (b))
22393 ptrdiff_t limit = BUF_BEGV (b);
22394 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22395 ptrdiff_t position;
22396 ptrdiff_t distance =
22397 (height * 2 + 30) * line_number_display_limit_width;
22399 if (startpos - distance > limit)
22401 limit = startpos - distance;
22402 limit_byte = CHAR_TO_BYTE (limit);
22405 nlines = display_count_lines (startpos_byte,
22406 limit_byte,
22407 - (height * 2 + 30),
22408 &position);
22409 /* If we couldn't find the lines we wanted within
22410 line_number_display_limit_width chars per line,
22411 give up on line numbers for this window. */
22412 if (position == limit_byte && limit == startpos - distance)
22414 w->base_line_pos = -1;
22415 w->base_line_number = 0;
22416 goto no_value;
22419 w->base_line_number = topline - nlines;
22420 w->base_line_pos = BYTE_TO_CHAR (position);
22423 /* Now count lines from the start pos to point. */
22424 nlines = display_count_lines (startpos_byte,
22425 PT_BYTE, PT, &junk);
22427 /* Record that we did display the line number. */
22428 line_number_displayed = 1;
22430 /* Make the string to show. */
22431 pint2str (decode_mode_spec_buf, width, topline + nlines);
22432 return decode_mode_spec_buf;
22433 no_value:
22435 char* p = decode_mode_spec_buf;
22436 int pad = width - 2;
22437 while (pad-- > 0)
22438 *p++ = ' ';
22439 *p++ = '?';
22440 *p++ = '?';
22441 *p = '\0';
22442 return decode_mode_spec_buf;
22445 break;
22447 case 'm':
22448 obj = BVAR (b, mode_name);
22449 break;
22451 case 'n':
22452 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22453 return " Narrow";
22454 break;
22456 case 'p':
22458 ptrdiff_t pos = marker_position (w->start);
22459 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22461 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22463 if (pos <= BUF_BEGV (b))
22464 return "All";
22465 else
22466 return "Bottom";
22468 else if (pos <= BUF_BEGV (b))
22469 return "Top";
22470 else
22472 if (total > 1000000)
22473 /* Do it differently for a large value, to avoid overflow. */
22474 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22475 else
22476 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22477 /* We can't normally display a 3-digit number,
22478 so get us a 2-digit number that is close. */
22479 if (total == 100)
22480 total = 99;
22481 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22482 return decode_mode_spec_buf;
22486 /* Display percentage of size above the bottom of the screen. */
22487 case 'P':
22489 ptrdiff_t toppos = marker_position (w->start);
22490 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22491 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22493 if (botpos >= BUF_ZV (b))
22495 if (toppos <= BUF_BEGV (b))
22496 return "All";
22497 else
22498 return "Bottom";
22500 else
22502 if (total > 1000000)
22503 /* Do it differently for a large value, to avoid overflow. */
22504 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22505 else
22506 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22507 /* We can't normally display a 3-digit number,
22508 so get us a 2-digit number that is close. */
22509 if (total == 100)
22510 total = 99;
22511 if (toppos <= BUF_BEGV (b))
22512 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22513 else
22514 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22515 return decode_mode_spec_buf;
22519 case 's':
22520 /* status of process */
22521 obj = Fget_buffer_process (Fcurrent_buffer ());
22522 if (NILP (obj))
22523 return "no process";
22524 #ifndef MSDOS
22525 obj = Fsymbol_name (Fprocess_status (obj));
22526 #endif
22527 break;
22529 case '@':
22531 ptrdiff_t count = inhibit_garbage_collection ();
22532 Lisp_Object val = call1 (intern ("file-remote-p"),
22533 BVAR (current_buffer, directory));
22534 unbind_to (count, Qnil);
22536 if (NILP (val))
22537 return "-";
22538 else
22539 return "@";
22542 case 'z':
22543 /* coding-system (not including end-of-line format) */
22544 case 'Z':
22545 /* coding-system (including end-of-line type) */
22547 int eol_flag = (c == 'Z');
22548 char *p = decode_mode_spec_buf;
22550 if (! FRAME_WINDOW_P (f))
22552 /* No need to mention EOL here--the terminal never needs
22553 to do EOL conversion. */
22554 p = decode_mode_spec_coding (CODING_ID_NAME
22555 (FRAME_KEYBOARD_CODING (f)->id),
22556 p, 0);
22557 p = decode_mode_spec_coding (CODING_ID_NAME
22558 (FRAME_TERMINAL_CODING (f)->id),
22559 p, 0);
22561 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22562 p, eol_flag);
22564 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22565 #ifdef subprocesses
22566 obj = Fget_buffer_process (Fcurrent_buffer ());
22567 if (PROCESSP (obj))
22569 p = decode_mode_spec_coding
22570 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22571 p = decode_mode_spec_coding
22572 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22574 #endif /* subprocesses */
22575 #endif /* 0 */
22576 *p = 0;
22577 return decode_mode_spec_buf;
22581 if (STRINGP (obj))
22583 *string = obj;
22584 return SSDATA (obj);
22586 else
22587 return "";
22591 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22592 means count lines back from START_BYTE. But don't go beyond
22593 LIMIT_BYTE. Return the number of lines thus found (always
22594 nonnegative).
22596 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22597 either the position COUNT lines after/before START_BYTE, if we
22598 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22599 COUNT lines. */
22601 static ptrdiff_t
22602 display_count_lines (ptrdiff_t start_byte,
22603 ptrdiff_t limit_byte, ptrdiff_t count,
22604 ptrdiff_t *byte_pos_ptr)
22606 register unsigned char *cursor;
22607 unsigned char *base;
22609 register ptrdiff_t ceiling;
22610 register unsigned char *ceiling_addr;
22611 ptrdiff_t orig_count = count;
22613 /* If we are not in selective display mode,
22614 check only for newlines. */
22615 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22616 && !INTEGERP (BVAR (current_buffer, selective_display)));
22618 if (count > 0)
22620 while (start_byte < limit_byte)
22622 ceiling = BUFFER_CEILING_OF (start_byte);
22623 ceiling = min (limit_byte - 1, ceiling);
22624 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22625 base = (cursor = BYTE_POS_ADDR (start_byte));
22629 if (selective_display)
22631 while (*cursor != '\n' && *cursor != 015
22632 && ++cursor != ceiling_addr)
22633 continue;
22634 if (cursor == ceiling_addr)
22635 break;
22637 else
22639 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22640 if (! cursor)
22641 break;
22644 cursor++;
22646 if (--count == 0)
22648 start_byte += cursor - base;
22649 *byte_pos_ptr = start_byte;
22650 return orig_count;
22653 while (cursor < ceiling_addr);
22655 start_byte += ceiling_addr - base;
22658 else
22660 while (start_byte > limit_byte)
22662 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22663 ceiling = max (limit_byte, ceiling);
22664 ceiling_addr = BYTE_POS_ADDR (ceiling);
22665 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22666 while (1)
22668 if (selective_display)
22670 while (--cursor >= ceiling_addr
22671 && *cursor != '\n' && *cursor != 015)
22672 continue;
22673 if (cursor < ceiling_addr)
22674 break;
22676 else
22678 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22679 if (! cursor)
22680 break;
22683 if (++count == 0)
22685 start_byte += cursor - base + 1;
22686 *byte_pos_ptr = start_byte;
22687 /* When scanning backwards, we should
22688 not count the newline posterior to which we stop. */
22689 return - orig_count - 1;
22692 start_byte += ceiling_addr - base;
22696 *byte_pos_ptr = limit_byte;
22698 if (count < 0)
22699 return - orig_count + count;
22700 return orig_count - count;
22706 /***********************************************************************
22707 Displaying strings
22708 ***********************************************************************/
22710 /* Display a NUL-terminated string, starting with index START.
22712 If STRING is non-null, display that C string. Otherwise, the Lisp
22713 string LISP_STRING is displayed. There's a case that STRING is
22714 non-null and LISP_STRING is not nil. It means STRING is a string
22715 data of LISP_STRING. In that case, we display LISP_STRING while
22716 ignoring its text properties.
22718 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22719 FACE_STRING. Display STRING or LISP_STRING with the face at
22720 FACE_STRING_POS in FACE_STRING:
22722 Display the string in the environment given by IT, but use the
22723 standard display table, temporarily.
22725 FIELD_WIDTH is the minimum number of output glyphs to produce.
22726 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22727 with spaces. If STRING has more characters, more than FIELD_WIDTH
22728 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22730 PRECISION is the maximum number of characters to output from
22731 STRING. PRECISION < 0 means don't truncate the string.
22733 This is roughly equivalent to printf format specifiers:
22735 FIELD_WIDTH PRECISION PRINTF
22736 ----------------------------------------
22737 -1 -1 %s
22738 -1 10 %.10s
22739 10 -1 %10s
22740 20 10 %20.10s
22742 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22743 display them, and < 0 means obey the current buffer's value of
22744 enable_multibyte_characters.
22746 Value is the number of columns displayed. */
22748 static int
22749 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22750 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22751 int field_width, int precision, int max_x, int multibyte)
22753 int hpos_at_start = it->hpos;
22754 int saved_face_id = it->face_id;
22755 struct glyph_row *row = it->glyph_row;
22756 ptrdiff_t it_charpos;
22758 /* Initialize the iterator IT for iteration over STRING beginning
22759 with index START. */
22760 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22761 precision, field_width, multibyte);
22762 if (string && STRINGP (lisp_string))
22763 /* LISP_STRING is the one returned by decode_mode_spec. We should
22764 ignore its text properties. */
22765 it->stop_charpos = it->end_charpos;
22767 /* If displaying STRING, set up the face of the iterator from
22768 FACE_STRING, if that's given. */
22769 if (STRINGP (face_string))
22771 ptrdiff_t endptr;
22772 struct face *face;
22774 it->face_id
22775 = face_at_string_position (it->w, face_string, face_string_pos,
22776 0, &endptr, it->base_face_id, 0);
22777 face = FACE_FROM_ID (it->f, it->face_id);
22778 it->face_box_p = face->box != FACE_NO_BOX;
22781 /* Set max_x to the maximum allowed X position. Don't let it go
22782 beyond the right edge of the window. */
22783 if (max_x <= 0)
22784 max_x = it->last_visible_x;
22785 else
22786 max_x = min (max_x, it->last_visible_x);
22788 /* Skip over display elements that are not visible. because IT->w is
22789 hscrolled. */
22790 if (it->current_x < it->first_visible_x)
22791 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22792 MOVE_TO_POS | MOVE_TO_X);
22794 row->ascent = it->max_ascent;
22795 row->height = it->max_ascent + it->max_descent;
22796 row->phys_ascent = it->max_phys_ascent;
22797 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22798 row->extra_line_spacing = it->max_extra_line_spacing;
22800 if (STRINGP (it->string))
22801 it_charpos = IT_STRING_CHARPOS (*it);
22802 else
22803 it_charpos = IT_CHARPOS (*it);
22805 /* This condition is for the case that we are called with current_x
22806 past last_visible_x. */
22807 while (it->current_x < max_x)
22809 int x_before, x, n_glyphs_before, i, nglyphs;
22811 /* Get the next display element. */
22812 if (!get_next_display_element (it))
22813 break;
22815 /* Produce glyphs. */
22816 x_before = it->current_x;
22817 n_glyphs_before = row->used[TEXT_AREA];
22818 PRODUCE_GLYPHS (it);
22820 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22821 i = 0;
22822 x = x_before;
22823 while (i < nglyphs)
22825 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22827 if (it->line_wrap != TRUNCATE
22828 && x + glyph->pixel_width > max_x)
22830 /* End of continued line or max_x reached. */
22831 if (CHAR_GLYPH_PADDING_P (*glyph))
22833 /* A wide character is unbreakable. */
22834 if (row->reversed_p)
22835 unproduce_glyphs (it, row->used[TEXT_AREA]
22836 - n_glyphs_before);
22837 row->used[TEXT_AREA] = n_glyphs_before;
22838 it->current_x = x_before;
22840 else
22842 if (row->reversed_p)
22843 unproduce_glyphs (it, row->used[TEXT_AREA]
22844 - (n_glyphs_before + i));
22845 row->used[TEXT_AREA] = n_glyphs_before + i;
22846 it->current_x = x;
22848 break;
22850 else if (x + glyph->pixel_width >= it->first_visible_x)
22852 /* Glyph is at least partially visible. */
22853 ++it->hpos;
22854 if (x < it->first_visible_x)
22855 row->x = x - it->first_visible_x;
22857 else
22859 /* Glyph is off the left margin of the display area.
22860 Should not happen. */
22861 emacs_abort ();
22864 row->ascent = max (row->ascent, it->max_ascent);
22865 row->height = max (row->height, it->max_ascent + it->max_descent);
22866 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22867 row->phys_height = max (row->phys_height,
22868 it->max_phys_ascent + it->max_phys_descent);
22869 row->extra_line_spacing = max (row->extra_line_spacing,
22870 it->max_extra_line_spacing);
22871 x += glyph->pixel_width;
22872 ++i;
22875 /* Stop if max_x reached. */
22876 if (i < nglyphs)
22877 break;
22879 /* Stop at line ends. */
22880 if (ITERATOR_AT_END_OF_LINE_P (it))
22882 it->continuation_lines_width = 0;
22883 break;
22886 set_iterator_to_next (it, 1);
22887 if (STRINGP (it->string))
22888 it_charpos = IT_STRING_CHARPOS (*it);
22889 else
22890 it_charpos = IT_CHARPOS (*it);
22892 /* Stop if truncating at the right edge. */
22893 if (it->line_wrap == TRUNCATE
22894 && it->current_x >= it->last_visible_x)
22896 /* Add truncation mark, but don't do it if the line is
22897 truncated at a padding space. */
22898 if (it_charpos < it->string_nchars)
22900 if (!FRAME_WINDOW_P (it->f))
22902 int ii, n;
22904 if (it->current_x > it->last_visible_x)
22906 if (!row->reversed_p)
22908 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22909 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22910 break;
22912 else
22914 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22915 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22916 break;
22917 unproduce_glyphs (it, ii + 1);
22918 ii = row->used[TEXT_AREA] - (ii + 1);
22920 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22922 row->used[TEXT_AREA] = ii;
22923 produce_special_glyphs (it, IT_TRUNCATION);
22926 produce_special_glyphs (it, IT_TRUNCATION);
22928 row->truncated_on_right_p = 1;
22930 break;
22934 /* Maybe insert a truncation at the left. */
22935 if (it->first_visible_x
22936 && it_charpos > 0)
22938 if (!FRAME_WINDOW_P (it->f)
22939 || (row->reversed_p
22940 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22941 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22942 insert_left_trunc_glyphs (it);
22943 row->truncated_on_left_p = 1;
22946 it->face_id = saved_face_id;
22948 /* Value is number of columns displayed. */
22949 return it->hpos - hpos_at_start;
22954 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22955 appears as an element of LIST or as the car of an element of LIST.
22956 If PROPVAL is a list, compare each element against LIST in that
22957 way, and return 1/2 if any element of PROPVAL is found in LIST.
22958 Otherwise return 0. This function cannot quit.
22959 The return value is 2 if the text is invisible but with an ellipsis
22960 and 1 if it's invisible and without an ellipsis. */
22963 invisible_p (register Lisp_Object propval, Lisp_Object list)
22965 register Lisp_Object tail, proptail;
22967 for (tail = list; CONSP (tail); tail = XCDR (tail))
22969 register Lisp_Object tem;
22970 tem = XCAR (tail);
22971 if (EQ (propval, tem))
22972 return 1;
22973 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22974 return NILP (XCDR (tem)) ? 1 : 2;
22977 if (CONSP (propval))
22979 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22981 Lisp_Object propelt;
22982 propelt = XCAR (proptail);
22983 for (tail = list; CONSP (tail); tail = XCDR (tail))
22985 register Lisp_Object tem;
22986 tem = XCAR (tail);
22987 if (EQ (propelt, tem))
22988 return 1;
22989 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22990 return NILP (XCDR (tem)) ? 1 : 2;
22995 return 0;
22998 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22999 doc: /* Non-nil if the property makes the text invisible.
23000 POS-OR-PROP can be a marker or number, in which case it is taken to be
23001 a position in the current buffer and the value of the `invisible' property
23002 is checked; or it can be some other value, which is then presumed to be the
23003 value of the `invisible' property of the text of interest.
23004 The non-nil value returned can be t for truly invisible text or something
23005 else if the text is replaced by an ellipsis. */)
23006 (Lisp_Object pos_or_prop)
23008 Lisp_Object prop
23009 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23010 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23011 : pos_or_prop);
23012 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23013 return (invis == 0 ? Qnil
23014 : invis == 1 ? Qt
23015 : make_number (invis));
23018 /* Calculate a width or height in pixels from a specification using
23019 the following elements:
23021 SPEC ::=
23022 NUM - a (fractional) multiple of the default font width/height
23023 (NUM) - specifies exactly NUM pixels
23024 UNIT - a fixed number of pixels, see below.
23025 ELEMENT - size of a display element in pixels, see below.
23026 (NUM . SPEC) - equals NUM * SPEC
23027 (+ SPEC SPEC ...) - add pixel values
23028 (- SPEC SPEC ...) - subtract pixel values
23029 (- SPEC) - negate pixel value
23031 NUM ::=
23032 INT or FLOAT - a number constant
23033 SYMBOL - use symbol's (buffer local) variable binding.
23035 UNIT ::=
23036 in - pixels per inch *)
23037 mm - pixels per 1/1000 meter *)
23038 cm - pixels per 1/100 meter *)
23039 width - width of current font in pixels.
23040 height - height of current font in pixels.
23042 *) using the ratio(s) defined in display-pixels-per-inch.
23044 ELEMENT ::=
23046 left-fringe - left fringe width in pixels
23047 right-fringe - right fringe width in pixels
23049 left-margin - left margin width in pixels
23050 right-margin - right margin width in pixels
23052 scroll-bar - scroll-bar area width in pixels
23054 Examples:
23056 Pixels corresponding to 5 inches:
23057 (5 . in)
23059 Total width of non-text areas on left side of window (if scroll-bar is on left):
23060 '(space :width (+ left-fringe left-margin scroll-bar))
23062 Align to first text column (in header line):
23063 '(space :align-to 0)
23065 Align to middle of text area minus half the width of variable `my-image'
23066 containing a loaded image:
23067 '(space :align-to (0.5 . (- text my-image)))
23069 Width of left margin minus width of 1 character in the default font:
23070 '(space :width (- left-margin 1))
23072 Width of left margin minus width of 2 characters in the current font:
23073 '(space :width (- left-margin (2 . width)))
23075 Center 1 character over left-margin (in header line):
23076 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23078 Different ways to express width of left fringe plus left margin minus one pixel:
23079 '(space :width (- (+ left-fringe left-margin) (1)))
23080 '(space :width (+ left-fringe left-margin (- (1))))
23081 '(space :width (+ left-fringe left-margin (-1)))
23085 static int
23086 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23087 struct font *font, int width_p, int *align_to)
23089 double pixels;
23091 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23092 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23094 if (NILP (prop))
23095 return OK_PIXELS (0);
23097 eassert (FRAME_LIVE_P (it->f));
23099 if (SYMBOLP (prop))
23101 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23103 char *unit = SSDATA (SYMBOL_NAME (prop));
23105 if (unit[0] == 'i' && unit[1] == 'n')
23106 pixels = 1.0;
23107 else if (unit[0] == 'm' && unit[1] == 'm')
23108 pixels = 25.4;
23109 else if (unit[0] == 'c' && unit[1] == 'm')
23110 pixels = 2.54;
23111 else
23112 pixels = 0;
23113 if (pixels > 0)
23115 double ppi = (width_p ? FRAME_RES_X (it->f)
23116 : FRAME_RES_Y (it->f));
23118 if (ppi > 0)
23119 return OK_PIXELS (ppi / pixels);
23120 return 0;
23124 #ifdef HAVE_WINDOW_SYSTEM
23125 if (EQ (prop, Qheight))
23126 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23127 if (EQ (prop, Qwidth))
23128 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23129 #else
23130 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23131 return OK_PIXELS (1);
23132 #endif
23134 if (EQ (prop, Qtext))
23135 return OK_PIXELS (width_p
23136 ? window_box_width (it->w, TEXT_AREA)
23137 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23139 if (align_to && *align_to < 0)
23141 *res = 0;
23142 if (EQ (prop, Qleft))
23143 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23144 if (EQ (prop, Qright))
23145 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23146 if (EQ (prop, Qcenter))
23147 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23148 + window_box_width (it->w, TEXT_AREA) / 2);
23149 if (EQ (prop, Qleft_fringe))
23150 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23151 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23152 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23153 if (EQ (prop, Qright_fringe))
23154 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23155 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23156 : window_box_right_offset (it->w, TEXT_AREA));
23157 if (EQ (prop, Qleft_margin))
23158 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23159 if (EQ (prop, Qright_margin))
23160 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23161 if (EQ (prop, Qscroll_bar))
23162 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23164 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23165 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23166 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23167 : 0)));
23169 else
23171 if (EQ (prop, Qleft_fringe))
23172 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23173 if (EQ (prop, Qright_fringe))
23174 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23175 if (EQ (prop, Qleft_margin))
23176 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23177 if (EQ (prop, Qright_margin))
23178 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23179 if (EQ (prop, Qscroll_bar))
23180 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23183 prop = buffer_local_value_1 (prop, it->w->contents);
23184 if (EQ (prop, Qunbound))
23185 prop = Qnil;
23188 if (INTEGERP (prop) || FLOATP (prop))
23190 int base_unit = (width_p
23191 ? FRAME_COLUMN_WIDTH (it->f)
23192 : FRAME_LINE_HEIGHT (it->f));
23193 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23196 if (CONSP (prop))
23198 Lisp_Object car = XCAR (prop);
23199 Lisp_Object cdr = XCDR (prop);
23201 if (SYMBOLP (car))
23203 #ifdef HAVE_WINDOW_SYSTEM
23204 if (FRAME_WINDOW_P (it->f)
23205 && valid_image_p (prop))
23207 ptrdiff_t id = lookup_image (it->f, prop);
23208 struct image *img = IMAGE_FROM_ID (it->f, id);
23210 return OK_PIXELS (width_p ? img->width : img->height);
23212 #endif
23213 if (EQ (car, Qplus) || EQ (car, Qminus))
23215 int first = 1;
23216 double px;
23218 pixels = 0;
23219 while (CONSP (cdr))
23221 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23222 font, width_p, align_to))
23223 return 0;
23224 if (first)
23225 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23226 else
23227 pixels += px;
23228 cdr = XCDR (cdr);
23230 if (EQ (car, Qminus))
23231 pixels = -pixels;
23232 return OK_PIXELS (pixels);
23235 car = buffer_local_value_1 (car, it->w->contents);
23236 if (EQ (car, Qunbound))
23237 car = Qnil;
23240 if (INTEGERP (car) || FLOATP (car))
23242 double fact;
23243 pixels = XFLOATINT (car);
23244 if (NILP (cdr))
23245 return OK_PIXELS (pixels);
23246 if (calc_pixel_width_or_height (&fact, it, cdr,
23247 font, width_p, align_to))
23248 return OK_PIXELS (pixels * fact);
23249 return 0;
23252 return 0;
23255 return 0;
23259 /***********************************************************************
23260 Glyph Display
23261 ***********************************************************************/
23263 #ifdef HAVE_WINDOW_SYSTEM
23265 #ifdef GLYPH_DEBUG
23267 void
23268 dump_glyph_string (struct glyph_string *s)
23270 fprintf (stderr, "glyph string\n");
23271 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23272 s->x, s->y, s->width, s->height);
23273 fprintf (stderr, " ybase = %d\n", s->ybase);
23274 fprintf (stderr, " hl = %d\n", s->hl);
23275 fprintf (stderr, " left overhang = %d, right = %d\n",
23276 s->left_overhang, s->right_overhang);
23277 fprintf (stderr, " nchars = %d\n", s->nchars);
23278 fprintf (stderr, " extends to end of line = %d\n",
23279 s->extends_to_end_of_line_p);
23280 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23281 fprintf (stderr, " bg width = %d\n", s->background_width);
23284 #endif /* GLYPH_DEBUG */
23286 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23287 of XChar2b structures for S; it can't be allocated in
23288 init_glyph_string because it must be allocated via `alloca'. W
23289 is the window on which S is drawn. ROW and AREA are the glyph row
23290 and area within the row from which S is constructed. START is the
23291 index of the first glyph structure covered by S. HL is a
23292 face-override for drawing S. */
23294 #ifdef HAVE_NTGUI
23295 #define OPTIONAL_HDC(hdc) HDC hdc,
23296 #define DECLARE_HDC(hdc) HDC hdc;
23297 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23298 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23299 #endif
23301 #ifndef OPTIONAL_HDC
23302 #define OPTIONAL_HDC(hdc)
23303 #define DECLARE_HDC(hdc)
23304 #define ALLOCATE_HDC(hdc, f)
23305 #define RELEASE_HDC(hdc, f)
23306 #endif
23308 static void
23309 init_glyph_string (struct glyph_string *s,
23310 OPTIONAL_HDC (hdc)
23311 XChar2b *char2b, struct window *w, struct glyph_row *row,
23312 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23314 memset (s, 0, sizeof *s);
23315 s->w = w;
23316 s->f = XFRAME (w->frame);
23317 #ifdef HAVE_NTGUI
23318 s->hdc = hdc;
23319 #endif
23320 s->display = FRAME_X_DISPLAY (s->f);
23321 s->window = FRAME_X_WINDOW (s->f);
23322 s->char2b = char2b;
23323 s->hl = hl;
23324 s->row = row;
23325 s->area = area;
23326 s->first_glyph = row->glyphs[area] + start;
23327 s->height = row->height;
23328 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23329 s->ybase = s->y + row->ascent;
23333 /* Append the list of glyph strings with head H and tail T to the list
23334 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23336 static void
23337 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23338 struct glyph_string *h, struct glyph_string *t)
23340 if (h)
23342 if (*head)
23343 (*tail)->next = h;
23344 else
23345 *head = h;
23346 h->prev = *tail;
23347 *tail = t;
23352 /* Prepend the list of glyph strings with head H and tail T to the
23353 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23354 result. */
23356 static void
23357 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23358 struct glyph_string *h, struct glyph_string *t)
23360 if (h)
23362 if (*head)
23363 (*head)->prev = t;
23364 else
23365 *tail = t;
23366 t->next = *head;
23367 *head = h;
23372 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23373 Set *HEAD and *TAIL to the resulting list. */
23375 static void
23376 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23377 struct glyph_string *s)
23379 s->next = s->prev = NULL;
23380 append_glyph_string_lists (head, tail, s, s);
23384 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23385 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23386 make sure that X resources for the face returned are allocated.
23387 Value is a pointer to a realized face that is ready for display if
23388 DISPLAY_P is non-zero. */
23390 static struct face *
23391 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23392 XChar2b *char2b, int display_p)
23394 struct face *face = FACE_FROM_ID (f, face_id);
23395 unsigned code = 0;
23397 if (face->font)
23399 code = face->font->driver->encode_char (face->font, c);
23401 if (code == FONT_INVALID_CODE)
23402 code = 0;
23404 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23406 /* Make sure X resources of the face are allocated. */
23407 #ifdef HAVE_X_WINDOWS
23408 if (display_p)
23409 #endif
23411 eassert (face != NULL);
23412 PREPARE_FACE_FOR_DISPLAY (f, face);
23415 return face;
23419 /* Get face and two-byte form of character glyph GLYPH on frame F.
23420 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23421 a pointer to a realized face that is ready for display. */
23423 static struct face *
23424 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23425 XChar2b *char2b, int *two_byte_p)
23427 struct face *face;
23428 unsigned code = 0;
23430 eassert (glyph->type == CHAR_GLYPH);
23431 face = FACE_FROM_ID (f, glyph->face_id);
23433 /* Make sure X resources of the face are allocated. */
23434 eassert (face != NULL);
23435 PREPARE_FACE_FOR_DISPLAY (f, face);
23437 if (two_byte_p)
23438 *two_byte_p = 0;
23440 if (face->font)
23442 if (CHAR_BYTE8_P (glyph->u.ch))
23443 code = CHAR_TO_BYTE8 (glyph->u.ch);
23444 else
23445 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23447 if (code == FONT_INVALID_CODE)
23448 code = 0;
23451 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23452 return face;
23456 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23457 Return 1 if FONT has a glyph for C, otherwise return 0. */
23459 static int
23460 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23462 unsigned code;
23464 if (CHAR_BYTE8_P (c))
23465 code = CHAR_TO_BYTE8 (c);
23466 else
23467 code = font->driver->encode_char (font, c);
23469 if (code == FONT_INVALID_CODE)
23470 return 0;
23471 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23472 return 1;
23476 /* Fill glyph string S with composition components specified by S->cmp.
23478 BASE_FACE is the base face of the composition.
23479 S->cmp_from is the index of the first component for S.
23481 OVERLAPS non-zero means S should draw the foreground only, and use
23482 its physical height for clipping. See also draw_glyphs.
23484 Value is the index of a component not in S. */
23486 static int
23487 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23488 int overlaps)
23490 int i;
23491 /* For all glyphs of this composition, starting at the offset
23492 S->cmp_from, until we reach the end of the definition or encounter a
23493 glyph that requires the different face, add it to S. */
23494 struct face *face;
23496 eassert (s);
23498 s->for_overlaps = overlaps;
23499 s->face = NULL;
23500 s->font = NULL;
23501 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23503 int c = COMPOSITION_GLYPH (s->cmp, i);
23505 /* TAB in a composition means display glyphs with padding space
23506 on the left or right. */
23507 if (c != '\t')
23509 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23510 -1, Qnil);
23512 face = get_char_face_and_encoding (s->f, c, face_id,
23513 s->char2b + i, 1);
23514 if (face)
23516 if (! s->face)
23518 s->face = face;
23519 s->font = s->face->font;
23521 else if (s->face != face)
23522 break;
23525 ++s->nchars;
23527 s->cmp_to = i;
23529 if (s->face == NULL)
23531 s->face = base_face->ascii_face;
23532 s->font = s->face->font;
23535 /* All glyph strings for the same composition has the same width,
23536 i.e. the width set for the first component of the composition. */
23537 s->width = s->first_glyph->pixel_width;
23539 /* If the specified font could not be loaded, use the frame's
23540 default font, but record the fact that we couldn't load it in
23541 the glyph string so that we can draw rectangles for the
23542 characters of the glyph string. */
23543 if (s->font == NULL)
23545 s->font_not_found_p = 1;
23546 s->font = FRAME_FONT (s->f);
23549 /* Adjust base line for subscript/superscript text. */
23550 s->ybase += s->first_glyph->voffset;
23552 /* This glyph string must always be drawn with 16-bit functions. */
23553 s->two_byte_p = 1;
23555 return s->cmp_to;
23558 static int
23559 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23560 int start, int end, int overlaps)
23562 struct glyph *glyph, *last;
23563 Lisp_Object lgstring;
23564 int i;
23566 s->for_overlaps = overlaps;
23567 glyph = s->row->glyphs[s->area] + start;
23568 last = s->row->glyphs[s->area] + end;
23569 s->cmp_id = glyph->u.cmp.id;
23570 s->cmp_from = glyph->slice.cmp.from;
23571 s->cmp_to = glyph->slice.cmp.to + 1;
23572 s->face = FACE_FROM_ID (s->f, face_id);
23573 lgstring = composition_gstring_from_id (s->cmp_id);
23574 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23575 glyph++;
23576 while (glyph < last
23577 && glyph->u.cmp.automatic
23578 && glyph->u.cmp.id == s->cmp_id
23579 && s->cmp_to == glyph->slice.cmp.from)
23580 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23582 for (i = s->cmp_from; i < s->cmp_to; i++)
23584 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23585 unsigned code = LGLYPH_CODE (lglyph);
23587 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23589 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23590 return glyph - s->row->glyphs[s->area];
23594 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23595 See the comment of fill_glyph_string for arguments.
23596 Value is the index of the first glyph not in S. */
23599 static int
23600 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23601 int start, int end, int overlaps)
23603 struct glyph *glyph, *last;
23604 int voffset;
23606 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23607 s->for_overlaps = overlaps;
23608 glyph = s->row->glyphs[s->area] + start;
23609 last = s->row->glyphs[s->area] + end;
23610 voffset = glyph->voffset;
23611 s->face = FACE_FROM_ID (s->f, face_id);
23612 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23613 s->nchars = 1;
23614 s->width = glyph->pixel_width;
23615 glyph++;
23616 while (glyph < last
23617 && glyph->type == GLYPHLESS_GLYPH
23618 && glyph->voffset == voffset
23619 && glyph->face_id == face_id)
23621 s->nchars++;
23622 s->width += glyph->pixel_width;
23623 glyph++;
23625 s->ybase += voffset;
23626 return glyph - s->row->glyphs[s->area];
23630 /* Fill glyph string S from a sequence of character glyphs.
23632 FACE_ID is the face id of the string. START is the index of the
23633 first glyph to consider, END is the index of the last + 1.
23634 OVERLAPS non-zero means S should draw the foreground only, and use
23635 its physical height for clipping. See also draw_glyphs.
23637 Value is the index of the first glyph not in S. */
23639 static int
23640 fill_glyph_string (struct glyph_string *s, int face_id,
23641 int start, int end, int overlaps)
23643 struct glyph *glyph, *last;
23644 int voffset;
23645 int glyph_not_available_p;
23647 eassert (s->f == XFRAME (s->w->frame));
23648 eassert (s->nchars == 0);
23649 eassert (start >= 0 && end > start);
23651 s->for_overlaps = overlaps;
23652 glyph = s->row->glyphs[s->area] + start;
23653 last = s->row->glyphs[s->area] + end;
23654 voffset = glyph->voffset;
23655 s->padding_p = glyph->padding_p;
23656 glyph_not_available_p = glyph->glyph_not_available_p;
23658 while (glyph < last
23659 && glyph->type == CHAR_GLYPH
23660 && glyph->voffset == voffset
23661 /* Same face id implies same font, nowadays. */
23662 && glyph->face_id == face_id
23663 && glyph->glyph_not_available_p == glyph_not_available_p)
23665 int two_byte_p;
23667 s->face = get_glyph_face_and_encoding (s->f, glyph,
23668 s->char2b + s->nchars,
23669 &two_byte_p);
23670 s->two_byte_p = two_byte_p;
23671 ++s->nchars;
23672 eassert (s->nchars <= end - start);
23673 s->width += glyph->pixel_width;
23674 if (glyph++->padding_p != s->padding_p)
23675 break;
23678 s->font = s->face->font;
23680 /* If the specified font could not be loaded, use the frame's font,
23681 but record the fact that we couldn't load it in
23682 S->font_not_found_p so that we can draw rectangles for the
23683 characters of the glyph string. */
23684 if (s->font == NULL || glyph_not_available_p)
23686 s->font_not_found_p = 1;
23687 s->font = FRAME_FONT (s->f);
23690 /* Adjust base line for subscript/superscript text. */
23691 s->ybase += voffset;
23693 eassert (s->face && s->face->gc);
23694 return glyph - s->row->glyphs[s->area];
23698 /* Fill glyph string S from image glyph S->first_glyph. */
23700 static void
23701 fill_image_glyph_string (struct glyph_string *s)
23703 eassert (s->first_glyph->type == IMAGE_GLYPH);
23704 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23705 eassert (s->img);
23706 s->slice = s->first_glyph->slice.img;
23707 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23708 s->font = s->face->font;
23709 s->width = s->first_glyph->pixel_width;
23711 /* Adjust base line for subscript/superscript text. */
23712 s->ybase += s->first_glyph->voffset;
23716 /* Fill glyph string S from a sequence of stretch glyphs.
23718 START is the index of the first glyph to consider,
23719 END is the index of the last + 1.
23721 Value is the index of the first glyph not in S. */
23723 static int
23724 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23726 struct glyph *glyph, *last;
23727 int voffset, face_id;
23729 eassert (s->first_glyph->type == STRETCH_GLYPH);
23731 glyph = s->row->glyphs[s->area] + start;
23732 last = s->row->glyphs[s->area] + end;
23733 face_id = glyph->face_id;
23734 s->face = FACE_FROM_ID (s->f, face_id);
23735 s->font = s->face->font;
23736 s->width = glyph->pixel_width;
23737 s->nchars = 1;
23738 voffset = glyph->voffset;
23740 for (++glyph;
23741 (glyph < last
23742 && glyph->type == STRETCH_GLYPH
23743 && glyph->voffset == voffset
23744 && glyph->face_id == face_id);
23745 ++glyph)
23746 s->width += glyph->pixel_width;
23748 /* Adjust base line for subscript/superscript text. */
23749 s->ybase += voffset;
23751 /* The case that face->gc == 0 is handled when drawing the glyph
23752 string by calling PREPARE_FACE_FOR_DISPLAY. */
23753 eassert (s->face);
23754 return glyph - s->row->glyphs[s->area];
23757 static struct font_metrics *
23758 get_per_char_metric (struct font *font, XChar2b *char2b)
23760 static struct font_metrics metrics;
23761 unsigned code;
23763 if (! font)
23764 return NULL;
23765 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23766 if (code == FONT_INVALID_CODE)
23767 return NULL;
23768 font->driver->text_extents (font, &code, 1, &metrics);
23769 return &metrics;
23772 /* EXPORT for RIF:
23773 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23774 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23775 assumed to be zero. */
23777 void
23778 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23780 *left = *right = 0;
23782 if (glyph->type == CHAR_GLYPH)
23784 struct face *face;
23785 XChar2b char2b;
23786 struct font_metrics *pcm;
23788 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23789 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23791 if (pcm->rbearing > pcm->width)
23792 *right = pcm->rbearing - pcm->width;
23793 if (pcm->lbearing < 0)
23794 *left = -pcm->lbearing;
23797 else if (glyph->type == COMPOSITE_GLYPH)
23799 if (! glyph->u.cmp.automatic)
23801 struct composition *cmp = composition_table[glyph->u.cmp.id];
23803 if (cmp->rbearing > cmp->pixel_width)
23804 *right = cmp->rbearing - cmp->pixel_width;
23805 if (cmp->lbearing < 0)
23806 *left = - cmp->lbearing;
23808 else
23810 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23811 struct font_metrics metrics;
23813 composition_gstring_width (gstring, glyph->slice.cmp.from,
23814 glyph->slice.cmp.to + 1, &metrics);
23815 if (metrics.rbearing > metrics.width)
23816 *right = metrics.rbearing - metrics.width;
23817 if (metrics.lbearing < 0)
23818 *left = - metrics.lbearing;
23824 /* Return the index of the first glyph preceding glyph string S that
23825 is overwritten by S because of S's left overhang. Value is -1
23826 if no glyphs are overwritten. */
23828 static int
23829 left_overwritten (struct glyph_string *s)
23831 int k;
23833 if (s->left_overhang)
23835 int x = 0, i;
23836 struct glyph *glyphs = s->row->glyphs[s->area];
23837 int first = s->first_glyph - glyphs;
23839 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23840 x -= glyphs[i].pixel_width;
23842 k = i + 1;
23844 else
23845 k = -1;
23847 return k;
23851 /* Return the index of the first glyph preceding glyph string S that
23852 is overwriting S because of its right overhang. Value is -1 if no
23853 glyph in front of S overwrites S. */
23855 static int
23856 left_overwriting (struct glyph_string *s)
23858 int i, k, x;
23859 struct glyph *glyphs = s->row->glyphs[s->area];
23860 int first = s->first_glyph - glyphs;
23862 k = -1;
23863 x = 0;
23864 for (i = first - 1; i >= 0; --i)
23866 int left, right;
23867 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23868 if (x + right > 0)
23869 k = i;
23870 x -= glyphs[i].pixel_width;
23873 return k;
23877 /* Return the index of the last glyph following glyph string S that is
23878 overwritten by S because of S's right overhang. Value is -1 if
23879 no such glyph is found. */
23881 static int
23882 right_overwritten (struct glyph_string *s)
23884 int k = -1;
23886 if (s->right_overhang)
23888 int x = 0, i;
23889 struct glyph *glyphs = s->row->glyphs[s->area];
23890 int first = (s->first_glyph - glyphs
23891 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23892 int end = s->row->used[s->area];
23894 for (i = first; i < end && s->right_overhang > x; ++i)
23895 x += glyphs[i].pixel_width;
23897 k = i;
23900 return k;
23904 /* Return the index of the last glyph following glyph string S that
23905 overwrites S because of its left overhang. Value is negative
23906 if no such glyph is found. */
23908 static int
23909 right_overwriting (struct glyph_string *s)
23911 int i, k, x;
23912 int end = s->row->used[s->area];
23913 struct glyph *glyphs = s->row->glyphs[s->area];
23914 int first = (s->first_glyph - glyphs
23915 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23917 k = -1;
23918 x = 0;
23919 for (i = first; i < end; ++i)
23921 int left, right;
23922 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23923 if (x - left < 0)
23924 k = i;
23925 x += glyphs[i].pixel_width;
23928 return k;
23932 /* Set background width of glyph string S. START is the index of the
23933 first glyph following S. LAST_X is the right-most x-position + 1
23934 in the drawing area. */
23936 static void
23937 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23939 /* If the face of this glyph string has to be drawn to the end of
23940 the drawing area, set S->extends_to_end_of_line_p. */
23942 if (start == s->row->used[s->area]
23943 && ((s->row->fill_line_p
23944 && (s->hl == DRAW_NORMAL_TEXT
23945 || s->hl == DRAW_IMAGE_RAISED
23946 || s->hl == DRAW_IMAGE_SUNKEN))
23947 || s->hl == DRAW_MOUSE_FACE))
23948 s->extends_to_end_of_line_p = 1;
23950 /* If S extends its face to the end of the line, set its
23951 background_width to the distance to the right edge of the drawing
23952 area. */
23953 if (s->extends_to_end_of_line_p)
23954 s->background_width = last_x - s->x + 1;
23955 else
23956 s->background_width = s->width;
23960 /* Compute overhangs and x-positions for glyph string S and its
23961 predecessors, or successors. X is the starting x-position for S.
23962 BACKWARD_P non-zero means process predecessors. */
23964 static void
23965 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23967 if (backward_p)
23969 while (s)
23971 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23972 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23973 x -= s->width;
23974 s->x = x;
23975 s = s->prev;
23978 else
23980 while (s)
23982 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23983 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23984 s->x = x;
23985 x += s->width;
23986 s = s->next;
23993 /* The following macros are only called from draw_glyphs below.
23994 They reference the following parameters of that function directly:
23995 `w', `row', `area', and `overlap_p'
23996 as well as the following local variables:
23997 `s', `f', and `hdc' (in W32) */
23999 #ifdef HAVE_NTGUI
24000 /* On W32, silently add local `hdc' variable to argument list of
24001 init_glyph_string. */
24002 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24003 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24004 #else
24005 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24006 init_glyph_string (s, char2b, w, row, area, start, hl)
24007 #endif
24009 /* Add a glyph string for a stretch glyph to the list of strings
24010 between HEAD and TAIL. START is the index of the stretch glyph in
24011 row area AREA of glyph row ROW. END is the index of the last glyph
24012 in that glyph row area. X is the current output position assigned
24013 to the new glyph string constructed. HL overrides that face of the
24014 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24015 is the right-most x-position of the drawing area. */
24017 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24018 and below -- keep them on one line. */
24019 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24020 do \
24022 s = alloca (sizeof *s); \
24023 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24024 START = fill_stretch_glyph_string (s, START, END); \
24025 append_glyph_string (&HEAD, &TAIL, s); \
24026 s->x = (X); \
24028 while (0)
24031 /* Add a glyph string for an image glyph to the list of strings
24032 between HEAD and TAIL. START is the index of the image glyph in
24033 row area AREA of glyph row ROW. END is the index of the last glyph
24034 in that glyph row area. X is the current output position assigned
24035 to the new glyph string constructed. HL overrides that face of the
24036 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24037 is the right-most x-position of the drawing area. */
24039 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24040 do \
24042 s = alloca (sizeof *s); \
24043 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24044 fill_image_glyph_string (s); \
24045 append_glyph_string (&HEAD, &TAIL, s); \
24046 ++START; \
24047 s->x = (X); \
24049 while (0)
24052 /* Add a glyph string for a sequence of character glyphs to the list
24053 of strings between HEAD and TAIL. START is the index of the first
24054 glyph in row area AREA of glyph row ROW that is part of the new
24055 glyph string. END is the index of the last glyph in that glyph row
24056 area. X is the current output position assigned to the new glyph
24057 string constructed. HL overrides that face of the glyph; e.g. it
24058 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24059 right-most x-position of the drawing area. */
24061 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24062 do \
24064 int face_id; \
24065 XChar2b *char2b; \
24067 face_id = (row)->glyphs[area][START].face_id; \
24069 s = alloca (sizeof *s); \
24070 char2b = alloca ((END - START) * sizeof *char2b); \
24071 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24072 append_glyph_string (&HEAD, &TAIL, s); \
24073 s->x = (X); \
24074 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24076 while (0)
24079 /* Add a glyph string for a composite sequence to the list of strings
24080 between HEAD and TAIL. START is the index of the first glyph in
24081 row area AREA of glyph row ROW that is part of the new glyph
24082 string. END is the index of the last glyph in that glyph row area.
24083 X is the current output position assigned to the new glyph string
24084 constructed. HL overrides that face of the glyph; e.g. it is
24085 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24086 x-position of the drawing area. */
24088 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24089 do { \
24090 int face_id = (row)->glyphs[area][START].face_id; \
24091 struct face *base_face = FACE_FROM_ID (f, face_id); \
24092 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24093 struct composition *cmp = composition_table[cmp_id]; \
24094 XChar2b *char2b; \
24095 struct glyph_string *first_s = NULL; \
24096 int n; \
24098 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24100 /* Make glyph_strings for each glyph sequence that is drawable by \
24101 the same face, and append them to HEAD/TAIL. */ \
24102 for (n = 0; n < cmp->glyph_len;) \
24104 s = alloca (sizeof *s); \
24105 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24106 append_glyph_string (&(HEAD), &(TAIL), s); \
24107 s->cmp = cmp; \
24108 s->cmp_from = n; \
24109 s->x = (X); \
24110 if (n == 0) \
24111 first_s = s; \
24112 n = fill_composite_glyph_string (s, base_face, overlaps); \
24115 ++START; \
24116 s = first_s; \
24117 } while (0)
24120 /* Add a glyph string for a glyph-string sequence to the list of strings
24121 between HEAD and TAIL. */
24123 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24124 do { \
24125 int face_id; \
24126 XChar2b *char2b; \
24127 Lisp_Object gstring; \
24129 face_id = (row)->glyphs[area][START].face_id; \
24130 gstring = (composition_gstring_from_id \
24131 ((row)->glyphs[area][START].u.cmp.id)); \
24132 s = alloca (sizeof *s); \
24133 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24134 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24135 append_glyph_string (&(HEAD), &(TAIL), s); \
24136 s->x = (X); \
24137 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24138 } while (0)
24141 /* Add a glyph string for a sequence of glyphless character's glyphs
24142 to the list of strings between HEAD and TAIL. The meanings of
24143 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24145 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24146 do \
24148 int face_id; \
24150 face_id = (row)->glyphs[area][START].face_id; \
24152 s = alloca (sizeof *s); \
24153 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24154 append_glyph_string (&HEAD, &TAIL, s); \
24155 s->x = (X); \
24156 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24157 overlaps); \
24159 while (0)
24162 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24163 of AREA of glyph row ROW on window W between indices START and END.
24164 HL overrides the face for drawing glyph strings, e.g. it is
24165 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24166 x-positions of the drawing area.
24168 This is an ugly monster macro construct because we must use alloca
24169 to allocate glyph strings (because draw_glyphs can be called
24170 asynchronously). */
24172 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24173 do \
24175 HEAD = TAIL = NULL; \
24176 while (START < END) \
24178 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24179 switch (first_glyph->type) \
24181 case CHAR_GLYPH: \
24182 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24183 HL, X, LAST_X); \
24184 break; \
24186 case COMPOSITE_GLYPH: \
24187 if (first_glyph->u.cmp.automatic) \
24188 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24189 HL, X, LAST_X); \
24190 else \
24191 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24192 HL, X, LAST_X); \
24193 break; \
24195 case STRETCH_GLYPH: \
24196 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24197 HL, X, LAST_X); \
24198 break; \
24200 case IMAGE_GLYPH: \
24201 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24202 HL, X, LAST_X); \
24203 break; \
24205 case GLYPHLESS_GLYPH: \
24206 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24207 HL, X, LAST_X); \
24208 break; \
24210 default: \
24211 emacs_abort (); \
24214 if (s) \
24216 set_glyph_string_background_width (s, START, LAST_X); \
24217 (X) += s->width; \
24220 } while (0)
24223 /* Draw glyphs between START and END in AREA of ROW on window W,
24224 starting at x-position X. X is relative to AREA in W. HL is a
24225 face-override with the following meaning:
24227 DRAW_NORMAL_TEXT draw normally
24228 DRAW_CURSOR draw in cursor face
24229 DRAW_MOUSE_FACE draw in mouse face.
24230 DRAW_INVERSE_VIDEO draw in mode line face
24231 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24232 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24234 If OVERLAPS is non-zero, draw only the foreground of characters and
24235 clip to the physical height of ROW. Non-zero value also defines
24236 the overlapping part to be drawn:
24238 OVERLAPS_PRED overlap with preceding rows
24239 OVERLAPS_SUCC overlap with succeeding rows
24240 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24241 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24243 Value is the x-position reached, relative to AREA of W. */
24245 static int
24246 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24247 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24248 enum draw_glyphs_face hl, int overlaps)
24250 struct glyph_string *head, *tail;
24251 struct glyph_string *s;
24252 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24253 int i, j, x_reached, last_x, area_left = 0;
24254 struct frame *f = XFRAME (WINDOW_FRAME (w));
24255 DECLARE_HDC (hdc);
24257 ALLOCATE_HDC (hdc, f);
24259 /* Let's rather be paranoid than getting a SEGV. */
24260 end = min (end, row->used[area]);
24261 start = clip_to_bounds (0, start, end);
24263 /* Translate X to frame coordinates. Set last_x to the right
24264 end of the drawing area. */
24265 if (row->full_width_p)
24267 /* X is relative to the left edge of W, without scroll bars
24268 or fringes. */
24269 area_left = WINDOW_LEFT_EDGE_X (w);
24270 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24271 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24273 else
24275 area_left = window_box_left (w, area);
24276 last_x = area_left + window_box_width (w, area);
24278 x += area_left;
24280 /* Build a doubly-linked list of glyph_string structures between
24281 head and tail from what we have to draw. Note that the macro
24282 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24283 the reason we use a separate variable `i'. */
24284 i = start;
24285 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24286 if (tail)
24287 x_reached = tail->x + tail->background_width;
24288 else
24289 x_reached = x;
24291 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24292 the row, redraw some glyphs in front or following the glyph
24293 strings built above. */
24294 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24296 struct glyph_string *h, *t;
24297 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24298 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24299 int check_mouse_face = 0;
24300 int dummy_x = 0;
24302 /* If mouse highlighting is on, we may need to draw adjacent
24303 glyphs using mouse-face highlighting. */
24304 if (area == TEXT_AREA && row->mouse_face_p
24305 && hlinfo->mouse_face_beg_row >= 0
24306 && hlinfo->mouse_face_end_row >= 0)
24308 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24310 if (row_vpos >= hlinfo->mouse_face_beg_row
24311 && row_vpos <= hlinfo->mouse_face_end_row)
24313 check_mouse_face = 1;
24314 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24315 ? hlinfo->mouse_face_beg_col : 0;
24316 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24317 ? hlinfo->mouse_face_end_col
24318 : row->used[TEXT_AREA];
24322 /* Compute overhangs for all glyph strings. */
24323 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24324 for (s = head; s; s = s->next)
24325 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24327 /* Prepend glyph strings for glyphs in front of the first glyph
24328 string that are overwritten because of the first glyph
24329 string's left overhang. The background of all strings
24330 prepended must be drawn because the first glyph string
24331 draws over it. */
24332 i = left_overwritten (head);
24333 if (i >= 0)
24335 enum draw_glyphs_face overlap_hl;
24337 /* If this row contains mouse highlighting, attempt to draw
24338 the overlapped glyphs with the correct highlight. This
24339 code fails if the overlap encompasses more than one glyph
24340 and mouse-highlight spans only some of these glyphs.
24341 However, making it work perfectly involves a lot more
24342 code, and I don't know if the pathological case occurs in
24343 practice, so we'll stick to this for now. --- cyd */
24344 if (check_mouse_face
24345 && mouse_beg_col < start && mouse_end_col > i)
24346 overlap_hl = DRAW_MOUSE_FACE;
24347 else
24348 overlap_hl = DRAW_NORMAL_TEXT;
24350 j = i;
24351 BUILD_GLYPH_STRINGS (j, start, h, t,
24352 overlap_hl, dummy_x, last_x);
24353 start = i;
24354 compute_overhangs_and_x (t, head->x, 1);
24355 prepend_glyph_string_lists (&head, &tail, h, t);
24356 clip_head = head;
24359 /* Prepend glyph strings for glyphs in front of the first glyph
24360 string that overwrite that glyph string because of their
24361 right overhang. For these strings, only the foreground must
24362 be drawn, because it draws over the glyph string at `head'.
24363 The background must not be drawn because this would overwrite
24364 right overhangs of preceding glyphs for which no glyph
24365 strings exist. */
24366 i = left_overwriting (head);
24367 if (i >= 0)
24369 enum draw_glyphs_face overlap_hl;
24371 if (check_mouse_face
24372 && mouse_beg_col < start && mouse_end_col > i)
24373 overlap_hl = DRAW_MOUSE_FACE;
24374 else
24375 overlap_hl = DRAW_NORMAL_TEXT;
24377 clip_head = head;
24378 BUILD_GLYPH_STRINGS (i, start, h, t,
24379 overlap_hl, dummy_x, last_x);
24380 for (s = h; s; s = s->next)
24381 s->background_filled_p = 1;
24382 compute_overhangs_and_x (t, head->x, 1);
24383 prepend_glyph_string_lists (&head, &tail, h, t);
24386 /* Append glyphs strings for glyphs following the last glyph
24387 string tail that are overwritten by tail. The background of
24388 these strings has to be drawn because tail's foreground draws
24389 over it. */
24390 i = right_overwritten (tail);
24391 if (i >= 0)
24393 enum draw_glyphs_face overlap_hl;
24395 if (check_mouse_face
24396 && mouse_beg_col < i && mouse_end_col > end)
24397 overlap_hl = DRAW_MOUSE_FACE;
24398 else
24399 overlap_hl = DRAW_NORMAL_TEXT;
24401 BUILD_GLYPH_STRINGS (end, i, h, t,
24402 overlap_hl, x, last_x);
24403 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24404 we don't have `end = i;' here. */
24405 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24406 append_glyph_string_lists (&head, &tail, h, t);
24407 clip_tail = tail;
24410 /* Append glyph strings for glyphs following the last glyph
24411 string tail that overwrite tail. The foreground of such
24412 glyphs has to be drawn because it writes into the background
24413 of tail. The background must not be drawn because it could
24414 paint over the foreground of following glyphs. */
24415 i = right_overwriting (tail);
24416 if (i >= 0)
24418 enum draw_glyphs_face overlap_hl;
24419 if (check_mouse_face
24420 && mouse_beg_col < i && mouse_end_col > end)
24421 overlap_hl = DRAW_MOUSE_FACE;
24422 else
24423 overlap_hl = DRAW_NORMAL_TEXT;
24425 clip_tail = tail;
24426 i++; /* We must include the Ith glyph. */
24427 BUILD_GLYPH_STRINGS (end, i, h, t,
24428 overlap_hl, x, last_x);
24429 for (s = h; s; s = s->next)
24430 s->background_filled_p = 1;
24431 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24432 append_glyph_string_lists (&head, &tail, h, t);
24434 if (clip_head || clip_tail)
24435 for (s = head; s; s = s->next)
24437 s->clip_head = clip_head;
24438 s->clip_tail = clip_tail;
24442 /* Draw all strings. */
24443 for (s = head; s; s = s->next)
24444 FRAME_RIF (f)->draw_glyph_string (s);
24446 #ifndef HAVE_NS
24447 /* When focus a sole frame and move horizontally, this sets on_p to 0
24448 causing a failure to erase prev cursor position. */
24449 if (area == TEXT_AREA
24450 && !row->full_width_p
24451 /* When drawing overlapping rows, only the glyph strings'
24452 foreground is drawn, which doesn't erase a cursor
24453 completely. */
24454 && !overlaps)
24456 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24457 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24458 : (tail ? tail->x + tail->background_width : x));
24459 x0 -= area_left;
24460 x1 -= area_left;
24462 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24463 row->y, MATRIX_ROW_BOTTOM_Y (row));
24465 #endif
24467 /* Value is the x-position up to which drawn, relative to AREA of W.
24468 This doesn't include parts drawn because of overhangs. */
24469 if (row->full_width_p)
24470 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24471 else
24472 x_reached -= area_left;
24474 RELEASE_HDC (hdc, f);
24476 return x_reached;
24479 /* Expand row matrix if too narrow. Don't expand if area
24480 is not present. */
24482 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24484 if (!it->f->fonts_changed \
24485 && (it->glyph_row->glyphs[area] \
24486 < it->glyph_row->glyphs[area + 1])) \
24488 it->w->ncols_scale_factor++; \
24489 it->f->fonts_changed = 1; \
24493 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24494 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24496 static void
24497 append_glyph (struct it *it)
24499 struct glyph *glyph;
24500 enum glyph_row_area area = it->area;
24502 eassert (it->glyph_row);
24503 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24505 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24506 if (glyph < it->glyph_row->glyphs[area + 1])
24508 /* If the glyph row is reversed, we need to prepend the glyph
24509 rather than append it. */
24510 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24512 struct glyph *g;
24514 /* Make room for the additional glyph. */
24515 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24516 g[1] = *g;
24517 glyph = it->glyph_row->glyphs[area];
24519 glyph->charpos = CHARPOS (it->position);
24520 glyph->object = it->object;
24521 if (it->pixel_width > 0)
24523 glyph->pixel_width = it->pixel_width;
24524 glyph->padding_p = 0;
24526 else
24528 /* Assure at least 1-pixel width. Otherwise, cursor can't
24529 be displayed correctly. */
24530 glyph->pixel_width = 1;
24531 glyph->padding_p = 1;
24533 glyph->ascent = it->ascent;
24534 glyph->descent = it->descent;
24535 glyph->voffset = it->voffset;
24536 glyph->type = CHAR_GLYPH;
24537 glyph->avoid_cursor_p = it->avoid_cursor_p;
24538 glyph->multibyte_p = it->multibyte_p;
24539 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24541 /* In R2L rows, the left and the right box edges need to be
24542 drawn in reverse direction. */
24543 glyph->right_box_line_p = it->start_of_box_run_p;
24544 glyph->left_box_line_p = it->end_of_box_run_p;
24546 else
24548 glyph->left_box_line_p = it->start_of_box_run_p;
24549 glyph->right_box_line_p = it->end_of_box_run_p;
24551 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24552 || it->phys_descent > it->descent);
24553 glyph->glyph_not_available_p = it->glyph_not_available_p;
24554 glyph->face_id = it->face_id;
24555 glyph->u.ch = it->char_to_display;
24556 glyph->slice.img = null_glyph_slice;
24557 glyph->font_type = FONT_TYPE_UNKNOWN;
24558 if (it->bidi_p)
24560 glyph->resolved_level = it->bidi_it.resolved_level;
24561 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24562 emacs_abort ();
24563 glyph->bidi_type = it->bidi_it.type;
24565 else
24567 glyph->resolved_level = 0;
24568 glyph->bidi_type = UNKNOWN_BT;
24570 ++it->glyph_row->used[area];
24572 else
24573 IT_EXPAND_MATRIX_WIDTH (it, area);
24576 /* Store one glyph for the composition IT->cmp_it.id in
24577 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24578 non-null. */
24580 static void
24581 append_composite_glyph (struct it *it)
24583 struct glyph *glyph;
24584 enum glyph_row_area area = it->area;
24586 eassert (it->glyph_row);
24588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24589 if (glyph < it->glyph_row->glyphs[area + 1])
24591 /* If the glyph row is reversed, we need to prepend the glyph
24592 rather than append it. */
24593 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24595 struct glyph *g;
24597 /* Make room for the new glyph. */
24598 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24599 g[1] = *g;
24600 glyph = it->glyph_row->glyphs[it->area];
24602 glyph->charpos = it->cmp_it.charpos;
24603 glyph->object = it->object;
24604 glyph->pixel_width = it->pixel_width;
24605 glyph->ascent = it->ascent;
24606 glyph->descent = it->descent;
24607 glyph->voffset = it->voffset;
24608 glyph->type = COMPOSITE_GLYPH;
24609 if (it->cmp_it.ch < 0)
24611 glyph->u.cmp.automatic = 0;
24612 glyph->u.cmp.id = it->cmp_it.id;
24613 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24615 else
24617 glyph->u.cmp.automatic = 1;
24618 glyph->u.cmp.id = it->cmp_it.id;
24619 glyph->slice.cmp.from = it->cmp_it.from;
24620 glyph->slice.cmp.to = it->cmp_it.to - 1;
24622 glyph->avoid_cursor_p = it->avoid_cursor_p;
24623 glyph->multibyte_p = it->multibyte_p;
24624 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24626 /* In R2L rows, the left and the right box edges need to be
24627 drawn in reverse direction. */
24628 glyph->right_box_line_p = it->start_of_box_run_p;
24629 glyph->left_box_line_p = it->end_of_box_run_p;
24631 else
24633 glyph->left_box_line_p = it->start_of_box_run_p;
24634 glyph->right_box_line_p = it->end_of_box_run_p;
24636 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24637 || it->phys_descent > it->descent);
24638 glyph->padding_p = 0;
24639 glyph->glyph_not_available_p = 0;
24640 glyph->face_id = it->face_id;
24641 glyph->font_type = FONT_TYPE_UNKNOWN;
24642 if (it->bidi_p)
24644 glyph->resolved_level = it->bidi_it.resolved_level;
24645 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24646 emacs_abort ();
24647 glyph->bidi_type = it->bidi_it.type;
24649 ++it->glyph_row->used[area];
24651 else
24652 IT_EXPAND_MATRIX_WIDTH (it, area);
24656 /* Change IT->ascent and IT->height according to the setting of
24657 IT->voffset. */
24659 static void
24660 take_vertical_position_into_account (struct it *it)
24662 if (it->voffset)
24664 if (it->voffset < 0)
24665 /* Increase the ascent so that we can display the text higher
24666 in the line. */
24667 it->ascent -= it->voffset;
24668 else
24669 /* Increase the descent so that we can display the text lower
24670 in the line. */
24671 it->descent += it->voffset;
24676 /* Produce glyphs/get display metrics for the image IT is loaded with.
24677 See the description of struct display_iterator in dispextern.h for
24678 an overview of struct display_iterator. */
24680 static void
24681 produce_image_glyph (struct it *it)
24683 struct image *img;
24684 struct face *face;
24685 int glyph_ascent, crop;
24686 struct glyph_slice slice;
24688 eassert (it->what == IT_IMAGE);
24690 face = FACE_FROM_ID (it->f, it->face_id);
24691 eassert (face);
24692 /* Make sure X resources of the face is loaded. */
24693 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24695 if (it->image_id < 0)
24697 /* Fringe bitmap. */
24698 it->ascent = it->phys_ascent = 0;
24699 it->descent = it->phys_descent = 0;
24700 it->pixel_width = 0;
24701 it->nglyphs = 0;
24702 return;
24705 img = IMAGE_FROM_ID (it->f, it->image_id);
24706 eassert (img);
24707 /* Make sure X resources of the image is loaded. */
24708 prepare_image_for_display (it->f, img);
24710 slice.x = slice.y = 0;
24711 slice.width = img->width;
24712 slice.height = img->height;
24714 if (INTEGERP (it->slice.x))
24715 slice.x = XINT (it->slice.x);
24716 else if (FLOATP (it->slice.x))
24717 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24719 if (INTEGERP (it->slice.y))
24720 slice.y = XINT (it->slice.y);
24721 else if (FLOATP (it->slice.y))
24722 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24724 if (INTEGERP (it->slice.width))
24725 slice.width = XINT (it->slice.width);
24726 else if (FLOATP (it->slice.width))
24727 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24729 if (INTEGERP (it->slice.height))
24730 slice.height = XINT (it->slice.height);
24731 else if (FLOATP (it->slice.height))
24732 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24734 if (slice.x >= img->width)
24735 slice.x = img->width;
24736 if (slice.y >= img->height)
24737 slice.y = img->height;
24738 if (slice.x + slice.width >= img->width)
24739 slice.width = img->width - slice.x;
24740 if (slice.y + slice.height > img->height)
24741 slice.height = img->height - slice.y;
24743 if (slice.width == 0 || slice.height == 0)
24744 return;
24746 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24748 it->descent = slice.height - glyph_ascent;
24749 if (slice.y == 0)
24750 it->descent += img->vmargin;
24751 if (slice.y + slice.height == img->height)
24752 it->descent += img->vmargin;
24753 it->phys_descent = it->descent;
24755 it->pixel_width = slice.width;
24756 if (slice.x == 0)
24757 it->pixel_width += img->hmargin;
24758 if (slice.x + slice.width == img->width)
24759 it->pixel_width += img->hmargin;
24761 /* It's quite possible for images to have an ascent greater than
24762 their height, so don't get confused in that case. */
24763 if (it->descent < 0)
24764 it->descent = 0;
24766 it->nglyphs = 1;
24768 if (face->box != FACE_NO_BOX)
24770 if (face->box_line_width > 0)
24772 if (slice.y == 0)
24773 it->ascent += face->box_line_width;
24774 if (slice.y + slice.height == img->height)
24775 it->descent += face->box_line_width;
24778 if (it->start_of_box_run_p && slice.x == 0)
24779 it->pixel_width += eabs (face->box_line_width);
24780 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24781 it->pixel_width += eabs (face->box_line_width);
24784 take_vertical_position_into_account (it);
24786 /* Automatically crop wide image glyphs at right edge so we can
24787 draw the cursor on same display row. */
24788 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24789 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24791 it->pixel_width -= crop;
24792 slice.width -= crop;
24795 if (it->glyph_row)
24797 struct glyph *glyph;
24798 enum glyph_row_area area = it->area;
24800 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24801 if (glyph < it->glyph_row->glyphs[area + 1])
24803 glyph->charpos = CHARPOS (it->position);
24804 glyph->object = it->object;
24805 glyph->pixel_width = it->pixel_width;
24806 glyph->ascent = glyph_ascent;
24807 glyph->descent = it->descent;
24808 glyph->voffset = it->voffset;
24809 glyph->type = IMAGE_GLYPH;
24810 glyph->avoid_cursor_p = it->avoid_cursor_p;
24811 glyph->multibyte_p = it->multibyte_p;
24812 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24814 /* In R2L rows, the left and the right box edges need to be
24815 drawn in reverse direction. */
24816 glyph->right_box_line_p = it->start_of_box_run_p;
24817 glyph->left_box_line_p = it->end_of_box_run_p;
24819 else
24821 glyph->left_box_line_p = it->start_of_box_run_p;
24822 glyph->right_box_line_p = it->end_of_box_run_p;
24824 glyph->overlaps_vertically_p = 0;
24825 glyph->padding_p = 0;
24826 glyph->glyph_not_available_p = 0;
24827 glyph->face_id = it->face_id;
24828 glyph->u.img_id = img->id;
24829 glyph->slice.img = slice;
24830 glyph->font_type = FONT_TYPE_UNKNOWN;
24831 if (it->bidi_p)
24833 glyph->resolved_level = it->bidi_it.resolved_level;
24834 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24835 emacs_abort ();
24836 glyph->bidi_type = it->bidi_it.type;
24838 ++it->glyph_row->used[area];
24840 else
24841 IT_EXPAND_MATRIX_WIDTH (it, area);
24846 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24847 of the glyph, WIDTH and HEIGHT are the width and height of the
24848 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24850 static void
24851 append_stretch_glyph (struct it *it, Lisp_Object object,
24852 int width, int height, int ascent)
24854 struct glyph *glyph;
24855 enum glyph_row_area area = it->area;
24857 eassert (ascent >= 0 && ascent <= height);
24859 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24860 if (glyph < it->glyph_row->glyphs[area + 1])
24862 /* If the glyph row is reversed, we need to prepend the glyph
24863 rather than append it. */
24864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24866 struct glyph *g;
24868 /* Make room for the additional glyph. */
24869 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24870 g[1] = *g;
24871 glyph = it->glyph_row->glyphs[area];
24873 glyph->charpos = CHARPOS (it->position);
24874 glyph->object = object;
24875 glyph->pixel_width = width;
24876 glyph->ascent = ascent;
24877 glyph->descent = height - ascent;
24878 glyph->voffset = it->voffset;
24879 glyph->type = STRETCH_GLYPH;
24880 glyph->avoid_cursor_p = it->avoid_cursor_p;
24881 glyph->multibyte_p = it->multibyte_p;
24882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24884 /* In R2L rows, the left and the right box edges need to be
24885 drawn in reverse direction. */
24886 glyph->right_box_line_p = it->start_of_box_run_p;
24887 glyph->left_box_line_p = it->end_of_box_run_p;
24889 else
24891 glyph->left_box_line_p = it->start_of_box_run_p;
24892 glyph->right_box_line_p = it->end_of_box_run_p;
24894 glyph->overlaps_vertically_p = 0;
24895 glyph->padding_p = 0;
24896 glyph->glyph_not_available_p = 0;
24897 glyph->face_id = it->face_id;
24898 glyph->u.stretch.ascent = ascent;
24899 glyph->u.stretch.height = height;
24900 glyph->slice.img = null_glyph_slice;
24901 glyph->font_type = FONT_TYPE_UNKNOWN;
24902 if (it->bidi_p)
24904 glyph->resolved_level = it->bidi_it.resolved_level;
24905 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24906 emacs_abort ();
24907 glyph->bidi_type = it->bidi_it.type;
24909 else
24911 glyph->resolved_level = 0;
24912 glyph->bidi_type = UNKNOWN_BT;
24914 ++it->glyph_row->used[area];
24916 else
24917 IT_EXPAND_MATRIX_WIDTH (it, area);
24920 #endif /* HAVE_WINDOW_SYSTEM */
24922 /* Produce a stretch glyph for iterator IT. IT->object is the value
24923 of the glyph property displayed. The value must be a list
24924 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24925 being recognized:
24927 1. `:width WIDTH' specifies that the space should be WIDTH *
24928 canonical char width wide. WIDTH may be an integer or floating
24929 point number.
24931 2. `:relative-width FACTOR' specifies that the width of the stretch
24932 should be computed from the width of the first character having the
24933 `glyph' property, and should be FACTOR times that width.
24935 3. `:align-to HPOS' specifies that the space should be wide enough
24936 to reach HPOS, a value in canonical character units.
24938 Exactly one of the above pairs must be present.
24940 4. `:height HEIGHT' specifies that the height of the stretch produced
24941 should be HEIGHT, measured in canonical character units.
24943 5. `:relative-height FACTOR' specifies that the height of the
24944 stretch should be FACTOR times the height of the characters having
24945 the glyph property.
24947 Either none or exactly one of 4 or 5 must be present.
24949 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24950 of the stretch should be used for the ascent of the stretch.
24951 ASCENT must be in the range 0 <= ASCENT <= 100. */
24953 void
24954 produce_stretch_glyph (struct it *it)
24956 /* (space :width WIDTH :height HEIGHT ...) */
24957 Lisp_Object prop, plist;
24958 int width = 0, height = 0, align_to = -1;
24959 int zero_width_ok_p = 0;
24960 double tem;
24961 struct font *font = NULL;
24963 #ifdef HAVE_WINDOW_SYSTEM
24964 int ascent = 0;
24965 int zero_height_ok_p = 0;
24967 if (FRAME_WINDOW_P (it->f))
24969 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24970 font = face->font ? face->font : FRAME_FONT (it->f);
24971 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24973 #endif
24975 /* List should start with `space'. */
24976 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24977 plist = XCDR (it->object);
24979 /* Compute the width of the stretch. */
24980 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24981 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24983 /* Absolute width `:width WIDTH' specified and valid. */
24984 zero_width_ok_p = 1;
24985 width = (int)tem;
24987 #ifdef HAVE_WINDOW_SYSTEM
24988 else if (FRAME_WINDOW_P (it->f)
24989 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24991 /* Relative width `:relative-width FACTOR' specified and valid.
24992 Compute the width of the characters having the `glyph'
24993 property. */
24994 struct it it2;
24995 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24997 it2 = *it;
24998 if (it->multibyte_p)
24999 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25000 else
25002 it2.c = it2.char_to_display = *p, it2.len = 1;
25003 if (! ASCII_CHAR_P (it2.c))
25004 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25007 it2.glyph_row = NULL;
25008 it2.what = IT_CHARACTER;
25009 x_produce_glyphs (&it2);
25010 width = NUMVAL (prop) * it2.pixel_width;
25012 #endif /* HAVE_WINDOW_SYSTEM */
25013 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25014 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25016 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25017 align_to = (align_to < 0
25019 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25020 else if (align_to < 0)
25021 align_to = window_box_left_offset (it->w, TEXT_AREA);
25022 width = max (0, (int)tem + align_to - it->current_x);
25023 zero_width_ok_p = 1;
25025 else
25026 /* Nothing specified -> width defaults to canonical char width. */
25027 width = FRAME_COLUMN_WIDTH (it->f);
25029 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25030 width = 1;
25032 #ifdef HAVE_WINDOW_SYSTEM
25033 /* Compute height. */
25034 if (FRAME_WINDOW_P (it->f))
25036 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25037 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25039 height = (int)tem;
25040 zero_height_ok_p = 1;
25042 else if (prop = Fplist_get (plist, QCrelative_height),
25043 NUMVAL (prop) > 0)
25044 height = FONT_HEIGHT (font) * NUMVAL (prop);
25045 else
25046 height = FONT_HEIGHT (font);
25048 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25049 height = 1;
25051 /* Compute percentage of height used for ascent. If
25052 `:ascent ASCENT' is present and valid, use that. Otherwise,
25053 derive the ascent from the font in use. */
25054 if (prop = Fplist_get (plist, QCascent),
25055 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25056 ascent = height * NUMVAL (prop) / 100.0;
25057 else if (!NILP (prop)
25058 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25059 ascent = min (max (0, (int)tem), height);
25060 else
25061 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25063 else
25064 #endif /* HAVE_WINDOW_SYSTEM */
25065 height = 1;
25067 if (width > 0 && it->line_wrap != TRUNCATE
25068 && it->current_x + width > it->last_visible_x)
25070 width = it->last_visible_x - it->current_x;
25071 #ifdef HAVE_WINDOW_SYSTEM
25072 /* Subtract one more pixel from the stretch width, but only on
25073 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25074 width -= FRAME_WINDOW_P (it->f);
25075 #endif
25078 if (width > 0 && height > 0 && it->glyph_row)
25080 Lisp_Object o_object = it->object;
25081 Lisp_Object object = it->stack[it->sp - 1].string;
25082 int n = width;
25084 if (!STRINGP (object))
25085 object = it->w->contents;
25086 #ifdef HAVE_WINDOW_SYSTEM
25087 if (FRAME_WINDOW_P (it->f))
25088 append_stretch_glyph (it, object, width, height, ascent);
25089 else
25090 #endif
25092 it->object = object;
25093 it->char_to_display = ' ';
25094 it->pixel_width = it->len = 1;
25095 while (n--)
25096 tty_append_glyph (it);
25097 it->object = o_object;
25101 it->pixel_width = width;
25102 #ifdef HAVE_WINDOW_SYSTEM
25103 if (FRAME_WINDOW_P (it->f))
25105 it->ascent = it->phys_ascent = ascent;
25106 it->descent = it->phys_descent = height - it->ascent;
25107 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25108 take_vertical_position_into_account (it);
25110 else
25111 #endif
25112 it->nglyphs = width;
25115 /* Get information about special display element WHAT in an
25116 environment described by IT. WHAT is one of IT_TRUNCATION or
25117 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25118 non-null glyph_row member. This function ensures that fields like
25119 face_id, c, len of IT are left untouched. */
25121 static void
25122 produce_special_glyphs (struct it *it, enum display_element_type what)
25124 struct it temp_it;
25125 Lisp_Object gc;
25126 GLYPH glyph;
25128 temp_it = *it;
25129 temp_it.object = make_number (0);
25130 memset (&temp_it.current, 0, sizeof temp_it.current);
25132 if (what == IT_CONTINUATION)
25134 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25135 if (it->bidi_it.paragraph_dir == R2L)
25136 SET_GLYPH_FROM_CHAR (glyph, '/');
25137 else
25138 SET_GLYPH_FROM_CHAR (glyph, '\\');
25139 if (it->dp
25140 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25142 /* FIXME: Should we mirror GC for R2L lines? */
25143 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25144 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25147 else if (what == IT_TRUNCATION)
25149 /* Truncation glyph. */
25150 SET_GLYPH_FROM_CHAR (glyph, '$');
25151 if (it->dp
25152 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25154 /* FIXME: Should we mirror GC for R2L lines? */
25155 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25156 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25159 else
25160 emacs_abort ();
25162 #ifdef HAVE_WINDOW_SYSTEM
25163 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25164 is turned off, we precede the truncation/continuation glyphs by a
25165 stretch glyph whose width is computed such that these special
25166 glyphs are aligned at the window margin, even when very different
25167 fonts are used in different glyph rows. */
25168 if (FRAME_WINDOW_P (temp_it.f)
25169 /* init_iterator calls this with it->glyph_row == NULL, and it
25170 wants only the pixel width of the truncation/continuation
25171 glyphs. */
25172 && temp_it.glyph_row
25173 /* insert_left_trunc_glyphs calls us at the beginning of the
25174 row, and it has its own calculation of the stretch glyph
25175 width. */
25176 && temp_it.glyph_row->used[TEXT_AREA] > 0
25177 && (temp_it.glyph_row->reversed_p
25178 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25179 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25181 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25183 if (stretch_width > 0)
25185 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25186 struct font *font =
25187 face->font ? face->font : FRAME_FONT (temp_it.f);
25188 int stretch_ascent =
25189 (((temp_it.ascent + temp_it.descent)
25190 * FONT_BASE (font)) / FONT_HEIGHT (font));
25192 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25193 temp_it.ascent + temp_it.descent,
25194 stretch_ascent);
25197 #endif
25199 temp_it.dp = NULL;
25200 temp_it.what = IT_CHARACTER;
25201 temp_it.len = 1;
25202 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25203 temp_it.face_id = GLYPH_FACE (glyph);
25204 temp_it.len = CHAR_BYTES (temp_it.c);
25206 PRODUCE_GLYPHS (&temp_it);
25207 it->pixel_width = temp_it.pixel_width;
25208 it->nglyphs = temp_it.pixel_width;
25211 #ifdef HAVE_WINDOW_SYSTEM
25213 /* Calculate line-height and line-spacing properties.
25214 An integer value specifies explicit pixel value.
25215 A float value specifies relative value to current face height.
25216 A cons (float . face-name) specifies relative value to
25217 height of specified face font.
25219 Returns height in pixels, or nil. */
25222 static Lisp_Object
25223 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25224 int boff, int override)
25226 Lisp_Object face_name = Qnil;
25227 int ascent, descent, height;
25229 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25230 return val;
25232 if (CONSP (val))
25234 face_name = XCAR (val);
25235 val = XCDR (val);
25236 if (!NUMBERP (val))
25237 val = make_number (1);
25238 if (NILP (face_name))
25240 height = it->ascent + it->descent;
25241 goto scale;
25245 if (NILP (face_name))
25247 font = FRAME_FONT (it->f);
25248 boff = FRAME_BASELINE_OFFSET (it->f);
25250 else if (EQ (face_name, Qt))
25252 override = 0;
25254 else
25256 int face_id;
25257 struct face *face;
25259 face_id = lookup_named_face (it->f, face_name, 0);
25260 if (face_id < 0)
25261 return make_number (-1);
25263 face = FACE_FROM_ID (it->f, face_id);
25264 font = face->font;
25265 if (font == NULL)
25266 return make_number (-1);
25267 boff = font->baseline_offset;
25268 if (font->vertical_centering)
25269 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25272 ascent = FONT_BASE (font) + boff;
25273 descent = FONT_DESCENT (font) - boff;
25275 if (override)
25277 it->override_ascent = ascent;
25278 it->override_descent = descent;
25279 it->override_boff = boff;
25282 height = ascent + descent;
25284 scale:
25285 if (FLOATP (val))
25286 height = (int)(XFLOAT_DATA (val) * height);
25287 else if (INTEGERP (val))
25288 height *= XINT (val);
25290 return make_number (height);
25294 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25295 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25296 and only if this is for a character for which no font was found.
25298 If the display method (it->glyphless_method) is
25299 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25300 length of the acronym or the hexadecimal string, UPPER_XOFF and
25301 UPPER_YOFF are pixel offsets for the upper part of the string,
25302 LOWER_XOFF and LOWER_YOFF are for the lower part.
25304 For the other display methods, LEN through LOWER_YOFF are zero. */
25306 static void
25307 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25308 short upper_xoff, short upper_yoff,
25309 short lower_xoff, short lower_yoff)
25311 struct glyph *glyph;
25312 enum glyph_row_area area = it->area;
25314 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25315 if (glyph < it->glyph_row->glyphs[area + 1])
25317 /* If the glyph row is reversed, we need to prepend the glyph
25318 rather than append it. */
25319 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25321 struct glyph *g;
25323 /* Make room for the additional glyph. */
25324 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25325 g[1] = *g;
25326 glyph = it->glyph_row->glyphs[area];
25328 glyph->charpos = CHARPOS (it->position);
25329 glyph->object = it->object;
25330 glyph->pixel_width = it->pixel_width;
25331 glyph->ascent = it->ascent;
25332 glyph->descent = it->descent;
25333 glyph->voffset = it->voffset;
25334 glyph->type = GLYPHLESS_GLYPH;
25335 glyph->u.glyphless.method = it->glyphless_method;
25336 glyph->u.glyphless.for_no_font = for_no_font;
25337 glyph->u.glyphless.len = len;
25338 glyph->u.glyphless.ch = it->c;
25339 glyph->slice.glyphless.upper_xoff = upper_xoff;
25340 glyph->slice.glyphless.upper_yoff = upper_yoff;
25341 glyph->slice.glyphless.lower_xoff = lower_xoff;
25342 glyph->slice.glyphless.lower_yoff = lower_yoff;
25343 glyph->avoid_cursor_p = it->avoid_cursor_p;
25344 glyph->multibyte_p = it->multibyte_p;
25345 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25347 /* In R2L rows, the left and the right box edges need to be
25348 drawn in reverse direction. */
25349 glyph->right_box_line_p = it->start_of_box_run_p;
25350 glyph->left_box_line_p = it->end_of_box_run_p;
25352 else
25354 glyph->left_box_line_p = it->start_of_box_run_p;
25355 glyph->right_box_line_p = it->end_of_box_run_p;
25357 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25358 || it->phys_descent > it->descent);
25359 glyph->padding_p = 0;
25360 glyph->glyph_not_available_p = 0;
25361 glyph->face_id = face_id;
25362 glyph->font_type = FONT_TYPE_UNKNOWN;
25363 if (it->bidi_p)
25365 glyph->resolved_level = it->bidi_it.resolved_level;
25366 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25367 emacs_abort ();
25368 glyph->bidi_type = it->bidi_it.type;
25370 ++it->glyph_row->used[area];
25372 else
25373 IT_EXPAND_MATRIX_WIDTH (it, area);
25377 /* Produce a glyph for a glyphless character for iterator IT.
25378 IT->glyphless_method specifies which method to use for displaying
25379 the character. See the description of enum
25380 glyphless_display_method in dispextern.h for the detail.
25382 FOR_NO_FONT is nonzero if and only if this is for a character for
25383 which no font was found. ACRONYM, if non-nil, is an acronym string
25384 for the character. */
25386 static void
25387 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25389 int face_id;
25390 struct face *face;
25391 struct font *font;
25392 int base_width, base_height, width, height;
25393 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25394 int len;
25396 /* Get the metrics of the base font. We always refer to the current
25397 ASCII face. */
25398 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25399 font = face->font ? face->font : FRAME_FONT (it->f);
25400 it->ascent = FONT_BASE (font) + font->baseline_offset;
25401 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25402 base_height = it->ascent + it->descent;
25403 base_width = font->average_width;
25405 face_id = merge_glyphless_glyph_face (it);
25407 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25409 it->pixel_width = THIN_SPACE_WIDTH;
25410 len = 0;
25411 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25413 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25415 width = CHAR_WIDTH (it->c);
25416 if (width == 0)
25417 width = 1;
25418 else if (width > 4)
25419 width = 4;
25420 it->pixel_width = base_width * width;
25421 len = 0;
25422 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25424 else
25426 char buf[7];
25427 const char *str;
25428 unsigned int code[6];
25429 int upper_len;
25430 int ascent, descent;
25431 struct font_metrics metrics_upper, metrics_lower;
25433 face = FACE_FROM_ID (it->f, face_id);
25434 font = face->font ? face->font : FRAME_FONT (it->f);
25435 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25437 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25439 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25440 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25441 if (CONSP (acronym))
25442 acronym = XCAR (acronym);
25443 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25445 else
25447 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25448 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25449 str = buf;
25451 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25452 code[len] = font->driver->encode_char (font, str[len]);
25453 upper_len = (len + 1) / 2;
25454 font->driver->text_extents (font, code, upper_len,
25455 &metrics_upper);
25456 font->driver->text_extents (font, code + upper_len, len - upper_len,
25457 &metrics_lower);
25461 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25462 width = max (metrics_upper.width, metrics_lower.width) + 4;
25463 upper_xoff = upper_yoff = 2; /* the typical case */
25464 if (base_width >= width)
25466 /* Align the upper to the left, the lower to the right. */
25467 it->pixel_width = base_width;
25468 lower_xoff = base_width - 2 - metrics_lower.width;
25470 else
25472 /* Center the shorter one. */
25473 it->pixel_width = width;
25474 if (metrics_upper.width >= metrics_lower.width)
25475 lower_xoff = (width - metrics_lower.width) / 2;
25476 else
25478 /* FIXME: This code doesn't look right. It formerly was
25479 missing the "lower_xoff = 0;", which couldn't have
25480 been right since it left lower_xoff uninitialized. */
25481 lower_xoff = 0;
25482 upper_xoff = (width - metrics_upper.width) / 2;
25486 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25487 top, bottom, and between upper and lower strings. */
25488 height = (metrics_upper.ascent + metrics_upper.descent
25489 + metrics_lower.ascent + metrics_lower.descent) + 5;
25490 /* Center vertically.
25491 H:base_height, D:base_descent
25492 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25494 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25495 descent = D - H/2 + h/2;
25496 lower_yoff = descent - 2 - ld;
25497 upper_yoff = lower_yoff - la - 1 - ud; */
25498 ascent = - (it->descent - (base_height + height + 1) / 2);
25499 descent = it->descent - (base_height - height) / 2;
25500 lower_yoff = descent - 2 - metrics_lower.descent;
25501 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25502 - metrics_upper.descent);
25503 /* Don't make the height shorter than the base height. */
25504 if (height > base_height)
25506 it->ascent = ascent;
25507 it->descent = descent;
25511 it->phys_ascent = it->ascent;
25512 it->phys_descent = it->descent;
25513 if (it->glyph_row)
25514 append_glyphless_glyph (it, face_id, for_no_font, len,
25515 upper_xoff, upper_yoff,
25516 lower_xoff, lower_yoff);
25517 it->nglyphs = 1;
25518 take_vertical_position_into_account (it);
25522 /* RIF:
25523 Produce glyphs/get display metrics for the display element IT is
25524 loaded with. See the description of struct it in dispextern.h
25525 for an overview of struct it. */
25527 void
25528 x_produce_glyphs (struct it *it)
25530 int extra_line_spacing = it->extra_line_spacing;
25532 it->glyph_not_available_p = 0;
25534 if (it->what == IT_CHARACTER)
25536 XChar2b char2b;
25537 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25538 struct font *font = face->font;
25539 struct font_metrics *pcm = NULL;
25540 int boff; /* Baseline offset. */
25542 if (font == NULL)
25544 /* When no suitable font is found, display this character by
25545 the method specified in the first extra slot of
25546 Vglyphless_char_display. */
25547 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25549 eassert (it->what == IT_GLYPHLESS);
25550 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25551 goto done;
25554 boff = font->baseline_offset;
25555 if (font->vertical_centering)
25556 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25558 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25560 int stretched_p;
25562 it->nglyphs = 1;
25564 if (it->override_ascent >= 0)
25566 it->ascent = it->override_ascent;
25567 it->descent = it->override_descent;
25568 boff = it->override_boff;
25570 else
25572 it->ascent = FONT_BASE (font) + boff;
25573 it->descent = FONT_DESCENT (font) - boff;
25576 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25578 pcm = get_per_char_metric (font, &char2b);
25579 if (pcm->width == 0
25580 && pcm->rbearing == 0 && pcm->lbearing == 0)
25581 pcm = NULL;
25584 if (pcm)
25586 it->phys_ascent = pcm->ascent + boff;
25587 it->phys_descent = pcm->descent - boff;
25588 it->pixel_width = pcm->width;
25590 else
25592 it->glyph_not_available_p = 1;
25593 it->phys_ascent = it->ascent;
25594 it->phys_descent = it->descent;
25595 it->pixel_width = font->space_width;
25598 if (it->constrain_row_ascent_descent_p)
25600 if (it->descent > it->max_descent)
25602 it->ascent += it->descent - it->max_descent;
25603 it->descent = it->max_descent;
25605 if (it->ascent > it->max_ascent)
25607 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25608 it->ascent = it->max_ascent;
25610 it->phys_ascent = min (it->phys_ascent, it->ascent);
25611 it->phys_descent = min (it->phys_descent, it->descent);
25612 extra_line_spacing = 0;
25615 /* If this is a space inside a region of text with
25616 `space-width' property, change its width. */
25617 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25618 if (stretched_p)
25619 it->pixel_width *= XFLOATINT (it->space_width);
25621 /* If face has a box, add the box thickness to the character
25622 height. If character has a box line to the left and/or
25623 right, add the box line width to the character's width. */
25624 if (face->box != FACE_NO_BOX)
25626 int thick = face->box_line_width;
25628 if (thick > 0)
25630 it->ascent += thick;
25631 it->descent += thick;
25633 else
25634 thick = -thick;
25636 if (it->start_of_box_run_p)
25637 it->pixel_width += thick;
25638 if (it->end_of_box_run_p)
25639 it->pixel_width += thick;
25642 /* If face has an overline, add the height of the overline
25643 (1 pixel) and a 1 pixel margin to the character height. */
25644 if (face->overline_p)
25645 it->ascent += overline_margin;
25647 if (it->constrain_row_ascent_descent_p)
25649 if (it->ascent > it->max_ascent)
25650 it->ascent = it->max_ascent;
25651 if (it->descent > it->max_descent)
25652 it->descent = it->max_descent;
25655 take_vertical_position_into_account (it);
25657 /* If we have to actually produce glyphs, do it. */
25658 if (it->glyph_row)
25660 if (stretched_p)
25662 /* Translate a space with a `space-width' property
25663 into a stretch glyph. */
25664 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25665 / FONT_HEIGHT (font));
25666 append_stretch_glyph (it, it->object, it->pixel_width,
25667 it->ascent + it->descent, ascent);
25669 else
25670 append_glyph (it);
25672 /* If characters with lbearing or rbearing are displayed
25673 in this line, record that fact in a flag of the
25674 glyph row. This is used to optimize X output code. */
25675 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25676 it->glyph_row->contains_overlapping_glyphs_p = 1;
25678 if (! stretched_p && it->pixel_width == 0)
25679 /* We assure that all visible glyphs have at least 1-pixel
25680 width. */
25681 it->pixel_width = 1;
25683 else if (it->char_to_display == '\n')
25685 /* A newline has no width, but we need the height of the
25686 line. But if previous part of the line sets a height,
25687 don't increase that height. */
25689 Lisp_Object height;
25690 Lisp_Object total_height = Qnil;
25692 it->override_ascent = -1;
25693 it->pixel_width = 0;
25694 it->nglyphs = 0;
25696 height = get_it_property (it, Qline_height);
25697 /* Split (line-height total-height) list. */
25698 if (CONSP (height)
25699 && CONSP (XCDR (height))
25700 && NILP (XCDR (XCDR (height))))
25702 total_height = XCAR (XCDR (height));
25703 height = XCAR (height);
25705 height = calc_line_height_property (it, height, font, boff, 1);
25707 if (it->override_ascent >= 0)
25709 it->ascent = it->override_ascent;
25710 it->descent = it->override_descent;
25711 boff = it->override_boff;
25713 else
25715 it->ascent = FONT_BASE (font) + boff;
25716 it->descent = FONT_DESCENT (font) - boff;
25719 if (EQ (height, Qt))
25721 if (it->descent > it->max_descent)
25723 it->ascent += it->descent - it->max_descent;
25724 it->descent = it->max_descent;
25726 if (it->ascent > it->max_ascent)
25728 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25729 it->ascent = it->max_ascent;
25731 it->phys_ascent = min (it->phys_ascent, it->ascent);
25732 it->phys_descent = min (it->phys_descent, it->descent);
25733 it->constrain_row_ascent_descent_p = 1;
25734 extra_line_spacing = 0;
25736 else
25738 Lisp_Object spacing;
25740 it->phys_ascent = it->ascent;
25741 it->phys_descent = it->descent;
25743 if ((it->max_ascent > 0 || it->max_descent > 0)
25744 && face->box != FACE_NO_BOX
25745 && face->box_line_width > 0)
25747 it->ascent += face->box_line_width;
25748 it->descent += face->box_line_width;
25750 if (!NILP (height)
25751 && XINT (height) > it->ascent + it->descent)
25752 it->ascent = XINT (height) - it->descent;
25754 if (!NILP (total_height))
25755 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25756 else
25758 spacing = get_it_property (it, Qline_spacing);
25759 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25761 if (INTEGERP (spacing))
25763 extra_line_spacing = XINT (spacing);
25764 if (!NILP (total_height))
25765 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25769 else /* i.e. (it->char_to_display == '\t') */
25771 if (font->space_width > 0)
25773 int tab_width = it->tab_width * font->space_width;
25774 int x = it->current_x + it->continuation_lines_width;
25775 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25777 /* If the distance from the current position to the next tab
25778 stop is less than a space character width, use the
25779 tab stop after that. */
25780 if (next_tab_x - x < font->space_width)
25781 next_tab_x += tab_width;
25783 it->pixel_width = next_tab_x - x;
25784 it->nglyphs = 1;
25785 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25786 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25788 if (it->glyph_row)
25790 append_stretch_glyph (it, it->object, it->pixel_width,
25791 it->ascent + it->descent, it->ascent);
25794 else
25796 it->pixel_width = 0;
25797 it->nglyphs = 1;
25801 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25803 /* A static composition.
25805 Note: A composition is represented as one glyph in the
25806 glyph matrix. There are no padding glyphs.
25808 Important note: pixel_width, ascent, and descent are the
25809 values of what is drawn by draw_glyphs (i.e. the values of
25810 the overall glyphs composed). */
25811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25812 int boff; /* baseline offset */
25813 struct composition *cmp = composition_table[it->cmp_it.id];
25814 int glyph_len = cmp->glyph_len;
25815 struct font *font = face->font;
25817 it->nglyphs = 1;
25819 /* If we have not yet calculated pixel size data of glyphs of
25820 the composition for the current face font, calculate them
25821 now. Theoretically, we have to check all fonts for the
25822 glyphs, but that requires much time and memory space. So,
25823 here we check only the font of the first glyph. This may
25824 lead to incorrect display, but it's very rare, and C-l
25825 (recenter-top-bottom) can correct the display anyway. */
25826 if (! cmp->font || cmp->font != font)
25828 /* Ascent and descent of the font of the first character
25829 of this composition (adjusted by baseline offset).
25830 Ascent and descent of overall glyphs should not be less
25831 than these, respectively. */
25832 int font_ascent, font_descent, font_height;
25833 /* Bounding box of the overall glyphs. */
25834 int leftmost, rightmost, lowest, highest;
25835 int lbearing, rbearing;
25836 int i, width, ascent, descent;
25837 int left_padded = 0, right_padded = 0;
25838 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25839 XChar2b char2b;
25840 struct font_metrics *pcm;
25841 int font_not_found_p;
25842 ptrdiff_t pos;
25844 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25845 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25846 break;
25847 if (glyph_len < cmp->glyph_len)
25848 right_padded = 1;
25849 for (i = 0; i < glyph_len; i++)
25851 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25852 break;
25853 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25855 if (i > 0)
25856 left_padded = 1;
25858 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25859 : IT_CHARPOS (*it));
25860 /* If no suitable font is found, use the default font. */
25861 font_not_found_p = font == NULL;
25862 if (font_not_found_p)
25864 face = face->ascii_face;
25865 font = face->font;
25867 boff = font->baseline_offset;
25868 if (font->vertical_centering)
25869 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25870 font_ascent = FONT_BASE (font) + boff;
25871 font_descent = FONT_DESCENT (font) - boff;
25872 font_height = FONT_HEIGHT (font);
25874 cmp->font = font;
25876 pcm = NULL;
25877 if (! font_not_found_p)
25879 get_char_face_and_encoding (it->f, c, it->face_id,
25880 &char2b, 0);
25881 pcm = get_per_char_metric (font, &char2b);
25884 /* Initialize the bounding box. */
25885 if (pcm)
25887 width = cmp->glyph_len > 0 ? pcm->width : 0;
25888 ascent = pcm->ascent;
25889 descent = pcm->descent;
25890 lbearing = pcm->lbearing;
25891 rbearing = pcm->rbearing;
25893 else
25895 width = cmp->glyph_len > 0 ? font->space_width : 0;
25896 ascent = FONT_BASE (font);
25897 descent = FONT_DESCENT (font);
25898 lbearing = 0;
25899 rbearing = width;
25902 rightmost = width;
25903 leftmost = 0;
25904 lowest = - descent + boff;
25905 highest = ascent + boff;
25907 if (! font_not_found_p
25908 && font->default_ascent
25909 && CHAR_TABLE_P (Vuse_default_ascent)
25910 && !NILP (Faref (Vuse_default_ascent,
25911 make_number (it->char_to_display))))
25912 highest = font->default_ascent + boff;
25914 /* Draw the first glyph at the normal position. It may be
25915 shifted to right later if some other glyphs are drawn
25916 at the left. */
25917 cmp->offsets[i * 2] = 0;
25918 cmp->offsets[i * 2 + 1] = boff;
25919 cmp->lbearing = lbearing;
25920 cmp->rbearing = rbearing;
25922 /* Set cmp->offsets for the remaining glyphs. */
25923 for (i++; i < glyph_len; i++)
25925 int left, right, btm, top;
25926 int ch = COMPOSITION_GLYPH (cmp, i);
25927 int face_id;
25928 struct face *this_face;
25930 if (ch == '\t')
25931 ch = ' ';
25932 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25933 this_face = FACE_FROM_ID (it->f, face_id);
25934 font = this_face->font;
25936 if (font == NULL)
25937 pcm = NULL;
25938 else
25940 get_char_face_and_encoding (it->f, ch, face_id,
25941 &char2b, 0);
25942 pcm = get_per_char_metric (font, &char2b);
25944 if (! pcm)
25945 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25946 else
25948 width = pcm->width;
25949 ascent = pcm->ascent;
25950 descent = pcm->descent;
25951 lbearing = pcm->lbearing;
25952 rbearing = pcm->rbearing;
25953 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25955 /* Relative composition with or without
25956 alternate chars. */
25957 left = (leftmost + rightmost - width) / 2;
25958 btm = - descent + boff;
25959 if (font->relative_compose
25960 && (! CHAR_TABLE_P (Vignore_relative_composition)
25961 || NILP (Faref (Vignore_relative_composition,
25962 make_number (ch)))))
25965 if (- descent >= font->relative_compose)
25966 /* One extra pixel between two glyphs. */
25967 btm = highest + 1;
25968 else if (ascent <= 0)
25969 /* One extra pixel between two glyphs. */
25970 btm = lowest - 1 - ascent - descent;
25973 else
25975 /* A composition rule is specified by an integer
25976 value that encodes global and new reference
25977 points (GREF and NREF). GREF and NREF are
25978 specified by numbers as below:
25980 0---1---2 -- ascent
25984 9--10--11 -- center
25986 ---3---4---5--- baseline
25988 6---7---8 -- descent
25990 int rule = COMPOSITION_RULE (cmp, i);
25991 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25993 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25994 grefx = gref % 3, nrefx = nref % 3;
25995 grefy = gref / 3, nrefy = nref / 3;
25996 if (xoff)
25997 xoff = font_height * (xoff - 128) / 256;
25998 if (yoff)
25999 yoff = font_height * (yoff - 128) / 256;
26001 left = (leftmost
26002 + grefx * (rightmost - leftmost) / 2
26003 - nrefx * width / 2
26004 + xoff);
26006 btm = ((grefy == 0 ? highest
26007 : grefy == 1 ? 0
26008 : grefy == 2 ? lowest
26009 : (highest + lowest) / 2)
26010 - (nrefy == 0 ? ascent + descent
26011 : nrefy == 1 ? descent - boff
26012 : nrefy == 2 ? 0
26013 : (ascent + descent) / 2)
26014 + yoff);
26017 cmp->offsets[i * 2] = left;
26018 cmp->offsets[i * 2 + 1] = btm + descent;
26020 /* Update the bounding box of the overall glyphs. */
26021 if (width > 0)
26023 right = left + width;
26024 if (left < leftmost)
26025 leftmost = left;
26026 if (right > rightmost)
26027 rightmost = right;
26029 top = btm + descent + ascent;
26030 if (top > highest)
26031 highest = top;
26032 if (btm < lowest)
26033 lowest = btm;
26035 if (cmp->lbearing > left + lbearing)
26036 cmp->lbearing = left + lbearing;
26037 if (cmp->rbearing < left + rbearing)
26038 cmp->rbearing = left + rbearing;
26042 /* If there are glyphs whose x-offsets are negative,
26043 shift all glyphs to the right and make all x-offsets
26044 non-negative. */
26045 if (leftmost < 0)
26047 for (i = 0; i < cmp->glyph_len; i++)
26048 cmp->offsets[i * 2] -= leftmost;
26049 rightmost -= leftmost;
26050 cmp->lbearing -= leftmost;
26051 cmp->rbearing -= leftmost;
26054 if (left_padded && cmp->lbearing < 0)
26056 for (i = 0; i < cmp->glyph_len; i++)
26057 cmp->offsets[i * 2] -= cmp->lbearing;
26058 rightmost -= cmp->lbearing;
26059 cmp->rbearing -= cmp->lbearing;
26060 cmp->lbearing = 0;
26062 if (right_padded && rightmost < cmp->rbearing)
26064 rightmost = cmp->rbearing;
26067 cmp->pixel_width = rightmost;
26068 cmp->ascent = highest;
26069 cmp->descent = - lowest;
26070 if (cmp->ascent < font_ascent)
26071 cmp->ascent = font_ascent;
26072 if (cmp->descent < font_descent)
26073 cmp->descent = font_descent;
26076 if (it->glyph_row
26077 && (cmp->lbearing < 0
26078 || cmp->rbearing > cmp->pixel_width))
26079 it->glyph_row->contains_overlapping_glyphs_p = 1;
26081 it->pixel_width = cmp->pixel_width;
26082 it->ascent = it->phys_ascent = cmp->ascent;
26083 it->descent = it->phys_descent = cmp->descent;
26084 if (face->box != FACE_NO_BOX)
26086 int thick = face->box_line_width;
26088 if (thick > 0)
26090 it->ascent += thick;
26091 it->descent += thick;
26093 else
26094 thick = - thick;
26096 if (it->start_of_box_run_p)
26097 it->pixel_width += thick;
26098 if (it->end_of_box_run_p)
26099 it->pixel_width += thick;
26102 /* If face has an overline, add the height of the overline
26103 (1 pixel) and a 1 pixel margin to the character height. */
26104 if (face->overline_p)
26105 it->ascent += overline_margin;
26107 take_vertical_position_into_account (it);
26108 if (it->ascent < 0)
26109 it->ascent = 0;
26110 if (it->descent < 0)
26111 it->descent = 0;
26113 if (it->glyph_row && cmp->glyph_len > 0)
26114 append_composite_glyph (it);
26116 else if (it->what == IT_COMPOSITION)
26118 /* A dynamic (automatic) composition. */
26119 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26120 Lisp_Object gstring;
26121 struct font_metrics metrics;
26123 it->nglyphs = 1;
26125 gstring = composition_gstring_from_id (it->cmp_it.id);
26126 it->pixel_width
26127 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26128 &metrics);
26129 if (it->glyph_row
26130 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26131 it->glyph_row->contains_overlapping_glyphs_p = 1;
26132 it->ascent = it->phys_ascent = metrics.ascent;
26133 it->descent = it->phys_descent = metrics.descent;
26134 if (face->box != FACE_NO_BOX)
26136 int thick = face->box_line_width;
26138 if (thick > 0)
26140 it->ascent += thick;
26141 it->descent += thick;
26143 else
26144 thick = - thick;
26146 if (it->start_of_box_run_p)
26147 it->pixel_width += thick;
26148 if (it->end_of_box_run_p)
26149 it->pixel_width += thick;
26151 /* If face has an overline, add the height of the overline
26152 (1 pixel) and a 1 pixel margin to the character height. */
26153 if (face->overline_p)
26154 it->ascent += overline_margin;
26155 take_vertical_position_into_account (it);
26156 if (it->ascent < 0)
26157 it->ascent = 0;
26158 if (it->descent < 0)
26159 it->descent = 0;
26161 if (it->glyph_row)
26162 append_composite_glyph (it);
26164 else if (it->what == IT_GLYPHLESS)
26165 produce_glyphless_glyph (it, 0, Qnil);
26166 else if (it->what == IT_IMAGE)
26167 produce_image_glyph (it);
26168 else if (it->what == IT_STRETCH)
26169 produce_stretch_glyph (it);
26171 done:
26172 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26173 because this isn't true for images with `:ascent 100'. */
26174 eassert (it->ascent >= 0 && it->descent >= 0);
26175 if (it->area == TEXT_AREA)
26176 it->current_x += it->pixel_width;
26178 if (extra_line_spacing > 0)
26180 it->descent += extra_line_spacing;
26181 if (extra_line_spacing > it->max_extra_line_spacing)
26182 it->max_extra_line_spacing = extra_line_spacing;
26185 it->max_ascent = max (it->max_ascent, it->ascent);
26186 it->max_descent = max (it->max_descent, it->descent);
26187 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26188 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26191 /* EXPORT for RIF:
26192 Output LEN glyphs starting at START at the nominal cursor position.
26193 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26194 being updated, and UPDATED_AREA is the area of that row being updated. */
26196 void
26197 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26198 struct glyph *start, enum glyph_row_area updated_area, int len)
26200 int x, hpos, chpos = w->phys_cursor.hpos;
26202 eassert (updated_row);
26203 /* When the window is hscrolled, cursor hpos can legitimately be out
26204 of bounds, but we draw the cursor at the corresponding window
26205 margin in that case. */
26206 if (!updated_row->reversed_p && chpos < 0)
26207 chpos = 0;
26208 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26209 chpos = updated_row->used[TEXT_AREA] - 1;
26211 block_input ();
26213 /* Write glyphs. */
26215 hpos = start - updated_row->glyphs[updated_area];
26216 x = draw_glyphs (w, w->output_cursor.x,
26217 updated_row, updated_area,
26218 hpos, hpos + len,
26219 DRAW_NORMAL_TEXT, 0);
26221 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26222 if (updated_area == TEXT_AREA
26223 && w->phys_cursor_on_p
26224 && w->phys_cursor.vpos == w->output_cursor.vpos
26225 && chpos >= hpos
26226 && chpos < hpos + len)
26227 w->phys_cursor_on_p = 0;
26229 unblock_input ();
26231 /* Advance the output cursor. */
26232 w->output_cursor.hpos += len;
26233 w->output_cursor.x = x;
26237 /* EXPORT for RIF:
26238 Insert LEN glyphs from START at the nominal cursor position. */
26240 void
26241 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26242 struct glyph *start, enum glyph_row_area updated_area, int len)
26244 struct frame *f;
26245 int line_height, shift_by_width, shifted_region_width;
26246 struct glyph_row *row;
26247 struct glyph *glyph;
26248 int frame_x, frame_y;
26249 ptrdiff_t hpos;
26251 eassert (updated_row);
26252 block_input ();
26253 f = XFRAME (WINDOW_FRAME (w));
26255 /* Get the height of the line we are in. */
26256 row = updated_row;
26257 line_height = row->height;
26259 /* Get the width of the glyphs to insert. */
26260 shift_by_width = 0;
26261 for (glyph = start; glyph < start + len; ++glyph)
26262 shift_by_width += glyph->pixel_width;
26264 /* Get the width of the region to shift right. */
26265 shifted_region_width = (window_box_width (w, updated_area)
26266 - w->output_cursor.x
26267 - shift_by_width);
26269 /* Shift right. */
26270 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26271 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26273 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26274 line_height, shift_by_width);
26276 /* Write the glyphs. */
26277 hpos = start - row->glyphs[updated_area];
26278 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26279 hpos, hpos + len,
26280 DRAW_NORMAL_TEXT, 0);
26282 /* Advance the output cursor. */
26283 w->output_cursor.hpos += len;
26284 w->output_cursor.x += shift_by_width;
26285 unblock_input ();
26289 /* EXPORT for RIF:
26290 Erase the current text line from the nominal cursor position
26291 (inclusive) to pixel column TO_X (exclusive). The idea is that
26292 everything from TO_X onward is already erased.
26294 TO_X is a pixel position relative to UPDATED_AREA of currently
26295 updated window W. TO_X == -1 means clear to the end of this area. */
26297 void
26298 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26299 enum glyph_row_area updated_area, int to_x)
26301 struct frame *f;
26302 int max_x, min_y, max_y;
26303 int from_x, from_y, to_y;
26305 eassert (updated_row);
26306 f = XFRAME (w->frame);
26308 if (updated_row->full_width_p)
26309 max_x = (WINDOW_PIXEL_WIDTH (w)
26310 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26311 else
26312 max_x = window_box_width (w, updated_area);
26313 max_y = window_text_bottom_y (w);
26315 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26316 of window. For TO_X > 0, truncate to end of drawing area. */
26317 if (to_x == 0)
26318 return;
26319 else if (to_x < 0)
26320 to_x = max_x;
26321 else
26322 to_x = min (to_x, max_x);
26324 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26326 /* Notice if the cursor will be cleared by this operation. */
26327 if (!updated_row->full_width_p)
26328 notice_overwritten_cursor (w, updated_area,
26329 w->output_cursor.x, -1,
26330 updated_row->y,
26331 MATRIX_ROW_BOTTOM_Y (updated_row));
26333 from_x = w->output_cursor.x;
26335 /* Translate to frame coordinates. */
26336 if (updated_row->full_width_p)
26338 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26339 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26341 else
26343 int area_left = window_box_left (w, updated_area);
26344 from_x += area_left;
26345 to_x += area_left;
26348 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26349 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26350 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26352 /* Prevent inadvertently clearing to end of the X window. */
26353 if (to_x > from_x && to_y > from_y)
26355 block_input ();
26356 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26357 to_x - from_x, to_y - from_y);
26358 unblock_input ();
26362 #endif /* HAVE_WINDOW_SYSTEM */
26366 /***********************************************************************
26367 Cursor types
26368 ***********************************************************************/
26370 /* Value is the internal representation of the specified cursor type
26371 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26372 of the bar cursor. */
26374 static enum text_cursor_kinds
26375 get_specified_cursor_type (Lisp_Object arg, int *width)
26377 enum text_cursor_kinds type;
26379 if (NILP (arg))
26380 return NO_CURSOR;
26382 if (EQ (arg, Qbox))
26383 return FILLED_BOX_CURSOR;
26385 if (EQ (arg, Qhollow))
26386 return HOLLOW_BOX_CURSOR;
26388 if (EQ (arg, Qbar))
26390 *width = 2;
26391 return BAR_CURSOR;
26394 if (CONSP (arg)
26395 && EQ (XCAR (arg), Qbar)
26396 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26398 *width = XINT (XCDR (arg));
26399 return BAR_CURSOR;
26402 if (EQ (arg, Qhbar))
26404 *width = 2;
26405 return HBAR_CURSOR;
26408 if (CONSP (arg)
26409 && EQ (XCAR (arg), Qhbar)
26410 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26412 *width = XINT (XCDR (arg));
26413 return HBAR_CURSOR;
26416 /* Treat anything unknown as "hollow box cursor".
26417 It was bad to signal an error; people have trouble fixing
26418 .Xdefaults with Emacs, when it has something bad in it. */
26419 type = HOLLOW_BOX_CURSOR;
26421 return type;
26424 /* Set the default cursor types for specified frame. */
26425 void
26426 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26428 int width = 1;
26429 Lisp_Object tem;
26431 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26432 FRAME_CURSOR_WIDTH (f) = width;
26434 /* By default, set up the blink-off state depending on the on-state. */
26436 tem = Fassoc (arg, Vblink_cursor_alist);
26437 if (!NILP (tem))
26439 FRAME_BLINK_OFF_CURSOR (f)
26440 = get_specified_cursor_type (XCDR (tem), &width);
26441 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26443 else
26444 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26446 /* Make sure the cursor gets redrawn. */
26447 f->cursor_type_changed = 1;
26451 #ifdef HAVE_WINDOW_SYSTEM
26453 /* Return the cursor we want to be displayed in window W. Return
26454 width of bar/hbar cursor through WIDTH arg. Return with
26455 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26456 (i.e. if the `system caret' should track this cursor).
26458 In a mini-buffer window, we want the cursor only to appear if we
26459 are reading input from this window. For the selected window, we
26460 want the cursor type given by the frame parameter or buffer local
26461 setting of cursor-type. If explicitly marked off, draw no cursor.
26462 In all other cases, we want a hollow box cursor. */
26464 static enum text_cursor_kinds
26465 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26466 int *active_cursor)
26468 struct frame *f = XFRAME (w->frame);
26469 struct buffer *b = XBUFFER (w->contents);
26470 int cursor_type = DEFAULT_CURSOR;
26471 Lisp_Object alt_cursor;
26472 int non_selected = 0;
26474 *active_cursor = 1;
26476 /* Echo area */
26477 if (cursor_in_echo_area
26478 && FRAME_HAS_MINIBUF_P (f)
26479 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26481 if (w == XWINDOW (echo_area_window))
26483 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26485 *width = FRAME_CURSOR_WIDTH (f);
26486 return FRAME_DESIRED_CURSOR (f);
26488 else
26489 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26492 *active_cursor = 0;
26493 non_selected = 1;
26496 /* Detect a nonselected window or nonselected frame. */
26497 else if (w != XWINDOW (f->selected_window)
26498 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26500 *active_cursor = 0;
26502 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26503 return NO_CURSOR;
26505 non_selected = 1;
26508 /* Never display a cursor in a window in which cursor-type is nil. */
26509 if (NILP (BVAR (b, cursor_type)))
26510 return NO_CURSOR;
26512 /* Get the normal cursor type for this window. */
26513 if (EQ (BVAR (b, cursor_type), Qt))
26515 cursor_type = FRAME_DESIRED_CURSOR (f);
26516 *width = FRAME_CURSOR_WIDTH (f);
26518 else
26519 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26521 /* Use cursor-in-non-selected-windows instead
26522 for non-selected window or frame. */
26523 if (non_selected)
26525 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26526 if (!EQ (Qt, alt_cursor))
26527 return get_specified_cursor_type (alt_cursor, width);
26528 /* t means modify the normal cursor type. */
26529 if (cursor_type == FILLED_BOX_CURSOR)
26530 cursor_type = HOLLOW_BOX_CURSOR;
26531 else if (cursor_type == BAR_CURSOR && *width > 1)
26532 --*width;
26533 return cursor_type;
26536 /* Use normal cursor if not blinked off. */
26537 if (!w->cursor_off_p)
26539 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26541 if (cursor_type == FILLED_BOX_CURSOR)
26543 /* Using a block cursor on large images can be very annoying.
26544 So use a hollow cursor for "large" images.
26545 If image is not transparent (no mask), also use hollow cursor. */
26546 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26547 if (img != NULL && IMAGEP (img->spec))
26549 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26550 where N = size of default frame font size.
26551 This should cover most of the "tiny" icons people may use. */
26552 if (!img->mask
26553 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26554 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26555 cursor_type = HOLLOW_BOX_CURSOR;
26558 else if (cursor_type != NO_CURSOR)
26560 /* Display current only supports BOX and HOLLOW cursors for images.
26561 So for now, unconditionally use a HOLLOW cursor when cursor is
26562 not a solid box cursor. */
26563 cursor_type = HOLLOW_BOX_CURSOR;
26566 return cursor_type;
26569 /* Cursor is blinked off, so determine how to "toggle" it. */
26571 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26572 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26573 return get_specified_cursor_type (XCDR (alt_cursor), width);
26575 /* Then see if frame has specified a specific blink off cursor type. */
26576 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26578 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26579 return FRAME_BLINK_OFF_CURSOR (f);
26582 #if 0
26583 /* Some people liked having a permanently visible blinking cursor,
26584 while others had very strong opinions against it. So it was
26585 decided to remove it. KFS 2003-09-03 */
26587 /* Finally perform built-in cursor blinking:
26588 filled box <-> hollow box
26589 wide [h]bar <-> narrow [h]bar
26590 narrow [h]bar <-> no cursor
26591 other type <-> no cursor */
26593 if (cursor_type == FILLED_BOX_CURSOR)
26594 return HOLLOW_BOX_CURSOR;
26596 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26598 *width = 1;
26599 return cursor_type;
26601 #endif
26603 return NO_CURSOR;
26607 /* Notice when the text cursor of window W has been completely
26608 overwritten by a drawing operation that outputs glyphs in AREA
26609 starting at X0 and ending at X1 in the line starting at Y0 and
26610 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26611 the rest of the line after X0 has been written. Y coordinates
26612 are window-relative. */
26614 static void
26615 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26616 int x0, int x1, int y0, int y1)
26618 int cx0, cx1, cy0, cy1;
26619 struct glyph_row *row;
26621 if (!w->phys_cursor_on_p)
26622 return;
26623 if (area != TEXT_AREA)
26624 return;
26626 if (w->phys_cursor.vpos < 0
26627 || w->phys_cursor.vpos >= w->current_matrix->nrows
26628 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26629 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26630 return;
26632 if (row->cursor_in_fringe_p)
26634 row->cursor_in_fringe_p = 0;
26635 draw_fringe_bitmap (w, row, row->reversed_p);
26636 w->phys_cursor_on_p = 0;
26637 return;
26640 cx0 = w->phys_cursor.x;
26641 cx1 = cx0 + w->phys_cursor_width;
26642 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26643 return;
26645 /* The cursor image will be completely removed from the
26646 screen if the output area intersects the cursor area in
26647 y-direction. When we draw in [y0 y1[, and some part of
26648 the cursor is at y < y0, that part must have been drawn
26649 before. When scrolling, the cursor is erased before
26650 actually scrolling, so we don't come here. When not
26651 scrolling, the rows above the old cursor row must have
26652 changed, and in this case these rows must have written
26653 over the cursor image.
26655 Likewise if part of the cursor is below y1, with the
26656 exception of the cursor being in the first blank row at
26657 the buffer and window end because update_text_area
26658 doesn't draw that row. (Except when it does, but
26659 that's handled in update_text_area.) */
26661 cy0 = w->phys_cursor.y;
26662 cy1 = cy0 + w->phys_cursor_height;
26663 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26664 return;
26666 w->phys_cursor_on_p = 0;
26669 #endif /* HAVE_WINDOW_SYSTEM */
26672 /************************************************************************
26673 Mouse Face
26674 ************************************************************************/
26676 #ifdef HAVE_WINDOW_SYSTEM
26678 /* EXPORT for RIF:
26679 Fix the display of area AREA of overlapping row ROW in window W
26680 with respect to the overlapping part OVERLAPS. */
26682 void
26683 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26684 enum glyph_row_area area, int overlaps)
26686 int i, x;
26688 block_input ();
26690 x = 0;
26691 for (i = 0; i < row->used[area];)
26693 if (row->glyphs[area][i].overlaps_vertically_p)
26695 int start = i, start_x = x;
26699 x += row->glyphs[area][i].pixel_width;
26700 ++i;
26702 while (i < row->used[area]
26703 && row->glyphs[area][i].overlaps_vertically_p);
26705 draw_glyphs (w, start_x, row, area,
26706 start, i,
26707 DRAW_NORMAL_TEXT, overlaps);
26709 else
26711 x += row->glyphs[area][i].pixel_width;
26712 ++i;
26716 unblock_input ();
26720 /* EXPORT:
26721 Draw the cursor glyph of window W in glyph row ROW. See the
26722 comment of draw_glyphs for the meaning of HL. */
26724 void
26725 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26726 enum draw_glyphs_face hl)
26728 /* If cursor hpos is out of bounds, don't draw garbage. This can
26729 happen in mini-buffer windows when switching between echo area
26730 glyphs and mini-buffer. */
26731 if ((row->reversed_p
26732 ? (w->phys_cursor.hpos >= 0)
26733 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26735 int on_p = w->phys_cursor_on_p;
26736 int x1;
26737 int hpos = w->phys_cursor.hpos;
26739 /* When the window is hscrolled, cursor hpos can legitimately be
26740 out of bounds, but we draw the cursor at the corresponding
26741 window margin in that case. */
26742 if (!row->reversed_p && hpos < 0)
26743 hpos = 0;
26744 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26745 hpos = row->used[TEXT_AREA] - 1;
26747 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26748 hl, 0);
26749 w->phys_cursor_on_p = on_p;
26751 if (hl == DRAW_CURSOR)
26752 w->phys_cursor_width = x1 - w->phys_cursor.x;
26753 /* When we erase the cursor, and ROW is overlapped by other
26754 rows, make sure that these overlapping parts of other rows
26755 are redrawn. */
26756 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26758 w->phys_cursor_width = x1 - w->phys_cursor.x;
26760 if (row > w->current_matrix->rows
26761 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26762 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26763 OVERLAPS_ERASED_CURSOR);
26765 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26766 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26767 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26768 OVERLAPS_ERASED_CURSOR);
26774 /* Erase the image of a cursor of window W from the screen. */
26776 #ifndef HAVE_NTGUI
26777 static
26778 #endif
26779 void
26780 erase_phys_cursor (struct window *w)
26782 struct frame *f = XFRAME (w->frame);
26783 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26784 int hpos = w->phys_cursor.hpos;
26785 int vpos = w->phys_cursor.vpos;
26786 int mouse_face_here_p = 0;
26787 struct glyph_matrix *active_glyphs = w->current_matrix;
26788 struct glyph_row *cursor_row;
26789 struct glyph *cursor_glyph;
26790 enum draw_glyphs_face hl;
26792 /* No cursor displayed or row invalidated => nothing to do on the
26793 screen. */
26794 if (w->phys_cursor_type == NO_CURSOR)
26795 goto mark_cursor_off;
26797 /* VPOS >= active_glyphs->nrows means that window has been resized.
26798 Don't bother to erase the cursor. */
26799 if (vpos >= active_glyphs->nrows)
26800 goto mark_cursor_off;
26802 /* If row containing cursor is marked invalid, there is nothing we
26803 can do. */
26804 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26805 if (!cursor_row->enabled_p)
26806 goto mark_cursor_off;
26808 /* If line spacing is > 0, old cursor may only be partially visible in
26809 window after split-window. So adjust visible height. */
26810 cursor_row->visible_height = min (cursor_row->visible_height,
26811 window_text_bottom_y (w) - cursor_row->y);
26813 /* If row is completely invisible, don't attempt to delete a cursor which
26814 isn't there. This can happen if cursor is at top of a window, and
26815 we switch to a buffer with a header line in that window. */
26816 if (cursor_row->visible_height <= 0)
26817 goto mark_cursor_off;
26819 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26820 if (cursor_row->cursor_in_fringe_p)
26822 cursor_row->cursor_in_fringe_p = 0;
26823 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26824 goto mark_cursor_off;
26827 /* This can happen when the new row is shorter than the old one.
26828 In this case, either draw_glyphs or clear_end_of_line
26829 should have cleared the cursor. Note that we wouldn't be
26830 able to erase the cursor in this case because we don't have a
26831 cursor glyph at hand. */
26832 if ((cursor_row->reversed_p
26833 ? (w->phys_cursor.hpos < 0)
26834 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26835 goto mark_cursor_off;
26837 /* When the window is hscrolled, cursor hpos can legitimately be out
26838 of bounds, but we draw the cursor at the corresponding window
26839 margin in that case. */
26840 if (!cursor_row->reversed_p && hpos < 0)
26841 hpos = 0;
26842 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26843 hpos = cursor_row->used[TEXT_AREA] - 1;
26845 /* If the cursor is in the mouse face area, redisplay that when
26846 we clear the cursor. */
26847 if (! NILP (hlinfo->mouse_face_window)
26848 && coords_in_mouse_face_p (w, hpos, vpos)
26849 /* Don't redraw the cursor's spot in mouse face if it is at the
26850 end of a line (on a newline). The cursor appears there, but
26851 mouse highlighting does not. */
26852 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26853 mouse_face_here_p = 1;
26855 /* Maybe clear the display under the cursor. */
26856 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26858 int x, y, left_x;
26859 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26860 int width;
26862 cursor_glyph = get_phys_cursor_glyph (w);
26863 if (cursor_glyph == NULL)
26864 goto mark_cursor_off;
26866 width = cursor_glyph->pixel_width;
26867 left_x = window_box_left_offset (w, TEXT_AREA);
26868 x = w->phys_cursor.x;
26869 if (x < left_x)
26870 width -= left_x - x;
26871 width = min (width, window_box_width (w, TEXT_AREA) - x);
26872 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26873 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26875 if (width > 0)
26876 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26879 /* Erase the cursor by redrawing the character underneath it. */
26880 if (mouse_face_here_p)
26881 hl = DRAW_MOUSE_FACE;
26882 else
26883 hl = DRAW_NORMAL_TEXT;
26884 draw_phys_cursor_glyph (w, cursor_row, hl);
26886 mark_cursor_off:
26887 w->phys_cursor_on_p = 0;
26888 w->phys_cursor_type = NO_CURSOR;
26892 /* EXPORT:
26893 Display or clear cursor of window W. If ON is zero, clear the
26894 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26895 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26897 void
26898 display_and_set_cursor (struct window *w, bool on,
26899 int hpos, int vpos, int x, int y)
26901 struct frame *f = XFRAME (w->frame);
26902 int new_cursor_type;
26903 int new_cursor_width;
26904 int active_cursor;
26905 struct glyph_row *glyph_row;
26906 struct glyph *glyph;
26908 /* This is pointless on invisible frames, and dangerous on garbaged
26909 windows and frames; in the latter case, the frame or window may
26910 be in the midst of changing its size, and x and y may be off the
26911 window. */
26912 if (! FRAME_VISIBLE_P (f)
26913 || FRAME_GARBAGED_P (f)
26914 || vpos >= w->current_matrix->nrows
26915 || hpos >= w->current_matrix->matrix_w)
26916 return;
26918 /* If cursor is off and we want it off, return quickly. */
26919 if (!on && !w->phys_cursor_on_p)
26920 return;
26922 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26923 /* If cursor row is not enabled, we don't really know where to
26924 display the cursor. */
26925 if (!glyph_row->enabled_p)
26927 w->phys_cursor_on_p = 0;
26928 return;
26931 glyph = NULL;
26932 if (!glyph_row->exact_window_width_line_p
26933 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26934 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26936 eassert (input_blocked_p ());
26938 /* Set new_cursor_type to the cursor we want to be displayed. */
26939 new_cursor_type = get_window_cursor_type (w, glyph,
26940 &new_cursor_width, &active_cursor);
26942 /* If cursor is currently being shown and we don't want it to be or
26943 it is in the wrong place, or the cursor type is not what we want,
26944 erase it. */
26945 if (w->phys_cursor_on_p
26946 && (!on
26947 || w->phys_cursor.x != x
26948 || w->phys_cursor.y != y
26949 || new_cursor_type != w->phys_cursor_type
26950 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26951 && new_cursor_width != w->phys_cursor_width)))
26952 erase_phys_cursor (w);
26954 /* Don't check phys_cursor_on_p here because that flag is only set
26955 to zero in some cases where we know that the cursor has been
26956 completely erased, to avoid the extra work of erasing the cursor
26957 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26958 still not be visible, or it has only been partly erased. */
26959 if (on)
26961 w->phys_cursor_ascent = glyph_row->ascent;
26962 w->phys_cursor_height = glyph_row->height;
26964 /* Set phys_cursor_.* before x_draw_.* is called because some
26965 of them may need the information. */
26966 w->phys_cursor.x = x;
26967 w->phys_cursor.y = glyph_row->y;
26968 w->phys_cursor.hpos = hpos;
26969 w->phys_cursor.vpos = vpos;
26972 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26973 new_cursor_type, new_cursor_width,
26974 on, active_cursor);
26978 /* Switch the display of W's cursor on or off, according to the value
26979 of ON. */
26981 static void
26982 update_window_cursor (struct window *w, bool on)
26984 /* Don't update cursor in windows whose frame is in the process
26985 of being deleted. */
26986 if (w->current_matrix)
26988 int hpos = w->phys_cursor.hpos;
26989 int vpos = w->phys_cursor.vpos;
26990 struct glyph_row *row;
26992 if (vpos >= w->current_matrix->nrows
26993 || hpos >= w->current_matrix->matrix_w)
26994 return;
26996 row = MATRIX_ROW (w->current_matrix, vpos);
26998 /* When the window is hscrolled, cursor hpos can legitimately be
26999 out of bounds, but we draw the cursor at the corresponding
27000 window margin in that case. */
27001 if (!row->reversed_p && hpos < 0)
27002 hpos = 0;
27003 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27004 hpos = row->used[TEXT_AREA] - 1;
27006 block_input ();
27007 display_and_set_cursor (w, on, hpos, vpos,
27008 w->phys_cursor.x, w->phys_cursor.y);
27009 unblock_input ();
27014 /* Call update_window_cursor with parameter ON_P on all leaf windows
27015 in the window tree rooted at W. */
27017 static void
27018 update_cursor_in_window_tree (struct window *w, bool on_p)
27020 while (w)
27022 if (WINDOWP (w->contents))
27023 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27024 else
27025 update_window_cursor (w, on_p);
27027 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27032 /* EXPORT:
27033 Display the cursor on window W, or clear it, according to ON_P.
27034 Don't change the cursor's position. */
27036 void
27037 x_update_cursor (struct frame *f, bool on_p)
27039 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27043 /* EXPORT:
27044 Clear the cursor of window W to background color, and mark the
27045 cursor as not shown. This is used when the text where the cursor
27046 is about to be rewritten. */
27048 void
27049 x_clear_cursor (struct window *w)
27051 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27052 update_window_cursor (w, 0);
27055 #endif /* HAVE_WINDOW_SYSTEM */
27057 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27058 and MSDOS. */
27059 static void
27060 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27061 int start_hpos, int end_hpos,
27062 enum draw_glyphs_face draw)
27064 #ifdef HAVE_WINDOW_SYSTEM
27065 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27067 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27068 return;
27070 #endif
27071 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27072 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27073 #endif
27076 /* Display the active region described by mouse_face_* according to DRAW. */
27078 static void
27079 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27081 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27082 struct frame *f = XFRAME (WINDOW_FRAME (w));
27084 if (/* If window is in the process of being destroyed, don't bother
27085 to do anything. */
27086 w->current_matrix != NULL
27087 /* Don't update mouse highlight if hidden */
27088 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27089 /* Recognize when we are called to operate on rows that don't exist
27090 anymore. This can happen when a window is split. */
27091 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27093 int phys_cursor_on_p = w->phys_cursor_on_p;
27094 struct glyph_row *row, *first, *last;
27096 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27097 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27099 for (row = first; row <= last && row->enabled_p; ++row)
27101 int start_hpos, end_hpos, start_x;
27103 /* For all but the first row, the highlight starts at column 0. */
27104 if (row == first)
27106 /* R2L rows have BEG and END in reversed order, but the
27107 screen drawing geometry is always left to right. So
27108 we need to mirror the beginning and end of the
27109 highlighted area in R2L rows. */
27110 if (!row->reversed_p)
27112 start_hpos = hlinfo->mouse_face_beg_col;
27113 start_x = hlinfo->mouse_face_beg_x;
27115 else if (row == last)
27117 start_hpos = hlinfo->mouse_face_end_col;
27118 start_x = hlinfo->mouse_face_end_x;
27120 else
27122 start_hpos = 0;
27123 start_x = 0;
27126 else if (row->reversed_p && row == last)
27128 start_hpos = hlinfo->mouse_face_end_col;
27129 start_x = hlinfo->mouse_face_end_x;
27131 else
27133 start_hpos = 0;
27134 start_x = 0;
27137 if (row == last)
27139 if (!row->reversed_p)
27140 end_hpos = hlinfo->mouse_face_end_col;
27141 else if (row == first)
27142 end_hpos = hlinfo->mouse_face_beg_col;
27143 else
27145 end_hpos = row->used[TEXT_AREA];
27146 if (draw == DRAW_NORMAL_TEXT)
27147 row->fill_line_p = 1; /* Clear to end of line */
27150 else if (row->reversed_p && row == first)
27151 end_hpos = hlinfo->mouse_face_beg_col;
27152 else
27154 end_hpos = row->used[TEXT_AREA];
27155 if (draw == DRAW_NORMAL_TEXT)
27156 row->fill_line_p = 1; /* Clear to end of line */
27159 if (end_hpos > start_hpos)
27161 draw_row_with_mouse_face (w, start_x, row,
27162 start_hpos, end_hpos, draw);
27164 row->mouse_face_p
27165 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27169 #ifdef HAVE_WINDOW_SYSTEM
27170 /* When we've written over the cursor, arrange for it to
27171 be displayed again. */
27172 if (FRAME_WINDOW_P (f)
27173 && phys_cursor_on_p && !w->phys_cursor_on_p)
27175 int hpos = w->phys_cursor.hpos;
27177 /* When the window is hscrolled, cursor hpos can legitimately be
27178 out of bounds, but we draw the cursor at the corresponding
27179 window margin in that case. */
27180 if (!row->reversed_p && hpos < 0)
27181 hpos = 0;
27182 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27183 hpos = row->used[TEXT_AREA] - 1;
27185 block_input ();
27186 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27187 w->phys_cursor.x, w->phys_cursor.y);
27188 unblock_input ();
27190 #endif /* HAVE_WINDOW_SYSTEM */
27193 #ifdef HAVE_WINDOW_SYSTEM
27194 /* Change the mouse cursor. */
27195 if (FRAME_WINDOW_P (f))
27197 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27198 if (draw == DRAW_NORMAL_TEXT
27199 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27200 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27201 else
27202 #endif
27203 if (draw == DRAW_MOUSE_FACE)
27204 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27205 else
27206 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27208 #endif /* HAVE_WINDOW_SYSTEM */
27211 /* EXPORT:
27212 Clear out the mouse-highlighted active region.
27213 Redraw it un-highlighted first. Value is non-zero if mouse
27214 face was actually drawn unhighlighted. */
27217 clear_mouse_face (Mouse_HLInfo *hlinfo)
27219 int cleared = 0;
27221 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27223 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27224 cleared = 1;
27227 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27228 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27229 hlinfo->mouse_face_window = Qnil;
27230 hlinfo->mouse_face_overlay = Qnil;
27231 return cleared;
27234 /* Return true if the coordinates HPOS and VPOS on windows W are
27235 within the mouse face on that window. */
27236 static bool
27237 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27239 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27241 /* Quickly resolve the easy cases. */
27242 if (!(WINDOWP (hlinfo->mouse_face_window)
27243 && XWINDOW (hlinfo->mouse_face_window) == w))
27244 return false;
27245 if (vpos < hlinfo->mouse_face_beg_row
27246 || vpos > hlinfo->mouse_face_end_row)
27247 return false;
27248 if (vpos > hlinfo->mouse_face_beg_row
27249 && vpos < hlinfo->mouse_face_end_row)
27250 return true;
27252 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27254 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27256 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27257 return true;
27259 else if ((vpos == hlinfo->mouse_face_beg_row
27260 && hpos >= hlinfo->mouse_face_beg_col)
27261 || (vpos == hlinfo->mouse_face_end_row
27262 && hpos < hlinfo->mouse_face_end_col))
27263 return true;
27265 else
27267 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27269 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27270 return true;
27272 else if ((vpos == hlinfo->mouse_face_beg_row
27273 && hpos <= hlinfo->mouse_face_beg_col)
27274 || (vpos == hlinfo->mouse_face_end_row
27275 && hpos > hlinfo->mouse_face_end_col))
27276 return true;
27278 return false;
27282 /* EXPORT:
27283 True if physical cursor of window W is within mouse face. */
27285 bool
27286 cursor_in_mouse_face_p (struct window *w)
27288 int hpos = w->phys_cursor.hpos;
27289 int vpos = w->phys_cursor.vpos;
27290 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27292 /* When the window is hscrolled, cursor hpos can legitimately be out
27293 of bounds, but we draw the cursor at the corresponding window
27294 margin in that case. */
27295 if (!row->reversed_p && hpos < 0)
27296 hpos = 0;
27297 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27298 hpos = row->used[TEXT_AREA] - 1;
27300 return coords_in_mouse_face_p (w, hpos, vpos);
27305 /* Find the glyph rows START_ROW and END_ROW of window W that display
27306 characters between buffer positions START_CHARPOS and END_CHARPOS
27307 (excluding END_CHARPOS). DISP_STRING is a display string that
27308 covers these buffer positions. This is similar to
27309 row_containing_pos, but is more accurate when bidi reordering makes
27310 buffer positions change non-linearly with glyph rows. */
27311 static void
27312 rows_from_pos_range (struct window *w,
27313 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27314 Lisp_Object disp_string,
27315 struct glyph_row **start, struct glyph_row **end)
27317 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27318 int last_y = window_text_bottom_y (w);
27319 struct glyph_row *row;
27321 *start = NULL;
27322 *end = NULL;
27324 while (!first->enabled_p
27325 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27326 first++;
27328 /* Find the START row. */
27329 for (row = first;
27330 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27331 row++)
27333 /* A row can potentially be the START row if the range of the
27334 characters it displays intersects the range
27335 [START_CHARPOS..END_CHARPOS). */
27336 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27337 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27338 /* See the commentary in row_containing_pos, for the
27339 explanation of the complicated way to check whether
27340 some position is beyond the end of the characters
27341 displayed by a row. */
27342 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27343 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27344 && !row->ends_at_zv_p
27345 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27346 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27347 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27348 && !row->ends_at_zv_p
27349 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27351 /* Found a candidate row. Now make sure at least one of the
27352 glyphs it displays has a charpos from the range
27353 [START_CHARPOS..END_CHARPOS).
27355 This is not obvious because bidi reordering could make
27356 buffer positions of a row be 1,2,3,102,101,100, and if we
27357 want to highlight characters in [50..60), we don't want
27358 this row, even though [50..60) does intersect [1..103),
27359 the range of character positions given by the row's start
27360 and end positions. */
27361 struct glyph *g = row->glyphs[TEXT_AREA];
27362 struct glyph *e = g + row->used[TEXT_AREA];
27364 while (g < e)
27366 if (((BUFFERP (g->object) || INTEGERP (g->object))
27367 && start_charpos <= g->charpos && g->charpos < end_charpos)
27368 /* A glyph that comes from DISP_STRING is by
27369 definition to be highlighted. */
27370 || EQ (g->object, disp_string))
27371 *start = row;
27372 g++;
27374 if (*start)
27375 break;
27379 /* Find the END row. */
27380 if (!*start
27381 /* If the last row is partially visible, start looking for END
27382 from that row, instead of starting from FIRST. */
27383 && !(row->enabled_p
27384 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27385 row = first;
27386 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27388 struct glyph_row *next = row + 1;
27389 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27391 if (!next->enabled_p
27392 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27393 /* The first row >= START whose range of displayed characters
27394 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27395 is the row END + 1. */
27396 || (start_charpos < next_start
27397 && end_charpos < next_start)
27398 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27399 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27400 && !next->ends_at_zv_p
27401 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27402 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27403 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27404 && !next->ends_at_zv_p
27405 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27407 *end = row;
27408 break;
27410 else
27412 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27413 but none of the characters it displays are in the range, it is
27414 also END + 1. */
27415 struct glyph *g = next->glyphs[TEXT_AREA];
27416 struct glyph *s = g;
27417 struct glyph *e = g + next->used[TEXT_AREA];
27419 while (g < e)
27421 if (((BUFFERP (g->object) || INTEGERP (g->object))
27422 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27423 /* If the buffer position of the first glyph in
27424 the row is equal to END_CHARPOS, it means
27425 the last character to be highlighted is the
27426 newline of ROW, and we must consider NEXT as
27427 END, not END+1. */
27428 || (((!next->reversed_p && g == s)
27429 || (next->reversed_p && g == e - 1))
27430 && (g->charpos == end_charpos
27431 /* Special case for when NEXT is an
27432 empty line at ZV. */
27433 || (g->charpos == -1
27434 && !row->ends_at_zv_p
27435 && next_start == end_charpos)))))
27436 /* A glyph that comes from DISP_STRING is by
27437 definition to be highlighted. */
27438 || EQ (g->object, disp_string))
27439 break;
27440 g++;
27442 if (g == e)
27444 *end = row;
27445 break;
27447 /* The first row that ends at ZV must be the last to be
27448 highlighted. */
27449 else if (next->ends_at_zv_p)
27451 *end = next;
27452 break;
27458 /* This function sets the mouse_face_* elements of HLINFO, assuming
27459 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27460 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27461 for the overlay or run of text properties specifying the mouse
27462 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27463 before-string and after-string that must also be highlighted.
27464 DISP_STRING, if non-nil, is a display string that may cover some
27465 or all of the highlighted text. */
27467 static void
27468 mouse_face_from_buffer_pos (Lisp_Object window,
27469 Mouse_HLInfo *hlinfo,
27470 ptrdiff_t mouse_charpos,
27471 ptrdiff_t start_charpos,
27472 ptrdiff_t end_charpos,
27473 Lisp_Object before_string,
27474 Lisp_Object after_string,
27475 Lisp_Object disp_string)
27477 struct window *w = XWINDOW (window);
27478 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27479 struct glyph_row *r1, *r2;
27480 struct glyph *glyph, *end;
27481 ptrdiff_t ignore, pos;
27482 int x;
27484 eassert (NILP (disp_string) || STRINGP (disp_string));
27485 eassert (NILP (before_string) || STRINGP (before_string));
27486 eassert (NILP (after_string) || STRINGP (after_string));
27488 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27489 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27490 if (r1 == NULL)
27491 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27492 /* If the before-string or display-string contains newlines,
27493 rows_from_pos_range skips to its last row. Move back. */
27494 if (!NILP (before_string) || !NILP (disp_string))
27496 struct glyph_row *prev;
27497 while ((prev = r1 - 1, prev >= first)
27498 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27499 && prev->used[TEXT_AREA] > 0)
27501 struct glyph *beg = prev->glyphs[TEXT_AREA];
27502 glyph = beg + prev->used[TEXT_AREA];
27503 while (--glyph >= beg && INTEGERP (glyph->object));
27504 if (glyph < beg
27505 || !(EQ (glyph->object, before_string)
27506 || EQ (glyph->object, disp_string)))
27507 break;
27508 r1 = prev;
27511 if (r2 == NULL)
27513 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27514 hlinfo->mouse_face_past_end = 1;
27516 else if (!NILP (after_string))
27518 /* If the after-string has newlines, advance to its last row. */
27519 struct glyph_row *next;
27520 struct glyph_row *last
27521 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27523 for (next = r2 + 1;
27524 next <= last
27525 && next->used[TEXT_AREA] > 0
27526 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27527 ++next)
27528 r2 = next;
27530 /* The rest of the display engine assumes that mouse_face_beg_row is
27531 either above mouse_face_end_row or identical to it. But with
27532 bidi-reordered continued lines, the row for START_CHARPOS could
27533 be below the row for END_CHARPOS. If so, swap the rows and store
27534 them in correct order. */
27535 if (r1->y > r2->y)
27537 struct glyph_row *tem = r2;
27539 r2 = r1;
27540 r1 = tem;
27543 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27544 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27546 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27547 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27548 could be anywhere in the row and in any order. The strategy
27549 below is to find the leftmost and the rightmost glyph that
27550 belongs to either of these 3 strings, or whose position is
27551 between START_CHARPOS and END_CHARPOS, and highlight all the
27552 glyphs between those two. This may cover more than just the text
27553 between START_CHARPOS and END_CHARPOS if the range of characters
27554 strides the bidi level boundary, e.g. if the beginning is in R2L
27555 text while the end is in L2R text or vice versa. */
27556 if (!r1->reversed_p)
27558 /* This row is in a left to right paragraph. Scan it left to
27559 right. */
27560 glyph = r1->glyphs[TEXT_AREA];
27561 end = glyph + r1->used[TEXT_AREA];
27562 x = r1->x;
27564 /* Skip truncation glyphs at the start of the glyph row. */
27565 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27566 for (; glyph < end
27567 && INTEGERP (glyph->object)
27568 && glyph->charpos < 0;
27569 ++glyph)
27570 x += glyph->pixel_width;
27572 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27573 or DISP_STRING, and the first glyph from buffer whose
27574 position is between START_CHARPOS and END_CHARPOS. */
27575 for (; glyph < end
27576 && !INTEGERP (glyph->object)
27577 && !EQ (glyph->object, disp_string)
27578 && !(BUFFERP (glyph->object)
27579 && (glyph->charpos >= start_charpos
27580 && glyph->charpos < end_charpos));
27581 ++glyph)
27583 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27584 are present at buffer positions between START_CHARPOS and
27585 END_CHARPOS, or if they come from an overlay. */
27586 if (EQ (glyph->object, before_string))
27588 pos = string_buffer_position (before_string,
27589 start_charpos);
27590 /* If pos == 0, it means before_string came from an
27591 overlay, not from a buffer position. */
27592 if (!pos || (pos >= start_charpos && pos < end_charpos))
27593 break;
27595 else if (EQ (glyph->object, after_string))
27597 pos = string_buffer_position (after_string, end_charpos);
27598 if (!pos || (pos >= start_charpos && pos < end_charpos))
27599 break;
27601 x += glyph->pixel_width;
27603 hlinfo->mouse_face_beg_x = x;
27604 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27606 else
27608 /* This row is in a right to left paragraph. Scan it right to
27609 left. */
27610 struct glyph *g;
27612 end = r1->glyphs[TEXT_AREA] - 1;
27613 glyph = end + r1->used[TEXT_AREA];
27615 /* Skip truncation glyphs at the start of the glyph row. */
27616 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27617 for (; glyph > end
27618 && INTEGERP (glyph->object)
27619 && glyph->charpos < 0;
27620 --glyph)
27623 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27624 or DISP_STRING, and the first glyph from buffer whose
27625 position is between START_CHARPOS and END_CHARPOS. */
27626 for (; glyph > end
27627 && !INTEGERP (glyph->object)
27628 && !EQ (glyph->object, disp_string)
27629 && !(BUFFERP (glyph->object)
27630 && (glyph->charpos >= start_charpos
27631 && glyph->charpos < end_charpos));
27632 --glyph)
27634 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27635 are present at buffer positions between START_CHARPOS and
27636 END_CHARPOS, or if they come from an overlay. */
27637 if (EQ (glyph->object, before_string))
27639 pos = string_buffer_position (before_string, start_charpos);
27640 /* If pos == 0, it means before_string came from an
27641 overlay, not from a buffer position. */
27642 if (!pos || (pos >= start_charpos && pos < end_charpos))
27643 break;
27645 else if (EQ (glyph->object, after_string))
27647 pos = string_buffer_position (after_string, end_charpos);
27648 if (!pos || (pos >= start_charpos && pos < end_charpos))
27649 break;
27653 glyph++; /* first glyph to the right of the highlighted area */
27654 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27655 x += g->pixel_width;
27656 hlinfo->mouse_face_beg_x = x;
27657 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27660 /* If the highlight ends in a different row, compute GLYPH and END
27661 for the end row. Otherwise, reuse the values computed above for
27662 the row where the highlight begins. */
27663 if (r2 != r1)
27665 if (!r2->reversed_p)
27667 glyph = r2->glyphs[TEXT_AREA];
27668 end = glyph + r2->used[TEXT_AREA];
27669 x = r2->x;
27671 else
27673 end = r2->glyphs[TEXT_AREA] - 1;
27674 glyph = end + r2->used[TEXT_AREA];
27678 if (!r2->reversed_p)
27680 /* Skip truncation and continuation glyphs near the end of the
27681 row, and also blanks and stretch glyphs inserted by
27682 extend_face_to_end_of_line. */
27683 while (end > glyph
27684 && INTEGERP ((end - 1)->object))
27685 --end;
27686 /* Scan the rest of the glyph row from the end, looking for the
27687 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27688 DISP_STRING, or whose position is between START_CHARPOS
27689 and END_CHARPOS */
27690 for (--end;
27691 end > glyph
27692 && !INTEGERP (end->object)
27693 && !EQ (end->object, disp_string)
27694 && !(BUFFERP (end->object)
27695 && (end->charpos >= start_charpos
27696 && end->charpos < end_charpos));
27697 --end)
27699 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27700 are present at buffer positions between START_CHARPOS and
27701 END_CHARPOS, or if they come from an overlay. */
27702 if (EQ (end->object, before_string))
27704 pos = string_buffer_position (before_string, start_charpos);
27705 if (!pos || (pos >= start_charpos && pos < end_charpos))
27706 break;
27708 else if (EQ (end->object, after_string))
27710 pos = string_buffer_position (after_string, end_charpos);
27711 if (!pos || (pos >= start_charpos && pos < end_charpos))
27712 break;
27715 /* Find the X coordinate of the last glyph to be highlighted. */
27716 for (; glyph <= end; ++glyph)
27717 x += glyph->pixel_width;
27719 hlinfo->mouse_face_end_x = x;
27720 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27722 else
27724 /* Skip truncation and continuation glyphs near the end of the
27725 row, and also blanks and stretch glyphs inserted by
27726 extend_face_to_end_of_line. */
27727 x = r2->x;
27728 end++;
27729 while (end < glyph
27730 && INTEGERP (end->object))
27732 x += end->pixel_width;
27733 ++end;
27735 /* Scan the rest of the glyph row from the end, looking for the
27736 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27737 DISP_STRING, or whose position is between START_CHARPOS
27738 and END_CHARPOS */
27739 for ( ;
27740 end < glyph
27741 && !INTEGERP (end->object)
27742 && !EQ (end->object, disp_string)
27743 && !(BUFFERP (end->object)
27744 && (end->charpos >= start_charpos
27745 && end->charpos < end_charpos));
27746 ++end)
27748 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27749 are present at buffer positions between START_CHARPOS and
27750 END_CHARPOS, or if they come from an overlay. */
27751 if (EQ (end->object, before_string))
27753 pos = string_buffer_position (before_string, start_charpos);
27754 if (!pos || (pos >= start_charpos && pos < end_charpos))
27755 break;
27757 else if (EQ (end->object, after_string))
27759 pos = string_buffer_position (after_string, end_charpos);
27760 if (!pos || (pos >= start_charpos && pos < end_charpos))
27761 break;
27763 x += end->pixel_width;
27765 /* If we exited the above loop because we arrived at the last
27766 glyph of the row, and its buffer position is still not in
27767 range, it means the last character in range is the preceding
27768 newline. Bump the end column and x values to get past the
27769 last glyph. */
27770 if (end == glyph
27771 && BUFFERP (end->object)
27772 && (end->charpos < start_charpos
27773 || end->charpos >= end_charpos))
27775 x += end->pixel_width;
27776 ++end;
27778 hlinfo->mouse_face_end_x = x;
27779 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27782 hlinfo->mouse_face_window = window;
27783 hlinfo->mouse_face_face_id
27784 = face_at_buffer_position (w, mouse_charpos, &ignore,
27785 mouse_charpos + 1,
27786 !hlinfo->mouse_face_hidden, -1);
27787 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27790 /* The following function is not used anymore (replaced with
27791 mouse_face_from_string_pos), but I leave it here for the time
27792 being, in case someone would. */
27794 #if 0 /* not used */
27796 /* Find the position of the glyph for position POS in OBJECT in
27797 window W's current matrix, and return in *X, *Y the pixel
27798 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27800 RIGHT_P non-zero means return the position of the right edge of the
27801 glyph, RIGHT_P zero means return the left edge position.
27803 If no glyph for POS exists in the matrix, return the position of
27804 the glyph with the next smaller position that is in the matrix, if
27805 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27806 exists in the matrix, return the position of the glyph with the
27807 next larger position in OBJECT.
27809 Value is non-zero if a glyph was found. */
27811 static int
27812 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27813 int *hpos, int *vpos, int *x, int *y, int right_p)
27815 int yb = window_text_bottom_y (w);
27816 struct glyph_row *r;
27817 struct glyph *best_glyph = NULL;
27818 struct glyph_row *best_row = NULL;
27819 int best_x = 0;
27821 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27822 r->enabled_p && r->y < yb;
27823 ++r)
27825 struct glyph *g = r->glyphs[TEXT_AREA];
27826 struct glyph *e = g + r->used[TEXT_AREA];
27827 int gx;
27829 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27830 if (EQ (g->object, object))
27832 if (g->charpos == pos)
27834 best_glyph = g;
27835 best_x = gx;
27836 best_row = r;
27837 goto found;
27839 else if (best_glyph == NULL
27840 || ((eabs (g->charpos - pos)
27841 < eabs (best_glyph->charpos - pos))
27842 && (right_p
27843 ? g->charpos < pos
27844 : g->charpos > pos)))
27846 best_glyph = g;
27847 best_x = gx;
27848 best_row = r;
27853 found:
27855 if (best_glyph)
27857 *x = best_x;
27858 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27860 if (right_p)
27862 *x += best_glyph->pixel_width;
27863 ++*hpos;
27866 *y = best_row->y;
27867 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27870 return best_glyph != NULL;
27872 #endif /* not used */
27874 /* Find the positions of the first and the last glyphs in window W's
27875 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27876 (assumed to be a string), and return in HLINFO's mouse_face_*
27877 members the pixel and column/row coordinates of those glyphs. */
27879 static void
27880 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27881 Lisp_Object object,
27882 ptrdiff_t startpos, ptrdiff_t endpos)
27884 int yb = window_text_bottom_y (w);
27885 struct glyph_row *r;
27886 struct glyph *g, *e;
27887 int gx;
27888 int found = 0;
27890 /* Find the glyph row with at least one position in the range
27891 [STARTPOS..ENDPOS), and the first glyph in that row whose
27892 position belongs to that range. */
27893 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27894 r->enabled_p && r->y < yb;
27895 ++r)
27897 if (!r->reversed_p)
27899 g = r->glyphs[TEXT_AREA];
27900 e = g + r->used[TEXT_AREA];
27901 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27902 if (EQ (g->object, object)
27903 && startpos <= g->charpos && g->charpos < endpos)
27905 hlinfo->mouse_face_beg_row
27906 = MATRIX_ROW_VPOS (r, w->current_matrix);
27907 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27908 hlinfo->mouse_face_beg_x = gx;
27909 found = 1;
27910 break;
27913 else
27915 struct glyph *g1;
27917 e = r->glyphs[TEXT_AREA];
27918 g = e + r->used[TEXT_AREA];
27919 for ( ; g > e; --g)
27920 if (EQ ((g-1)->object, object)
27921 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27923 hlinfo->mouse_face_beg_row
27924 = MATRIX_ROW_VPOS (r, w->current_matrix);
27925 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27926 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27927 gx += g1->pixel_width;
27928 hlinfo->mouse_face_beg_x = gx;
27929 found = 1;
27930 break;
27933 if (found)
27934 break;
27937 if (!found)
27938 return;
27940 /* Starting with the next row, look for the first row which does NOT
27941 include any glyphs whose positions are in the range. */
27942 for (++r; r->enabled_p && r->y < yb; ++r)
27944 g = r->glyphs[TEXT_AREA];
27945 e = g + r->used[TEXT_AREA];
27946 found = 0;
27947 for ( ; g < e; ++g)
27948 if (EQ (g->object, object)
27949 && startpos <= g->charpos && g->charpos < endpos)
27951 found = 1;
27952 break;
27954 if (!found)
27955 break;
27958 /* The highlighted region ends on the previous row. */
27959 r--;
27961 /* Set the end row. */
27962 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27964 /* Compute and set the end column and the end column's horizontal
27965 pixel coordinate. */
27966 if (!r->reversed_p)
27968 g = r->glyphs[TEXT_AREA];
27969 e = g + r->used[TEXT_AREA];
27970 for ( ; e > g; --e)
27971 if (EQ ((e-1)->object, object)
27972 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27973 break;
27974 hlinfo->mouse_face_end_col = e - g;
27976 for (gx = r->x; g < e; ++g)
27977 gx += g->pixel_width;
27978 hlinfo->mouse_face_end_x = gx;
27980 else
27982 e = r->glyphs[TEXT_AREA];
27983 g = e + r->used[TEXT_AREA];
27984 for (gx = r->x ; e < g; ++e)
27986 if (EQ (e->object, object)
27987 && startpos <= e->charpos && e->charpos < endpos)
27988 break;
27989 gx += e->pixel_width;
27991 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27992 hlinfo->mouse_face_end_x = gx;
27996 #ifdef HAVE_WINDOW_SYSTEM
27998 /* See if position X, Y is within a hot-spot of an image. */
28000 static int
28001 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28003 if (!CONSP (hot_spot))
28004 return 0;
28006 if (EQ (XCAR (hot_spot), Qrect))
28008 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28009 Lisp_Object rect = XCDR (hot_spot);
28010 Lisp_Object tem;
28011 if (!CONSP (rect))
28012 return 0;
28013 if (!CONSP (XCAR (rect)))
28014 return 0;
28015 if (!CONSP (XCDR (rect)))
28016 return 0;
28017 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28018 return 0;
28019 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28020 return 0;
28021 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28022 return 0;
28023 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28024 return 0;
28025 return 1;
28027 else if (EQ (XCAR (hot_spot), Qcircle))
28029 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28030 Lisp_Object circ = XCDR (hot_spot);
28031 Lisp_Object lr, lx0, ly0;
28032 if (CONSP (circ)
28033 && CONSP (XCAR (circ))
28034 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28035 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28036 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28038 double r = XFLOATINT (lr);
28039 double dx = XINT (lx0) - x;
28040 double dy = XINT (ly0) - y;
28041 return (dx * dx + dy * dy <= r * r);
28044 else if (EQ (XCAR (hot_spot), Qpoly))
28046 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28047 if (VECTORP (XCDR (hot_spot)))
28049 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28050 Lisp_Object *poly = v->contents;
28051 ptrdiff_t n = v->header.size;
28052 ptrdiff_t i;
28053 int inside = 0;
28054 Lisp_Object lx, ly;
28055 int x0, y0;
28057 /* Need an even number of coordinates, and at least 3 edges. */
28058 if (n < 6 || n & 1)
28059 return 0;
28061 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28062 If count is odd, we are inside polygon. Pixels on edges
28063 may or may not be included depending on actual geometry of the
28064 polygon. */
28065 if ((lx = poly[n-2], !INTEGERP (lx))
28066 || (ly = poly[n-1], !INTEGERP (lx)))
28067 return 0;
28068 x0 = XINT (lx), y0 = XINT (ly);
28069 for (i = 0; i < n; i += 2)
28071 int x1 = x0, y1 = y0;
28072 if ((lx = poly[i], !INTEGERP (lx))
28073 || (ly = poly[i+1], !INTEGERP (ly)))
28074 return 0;
28075 x0 = XINT (lx), y0 = XINT (ly);
28077 /* Does this segment cross the X line? */
28078 if (x0 >= x)
28080 if (x1 >= x)
28081 continue;
28083 else if (x1 < x)
28084 continue;
28085 if (y > y0 && y > y1)
28086 continue;
28087 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28088 inside = !inside;
28090 return inside;
28093 return 0;
28096 Lisp_Object
28097 find_hot_spot (Lisp_Object map, int x, int y)
28099 while (CONSP (map))
28101 if (CONSP (XCAR (map))
28102 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28103 return XCAR (map);
28104 map = XCDR (map);
28107 return Qnil;
28110 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28111 3, 3, 0,
28112 doc: /* Lookup in image map MAP coordinates X and Y.
28113 An image map is an alist where each element has the format (AREA ID PLIST).
28114 An AREA is specified as either a rectangle, a circle, or a polygon:
28115 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28116 pixel coordinates of the upper left and bottom right corners.
28117 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28118 and the radius of the circle; r may be a float or integer.
28119 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28120 vector describes one corner in the polygon.
28121 Returns the alist element for the first matching AREA in MAP. */)
28122 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28124 if (NILP (map))
28125 return Qnil;
28127 CHECK_NUMBER (x);
28128 CHECK_NUMBER (y);
28130 return find_hot_spot (map,
28131 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28132 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28136 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28137 static void
28138 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28140 /* Do not change cursor shape while dragging mouse. */
28141 if (!NILP (do_mouse_tracking))
28142 return;
28144 if (!NILP (pointer))
28146 if (EQ (pointer, Qarrow))
28147 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28148 else if (EQ (pointer, Qhand))
28149 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28150 else if (EQ (pointer, Qtext))
28151 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28152 else if (EQ (pointer, intern ("hdrag")))
28153 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28154 else if (EQ (pointer, intern ("nhdrag")))
28155 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28156 #ifdef HAVE_X_WINDOWS
28157 else if (EQ (pointer, intern ("vdrag")))
28158 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28159 #endif
28160 else if (EQ (pointer, intern ("hourglass")))
28161 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28162 else if (EQ (pointer, Qmodeline))
28163 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28164 else
28165 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28168 if (cursor != No_Cursor)
28169 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28172 #endif /* HAVE_WINDOW_SYSTEM */
28174 /* Take proper action when mouse has moved to the mode or header line
28175 or marginal area AREA of window W, x-position X and y-position Y.
28176 X is relative to the start of the text display area of W, so the
28177 width of bitmap areas and scroll bars must be subtracted to get a
28178 position relative to the start of the mode line. */
28180 static void
28181 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28182 enum window_part area)
28184 struct window *w = XWINDOW (window);
28185 struct frame *f = XFRAME (w->frame);
28186 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28187 #ifdef HAVE_WINDOW_SYSTEM
28188 Display_Info *dpyinfo;
28189 #endif
28190 Cursor cursor = No_Cursor;
28191 Lisp_Object pointer = Qnil;
28192 int dx, dy, width, height;
28193 ptrdiff_t charpos;
28194 Lisp_Object string, object = Qnil;
28195 Lisp_Object pos IF_LINT (= Qnil), help;
28197 Lisp_Object mouse_face;
28198 int original_x_pixel = x;
28199 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28200 struct glyph_row *row IF_LINT (= 0);
28202 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28204 int x0;
28205 struct glyph *end;
28207 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28208 returns them in row/column units! */
28209 string = mode_line_string (w, area, &x, &y, &charpos,
28210 &object, &dx, &dy, &width, &height);
28212 row = (area == ON_MODE_LINE
28213 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28214 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28216 /* Find the glyph under the mouse pointer. */
28217 if (row->mode_line_p && row->enabled_p)
28219 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28220 end = glyph + row->used[TEXT_AREA];
28222 for (x0 = original_x_pixel;
28223 glyph < end && x0 >= glyph->pixel_width;
28224 ++glyph)
28225 x0 -= glyph->pixel_width;
28227 if (glyph >= end)
28228 glyph = NULL;
28231 else
28233 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28234 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28235 returns them in row/column units! */
28236 string = marginal_area_string (w, area, &x, &y, &charpos,
28237 &object, &dx, &dy, &width, &height);
28240 help = Qnil;
28242 #ifdef HAVE_WINDOW_SYSTEM
28243 if (IMAGEP (object))
28245 Lisp_Object image_map, hotspot;
28246 if ((image_map = Fplist_get (XCDR (object), QCmap),
28247 !NILP (image_map))
28248 && (hotspot = find_hot_spot (image_map, dx, dy),
28249 CONSP (hotspot))
28250 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28252 Lisp_Object plist;
28254 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28255 If so, we could look for mouse-enter, mouse-leave
28256 properties in PLIST (and do something...). */
28257 hotspot = XCDR (hotspot);
28258 if (CONSP (hotspot)
28259 && (plist = XCAR (hotspot), CONSP (plist)))
28261 pointer = Fplist_get (plist, Qpointer);
28262 if (NILP (pointer))
28263 pointer = Qhand;
28264 help = Fplist_get (plist, Qhelp_echo);
28265 if (!NILP (help))
28267 help_echo_string = help;
28268 XSETWINDOW (help_echo_window, w);
28269 help_echo_object = w->contents;
28270 help_echo_pos = charpos;
28274 if (NILP (pointer))
28275 pointer = Fplist_get (XCDR (object), QCpointer);
28277 #endif /* HAVE_WINDOW_SYSTEM */
28279 if (STRINGP (string))
28280 pos = make_number (charpos);
28282 /* Set the help text and mouse pointer. If the mouse is on a part
28283 of the mode line without any text (e.g. past the right edge of
28284 the mode line text), use the default help text and pointer. */
28285 if (STRINGP (string) || area == ON_MODE_LINE)
28287 /* Arrange to display the help by setting the global variables
28288 help_echo_string, help_echo_object, and help_echo_pos. */
28289 if (NILP (help))
28291 if (STRINGP (string))
28292 help = Fget_text_property (pos, Qhelp_echo, string);
28294 if (!NILP (help))
28296 help_echo_string = help;
28297 XSETWINDOW (help_echo_window, w);
28298 help_echo_object = string;
28299 help_echo_pos = charpos;
28301 else if (area == ON_MODE_LINE)
28303 Lisp_Object default_help
28304 = buffer_local_value_1 (Qmode_line_default_help_echo,
28305 w->contents);
28307 if (STRINGP (default_help))
28309 help_echo_string = default_help;
28310 XSETWINDOW (help_echo_window, w);
28311 help_echo_object = Qnil;
28312 help_echo_pos = -1;
28317 #ifdef HAVE_WINDOW_SYSTEM
28318 /* Change the mouse pointer according to what is under it. */
28319 if (FRAME_WINDOW_P (f))
28321 dpyinfo = FRAME_DISPLAY_INFO (f);
28322 if (STRINGP (string))
28324 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28326 if (NILP (pointer))
28327 pointer = Fget_text_property (pos, Qpointer, string);
28329 /* Change the mouse pointer according to what is under X/Y. */
28330 if (NILP (pointer)
28331 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28333 Lisp_Object map;
28334 map = Fget_text_property (pos, Qlocal_map, string);
28335 if (!KEYMAPP (map))
28336 map = Fget_text_property (pos, Qkeymap, string);
28337 if (!KEYMAPP (map))
28338 cursor = dpyinfo->vertical_scroll_bar_cursor;
28341 else
28342 /* Default mode-line pointer. */
28343 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28345 #endif
28348 /* Change the mouse face according to what is under X/Y. */
28349 if (STRINGP (string))
28351 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28352 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28353 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28354 && glyph)
28356 Lisp_Object b, e;
28358 struct glyph * tmp_glyph;
28360 int gpos;
28361 int gseq_length;
28362 int total_pixel_width;
28363 ptrdiff_t begpos, endpos, ignore;
28365 int vpos, hpos;
28367 b = Fprevious_single_property_change (make_number (charpos + 1),
28368 Qmouse_face, string, Qnil);
28369 if (NILP (b))
28370 begpos = 0;
28371 else
28372 begpos = XINT (b);
28374 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28375 if (NILP (e))
28376 endpos = SCHARS (string);
28377 else
28378 endpos = XINT (e);
28380 /* Calculate the glyph position GPOS of GLYPH in the
28381 displayed string, relative to the beginning of the
28382 highlighted part of the string.
28384 Note: GPOS is different from CHARPOS. CHARPOS is the
28385 position of GLYPH in the internal string object. A mode
28386 line string format has structures which are converted to
28387 a flattened string by the Emacs Lisp interpreter. The
28388 internal string is an element of those structures. The
28389 displayed string is the flattened string. */
28390 tmp_glyph = row_start_glyph;
28391 while (tmp_glyph < glyph
28392 && (!(EQ (tmp_glyph->object, glyph->object)
28393 && begpos <= tmp_glyph->charpos
28394 && tmp_glyph->charpos < endpos)))
28395 tmp_glyph++;
28396 gpos = glyph - tmp_glyph;
28398 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28399 the highlighted part of the displayed string to which
28400 GLYPH belongs. Note: GSEQ_LENGTH is different from
28401 SCHARS (STRING), because the latter returns the length of
28402 the internal string. */
28403 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28404 tmp_glyph > glyph
28405 && (!(EQ (tmp_glyph->object, glyph->object)
28406 && begpos <= tmp_glyph->charpos
28407 && tmp_glyph->charpos < endpos));
28408 tmp_glyph--)
28410 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28412 /* Calculate the total pixel width of all the glyphs between
28413 the beginning of the highlighted area and GLYPH. */
28414 total_pixel_width = 0;
28415 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28416 total_pixel_width += tmp_glyph->pixel_width;
28418 /* Pre calculation of re-rendering position. Note: X is in
28419 column units here, after the call to mode_line_string or
28420 marginal_area_string. */
28421 hpos = x - gpos;
28422 vpos = (area == ON_MODE_LINE
28423 ? (w->current_matrix)->nrows - 1
28424 : 0);
28426 /* If GLYPH's position is included in the region that is
28427 already drawn in mouse face, we have nothing to do. */
28428 if ( EQ (window, hlinfo->mouse_face_window)
28429 && (!row->reversed_p
28430 ? (hlinfo->mouse_face_beg_col <= hpos
28431 && hpos < hlinfo->mouse_face_end_col)
28432 /* In R2L rows we swap BEG and END, see below. */
28433 : (hlinfo->mouse_face_end_col <= hpos
28434 && hpos < hlinfo->mouse_face_beg_col))
28435 && hlinfo->mouse_face_beg_row == vpos )
28436 return;
28438 if (clear_mouse_face (hlinfo))
28439 cursor = No_Cursor;
28441 if (!row->reversed_p)
28443 hlinfo->mouse_face_beg_col = hpos;
28444 hlinfo->mouse_face_beg_x = original_x_pixel
28445 - (total_pixel_width + dx);
28446 hlinfo->mouse_face_end_col = hpos + gseq_length;
28447 hlinfo->mouse_face_end_x = 0;
28449 else
28451 /* In R2L rows, show_mouse_face expects BEG and END
28452 coordinates to be swapped. */
28453 hlinfo->mouse_face_end_col = hpos;
28454 hlinfo->mouse_face_end_x = original_x_pixel
28455 - (total_pixel_width + dx);
28456 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28457 hlinfo->mouse_face_beg_x = 0;
28460 hlinfo->mouse_face_beg_row = vpos;
28461 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28462 hlinfo->mouse_face_past_end = 0;
28463 hlinfo->mouse_face_window = window;
28465 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28466 charpos,
28467 0, &ignore,
28468 glyph->face_id,
28470 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28472 if (NILP (pointer))
28473 pointer = Qhand;
28475 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28476 clear_mouse_face (hlinfo);
28478 #ifdef HAVE_WINDOW_SYSTEM
28479 if (FRAME_WINDOW_P (f))
28480 define_frame_cursor1 (f, cursor, pointer);
28481 #endif
28485 /* EXPORT:
28486 Take proper action when the mouse has moved to position X, Y on
28487 frame F with regards to highlighting portions of display that have
28488 mouse-face properties. Also de-highlight portions of display where
28489 the mouse was before, set the mouse pointer shape as appropriate
28490 for the mouse coordinates, and activate help echo (tooltips).
28491 X and Y can be negative or out of range. */
28493 void
28494 note_mouse_highlight (struct frame *f, int x, int y)
28496 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28497 enum window_part part = ON_NOTHING;
28498 Lisp_Object window;
28499 struct window *w;
28500 Cursor cursor = No_Cursor;
28501 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28502 struct buffer *b;
28504 /* When a menu is active, don't highlight because this looks odd. */
28505 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28506 if (popup_activated ())
28507 return;
28508 #endif
28510 if (!f->glyphs_initialized_p
28511 || f->pointer_invisible)
28512 return;
28514 hlinfo->mouse_face_mouse_x = x;
28515 hlinfo->mouse_face_mouse_y = y;
28516 hlinfo->mouse_face_mouse_frame = f;
28518 if (hlinfo->mouse_face_defer)
28519 return;
28521 /* Which window is that in? */
28522 window = window_from_coordinates (f, x, y, &part, 1);
28524 /* If displaying active text in another window, clear that. */
28525 if (! EQ (window, hlinfo->mouse_face_window)
28526 /* Also clear if we move out of text area in same window. */
28527 || (!NILP (hlinfo->mouse_face_window)
28528 && !NILP (window)
28529 && part != ON_TEXT
28530 && part != ON_MODE_LINE
28531 && part != ON_HEADER_LINE))
28532 clear_mouse_face (hlinfo);
28534 /* Not on a window -> return. */
28535 if (!WINDOWP (window))
28536 return;
28538 /* Reset help_echo_string. It will get recomputed below. */
28539 help_echo_string = Qnil;
28541 /* Convert to window-relative pixel coordinates. */
28542 w = XWINDOW (window);
28543 frame_to_window_pixel_xy (w, &x, &y);
28545 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28546 /* Handle tool-bar window differently since it doesn't display a
28547 buffer. */
28548 if (EQ (window, f->tool_bar_window))
28550 note_tool_bar_highlight (f, x, y);
28551 return;
28553 #endif
28555 /* Mouse is on the mode, header line or margin? */
28556 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28557 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28559 note_mode_line_or_margin_highlight (window, x, y, part);
28560 return;
28563 #ifdef HAVE_WINDOW_SYSTEM
28564 if (part == ON_VERTICAL_BORDER)
28566 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28567 help_echo_string = build_string ("drag-mouse-1: resize");
28569 else if (part == ON_RIGHT_DIVIDER)
28571 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28572 help_echo_string = build_string ("drag-mouse-1: resize");
28574 else if (part == ON_BOTTOM_DIVIDER)
28576 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28577 help_echo_string = build_string ("drag-mouse-1: resize");
28579 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28580 || part == ON_SCROLL_BAR)
28581 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28582 else
28583 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28584 #endif
28586 /* Are we in a window whose display is up to date?
28587 And verify the buffer's text has not changed. */
28588 b = XBUFFER (w->contents);
28589 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28591 int hpos, vpos, dx, dy, area = LAST_AREA;
28592 ptrdiff_t pos;
28593 struct glyph *glyph;
28594 Lisp_Object object;
28595 Lisp_Object mouse_face = Qnil, position;
28596 Lisp_Object *overlay_vec = NULL;
28597 ptrdiff_t i, noverlays;
28598 struct buffer *obuf;
28599 ptrdiff_t obegv, ozv;
28600 int same_region;
28602 /* Find the glyph under X/Y. */
28603 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28605 #ifdef HAVE_WINDOW_SYSTEM
28606 /* Look for :pointer property on image. */
28607 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28609 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28610 if (img != NULL && IMAGEP (img->spec))
28612 Lisp_Object image_map, hotspot;
28613 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28614 !NILP (image_map))
28615 && (hotspot = find_hot_spot (image_map,
28616 glyph->slice.img.x + dx,
28617 glyph->slice.img.y + dy),
28618 CONSP (hotspot))
28619 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28621 Lisp_Object plist;
28623 /* Could check XCAR (hotspot) to see if we enter/leave
28624 this hot-spot.
28625 If so, we could look for mouse-enter, mouse-leave
28626 properties in PLIST (and do something...). */
28627 hotspot = XCDR (hotspot);
28628 if (CONSP (hotspot)
28629 && (plist = XCAR (hotspot), CONSP (plist)))
28631 pointer = Fplist_get (plist, Qpointer);
28632 if (NILP (pointer))
28633 pointer = Qhand;
28634 help_echo_string = Fplist_get (plist, Qhelp_echo);
28635 if (!NILP (help_echo_string))
28637 help_echo_window = window;
28638 help_echo_object = glyph->object;
28639 help_echo_pos = glyph->charpos;
28643 if (NILP (pointer))
28644 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28647 #endif /* HAVE_WINDOW_SYSTEM */
28649 /* Clear mouse face if X/Y not over text. */
28650 if (glyph == NULL
28651 || area != TEXT_AREA
28652 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28653 /* Glyph's OBJECT is an integer for glyphs inserted by the
28654 display engine for its internal purposes, like truncation
28655 and continuation glyphs and blanks beyond the end of
28656 line's text on text terminals. If we are over such a
28657 glyph, we are not over any text. */
28658 || INTEGERP (glyph->object)
28659 /* R2L rows have a stretch glyph at their front, which
28660 stands for no text, whereas L2R rows have no glyphs at
28661 all beyond the end of text. Treat such stretch glyphs
28662 like we do with NULL glyphs in L2R rows. */
28663 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28664 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28665 && glyph->type == STRETCH_GLYPH
28666 && glyph->avoid_cursor_p))
28668 if (clear_mouse_face (hlinfo))
28669 cursor = No_Cursor;
28670 #ifdef HAVE_WINDOW_SYSTEM
28671 if (FRAME_WINDOW_P (f) && NILP (pointer))
28673 if (area != TEXT_AREA)
28674 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28675 else
28676 pointer = Vvoid_text_area_pointer;
28678 #endif
28679 goto set_cursor;
28682 pos = glyph->charpos;
28683 object = glyph->object;
28684 if (!STRINGP (object) && !BUFFERP (object))
28685 goto set_cursor;
28687 /* If we get an out-of-range value, return now; avoid an error. */
28688 if (BUFFERP (object) && pos > BUF_Z (b))
28689 goto set_cursor;
28691 /* Make the window's buffer temporarily current for
28692 overlays_at and compute_char_face. */
28693 obuf = current_buffer;
28694 current_buffer = b;
28695 obegv = BEGV;
28696 ozv = ZV;
28697 BEGV = BEG;
28698 ZV = Z;
28700 /* Is this char mouse-active or does it have help-echo? */
28701 position = make_number (pos);
28703 if (BUFFERP (object))
28705 /* Put all the overlays we want in a vector in overlay_vec. */
28706 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28707 /* Sort overlays into increasing priority order. */
28708 noverlays = sort_overlays (overlay_vec, noverlays, w);
28710 else
28711 noverlays = 0;
28713 if (NILP (Vmouse_highlight))
28715 clear_mouse_face (hlinfo);
28716 goto check_help_echo;
28719 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28721 if (same_region)
28722 cursor = No_Cursor;
28724 /* Check mouse-face highlighting. */
28725 if (! same_region
28726 /* If there exists an overlay with mouse-face overlapping
28727 the one we are currently highlighting, we have to
28728 check if we enter the overlapping overlay, and then
28729 highlight only that. */
28730 || (OVERLAYP (hlinfo->mouse_face_overlay)
28731 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28733 /* Find the highest priority overlay with a mouse-face. */
28734 Lisp_Object overlay = Qnil;
28735 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28737 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28738 if (!NILP (mouse_face))
28739 overlay = overlay_vec[i];
28742 /* If we're highlighting the same overlay as before, there's
28743 no need to do that again. */
28744 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28745 goto check_help_echo;
28746 hlinfo->mouse_face_overlay = overlay;
28748 /* Clear the display of the old active region, if any. */
28749 if (clear_mouse_face (hlinfo))
28750 cursor = No_Cursor;
28752 /* If no overlay applies, get a text property. */
28753 if (NILP (overlay))
28754 mouse_face = Fget_text_property (position, Qmouse_face, object);
28756 /* Next, compute the bounds of the mouse highlighting and
28757 display it. */
28758 if (!NILP (mouse_face) && STRINGP (object))
28760 /* The mouse-highlighting comes from a display string
28761 with a mouse-face. */
28762 Lisp_Object s, e;
28763 ptrdiff_t ignore;
28765 s = Fprevious_single_property_change
28766 (make_number (pos + 1), Qmouse_face, object, Qnil);
28767 e = Fnext_single_property_change
28768 (position, Qmouse_face, object, Qnil);
28769 if (NILP (s))
28770 s = make_number (0);
28771 if (NILP (e))
28772 e = make_number (SCHARS (object));
28773 mouse_face_from_string_pos (w, hlinfo, object,
28774 XINT (s), XINT (e));
28775 hlinfo->mouse_face_past_end = 0;
28776 hlinfo->mouse_face_window = window;
28777 hlinfo->mouse_face_face_id
28778 = face_at_string_position (w, object, pos, 0, &ignore,
28779 glyph->face_id, 1);
28780 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28781 cursor = No_Cursor;
28783 else
28785 /* The mouse-highlighting, if any, comes from an overlay
28786 or text property in the buffer. */
28787 Lisp_Object buffer IF_LINT (= Qnil);
28788 Lisp_Object disp_string IF_LINT (= Qnil);
28790 if (STRINGP (object))
28792 /* If we are on a display string with no mouse-face,
28793 check if the text under it has one. */
28794 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28795 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28796 pos = string_buffer_position (object, start);
28797 if (pos > 0)
28799 mouse_face = get_char_property_and_overlay
28800 (make_number (pos), Qmouse_face, w->contents, &overlay);
28801 buffer = w->contents;
28802 disp_string = object;
28805 else
28807 buffer = object;
28808 disp_string = Qnil;
28811 if (!NILP (mouse_face))
28813 Lisp_Object before, after;
28814 Lisp_Object before_string, after_string;
28815 /* To correctly find the limits of mouse highlight
28816 in a bidi-reordered buffer, we must not use the
28817 optimization of limiting the search in
28818 previous-single-property-change and
28819 next-single-property-change, because
28820 rows_from_pos_range needs the real start and end
28821 positions to DTRT in this case. That's because
28822 the first row visible in a window does not
28823 necessarily display the character whose position
28824 is the smallest. */
28825 Lisp_Object lim1
28826 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28827 ? Fmarker_position (w->start)
28828 : Qnil;
28829 Lisp_Object lim2
28830 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28831 ? make_number (BUF_Z (XBUFFER (buffer))
28832 - w->window_end_pos)
28833 : Qnil;
28835 if (NILP (overlay))
28837 /* Handle the text property case. */
28838 before = Fprevious_single_property_change
28839 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28840 after = Fnext_single_property_change
28841 (make_number (pos), Qmouse_face, buffer, lim2);
28842 before_string = after_string = Qnil;
28844 else
28846 /* Handle the overlay case. */
28847 before = Foverlay_start (overlay);
28848 after = Foverlay_end (overlay);
28849 before_string = Foverlay_get (overlay, Qbefore_string);
28850 after_string = Foverlay_get (overlay, Qafter_string);
28852 if (!STRINGP (before_string)) before_string = Qnil;
28853 if (!STRINGP (after_string)) after_string = Qnil;
28856 mouse_face_from_buffer_pos (window, hlinfo, pos,
28857 NILP (before)
28859 : XFASTINT (before),
28860 NILP (after)
28861 ? BUF_Z (XBUFFER (buffer))
28862 : XFASTINT (after),
28863 before_string, after_string,
28864 disp_string);
28865 cursor = No_Cursor;
28870 check_help_echo:
28872 /* Look for a `help-echo' property. */
28873 if (NILP (help_echo_string)) {
28874 Lisp_Object help, overlay;
28876 /* Check overlays first. */
28877 help = overlay = Qnil;
28878 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28880 overlay = overlay_vec[i];
28881 help = Foverlay_get (overlay, Qhelp_echo);
28884 if (!NILP (help))
28886 help_echo_string = help;
28887 help_echo_window = window;
28888 help_echo_object = overlay;
28889 help_echo_pos = pos;
28891 else
28893 Lisp_Object obj = glyph->object;
28894 ptrdiff_t charpos = glyph->charpos;
28896 /* Try text properties. */
28897 if (STRINGP (obj)
28898 && charpos >= 0
28899 && charpos < SCHARS (obj))
28901 help = Fget_text_property (make_number (charpos),
28902 Qhelp_echo, obj);
28903 if (NILP (help))
28905 /* If the string itself doesn't specify a help-echo,
28906 see if the buffer text ``under'' it does. */
28907 struct glyph_row *r
28908 = MATRIX_ROW (w->current_matrix, vpos);
28909 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28910 ptrdiff_t p = string_buffer_position (obj, start);
28911 if (p > 0)
28913 help = Fget_char_property (make_number (p),
28914 Qhelp_echo, w->contents);
28915 if (!NILP (help))
28917 charpos = p;
28918 obj = w->contents;
28923 else if (BUFFERP (obj)
28924 && charpos >= BEGV
28925 && charpos < ZV)
28926 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28927 obj);
28929 if (!NILP (help))
28931 help_echo_string = help;
28932 help_echo_window = window;
28933 help_echo_object = obj;
28934 help_echo_pos = charpos;
28939 #ifdef HAVE_WINDOW_SYSTEM
28940 /* Look for a `pointer' property. */
28941 if (FRAME_WINDOW_P (f) && NILP (pointer))
28943 /* Check overlays first. */
28944 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28945 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28947 if (NILP (pointer))
28949 Lisp_Object obj = glyph->object;
28950 ptrdiff_t charpos = glyph->charpos;
28952 /* Try text properties. */
28953 if (STRINGP (obj)
28954 && charpos >= 0
28955 && charpos < SCHARS (obj))
28957 pointer = Fget_text_property (make_number (charpos),
28958 Qpointer, obj);
28959 if (NILP (pointer))
28961 /* If the string itself doesn't specify a pointer,
28962 see if the buffer text ``under'' it does. */
28963 struct glyph_row *r
28964 = MATRIX_ROW (w->current_matrix, vpos);
28965 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28966 ptrdiff_t p = string_buffer_position (obj, start);
28967 if (p > 0)
28968 pointer = Fget_char_property (make_number (p),
28969 Qpointer, w->contents);
28972 else if (BUFFERP (obj)
28973 && charpos >= BEGV
28974 && charpos < ZV)
28975 pointer = Fget_text_property (make_number (charpos),
28976 Qpointer, obj);
28979 #endif /* HAVE_WINDOW_SYSTEM */
28981 BEGV = obegv;
28982 ZV = ozv;
28983 current_buffer = obuf;
28986 set_cursor:
28988 #ifdef HAVE_WINDOW_SYSTEM
28989 if (FRAME_WINDOW_P (f))
28990 define_frame_cursor1 (f, cursor, pointer);
28991 #else
28992 /* This is here to prevent a compiler error, about "label at end of
28993 compound statement". */
28994 return;
28995 #endif
28999 /* EXPORT for RIF:
29000 Clear any mouse-face on window W. This function is part of the
29001 redisplay interface, and is called from try_window_id and similar
29002 functions to ensure the mouse-highlight is off. */
29004 void
29005 x_clear_window_mouse_face (struct window *w)
29007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29008 Lisp_Object window;
29010 block_input ();
29011 XSETWINDOW (window, w);
29012 if (EQ (window, hlinfo->mouse_face_window))
29013 clear_mouse_face (hlinfo);
29014 unblock_input ();
29018 /* EXPORT:
29019 Just discard the mouse face information for frame F, if any.
29020 This is used when the size of F is changed. */
29022 void
29023 cancel_mouse_face (struct frame *f)
29025 Lisp_Object window;
29026 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29028 window = hlinfo->mouse_face_window;
29029 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29030 reset_mouse_highlight (hlinfo);
29035 /***********************************************************************
29036 Exposure Events
29037 ***********************************************************************/
29039 #ifdef HAVE_WINDOW_SYSTEM
29041 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29042 which intersects rectangle R. R is in window-relative coordinates. */
29044 static void
29045 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29046 enum glyph_row_area area)
29048 struct glyph *first = row->glyphs[area];
29049 struct glyph *end = row->glyphs[area] + row->used[area];
29050 struct glyph *last;
29051 int first_x, start_x, x;
29053 if (area == TEXT_AREA && row->fill_line_p)
29054 /* If row extends face to end of line write the whole line. */
29055 draw_glyphs (w, 0, row, area,
29056 0, row->used[area],
29057 DRAW_NORMAL_TEXT, 0);
29058 else
29060 /* Set START_X to the window-relative start position for drawing glyphs of
29061 AREA. The first glyph of the text area can be partially visible.
29062 The first glyphs of other areas cannot. */
29063 start_x = window_box_left_offset (w, area);
29064 x = start_x;
29065 if (area == TEXT_AREA)
29066 x += row->x;
29068 /* Find the first glyph that must be redrawn. */
29069 while (first < end
29070 && x + first->pixel_width < r->x)
29072 x += first->pixel_width;
29073 ++first;
29076 /* Find the last one. */
29077 last = first;
29078 first_x = x;
29079 while (last < end
29080 && x < r->x + r->width)
29082 x += last->pixel_width;
29083 ++last;
29086 /* Repaint. */
29087 if (last > first)
29088 draw_glyphs (w, first_x - start_x, row, area,
29089 first - row->glyphs[area], last - row->glyphs[area],
29090 DRAW_NORMAL_TEXT, 0);
29095 /* Redraw the parts of the glyph row ROW on window W intersecting
29096 rectangle R. R is in window-relative coordinates. Value is
29097 non-zero if mouse-face was overwritten. */
29099 static int
29100 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29102 eassert (row->enabled_p);
29104 if (row->mode_line_p || w->pseudo_window_p)
29105 draw_glyphs (w, 0, row, TEXT_AREA,
29106 0, row->used[TEXT_AREA],
29107 DRAW_NORMAL_TEXT, 0);
29108 else
29110 if (row->used[LEFT_MARGIN_AREA])
29111 expose_area (w, row, r, LEFT_MARGIN_AREA);
29112 if (row->used[TEXT_AREA])
29113 expose_area (w, row, r, TEXT_AREA);
29114 if (row->used[RIGHT_MARGIN_AREA])
29115 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29116 draw_row_fringe_bitmaps (w, row);
29119 return row->mouse_face_p;
29123 /* Redraw those parts of glyphs rows during expose event handling that
29124 overlap other rows. Redrawing of an exposed line writes over parts
29125 of lines overlapping that exposed line; this function fixes that.
29127 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29128 row in W's current matrix that is exposed and overlaps other rows.
29129 LAST_OVERLAPPING_ROW is the last such row. */
29131 static void
29132 expose_overlaps (struct window *w,
29133 struct glyph_row *first_overlapping_row,
29134 struct glyph_row *last_overlapping_row,
29135 XRectangle *r)
29137 struct glyph_row *row;
29139 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29140 if (row->overlapping_p)
29142 eassert (row->enabled_p && !row->mode_line_p);
29144 row->clip = r;
29145 if (row->used[LEFT_MARGIN_AREA])
29146 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29148 if (row->used[TEXT_AREA])
29149 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29151 if (row->used[RIGHT_MARGIN_AREA])
29152 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29153 row->clip = NULL;
29158 /* Return non-zero if W's cursor intersects rectangle R. */
29160 static int
29161 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29163 XRectangle cr, result;
29164 struct glyph *cursor_glyph;
29165 struct glyph_row *row;
29167 if (w->phys_cursor.vpos >= 0
29168 && w->phys_cursor.vpos < w->current_matrix->nrows
29169 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29170 row->enabled_p)
29171 && row->cursor_in_fringe_p)
29173 /* Cursor is in the fringe. */
29174 cr.x = window_box_right_offset (w,
29175 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29176 ? RIGHT_MARGIN_AREA
29177 : TEXT_AREA));
29178 cr.y = row->y;
29179 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29180 cr.height = row->height;
29181 return x_intersect_rectangles (&cr, r, &result);
29184 cursor_glyph = get_phys_cursor_glyph (w);
29185 if (cursor_glyph)
29187 /* r is relative to W's box, but w->phys_cursor.x is relative
29188 to left edge of W's TEXT area. Adjust it. */
29189 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29190 cr.y = w->phys_cursor.y;
29191 cr.width = cursor_glyph->pixel_width;
29192 cr.height = w->phys_cursor_height;
29193 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29194 I assume the effect is the same -- and this is portable. */
29195 return x_intersect_rectangles (&cr, r, &result);
29197 /* If we don't understand the format, pretend we're not in the hot-spot. */
29198 return 0;
29202 /* EXPORT:
29203 Draw a vertical window border to the right of window W if W doesn't
29204 have vertical scroll bars. */
29206 void
29207 x_draw_vertical_border (struct window *w)
29209 struct frame *f = XFRAME (WINDOW_FRAME (w));
29211 /* We could do better, if we knew what type of scroll-bar the adjacent
29212 windows (on either side) have... But we don't :-(
29213 However, I think this works ok. ++KFS 2003-04-25 */
29215 /* Redraw borders between horizontally adjacent windows. Don't
29216 do it for frames with vertical scroll bars because either the
29217 right scroll bar of a window, or the left scroll bar of its
29218 neighbor will suffice as a border. */
29219 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29220 return;
29222 /* Note: It is necessary to redraw both the left and the right
29223 borders, for when only this single window W is being
29224 redisplayed. */
29225 if (!WINDOW_RIGHTMOST_P (w)
29226 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29228 int x0, x1, y0, y1;
29230 window_box_edges (w, &x0, &y0, &x1, &y1);
29231 y1 -= 1;
29233 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29234 x1 -= 1;
29236 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29239 if (!WINDOW_LEFTMOST_P (w)
29240 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29242 int x0, x1, y0, y1;
29244 window_box_edges (w, &x0, &y0, &x1, &y1);
29245 y1 -= 1;
29247 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29248 x0 -= 1;
29250 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29255 /* Draw window dividers for window W. */
29257 void
29258 x_draw_right_divider (struct window *w)
29260 struct frame *f = WINDOW_XFRAME (w);
29262 if (w->mini || w->pseudo_window_p)
29263 return;
29264 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29266 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29267 int x1 = WINDOW_RIGHT_EDGE_X (w);
29268 int y0 = WINDOW_TOP_EDGE_Y (w);
29269 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29271 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29275 static void
29276 x_draw_bottom_divider (struct window *w)
29278 struct frame *f = XFRAME (WINDOW_FRAME (w));
29280 if (w->mini || w->pseudo_window_p)
29281 return;
29282 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29284 int x0 = WINDOW_LEFT_EDGE_X (w);
29285 int x1 = WINDOW_RIGHT_EDGE_X (w);
29286 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29287 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29289 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29293 /* Redraw the part of window W intersection rectangle FR. Pixel
29294 coordinates in FR are frame-relative. Call this function with
29295 input blocked. Value is non-zero if the exposure overwrites
29296 mouse-face. */
29298 static int
29299 expose_window (struct window *w, XRectangle *fr)
29301 struct frame *f = XFRAME (w->frame);
29302 XRectangle wr, r;
29303 int mouse_face_overwritten_p = 0;
29305 /* If window is not yet fully initialized, do nothing. This can
29306 happen when toolkit scroll bars are used and a window is split.
29307 Reconfiguring the scroll bar will generate an expose for a newly
29308 created window. */
29309 if (w->current_matrix == NULL)
29310 return 0;
29312 /* When we're currently updating the window, display and current
29313 matrix usually don't agree. Arrange for a thorough display
29314 later. */
29315 if (w->must_be_updated_p)
29317 SET_FRAME_GARBAGED (f);
29318 return 0;
29321 /* Frame-relative pixel rectangle of W. */
29322 wr.x = WINDOW_LEFT_EDGE_X (w);
29323 wr.y = WINDOW_TOP_EDGE_Y (w);
29324 wr.width = WINDOW_PIXEL_WIDTH (w);
29325 wr.height = WINDOW_PIXEL_HEIGHT (w);
29327 if (x_intersect_rectangles (fr, &wr, &r))
29329 int yb = window_text_bottom_y (w);
29330 struct glyph_row *row;
29331 int cursor_cleared_p, phys_cursor_on_p;
29332 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29334 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29335 r.x, r.y, r.width, r.height));
29337 /* Convert to window coordinates. */
29338 r.x -= WINDOW_LEFT_EDGE_X (w);
29339 r.y -= WINDOW_TOP_EDGE_Y (w);
29341 /* Turn off the cursor. */
29342 if (!w->pseudo_window_p
29343 && phys_cursor_in_rect_p (w, &r))
29345 x_clear_cursor (w);
29346 cursor_cleared_p = 1;
29348 else
29349 cursor_cleared_p = 0;
29351 /* If the row containing the cursor extends face to end of line,
29352 then expose_area might overwrite the cursor outside the
29353 rectangle and thus notice_overwritten_cursor might clear
29354 w->phys_cursor_on_p. We remember the original value and
29355 check later if it is changed. */
29356 phys_cursor_on_p = w->phys_cursor_on_p;
29358 /* Update lines intersecting rectangle R. */
29359 first_overlapping_row = last_overlapping_row = NULL;
29360 for (row = w->current_matrix->rows;
29361 row->enabled_p;
29362 ++row)
29364 int y0 = row->y;
29365 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29367 if ((y0 >= r.y && y0 < r.y + r.height)
29368 || (y1 > r.y && y1 < r.y + r.height)
29369 || (r.y >= y0 && r.y < y1)
29370 || (r.y + r.height > y0 && r.y + r.height < y1))
29372 /* A header line may be overlapping, but there is no need
29373 to fix overlapping areas for them. KFS 2005-02-12 */
29374 if (row->overlapping_p && !row->mode_line_p)
29376 if (first_overlapping_row == NULL)
29377 first_overlapping_row = row;
29378 last_overlapping_row = row;
29381 row->clip = fr;
29382 if (expose_line (w, row, &r))
29383 mouse_face_overwritten_p = 1;
29384 row->clip = NULL;
29386 else if (row->overlapping_p)
29388 /* We must redraw a row overlapping the exposed area. */
29389 if (y0 < r.y
29390 ? y0 + row->phys_height > r.y
29391 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29393 if (first_overlapping_row == NULL)
29394 first_overlapping_row = row;
29395 last_overlapping_row = row;
29399 if (y1 >= yb)
29400 break;
29403 /* Display the mode line if there is one. */
29404 if (WINDOW_WANTS_MODELINE_P (w)
29405 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29406 row->enabled_p)
29407 && row->y < r.y + r.height)
29409 if (expose_line (w, row, &r))
29410 mouse_face_overwritten_p = 1;
29413 if (!w->pseudo_window_p)
29415 /* Fix the display of overlapping rows. */
29416 if (first_overlapping_row)
29417 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29418 fr);
29420 /* Draw border between windows. */
29421 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29422 x_draw_right_divider (w);
29423 else
29424 x_draw_vertical_border (w);
29426 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29427 x_draw_bottom_divider (w);
29429 /* Turn the cursor on again. */
29430 if (cursor_cleared_p
29431 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29432 update_window_cursor (w, 1);
29436 return mouse_face_overwritten_p;
29441 /* Redraw (parts) of all windows in the window tree rooted at W that
29442 intersect R. R contains frame pixel coordinates. Value is
29443 non-zero if the exposure overwrites mouse-face. */
29445 static int
29446 expose_window_tree (struct window *w, XRectangle *r)
29448 struct frame *f = XFRAME (w->frame);
29449 int mouse_face_overwritten_p = 0;
29451 while (w && !FRAME_GARBAGED_P (f))
29453 if (WINDOWP (w->contents))
29454 mouse_face_overwritten_p
29455 |= expose_window_tree (XWINDOW (w->contents), r);
29456 else
29457 mouse_face_overwritten_p |= expose_window (w, r);
29459 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29462 return mouse_face_overwritten_p;
29466 /* EXPORT:
29467 Redisplay an exposed area of frame F. X and Y are the upper-left
29468 corner of the exposed rectangle. W and H are width and height of
29469 the exposed area. All are pixel values. W or H zero means redraw
29470 the entire frame. */
29472 void
29473 expose_frame (struct frame *f, int x, int y, int w, int h)
29475 XRectangle r;
29476 int mouse_face_overwritten_p = 0;
29478 TRACE ((stderr, "expose_frame "));
29480 /* No need to redraw if frame will be redrawn soon. */
29481 if (FRAME_GARBAGED_P (f))
29483 TRACE ((stderr, " garbaged\n"));
29484 return;
29487 /* If basic faces haven't been realized yet, there is no point in
29488 trying to redraw anything. This can happen when we get an expose
29489 event while Emacs is starting, e.g. by moving another window. */
29490 if (FRAME_FACE_CACHE (f) == NULL
29491 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29493 TRACE ((stderr, " no faces\n"));
29494 return;
29497 if (w == 0 || h == 0)
29499 r.x = r.y = 0;
29500 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29501 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29503 else
29505 r.x = x;
29506 r.y = y;
29507 r.width = w;
29508 r.height = h;
29511 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29512 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29514 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29515 if (WINDOWP (f->tool_bar_window))
29516 mouse_face_overwritten_p
29517 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29518 #endif
29520 #ifdef HAVE_X_WINDOWS
29521 #ifndef MSDOS
29522 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29523 if (WINDOWP (f->menu_bar_window))
29524 mouse_face_overwritten_p
29525 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29526 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29527 #endif
29528 #endif
29530 /* Some window managers support a focus-follows-mouse style with
29531 delayed raising of frames. Imagine a partially obscured frame,
29532 and moving the mouse into partially obscured mouse-face on that
29533 frame. The visible part of the mouse-face will be highlighted,
29534 then the WM raises the obscured frame. With at least one WM, KDE
29535 2.1, Emacs is not getting any event for the raising of the frame
29536 (even tried with SubstructureRedirectMask), only Expose events.
29537 These expose events will draw text normally, i.e. not
29538 highlighted. Which means we must redo the highlight here.
29539 Subsume it under ``we love X''. --gerd 2001-08-15 */
29540 /* Included in Windows version because Windows most likely does not
29541 do the right thing if any third party tool offers
29542 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29543 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29546 if (f == hlinfo->mouse_face_mouse_frame)
29548 int mouse_x = hlinfo->mouse_face_mouse_x;
29549 int mouse_y = hlinfo->mouse_face_mouse_y;
29550 clear_mouse_face (hlinfo);
29551 note_mouse_highlight (f, mouse_x, mouse_y);
29557 /* EXPORT:
29558 Determine the intersection of two rectangles R1 and R2. Return
29559 the intersection in *RESULT. Value is non-zero if RESULT is not
29560 empty. */
29563 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29565 XRectangle *left, *right;
29566 XRectangle *upper, *lower;
29567 int intersection_p = 0;
29569 /* Rearrange so that R1 is the left-most rectangle. */
29570 if (r1->x < r2->x)
29571 left = r1, right = r2;
29572 else
29573 left = r2, right = r1;
29575 /* X0 of the intersection is right.x0, if this is inside R1,
29576 otherwise there is no intersection. */
29577 if (right->x <= left->x + left->width)
29579 result->x = right->x;
29581 /* The right end of the intersection is the minimum of
29582 the right ends of left and right. */
29583 result->width = (min (left->x + left->width, right->x + right->width)
29584 - result->x);
29586 /* Same game for Y. */
29587 if (r1->y < r2->y)
29588 upper = r1, lower = r2;
29589 else
29590 upper = r2, lower = r1;
29592 /* The upper end of the intersection is lower.y0, if this is inside
29593 of upper. Otherwise, there is no intersection. */
29594 if (lower->y <= upper->y + upper->height)
29596 result->y = lower->y;
29598 /* The lower end of the intersection is the minimum of the lower
29599 ends of upper and lower. */
29600 result->height = (min (lower->y + lower->height,
29601 upper->y + upper->height)
29602 - result->y);
29603 intersection_p = 1;
29607 return intersection_p;
29610 #endif /* HAVE_WINDOW_SYSTEM */
29613 /***********************************************************************
29614 Initialization
29615 ***********************************************************************/
29617 void
29618 syms_of_xdisp (void)
29620 Vwith_echo_area_save_vector = Qnil;
29621 staticpro (&Vwith_echo_area_save_vector);
29623 Vmessage_stack = Qnil;
29624 staticpro (&Vmessage_stack);
29626 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29627 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29629 message_dolog_marker1 = Fmake_marker ();
29630 staticpro (&message_dolog_marker1);
29631 message_dolog_marker2 = Fmake_marker ();
29632 staticpro (&message_dolog_marker2);
29633 message_dolog_marker3 = Fmake_marker ();
29634 staticpro (&message_dolog_marker3);
29636 #ifdef GLYPH_DEBUG
29637 defsubr (&Sdump_frame_glyph_matrix);
29638 defsubr (&Sdump_glyph_matrix);
29639 defsubr (&Sdump_glyph_row);
29640 defsubr (&Sdump_tool_bar_row);
29641 defsubr (&Strace_redisplay);
29642 defsubr (&Strace_to_stderr);
29643 #endif
29644 #ifdef HAVE_WINDOW_SYSTEM
29645 defsubr (&Stool_bar_height);
29646 defsubr (&Slookup_image_map);
29647 #endif
29648 defsubr (&Sline_pixel_height);
29649 defsubr (&Sformat_mode_line);
29650 defsubr (&Sinvisible_p);
29651 defsubr (&Scurrent_bidi_paragraph_direction);
29652 defsubr (&Swindow_text_pixel_size);
29653 defsubr (&Smove_point_visually);
29655 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29656 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29657 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29658 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29659 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29660 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29661 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29662 DEFSYM (Qeval, "eval");
29663 DEFSYM (QCdata, ":data");
29664 DEFSYM (Qdisplay, "display");
29665 DEFSYM (Qspace_width, "space-width");
29666 DEFSYM (Qraise, "raise");
29667 DEFSYM (Qslice, "slice");
29668 DEFSYM (Qspace, "space");
29669 DEFSYM (Qmargin, "margin");
29670 DEFSYM (Qpointer, "pointer");
29671 DEFSYM (Qleft_margin, "left-margin");
29672 DEFSYM (Qright_margin, "right-margin");
29673 DEFSYM (Qcenter, "center");
29674 DEFSYM (Qline_height, "line-height");
29675 DEFSYM (QCalign_to, ":align-to");
29676 DEFSYM (QCrelative_width, ":relative-width");
29677 DEFSYM (QCrelative_height, ":relative-height");
29678 DEFSYM (QCeval, ":eval");
29679 DEFSYM (QCpropertize, ":propertize");
29680 DEFSYM (QCfile, ":file");
29681 DEFSYM (Qfontified, "fontified");
29682 DEFSYM (Qfontification_functions, "fontification-functions");
29683 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29684 DEFSYM (Qescape_glyph, "escape-glyph");
29685 DEFSYM (Qnobreak_space, "nobreak-space");
29686 DEFSYM (Qimage, "image");
29687 DEFSYM (Qtext, "text");
29688 DEFSYM (Qboth, "both");
29689 DEFSYM (Qboth_horiz, "both-horiz");
29690 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29691 DEFSYM (QCmap, ":map");
29692 DEFSYM (QCpointer, ":pointer");
29693 DEFSYM (Qrect, "rect");
29694 DEFSYM (Qcircle, "circle");
29695 DEFSYM (Qpoly, "poly");
29696 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29697 DEFSYM (Qgrow_only, "grow-only");
29698 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29699 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29700 DEFSYM (Qposition, "position");
29701 DEFSYM (Qbuffer_position, "buffer-position");
29702 DEFSYM (Qobject, "object");
29703 DEFSYM (Qbar, "bar");
29704 DEFSYM (Qhbar, "hbar");
29705 DEFSYM (Qbox, "box");
29706 DEFSYM (Qhollow, "hollow");
29707 DEFSYM (Qhand, "hand");
29708 DEFSYM (Qarrow, "arrow");
29709 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29711 list_of_error = list1 (list2 (intern_c_string ("error"),
29712 intern_c_string ("void-variable")));
29713 staticpro (&list_of_error);
29715 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29716 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29717 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29718 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29720 echo_buffer[0] = echo_buffer[1] = Qnil;
29721 staticpro (&echo_buffer[0]);
29722 staticpro (&echo_buffer[1]);
29724 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29725 staticpro (&echo_area_buffer[0]);
29726 staticpro (&echo_area_buffer[1]);
29728 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29729 staticpro (&Vmessages_buffer_name);
29731 mode_line_proptrans_alist = Qnil;
29732 staticpro (&mode_line_proptrans_alist);
29733 mode_line_string_list = Qnil;
29734 staticpro (&mode_line_string_list);
29735 mode_line_string_face = Qnil;
29736 staticpro (&mode_line_string_face);
29737 mode_line_string_face_prop = Qnil;
29738 staticpro (&mode_line_string_face_prop);
29739 Vmode_line_unwind_vector = Qnil;
29740 staticpro (&Vmode_line_unwind_vector);
29742 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29744 help_echo_string = Qnil;
29745 staticpro (&help_echo_string);
29746 help_echo_object = Qnil;
29747 staticpro (&help_echo_object);
29748 help_echo_window = Qnil;
29749 staticpro (&help_echo_window);
29750 previous_help_echo_string = Qnil;
29751 staticpro (&previous_help_echo_string);
29752 help_echo_pos = -1;
29754 DEFSYM (Qright_to_left, "right-to-left");
29755 DEFSYM (Qleft_to_right, "left-to-right");
29757 #ifdef HAVE_WINDOW_SYSTEM
29758 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29759 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29760 For example, if a block cursor is over a tab, it will be drawn as
29761 wide as that tab on the display. */);
29762 x_stretch_cursor_p = 0;
29763 #endif
29765 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29766 doc: /* Non-nil means highlight trailing whitespace.
29767 The face used for trailing whitespace is `trailing-whitespace'. */);
29768 Vshow_trailing_whitespace = Qnil;
29770 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29771 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29772 If the value is t, Emacs highlights non-ASCII chars which have the
29773 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29774 or `escape-glyph' face respectively.
29776 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29777 U+2011 (non-breaking hyphen) are affected.
29779 Any other non-nil value means to display these characters as a escape
29780 glyph followed by an ordinary space or hyphen.
29782 A value of nil means no special handling of these characters. */);
29783 Vnobreak_char_display = Qt;
29785 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29786 doc: /* The pointer shape to show in void text areas.
29787 A value of nil means to show the text pointer. Other options are `arrow',
29788 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29789 Vvoid_text_area_pointer = Qarrow;
29791 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29792 doc: /* Non-nil means don't actually do any redisplay.
29793 This is used for internal purposes. */);
29794 Vinhibit_redisplay = Qnil;
29796 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29797 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29798 Vglobal_mode_string = Qnil;
29800 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29801 doc: /* Marker for where to display an arrow on top of the buffer text.
29802 This must be the beginning of a line in order to work.
29803 See also `overlay-arrow-string'. */);
29804 Voverlay_arrow_position = Qnil;
29806 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29807 doc: /* String to display as an arrow in non-window frames.
29808 See also `overlay-arrow-position'. */);
29809 Voverlay_arrow_string = build_pure_c_string ("=>");
29811 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29812 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29813 The symbols on this list are examined during redisplay to determine
29814 where to display overlay arrows. */);
29815 Voverlay_arrow_variable_list
29816 = list1 (intern_c_string ("overlay-arrow-position"));
29818 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29819 doc: /* The number of lines to try scrolling a window by when point moves out.
29820 If that fails to bring point back on frame, point is centered instead.
29821 If this is zero, point is always centered after it moves off frame.
29822 If you want scrolling to always be a line at a time, you should set
29823 `scroll-conservatively' to a large value rather than set this to 1. */);
29825 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29826 doc: /* Scroll up to this many lines, to bring point back on screen.
29827 If point moves off-screen, redisplay will scroll by up to
29828 `scroll-conservatively' lines in order to bring point just barely
29829 onto the screen again. If that cannot be done, then redisplay
29830 recenters point as usual.
29832 If the value is greater than 100, redisplay will never recenter point,
29833 but will always scroll just enough text to bring point into view, even
29834 if you move far away.
29836 A value of zero means always recenter point if it moves off screen. */);
29837 scroll_conservatively = 0;
29839 DEFVAR_INT ("scroll-margin", scroll_margin,
29840 doc: /* Number of lines of margin at the top and bottom of a window.
29841 Recenter the window whenever point gets within this many lines
29842 of the top or bottom of the window. */);
29843 scroll_margin = 0;
29845 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29846 doc: /* Pixels per inch value for non-window system displays.
29847 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29848 Vdisplay_pixels_per_inch = make_float (72.0);
29850 #ifdef GLYPH_DEBUG
29851 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29852 #endif
29854 DEFVAR_LISP ("truncate-partial-width-windows",
29855 Vtruncate_partial_width_windows,
29856 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29857 For an integer value, truncate lines in each window narrower than the
29858 full frame width, provided the window width is less than that integer;
29859 otherwise, respect the value of `truncate-lines'.
29861 For any other non-nil value, truncate lines in all windows that do
29862 not span the full frame width.
29864 A value of nil means to respect the value of `truncate-lines'.
29866 If `word-wrap' is enabled, you might want to reduce this. */);
29867 Vtruncate_partial_width_windows = make_number (50);
29869 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29870 doc: /* Maximum buffer size for which line number should be displayed.
29871 If the buffer is bigger than this, the line number does not appear
29872 in the mode line. A value of nil means no limit. */);
29873 Vline_number_display_limit = Qnil;
29875 DEFVAR_INT ("line-number-display-limit-width",
29876 line_number_display_limit_width,
29877 doc: /* Maximum line width (in characters) for line number display.
29878 If the average length of the lines near point is bigger than this, then the
29879 line number may be omitted from the mode line. */);
29880 line_number_display_limit_width = 200;
29882 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29883 doc: /* Non-nil means highlight region even in nonselected windows. */);
29884 highlight_nonselected_windows = 0;
29886 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29887 doc: /* Non-nil if more than one frame is visible on this display.
29888 Minibuffer-only frames don't count, but iconified frames do.
29889 This variable is not guaranteed to be accurate except while processing
29890 `frame-title-format' and `icon-title-format'. */);
29892 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29893 doc: /* Template for displaying the title bar of visible frames.
29894 \(Assuming the window manager supports this feature.)
29896 This variable has the same structure as `mode-line-format', except that
29897 the %c and %l constructs are ignored. It is used only on frames for
29898 which no explicit name has been set \(see `modify-frame-parameters'). */);
29900 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29901 doc: /* Template for displaying the title bar of an iconified frame.
29902 \(Assuming the window manager supports this feature.)
29903 This variable has the same structure as `mode-line-format' (which see),
29904 and is used only on frames for which no explicit name has been set
29905 \(see `modify-frame-parameters'). */);
29906 Vicon_title_format
29907 = Vframe_title_format
29908 = listn (CONSTYPE_PURE, 3,
29909 intern_c_string ("multiple-frames"),
29910 build_pure_c_string ("%b"),
29911 listn (CONSTYPE_PURE, 4,
29912 empty_unibyte_string,
29913 intern_c_string ("invocation-name"),
29914 build_pure_c_string ("@"),
29915 intern_c_string ("system-name")));
29917 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29918 doc: /* Maximum number of lines to keep in the message log buffer.
29919 If nil, disable message logging. If t, log messages but don't truncate
29920 the buffer when it becomes large. */);
29921 Vmessage_log_max = make_number (1000);
29923 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29924 doc: /* Functions called before redisplay, if window sizes have changed.
29925 The value should be a list of functions that take one argument.
29926 Just before redisplay, for each frame, if any of its windows have changed
29927 size since the last redisplay, or have been split or deleted,
29928 all the functions in the list are called, with the frame as argument. */);
29929 Vwindow_size_change_functions = Qnil;
29931 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29932 doc: /* List of functions to call before redisplaying a window with scrolling.
29933 Each function is called with two arguments, the window and its new
29934 display-start position. Note that these functions are also called by
29935 `set-window-buffer'. Also note that the value of `window-end' is not
29936 valid when these functions are called.
29938 Warning: Do not use this feature to alter the way the window
29939 is scrolled. It is not designed for that, and such use probably won't
29940 work. */);
29941 Vwindow_scroll_functions = Qnil;
29943 DEFVAR_LISP ("window-text-change-functions",
29944 Vwindow_text_change_functions,
29945 doc: /* Functions to call in redisplay when text in the window might change. */);
29946 Vwindow_text_change_functions = Qnil;
29948 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29949 doc: /* Functions called when redisplay of a window reaches the end trigger.
29950 Each function is called with two arguments, the window and the end trigger value.
29951 See `set-window-redisplay-end-trigger'. */);
29952 Vredisplay_end_trigger_functions = Qnil;
29954 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29955 doc: /* Non-nil means autoselect window with mouse pointer.
29956 If nil, do not autoselect windows.
29957 A positive number means delay autoselection by that many seconds: a
29958 window is autoselected only after the mouse has remained in that
29959 window for the duration of the delay.
29960 A negative number has a similar effect, but causes windows to be
29961 autoselected only after the mouse has stopped moving. \(Because of
29962 the way Emacs compares mouse events, you will occasionally wait twice
29963 that time before the window gets selected.\)
29964 Any other value means to autoselect window instantaneously when the
29965 mouse pointer enters it.
29967 Autoselection selects the minibuffer only if it is active, and never
29968 unselects the minibuffer if it is active.
29970 When customizing this variable make sure that the actual value of
29971 `focus-follows-mouse' matches the behavior of your window manager. */);
29972 Vmouse_autoselect_window = Qnil;
29974 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29975 doc: /* Non-nil means automatically resize tool-bars.
29976 This dynamically changes the tool-bar's height to the minimum height
29977 that is needed to make all tool-bar items visible.
29978 If value is `grow-only', the tool-bar's height is only increased
29979 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29980 Vauto_resize_tool_bars = Qt;
29982 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29983 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29984 auto_raise_tool_bar_buttons_p = 1;
29986 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29987 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29988 make_cursor_line_fully_visible_p = 1;
29990 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29991 doc: /* Border below tool-bar in pixels.
29992 If an integer, use it as the height of the border.
29993 If it is one of `internal-border-width' or `border-width', use the
29994 value of the corresponding frame parameter.
29995 Otherwise, no border is added below the tool-bar. */);
29996 Vtool_bar_border = Qinternal_border_width;
29998 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29999 doc: /* Margin around tool-bar buttons in pixels.
30000 If an integer, use that for both horizontal and vertical margins.
30001 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30002 HORZ specifying the horizontal margin, and VERT specifying the
30003 vertical margin. */);
30004 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30006 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30007 doc: /* Relief thickness of tool-bar buttons. */);
30008 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30010 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30011 doc: /* Tool bar style to use.
30012 It can be one of
30013 image - show images only
30014 text - show text only
30015 both - show both, text below image
30016 both-horiz - show text to the right of the image
30017 text-image-horiz - show text to the left of the image
30018 any other - use system default or image if no system default.
30020 This variable only affects the GTK+ toolkit version of Emacs. */);
30021 Vtool_bar_style = Qnil;
30023 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30024 doc: /* Maximum number of characters a label can have to be shown.
30025 The tool bar style must also show labels for this to have any effect, see
30026 `tool-bar-style'. */);
30027 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30029 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30030 doc: /* List of functions to call to fontify regions of text.
30031 Each function is called with one argument POS. Functions must
30032 fontify a region starting at POS in the current buffer, and give
30033 fontified regions the property `fontified'. */);
30034 Vfontification_functions = Qnil;
30035 Fmake_variable_buffer_local (Qfontification_functions);
30037 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30038 unibyte_display_via_language_environment,
30039 doc: /* Non-nil means display unibyte text according to language environment.
30040 Specifically, this means that raw bytes in the range 160-255 decimal
30041 are displayed by converting them to the equivalent multibyte characters
30042 according to the current language environment. As a result, they are
30043 displayed according to the current fontset.
30045 Note that this variable affects only how these bytes are displayed,
30046 but does not change the fact they are interpreted as raw bytes. */);
30047 unibyte_display_via_language_environment = 0;
30049 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30050 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30051 If a float, it specifies a fraction of the mini-window frame's height.
30052 If an integer, it specifies a number of lines. */);
30053 Vmax_mini_window_height = make_float (0.25);
30055 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30056 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30057 A value of nil means don't automatically resize mini-windows.
30058 A value of t means resize them to fit the text displayed in them.
30059 A value of `grow-only', the default, means let mini-windows grow only;
30060 they return to their normal size when the minibuffer is closed, or the
30061 echo area becomes empty. */);
30062 Vresize_mini_windows = Qgrow_only;
30064 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30065 doc: /* Alist specifying how to blink the cursor off.
30066 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30067 `cursor-type' frame-parameter or variable equals ON-STATE,
30068 comparing using `equal', Emacs uses OFF-STATE to specify
30069 how to blink it off. ON-STATE and OFF-STATE are values for
30070 the `cursor-type' frame parameter.
30072 If a frame's ON-STATE has no entry in this list,
30073 the frame's other specifications determine how to blink the cursor off. */);
30074 Vblink_cursor_alist = Qnil;
30076 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30077 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30078 If non-nil, windows are automatically scrolled horizontally to make
30079 point visible. */);
30080 automatic_hscrolling_p = 1;
30081 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30083 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30084 doc: /* How many columns away from the window edge point is allowed to get
30085 before automatic hscrolling will horizontally scroll the window. */);
30086 hscroll_margin = 5;
30088 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30089 doc: /* How many columns to scroll the window when point gets too close to the edge.
30090 When point is less than `hscroll-margin' columns from the window
30091 edge, automatic hscrolling will scroll the window by the amount of columns
30092 determined by this variable. If its value is a positive integer, scroll that
30093 many columns. If it's a positive floating-point number, it specifies the
30094 fraction of the window's width to scroll. If it's nil or zero, point will be
30095 centered horizontally after the scroll. Any other value, including negative
30096 numbers, are treated as if the value were zero.
30098 Automatic hscrolling always moves point outside the scroll margin, so if
30099 point was more than scroll step columns inside the margin, the window will
30100 scroll more than the value given by the scroll step.
30102 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30103 and `scroll-right' overrides this variable's effect. */);
30104 Vhscroll_step = make_number (0);
30106 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30107 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30108 Bind this around calls to `message' to let it take effect. */);
30109 message_truncate_lines = 0;
30111 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30112 doc: /* Normal hook run to update the menu bar definitions.
30113 Redisplay runs this hook before it redisplays the menu bar.
30114 This is used to update submenus such as Buffers,
30115 whose contents depend on various data. */);
30116 Vmenu_bar_update_hook = Qnil;
30118 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30119 doc: /* Frame for which we are updating a menu.
30120 The enable predicate for a menu binding should check this variable. */);
30121 Vmenu_updating_frame = Qnil;
30123 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30124 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30125 inhibit_menubar_update = 0;
30127 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30128 doc: /* Prefix prepended to all continuation lines at display time.
30129 The value may be a string, an image, or a stretch-glyph; it is
30130 interpreted in the same way as the value of a `display' text property.
30132 This variable is overridden by any `wrap-prefix' text or overlay
30133 property.
30135 To add a prefix to non-continuation lines, use `line-prefix'. */);
30136 Vwrap_prefix = Qnil;
30137 DEFSYM (Qwrap_prefix, "wrap-prefix");
30138 Fmake_variable_buffer_local (Qwrap_prefix);
30140 DEFVAR_LISP ("line-prefix", Vline_prefix,
30141 doc: /* Prefix prepended to all non-continuation lines at display time.
30142 The value may be a string, an image, or a stretch-glyph; it is
30143 interpreted in the same way as the value of a `display' text property.
30145 This variable is overridden by any `line-prefix' text or overlay
30146 property.
30148 To add a prefix to continuation lines, use `wrap-prefix'. */);
30149 Vline_prefix = Qnil;
30150 DEFSYM (Qline_prefix, "line-prefix");
30151 Fmake_variable_buffer_local (Qline_prefix);
30153 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30154 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30155 inhibit_eval_during_redisplay = 0;
30157 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30158 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30159 inhibit_free_realized_faces = 0;
30161 #ifdef GLYPH_DEBUG
30162 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30163 doc: /* Inhibit try_window_id display optimization. */);
30164 inhibit_try_window_id = 0;
30166 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30167 doc: /* Inhibit try_window_reusing display optimization. */);
30168 inhibit_try_window_reusing = 0;
30170 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30171 doc: /* Inhibit try_cursor_movement display optimization. */);
30172 inhibit_try_cursor_movement = 0;
30173 #endif /* GLYPH_DEBUG */
30175 DEFVAR_INT ("overline-margin", overline_margin,
30176 doc: /* Space between overline and text, in pixels.
30177 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30178 margin to the character height. */);
30179 overline_margin = 2;
30181 DEFVAR_INT ("underline-minimum-offset",
30182 underline_minimum_offset,
30183 doc: /* Minimum distance between baseline and underline.
30184 This can improve legibility of underlined text at small font sizes,
30185 particularly when using variable `x-use-underline-position-properties'
30186 with fonts that specify an UNDERLINE_POSITION relatively close to the
30187 baseline. The default value is 1. */);
30188 underline_minimum_offset = 1;
30190 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30191 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30192 This feature only works when on a window system that can change
30193 cursor shapes. */);
30194 display_hourglass_p = 1;
30196 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30197 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30198 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30200 #ifdef HAVE_WINDOW_SYSTEM
30201 hourglass_atimer = NULL;
30202 hourglass_shown_p = 0;
30203 #endif /* HAVE_WINDOW_SYSTEM */
30205 DEFSYM (Qglyphless_char, "glyphless-char");
30206 DEFSYM (Qhex_code, "hex-code");
30207 DEFSYM (Qempty_box, "empty-box");
30208 DEFSYM (Qthin_space, "thin-space");
30209 DEFSYM (Qzero_width, "zero-width");
30211 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30212 doc: /* Function run just before redisplay.
30213 It is called with one argument, which is the set of windows that are to
30214 be redisplayed. This set can be nil (meaning, only the selected window),
30215 or t (meaning all windows). */);
30216 Vpre_redisplay_function = intern ("ignore");
30218 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30219 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30221 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30222 doc: /* Char-table defining glyphless characters.
30223 Each element, if non-nil, should be one of the following:
30224 an ASCII acronym string: display this string in a box
30225 `hex-code': display the hexadecimal code of a character in a box
30226 `empty-box': display as an empty box
30227 `thin-space': display as 1-pixel width space
30228 `zero-width': don't display
30229 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30230 display method for graphical terminals and text terminals respectively.
30231 GRAPHICAL and TEXT should each have one of the values listed above.
30233 The char-table has one extra slot to control the display of a character for
30234 which no font is found. This slot only takes effect on graphical terminals.
30235 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30236 `thin-space'. The default is `empty-box'.
30238 If a character has a non-nil entry in an active display table, the
30239 display table takes effect; in this case, Emacs does not consult
30240 `glyphless-char-display' at all. */);
30241 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30242 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30243 Qempty_box);
30245 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30246 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30247 Vdebug_on_message = Qnil;
30249 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30250 doc: /* */);
30251 Vredisplay__all_windows_cause
30252 = Fmake_vector (make_number (100), make_number (0));
30254 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30255 doc: /* */);
30256 Vredisplay__mode_lines_cause
30257 = Fmake_vector (make_number (100), make_number (0));
30261 /* Initialize this module when Emacs starts. */
30263 void
30264 init_xdisp (void)
30266 CHARPOS (this_line_start_pos) = 0;
30268 if (!noninteractive)
30270 struct window *m = XWINDOW (minibuf_window);
30271 Lisp_Object frame = m->frame;
30272 struct frame *f = XFRAME (frame);
30273 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30274 struct window *r = XWINDOW (root);
30275 int i;
30277 echo_area_window = minibuf_window;
30279 r->top_line = FRAME_TOP_MARGIN (f);
30280 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30281 r->total_cols = FRAME_COLS (f);
30282 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30283 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30284 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30286 m->top_line = FRAME_LINES (f) - 1;
30287 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30288 m->total_cols = FRAME_COLS (f);
30289 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30290 m->total_lines = 1;
30291 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30293 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30294 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30295 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30297 /* The default ellipsis glyphs `...'. */
30298 for (i = 0; i < 3; ++i)
30299 default_invis_vector[i] = make_number ('.');
30303 /* Allocate the buffer for frame titles.
30304 Also used for `format-mode-line'. */
30305 int size = 100;
30306 mode_line_noprop_buf = xmalloc (size);
30307 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30308 mode_line_noprop_ptr = mode_line_noprop_buf;
30309 mode_line_target = MODE_LINE_DISPLAY;
30312 help_echo_showing_p = 0;
30315 #ifdef HAVE_WINDOW_SYSTEM
30317 /* Platform-independent portion of hourglass implementation. */
30319 /* Cancel a currently active hourglass timer, and start a new one. */
30320 void
30321 start_hourglass (void)
30323 struct timespec delay;
30325 cancel_hourglass ();
30327 if (INTEGERP (Vhourglass_delay)
30328 && XINT (Vhourglass_delay) > 0)
30329 delay = make_timespec (min (XINT (Vhourglass_delay),
30330 TYPE_MAXIMUM (time_t)),
30332 else if (FLOATP (Vhourglass_delay)
30333 && XFLOAT_DATA (Vhourglass_delay) > 0)
30334 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30335 else
30336 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30338 #ifdef HAVE_NTGUI
30340 extern void w32_note_current_window (void);
30341 w32_note_current_window ();
30343 #endif /* HAVE_NTGUI */
30345 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30346 show_hourglass, NULL);
30350 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30351 shown. */
30352 void
30353 cancel_hourglass (void)
30355 if (hourglass_atimer)
30357 cancel_atimer (hourglass_atimer);
30358 hourglass_atimer = NULL;
30361 if (hourglass_shown_p)
30362 hide_hourglass ();
30365 #endif /* HAVE_WINDOW_SYSTEM */