In balance-windows-area don't call window-height and window-width. Bug#16241.
[emacs.git] / src / xdisp.c
blob1c157c7be0a4465148a506b38ef4aa834f557c77
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
309 #define INFINITY 10000000
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
334 static Lisp_Object Qfontification_functions;
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
340 /* Non-nil means don't actually do any redisplay. */
342 Lisp_Object Qinhibit_redisplay;
344 /* Names of text properties relevant for redisplay. */
346 Lisp_Object Qdisplay;
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
357 #ifdef HAVE_WINDOW_SYSTEM
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390 /* Name of the face used to highlight trailing whitespace. */
392 static Lisp_Object Qtrailing_whitespace;
394 /* Name and number of the face used to highlight escape glyphs. */
396 static Lisp_Object Qescape_glyph;
398 /* Name and number of the face used to highlight non-breaking spaces. */
400 static Lisp_Object Qnobreak_space;
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
405 Lisp_Object Qimage;
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
418 bool noninteractive_need_newline;
420 /* Non-zero means print newline to message log before next message. */
422 static bool message_log_need_newline;
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
436 static struct text_pos this_line_start_pos;
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
441 static struct text_pos this_line_end_pos;
443 /* The vertical positions and the height of this line. */
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
452 static int this_line_start_x;
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
458 static struct text_pos this_line_min_pos;
460 /* Buffer that this_line_.* variables are referring to. */
462 static struct buffer *this_line_buffer;
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477 Lisp_Object Qmenu_bar_update_hook;
479 /* Nonzero if an overlay arrow has been displayed in this window. */
481 static bool overlay_arrow_seen;
483 /* Vector containing glyphs for an ellipsis `...'. */
485 static Lisp_Object default_invis_vector[3];
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
491 Lisp_Object echo_area_window;
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
498 static Lisp_Object Vmessage_stack;
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
503 static bool message_enable_multibyte;
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
511 int update_mode_lines;
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
520 int windows_or_buffers_changed;
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
525 static bool line_number_displayed;
527 /* The name of the *Messages* buffer, a string. */
529 static Lisp_Object Vmessages_buffer_name;
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
534 Lisp_Object echo_area_buffer[2];
536 /* The buffers referenced from echo_area_buffer. */
538 static Lisp_Object echo_buffer[2];
540 /* A vector saved used in with_area_buffer to reduce consing. */
542 static Lisp_Object Vwith_echo_area_save_vector;
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
547 static bool display_last_displayed_message_p;
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
552 static bool message_buf_print;
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
562 static bool message_cleared_p;
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
571 /* Ascent and height of the last line processed by move_it_to. */
573 static int last_max_ascent, last_height;
575 /* Non-zero if there's a help-echo in the echo area. */
577 bool help_echo_showing_p;
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
585 #define TEXT_PROP_DISTANCE_LIMIT 100
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
613 void
614 redisplay_other_windows (void)
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
620 void
621 wset_redisplay (struct window *w)
623 /* Beware: selected_window can be nil during early stages. */
624 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
625 redisplay_other_windows ();
626 w->redisplay = true;
629 void
630 fset_redisplay (struct frame *f)
632 redisplay_other_windows ();
633 f->redisplay = true;
636 void
637 bset_redisplay (struct buffer *b)
639 int count = buffer_window_count (b);
640 if (count > 0)
642 /* ... it's visible in other window than selected, */
643 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
644 redisplay_other_windows ();
645 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
646 so that if we later set windows_or_buffers_changed, this buffer will
647 not be omitted. */
648 b->text->redisplay = true;
652 void
653 bset_update_mode_line (struct buffer *b)
655 if (!update_mode_lines)
656 update_mode_lines = REDISPLAY_SOME;
657 b->text->redisplay = true;
660 #ifdef GLYPH_DEBUG
662 /* Non-zero means print traces of redisplay if compiled with
663 GLYPH_DEBUG defined. */
665 bool trace_redisplay_p;
667 #endif /* GLYPH_DEBUG */
669 #ifdef DEBUG_TRACE_MOVE
670 /* Non-zero means trace with TRACE_MOVE to stderr. */
671 int trace_move;
673 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
674 #else
675 #define TRACE_MOVE(x) (void) 0
676 #endif
678 static Lisp_Object Qauto_hscroll_mode;
680 /* Buffer being redisplayed -- for redisplay_window_error. */
682 static struct buffer *displayed_buffer;
684 /* Value returned from text property handlers (see below). */
686 enum prop_handled
688 HANDLED_NORMALLY,
689 HANDLED_RECOMPUTE_PROPS,
690 HANDLED_OVERLAY_STRING_CONSUMED,
691 HANDLED_RETURN
694 /* A description of text properties that redisplay is interested
695 in. */
697 struct props
699 /* The name of the property. */
700 Lisp_Object *name;
702 /* A unique index for the property. */
703 enum prop_idx idx;
705 /* A handler function called to set up iterator IT from the property
706 at IT's current position. Value is used to steer handle_stop. */
707 enum prop_handled (*handler) (struct it *it);
710 static enum prop_handled handle_face_prop (struct it *);
711 static enum prop_handled handle_invisible_prop (struct it *);
712 static enum prop_handled handle_display_prop (struct it *);
713 static enum prop_handled handle_composition_prop (struct it *);
714 static enum prop_handled handle_overlay_change (struct it *);
715 static enum prop_handled handle_fontified_prop (struct it *);
717 /* Properties handled by iterators. */
719 static struct props it_props[] =
721 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
722 /* Handle `face' before `display' because some sub-properties of
723 `display' need to know the face. */
724 {&Qface, FACE_PROP_IDX, handle_face_prop},
725 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
726 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
727 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
728 {NULL, 0, NULL}
731 /* Value is the position described by X. If X is a marker, value is
732 the marker_position of X. Otherwise, value is X. */
734 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736 /* Enumeration returned by some move_it_.* functions internally. */
738 enum move_it_result
740 /* Not used. Undefined value. */
741 MOVE_UNDEFINED,
743 /* Move ended at the requested buffer position or ZV. */
744 MOVE_POS_MATCH_OR_ZV,
746 /* Move ended at the requested X pixel position. */
747 MOVE_X_REACHED,
749 /* Move within a line ended at the end of a line that must be
750 continued. */
751 MOVE_LINE_CONTINUED,
753 /* Move within a line ended at the end of a line that would
754 be displayed truncated. */
755 MOVE_LINE_TRUNCATED,
757 /* Move within a line ended at a line end. */
758 MOVE_NEWLINE_OR_CR
761 /* This counter is used to clear the face cache every once in a while
762 in redisplay_internal. It is incremented for each redisplay.
763 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
764 cleared. */
766 #define CLEAR_FACE_CACHE_COUNT 500
767 static int clear_face_cache_count;
769 /* Similarly for the image cache. */
771 #ifdef HAVE_WINDOW_SYSTEM
772 #define CLEAR_IMAGE_CACHE_COUNT 101
773 static int clear_image_cache_count;
775 /* Null glyph slice */
776 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
777 #endif
779 /* True while redisplay_internal is in progress. */
781 bool redisplaying_p;
783 static Lisp_Object Qinhibit_free_realized_faces;
784 static Lisp_Object Qmode_line_default_help_echo;
786 /* If a string, XTread_socket generates an event to display that string.
787 (The display is done in read_char.) */
789 Lisp_Object help_echo_string;
790 Lisp_Object help_echo_window;
791 Lisp_Object help_echo_object;
792 ptrdiff_t help_echo_pos;
794 /* Temporary variable for XTread_socket. */
796 Lisp_Object previous_help_echo_string;
798 /* Platform-independent portion of hourglass implementation. */
800 #ifdef HAVE_WINDOW_SYSTEM
802 /* Non-zero means an hourglass cursor is currently shown. */
803 bool hourglass_shown_p;
805 /* If non-null, an asynchronous timer that, when it expires, displays
806 an hourglass cursor on all frames. */
807 struct atimer *hourglass_atimer;
809 #endif /* HAVE_WINDOW_SYSTEM */
811 /* Name of the face used to display glyphless characters. */
812 static Lisp_Object Qglyphless_char;
814 /* Symbol for the purpose of Vglyphless_char_display. */
815 static Lisp_Object Qglyphless_char_display;
817 /* Method symbols for Vglyphless_char_display. */
818 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
820 /* Default number of seconds to wait before displaying an hourglass
821 cursor. */
822 #define DEFAULT_HOURGLASS_DELAY 1
824 #ifdef HAVE_WINDOW_SYSTEM
826 /* Default pixel width of `thin-space' display method. */
827 #define THIN_SPACE_WIDTH 1
829 #endif /* HAVE_WINDOW_SYSTEM */
831 /* Function prototypes. */
833 static void setup_for_ellipsis (struct it *, int);
834 static void set_iterator_to_next (struct it *, int);
835 static void mark_window_display_accurate_1 (struct window *, int);
836 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
837 static int display_prop_string_p (Lisp_Object, Lisp_Object);
838 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
839 static int cursor_row_p (struct glyph_row *);
840 static int redisplay_mode_lines (Lisp_Object, bool);
841 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
843 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
845 static void handle_line_prefix (struct it *);
847 static void pint2str (char *, int, ptrdiff_t);
848 static void pint2hrstr (char *, int, ptrdiff_t);
849 static struct text_pos run_window_scroll_functions (Lisp_Object,
850 struct text_pos);
851 static int text_outside_line_unchanged_p (struct window *,
852 ptrdiff_t, ptrdiff_t);
853 static void store_mode_line_noprop_char (char);
854 static int store_mode_line_noprop (const char *, int, int);
855 static void handle_stop (struct it *);
856 static void handle_stop_backwards (struct it *, ptrdiff_t);
857 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
858 static void ensure_echo_area_buffers (void);
859 static void unwind_with_echo_area_buffer (Lisp_Object);
860 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
861 static int with_echo_area_buffer (struct window *, int,
862 int (*) (ptrdiff_t, Lisp_Object),
863 ptrdiff_t, Lisp_Object);
864 static void clear_garbaged_frames (void);
865 static int current_message_1 (ptrdiff_t, Lisp_Object);
866 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
867 static void set_message (Lisp_Object);
868 static int set_message_1 (ptrdiff_t, Lisp_Object);
869 static int display_echo_area (struct window *);
870 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
871 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
872 static void unwind_redisplay (void);
873 static int string_char_and_length (const unsigned char *, int *);
874 static struct text_pos display_prop_end (struct it *, Lisp_Object,
875 struct text_pos);
876 static int compute_window_start_on_continuation_line (struct window *);
877 static void insert_left_trunc_glyphs (struct it *);
878 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
879 Lisp_Object);
880 static void extend_face_to_end_of_line (struct it *);
881 static int append_space_for_newline (struct it *, int);
882 static int cursor_row_fully_visible_p (struct window *, int, int);
883 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
884 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
885 static int trailing_whitespace_p (ptrdiff_t);
886 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
887 static void push_it (struct it *, struct text_pos *);
888 static void iterate_out_of_display_property (struct it *);
889 static void pop_it (struct it *);
890 static void sync_frame_with_window_matrix_rows (struct window *);
891 static void redisplay_internal (void);
892 static int echo_area_display (int);
893 static void redisplay_windows (Lisp_Object);
894 static void redisplay_window (Lisp_Object, bool);
895 static Lisp_Object redisplay_window_error (Lisp_Object);
896 static Lisp_Object redisplay_window_0 (Lisp_Object);
897 static Lisp_Object redisplay_window_1 (Lisp_Object);
898 static int set_cursor_from_row (struct window *, struct glyph_row *,
899 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
900 int, int);
901 static int update_menu_bar (struct frame *, int, int);
902 static int try_window_reusing_current_matrix (struct window *);
903 static int try_window_id (struct window *);
904 static int display_line (struct it *);
905 static int display_mode_lines (struct window *);
906 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
907 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
908 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
909 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
910 static void display_menu_bar (struct window *);
911 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
912 ptrdiff_t *);
913 static int display_string (const char *, Lisp_Object, Lisp_Object,
914 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
915 static void compute_line_metrics (struct it *);
916 static void run_redisplay_end_trigger_hook (struct it *);
917 static int get_overlay_strings (struct it *, ptrdiff_t);
918 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
919 static void next_overlay_string (struct it *);
920 static void reseat (struct it *, struct text_pos, int);
921 static void reseat_1 (struct it *, struct text_pos, int);
922 static void back_to_previous_visible_line_start (struct it *);
923 static void reseat_at_next_visible_line_start (struct it *, int);
924 static int next_element_from_ellipsis (struct it *);
925 static int next_element_from_display_vector (struct it *);
926 static int next_element_from_string (struct it *);
927 static int next_element_from_c_string (struct it *);
928 static int next_element_from_buffer (struct it *);
929 static int next_element_from_composition (struct it *);
930 static int next_element_from_image (struct it *);
931 static int next_element_from_stretch (struct it *);
932 static void load_overlay_strings (struct it *, ptrdiff_t);
933 static int init_from_display_pos (struct it *, struct window *,
934 struct display_pos *);
935 static void reseat_to_string (struct it *, const char *,
936 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
937 static int get_next_display_element (struct it *);
938 static enum move_it_result
939 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
940 enum move_operation_enum);
941 static void get_visually_first_element (struct it *);
942 static void init_to_row_start (struct it *, struct window *,
943 struct glyph_row *);
944 static int init_to_row_end (struct it *, struct window *,
945 struct glyph_row *);
946 static void back_to_previous_line_start (struct it *);
947 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
948 static struct text_pos string_pos_nchars_ahead (struct text_pos,
949 Lisp_Object, ptrdiff_t);
950 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
951 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
952 static ptrdiff_t number_of_chars (const char *, bool);
953 static void compute_stop_pos (struct it *);
954 static void compute_string_pos (struct text_pos *, struct text_pos,
955 Lisp_Object);
956 static int face_before_or_after_it_pos (struct it *, int);
957 static ptrdiff_t next_overlay_change (ptrdiff_t);
958 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
959 Lisp_Object, struct text_pos *, ptrdiff_t, int);
960 static int handle_single_display_spec (struct it *, Lisp_Object,
961 Lisp_Object, Lisp_Object,
962 struct text_pos *, ptrdiff_t, int, int);
963 static int underlying_face_id (struct it *);
964 static int in_ellipses_for_invisible_text_p (struct display_pos *,
965 struct window *);
967 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
968 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
970 #ifdef HAVE_WINDOW_SYSTEM
972 static void x_consider_frame_title (Lisp_Object);
973 static void update_tool_bar (struct frame *, int);
974 static int redisplay_tool_bar (struct frame *);
975 static void x_draw_bottom_divider (struct window *w);
976 static void notice_overwritten_cursor (struct window *,
977 enum glyph_row_area,
978 int, int, int, int);
979 static void append_stretch_glyph (struct it *, Lisp_Object,
980 int, int, int);
983 #endif /* HAVE_WINDOW_SYSTEM */
985 static void produce_special_glyphs (struct it *, enum display_element_type);
986 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
987 static bool coords_in_mouse_face_p (struct window *, int, int);
991 /***********************************************************************
992 Window display dimensions
993 ***********************************************************************/
995 /* Return the bottom boundary y-position for text lines in window W.
996 This is the first y position at which a line cannot start.
997 It is relative to the top of the window.
999 This is the height of W minus the height of a mode line, if any. */
1002 window_text_bottom_y (struct window *w)
1004 int height = WINDOW_PIXEL_HEIGHT (w);
1006 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1008 if (WINDOW_WANTS_MODELINE_P (w))
1009 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011 return height;
1014 /* Return the pixel width of display area AREA of window W.
1015 ANY_AREA means return the total width of W, not including
1016 fringes to the left and right of the window. */
1019 window_box_width (struct window *w, enum glyph_row_area area)
1021 int pixels = w->pixel_width;
1023 if (!w->pseudo_window_p)
1025 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1026 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1028 if (area == TEXT_AREA)
1029 pixels -= (WINDOW_MARGINS_WIDTH (w)
1030 + WINDOW_FRINGES_WIDTH (w));
1031 else if (area == LEFT_MARGIN_AREA)
1032 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1033 else if (area == RIGHT_MARGIN_AREA)
1034 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1037 return pixels;
1041 /* Return the pixel height of the display area of window W, not
1042 including mode lines of W, if any. */
1045 window_box_height (struct window *w)
1047 struct frame *f = XFRAME (w->frame);
1048 int height = WINDOW_PIXEL_HEIGHT (w);
1050 eassert (height >= 0);
1052 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1054 /* Note: the code below that determines the mode-line/header-line
1055 height is essentially the same as that contained in the macro
1056 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1057 the appropriate glyph row has its `mode_line_p' flag set,
1058 and if it doesn't, uses estimate_mode_line_height instead. */
1060 if (WINDOW_WANTS_MODELINE_P (w))
1062 struct glyph_row *ml_row
1063 = (w->current_matrix && w->current_matrix->rows
1064 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1065 : 0);
1066 if (ml_row && ml_row->mode_line_p)
1067 height -= ml_row->height;
1068 else
1069 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1072 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 struct glyph_row *hl_row
1075 = (w->current_matrix && w->current_matrix->rows
1076 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1077 : 0);
1078 if (hl_row && hl_row->mode_line_p)
1079 height -= hl_row->height;
1080 else
1081 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1084 /* With a very small font and a mode-line that's taller than
1085 default, we might end up with a negative height. */
1086 return max (0, height);
1089 /* Return the window-relative coordinate of the left edge of display
1090 area AREA of window W. ANY_AREA means return the left edge of the
1091 whole window, to the right of the left fringe of W. */
1094 window_box_left_offset (struct window *w, enum glyph_row_area area)
1096 int x;
1098 if (w->pseudo_window_p)
1099 return 0;
1101 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103 if (area == TEXT_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA));
1106 else if (area == RIGHT_MARGIN_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA)
1109 + window_box_width (w, TEXT_AREA)
1110 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1113 else if (area == LEFT_MARGIN_AREA
1114 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1115 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117 return x;
1121 /* Return the window-relative coordinate of the right edge of display
1122 area AREA of window W. ANY_AREA means return the right edge of the
1123 whole window, to the left of the right fringe of W. */
1126 window_box_right_offset (struct window *w, enum glyph_row_area area)
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. ANY_AREA means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1136 window_box_left (struct window *w, enum glyph_row_area area)
1138 struct frame *f = XFRAME (w->frame);
1139 int x;
1141 if (w->pseudo_window_p)
1142 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144 x = (WINDOW_LEFT_EDGE_X (w)
1145 + window_box_left_offset (w, area));
1147 return x;
1151 /* Return the frame-relative coordinate of the right edge of display
1152 area AREA of window W. ANY_AREA means return the right edge of the
1153 whole window, to the left of the right fringe of W. */
1156 window_box_right (struct window *w, enum glyph_row_area area)
1158 return window_box_left (w, area) + window_box_width (w, area);
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines, in frame-relative coordinates. ANY_AREA means the
1163 whole window, not including the left and right fringes of
1164 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1165 coordinates of the upper-left corner of the box. Return in
1166 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168 void
1169 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1170 int *box_y, int *box_width, int *box_height)
1172 if (box_width)
1173 *box_width = window_box_width (w, area);
1174 if (box_height)
1175 *box_height = window_box_height (w);
1176 if (box_x)
1177 *box_x = window_box_left (w, area);
1178 if (box_y)
1180 *box_y = WINDOW_TOP_EDGE_Y (w);
1181 if (WINDOW_WANTS_HEADER_LINE_P (w))
1182 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1186 #ifdef HAVE_WINDOW_SYSTEM
1188 /* Get the bounding box of the display area AREA of window W, without
1189 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1190 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1191 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1192 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1193 box. */
1195 static void
1196 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1197 int *bottom_right_x, int *bottom_right_y)
1199 window_box (w, ANY_AREA, top_left_x, top_left_y,
1200 bottom_right_x, bottom_right_y);
1201 *bottom_right_x += *top_left_x;
1202 *bottom_right_y += *top_left_y;
1205 #endif /* HAVE_WINDOW_SYSTEM */
1207 /***********************************************************************
1208 Utilities
1209 ***********************************************************************/
1211 /* Return the bottom y-position of the line the iterator IT is in.
1212 This can modify IT's settings. */
1215 line_bottom_y (struct it *it)
1217 int line_height = it->max_ascent + it->max_descent;
1218 int line_top_y = it->current_y;
1220 if (line_height == 0)
1222 if (last_height)
1223 line_height = last_height;
1224 else if (IT_CHARPOS (*it) < ZV)
1226 move_it_by_lines (it, 1);
1227 line_height = (it->max_ascent || it->max_descent
1228 ? it->max_ascent + it->max_descent
1229 : last_height);
1231 else
1233 struct glyph_row *row = it->glyph_row;
1235 /* Use the default character height. */
1236 it->glyph_row = NULL;
1237 it->what = IT_CHARACTER;
1238 it->c = ' ';
1239 it->len = 1;
1240 PRODUCE_GLYPHS (it);
1241 line_height = it->ascent + it->descent;
1242 it->glyph_row = row;
1246 return line_top_y + line_height;
1249 DEFUN ("line-pixel-height", Fline_pixel_height,
1250 Sline_pixel_height, 0, 0, 0,
1251 doc: /* Return height in pixels of text line in the selected window.
1253 Value is the height in pixels of the line at point. */)
1254 (void)
1256 struct it it;
1257 struct text_pos pt;
1258 struct window *w = XWINDOW (selected_window);
1260 SET_TEXT_POS (pt, PT, PT_BYTE);
1261 start_display (&it, w, pt);
1262 it.vpos = it.current_y = 0;
1263 last_height = 0;
1264 return make_number (line_bottom_y (&it));
1267 /* Return the default pixel height of text lines in window W. The
1268 value is the canonical height of the W frame's default font, plus
1269 any extra space required by the line-spacing variable or frame
1270 parameter.
1272 Implementation note: this ignores any line-spacing text properties
1273 put on the newline characters. This is because those properties
1274 only affect the _screen_ line ending in the newline (i.e., in a
1275 continued line, only the last screen line will be affected), which
1276 means only a small number of lines in a buffer can ever use this
1277 feature. Since this function is used to compute the default pixel
1278 equivalent of text lines in a window, we can safely ignore those
1279 few lines. For the same reasons, we ignore the line-height
1280 properties. */
1282 default_line_pixel_height (struct window *w)
1284 struct frame *f = WINDOW_XFRAME (w);
1285 int height = FRAME_LINE_HEIGHT (f);
1287 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1289 struct buffer *b = XBUFFER (w->contents);
1290 Lisp_Object val = BVAR (b, extra_line_spacing);
1292 if (NILP (val))
1293 val = BVAR (&buffer_defaults, extra_line_spacing);
1294 if (!NILP (val))
1296 if (RANGED_INTEGERP (0, val, INT_MAX))
1297 height += XFASTINT (val);
1298 else if (FLOATP (val))
1300 int addon = XFLOAT_DATA (val) * height + 0.5;
1302 if (addon >= 0)
1303 height += addon;
1306 else
1307 height += f->extra_line_spacing;
1310 return height;
1313 /* Subroutine of pos_visible_p below. Extracts a display string, if
1314 any, from the display spec given as its argument. */
1315 static Lisp_Object
1316 string_from_display_spec (Lisp_Object spec)
1318 if (CONSP (spec))
1320 while (CONSP (spec))
1322 if (STRINGP (XCAR (spec)))
1323 return XCAR (spec);
1324 spec = XCDR (spec);
1327 else if (VECTORP (spec))
1329 ptrdiff_t i;
1331 for (i = 0; i < ASIZE (spec); i++)
1333 if (STRINGP (AREF (spec, i)))
1334 return AREF (spec, i);
1336 return Qnil;
1339 return spec;
1343 /* Limit insanely large values of W->hscroll on frame F to the largest
1344 value that will still prevent first_visible_x and last_visible_x of
1345 'struct it' from overflowing an int. */
1346 static int
1347 window_hscroll_limited (struct window *w, struct frame *f)
1349 ptrdiff_t window_hscroll = w->hscroll;
1350 int window_text_width = window_box_width (w, TEXT_AREA);
1351 int colwidth = FRAME_COLUMN_WIDTH (f);
1353 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1354 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1356 return window_hscroll;
1359 /* Return 1 if position CHARPOS is visible in window W.
1360 CHARPOS < 0 means return info about WINDOW_END position.
1361 If visible, set *X and *Y to pixel coordinates of top left corner.
1362 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1363 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1366 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1367 int *rtop, int *rbot, int *rowh, int *vpos)
1369 struct it it;
1370 void *itdata = bidi_shelve_cache ();
1371 struct text_pos top;
1372 int visible_p = 0;
1373 struct buffer *old_buffer = NULL;
1375 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1376 return visible_p;
1378 if (XBUFFER (w->contents) != current_buffer)
1380 old_buffer = current_buffer;
1381 set_buffer_internal_1 (XBUFFER (w->contents));
1384 SET_TEXT_POS_FROM_MARKER (top, w->start);
1385 /* Scrolling a minibuffer window via scroll bar when the echo area
1386 shows long text sometimes resets the minibuffer contents behind
1387 our backs. */
1388 if (CHARPOS (top) > ZV)
1389 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1391 /* Compute exact mode line heights. */
1392 if (WINDOW_WANTS_MODELINE_P (w))
1393 w->mode_line_height
1394 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1395 BVAR (current_buffer, mode_line_format));
1397 if (WINDOW_WANTS_HEADER_LINE_P (w))
1398 w->header_line_height
1399 = display_mode_line (w, HEADER_LINE_FACE_ID,
1400 BVAR (current_buffer, header_line_format));
1402 start_display (&it, w, top);
1403 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1404 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1406 if (charpos >= 0
1407 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1408 && IT_CHARPOS (it) >= charpos)
1409 /* When scanning backwards under bidi iteration, move_it_to
1410 stops at or _before_ CHARPOS, because it stops at or to
1411 the _right_ of the character at CHARPOS. */
1412 || (it.bidi_p && it.bidi_it.scan_dir == -1
1413 && IT_CHARPOS (it) <= charpos)))
1415 /* We have reached CHARPOS, or passed it. How the call to
1416 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1417 or covered by a display property, move_it_to stops at the end
1418 of the invisible text, to the right of CHARPOS. (ii) If
1419 CHARPOS is in a display vector, move_it_to stops on its last
1420 glyph. */
1421 int top_x = it.current_x;
1422 int top_y = it.current_y;
1423 /* Calling line_bottom_y may change it.method, it.position, etc. */
1424 enum it_method it_method = it.method;
1425 int bottom_y = (last_height = 0, line_bottom_y (&it));
1426 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1428 if (top_y < window_top_y)
1429 visible_p = bottom_y > window_top_y;
1430 else if (top_y < it.last_visible_y)
1431 visible_p = true;
1432 if (bottom_y >= it.last_visible_y
1433 && it.bidi_p && it.bidi_it.scan_dir == -1
1434 && IT_CHARPOS (it) < charpos)
1436 /* When the last line of the window is scanned backwards
1437 under bidi iteration, we could be duped into thinking
1438 that we have passed CHARPOS, when in fact move_it_to
1439 simply stopped short of CHARPOS because it reached
1440 last_visible_y. To see if that's what happened, we call
1441 move_it_to again with a slightly larger vertical limit,
1442 and see if it actually moved vertically; if it did, we
1443 didn't really reach CHARPOS, which is beyond window end. */
1444 struct it save_it = it;
1445 /* Why 10? because we don't know how many canonical lines
1446 will the height of the next line(s) be. So we guess. */
1447 int ten_more_lines = 10 * default_line_pixel_height (w);
1449 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1450 MOVE_TO_POS | MOVE_TO_Y);
1451 if (it.current_y > top_y)
1452 visible_p = 0;
1454 it = save_it;
1456 if (visible_p)
1458 if (it_method == GET_FROM_DISPLAY_VECTOR)
1460 /* We stopped on the last glyph of a display vector.
1461 Try and recompute. Hack alert! */
1462 if (charpos < 2 || top.charpos >= charpos)
1463 top_x = it.glyph_row->x;
1464 else
1466 struct it it2, it2_prev;
1467 /* The idea is to get to the previous buffer
1468 position, consume the character there, and use
1469 the pixel coordinates we get after that. But if
1470 the previous buffer position is also displayed
1471 from a display vector, we need to consume all of
1472 the glyphs from that display vector. */
1473 start_display (&it2, w, top);
1474 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1475 /* If we didn't get to CHARPOS - 1, there's some
1476 replacing display property at that position, and
1477 we stopped after it. That is exactly the place
1478 whose coordinates we want. */
1479 if (IT_CHARPOS (it2) != charpos - 1)
1480 it2_prev = it2;
1481 else
1483 /* Iterate until we get out of the display
1484 vector that displays the character at
1485 CHARPOS - 1. */
1486 do {
1487 get_next_display_element (&it2);
1488 PRODUCE_GLYPHS (&it2);
1489 it2_prev = it2;
1490 set_iterator_to_next (&it2, 1);
1491 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1492 && IT_CHARPOS (it2) < charpos);
1494 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1495 || it2_prev.current_x > it2_prev.last_visible_x)
1496 top_x = it.glyph_row->x;
1497 else
1499 top_x = it2_prev.current_x;
1500 top_y = it2_prev.current_y;
1504 else if (IT_CHARPOS (it) != charpos)
1506 Lisp_Object cpos = make_number (charpos);
1507 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1508 Lisp_Object string = string_from_display_spec (spec);
1509 struct text_pos tpos;
1510 int replacing_spec_p;
1511 bool newline_in_string
1512 = (STRINGP (string)
1513 && memchr (SDATA (string), '\n', SBYTES (string)));
1515 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1516 replacing_spec_p
1517 = (!NILP (spec)
1518 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1519 charpos, FRAME_WINDOW_P (it.f)));
1520 /* The tricky code below is needed because there's a
1521 discrepancy between move_it_to and how we set cursor
1522 when PT is at the beginning of a portion of text
1523 covered by a display property or an overlay with a
1524 display property, or the display line ends in a
1525 newline from a display string. move_it_to will stop
1526 _after_ such display strings, whereas
1527 set_cursor_from_row conspires with cursor_row_p to
1528 place the cursor on the first glyph produced from the
1529 display string. */
1531 /* We have overshoot PT because it is covered by a
1532 display property that replaces the text it covers.
1533 If the string includes embedded newlines, we are also
1534 in the wrong display line. Backtrack to the correct
1535 line, where the display property begins. */
1536 if (replacing_spec_p)
1538 Lisp_Object startpos, endpos;
1539 EMACS_INT start, end;
1540 struct it it3;
1541 int it3_moved;
1543 /* Find the first and the last buffer positions
1544 covered by the display string. */
1545 endpos =
1546 Fnext_single_char_property_change (cpos, Qdisplay,
1547 Qnil, Qnil);
1548 startpos =
1549 Fprevious_single_char_property_change (endpos, Qdisplay,
1550 Qnil, Qnil);
1551 start = XFASTINT (startpos);
1552 end = XFASTINT (endpos);
1553 /* Move to the last buffer position before the
1554 display property. */
1555 start_display (&it3, w, top);
1556 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1557 /* Move forward one more line if the position before
1558 the display string is a newline or if it is the
1559 rightmost character on a line that is
1560 continued or word-wrapped. */
1561 if (it3.method == GET_FROM_BUFFER
1562 && (it3.c == '\n'
1563 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1564 move_it_by_lines (&it3, 1);
1565 else if (move_it_in_display_line_to (&it3, -1,
1566 it3.current_x
1567 + it3.pixel_width,
1568 MOVE_TO_X)
1569 == MOVE_LINE_CONTINUED)
1571 move_it_by_lines (&it3, 1);
1572 /* When we are under word-wrap, the #$@%!
1573 move_it_by_lines moves 2 lines, so we need to
1574 fix that up. */
1575 if (it3.line_wrap == WORD_WRAP)
1576 move_it_by_lines (&it3, -1);
1579 /* Record the vertical coordinate of the display
1580 line where we wound up. */
1581 top_y = it3.current_y;
1582 if (it3.bidi_p)
1584 /* When characters are reordered for display,
1585 the character displayed to the left of the
1586 display string could be _after_ the display
1587 property in the logical order. Use the
1588 smallest vertical position of these two. */
1589 start_display (&it3, w, top);
1590 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1591 if (it3.current_y < top_y)
1592 top_y = it3.current_y;
1594 /* Move from the top of the window to the beginning
1595 of the display line where the display string
1596 begins. */
1597 start_display (&it3, w, top);
1598 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1599 /* If it3_moved stays zero after the 'while' loop
1600 below, that means we already were at a newline
1601 before the loop (e.g., the display string begins
1602 with a newline), so we don't need to (and cannot)
1603 inspect the glyphs of it3.glyph_row, because
1604 PRODUCE_GLYPHS will not produce anything for a
1605 newline, and thus it3.glyph_row stays at its
1606 stale content it got at top of the window. */
1607 it3_moved = 0;
1608 /* Finally, advance the iterator until we hit the
1609 first display element whose character position is
1610 CHARPOS, or until the first newline from the
1611 display string, which signals the end of the
1612 display line. */
1613 while (get_next_display_element (&it3))
1615 PRODUCE_GLYPHS (&it3);
1616 if (IT_CHARPOS (it3) == charpos
1617 || ITERATOR_AT_END_OF_LINE_P (&it3))
1618 break;
1619 it3_moved = 1;
1620 set_iterator_to_next (&it3, 0);
1622 top_x = it3.current_x - it3.pixel_width;
1623 /* Normally, we would exit the above loop because we
1624 found the display element whose character
1625 position is CHARPOS. For the contingency that we
1626 didn't, and stopped at the first newline from the
1627 display string, move back over the glyphs
1628 produced from the string, until we find the
1629 rightmost glyph not from the string. */
1630 if (it3_moved
1631 && newline_in_string
1632 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1634 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1635 + it3.glyph_row->used[TEXT_AREA];
1637 while (EQ ((g - 1)->object, string))
1639 --g;
1640 top_x -= g->pixel_width;
1642 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1643 + it3.glyph_row->used[TEXT_AREA]);
1648 *x = top_x;
1649 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1650 *rtop = max (0, window_top_y - top_y);
1651 *rbot = max (0, bottom_y - it.last_visible_y);
1652 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1653 - max (top_y, window_top_y)));
1654 *vpos = it.vpos;
1657 else
1659 /* We were asked to provide info about WINDOW_END. */
1660 struct it it2;
1661 void *it2data = NULL;
1663 SAVE_IT (it2, it, it2data);
1664 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1665 move_it_by_lines (&it, 1);
1666 if (charpos < IT_CHARPOS (it)
1667 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1669 visible_p = true;
1670 RESTORE_IT (&it2, &it2, it2data);
1671 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1672 *x = it2.current_x;
1673 *y = it2.current_y + it2.max_ascent - it2.ascent;
1674 *rtop = max (0, -it2.current_y);
1675 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1676 - it.last_visible_y));
1677 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1678 it.last_visible_y)
1679 - max (it2.current_y,
1680 WINDOW_HEADER_LINE_HEIGHT (w))));
1681 *vpos = it2.vpos;
1683 else
1684 bidi_unshelve_cache (it2data, 1);
1686 bidi_unshelve_cache (itdata, 0);
1688 if (old_buffer)
1689 set_buffer_internal_1 (old_buffer);
1691 if (visible_p && w->hscroll > 0)
1692 *x -=
1693 window_hscroll_limited (w, WINDOW_XFRAME (w))
1694 * WINDOW_FRAME_COLUMN_WIDTH (w);
1696 #if 0
1697 /* Debugging code. */
1698 if (visible_p)
1699 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1700 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1701 else
1702 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1703 #endif
1705 return visible_p;
1709 /* Return the next character from STR. Return in *LEN the length of
1710 the character. This is like STRING_CHAR_AND_LENGTH but never
1711 returns an invalid character. If we find one, we return a `?', but
1712 with the length of the invalid character. */
1714 static int
1715 string_char_and_length (const unsigned char *str, int *len)
1717 int c;
1719 c = STRING_CHAR_AND_LENGTH (str, *len);
1720 if (!CHAR_VALID_P (c))
1721 /* We may not change the length here because other places in Emacs
1722 don't use this function, i.e. they silently accept invalid
1723 characters. */
1724 c = '?';
1726 return c;
1731 /* Given a position POS containing a valid character and byte position
1732 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734 static struct text_pos
1735 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 eassert (STRINGP (string) && nchars >= 0);
1739 if (STRING_MULTIBYTE (string))
1741 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1742 int len;
1744 while (nchars--)
1746 string_char_and_length (p, &len);
1747 p += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755 return pos;
1759 /* Value is the text position, i.e. character and byte position,
1760 for character position CHARPOS in STRING. */
1762 static struct text_pos
1763 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 struct text_pos pos;
1766 eassert (STRINGP (string));
1767 eassert (charpos >= 0);
1768 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1769 return pos;
1773 /* Value is a text position, i.e. character and byte position, for
1774 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1775 means recognize multibyte characters. */
1777 static struct text_pos
1778 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 struct text_pos pos;
1782 eassert (s != NULL);
1783 eassert (charpos >= 0);
1785 if (multibyte_p)
1787 int len;
1789 SET_TEXT_POS (pos, 0, 0);
1790 while (charpos--)
1792 string_char_and_length ((const unsigned char *) s, &len);
1793 s += len;
1794 CHARPOS (pos) += 1;
1795 BYTEPOS (pos) += len;
1798 else
1799 SET_TEXT_POS (pos, charpos, charpos);
1801 return pos;
1805 /* Value is the number of characters in C string S. MULTIBYTE_P
1806 non-zero means recognize multibyte characters. */
1808 static ptrdiff_t
1809 number_of_chars (const char *s, bool multibyte_p)
1811 ptrdiff_t nchars;
1813 if (multibyte_p)
1815 ptrdiff_t rest = strlen (s);
1816 int len;
1817 const unsigned char *p = (const unsigned char *) s;
1819 for (nchars = 0; rest > 0; ++nchars)
1821 string_char_and_length (p, &len);
1822 rest -= len, p += len;
1825 else
1826 nchars = strlen (s);
1828 return nchars;
1832 /* Compute byte position NEWPOS->bytepos corresponding to
1833 NEWPOS->charpos. POS is a known position in string STRING.
1834 NEWPOS->charpos must be >= POS.charpos. */
1836 static void
1837 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 eassert (STRINGP (string));
1840 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842 if (STRING_MULTIBYTE (string))
1843 *newpos = string_pos_nchars_ahead (pos, string,
1844 CHARPOS (*newpos) - CHARPOS (pos));
1845 else
1846 BYTEPOS (*newpos) = CHARPOS (*newpos);
1849 /* EXPORT:
1850 Return an estimation of the pixel height of mode or header lines on
1851 frame F. FACE_ID specifies what line's height to estimate. */
1854 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1859 int height = FONT_HEIGHT (FRAME_FONT (f));
1861 /* This function is called so early when Emacs starts that the face
1862 cache and mode line face are not yet initialized. */
1863 if (FRAME_FACE_CACHE (f))
1865 struct face *face = FACE_FROM_ID (f, face_id);
1866 if (face)
1868 if (face->font)
1869 height = FONT_HEIGHT (face->font);
1870 if (face->box_line_width > 0)
1871 height += 2 * face->box_line_width;
1875 return height;
1877 #endif
1879 return 1;
1882 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1883 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1884 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1885 not force the value into range. */
1887 void
1888 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1889 int *x, int *y, NativeRectangle *bounds, int noclip)
1892 #ifdef HAVE_WINDOW_SYSTEM
1893 if (FRAME_WINDOW_P (f))
1895 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1896 even for negative values. */
1897 if (pix_x < 0)
1898 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1899 if (pix_y < 0)
1900 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1903 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905 if (bounds)
1906 STORE_NATIVE_RECT (*bounds,
1907 FRAME_COL_TO_PIXEL_X (f, pix_x),
1908 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1909 FRAME_COLUMN_WIDTH (f) - 1,
1910 FRAME_LINE_HEIGHT (f) - 1);
1912 /* PXW: Should we clip pixels before converting to columns/lines? */
1913 if (!noclip)
1915 if (pix_x < 0)
1916 pix_x = 0;
1917 else if (pix_x > FRAME_TOTAL_COLS (f))
1918 pix_x = FRAME_TOTAL_COLS (f);
1920 if (pix_y < 0)
1921 pix_y = 0;
1922 else if (pix_y > FRAME_LINES (f))
1923 pix_y = FRAME_LINES (f);
1926 #endif
1928 *x = pix_x;
1929 *y = pix_y;
1933 /* Find the glyph under window-relative coordinates X/Y in window W.
1934 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1935 strings. Return in *HPOS and *VPOS the row and column number of
1936 the glyph found. Return in *AREA the glyph area containing X.
1937 Value is a pointer to the glyph found or null if X/Y is not on
1938 text, or we can't tell because W's current matrix is not up to
1939 date. */
1941 static struct glyph *
1942 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1943 int *dx, int *dy, int *area)
1945 struct glyph *glyph, *end;
1946 struct glyph_row *row = NULL;
1947 int x0, i;
1949 /* Find row containing Y. Give up if some row is not enabled. */
1950 for (i = 0; i < w->current_matrix->nrows; ++i)
1952 row = MATRIX_ROW (w->current_matrix, i);
1953 if (!row->enabled_p)
1954 return NULL;
1955 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1956 break;
1959 *vpos = i;
1960 *hpos = 0;
1962 /* Give up if Y is not in the window. */
1963 if (i == w->current_matrix->nrows)
1964 return NULL;
1966 /* Get the glyph area containing X. */
1967 if (w->pseudo_window_p)
1969 *area = TEXT_AREA;
1970 x0 = 0;
1972 else
1974 if (x < window_box_left_offset (w, TEXT_AREA))
1976 *area = LEFT_MARGIN_AREA;
1977 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1979 else if (x < window_box_right_offset (w, TEXT_AREA))
1981 *area = TEXT_AREA;
1982 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1984 else
1986 *area = RIGHT_MARGIN_AREA;
1987 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1991 /* Find glyph containing X. */
1992 glyph = row->glyphs[*area];
1993 end = glyph + row->used[*area];
1994 x -= x0;
1995 while (glyph < end && x >= glyph->pixel_width)
1997 x -= glyph->pixel_width;
1998 ++glyph;
2001 if (glyph == end)
2002 return NULL;
2004 if (dx)
2006 *dx = x;
2007 *dy = y - (row->y + row->ascent - glyph->ascent);
2010 *hpos = glyph - row->glyphs[*area];
2011 return glyph;
2014 /* Convert frame-relative x/y to coordinates relative to window W.
2015 Takes pseudo-windows into account. */
2017 static void
2018 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2020 if (w->pseudo_window_p)
2022 /* A pseudo-window is always full-width, and starts at the
2023 left edge of the frame, plus a frame border. */
2024 struct frame *f = XFRAME (w->frame);
2025 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2026 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2028 else
2030 *x -= WINDOW_LEFT_EDGE_X (w);
2031 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2035 #ifdef HAVE_WINDOW_SYSTEM
2037 /* EXPORT:
2038 Return in RECTS[] at most N clipping rectangles for glyph string S.
2039 Return the number of stored rectangles. */
2042 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2044 XRectangle r;
2046 if (n <= 0)
2047 return 0;
2049 if (s->row->full_width_p)
2051 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2052 r.x = WINDOW_LEFT_EDGE_X (s->w);
2053 if (s->row->mode_line_p)
2054 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2055 else
2056 r.width = WINDOW_PIXEL_WIDTH (s->w);
2058 /* Unless displaying a mode or menu bar line, which are always
2059 fully visible, clip to the visible part of the row. */
2060 if (s->w->pseudo_window_p)
2061 r.height = s->row->visible_height;
2062 else
2063 r.height = s->height;
2065 else
2067 /* This is a text line that may be partially visible. */
2068 r.x = window_box_left (s->w, s->area);
2069 r.width = window_box_width (s->w, s->area);
2070 r.height = s->row->visible_height;
2073 if (s->clip_head)
2074 if (r.x < s->clip_head->x)
2076 if (r.width >= s->clip_head->x - r.x)
2077 r.width -= s->clip_head->x - r.x;
2078 else
2079 r.width = 0;
2080 r.x = s->clip_head->x;
2082 if (s->clip_tail)
2083 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2085 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2086 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2087 else
2088 r.width = 0;
2091 /* If S draws overlapping rows, it's sufficient to use the top and
2092 bottom of the window for clipping because this glyph string
2093 intentionally draws over other lines. */
2094 if (s->for_overlaps)
2096 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2097 r.height = window_text_bottom_y (s->w) - r.y;
2099 /* Alas, the above simple strategy does not work for the
2100 environments with anti-aliased text: if the same text is
2101 drawn onto the same place multiple times, it gets thicker.
2102 If the overlap we are processing is for the erased cursor, we
2103 take the intersection with the rectangle of the cursor. */
2104 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2106 XRectangle rc, r_save = r;
2108 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2109 rc.y = s->w->phys_cursor.y;
2110 rc.width = s->w->phys_cursor_width;
2111 rc.height = s->w->phys_cursor_height;
2113 x_intersect_rectangles (&r_save, &rc, &r);
2116 else
2118 /* Don't use S->y for clipping because it doesn't take partially
2119 visible lines into account. For example, it can be negative for
2120 partially visible lines at the top of a window. */
2121 if (!s->row->full_width_p
2122 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2123 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2124 else
2125 r.y = max (0, s->row->y);
2128 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2130 /* If drawing the cursor, don't let glyph draw outside its
2131 advertised boundaries. Cleartype does this under some circumstances. */
2132 if (s->hl == DRAW_CURSOR)
2134 struct glyph *glyph = s->first_glyph;
2135 int height, max_y;
2137 if (s->x > r.x)
2139 r.width -= s->x - r.x;
2140 r.x = s->x;
2142 r.width = min (r.width, glyph->pixel_width);
2144 /* If r.y is below window bottom, ensure that we still see a cursor. */
2145 height = min (glyph->ascent + glyph->descent,
2146 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2147 max_y = window_text_bottom_y (s->w) - height;
2148 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2149 if (s->ybase - glyph->ascent > max_y)
2151 r.y = max_y;
2152 r.height = height;
2154 else
2156 /* Don't draw cursor glyph taller than our actual glyph. */
2157 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2158 if (height < r.height)
2160 max_y = r.y + r.height;
2161 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2162 r.height = min (max_y - r.y, height);
2167 if (s->row->clip)
2169 XRectangle r_save = r;
2171 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2172 r.width = 0;
2175 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2176 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2178 #ifdef CONVERT_FROM_XRECT
2179 CONVERT_FROM_XRECT (r, *rects);
2180 #else
2181 *rects = r;
2182 #endif
2183 return 1;
2185 else
2187 /* If we are processing overlapping and allowed to return
2188 multiple clipping rectangles, we exclude the row of the glyph
2189 string from the clipping rectangle. This is to avoid drawing
2190 the same text on the environment with anti-aliasing. */
2191 #ifdef CONVERT_FROM_XRECT
2192 XRectangle rs[2];
2193 #else
2194 XRectangle *rs = rects;
2195 #endif
2196 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2198 if (s->for_overlaps & OVERLAPS_PRED)
2200 rs[i] = r;
2201 if (r.y + r.height > row_y)
2203 if (r.y < row_y)
2204 rs[i].height = row_y - r.y;
2205 else
2206 rs[i].height = 0;
2208 i++;
2210 if (s->for_overlaps & OVERLAPS_SUCC)
2212 rs[i] = r;
2213 if (r.y < row_y + s->row->visible_height)
2215 if (r.y + r.height > row_y + s->row->visible_height)
2217 rs[i].y = row_y + s->row->visible_height;
2218 rs[i].height = r.y + r.height - rs[i].y;
2220 else
2221 rs[i].height = 0;
2223 i++;
2226 n = i;
2227 #ifdef CONVERT_FROM_XRECT
2228 for (i = 0; i < n; i++)
2229 CONVERT_FROM_XRECT (rs[i], rects[i]);
2230 #endif
2231 return n;
2235 /* EXPORT:
2236 Return in *NR the clipping rectangle for glyph string S. */
2238 void
2239 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2241 get_glyph_string_clip_rects (s, nr, 1);
2245 /* EXPORT:
2246 Return the position and height of the phys cursor in window W.
2247 Set w->phys_cursor_width to width of phys cursor.
2250 void
2251 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2252 struct glyph *glyph, int *xp, int *yp, int *heightp)
2254 struct frame *f = XFRAME (WINDOW_FRAME (w));
2255 int x, y, wd, h, h0, y0;
2257 /* Compute the width of the rectangle to draw. If on a stretch
2258 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2259 rectangle as wide as the glyph, but use a canonical character
2260 width instead. */
2261 wd = glyph->pixel_width - 1;
2262 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2263 wd++; /* Why? */
2264 #endif
2266 x = w->phys_cursor.x;
2267 if (x < 0)
2269 wd += x;
2270 x = 0;
2273 if (glyph->type == STRETCH_GLYPH
2274 && !x_stretch_cursor_p)
2275 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2276 w->phys_cursor_width = wd;
2278 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2280 /* If y is below window bottom, ensure that we still see a cursor. */
2281 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2283 h = max (h0, glyph->ascent + glyph->descent);
2284 h0 = min (h0, glyph->ascent + glyph->descent);
2286 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2287 if (y < y0)
2289 h = max (h - (y0 - y) + 1, h0);
2290 y = y0 - 1;
2292 else
2294 y0 = window_text_bottom_y (w) - h0;
2295 if (y > y0)
2297 h += y - y0;
2298 y = y0;
2302 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2303 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2304 *heightp = h;
2308 * Remember which glyph the mouse is over.
2311 void
2312 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2314 Lisp_Object window;
2315 struct window *w;
2316 struct glyph_row *r, *gr, *end_row;
2317 enum window_part part;
2318 enum glyph_row_area area;
2319 int x, y, width, height;
2321 /* Try to determine frame pixel position and size of the glyph under
2322 frame pixel coordinates X/Y on frame F. */
2324 if (window_resize_pixelwise)
2326 width = height = 1;
2327 goto virtual_glyph;
2329 else if (!f->glyphs_initialized_p
2330 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2331 NILP (window)))
2333 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2334 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2335 goto virtual_glyph;
2338 w = XWINDOW (window);
2339 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2340 height = WINDOW_FRAME_LINE_HEIGHT (w);
2342 x = window_relative_x_coord (w, part, gx);
2343 y = gy - WINDOW_TOP_EDGE_Y (w);
2345 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2346 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2348 if (w->pseudo_window_p)
2350 area = TEXT_AREA;
2351 part = ON_MODE_LINE; /* Don't adjust margin. */
2352 goto text_glyph;
2355 switch (part)
2357 case ON_LEFT_MARGIN:
2358 area = LEFT_MARGIN_AREA;
2359 goto text_glyph;
2361 case ON_RIGHT_MARGIN:
2362 area = RIGHT_MARGIN_AREA;
2363 goto text_glyph;
2365 case ON_HEADER_LINE:
2366 case ON_MODE_LINE:
2367 gr = (part == ON_HEADER_LINE
2368 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2369 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2370 gy = gr->y;
2371 area = TEXT_AREA;
2372 goto text_glyph_row_found;
2374 case ON_TEXT:
2375 area = TEXT_AREA;
2377 text_glyph:
2378 gr = 0; gy = 0;
2379 for (; r <= end_row && r->enabled_p; ++r)
2380 if (r->y + r->height > y)
2382 gr = r; gy = r->y;
2383 break;
2386 text_glyph_row_found:
2387 if (gr && gy <= y)
2389 struct glyph *g = gr->glyphs[area];
2390 struct glyph *end = g + gr->used[area];
2392 height = gr->height;
2393 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2394 if (gx + g->pixel_width > x)
2395 break;
2397 if (g < end)
2399 if (g->type == IMAGE_GLYPH)
2401 /* Don't remember when mouse is over image, as
2402 image may have hot-spots. */
2403 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2404 return;
2406 width = g->pixel_width;
2408 else
2410 /* Use nominal char spacing at end of line. */
2411 x -= gx;
2412 gx += (x / width) * width;
2415 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2416 gx += window_box_left_offset (w, area);
2418 else
2420 /* Use nominal line height at end of window. */
2421 gx = (x / width) * width;
2422 y -= gy;
2423 gy += (y / height) * height;
2425 break;
2427 case ON_LEFT_FRINGE:
2428 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2429 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2430 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2431 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2432 goto row_glyph;
2434 case ON_RIGHT_FRINGE:
2435 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2436 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2437 : window_box_right_offset (w, TEXT_AREA));
2438 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2439 goto row_glyph;
2441 case ON_SCROLL_BAR:
2442 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2444 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2445 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2446 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2447 : 0)));
2448 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2450 row_glyph:
2451 gr = 0, gy = 0;
2452 for (; r <= end_row && r->enabled_p; ++r)
2453 if (r->y + r->height > y)
2455 gr = r; gy = r->y;
2456 break;
2459 if (gr && gy <= y)
2460 height = gr->height;
2461 else
2463 /* Use nominal line height at end of window. */
2464 y -= gy;
2465 gy += (y / height) * height;
2467 break;
2469 default:
2471 virtual_glyph:
2472 /* If there is no glyph under the mouse, then we divide the screen
2473 into a grid of the smallest glyph in the frame, and use that
2474 as our "glyph". */
2476 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2477 round down even for negative values. */
2478 if (gx < 0)
2479 gx -= width - 1;
2480 if (gy < 0)
2481 gy -= height - 1;
2483 gx = (gx / width) * width;
2484 gy = (gy / height) * height;
2486 goto store_rect;
2489 gx += WINDOW_LEFT_EDGE_X (w);
2490 gy += WINDOW_TOP_EDGE_Y (w);
2492 store_rect:
2493 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2495 /* Visible feedback for debugging. */
2496 #if 0
2497 #if HAVE_X_WINDOWS
2498 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2499 f->output_data.x->normal_gc,
2500 gx, gy, width, height);
2501 #endif
2502 #endif
2506 #endif /* HAVE_WINDOW_SYSTEM */
2508 static void
2509 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2511 eassert (w);
2512 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2513 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2514 w->window_end_vpos
2515 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2518 /***********************************************************************
2519 Lisp form evaluation
2520 ***********************************************************************/
2522 /* Error handler for safe_eval and safe_call. */
2524 static Lisp_Object
2525 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2527 add_to_log ("Error during redisplay: %S signaled %S",
2528 Flist (nargs, args), arg);
2529 return Qnil;
2532 /* Call function FUNC with the rest of NARGS - 1 arguments
2533 following. Return the result, or nil if something went
2534 wrong. Prevent redisplay during the evaluation. */
2536 Lisp_Object
2537 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2539 Lisp_Object val;
2541 if (inhibit_eval_during_redisplay)
2542 val = Qnil;
2543 else
2545 va_list ap;
2546 ptrdiff_t i;
2547 ptrdiff_t count = SPECPDL_INDEX ();
2548 struct gcpro gcpro1;
2549 Lisp_Object *args = alloca (nargs * word_size);
2551 args[0] = func;
2552 va_start (ap, func);
2553 for (i = 1; i < nargs; i++)
2554 args[i] = va_arg (ap, Lisp_Object);
2555 va_end (ap);
2557 GCPRO1 (args[0]);
2558 gcpro1.nvars = nargs;
2559 specbind (Qinhibit_redisplay, Qt);
2560 /* Use Qt to ensure debugger does not run,
2561 so there is no possibility of wanting to redisplay. */
2562 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2563 safe_eval_handler);
2564 UNGCPRO;
2565 val = unbind_to (count, val);
2568 return val;
2572 /* Call function FN with one argument ARG.
2573 Return the result, or nil if something went wrong. */
2575 Lisp_Object
2576 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2578 return safe_call (2, fn, arg);
2581 static Lisp_Object Qeval;
2583 Lisp_Object
2584 safe_eval (Lisp_Object sexpr)
2586 return safe_call1 (Qeval, sexpr);
2589 /* Call function FN with two arguments ARG1 and ARG2.
2590 Return the result, or nil if something went wrong. */
2592 Lisp_Object
2593 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2595 return safe_call (3, fn, arg1, arg2);
2600 /***********************************************************************
2601 Debugging
2602 ***********************************************************************/
2604 #if 0
2606 /* Define CHECK_IT to perform sanity checks on iterators.
2607 This is for debugging. It is too slow to do unconditionally. */
2609 static void
2610 check_it (struct it *it)
2612 if (it->method == GET_FROM_STRING)
2614 eassert (STRINGP (it->string));
2615 eassert (IT_STRING_CHARPOS (*it) >= 0);
2617 else
2619 eassert (IT_STRING_CHARPOS (*it) < 0);
2620 if (it->method == GET_FROM_BUFFER)
2622 /* Check that character and byte positions agree. */
2623 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2627 if (it->dpvec)
2628 eassert (it->current.dpvec_index >= 0);
2629 else
2630 eassert (it->current.dpvec_index < 0);
2633 #define CHECK_IT(IT) check_it ((IT))
2635 #else /* not 0 */
2637 #define CHECK_IT(IT) (void) 0
2639 #endif /* not 0 */
2642 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2644 /* Check that the window end of window W is what we expect it
2645 to be---the last row in the current matrix displaying text. */
2647 static void
2648 check_window_end (struct window *w)
2650 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2652 struct glyph_row *row;
2653 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2654 !row->enabled_p
2655 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2656 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2660 #define CHECK_WINDOW_END(W) check_window_end ((W))
2662 #else
2664 #define CHECK_WINDOW_END(W) (void) 0
2666 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2668 /***********************************************************************
2669 Iterator initialization
2670 ***********************************************************************/
2672 /* Initialize IT for displaying current_buffer in window W, starting
2673 at character position CHARPOS. CHARPOS < 0 means that no buffer
2674 position is specified which is useful when the iterator is assigned
2675 a position later. BYTEPOS is the byte position corresponding to
2676 CHARPOS.
2678 If ROW is not null, calls to produce_glyphs with IT as parameter
2679 will produce glyphs in that row.
2681 BASE_FACE_ID is the id of a base face to use. It must be one of
2682 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2683 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2684 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2686 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2687 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2688 will be initialized to use the corresponding mode line glyph row of
2689 the desired matrix of W. */
2691 void
2692 init_iterator (struct it *it, struct window *w,
2693 ptrdiff_t charpos, ptrdiff_t bytepos,
2694 struct glyph_row *row, enum face_id base_face_id)
2696 enum face_id remapped_base_face_id = base_face_id;
2698 /* Some precondition checks. */
2699 eassert (w != NULL && it != NULL);
2700 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2701 && charpos <= ZV));
2703 /* If face attributes have been changed since the last redisplay,
2704 free realized faces now because they depend on face definitions
2705 that might have changed. Don't free faces while there might be
2706 desired matrices pending which reference these faces. */
2707 if (face_change_count && !inhibit_free_realized_faces)
2709 face_change_count = 0;
2710 free_all_realized_faces (Qnil);
2713 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2714 if (! NILP (Vface_remapping_alist))
2715 remapped_base_face_id
2716 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2718 /* Use one of the mode line rows of W's desired matrix if
2719 appropriate. */
2720 if (row == NULL)
2722 if (base_face_id == MODE_LINE_FACE_ID
2723 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2724 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2725 else if (base_face_id == HEADER_LINE_FACE_ID)
2726 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2729 /* Clear IT. */
2730 memset (it, 0, sizeof *it);
2731 it->current.overlay_string_index = -1;
2732 it->current.dpvec_index = -1;
2733 it->base_face_id = remapped_base_face_id;
2734 it->string = Qnil;
2735 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2736 it->paragraph_embedding = L2R;
2737 it->bidi_it.string.lstring = Qnil;
2738 it->bidi_it.string.s = NULL;
2739 it->bidi_it.string.bufpos = 0;
2740 it->bidi_it.w = w;
2742 /* The window in which we iterate over current_buffer: */
2743 XSETWINDOW (it->window, w);
2744 it->w = w;
2745 it->f = XFRAME (w->frame);
2747 it->cmp_it.id = -1;
2749 /* Extra space between lines (on window systems only). */
2750 if (base_face_id == DEFAULT_FACE_ID
2751 && FRAME_WINDOW_P (it->f))
2753 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2755 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2756 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2757 * FRAME_LINE_HEIGHT (it->f));
2758 else if (it->f->extra_line_spacing > 0)
2759 it->extra_line_spacing = it->f->extra_line_spacing;
2760 it->max_extra_line_spacing = 0;
2763 /* If realized faces have been removed, e.g. because of face
2764 attribute changes of named faces, recompute them. When running
2765 in batch mode, the face cache of the initial frame is null. If
2766 we happen to get called, make a dummy face cache. */
2767 if (FRAME_FACE_CACHE (it->f) == NULL)
2768 init_frame_faces (it->f);
2769 if (FRAME_FACE_CACHE (it->f)->used == 0)
2770 recompute_basic_faces (it->f);
2772 /* Current value of the `slice', `space-width', and 'height' properties. */
2773 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2774 it->space_width = Qnil;
2775 it->font_height = Qnil;
2776 it->override_ascent = -1;
2778 /* Are control characters displayed as `^C'? */
2779 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2781 /* -1 means everything between a CR and the following line end
2782 is invisible. >0 means lines indented more than this value are
2783 invisible. */
2784 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2785 ? (clip_to_bounds
2786 (-1, XINT (BVAR (current_buffer, selective_display)),
2787 PTRDIFF_MAX))
2788 : (!NILP (BVAR (current_buffer, selective_display))
2789 ? -1 : 0));
2790 it->selective_display_ellipsis_p
2791 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2793 /* Display table to use. */
2794 it->dp = window_display_table (w);
2796 /* Are multibyte characters enabled in current_buffer? */
2797 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2799 /* Get the position at which the redisplay_end_trigger hook should
2800 be run, if it is to be run at all. */
2801 if (MARKERP (w->redisplay_end_trigger)
2802 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2803 it->redisplay_end_trigger_charpos
2804 = marker_position (w->redisplay_end_trigger);
2805 else if (INTEGERP (w->redisplay_end_trigger))
2806 it->redisplay_end_trigger_charpos =
2807 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2809 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2811 /* Are lines in the display truncated? */
2812 if (base_face_id != DEFAULT_FACE_ID
2813 || it->w->hscroll
2814 || (! WINDOW_FULL_WIDTH_P (it->w)
2815 && ((!NILP (Vtruncate_partial_width_windows)
2816 && !INTEGERP (Vtruncate_partial_width_windows))
2817 || (INTEGERP (Vtruncate_partial_width_windows)
2818 /* PXW: Shall we do something about this? */
2819 && (WINDOW_TOTAL_COLS (it->w)
2820 < XINT (Vtruncate_partial_width_windows))))))
2821 it->line_wrap = TRUNCATE;
2822 else if (NILP (BVAR (current_buffer, truncate_lines)))
2823 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2824 ? WINDOW_WRAP : WORD_WRAP;
2825 else
2826 it->line_wrap = TRUNCATE;
2828 /* Get dimensions of truncation and continuation glyphs. These are
2829 displayed as fringe bitmaps under X, but we need them for such
2830 frames when the fringes are turned off. But leave the dimensions
2831 zero for tooltip frames, as these glyphs look ugly there and also
2832 sabotage calculations of tooltip dimensions in x-show-tip. */
2833 #ifdef HAVE_WINDOW_SYSTEM
2834 if (!(FRAME_WINDOW_P (it->f)
2835 && FRAMEP (tip_frame)
2836 && it->f == XFRAME (tip_frame)))
2837 #endif
2839 if (it->line_wrap == TRUNCATE)
2841 /* We will need the truncation glyph. */
2842 eassert (it->glyph_row == NULL);
2843 produce_special_glyphs (it, IT_TRUNCATION);
2844 it->truncation_pixel_width = it->pixel_width;
2846 else
2848 /* We will need the continuation glyph. */
2849 eassert (it->glyph_row == NULL);
2850 produce_special_glyphs (it, IT_CONTINUATION);
2851 it->continuation_pixel_width = it->pixel_width;
2855 /* Reset these values to zero because the produce_special_glyphs
2856 above has changed them. */
2857 it->pixel_width = it->ascent = it->descent = 0;
2858 it->phys_ascent = it->phys_descent = 0;
2860 /* Set this after getting the dimensions of truncation and
2861 continuation glyphs, so that we don't produce glyphs when calling
2862 produce_special_glyphs, above. */
2863 it->glyph_row = row;
2864 it->area = TEXT_AREA;
2866 /* Forget any previous info about this row being reversed. */
2867 if (it->glyph_row)
2868 it->glyph_row->reversed_p = 0;
2870 /* Get the dimensions of the display area. The display area
2871 consists of the visible window area plus a horizontally scrolled
2872 part to the left of the window. All x-values are relative to the
2873 start of this total display area. */
2874 if (base_face_id != DEFAULT_FACE_ID)
2876 /* Mode lines, menu bar in terminal frames. */
2877 it->first_visible_x = 0;
2878 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2880 else
2882 it->first_visible_x
2883 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2884 it->last_visible_x = (it->first_visible_x
2885 + window_box_width (w, TEXT_AREA));
2887 /* If we truncate lines, leave room for the truncation glyph(s) at
2888 the right margin. Otherwise, leave room for the continuation
2889 glyph(s). Done only if the window has no fringes. Since we
2890 don't know at this point whether there will be any R2L lines in
2891 the window, we reserve space for truncation/continuation glyphs
2892 even if only one of the fringes is absent. */
2893 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2894 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2896 if (it->line_wrap == TRUNCATE)
2897 it->last_visible_x -= it->truncation_pixel_width;
2898 else
2899 it->last_visible_x -= it->continuation_pixel_width;
2902 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2903 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2906 /* Leave room for a border glyph. */
2907 if (!FRAME_WINDOW_P (it->f)
2908 && !WINDOW_RIGHTMOST_P (it->w))
2909 it->last_visible_x -= 1;
2911 it->last_visible_y = window_text_bottom_y (w);
2913 /* For mode lines and alike, arrange for the first glyph having a
2914 left box line if the face specifies a box. */
2915 if (base_face_id != DEFAULT_FACE_ID)
2917 struct face *face;
2919 it->face_id = remapped_base_face_id;
2921 /* If we have a boxed mode line, make the first character appear
2922 with a left box line. */
2923 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2924 if (face->box != FACE_NO_BOX)
2925 it->start_of_box_run_p = true;
2928 /* If a buffer position was specified, set the iterator there,
2929 getting overlays and face properties from that position. */
2930 if (charpos >= BUF_BEG (current_buffer))
2932 it->end_charpos = ZV;
2933 eassert (charpos == BYTE_TO_CHAR (bytepos));
2934 IT_CHARPOS (*it) = charpos;
2935 IT_BYTEPOS (*it) = bytepos;
2937 /* We will rely on `reseat' to set this up properly, via
2938 handle_face_prop. */
2939 it->face_id = it->base_face_id;
2941 it->start = it->current;
2942 /* Do we need to reorder bidirectional text? Not if this is a
2943 unibyte buffer: by definition, none of the single-byte
2944 characters are strong R2L, so no reordering is needed. And
2945 bidi.c doesn't support unibyte buffers anyway. Also, don't
2946 reorder while we are loading loadup.el, since the tables of
2947 character properties needed for reordering are not yet
2948 available. */
2949 it->bidi_p =
2950 NILP (Vpurify_flag)
2951 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2952 && it->multibyte_p;
2954 /* If we are to reorder bidirectional text, init the bidi
2955 iterator. */
2956 if (it->bidi_p)
2958 /* Note the paragraph direction that this buffer wants to
2959 use. */
2960 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2961 Qleft_to_right))
2962 it->paragraph_embedding = L2R;
2963 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2964 Qright_to_left))
2965 it->paragraph_embedding = R2L;
2966 else
2967 it->paragraph_embedding = NEUTRAL_DIR;
2968 bidi_unshelve_cache (NULL, 0);
2969 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2970 &it->bidi_it);
2973 /* Compute faces etc. */
2974 reseat (it, it->current.pos, 1);
2977 CHECK_IT (it);
2981 /* Initialize IT for the display of window W with window start POS. */
2983 void
2984 start_display (struct it *it, struct window *w, struct text_pos pos)
2986 struct glyph_row *row;
2987 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2989 row = w->desired_matrix->rows + first_vpos;
2990 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2991 it->first_vpos = first_vpos;
2993 /* Don't reseat to previous visible line start if current start
2994 position is in a string or image. */
2995 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2997 int start_at_line_beg_p;
2998 int first_y = it->current_y;
3000 /* If window start is not at a line start, skip forward to POS to
3001 get the correct continuation lines width. */
3002 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3003 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3004 if (!start_at_line_beg_p)
3006 int new_x;
3008 reseat_at_previous_visible_line_start (it);
3009 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3011 new_x = it->current_x + it->pixel_width;
3013 /* If lines are continued, this line may end in the middle
3014 of a multi-glyph character (e.g. a control character
3015 displayed as \003, or in the middle of an overlay
3016 string). In this case move_it_to above will not have
3017 taken us to the start of the continuation line but to the
3018 end of the continued line. */
3019 if (it->current_x > 0
3020 && it->line_wrap != TRUNCATE /* Lines are continued. */
3021 && (/* And glyph doesn't fit on the line. */
3022 new_x > it->last_visible_x
3023 /* Or it fits exactly and we're on a window
3024 system frame. */
3025 || (new_x == it->last_visible_x
3026 && FRAME_WINDOW_P (it->f)
3027 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3028 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3029 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3031 if ((it->current.dpvec_index >= 0
3032 || it->current.overlay_string_index >= 0)
3033 /* If we are on a newline from a display vector or
3034 overlay string, then we are already at the end of
3035 a screen line; no need to go to the next line in
3036 that case, as this line is not really continued.
3037 (If we do go to the next line, C-e will not DTRT.) */
3038 && it->c != '\n')
3040 set_iterator_to_next (it, 1);
3041 move_it_in_display_line_to (it, -1, -1, 0);
3044 it->continuation_lines_width += it->current_x;
3046 /* If the character at POS is displayed via a display
3047 vector, move_it_to above stops at the final glyph of
3048 IT->dpvec. To make the caller redisplay that character
3049 again (a.k.a. start at POS), we need to reset the
3050 dpvec_index to the beginning of IT->dpvec. */
3051 else if (it->current.dpvec_index >= 0)
3052 it->current.dpvec_index = 0;
3054 /* We're starting a new display line, not affected by the
3055 height of the continued line, so clear the appropriate
3056 fields in the iterator structure. */
3057 it->max_ascent = it->max_descent = 0;
3058 it->max_phys_ascent = it->max_phys_descent = 0;
3060 it->current_y = first_y;
3061 it->vpos = 0;
3062 it->current_x = it->hpos = 0;
3068 /* Return 1 if POS is a position in ellipses displayed for invisible
3069 text. W is the window we display, for text property lookup. */
3071 static int
3072 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3074 Lisp_Object prop, window;
3075 int ellipses_p = 0;
3076 ptrdiff_t charpos = CHARPOS (pos->pos);
3078 /* If POS specifies a position in a display vector, this might
3079 be for an ellipsis displayed for invisible text. We won't
3080 get the iterator set up for delivering that ellipsis unless
3081 we make sure that it gets aware of the invisible text. */
3082 if (pos->dpvec_index >= 0
3083 && pos->overlay_string_index < 0
3084 && CHARPOS (pos->string_pos) < 0
3085 && charpos > BEGV
3086 && (XSETWINDOW (window, w),
3087 prop = Fget_char_property (make_number (charpos),
3088 Qinvisible, window),
3089 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3091 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3092 window);
3093 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3096 return ellipses_p;
3100 /* Initialize IT for stepping through current_buffer in window W,
3101 starting at position POS that includes overlay string and display
3102 vector/ control character translation position information. Value
3103 is zero if there are overlay strings with newlines at POS. */
3105 static int
3106 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3108 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3109 int i, overlay_strings_with_newlines = 0;
3111 /* If POS specifies a position in a display vector, this might
3112 be for an ellipsis displayed for invisible text. We won't
3113 get the iterator set up for delivering that ellipsis unless
3114 we make sure that it gets aware of the invisible text. */
3115 if (in_ellipses_for_invisible_text_p (pos, w))
3117 --charpos;
3118 bytepos = 0;
3121 /* Keep in mind: the call to reseat in init_iterator skips invisible
3122 text, so we might end up at a position different from POS. This
3123 is only a problem when POS is a row start after a newline and an
3124 overlay starts there with an after-string, and the overlay has an
3125 invisible property. Since we don't skip invisible text in
3126 display_line and elsewhere immediately after consuming the
3127 newline before the row start, such a POS will not be in a string,
3128 but the call to init_iterator below will move us to the
3129 after-string. */
3130 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3132 /* This only scans the current chunk -- it should scan all chunks.
3133 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3134 to 16 in 22.1 to make this a lesser problem. */
3135 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3137 const char *s = SSDATA (it->overlay_strings[i]);
3138 const char *e = s + SBYTES (it->overlay_strings[i]);
3140 while (s < e && *s != '\n')
3141 ++s;
3143 if (s < e)
3145 overlay_strings_with_newlines = 1;
3146 break;
3150 /* If position is within an overlay string, set up IT to the right
3151 overlay string. */
3152 if (pos->overlay_string_index >= 0)
3154 int relative_index;
3156 /* If the first overlay string happens to have a `display'
3157 property for an image, the iterator will be set up for that
3158 image, and we have to undo that setup first before we can
3159 correct the overlay string index. */
3160 if (it->method == GET_FROM_IMAGE)
3161 pop_it (it);
3163 /* We already have the first chunk of overlay strings in
3164 IT->overlay_strings. Load more until the one for
3165 pos->overlay_string_index is in IT->overlay_strings. */
3166 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3168 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3169 it->current.overlay_string_index = 0;
3170 while (n--)
3172 load_overlay_strings (it, 0);
3173 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3177 it->current.overlay_string_index = pos->overlay_string_index;
3178 relative_index = (it->current.overlay_string_index
3179 % OVERLAY_STRING_CHUNK_SIZE);
3180 it->string = it->overlay_strings[relative_index];
3181 eassert (STRINGP (it->string));
3182 it->current.string_pos = pos->string_pos;
3183 it->method = GET_FROM_STRING;
3184 it->end_charpos = SCHARS (it->string);
3185 /* Set up the bidi iterator for this overlay string. */
3186 if (it->bidi_p)
3188 it->bidi_it.string.lstring = it->string;
3189 it->bidi_it.string.s = NULL;
3190 it->bidi_it.string.schars = SCHARS (it->string);
3191 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3192 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3193 it->bidi_it.string.unibyte = !it->multibyte_p;
3194 it->bidi_it.w = it->w;
3195 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3196 FRAME_WINDOW_P (it->f), &it->bidi_it);
3198 /* Synchronize the state of the bidi iterator with
3199 pos->string_pos. For any string position other than
3200 zero, this will be done automagically when we resume
3201 iteration over the string and get_visually_first_element
3202 is called. But if string_pos is zero, and the string is
3203 to be reordered for display, we need to resync manually,
3204 since it could be that the iteration state recorded in
3205 pos ended at string_pos of 0 moving backwards in string. */
3206 if (CHARPOS (pos->string_pos) == 0)
3208 get_visually_first_element (it);
3209 if (IT_STRING_CHARPOS (*it) != 0)
3210 do {
3211 /* Paranoia. */
3212 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3213 bidi_move_to_visually_next (&it->bidi_it);
3214 } while (it->bidi_it.charpos != 0);
3216 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3217 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3221 if (CHARPOS (pos->string_pos) >= 0)
3223 /* Recorded position is not in an overlay string, but in another
3224 string. This can only be a string from a `display' property.
3225 IT should already be filled with that string. */
3226 it->current.string_pos = pos->string_pos;
3227 eassert (STRINGP (it->string));
3228 if (it->bidi_p)
3229 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3230 FRAME_WINDOW_P (it->f), &it->bidi_it);
3233 /* Restore position in display vector translations, control
3234 character translations or ellipses. */
3235 if (pos->dpvec_index >= 0)
3237 if (it->dpvec == NULL)
3238 get_next_display_element (it);
3239 eassert (it->dpvec && it->current.dpvec_index == 0);
3240 it->current.dpvec_index = pos->dpvec_index;
3243 CHECK_IT (it);
3244 return !overlay_strings_with_newlines;
3248 /* Initialize IT for stepping through current_buffer in window W
3249 starting at ROW->start. */
3251 static void
3252 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3254 init_from_display_pos (it, w, &row->start);
3255 it->start = row->start;
3256 it->continuation_lines_width = row->continuation_lines_width;
3257 CHECK_IT (it);
3261 /* Initialize IT for stepping through current_buffer in window W
3262 starting in the line following ROW, i.e. starting at ROW->end.
3263 Value is zero if there are overlay strings with newlines at ROW's
3264 end position. */
3266 static int
3267 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3269 int success = 0;
3271 if (init_from_display_pos (it, w, &row->end))
3273 if (row->continued_p)
3274 it->continuation_lines_width
3275 = row->continuation_lines_width + row->pixel_width;
3276 CHECK_IT (it);
3277 success = 1;
3280 return success;
3286 /***********************************************************************
3287 Text properties
3288 ***********************************************************************/
3290 /* Called when IT reaches IT->stop_charpos. Handle text property and
3291 overlay changes. Set IT->stop_charpos to the next position where
3292 to stop. */
3294 static void
3295 handle_stop (struct it *it)
3297 enum prop_handled handled;
3298 int handle_overlay_change_p;
3299 struct props *p;
3301 it->dpvec = NULL;
3302 it->current.dpvec_index = -1;
3303 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3304 it->ignore_overlay_strings_at_pos_p = 0;
3305 it->ellipsis_p = 0;
3307 /* Use face of preceding text for ellipsis (if invisible) */
3308 if (it->selective_display_ellipsis_p)
3309 it->saved_face_id = it->face_id;
3313 handled = HANDLED_NORMALLY;
3315 /* Call text property handlers. */
3316 for (p = it_props; p->handler; ++p)
3318 handled = p->handler (it);
3320 if (handled == HANDLED_RECOMPUTE_PROPS)
3321 break;
3322 else if (handled == HANDLED_RETURN)
3324 /* We still want to show before and after strings from
3325 overlays even if the actual buffer text is replaced. */
3326 if (!handle_overlay_change_p
3327 || it->sp > 1
3328 /* Don't call get_overlay_strings_1 if we already
3329 have overlay strings loaded, because doing so
3330 will load them again and push the iterator state
3331 onto the stack one more time, which is not
3332 expected by the rest of the code that processes
3333 overlay strings. */
3334 || (it->current.overlay_string_index < 0
3335 ? !get_overlay_strings_1 (it, 0, 0)
3336 : 0))
3338 if (it->ellipsis_p)
3339 setup_for_ellipsis (it, 0);
3340 /* When handling a display spec, we might load an
3341 empty string. In that case, discard it here. We
3342 used to discard it in handle_single_display_spec,
3343 but that causes get_overlay_strings_1, above, to
3344 ignore overlay strings that we must check. */
3345 if (STRINGP (it->string) && !SCHARS (it->string))
3346 pop_it (it);
3347 return;
3349 else if (STRINGP (it->string) && !SCHARS (it->string))
3350 pop_it (it);
3351 else
3353 it->ignore_overlay_strings_at_pos_p = true;
3354 it->string_from_display_prop_p = 0;
3355 it->from_disp_prop_p = 0;
3356 handle_overlay_change_p = 0;
3358 handled = HANDLED_RECOMPUTE_PROPS;
3359 break;
3361 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3362 handle_overlay_change_p = 0;
3365 if (handled != HANDLED_RECOMPUTE_PROPS)
3367 /* Don't check for overlay strings below when set to deliver
3368 characters from a display vector. */
3369 if (it->method == GET_FROM_DISPLAY_VECTOR)
3370 handle_overlay_change_p = 0;
3372 /* Handle overlay changes.
3373 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3374 if it finds overlays. */
3375 if (handle_overlay_change_p)
3376 handled = handle_overlay_change (it);
3379 if (it->ellipsis_p)
3381 setup_for_ellipsis (it, 0);
3382 break;
3385 while (handled == HANDLED_RECOMPUTE_PROPS);
3387 /* Determine where to stop next. */
3388 if (handled == HANDLED_NORMALLY)
3389 compute_stop_pos (it);
3393 /* Compute IT->stop_charpos from text property and overlay change
3394 information for IT's current position. */
3396 static void
3397 compute_stop_pos (struct it *it)
3399 register INTERVAL iv, next_iv;
3400 Lisp_Object object, limit, position;
3401 ptrdiff_t charpos, bytepos;
3403 if (STRINGP (it->string))
3405 /* Strings are usually short, so don't limit the search for
3406 properties. */
3407 it->stop_charpos = it->end_charpos;
3408 object = it->string;
3409 limit = Qnil;
3410 charpos = IT_STRING_CHARPOS (*it);
3411 bytepos = IT_STRING_BYTEPOS (*it);
3413 else
3415 ptrdiff_t pos;
3417 /* If end_charpos is out of range for some reason, such as a
3418 misbehaving display function, rationalize it (Bug#5984). */
3419 if (it->end_charpos > ZV)
3420 it->end_charpos = ZV;
3421 it->stop_charpos = it->end_charpos;
3423 /* If next overlay change is in front of the current stop pos
3424 (which is IT->end_charpos), stop there. Note: value of
3425 next_overlay_change is point-max if no overlay change
3426 follows. */
3427 charpos = IT_CHARPOS (*it);
3428 bytepos = IT_BYTEPOS (*it);
3429 pos = next_overlay_change (charpos);
3430 if (pos < it->stop_charpos)
3431 it->stop_charpos = pos;
3433 /* Set up variables for computing the stop position from text
3434 property changes. */
3435 XSETBUFFER (object, current_buffer);
3436 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3439 /* Get the interval containing IT's position. Value is a null
3440 interval if there isn't such an interval. */
3441 position = make_number (charpos);
3442 iv = validate_interval_range (object, &position, &position, 0);
3443 if (iv)
3445 Lisp_Object values_here[LAST_PROP_IDX];
3446 struct props *p;
3448 /* Get properties here. */
3449 for (p = it_props; p->handler; ++p)
3450 values_here[p->idx] = textget (iv->plist, *p->name);
3452 /* Look for an interval following iv that has different
3453 properties. */
3454 for (next_iv = next_interval (iv);
3455 (next_iv
3456 && (NILP (limit)
3457 || XFASTINT (limit) > next_iv->position));
3458 next_iv = next_interval (next_iv))
3460 for (p = it_props; p->handler; ++p)
3462 Lisp_Object new_value;
3464 new_value = textget (next_iv->plist, *p->name);
3465 if (!EQ (values_here[p->idx], new_value))
3466 break;
3469 if (p->handler)
3470 break;
3473 if (next_iv)
3475 if (INTEGERP (limit)
3476 && next_iv->position >= XFASTINT (limit))
3477 /* No text property change up to limit. */
3478 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3479 else
3480 /* Text properties change in next_iv. */
3481 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3485 if (it->cmp_it.id < 0)
3487 ptrdiff_t stoppos = it->end_charpos;
3489 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3490 stoppos = -1;
3491 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3492 stoppos, it->string);
3495 eassert (STRINGP (it->string)
3496 || (it->stop_charpos >= BEGV
3497 && it->stop_charpos >= IT_CHARPOS (*it)));
3501 /* Return the position of the next overlay change after POS in
3502 current_buffer. Value is point-max if no overlay change
3503 follows. This is like `next-overlay-change' but doesn't use
3504 xmalloc. */
3506 static ptrdiff_t
3507 next_overlay_change (ptrdiff_t pos)
3509 ptrdiff_t i, noverlays;
3510 ptrdiff_t endpos;
3511 Lisp_Object *overlays;
3513 /* Get all overlays at the given position. */
3514 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3516 /* If any of these overlays ends before endpos,
3517 use its ending point instead. */
3518 for (i = 0; i < noverlays; ++i)
3520 Lisp_Object oend;
3521 ptrdiff_t oendpos;
3523 oend = OVERLAY_END (overlays[i]);
3524 oendpos = OVERLAY_POSITION (oend);
3525 endpos = min (endpos, oendpos);
3528 return endpos;
3531 /* How many characters forward to search for a display property or
3532 display string. Searching too far forward makes the bidi display
3533 sluggish, especially in small windows. */
3534 #define MAX_DISP_SCAN 250
3536 /* Return the character position of a display string at or after
3537 position specified by POSITION. If no display string exists at or
3538 after POSITION, return ZV. A display string is either an overlay
3539 with `display' property whose value is a string, or a `display'
3540 text property whose value is a string. STRING is data about the
3541 string to iterate; if STRING->lstring is nil, we are iterating a
3542 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3543 on a GUI frame. DISP_PROP is set to zero if we searched
3544 MAX_DISP_SCAN characters forward without finding any display
3545 strings, non-zero otherwise. It is set to 2 if the display string
3546 uses any kind of `(space ...)' spec that will produce a stretch of
3547 white space in the text area. */
3548 ptrdiff_t
3549 compute_display_string_pos (struct text_pos *position,
3550 struct bidi_string_data *string,
3551 struct window *w,
3552 int frame_window_p, int *disp_prop)
3554 /* OBJECT = nil means current buffer. */
3555 Lisp_Object object, object1;
3556 Lisp_Object pos, spec, limpos;
3557 int string_p = (string && (STRINGP (string->lstring) || string->s));
3558 ptrdiff_t eob = string_p ? string->schars : ZV;
3559 ptrdiff_t begb = string_p ? 0 : BEGV;
3560 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3561 ptrdiff_t lim =
3562 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3563 struct text_pos tpos;
3564 int rv = 0;
3566 if (string && STRINGP (string->lstring))
3567 object1 = object = string->lstring;
3568 else if (w && !string_p)
3570 XSETWINDOW (object, w);
3571 object1 = Qnil;
3573 else
3574 object1 = object = Qnil;
3576 *disp_prop = 1;
3578 if (charpos >= eob
3579 /* We don't support display properties whose values are strings
3580 that have display string properties. */
3581 || string->from_disp_str
3582 /* C strings cannot have display properties. */
3583 || (string->s && !STRINGP (object)))
3585 *disp_prop = 0;
3586 return eob;
3589 /* If the character at CHARPOS is where the display string begins,
3590 return CHARPOS. */
3591 pos = make_number (charpos);
3592 if (STRINGP (object))
3593 bufpos = string->bufpos;
3594 else
3595 bufpos = charpos;
3596 tpos = *position;
3597 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3598 && (charpos <= begb
3599 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3600 object),
3601 spec))
3602 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3603 frame_window_p)))
3605 if (rv == 2)
3606 *disp_prop = 2;
3607 return charpos;
3610 /* Look forward for the first character with a `display' property
3611 that will replace the underlying text when displayed. */
3612 limpos = make_number (lim);
3613 do {
3614 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3615 CHARPOS (tpos) = XFASTINT (pos);
3616 if (CHARPOS (tpos) >= lim)
3618 *disp_prop = 0;
3619 break;
3621 if (STRINGP (object))
3622 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3623 else
3624 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3625 spec = Fget_char_property (pos, Qdisplay, object);
3626 if (!STRINGP (object))
3627 bufpos = CHARPOS (tpos);
3628 } while (NILP (spec)
3629 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3630 bufpos, frame_window_p)));
3631 if (rv == 2)
3632 *disp_prop = 2;
3634 return CHARPOS (tpos);
3637 /* Return the character position of the end of the display string that
3638 started at CHARPOS. If there's no display string at CHARPOS,
3639 return -1. A display string is either an overlay with `display'
3640 property whose value is a string or a `display' text property whose
3641 value is a string. */
3642 ptrdiff_t
3643 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3645 /* OBJECT = nil means current buffer. */
3646 Lisp_Object object =
3647 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3648 Lisp_Object pos = make_number (charpos);
3649 ptrdiff_t eob =
3650 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3652 if (charpos >= eob || (string->s && !STRINGP (object)))
3653 return eob;
3655 /* It could happen that the display property or overlay was removed
3656 since we found it in compute_display_string_pos above. One way
3657 this can happen is if JIT font-lock was called (through
3658 handle_fontified_prop), and jit-lock-functions remove text
3659 properties or overlays from the portion of buffer that includes
3660 CHARPOS. Muse mode is known to do that, for example. In this
3661 case, we return -1 to the caller, to signal that no display
3662 string is actually present at CHARPOS. See bidi_fetch_char for
3663 how this is handled.
3665 An alternative would be to never look for display properties past
3666 it->stop_charpos. But neither compute_display_string_pos nor
3667 bidi_fetch_char that calls it know or care where the next
3668 stop_charpos is. */
3669 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3670 return -1;
3672 /* Look forward for the first character where the `display' property
3673 changes. */
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3676 return XFASTINT (pos);
3681 /***********************************************************************
3682 Fontification
3683 ***********************************************************************/
3685 /* Handle changes in the `fontified' property of the current buffer by
3686 calling hook functions from Qfontification_functions to fontify
3687 regions of text. */
3689 static enum prop_handled
3690 handle_fontified_prop (struct it *it)
3692 Lisp_Object prop, pos;
3693 enum prop_handled handled = HANDLED_NORMALLY;
3695 if (!NILP (Vmemory_full))
3696 return handled;
3698 /* Get the value of the `fontified' property at IT's current buffer
3699 position. (The `fontified' property doesn't have a special
3700 meaning in strings.) If the value is nil, call functions from
3701 Qfontification_functions. */
3702 if (!STRINGP (it->string)
3703 && it->s == NULL
3704 && !NILP (Vfontification_functions)
3705 && !NILP (Vrun_hooks)
3706 && (pos = make_number (IT_CHARPOS (*it)),
3707 prop = Fget_char_property (pos, Qfontified, Qnil),
3708 /* Ignore the special cased nil value always present at EOB since
3709 no amount of fontifying will be able to change it. */
3710 NILP (prop) && IT_CHARPOS (*it) < Z))
3712 ptrdiff_t count = SPECPDL_INDEX ();
3713 Lisp_Object val;
3714 struct buffer *obuf = current_buffer;
3715 ptrdiff_t begv = BEGV, zv = ZV;
3716 bool old_clip_changed = current_buffer->clip_changed;
3718 val = Vfontification_functions;
3719 specbind (Qfontification_functions, Qnil);
3721 eassert (it->end_charpos == ZV);
3723 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3724 safe_call1 (val, pos);
3725 else
3727 Lisp_Object fns, fn;
3728 struct gcpro gcpro1, gcpro2;
3730 fns = Qnil;
3731 GCPRO2 (val, fns);
3733 for (; CONSP (val); val = XCDR (val))
3735 fn = XCAR (val);
3737 if (EQ (fn, Qt))
3739 /* A value of t indicates this hook has a local
3740 binding; it means to run the global binding too.
3741 In a global value, t should not occur. If it
3742 does, we must ignore it to avoid an endless
3743 loop. */
3744 for (fns = Fdefault_value (Qfontification_functions);
3745 CONSP (fns);
3746 fns = XCDR (fns))
3748 fn = XCAR (fns);
3749 if (!EQ (fn, Qt))
3750 safe_call1 (fn, pos);
3753 else
3754 safe_call1 (fn, pos);
3757 UNGCPRO;
3760 unbind_to (count, Qnil);
3762 /* Fontification functions routinely call `save-restriction'.
3763 Normally, this tags clip_changed, which can confuse redisplay
3764 (see discussion in Bug#6671). Since we don't perform any
3765 special handling of fontification changes in the case where
3766 `save-restriction' isn't called, there's no point doing so in
3767 this case either. So, if the buffer's restrictions are
3768 actually left unchanged, reset clip_changed. */
3769 if (obuf == current_buffer)
3771 if (begv == BEGV && zv == ZV)
3772 current_buffer->clip_changed = old_clip_changed;
3774 /* There isn't much we can reasonably do to protect against
3775 misbehaving fontification, but here's a fig leaf. */
3776 else if (BUFFER_LIVE_P (obuf))
3777 set_buffer_internal_1 (obuf);
3779 /* The fontification code may have added/removed text.
3780 It could do even a lot worse, but let's at least protect against
3781 the most obvious case where only the text past `pos' gets changed',
3782 as is/was done in grep.el where some escapes sequences are turned
3783 into face properties (bug#7876). */
3784 it->end_charpos = ZV;
3786 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3787 something. This avoids an endless loop if they failed to
3788 fontify the text for which reason ever. */
3789 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3790 handled = HANDLED_RECOMPUTE_PROPS;
3793 return handled;
3798 /***********************************************************************
3799 Faces
3800 ***********************************************************************/
3802 /* Set up iterator IT from face properties at its current position.
3803 Called from handle_stop. */
3805 static enum prop_handled
3806 handle_face_prop (struct it *it)
3808 int new_face_id;
3809 ptrdiff_t next_stop;
3811 if (!STRINGP (it->string))
3813 new_face_id
3814 = face_at_buffer_position (it->w,
3815 IT_CHARPOS (*it),
3816 &next_stop,
3817 (IT_CHARPOS (*it)
3818 + TEXT_PROP_DISTANCE_LIMIT),
3819 0, it->base_face_id);
3821 /* Is this a start of a run of characters with box face?
3822 Caveat: this can be called for a freshly initialized
3823 iterator; face_id is -1 in this case. We know that the new
3824 face will not change until limit, i.e. if the new face has a
3825 box, all characters up to limit will have one. But, as
3826 usual, we don't know whether limit is really the end. */
3827 if (new_face_id != it->face_id)
3829 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3830 /* If it->face_id is -1, old_face below will be NULL, see
3831 the definition of FACE_FROM_ID. This will happen if this
3832 is the initial call that gets the face. */
3833 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3835 /* If the value of face_id of the iterator is -1, we have to
3836 look in front of IT's position and see whether there is a
3837 face there that's different from new_face_id. */
3838 if (!old_face && IT_CHARPOS (*it) > BEG)
3840 int prev_face_id = face_before_it_pos (it);
3842 old_face = FACE_FROM_ID (it->f, prev_face_id);
3845 /* If the new face has a box, but the old face does not,
3846 this is the start of a run of characters with box face,
3847 i.e. this character has a shadow on the left side. */
3848 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3849 && (old_face == NULL || !old_face->box));
3850 it->face_box_p = new_face->box != FACE_NO_BOX;
3853 else
3855 int base_face_id;
3856 ptrdiff_t bufpos;
3857 int i;
3858 Lisp_Object from_overlay
3859 = (it->current.overlay_string_index >= 0
3860 ? it->string_overlays[it->current.overlay_string_index
3861 % OVERLAY_STRING_CHUNK_SIZE]
3862 : Qnil);
3864 /* See if we got to this string directly or indirectly from
3865 an overlay property. That includes the before-string or
3866 after-string of an overlay, strings in display properties
3867 provided by an overlay, their text properties, etc.
3869 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3870 if (! NILP (from_overlay))
3871 for (i = it->sp - 1; i >= 0; i--)
3873 if (it->stack[i].current.overlay_string_index >= 0)
3874 from_overlay
3875 = it->string_overlays[it->stack[i].current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE];
3877 else if (! NILP (it->stack[i].from_overlay))
3878 from_overlay = it->stack[i].from_overlay;
3880 if (!NILP (from_overlay))
3881 break;
3884 if (! NILP (from_overlay))
3886 bufpos = IT_CHARPOS (*it);
3887 /* For a string from an overlay, the base face depends
3888 only on text properties and ignores overlays. */
3889 base_face_id
3890 = face_for_overlay_string (it->w,
3891 IT_CHARPOS (*it),
3892 &next_stop,
3893 (IT_CHARPOS (*it)
3894 + TEXT_PROP_DISTANCE_LIMIT),
3896 from_overlay);
3898 else
3900 bufpos = 0;
3902 /* For strings from a `display' property, use the face at
3903 IT's current buffer position as the base face to merge
3904 with, so that overlay strings appear in the same face as
3905 surrounding text, unless they specify their own faces.
3906 For strings from wrap-prefix and line-prefix properties,
3907 use the default face, possibly remapped via
3908 Vface_remapping_alist. */
3909 base_face_id = it->string_from_prefix_prop_p
3910 ? (!NILP (Vface_remapping_alist)
3911 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3912 : DEFAULT_FACE_ID)
3913 : underlying_face_id (it);
3916 new_face_id = face_at_string_position (it->w,
3917 it->string,
3918 IT_STRING_CHARPOS (*it),
3919 bufpos,
3920 &next_stop,
3921 base_face_id, 0);
3923 /* Is this a start of a run of characters with box? Caveat:
3924 this can be called for a freshly allocated iterator; face_id
3925 is -1 is this case. We know that the new face will not
3926 change until the next check pos, i.e. if the new face has a
3927 box, all characters up to that position will have a
3928 box. But, as usual, we don't know whether that position
3929 is really the end. */
3930 if (new_face_id != it->face_id)
3932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3933 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3935 /* If new face has a box but old face hasn't, this is the
3936 start of a run of characters with box, i.e. it has a
3937 shadow on the left side. */
3938 it->start_of_box_run_p
3939 = new_face->box && (old_face == NULL || !old_face->box);
3940 it->face_box_p = new_face->box != FACE_NO_BOX;
3944 it->face_id = new_face_id;
3945 return HANDLED_NORMALLY;
3949 /* Return the ID of the face ``underlying'' IT's current position,
3950 which is in a string. If the iterator is associated with a
3951 buffer, return the face at IT's current buffer position.
3952 Otherwise, use the iterator's base_face_id. */
3954 static int
3955 underlying_face_id (struct it *it)
3957 int face_id = it->base_face_id, i;
3959 eassert (STRINGP (it->string));
3961 for (i = it->sp - 1; i >= 0; --i)
3962 if (NILP (it->stack[i].string))
3963 face_id = it->stack[i].face_id;
3965 return face_id;
3969 /* Compute the face one character before or after the current position
3970 of IT, in the visual order. BEFORE_P non-zero means get the face
3971 in front (to the left in L2R paragraphs, to the right in R2L
3972 paragraphs) of IT's screen position. Value is the ID of the face. */
3974 static int
3975 face_before_or_after_it_pos (struct it *it, int before_p)
3977 int face_id, limit;
3978 ptrdiff_t next_check_charpos;
3979 struct it it_copy;
3980 void *it_copy_data = NULL;
3982 eassert (it->s == NULL);
3984 if (STRINGP (it->string))
3986 ptrdiff_t bufpos, charpos;
3987 int base_face_id;
3989 /* No face change past the end of the string (for the case
3990 we are padding with spaces). No face change before the
3991 string start. */
3992 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3993 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3994 return it->face_id;
3996 if (!it->bidi_p)
3998 /* Set charpos to the position before or after IT's current
3999 position, in the logical order, which in the non-bidi
4000 case is the same as the visual order. */
4001 if (before_p)
4002 charpos = IT_STRING_CHARPOS (*it) - 1;
4003 else if (it->what == IT_COMPOSITION)
4004 /* For composition, we must check the character after the
4005 composition. */
4006 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4007 else
4008 charpos = IT_STRING_CHARPOS (*it) + 1;
4010 else
4012 if (before_p)
4014 /* With bidi iteration, the character before the current
4015 in the visual order cannot be found by simple
4016 iteration, because "reverse" reordering is not
4017 supported. Instead, we need to use the move_it_*
4018 family of functions. */
4019 /* Ignore face changes before the first visible
4020 character on this display line. */
4021 if (it->current_x <= it->first_visible_x)
4022 return it->face_id;
4023 SAVE_IT (it_copy, *it, it_copy_data);
4024 /* Implementation note: Since move_it_in_display_line
4025 works in the iterator geometry, and thinks the first
4026 character is always the leftmost, even in R2L lines,
4027 we don't need to distinguish between the R2L and L2R
4028 cases here. */
4029 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4030 it_copy.current_x - 1, MOVE_TO_X);
4031 charpos = IT_STRING_CHARPOS (it_copy);
4032 RESTORE_IT (it, it, it_copy_data);
4034 else
4036 /* Set charpos to the string position of the character
4037 that comes after IT's current position in the visual
4038 order. */
4039 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4041 it_copy = *it;
4042 while (n--)
4043 bidi_move_to_visually_next (&it_copy.bidi_it);
4045 charpos = it_copy.bidi_it.charpos;
4048 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4050 if (it->current.overlay_string_index >= 0)
4051 bufpos = IT_CHARPOS (*it);
4052 else
4053 bufpos = 0;
4055 base_face_id = underlying_face_id (it);
4057 /* Get the face for ASCII, or unibyte. */
4058 face_id = face_at_string_position (it->w,
4059 it->string,
4060 charpos,
4061 bufpos,
4062 &next_check_charpos,
4063 base_face_id, 0);
4065 /* Correct the face for charsets different from ASCII. Do it
4066 for the multibyte case only. The face returned above is
4067 suitable for unibyte text if IT->string is unibyte. */
4068 if (STRING_MULTIBYTE (it->string))
4070 struct text_pos pos1 = string_pos (charpos, it->string);
4071 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4072 int c, len;
4073 struct face *face = FACE_FROM_ID (it->f, face_id);
4075 c = string_char_and_length (p, &len);
4076 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4079 else
4081 struct text_pos pos;
4083 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4084 || (IT_CHARPOS (*it) <= BEGV && before_p))
4085 return it->face_id;
4087 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4088 pos = it->current.pos;
4090 if (!it->bidi_p)
4092 if (before_p)
4093 DEC_TEXT_POS (pos, it->multibyte_p);
4094 else
4096 if (it->what == IT_COMPOSITION)
4098 /* For composition, we must check the position after
4099 the composition. */
4100 pos.charpos += it->cmp_it.nchars;
4101 pos.bytepos += it->len;
4103 else
4104 INC_TEXT_POS (pos, it->multibyte_p);
4107 else
4109 if (before_p)
4111 /* With bidi iteration, the character before the current
4112 in the visual order cannot be found by simple
4113 iteration, because "reverse" reordering is not
4114 supported. Instead, we need to use the move_it_*
4115 family of functions. */
4116 /* Ignore face changes before the first visible
4117 character on this display line. */
4118 if (it->current_x <= it->first_visible_x)
4119 return it->face_id;
4120 SAVE_IT (it_copy, *it, it_copy_data);
4121 /* Implementation note: Since move_it_in_display_line
4122 works in the iterator geometry, and thinks the first
4123 character is always the leftmost, even in R2L lines,
4124 we don't need to distinguish between the R2L and L2R
4125 cases here. */
4126 move_it_in_display_line (&it_copy, ZV,
4127 it_copy.current_x - 1, MOVE_TO_X);
4128 pos = it_copy.current.pos;
4129 RESTORE_IT (it, it, it_copy_data);
4131 else
4133 /* Set charpos to the buffer position of the character
4134 that comes after IT's current position in the visual
4135 order. */
4136 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4138 it_copy = *it;
4139 while (n--)
4140 bidi_move_to_visually_next (&it_copy.bidi_it);
4142 SET_TEXT_POS (pos,
4143 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4146 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4148 /* Determine face for CHARSET_ASCII, or unibyte. */
4149 face_id = face_at_buffer_position (it->w,
4150 CHARPOS (pos),
4151 &next_check_charpos,
4152 limit, 0, -1);
4154 /* Correct the face for charsets different from ASCII. Do it
4155 for the multibyte case only. The face returned above is
4156 suitable for unibyte text if current_buffer is unibyte. */
4157 if (it->multibyte_p)
4159 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4160 struct face *face = FACE_FROM_ID (it->f, face_id);
4161 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4165 return face_id;
4170 /***********************************************************************
4171 Invisible text
4172 ***********************************************************************/
4174 /* Set up iterator IT from invisible properties at its current
4175 position. Called from handle_stop. */
4177 static enum prop_handled
4178 handle_invisible_prop (struct it *it)
4180 enum prop_handled handled = HANDLED_NORMALLY;
4181 int invis_p;
4182 Lisp_Object prop;
4184 if (STRINGP (it->string))
4186 Lisp_Object end_charpos, limit, charpos;
4188 /* Get the value of the invisible text property at the
4189 current position. Value will be nil if there is no such
4190 property. */
4191 charpos = make_number (IT_STRING_CHARPOS (*it));
4192 prop = Fget_text_property (charpos, Qinvisible, it->string);
4193 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4195 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4197 /* Record whether we have to display an ellipsis for the
4198 invisible text. */
4199 int display_ellipsis_p = (invis_p == 2);
4200 ptrdiff_t len, endpos;
4202 handled = HANDLED_RECOMPUTE_PROPS;
4204 /* Get the position at which the next visible text can be
4205 found in IT->string, if any. */
4206 endpos = len = SCHARS (it->string);
4207 XSETINT (limit, len);
4210 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4211 it->string, limit);
4212 if (INTEGERP (end_charpos))
4214 endpos = XFASTINT (end_charpos);
4215 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4216 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4217 if (invis_p == 2)
4218 display_ellipsis_p = true;
4221 while (invis_p && endpos < len);
4223 if (display_ellipsis_p)
4224 it->ellipsis_p = true;
4226 if (endpos < len)
4228 /* Text at END_CHARPOS is visible. Move IT there. */
4229 struct text_pos old;
4230 ptrdiff_t oldpos;
4232 old = it->current.string_pos;
4233 oldpos = CHARPOS (old);
4234 if (it->bidi_p)
4236 if (it->bidi_it.first_elt
4237 && it->bidi_it.charpos < SCHARS (it->string))
4238 bidi_paragraph_init (it->paragraph_embedding,
4239 &it->bidi_it, 1);
4240 /* Bidi-iterate out of the invisible text. */
4243 bidi_move_to_visually_next (&it->bidi_it);
4245 while (oldpos <= it->bidi_it.charpos
4246 && it->bidi_it.charpos < endpos);
4248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4249 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4250 if (IT_CHARPOS (*it) >= endpos)
4251 it->prev_stop = endpos;
4253 else
4255 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4256 compute_string_pos (&it->current.string_pos, old, it->string);
4259 else
4261 /* The rest of the string is invisible. If this is an
4262 overlay string, proceed with the next overlay string
4263 or whatever comes and return a character from there. */
4264 if (it->current.overlay_string_index >= 0
4265 && !display_ellipsis_p)
4267 next_overlay_string (it);
4268 /* Don't check for overlay strings when we just
4269 finished processing them. */
4270 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4272 else
4274 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4275 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4280 else
4282 ptrdiff_t newpos, next_stop, start_charpos, tem;
4283 Lisp_Object pos, overlay;
4285 /* First of all, is there invisible text at this position? */
4286 tem = start_charpos = IT_CHARPOS (*it);
4287 pos = make_number (tem);
4288 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4289 &overlay);
4290 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4292 /* If we are on invisible text, skip over it. */
4293 if (invis_p && start_charpos < it->end_charpos)
4295 /* Record whether we have to display an ellipsis for the
4296 invisible text. */
4297 int display_ellipsis_p = invis_p == 2;
4299 handled = HANDLED_RECOMPUTE_PROPS;
4301 /* Loop skipping over invisible text. The loop is left at
4302 ZV or with IT on the first char being visible again. */
4305 /* Try to skip some invisible text. Return value is the
4306 position reached which can be equal to where we start
4307 if there is nothing invisible there. This skips both
4308 over invisible text properties and overlays with
4309 invisible property. */
4310 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4312 /* If we skipped nothing at all we weren't at invisible
4313 text in the first place. If everything to the end of
4314 the buffer was skipped, end the loop. */
4315 if (newpos == tem || newpos >= ZV)
4316 invis_p = 0;
4317 else
4319 /* We skipped some characters but not necessarily
4320 all there are. Check if we ended up on visible
4321 text. Fget_char_property returns the property of
4322 the char before the given position, i.e. if we
4323 get invis_p = 0, this means that the char at
4324 newpos is visible. */
4325 pos = make_number (newpos);
4326 prop = Fget_char_property (pos, Qinvisible, it->window);
4327 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4330 /* If we ended up on invisible text, proceed to
4331 skip starting with next_stop. */
4332 if (invis_p)
4333 tem = next_stop;
4335 /* If there are adjacent invisible texts, don't lose the
4336 second one's ellipsis. */
4337 if (invis_p == 2)
4338 display_ellipsis_p = true;
4340 while (invis_p);
4342 /* The position newpos is now either ZV or on visible text. */
4343 if (it->bidi_p)
4345 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4346 int on_newline
4347 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4348 int after_newline
4349 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4351 /* If the invisible text ends on a newline or on a
4352 character after a newline, we can avoid the costly,
4353 character by character, bidi iteration to NEWPOS, and
4354 instead simply reseat the iterator there. That's
4355 because all bidi reordering information is tossed at
4356 the newline. This is a big win for modes that hide
4357 complete lines, like Outline, Org, etc. */
4358 if (on_newline || after_newline)
4360 struct text_pos tpos;
4361 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4363 SET_TEXT_POS (tpos, newpos, bpos);
4364 reseat_1 (it, tpos, 0);
4365 /* If we reseat on a newline/ZV, we need to prep the
4366 bidi iterator for advancing to the next character
4367 after the newline/EOB, keeping the current paragraph
4368 direction (so that PRODUCE_GLYPHS does TRT wrt
4369 prepending/appending glyphs to a glyph row). */
4370 if (on_newline)
4372 it->bidi_it.first_elt = 0;
4373 it->bidi_it.paragraph_dir = pdir;
4374 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4375 it->bidi_it.nchars = 1;
4376 it->bidi_it.ch_len = 1;
4379 else /* Must use the slow method. */
4381 /* With bidi iteration, the region of invisible text
4382 could start and/or end in the middle of a
4383 non-base embedding level. Therefore, we need to
4384 skip invisible text using the bidi iterator,
4385 starting at IT's current position, until we find
4386 ourselves outside of the invisible text.
4387 Skipping invisible text _after_ bidi iteration
4388 avoids affecting the visual order of the
4389 displayed text when invisible properties are
4390 added or removed. */
4391 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4393 /* If we were `reseat'ed to a new paragraph,
4394 determine the paragraph base direction. We
4395 need to do it now because
4396 next_element_from_buffer may not have a
4397 chance to do it, if we are going to skip any
4398 text at the beginning, which resets the
4399 FIRST_ELT flag. */
4400 bidi_paragraph_init (it->paragraph_embedding,
4401 &it->bidi_it, 1);
4405 bidi_move_to_visually_next (&it->bidi_it);
4407 while (it->stop_charpos <= it->bidi_it.charpos
4408 && it->bidi_it.charpos < newpos);
4409 IT_CHARPOS (*it) = it->bidi_it.charpos;
4410 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4411 /* If we overstepped NEWPOS, record its position in
4412 the iterator, so that we skip invisible text if
4413 later the bidi iteration lands us in the
4414 invisible region again. */
4415 if (IT_CHARPOS (*it) >= newpos)
4416 it->prev_stop = newpos;
4419 else
4421 IT_CHARPOS (*it) = newpos;
4422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4425 /* If there are before-strings at the start of invisible
4426 text, and the text is invisible because of a text
4427 property, arrange to show before-strings because 20.x did
4428 it that way. (If the text is invisible because of an
4429 overlay property instead of a text property, this is
4430 already handled in the overlay code.) */
4431 if (NILP (overlay)
4432 && get_overlay_strings (it, it->stop_charpos))
4434 handled = HANDLED_RECOMPUTE_PROPS;
4435 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4437 else if (display_ellipsis_p)
4439 /* Make sure that the glyphs of the ellipsis will get
4440 correct `charpos' values. If we would not update
4441 it->position here, the glyphs would belong to the
4442 last visible character _before_ the invisible
4443 text, which confuses `set_cursor_from_row'.
4445 We use the last invisible position instead of the
4446 first because this way the cursor is always drawn on
4447 the first "." of the ellipsis, whenever PT is inside
4448 the invisible text. Otherwise the cursor would be
4449 placed _after_ the ellipsis when the point is after the
4450 first invisible character. */
4451 if (!STRINGP (it->object))
4453 it->position.charpos = newpos - 1;
4454 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4456 it->ellipsis_p = true;
4457 /* Let the ellipsis display before
4458 considering any properties of the following char.
4459 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4460 handled = HANDLED_RETURN;
4465 return handled;
4469 /* Make iterator IT return `...' next.
4470 Replaces LEN characters from buffer. */
4472 static void
4473 setup_for_ellipsis (struct it *it, int len)
4475 /* Use the display table definition for `...'. Invalid glyphs
4476 will be handled by the method returning elements from dpvec. */
4477 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4479 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4480 it->dpvec = v->contents;
4481 it->dpend = v->contents + v->header.size;
4483 else
4485 /* Default `...'. */
4486 it->dpvec = default_invis_vector;
4487 it->dpend = default_invis_vector + 3;
4490 it->dpvec_char_len = len;
4491 it->current.dpvec_index = 0;
4492 it->dpvec_face_id = -1;
4494 /* Remember the current face id in case glyphs specify faces.
4495 IT's face is restored in set_iterator_to_next.
4496 saved_face_id was set to preceding char's face in handle_stop. */
4497 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4498 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4500 it->method = GET_FROM_DISPLAY_VECTOR;
4501 it->ellipsis_p = true;
4506 /***********************************************************************
4507 'display' property
4508 ***********************************************************************/
4510 /* Set up iterator IT from `display' property at its current position.
4511 Called from handle_stop.
4512 We return HANDLED_RETURN if some part of the display property
4513 overrides the display of the buffer text itself.
4514 Otherwise we return HANDLED_NORMALLY. */
4516 static enum prop_handled
4517 handle_display_prop (struct it *it)
4519 Lisp_Object propval, object, overlay;
4520 struct text_pos *position;
4521 ptrdiff_t bufpos;
4522 /* Nonzero if some property replaces the display of the text itself. */
4523 int display_replaced_p = 0;
4525 if (STRINGP (it->string))
4527 object = it->string;
4528 position = &it->current.string_pos;
4529 bufpos = CHARPOS (it->current.pos);
4531 else
4533 XSETWINDOW (object, it->w);
4534 position = &it->current.pos;
4535 bufpos = CHARPOS (*position);
4538 /* Reset those iterator values set from display property values. */
4539 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4540 it->space_width = Qnil;
4541 it->font_height = Qnil;
4542 it->voffset = 0;
4544 /* We don't support recursive `display' properties, i.e. string
4545 values that have a string `display' property, that have a string
4546 `display' property etc. */
4547 if (!it->string_from_display_prop_p)
4548 it->area = TEXT_AREA;
4550 propval = get_char_property_and_overlay (make_number (position->charpos),
4551 Qdisplay, object, &overlay);
4552 if (NILP (propval))
4553 return HANDLED_NORMALLY;
4554 /* Now OVERLAY is the overlay that gave us this property, or nil
4555 if it was a text property. */
4557 if (!STRINGP (it->string))
4558 object = it->w->contents;
4560 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4561 position, bufpos,
4562 FRAME_WINDOW_P (it->f));
4564 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4567 /* Subroutine of handle_display_prop. Returns non-zero if the display
4568 specification in SPEC is a replacing specification, i.e. it would
4569 replace the text covered by `display' property with something else,
4570 such as an image or a display string. If SPEC includes any kind or
4571 `(space ...) specification, the value is 2; this is used by
4572 compute_display_string_pos, which see.
4574 See handle_single_display_spec for documentation of arguments.
4575 frame_window_p is non-zero if the window being redisplayed is on a
4576 GUI frame; this argument is used only if IT is NULL, see below.
4578 IT can be NULL, if this is called by the bidi reordering code
4579 through compute_display_string_pos, which see. In that case, this
4580 function only examines SPEC, but does not otherwise "handle" it, in
4581 the sense that it doesn't set up members of IT from the display
4582 spec. */
4583 static int
4584 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4585 Lisp_Object overlay, struct text_pos *position,
4586 ptrdiff_t bufpos, int frame_window_p)
4588 int replacing_p = 0;
4589 int rv;
4591 if (CONSP (spec)
4592 /* Simple specifications. */
4593 && !EQ (XCAR (spec), Qimage)
4594 && !EQ (XCAR (spec), Qspace)
4595 && !EQ (XCAR (spec), Qwhen)
4596 && !EQ (XCAR (spec), Qslice)
4597 && !EQ (XCAR (spec), Qspace_width)
4598 && !EQ (XCAR (spec), Qheight)
4599 && !EQ (XCAR (spec), Qraise)
4600 /* Marginal area specifications. */
4601 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4602 && !EQ (XCAR (spec), Qleft_fringe)
4603 && !EQ (XCAR (spec), Qright_fringe)
4604 && !NILP (XCAR (spec)))
4606 for (; CONSP (spec); spec = XCDR (spec))
4608 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4609 overlay, position, bufpos,
4610 replacing_p, frame_window_p)))
4612 replacing_p = rv;
4613 /* If some text in a string is replaced, `position' no
4614 longer points to the position of `object'. */
4615 if (!it || STRINGP (object))
4616 break;
4620 else if (VECTORP (spec))
4622 ptrdiff_t i;
4623 for (i = 0; i < ASIZE (spec); ++i)
4624 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4625 overlay, position, bufpos,
4626 replacing_p, frame_window_p)))
4628 replacing_p = rv;
4629 /* If some text in a string is replaced, `position' no
4630 longer points to the position of `object'. */
4631 if (!it || STRINGP (object))
4632 break;
4635 else
4637 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4638 position, bufpos, 0,
4639 frame_window_p)))
4640 replacing_p = rv;
4643 return replacing_p;
4646 /* Value is the position of the end of the `display' property starting
4647 at START_POS in OBJECT. */
4649 static struct text_pos
4650 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4652 Lisp_Object end;
4653 struct text_pos end_pos;
4655 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4656 Qdisplay, object, Qnil);
4657 CHARPOS (end_pos) = XFASTINT (end);
4658 if (STRINGP (object))
4659 compute_string_pos (&end_pos, start_pos, it->string);
4660 else
4661 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4663 return end_pos;
4667 /* Set up IT from a single `display' property specification SPEC. OBJECT
4668 is the object in which the `display' property was found. *POSITION
4669 is the position in OBJECT at which the `display' property was found.
4670 BUFPOS is the buffer position of OBJECT (different from POSITION if
4671 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4672 previously saw a display specification which already replaced text
4673 display with something else, for example an image; we ignore such
4674 properties after the first one has been processed.
4676 OVERLAY is the overlay this `display' property came from,
4677 or nil if it was a text property.
4679 If SPEC is a `space' or `image' specification, and in some other
4680 cases too, set *POSITION to the position where the `display'
4681 property ends.
4683 If IT is NULL, only examine the property specification in SPEC, but
4684 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4685 is intended to be displayed in a window on a GUI frame.
4687 Value is non-zero if something was found which replaces the display
4688 of buffer or string text. */
4690 static int
4691 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4692 Lisp_Object overlay, struct text_pos *position,
4693 ptrdiff_t bufpos, int display_replaced_p,
4694 int frame_window_p)
4696 Lisp_Object form;
4697 Lisp_Object location, value;
4698 struct text_pos start_pos = *position;
4699 int valid_p;
4701 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4702 If the result is non-nil, use VALUE instead of SPEC. */
4703 form = Qt;
4704 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4706 spec = XCDR (spec);
4707 if (!CONSP (spec))
4708 return 0;
4709 form = XCAR (spec);
4710 spec = XCDR (spec);
4713 if (!NILP (form) && !EQ (form, Qt))
4715 ptrdiff_t count = SPECPDL_INDEX ();
4716 struct gcpro gcpro1;
4718 /* Bind `object' to the object having the `display' property, a
4719 buffer or string. Bind `position' to the position in the
4720 object where the property was found, and `buffer-position'
4721 to the current position in the buffer. */
4723 if (NILP (object))
4724 XSETBUFFER (object, current_buffer);
4725 specbind (Qobject, object);
4726 specbind (Qposition, make_number (CHARPOS (*position)));
4727 specbind (Qbuffer_position, make_number (bufpos));
4728 GCPRO1 (form);
4729 form = safe_eval (form);
4730 UNGCPRO;
4731 unbind_to (count, Qnil);
4734 if (NILP (form))
4735 return 0;
4737 /* Handle `(height HEIGHT)' specifications. */
4738 if (CONSP (spec)
4739 && EQ (XCAR (spec), Qheight)
4740 && CONSP (XCDR (spec)))
4742 if (it)
4744 if (!FRAME_WINDOW_P (it->f))
4745 return 0;
4747 it->font_height = XCAR (XCDR (spec));
4748 if (!NILP (it->font_height))
4750 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4751 int new_height = -1;
4753 if (CONSP (it->font_height)
4754 && (EQ (XCAR (it->font_height), Qplus)
4755 || EQ (XCAR (it->font_height), Qminus))
4756 && CONSP (XCDR (it->font_height))
4757 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4759 /* `(+ N)' or `(- N)' where N is an integer. */
4760 int steps = XINT (XCAR (XCDR (it->font_height)));
4761 if (EQ (XCAR (it->font_height), Qplus))
4762 steps = - steps;
4763 it->face_id = smaller_face (it->f, it->face_id, steps);
4765 else if (FUNCTIONP (it->font_height))
4767 /* Call function with current height as argument.
4768 Value is the new height. */
4769 Lisp_Object height;
4770 height = safe_call1 (it->font_height,
4771 face->lface[LFACE_HEIGHT_INDEX]);
4772 if (NUMBERP (height))
4773 new_height = XFLOATINT (height);
4775 else if (NUMBERP (it->font_height))
4777 /* Value is a multiple of the canonical char height. */
4778 struct face *f;
4780 f = FACE_FROM_ID (it->f,
4781 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4782 new_height = (XFLOATINT (it->font_height)
4783 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4785 else
4787 /* Evaluate IT->font_height with `height' bound to the
4788 current specified height to get the new height. */
4789 ptrdiff_t count = SPECPDL_INDEX ();
4791 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4792 value = safe_eval (it->font_height);
4793 unbind_to (count, Qnil);
4795 if (NUMBERP (value))
4796 new_height = XFLOATINT (value);
4799 if (new_height > 0)
4800 it->face_id = face_with_height (it->f, it->face_id, new_height);
4804 return 0;
4807 /* Handle `(space-width WIDTH)'. */
4808 if (CONSP (spec)
4809 && EQ (XCAR (spec), Qspace_width)
4810 && CONSP (XCDR (spec)))
4812 if (it)
4814 if (!FRAME_WINDOW_P (it->f))
4815 return 0;
4817 value = XCAR (XCDR (spec));
4818 if (NUMBERP (value) && XFLOATINT (value) > 0)
4819 it->space_width = value;
4822 return 0;
4825 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4826 if (CONSP (spec)
4827 && EQ (XCAR (spec), Qslice))
4829 Lisp_Object tem;
4831 if (it)
4833 if (!FRAME_WINDOW_P (it->f))
4834 return 0;
4836 if (tem = XCDR (spec), CONSP (tem))
4838 it->slice.x = XCAR (tem);
4839 if (tem = XCDR (tem), CONSP (tem))
4841 it->slice.y = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4844 it->slice.width = XCAR (tem);
4845 if (tem = XCDR (tem), CONSP (tem))
4846 it->slice.height = XCAR (tem);
4852 return 0;
4855 /* Handle `(raise FACTOR)'. */
4856 if (CONSP (spec)
4857 && EQ (XCAR (spec), Qraise)
4858 && CONSP (XCDR (spec)))
4860 if (it)
4862 if (!FRAME_WINDOW_P (it->f))
4863 return 0;
4865 #ifdef HAVE_WINDOW_SYSTEM
4866 value = XCAR (XCDR (spec));
4867 if (NUMBERP (value))
4869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4870 it->voffset = - (XFLOATINT (value)
4871 * (FONT_HEIGHT (face->font)));
4873 #endif /* HAVE_WINDOW_SYSTEM */
4876 return 0;
4879 /* Don't handle the other kinds of display specifications
4880 inside a string that we got from a `display' property. */
4881 if (it && it->string_from_display_prop_p)
4882 return 0;
4884 /* Characters having this form of property are not displayed, so
4885 we have to find the end of the property. */
4886 if (it)
4888 start_pos = *position;
4889 *position = display_prop_end (it, object, start_pos);
4891 value = Qnil;
4893 /* Stop the scan at that end position--we assume that all
4894 text properties change there. */
4895 if (it)
4896 it->stop_charpos = position->charpos;
4898 /* Handle `(left-fringe BITMAP [FACE])'
4899 and `(right-fringe BITMAP [FACE])'. */
4900 if (CONSP (spec)
4901 && (EQ (XCAR (spec), Qleft_fringe)
4902 || EQ (XCAR (spec), Qright_fringe))
4903 && CONSP (XCDR (spec)))
4905 int fringe_bitmap;
4907 if (it)
4909 if (!FRAME_WINDOW_P (it->f))
4910 /* If we return here, POSITION has been advanced
4911 across the text with this property. */
4913 /* Synchronize the bidi iterator with POSITION. This is
4914 needed because we are not going to push the iterator
4915 on behalf of this display property, so there will be
4916 no pop_it call to do this synchronization for us. */
4917 if (it->bidi_p)
4919 it->position = *position;
4920 iterate_out_of_display_property (it);
4921 *position = it->position;
4923 return 1;
4926 else if (!frame_window_p)
4927 return 1;
4929 #ifdef HAVE_WINDOW_SYSTEM
4930 value = XCAR (XCDR (spec));
4931 if (!SYMBOLP (value)
4932 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4933 /* If we return here, POSITION has been advanced
4934 across the text with this property. */
4936 if (it && it->bidi_p)
4938 it->position = *position;
4939 iterate_out_of_display_property (it);
4940 *position = it->position;
4942 return 1;
4945 if (it)
4947 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4949 if (CONSP (XCDR (XCDR (spec))))
4951 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4952 int face_id2 = lookup_derived_face (it->f, face_name,
4953 FRINGE_FACE_ID, 0);
4954 if (face_id2 >= 0)
4955 face_id = face_id2;
4958 /* Save current settings of IT so that we can restore them
4959 when we are finished with the glyph property value. */
4960 push_it (it, position);
4962 it->area = TEXT_AREA;
4963 it->what = IT_IMAGE;
4964 it->image_id = -1; /* no image */
4965 it->position = start_pos;
4966 it->object = NILP (object) ? it->w->contents : object;
4967 it->method = GET_FROM_IMAGE;
4968 it->from_overlay = Qnil;
4969 it->face_id = face_id;
4970 it->from_disp_prop_p = true;
4972 /* Say that we haven't consumed the characters with
4973 `display' property yet. The call to pop_it in
4974 set_iterator_to_next will clean this up. */
4975 *position = start_pos;
4977 if (EQ (XCAR (spec), Qleft_fringe))
4979 it->left_user_fringe_bitmap = fringe_bitmap;
4980 it->left_user_fringe_face_id = face_id;
4982 else
4984 it->right_user_fringe_bitmap = fringe_bitmap;
4985 it->right_user_fringe_face_id = face_id;
4988 #endif /* HAVE_WINDOW_SYSTEM */
4989 return 1;
4992 /* Prepare to handle `((margin left-margin) ...)',
4993 `((margin right-margin) ...)' and `((margin nil) ...)'
4994 prefixes for display specifications. */
4995 location = Qunbound;
4996 if (CONSP (spec) && CONSP (XCAR (spec)))
4998 Lisp_Object tem;
5000 value = XCDR (spec);
5001 if (CONSP (value))
5002 value = XCAR (value);
5004 tem = XCAR (spec);
5005 if (EQ (XCAR (tem), Qmargin)
5006 && (tem = XCDR (tem),
5007 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5008 (NILP (tem)
5009 || EQ (tem, Qleft_margin)
5010 || EQ (tem, Qright_margin))))
5011 location = tem;
5014 if (EQ (location, Qunbound))
5016 location = Qnil;
5017 value = spec;
5020 /* After this point, VALUE is the property after any
5021 margin prefix has been stripped. It must be a string,
5022 an image specification, or `(space ...)'.
5024 LOCATION specifies where to display: `left-margin',
5025 `right-margin' or nil. */
5027 valid_p = (STRINGP (value)
5028 #ifdef HAVE_WINDOW_SYSTEM
5029 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5030 && valid_image_p (value))
5031 #endif /* not HAVE_WINDOW_SYSTEM */
5032 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5034 if (valid_p && !display_replaced_p)
5036 int retval = 1;
5038 if (!it)
5040 /* Callers need to know whether the display spec is any kind
5041 of `(space ...)' spec that is about to affect text-area
5042 display. */
5043 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5044 retval = 2;
5045 return retval;
5048 /* Save current settings of IT so that we can restore them
5049 when we are finished with the glyph property value. */
5050 push_it (it, position);
5051 it->from_overlay = overlay;
5052 it->from_disp_prop_p = true;
5054 if (NILP (location))
5055 it->area = TEXT_AREA;
5056 else if (EQ (location, Qleft_margin))
5057 it->area = LEFT_MARGIN_AREA;
5058 else
5059 it->area = RIGHT_MARGIN_AREA;
5061 if (STRINGP (value))
5063 it->string = value;
5064 it->multibyte_p = STRING_MULTIBYTE (it->string);
5065 it->current.overlay_string_index = -1;
5066 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5067 it->end_charpos = it->string_nchars = SCHARS (it->string);
5068 it->method = GET_FROM_STRING;
5069 it->stop_charpos = 0;
5070 it->prev_stop = 0;
5071 it->base_level_stop = 0;
5072 it->string_from_display_prop_p = true;
5073 /* Say that we haven't consumed the characters with
5074 `display' property yet. The call to pop_it in
5075 set_iterator_to_next will clean this up. */
5076 if (BUFFERP (object))
5077 *position = start_pos;
5079 /* Force paragraph direction to be that of the parent
5080 object. If the parent object's paragraph direction is
5081 not yet determined, default to L2R. */
5082 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5083 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5084 else
5085 it->paragraph_embedding = L2R;
5087 /* Set up the bidi iterator for this display string. */
5088 if (it->bidi_p)
5090 it->bidi_it.string.lstring = it->string;
5091 it->bidi_it.string.s = NULL;
5092 it->bidi_it.string.schars = it->end_charpos;
5093 it->bidi_it.string.bufpos = bufpos;
5094 it->bidi_it.string.from_disp_str = 1;
5095 it->bidi_it.string.unibyte = !it->multibyte_p;
5096 it->bidi_it.w = it->w;
5097 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5100 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5102 it->method = GET_FROM_STRETCH;
5103 it->object = value;
5104 *position = it->position = start_pos;
5105 retval = 1 + (it->area == TEXT_AREA);
5107 #ifdef HAVE_WINDOW_SYSTEM
5108 else
5110 it->what = IT_IMAGE;
5111 it->image_id = lookup_image (it->f, value);
5112 it->position = start_pos;
5113 it->object = NILP (object) ? it->w->contents : object;
5114 it->method = GET_FROM_IMAGE;
5116 /* Say that we haven't consumed the characters with
5117 `display' property yet. The call to pop_it in
5118 set_iterator_to_next will clean this up. */
5119 *position = start_pos;
5121 #endif /* HAVE_WINDOW_SYSTEM */
5123 return retval;
5126 /* Invalid property or property not supported. Restore
5127 POSITION to what it was before. */
5128 *position = start_pos;
5129 return 0;
5132 /* Check if PROP is a display property value whose text should be
5133 treated as intangible. OVERLAY is the overlay from which PROP
5134 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5135 specify the buffer position covered by PROP. */
5138 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5139 ptrdiff_t charpos, ptrdiff_t bytepos)
5141 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5142 struct text_pos position;
5144 SET_TEXT_POS (position, charpos, bytepos);
5145 return handle_display_spec (NULL, prop, Qnil, overlay,
5146 &position, charpos, frame_window_p);
5150 /* Return 1 if PROP is a display sub-property value containing STRING.
5152 Implementation note: this and the following function are really
5153 special cases of handle_display_spec and
5154 handle_single_display_spec, and should ideally use the same code.
5155 Until they do, these two pairs must be consistent and must be
5156 modified in sync. */
5158 static int
5159 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5161 if (EQ (string, prop))
5162 return 1;
5164 /* Skip over `when FORM'. */
5165 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5167 prop = XCDR (prop);
5168 if (!CONSP (prop))
5169 return 0;
5170 /* Actually, the condition following `when' should be eval'ed,
5171 like handle_single_display_spec does, and we should return
5172 zero if it evaluates to nil. However, this function is
5173 called only when the buffer was already displayed and some
5174 glyph in the glyph matrix was found to come from a display
5175 string. Therefore, the condition was already evaluated, and
5176 the result was non-nil, otherwise the display string wouldn't
5177 have been displayed and we would have never been called for
5178 this property. Thus, we can skip the evaluation and assume
5179 its result is non-nil. */
5180 prop = XCDR (prop);
5183 if (CONSP (prop))
5184 /* Skip over `margin LOCATION'. */
5185 if (EQ (XCAR (prop), Qmargin))
5187 prop = XCDR (prop);
5188 if (!CONSP (prop))
5189 return 0;
5191 prop = XCDR (prop);
5192 if (!CONSP (prop))
5193 return 0;
5196 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5200 /* Return 1 if STRING appears in the `display' property PROP. */
5202 static int
5203 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5205 if (CONSP (prop)
5206 && !EQ (XCAR (prop), Qwhen)
5207 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5209 /* A list of sub-properties. */
5210 while (CONSP (prop))
5212 if (single_display_spec_string_p (XCAR (prop), string))
5213 return 1;
5214 prop = XCDR (prop);
5217 else if (VECTORP (prop))
5219 /* A vector of sub-properties. */
5220 ptrdiff_t i;
5221 for (i = 0; i < ASIZE (prop); ++i)
5222 if (single_display_spec_string_p (AREF (prop, i), string))
5223 return 1;
5225 else
5226 return single_display_spec_string_p (prop, string);
5228 return 0;
5231 /* Look for STRING in overlays and text properties in the current
5232 buffer, between character positions FROM and TO (excluding TO).
5233 BACK_P non-zero means look back (in this case, TO is supposed to be
5234 less than FROM).
5235 Value is the first character position where STRING was found, or
5236 zero if it wasn't found before hitting TO.
5238 This function may only use code that doesn't eval because it is
5239 called asynchronously from note_mouse_highlight. */
5241 static ptrdiff_t
5242 string_buffer_position_lim (Lisp_Object string,
5243 ptrdiff_t from, ptrdiff_t to, int back_p)
5245 Lisp_Object limit, prop, pos;
5246 int found = 0;
5248 pos = make_number (max (from, BEGV));
5250 if (!back_p) /* looking forward */
5252 limit = make_number (min (to, ZV));
5253 while (!found && !EQ (pos, limit))
5255 prop = Fget_char_property (pos, Qdisplay, Qnil);
5256 if (!NILP (prop) && display_prop_string_p (prop, string))
5257 found = 1;
5258 else
5259 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5260 limit);
5263 else /* looking back */
5265 limit = make_number (max (to, BEGV));
5266 while (!found && !EQ (pos, limit))
5268 prop = Fget_char_property (pos, Qdisplay, Qnil);
5269 if (!NILP (prop) && display_prop_string_p (prop, string))
5270 found = 1;
5271 else
5272 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5273 limit);
5277 return found ? XINT (pos) : 0;
5280 /* Determine which buffer position in current buffer STRING comes from.
5281 AROUND_CHARPOS is an approximate position where it could come from.
5282 Value is the buffer position or 0 if it couldn't be determined.
5284 This function is necessary because we don't record buffer positions
5285 in glyphs generated from strings (to keep struct glyph small).
5286 This function may only use code that doesn't eval because it is
5287 called asynchronously from note_mouse_highlight. */
5289 static ptrdiff_t
5290 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5292 const int MAX_DISTANCE = 1000;
5293 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5294 around_charpos + MAX_DISTANCE,
5297 if (!found)
5298 found = string_buffer_position_lim (string, around_charpos,
5299 around_charpos - MAX_DISTANCE, 1);
5300 return found;
5305 /***********************************************************************
5306 `composition' property
5307 ***********************************************************************/
5309 /* Set up iterator IT from `composition' property at its current
5310 position. Called from handle_stop. */
5312 static enum prop_handled
5313 handle_composition_prop (struct it *it)
5315 Lisp_Object prop, string;
5316 ptrdiff_t pos, pos_byte, start, end;
5318 if (STRINGP (it->string))
5320 unsigned char *s;
5322 pos = IT_STRING_CHARPOS (*it);
5323 pos_byte = IT_STRING_BYTEPOS (*it);
5324 string = it->string;
5325 s = SDATA (string) + pos_byte;
5326 it->c = STRING_CHAR (s);
5328 else
5330 pos = IT_CHARPOS (*it);
5331 pos_byte = IT_BYTEPOS (*it);
5332 string = Qnil;
5333 it->c = FETCH_CHAR (pos_byte);
5336 /* If there's a valid composition and point is not inside of the
5337 composition (in the case that the composition is from the current
5338 buffer), draw a glyph composed from the composition components. */
5339 if (find_composition (pos, -1, &start, &end, &prop, string)
5340 && composition_valid_p (start, end, prop)
5341 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5343 if (start < pos)
5344 /* As we can't handle this situation (perhaps font-lock added
5345 a new composition), we just return here hoping that next
5346 redisplay will detect this composition much earlier. */
5347 return HANDLED_NORMALLY;
5348 if (start != pos)
5350 if (STRINGP (it->string))
5351 pos_byte = string_char_to_byte (it->string, start);
5352 else
5353 pos_byte = CHAR_TO_BYTE (start);
5355 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5356 prop, string);
5358 if (it->cmp_it.id >= 0)
5360 it->cmp_it.ch = -1;
5361 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5362 it->cmp_it.nglyphs = -1;
5366 return HANDLED_NORMALLY;
5371 /***********************************************************************
5372 Overlay strings
5373 ***********************************************************************/
5375 /* The following structure is used to record overlay strings for
5376 later sorting in load_overlay_strings. */
5378 struct overlay_entry
5380 Lisp_Object overlay;
5381 Lisp_Object string;
5382 EMACS_INT priority;
5383 int after_string_p;
5387 /* Set up iterator IT from overlay strings at its current position.
5388 Called from handle_stop. */
5390 static enum prop_handled
5391 handle_overlay_change (struct it *it)
5393 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5394 return HANDLED_RECOMPUTE_PROPS;
5395 else
5396 return HANDLED_NORMALLY;
5400 /* Set up the next overlay string for delivery by IT, if there is an
5401 overlay string to deliver. Called by set_iterator_to_next when the
5402 end of the current overlay string is reached. If there are more
5403 overlay strings to display, IT->string and
5404 IT->current.overlay_string_index are set appropriately here.
5405 Otherwise IT->string is set to nil. */
5407 static void
5408 next_overlay_string (struct it *it)
5410 ++it->current.overlay_string_index;
5411 if (it->current.overlay_string_index == it->n_overlay_strings)
5413 /* No more overlay strings. Restore IT's settings to what
5414 they were before overlay strings were processed, and
5415 continue to deliver from current_buffer. */
5417 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5418 pop_it (it);
5419 eassert (it->sp > 0
5420 || (NILP (it->string)
5421 && it->method == GET_FROM_BUFFER
5422 && it->stop_charpos >= BEGV
5423 && it->stop_charpos <= it->end_charpos));
5424 it->current.overlay_string_index = -1;
5425 it->n_overlay_strings = 0;
5426 it->overlay_strings_charpos = -1;
5427 /* If there's an empty display string on the stack, pop the
5428 stack, to resync the bidi iterator with IT's position. Such
5429 empty strings are pushed onto the stack in
5430 get_overlay_strings_1. */
5431 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5432 pop_it (it);
5434 /* If we're at the end of the buffer, record that we have
5435 processed the overlay strings there already, so that
5436 next_element_from_buffer doesn't try it again. */
5437 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5438 it->overlay_strings_at_end_processed_p = true;
5440 else
5442 /* There are more overlay strings to process. If
5443 IT->current.overlay_string_index has advanced to a position
5444 where we must load IT->overlay_strings with more strings, do
5445 it. We must load at the IT->overlay_strings_charpos where
5446 IT->n_overlay_strings was originally computed; when invisible
5447 text is present, this might not be IT_CHARPOS (Bug#7016). */
5448 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5450 if (it->current.overlay_string_index && i == 0)
5451 load_overlay_strings (it, it->overlay_strings_charpos);
5453 /* Initialize IT to deliver display elements from the overlay
5454 string. */
5455 it->string = it->overlay_strings[i];
5456 it->multibyte_p = STRING_MULTIBYTE (it->string);
5457 SET_TEXT_POS (it->current.string_pos, 0, 0);
5458 it->method = GET_FROM_STRING;
5459 it->stop_charpos = 0;
5460 it->end_charpos = SCHARS (it->string);
5461 if (it->cmp_it.stop_pos >= 0)
5462 it->cmp_it.stop_pos = 0;
5463 it->prev_stop = 0;
5464 it->base_level_stop = 0;
5466 /* Set up the bidi iterator for this overlay string. */
5467 if (it->bidi_p)
5469 it->bidi_it.string.lstring = it->string;
5470 it->bidi_it.string.s = NULL;
5471 it->bidi_it.string.schars = SCHARS (it->string);
5472 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5473 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5474 it->bidi_it.string.unibyte = !it->multibyte_p;
5475 it->bidi_it.w = it->w;
5476 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5480 CHECK_IT (it);
5484 /* Compare two overlay_entry structures E1 and E2. Used as a
5485 comparison function for qsort in load_overlay_strings. Overlay
5486 strings for the same position are sorted so that
5488 1. All after-strings come in front of before-strings, except
5489 when they come from the same overlay.
5491 2. Within after-strings, strings are sorted so that overlay strings
5492 from overlays with higher priorities come first.
5494 2. Within before-strings, strings are sorted so that overlay
5495 strings from overlays with higher priorities come last.
5497 Value is analogous to strcmp. */
5500 static int
5501 compare_overlay_entries (const void *e1, const void *e2)
5503 struct overlay_entry const *entry1 = e1;
5504 struct overlay_entry const *entry2 = e2;
5505 int result;
5507 if (entry1->after_string_p != entry2->after_string_p)
5509 /* Let after-strings appear in front of before-strings if
5510 they come from different overlays. */
5511 if (EQ (entry1->overlay, entry2->overlay))
5512 result = entry1->after_string_p ? 1 : -1;
5513 else
5514 result = entry1->after_string_p ? -1 : 1;
5516 else if (entry1->priority != entry2->priority)
5518 if (entry1->after_string_p)
5519 /* After-strings sorted in order of decreasing priority. */
5520 result = entry2->priority < entry1->priority ? -1 : 1;
5521 else
5522 /* Before-strings sorted in order of increasing priority. */
5523 result = entry1->priority < entry2->priority ? -1 : 1;
5525 else
5526 result = 0;
5528 return result;
5532 /* Load the vector IT->overlay_strings with overlay strings from IT's
5533 current buffer position, or from CHARPOS if that is > 0. Set
5534 IT->n_overlays to the total number of overlay strings found.
5536 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5537 a time. On entry into load_overlay_strings,
5538 IT->current.overlay_string_index gives the number of overlay
5539 strings that have already been loaded by previous calls to this
5540 function.
5542 IT->add_overlay_start contains an additional overlay start
5543 position to consider for taking overlay strings from, if non-zero.
5544 This position comes into play when the overlay has an `invisible'
5545 property, and both before and after-strings. When we've skipped to
5546 the end of the overlay, because of its `invisible' property, we
5547 nevertheless want its before-string to appear.
5548 IT->add_overlay_start will contain the overlay start position
5549 in this case.
5551 Overlay strings are sorted so that after-string strings come in
5552 front of before-string strings. Within before and after-strings,
5553 strings are sorted by overlay priority. See also function
5554 compare_overlay_entries. */
5556 static void
5557 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5559 Lisp_Object overlay, window, str, invisible;
5560 struct Lisp_Overlay *ov;
5561 ptrdiff_t start, end;
5562 ptrdiff_t size = 20;
5563 ptrdiff_t n = 0, i, j;
5564 int invis_p;
5565 struct overlay_entry *entries = alloca (size * sizeof *entries);
5566 USE_SAFE_ALLOCA;
5568 if (charpos <= 0)
5569 charpos = IT_CHARPOS (*it);
5571 /* Append the overlay string STRING of overlay OVERLAY to vector
5572 `entries' which has size `size' and currently contains `n'
5573 elements. AFTER_P non-zero means STRING is an after-string of
5574 OVERLAY. */
5575 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5576 do \
5578 Lisp_Object priority; \
5580 if (n == size) \
5582 struct overlay_entry *old = entries; \
5583 SAFE_NALLOCA (entries, 2, size); \
5584 memcpy (entries, old, size * sizeof *entries); \
5585 size *= 2; \
5588 entries[n].string = (STRING); \
5589 entries[n].overlay = (OVERLAY); \
5590 priority = Foverlay_get ((OVERLAY), Qpriority); \
5591 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5592 entries[n].after_string_p = (AFTER_P); \
5593 ++n; \
5595 while (0)
5597 /* Process overlay before the overlay center. */
5598 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5600 XSETMISC (overlay, ov);
5601 eassert (OVERLAYP (overlay));
5602 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5603 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5605 if (end < charpos)
5606 break;
5608 /* Skip this overlay if it doesn't start or end at IT's current
5609 position. */
5610 if (end != charpos && start != charpos)
5611 continue;
5613 /* Skip this overlay if it doesn't apply to IT->w. */
5614 window = Foverlay_get (overlay, Qwindow);
5615 if (WINDOWP (window) && XWINDOW (window) != it->w)
5616 continue;
5618 /* If the text ``under'' the overlay is invisible, both before-
5619 and after-strings from this overlay are visible; start and
5620 end position are indistinguishable. */
5621 invisible = Foverlay_get (overlay, Qinvisible);
5622 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5624 /* If overlay has a non-empty before-string, record it. */
5625 if ((start == charpos || (end == charpos && invis_p))
5626 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5627 && SCHARS (str))
5628 RECORD_OVERLAY_STRING (overlay, str, 0);
5630 /* If overlay has a non-empty after-string, record it. */
5631 if ((end == charpos || (start == charpos && invis_p))
5632 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5633 && SCHARS (str))
5634 RECORD_OVERLAY_STRING (overlay, str, 1);
5637 /* Process overlays after the overlay center. */
5638 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5640 XSETMISC (overlay, ov);
5641 eassert (OVERLAYP (overlay));
5642 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5643 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5645 if (start > charpos)
5646 break;
5648 /* Skip this overlay if it doesn't start or end at IT's current
5649 position. */
5650 if (end != charpos && start != charpos)
5651 continue;
5653 /* Skip this overlay if it doesn't apply to IT->w. */
5654 window = Foverlay_get (overlay, Qwindow);
5655 if (WINDOWP (window) && XWINDOW (window) != it->w)
5656 continue;
5658 /* If the text ``under'' the overlay is invisible, it has a zero
5659 dimension, and both before- and after-strings apply. */
5660 invisible = Foverlay_get (overlay, Qinvisible);
5661 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5663 /* If overlay has a non-empty before-string, record it. */
5664 if ((start == charpos || (end == charpos && invis_p))
5665 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5666 && SCHARS (str))
5667 RECORD_OVERLAY_STRING (overlay, str, 0);
5669 /* If overlay has a non-empty after-string, record it. */
5670 if ((end == charpos || (start == charpos && invis_p))
5671 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5672 && SCHARS (str))
5673 RECORD_OVERLAY_STRING (overlay, str, 1);
5676 #undef RECORD_OVERLAY_STRING
5678 /* Sort entries. */
5679 if (n > 1)
5680 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5682 /* Record number of overlay strings, and where we computed it. */
5683 it->n_overlay_strings = n;
5684 it->overlay_strings_charpos = charpos;
5686 /* IT->current.overlay_string_index is the number of overlay strings
5687 that have already been consumed by IT. Copy some of the
5688 remaining overlay strings to IT->overlay_strings. */
5689 i = 0;
5690 j = it->current.overlay_string_index;
5691 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5693 it->overlay_strings[i] = entries[j].string;
5694 it->string_overlays[i++] = entries[j++].overlay;
5697 CHECK_IT (it);
5698 SAFE_FREE ();
5702 /* Get the first chunk of overlay strings at IT's current buffer
5703 position, or at CHARPOS if that is > 0. Value is non-zero if at
5704 least one overlay string was found. */
5706 static int
5707 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5709 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5710 process. This fills IT->overlay_strings with strings, and sets
5711 IT->n_overlay_strings to the total number of strings to process.
5712 IT->pos.overlay_string_index has to be set temporarily to zero
5713 because load_overlay_strings needs this; it must be set to -1
5714 when no overlay strings are found because a zero value would
5715 indicate a position in the first overlay string. */
5716 it->current.overlay_string_index = 0;
5717 load_overlay_strings (it, charpos);
5719 /* If we found overlay strings, set up IT to deliver display
5720 elements from the first one. Otherwise set up IT to deliver
5721 from current_buffer. */
5722 if (it->n_overlay_strings)
5724 /* Make sure we know settings in current_buffer, so that we can
5725 restore meaningful values when we're done with the overlay
5726 strings. */
5727 if (compute_stop_p)
5728 compute_stop_pos (it);
5729 eassert (it->face_id >= 0);
5731 /* Save IT's settings. They are restored after all overlay
5732 strings have been processed. */
5733 eassert (!compute_stop_p || it->sp == 0);
5735 /* When called from handle_stop, there might be an empty display
5736 string loaded. In that case, don't bother saving it. But
5737 don't use this optimization with the bidi iterator, since we
5738 need the corresponding pop_it call to resync the bidi
5739 iterator's position with IT's position, after we are done
5740 with the overlay strings. (The corresponding call to pop_it
5741 in case of an empty display string is in
5742 next_overlay_string.) */
5743 if (!(!it->bidi_p
5744 && STRINGP (it->string) && !SCHARS (it->string)))
5745 push_it (it, NULL);
5747 /* Set up IT to deliver display elements from the first overlay
5748 string. */
5749 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5750 it->string = it->overlay_strings[0];
5751 it->from_overlay = Qnil;
5752 it->stop_charpos = 0;
5753 eassert (STRINGP (it->string));
5754 it->end_charpos = SCHARS (it->string);
5755 it->prev_stop = 0;
5756 it->base_level_stop = 0;
5757 it->multibyte_p = STRING_MULTIBYTE (it->string);
5758 it->method = GET_FROM_STRING;
5759 it->from_disp_prop_p = 0;
5761 /* Force paragraph direction to be that of the parent
5762 buffer. */
5763 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5764 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5765 else
5766 it->paragraph_embedding = L2R;
5768 /* Set up the bidi iterator for this overlay string. */
5769 if (it->bidi_p)
5771 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5773 it->bidi_it.string.lstring = it->string;
5774 it->bidi_it.string.s = NULL;
5775 it->bidi_it.string.schars = SCHARS (it->string);
5776 it->bidi_it.string.bufpos = pos;
5777 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5778 it->bidi_it.string.unibyte = !it->multibyte_p;
5779 it->bidi_it.w = it->w;
5780 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5782 return 1;
5785 it->current.overlay_string_index = -1;
5786 return 0;
5789 static int
5790 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5792 it->string = Qnil;
5793 it->method = GET_FROM_BUFFER;
5795 (void) get_overlay_strings_1 (it, charpos, 1);
5797 CHECK_IT (it);
5799 /* Value is non-zero if we found at least one overlay string. */
5800 return STRINGP (it->string);
5805 /***********************************************************************
5806 Saving and restoring state
5807 ***********************************************************************/
5809 /* Save current settings of IT on IT->stack. Called, for example,
5810 before setting up IT for an overlay string, to be able to restore
5811 IT's settings to what they were after the overlay string has been
5812 processed. If POSITION is non-NULL, it is the position to save on
5813 the stack instead of IT->position. */
5815 static void
5816 push_it (struct it *it, struct text_pos *position)
5818 struct iterator_stack_entry *p;
5820 eassert (it->sp < IT_STACK_SIZE);
5821 p = it->stack + it->sp;
5823 p->stop_charpos = it->stop_charpos;
5824 p->prev_stop = it->prev_stop;
5825 p->base_level_stop = it->base_level_stop;
5826 p->cmp_it = it->cmp_it;
5827 eassert (it->face_id >= 0);
5828 p->face_id = it->face_id;
5829 p->string = it->string;
5830 p->method = it->method;
5831 p->from_overlay = it->from_overlay;
5832 switch (p->method)
5834 case GET_FROM_IMAGE:
5835 p->u.image.object = it->object;
5836 p->u.image.image_id = it->image_id;
5837 p->u.image.slice = it->slice;
5838 break;
5839 case GET_FROM_STRETCH:
5840 p->u.stretch.object = it->object;
5841 break;
5843 p->position = position ? *position : it->position;
5844 p->current = it->current;
5845 p->end_charpos = it->end_charpos;
5846 p->string_nchars = it->string_nchars;
5847 p->area = it->area;
5848 p->multibyte_p = it->multibyte_p;
5849 p->avoid_cursor_p = it->avoid_cursor_p;
5850 p->space_width = it->space_width;
5851 p->font_height = it->font_height;
5852 p->voffset = it->voffset;
5853 p->string_from_display_prop_p = it->string_from_display_prop_p;
5854 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5855 p->display_ellipsis_p = 0;
5856 p->line_wrap = it->line_wrap;
5857 p->bidi_p = it->bidi_p;
5858 p->paragraph_embedding = it->paragraph_embedding;
5859 p->from_disp_prop_p = it->from_disp_prop_p;
5860 ++it->sp;
5862 /* Save the state of the bidi iterator as well. */
5863 if (it->bidi_p)
5864 bidi_push_it (&it->bidi_it);
5867 static void
5868 iterate_out_of_display_property (struct it *it)
5870 int buffer_p = !STRINGP (it->string);
5871 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5872 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5874 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5876 /* Maybe initialize paragraph direction. If we are at the beginning
5877 of a new paragraph, next_element_from_buffer may not have a
5878 chance to do that. */
5879 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5880 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5881 /* prev_stop can be zero, so check against BEGV as well. */
5882 while (it->bidi_it.charpos >= bob
5883 && it->prev_stop <= it->bidi_it.charpos
5884 && it->bidi_it.charpos < CHARPOS (it->position)
5885 && it->bidi_it.charpos < eob)
5886 bidi_move_to_visually_next (&it->bidi_it);
5887 /* Record the stop_pos we just crossed, for when we cross it
5888 back, maybe. */
5889 if (it->bidi_it.charpos > CHARPOS (it->position))
5890 it->prev_stop = CHARPOS (it->position);
5891 /* If we ended up not where pop_it put us, resync IT's
5892 positional members with the bidi iterator. */
5893 if (it->bidi_it.charpos != CHARPOS (it->position))
5894 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5895 if (buffer_p)
5896 it->current.pos = it->position;
5897 else
5898 it->current.string_pos = it->position;
5901 /* Restore IT's settings from IT->stack. Called, for example, when no
5902 more overlay strings must be processed, and we return to delivering
5903 display elements from a buffer, or when the end of a string from a
5904 `display' property is reached and we return to delivering display
5905 elements from an overlay string, or from a buffer. */
5907 static void
5908 pop_it (struct it *it)
5910 struct iterator_stack_entry *p;
5911 int from_display_prop = it->from_disp_prop_p;
5913 eassert (it->sp > 0);
5914 --it->sp;
5915 p = it->stack + it->sp;
5916 it->stop_charpos = p->stop_charpos;
5917 it->prev_stop = p->prev_stop;
5918 it->base_level_stop = p->base_level_stop;
5919 it->cmp_it = p->cmp_it;
5920 it->face_id = p->face_id;
5921 it->current = p->current;
5922 it->position = p->position;
5923 it->string = p->string;
5924 it->from_overlay = p->from_overlay;
5925 if (NILP (it->string))
5926 SET_TEXT_POS (it->current.string_pos, -1, -1);
5927 it->method = p->method;
5928 switch (it->method)
5930 case GET_FROM_IMAGE:
5931 it->image_id = p->u.image.image_id;
5932 it->object = p->u.image.object;
5933 it->slice = p->u.image.slice;
5934 break;
5935 case GET_FROM_STRETCH:
5936 it->object = p->u.stretch.object;
5937 break;
5938 case GET_FROM_BUFFER:
5939 it->object = it->w->contents;
5940 break;
5941 case GET_FROM_STRING:
5942 it->object = it->string;
5943 break;
5944 case GET_FROM_DISPLAY_VECTOR:
5945 if (it->s)
5946 it->method = GET_FROM_C_STRING;
5947 else if (STRINGP (it->string))
5948 it->method = GET_FROM_STRING;
5949 else
5951 it->method = GET_FROM_BUFFER;
5952 it->object = it->w->contents;
5955 it->end_charpos = p->end_charpos;
5956 it->string_nchars = p->string_nchars;
5957 it->area = p->area;
5958 it->multibyte_p = p->multibyte_p;
5959 it->avoid_cursor_p = p->avoid_cursor_p;
5960 it->space_width = p->space_width;
5961 it->font_height = p->font_height;
5962 it->voffset = p->voffset;
5963 it->string_from_display_prop_p = p->string_from_display_prop_p;
5964 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5965 it->line_wrap = p->line_wrap;
5966 it->bidi_p = p->bidi_p;
5967 it->paragraph_embedding = p->paragraph_embedding;
5968 it->from_disp_prop_p = p->from_disp_prop_p;
5969 if (it->bidi_p)
5971 bidi_pop_it (&it->bidi_it);
5972 /* Bidi-iterate until we get out of the portion of text, if any,
5973 covered by a `display' text property or by an overlay with
5974 `display' property. (We cannot just jump there, because the
5975 internal coherency of the bidi iterator state can not be
5976 preserved across such jumps.) We also must determine the
5977 paragraph base direction if the overlay we just processed is
5978 at the beginning of a new paragraph. */
5979 if (from_display_prop
5980 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5981 iterate_out_of_display_property (it);
5983 eassert ((BUFFERP (it->object)
5984 && IT_CHARPOS (*it) == it->bidi_it.charpos
5985 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5986 || (STRINGP (it->object)
5987 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5988 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5989 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5995 /***********************************************************************
5996 Moving over lines
5997 ***********************************************************************/
5999 /* Set IT's current position to the previous line start. */
6001 static void
6002 back_to_previous_line_start (struct it *it)
6004 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6006 DEC_BOTH (cp, bp);
6007 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6011 /* Move IT to the next line start.
6013 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6014 we skipped over part of the text (as opposed to moving the iterator
6015 continuously over the text). Otherwise, don't change the value
6016 of *SKIPPED_P.
6018 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6019 iterator on the newline, if it was found.
6021 Newlines may come from buffer text, overlay strings, or strings
6022 displayed via the `display' property. That's the reason we can't
6023 simply use find_newline_no_quit.
6025 Note that this function may not skip over invisible text that is so
6026 because of text properties and immediately follows a newline. If
6027 it would, function reseat_at_next_visible_line_start, when called
6028 from set_iterator_to_next, would effectively make invisible
6029 characters following a newline part of the wrong glyph row, which
6030 leads to wrong cursor motion. */
6032 static int
6033 forward_to_next_line_start (struct it *it, int *skipped_p,
6034 struct bidi_it *bidi_it_prev)
6036 ptrdiff_t old_selective;
6037 int newline_found_p, n;
6038 const int MAX_NEWLINE_DISTANCE = 500;
6040 /* If already on a newline, just consume it to avoid unintended
6041 skipping over invisible text below. */
6042 if (it->what == IT_CHARACTER
6043 && it->c == '\n'
6044 && CHARPOS (it->position) == IT_CHARPOS (*it))
6046 if (it->bidi_p && bidi_it_prev)
6047 *bidi_it_prev = it->bidi_it;
6048 set_iterator_to_next (it, 0);
6049 it->c = 0;
6050 return 1;
6053 /* Don't handle selective display in the following. It's (a)
6054 unnecessary because it's done by the caller, and (b) leads to an
6055 infinite recursion because next_element_from_ellipsis indirectly
6056 calls this function. */
6057 old_selective = it->selective;
6058 it->selective = 0;
6060 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6061 from buffer text. */
6062 for (n = newline_found_p = 0;
6063 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6064 n += STRINGP (it->string) ? 0 : 1)
6066 if (!get_next_display_element (it))
6067 return 0;
6068 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6069 if (newline_found_p && it->bidi_p && bidi_it_prev)
6070 *bidi_it_prev = it->bidi_it;
6071 set_iterator_to_next (it, 0);
6074 /* If we didn't find a newline near enough, see if we can use a
6075 short-cut. */
6076 if (!newline_found_p)
6078 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6079 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6080 1, &bytepos);
6081 Lisp_Object pos;
6083 eassert (!STRINGP (it->string));
6085 /* If there isn't any `display' property in sight, and no
6086 overlays, we can just use the position of the newline in
6087 buffer text. */
6088 if (it->stop_charpos >= limit
6089 || ((pos = Fnext_single_property_change (make_number (start),
6090 Qdisplay, Qnil,
6091 make_number (limit)),
6092 NILP (pos))
6093 && next_overlay_change (start) == ZV))
6095 if (!it->bidi_p)
6097 IT_CHARPOS (*it) = limit;
6098 IT_BYTEPOS (*it) = bytepos;
6100 else
6102 struct bidi_it bprev;
6104 /* Help bidi.c avoid expensive searches for display
6105 properties and overlays, by telling it that there are
6106 none up to `limit'. */
6107 if (it->bidi_it.disp_pos < limit)
6109 it->bidi_it.disp_pos = limit;
6110 it->bidi_it.disp_prop = 0;
6112 do {
6113 bprev = it->bidi_it;
6114 bidi_move_to_visually_next (&it->bidi_it);
6115 } while (it->bidi_it.charpos != limit);
6116 IT_CHARPOS (*it) = limit;
6117 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6118 if (bidi_it_prev)
6119 *bidi_it_prev = bprev;
6121 *skipped_p = newline_found_p = true;
6123 else
6125 while (get_next_display_element (it)
6126 && !newline_found_p)
6128 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6129 if (newline_found_p && it->bidi_p && bidi_it_prev)
6130 *bidi_it_prev = it->bidi_it;
6131 set_iterator_to_next (it, 0);
6136 it->selective = old_selective;
6137 return newline_found_p;
6141 /* Set IT's current position to the previous visible line start. Skip
6142 invisible text that is so either due to text properties or due to
6143 selective display. Caution: this does not change IT->current_x and
6144 IT->hpos. */
6146 static void
6147 back_to_previous_visible_line_start (struct it *it)
6149 while (IT_CHARPOS (*it) > BEGV)
6151 back_to_previous_line_start (it);
6153 if (IT_CHARPOS (*it) <= BEGV)
6154 break;
6156 /* If selective > 0, then lines indented more than its value are
6157 invisible. */
6158 if (it->selective > 0
6159 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6160 it->selective))
6161 continue;
6163 /* Check the newline before point for invisibility. */
6165 Lisp_Object prop;
6166 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6167 Qinvisible, it->window);
6168 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6169 continue;
6172 if (IT_CHARPOS (*it) <= BEGV)
6173 break;
6176 struct it it2;
6177 void *it2data = NULL;
6178 ptrdiff_t pos;
6179 ptrdiff_t beg, end;
6180 Lisp_Object val, overlay;
6182 SAVE_IT (it2, *it, it2data);
6184 /* If newline is part of a composition, continue from start of composition */
6185 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6186 && beg < IT_CHARPOS (*it))
6187 goto replaced;
6189 /* If newline is replaced by a display property, find start of overlay
6190 or interval and continue search from that point. */
6191 pos = --IT_CHARPOS (it2);
6192 --IT_BYTEPOS (it2);
6193 it2.sp = 0;
6194 bidi_unshelve_cache (NULL, 0);
6195 it2.string_from_display_prop_p = 0;
6196 it2.from_disp_prop_p = 0;
6197 if (handle_display_prop (&it2) == HANDLED_RETURN
6198 && !NILP (val = get_char_property_and_overlay
6199 (make_number (pos), Qdisplay, Qnil, &overlay))
6200 && (OVERLAYP (overlay)
6201 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6202 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6204 RESTORE_IT (it, it, it2data);
6205 goto replaced;
6208 /* Newline is not replaced by anything -- so we are done. */
6209 RESTORE_IT (it, it, it2data);
6210 break;
6212 replaced:
6213 if (beg < BEGV)
6214 beg = BEGV;
6215 IT_CHARPOS (*it) = beg;
6216 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6220 it->continuation_lines_width = 0;
6222 eassert (IT_CHARPOS (*it) >= BEGV);
6223 eassert (IT_CHARPOS (*it) == BEGV
6224 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6225 CHECK_IT (it);
6229 /* Reseat iterator IT at the previous visible line start. Skip
6230 invisible text that is so either due to text properties or due to
6231 selective display. At the end, update IT's overlay information,
6232 face information etc. */
6234 void
6235 reseat_at_previous_visible_line_start (struct it *it)
6237 back_to_previous_visible_line_start (it);
6238 reseat (it, it->current.pos, 1);
6239 CHECK_IT (it);
6243 /* Reseat iterator IT on the next visible line start in the current
6244 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6245 preceding the line start. Skip over invisible text that is so
6246 because of selective display. Compute faces, overlays etc at the
6247 new position. Note that this function does not skip over text that
6248 is invisible because of text properties. */
6250 static void
6251 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6253 int newline_found_p, skipped_p = 0;
6254 struct bidi_it bidi_it_prev;
6256 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6258 /* Skip over lines that are invisible because they are indented
6259 more than the value of IT->selective. */
6260 if (it->selective > 0)
6261 while (IT_CHARPOS (*it) < ZV
6262 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6263 it->selective))
6265 eassert (IT_BYTEPOS (*it) == BEGV
6266 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6267 newline_found_p =
6268 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6271 /* Position on the newline if that's what's requested. */
6272 if (on_newline_p && newline_found_p)
6274 if (STRINGP (it->string))
6276 if (IT_STRING_CHARPOS (*it) > 0)
6278 if (!it->bidi_p)
6280 --IT_STRING_CHARPOS (*it);
6281 --IT_STRING_BYTEPOS (*it);
6283 else
6285 /* We need to restore the bidi iterator to the state
6286 it had on the newline, and resync the IT's
6287 position with that. */
6288 it->bidi_it = bidi_it_prev;
6289 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6290 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6294 else if (IT_CHARPOS (*it) > BEGV)
6296 if (!it->bidi_p)
6298 --IT_CHARPOS (*it);
6299 --IT_BYTEPOS (*it);
6301 else
6303 /* We need to restore the bidi iterator to the state it
6304 had on the newline and resync IT with that. */
6305 it->bidi_it = bidi_it_prev;
6306 IT_CHARPOS (*it) = it->bidi_it.charpos;
6307 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6309 reseat (it, it->current.pos, 0);
6312 else if (skipped_p)
6313 reseat (it, it->current.pos, 0);
6315 CHECK_IT (it);
6320 /***********************************************************************
6321 Changing an iterator's position
6322 ***********************************************************************/
6324 /* Change IT's current position to POS in current_buffer. If FORCE_P
6325 is non-zero, always check for text properties at the new position.
6326 Otherwise, text properties are only looked up if POS >=
6327 IT->check_charpos of a property. */
6329 static void
6330 reseat (struct it *it, struct text_pos pos, int force_p)
6332 ptrdiff_t original_pos = IT_CHARPOS (*it);
6334 reseat_1 (it, pos, 0);
6336 /* Determine where to check text properties. Avoid doing it
6337 where possible because text property lookup is very expensive. */
6338 if (force_p
6339 || CHARPOS (pos) > it->stop_charpos
6340 || CHARPOS (pos) < original_pos)
6342 if (it->bidi_p)
6344 /* For bidi iteration, we need to prime prev_stop and
6345 base_level_stop with our best estimations. */
6346 /* Implementation note: Of course, POS is not necessarily a
6347 stop position, so assigning prev_pos to it is a lie; we
6348 should have called compute_stop_backwards. However, if
6349 the current buffer does not include any R2L characters,
6350 that call would be a waste of cycles, because the
6351 iterator will never move back, and thus never cross this
6352 "fake" stop position. So we delay that backward search
6353 until the time we really need it, in next_element_from_buffer. */
6354 if (CHARPOS (pos) != it->prev_stop)
6355 it->prev_stop = CHARPOS (pos);
6356 if (CHARPOS (pos) < it->base_level_stop)
6357 it->base_level_stop = 0; /* meaning it's unknown */
6358 handle_stop (it);
6360 else
6362 handle_stop (it);
6363 it->prev_stop = it->base_level_stop = 0;
6368 CHECK_IT (it);
6372 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6373 IT->stop_pos to POS, also. */
6375 static void
6376 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6378 /* Don't call this function when scanning a C string. */
6379 eassert (it->s == NULL);
6381 /* POS must be a reasonable value. */
6382 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6384 it->current.pos = it->position = pos;
6385 it->end_charpos = ZV;
6386 it->dpvec = NULL;
6387 it->current.dpvec_index = -1;
6388 it->current.overlay_string_index = -1;
6389 IT_STRING_CHARPOS (*it) = -1;
6390 IT_STRING_BYTEPOS (*it) = -1;
6391 it->string = Qnil;
6392 it->method = GET_FROM_BUFFER;
6393 it->object = it->w->contents;
6394 it->area = TEXT_AREA;
6395 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6396 it->sp = 0;
6397 it->string_from_display_prop_p = 0;
6398 it->string_from_prefix_prop_p = 0;
6400 it->from_disp_prop_p = 0;
6401 it->face_before_selective_p = 0;
6402 if (it->bidi_p)
6404 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6405 &it->bidi_it);
6406 bidi_unshelve_cache (NULL, 0);
6407 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6408 it->bidi_it.string.s = NULL;
6409 it->bidi_it.string.lstring = Qnil;
6410 it->bidi_it.string.bufpos = 0;
6411 it->bidi_it.string.unibyte = 0;
6412 it->bidi_it.w = it->w;
6415 if (set_stop_p)
6417 it->stop_charpos = CHARPOS (pos);
6418 it->base_level_stop = CHARPOS (pos);
6420 /* This make the information stored in it->cmp_it invalidate. */
6421 it->cmp_it.id = -1;
6425 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6426 If S is non-null, it is a C string to iterate over. Otherwise,
6427 STRING gives a Lisp string to iterate over.
6429 If PRECISION > 0, don't return more then PRECISION number of
6430 characters from the string.
6432 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6433 characters have been returned. FIELD_WIDTH < 0 means an infinite
6434 field width.
6436 MULTIBYTE = 0 means disable processing of multibyte characters,
6437 MULTIBYTE > 0 means enable it,
6438 MULTIBYTE < 0 means use IT->multibyte_p.
6440 IT must be initialized via a prior call to init_iterator before
6441 calling this function. */
6443 static void
6444 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6445 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6446 int multibyte)
6448 /* No text property checks performed by default, but see below. */
6449 it->stop_charpos = -1;
6451 /* Set iterator position and end position. */
6452 memset (&it->current, 0, sizeof it->current);
6453 it->current.overlay_string_index = -1;
6454 it->current.dpvec_index = -1;
6455 eassert (charpos >= 0);
6457 /* If STRING is specified, use its multibyteness, otherwise use the
6458 setting of MULTIBYTE, if specified. */
6459 if (multibyte >= 0)
6460 it->multibyte_p = multibyte > 0;
6462 /* Bidirectional reordering of strings is controlled by the default
6463 value of bidi-display-reordering. Don't try to reorder while
6464 loading loadup.el, as the necessary character property tables are
6465 not yet available. */
6466 it->bidi_p =
6467 NILP (Vpurify_flag)
6468 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6470 if (s == NULL)
6472 eassert (STRINGP (string));
6473 it->string = string;
6474 it->s = NULL;
6475 it->end_charpos = it->string_nchars = SCHARS (string);
6476 it->method = GET_FROM_STRING;
6477 it->current.string_pos = string_pos (charpos, string);
6479 if (it->bidi_p)
6481 it->bidi_it.string.lstring = string;
6482 it->bidi_it.string.s = NULL;
6483 it->bidi_it.string.schars = it->end_charpos;
6484 it->bidi_it.string.bufpos = 0;
6485 it->bidi_it.string.from_disp_str = 0;
6486 it->bidi_it.string.unibyte = !it->multibyte_p;
6487 it->bidi_it.w = it->w;
6488 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6489 FRAME_WINDOW_P (it->f), &it->bidi_it);
6492 else
6494 it->s = (const unsigned char *) s;
6495 it->string = Qnil;
6497 /* Note that we use IT->current.pos, not it->current.string_pos,
6498 for displaying C strings. */
6499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6500 if (it->multibyte_p)
6502 it->current.pos = c_string_pos (charpos, s, 1);
6503 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6505 else
6507 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6508 it->end_charpos = it->string_nchars = strlen (s);
6511 if (it->bidi_p)
6513 it->bidi_it.string.lstring = Qnil;
6514 it->bidi_it.string.s = (const unsigned char *) s;
6515 it->bidi_it.string.schars = it->end_charpos;
6516 it->bidi_it.string.bufpos = 0;
6517 it->bidi_it.string.from_disp_str = 0;
6518 it->bidi_it.string.unibyte = !it->multibyte_p;
6519 it->bidi_it.w = it->w;
6520 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6521 &it->bidi_it);
6523 it->method = GET_FROM_C_STRING;
6526 /* PRECISION > 0 means don't return more than PRECISION characters
6527 from the string. */
6528 if (precision > 0 && it->end_charpos - charpos > precision)
6530 it->end_charpos = it->string_nchars = charpos + precision;
6531 if (it->bidi_p)
6532 it->bidi_it.string.schars = it->end_charpos;
6535 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6536 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6537 FIELD_WIDTH < 0 means infinite field width. This is useful for
6538 padding with `-' at the end of a mode line. */
6539 if (field_width < 0)
6540 field_width = INFINITY;
6541 /* Implementation note: We deliberately don't enlarge
6542 it->bidi_it.string.schars here to fit it->end_charpos, because
6543 the bidi iterator cannot produce characters out of thin air. */
6544 if (field_width > it->end_charpos - charpos)
6545 it->end_charpos = charpos + field_width;
6547 /* Use the standard display table for displaying strings. */
6548 if (DISP_TABLE_P (Vstandard_display_table))
6549 it->dp = XCHAR_TABLE (Vstandard_display_table);
6551 it->stop_charpos = charpos;
6552 it->prev_stop = charpos;
6553 it->base_level_stop = 0;
6554 if (it->bidi_p)
6556 it->bidi_it.first_elt = 1;
6557 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6558 it->bidi_it.disp_pos = -1;
6560 if (s == NULL && it->multibyte_p)
6562 ptrdiff_t endpos = SCHARS (it->string);
6563 if (endpos > it->end_charpos)
6564 endpos = it->end_charpos;
6565 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6566 it->string);
6568 CHECK_IT (it);
6573 /***********************************************************************
6574 Iteration
6575 ***********************************************************************/
6577 /* Map enum it_method value to corresponding next_element_from_* function. */
6579 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6581 next_element_from_buffer,
6582 next_element_from_display_vector,
6583 next_element_from_string,
6584 next_element_from_c_string,
6585 next_element_from_image,
6586 next_element_from_stretch
6589 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6592 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6593 (possibly with the following characters). */
6595 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6596 ((IT)->cmp_it.id >= 0 \
6597 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6598 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6599 END_CHARPOS, (IT)->w, \
6600 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6601 (IT)->string)))
6604 /* Lookup the char-table Vglyphless_char_display for character C (-1
6605 if we want information for no-font case), and return the display
6606 method symbol. By side-effect, update it->what and
6607 it->glyphless_method. This function is called from
6608 get_next_display_element for each character element, and from
6609 x_produce_glyphs when no suitable font was found. */
6611 Lisp_Object
6612 lookup_glyphless_char_display (int c, struct it *it)
6614 Lisp_Object glyphless_method = Qnil;
6616 if (CHAR_TABLE_P (Vglyphless_char_display)
6617 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6619 if (c >= 0)
6621 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6622 if (CONSP (glyphless_method))
6623 glyphless_method = FRAME_WINDOW_P (it->f)
6624 ? XCAR (glyphless_method)
6625 : XCDR (glyphless_method);
6627 else
6628 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6631 retry:
6632 if (NILP (glyphless_method))
6634 if (c >= 0)
6635 /* The default is to display the character by a proper font. */
6636 return Qnil;
6637 /* The default for the no-font case is to display an empty box. */
6638 glyphless_method = Qempty_box;
6640 if (EQ (glyphless_method, Qzero_width))
6642 if (c >= 0)
6643 return glyphless_method;
6644 /* This method can't be used for the no-font case. */
6645 glyphless_method = Qempty_box;
6647 if (EQ (glyphless_method, Qthin_space))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6649 else if (EQ (glyphless_method, Qempty_box))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6651 else if (EQ (glyphless_method, Qhex_code))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6653 else if (STRINGP (glyphless_method))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6655 else
6657 /* Invalid value. We use the default method. */
6658 glyphless_method = Qnil;
6659 goto retry;
6661 it->what = IT_GLYPHLESS;
6662 return glyphless_method;
6665 /* Merge escape glyph face and cache the result. */
6667 static struct frame *last_escape_glyph_frame = NULL;
6668 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6669 static int last_escape_glyph_merged_face_id = 0;
6671 static int
6672 merge_escape_glyph_face (struct it *it)
6674 int face_id;
6676 if (it->f == last_escape_glyph_frame
6677 && it->face_id == last_escape_glyph_face_id)
6678 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the `escape-glyph' face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6683 last_escape_glyph_frame = it->f;
6684 last_escape_glyph_face_id = it->face_id;
6685 last_escape_glyph_merged_face_id = face_id;
6687 return face_id;
6690 /* Likewise for glyphless glyph face. */
6692 static struct frame *last_glyphless_glyph_frame = NULL;
6693 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 static int last_glyphless_glyph_merged_face_id = 0;
6697 merge_glyphless_glyph_face (struct it *it)
6699 int face_id;
6701 if (it->f == last_glyphless_glyph_frame
6702 && it->face_id == last_glyphless_glyph_face_id)
6703 face_id = last_glyphless_glyph_merged_face_id;
6704 else
6706 /* Merge the `glyphless-char' face into the current face. */
6707 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6708 last_glyphless_glyph_frame = it->f;
6709 last_glyphless_glyph_face_id = it->face_id;
6710 last_glyphless_glyph_merged_face_id = face_id;
6712 return face_id;
6715 /* Load IT's display element fields with information about the next
6716 display element from the current position of IT. Value is zero if
6717 end of buffer (or C string) is reached. */
6719 static int
6720 get_next_display_element (struct it *it)
6722 /* Non-zero means that we found a display element. Zero means that
6723 we hit the end of what we iterate over. Performance note: the
6724 function pointer `method' used here turns out to be faster than
6725 using a sequence of if-statements. */
6726 int success_p;
6728 get_next:
6729 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6731 if (it->what == IT_CHARACTER)
6733 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6734 and only if (a) the resolved directionality of that character
6735 is R..." */
6736 /* FIXME: Do we need an exception for characters from display
6737 tables? */
6738 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6739 it->c = bidi_mirror_char (it->c);
6740 /* Map via display table or translate control characters.
6741 IT->c, IT->len etc. have been set to the next character by
6742 the function call above. If we have a display table, and it
6743 contains an entry for IT->c, translate it. Don't do this if
6744 IT->c itself comes from a display table, otherwise we could
6745 end up in an infinite recursion. (An alternative could be to
6746 count the recursion depth of this function and signal an
6747 error when a certain maximum depth is reached.) Is it worth
6748 it? */
6749 if (success_p && it->dpvec == NULL)
6751 Lisp_Object dv;
6752 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6753 int nonascii_space_p = 0;
6754 int nonascii_hyphen_p = 0;
6755 int c = it->c; /* This is the character to display. */
6757 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6759 eassert (SINGLE_BYTE_CHAR_P (c));
6760 if (unibyte_display_via_language_environment)
6762 c = DECODE_CHAR (unibyte, c);
6763 if (c < 0)
6764 c = BYTE8_TO_CHAR (it->c);
6766 else
6767 c = BYTE8_TO_CHAR (it->c);
6770 if (it->dp
6771 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6772 VECTORP (dv)))
6774 struct Lisp_Vector *v = XVECTOR (dv);
6776 /* Return the first character from the display table
6777 entry, if not empty. If empty, don't display the
6778 current character. */
6779 if (v->header.size)
6781 it->dpvec_char_len = it->len;
6782 it->dpvec = v->contents;
6783 it->dpend = v->contents + v->header.size;
6784 it->current.dpvec_index = 0;
6785 it->dpvec_face_id = -1;
6786 it->saved_face_id = it->face_id;
6787 it->method = GET_FROM_DISPLAY_VECTOR;
6788 it->ellipsis_p = 0;
6790 else
6792 set_iterator_to_next (it, 0);
6794 goto get_next;
6797 if (! NILP (lookup_glyphless_char_display (c, it)))
6799 if (it->what == IT_GLYPHLESS)
6800 goto done;
6801 /* Don't display this character. */
6802 set_iterator_to_next (it, 0);
6803 goto get_next;
6806 /* If `nobreak-char-display' is non-nil, we display
6807 non-ASCII spaces and hyphens specially. */
6808 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6810 if (c == 0xA0)
6811 nonascii_space_p = true;
6812 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6813 nonascii_hyphen_p = true;
6816 /* Translate control characters into `\003' or `^C' form.
6817 Control characters coming from a display table entry are
6818 currently not translated because we use IT->dpvec to hold
6819 the translation. This could easily be changed but I
6820 don't believe that it is worth doing.
6822 The characters handled by `nobreak-char-display' must be
6823 translated too.
6825 Non-printable characters and raw-byte characters are also
6826 translated to octal form. */
6827 if (((c < ' ' || c == 127) /* ASCII control chars. */
6828 ? (it->area != TEXT_AREA
6829 /* In mode line, treat \n, \t like other crl chars. */
6830 || (c != '\t'
6831 && it->glyph_row
6832 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6833 || (c != '\n' && c != '\t'))
6834 : (nonascii_space_p
6835 || nonascii_hyphen_p
6836 || CHAR_BYTE8_P (c)
6837 || ! CHAR_PRINTABLE_P (c))))
6839 /* C is a control character, non-ASCII space/hyphen,
6840 raw-byte, or a non-printable character which must be
6841 displayed either as '\003' or as `^C' where the '\\'
6842 and '^' can be defined in the display table. Fill
6843 IT->ctl_chars with glyphs for what we have to
6844 display. Then, set IT->dpvec to these glyphs. */
6845 Lisp_Object gc;
6846 int ctl_len;
6847 int face_id;
6848 int lface_id = 0;
6849 int escape_glyph;
6851 /* Handle control characters with ^. */
6853 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6855 int g;
6857 g = '^'; /* default glyph for Control */
6858 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6859 if (it->dp
6860 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6862 g = GLYPH_CODE_CHAR (gc);
6863 lface_id = GLYPH_CODE_FACE (gc);
6866 face_id = (lface_id
6867 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6868 : merge_escape_glyph_face (it));
6870 XSETINT (it->ctl_chars[0], g);
6871 XSETINT (it->ctl_chars[1], c ^ 0100);
6872 ctl_len = 2;
6873 goto display_control;
6876 /* Handle non-ascii space in the mode where it only gets
6877 highlighting. */
6879 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6881 /* Merge `nobreak-space' into the current face. */
6882 face_id = merge_faces (it->f, Qnobreak_space, 0,
6883 it->face_id);
6884 XSETINT (it->ctl_chars[0], ' ');
6885 ctl_len = 1;
6886 goto display_control;
6889 /* Handle sequences that start with the "escape glyph". */
6891 /* the default escape glyph is \. */
6892 escape_glyph = '\\';
6894 if (it->dp
6895 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6897 escape_glyph = GLYPH_CODE_CHAR (gc);
6898 lface_id = GLYPH_CODE_FACE (gc);
6901 face_id = (lface_id
6902 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6903 : merge_escape_glyph_face (it));
6905 /* Draw non-ASCII hyphen with just highlighting: */
6907 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 XSETINT (it->ctl_chars[0], '-');
6910 ctl_len = 1;
6911 goto display_control;
6914 /* Draw non-ASCII space/hyphen with escape glyph: */
6916 if (nonascii_space_p || nonascii_hyphen_p)
6918 XSETINT (it->ctl_chars[0], escape_glyph);
6919 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6920 ctl_len = 2;
6921 goto display_control;
6925 char str[10];
6926 int len, i;
6928 if (CHAR_BYTE8_P (c))
6929 /* Display \200 instead of \17777600. */
6930 c = CHAR_TO_BYTE8 (c);
6931 len = sprintf (str, "%03o", c);
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 for (i = 0; i < len; i++)
6935 XSETINT (it->ctl_chars[i + 1], str[i]);
6936 ctl_len = len + 1;
6939 display_control:
6940 /* Set up IT->dpvec and return first character from it. */
6941 it->dpvec_char_len = it->len;
6942 it->dpvec = it->ctl_chars;
6943 it->dpend = it->dpvec + ctl_len;
6944 it->current.dpvec_index = 0;
6945 it->dpvec_face_id = face_id;
6946 it->saved_face_id = it->face_id;
6947 it->method = GET_FROM_DISPLAY_VECTOR;
6948 it->ellipsis_p = 0;
6949 goto get_next;
6951 it->char_to_display = c;
6953 else if (success_p)
6955 it->char_to_display = it->c;
6959 #ifdef HAVE_WINDOW_SYSTEM
6960 /* Adjust face id for a multibyte character. There are no multibyte
6961 character in unibyte text. */
6962 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6963 && it->multibyte_p
6964 && success_p
6965 && FRAME_WINDOW_P (it->f))
6967 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6969 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6971 /* Automatic composition with glyph-string. */
6972 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6974 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6976 else
6978 ptrdiff_t pos = (it->s ? -1
6979 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6980 : IT_CHARPOS (*it));
6981 int c;
6983 if (it->what == IT_CHARACTER)
6984 c = it->char_to_display;
6985 else
6987 struct composition *cmp = composition_table[it->cmp_it.id];
6988 int i;
6990 c = ' ';
6991 for (i = 0; i < cmp->glyph_len; i++)
6992 /* TAB in a composition means display glyphs with
6993 padding space on the left or right. */
6994 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6995 break;
6997 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7000 #endif /* HAVE_WINDOW_SYSTEM */
7002 done:
7003 /* Is this character the last one of a run of characters with
7004 box? If yes, set IT->end_of_box_run_p to 1. */
7005 if (it->face_box_p
7006 && it->s == NULL)
7008 if (it->method == GET_FROM_STRING && it->sp)
7010 int face_id = underlying_face_id (it);
7011 struct face *face = FACE_FROM_ID (it->f, face_id);
7013 if (face)
7015 if (face->box == FACE_NO_BOX)
7017 /* If the box comes from face properties in a
7018 display string, check faces in that string. */
7019 int string_face_id = face_after_it_pos (it);
7020 it->end_of_box_run_p
7021 = (FACE_FROM_ID (it->f, string_face_id)->box
7022 == FACE_NO_BOX);
7024 /* Otherwise, the box comes from the underlying face.
7025 If this is the last string character displayed, check
7026 the next buffer location. */
7027 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7028 && (it->current.overlay_string_index
7029 == it->n_overlay_strings - 1))
7031 ptrdiff_t ignore;
7032 int next_face_id;
7033 struct text_pos pos = it->current.pos;
7034 INC_TEXT_POS (pos, it->multibyte_p);
7036 next_face_id = face_at_buffer_position
7037 (it->w, CHARPOS (pos), &ignore,
7038 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7039 -1);
7040 it->end_of_box_run_p
7041 = (FACE_FROM_ID (it->f, next_face_id)->box
7042 == FACE_NO_BOX);
7046 /* next_element_from_display_vector sets this flag according to
7047 faces of the display vector glyphs, see there. */
7048 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7050 int face_id = face_after_it_pos (it);
7051 it->end_of_box_run_p
7052 = (face_id != it->face_id
7053 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7056 /* If we reached the end of the object we've been iterating (e.g., a
7057 display string or an overlay string), and there's something on
7058 IT->stack, proceed with what's on the stack. It doesn't make
7059 sense to return zero if there's unprocessed stuff on the stack,
7060 because otherwise that stuff will never be displayed. */
7061 if (!success_p && it->sp > 0)
7063 set_iterator_to_next (it, 0);
7064 success_p = get_next_display_element (it);
7067 /* Value is 0 if end of buffer or string reached. */
7068 return success_p;
7072 /* Move IT to the next display element.
7074 RESEAT_P non-zero means if called on a newline in buffer text,
7075 skip to the next visible line start.
7077 Functions get_next_display_element and set_iterator_to_next are
7078 separate because I find this arrangement easier to handle than a
7079 get_next_display_element function that also increments IT's
7080 position. The way it is we can first look at an iterator's current
7081 display element, decide whether it fits on a line, and if it does,
7082 increment the iterator position. The other way around we probably
7083 would either need a flag indicating whether the iterator has to be
7084 incremented the next time, or we would have to implement a
7085 decrement position function which would not be easy to write. */
7087 void
7088 set_iterator_to_next (struct it *it, int reseat_p)
7090 /* Reset flags indicating start and end of a sequence of characters
7091 with box. Reset them at the start of this function because
7092 moving the iterator to a new position might set them. */
7093 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7095 switch (it->method)
7097 case GET_FROM_BUFFER:
7098 /* The current display element of IT is a character from
7099 current_buffer. Advance in the buffer, and maybe skip over
7100 invisible lines that are so because of selective display. */
7101 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7102 reseat_at_next_visible_line_start (it, 0);
7103 else if (it->cmp_it.id >= 0)
7105 /* We are currently getting glyphs from a composition. */
7106 int i;
7108 if (! it->bidi_p)
7110 IT_CHARPOS (*it) += it->cmp_it.nchars;
7111 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 it->cmp_it.from = it->cmp_it.to;
7116 else
7118 it->cmp_it.id = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it),
7121 it->end_charpos, Qnil);
7124 else if (! it->cmp_it.reversed_p)
7126 /* Composition created while scanning forward. */
7127 /* Update IT's char/byte positions to point to the first
7128 character of the next grapheme cluster, or to the
7129 character visually after the current composition. */
7130 for (i = 0; i < it->cmp_it.nchars; i++)
7131 bidi_move_to_visually_next (&it->bidi_it);
7132 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7133 IT_CHARPOS (*it) = it->bidi_it.charpos;
7135 if (it->cmp_it.to < it->cmp_it.nglyphs)
7137 /* Proceed to the next grapheme cluster. */
7138 it->cmp_it.from = it->cmp_it.to;
7140 else
7142 /* No more grapheme clusters in this composition.
7143 Find the next stop position. */
7144 ptrdiff_t stop = it->end_charpos;
7145 if (it->bidi_it.scan_dir < 0)
7146 /* Now we are scanning backward and don't know
7147 where to stop. */
7148 stop = -1;
7149 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7150 IT_BYTEPOS (*it), stop, Qnil);
7153 else
7155 /* Composition created while scanning backward. */
7156 /* Update IT's char/byte positions to point to the last
7157 character of the previous grapheme cluster, or the
7158 character visually after the current composition. */
7159 for (i = 0; i < it->cmp_it.nchars; i++)
7160 bidi_move_to_visually_next (&it->bidi_it);
7161 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7162 IT_CHARPOS (*it) = it->bidi_it.charpos;
7163 if (it->cmp_it.from > 0)
7165 /* Proceed to the previous grapheme cluster. */
7166 it->cmp_it.to = it->cmp_it.from;
7168 else
7170 /* No more grapheme clusters in this composition.
7171 Find the next stop position. */
7172 ptrdiff_t stop = it->end_charpos;
7173 if (it->bidi_it.scan_dir < 0)
7174 /* Now we are scanning backward and don't know
7175 where to stop. */
7176 stop = -1;
7177 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7178 IT_BYTEPOS (*it), stop, Qnil);
7182 else
7184 eassert (it->len != 0);
7186 if (!it->bidi_p)
7188 IT_BYTEPOS (*it) += it->len;
7189 IT_CHARPOS (*it) += 1;
7191 else
7193 int prev_scan_dir = it->bidi_it.scan_dir;
7194 /* If this is a new paragraph, determine its base
7195 direction (a.k.a. its base embedding level). */
7196 if (it->bidi_it.new_paragraph)
7197 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7198 bidi_move_to_visually_next (&it->bidi_it);
7199 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7200 IT_CHARPOS (*it) = it->bidi_it.charpos;
7201 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 /* As the scan direction was changed, we must
7204 re-compute the stop position for composition. */
7205 ptrdiff_t stop = it->end_charpos;
7206 if (it->bidi_it.scan_dir < 0)
7207 stop = -1;
7208 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7209 IT_BYTEPOS (*it), stop, Qnil);
7212 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7214 break;
7216 case GET_FROM_C_STRING:
7217 /* Current display element of IT is from a C string. */
7218 if (!it->bidi_p
7219 /* If the string position is beyond string's end, it means
7220 next_element_from_c_string is padding the string with
7221 blanks, in which case we bypass the bidi iterator,
7222 because it cannot deal with such virtual characters. */
7223 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7225 IT_BYTEPOS (*it) += it->len;
7226 IT_CHARPOS (*it) += 1;
7228 else
7230 bidi_move_to_visually_next (&it->bidi_it);
7231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7232 IT_CHARPOS (*it) = it->bidi_it.charpos;
7234 break;
7236 case GET_FROM_DISPLAY_VECTOR:
7237 /* Current display element of IT is from a display table entry.
7238 Advance in the display table definition. Reset it to null if
7239 end reached, and continue with characters from buffers/
7240 strings. */
7241 ++it->current.dpvec_index;
7243 /* Restore face of the iterator to what they were before the
7244 display vector entry (these entries may contain faces). */
7245 it->face_id = it->saved_face_id;
7247 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7249 int recheck_faces = it->ellipsis_p;
7251 if (it->s)
7252 it->method = GET_FROM_C_STRING;
7253 else if (STRINGP (it->string))
7254 it->method = GET_FROM_STRING;
7255 else
7257 it->method = GET_FROM_BUFFER;
7258 it->object = it->w->contents;
7261 it->dpvec = NULL;
7262 it->current.dpvec_index = -1;
7264 /* Skip over characters which were displayed via IT->dpvec. */
7265 if (it->dpvec_char_len < 0)
7266 reseat_at_next_visible_line_start (it, 1);
7267 else if (it->dpvec_char_len > 0)
7269 if (it->method == GET_FROM_STRING
7270 && it->current.overlay_string_index >= 0
7271 && it->n_overlay_strings > 0)
7272 it->ignore_overlay_strings_at_pos_p = true;
7273 it->len = it->dpvec_char_len;
7274 set_iterator_to_next (it, reseat_p);
7277 /* Maybe recheck faces after display vector. */
7278 if (recheck_faces)
7279 it->stop_charpos = IT_CHARPOS (*it);
7281 break;
7283 case GET_FROM_STRING:
7284 /* Current display element is a character from a Lisp string. */
7285 eassert (it->s == NULL && STRINGP (it->string));
7286 /* Don't advance past string end. These conditions are true
7287 when set_iterator_to_next is called at the end of
7288 get_next_display_element, in which case the Lisp string is
7289 already exhausted, and all we want is pop the iterator
7290 stack. */
7291 if (it->current.overlay_string_index >= 0)
7293 /* This is an overlay string, so there's no padding with
7294 spaces, and the number of characters in the string is
7295 where the string ends. */
7296 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7297 goto consider_string_end;
7299 else
7301 /* Not an overlay string. There could be padding, so test
7302 against it->end_charpos. */
7303 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7304 goto consider_string_end;
7306 if (it->cmp_it.id >= 0)
7308 int i;
7310 if (! it->bidi_p)
7312 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7313 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7314 if (it->cmp_it.to < it->cmp_it.nglyphs)
7315 it->cmp_it.from = it->cmp_it.to;
7316 else
7318 it->cmp_it.id = -1;
7319 composition_compute_stop_pos (&it->cmp_it,
7320 IT_STRING_CHARPOS (*it),
7321 IT_STRING_BYTEPOS (*it),
7322 it->end_charpos, it->string);
7325 else if (! it->cmp_it.reversed_p)
7327 for (i = 0; i < it->cmp_it.nchars; i++)
7328 bidi_move_to_visually_next (&it->bidi_it);
7329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7330 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7332 if (it->cmp_it.to < it->cmp_it.nglyphs)
7333 it->cmp_it.from = it->cmp_it.to;
7334 else
7336 ptrdiff_t stop = it->end_charpos;
7337 if (it->bidi_it.scan_dir < 0)
7338 stop = -1;
7339 composition_compute_stop_pos (&it->cmp_it,
7340 IT_STRING_CHARPOS (*it),
7341 IT_STRING_BYTEPOS (*it), stop,
7342 it->string);
7345 else
7347 for (i = 0; i < it->cmp_it.nchars; i++)
7348 bidi_move_to_visually_next (&it->bidi_it);
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7350 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7351 if (it->cmp_it.from > 0)
7352 it->cmp_it.to = it->cmp_it.from;
7353 else
7355 ptrdiff_t stop = it->end_charpos;
7356 if (it->bidi_it.scan_dir < 0)
7357 stop = -1;
7358 composition_compute_stop_pos (&it->cmp_it,
7359 IT_STRING_CHARPOS (*it),
7360 IT_STRING_BYTEPOS (*it), stop,
7361 it->string);
7365 else
7367 if (!it->bidi_p
7368 /* If the string position is beyond string's end, it
7369 means next_element_from_string is padding the string
7370 with blanks, in which case we bypass the bidi
7371 iterator, because it cannot deal with such virtual
7372 characters. */
7373 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7375 IT_STRING_BYTEPOS (*it) += it->len;
7376 IT_STRING_CHARPOS (*it) += 1;
7378 else
7380 int prev_scan_dir = it->bidi_it.scan_dir;
7382 bidi_move_to_visually_next (&it->bidi_it);
7383 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7384 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7385 if (prev_scan_dir != it->bidi_it.scan_dir)
7387 ptrdiff_t stop = it->end_charpos;
7389 if (it->bidi_it.scan_dir < 0)
7390 stop = -1;
7391 composition_compute_stop_pos (&it->cmp_it,
7392 IT_STRING_CHARPOS (*it),
7393 IT_STRING_BYTEPOS (*it), stop,
7394 it->string);
7399 consider_string_end:
7401 if (it->current.overlay_string_index >= 0)
7403 /* IT->string is an overlay string. Advance to the
7404 next, if there is one. */
7405 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 it->ellipsis_p = 0;
7408 next_overlay_string (it);
7409 if (it->ellipsis_p)
7410 setup_for_ellipsis (it, 0);
7413 else
7415 /* IT->string is not an overlay string. If we reached
7416 its end, and there is something on IT->stack, proceed
7417 with what is on the stack. This can be either another
7418 string, this time an overlay string, or a buffer. */
7419 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7420 && it->sp > 0)
7422 pop_it (it);
7423 if (it->method == GET_FROM_STRING)
7424 goto consider_string_end;
7427 break;
7429 case GET_FROM_IMAGE:
7430 case GET_FROM_STRETCH:
7431 /* The position etc with which we have to proceed are on
7432 the stack. The position may be at the end of a string,
7433 if the `display' property takes up the whole string. */
7434 eassert (it->sp > 0);
7435 pop_it (it);
7436 if (it->method == GET_FROM_STRING)
7437 goto consider_string_end;
7438 break;
7440 default:
7441 /* There are no other methods defined, so this should be a bug. */
7442 emacs_abort ();
7445 eassert (it->method != GET_FROM_STRING
7446 || (STRINGP (it->string)
7447 && IT_STRING_CHARPOS (*it) >= 0));
7450 /* Load IT's display element fields with information about the next
7451 display element which comes from a display table entry or from the
7452 result of translating a control character to one of the forms `^C'
7453 or `\003'.
7455 IT->dpvec holds the glyphs to return as characters.
7456 IT->saved_face_id holds the face id before the display vector--it
7457 is restored into IT->face_id in set_iterator_to_next. */
7459 static int
7460 next_element_from_display_vector (struct it *it)
7462 Lisp_Object gc;
7463 int prev_face_id = it->face_id;
7464 int next_face_id;
7466 /* Precondition. */
7467 eassert (it->dpvec && it->current.dpvec_index >= 0);
7469 it->face_id = it->saved_face_id;
7471 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7472 That seemed totally bogus - so I changed it... */
7473 gc = it->dpvec[it->current.dpvec_index];
7475 if (GLYPH_CODE_P (gc))
7477 struct face *this_face, *prev_face, *next_face;
7479 it->c = GLYPH_CODE_CHAR (gc);
7480 it->len = CHAR_BYTES (it->c);
7482 /* The entry may contain a face id to use. Such a face id is
7483 the id of a Lisp face, not a realized face. A face id of
7484 zero means no face is specified. */
7485 if (it->dpvec_face_id >= 0)
7486 it->face_id = it->dpvec_face_id;
7487 else
7489 int lface_id = GLYPH_CODE_FACE (gc);
7490 if (lface_id > 0)
7491 it->face_id = merge_faces (it->f, Qt, lface_id,
7492 it->saved_face_id);
7495 /* Glyphs in the display vector could have the box face, so we
7496 need to set the related flags in the iterator, as
7497 appropriate. */
7498 this_face = FACE_FROM_ID (it->f, it->face_id);
7499 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7501 /* Is this character the first character of a box-face run? */
7502 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7503 && (!prev_face
7504 || prev_face->box == FACE_NO_BOX));
7506 /* For the last character of the box-face run, we need to look
7507 either at the next glyph from the display vector, or at the
7508 face we saw before the display vector. */
7509 next_face_id = it->saved_face_id;
7510 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7512 if (it->dpvec_face_id >= 0)
7513 next_face_id = it->dpvec_face_id;
7514 else
7516 int lface_id =
7517 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7519 if (lface_id > 0)
7520 next_face_id = merge_faces (it->f, Qt, lface_id,
7521 it->saved_face_id);
7524 next_face = FACE_FROM_ID (it->f, next_face_id);
7525 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7526 && (!next_face
7527 || next_face->box == FACE_NO_BOX));
7528 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7530 else
7531 /* Display table entry is invalid. Return a space. */
7532 it->c = ' ', it->len = 1;
7534 /* Don't change position and object of the iterator here. They are
7535 still the values of the character that had this display table
7536 entry or was translated, and that's what we want. */
7537 it->what = IT_CHARACTER;
7538 return 1;
7541 /* Get the first element of string/buffer in the visual order, after
7542 being reseated to a new position in a string or a buffer. */
7543 static void
7544 get_visually_first_element (struct it *it)
7546 int string_p = STRINGP (it->string) || it->s;
7547 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7548 ptrdiff_t bob = (string_p ? 0 : BEGV);
7550 if (STRINGP (it->string))
7552 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7553 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7555 else
7557 it->bidi_it.charpos = IT_CHARPOS (*it);
7558 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7561 if (it->bidi_it.charpos == eob)
7563 /* Nothing to do, but reset the FIRST_ELT flag, like
7564 bidi_paragraph_init does, because we are not going to
7565 call it. */
7566 it->bidi_it.first_elt = 0;
7568 else if (it->bidi_it.charpos == bob
7569 || (!string_p
7570 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7571 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7573 /* If we are at the beginning of a line/string, we can produce
7574 the next element right away. */
7575 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7576 bidi_move_to_visually_next (&it->bidi_it);
7578 else
7580 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7582 /* We need to prime the bidi iterator starting at the line's or
7583 string's beginning, before we will be able to produce the
7584 next element. */
7585 if (string_p)
7586 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7587 else
7588 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7589 IT_BYTEPOS (*it), -1,
7590 &it->bidi_it.bytepos);
7591 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7594 /* Now return to buffer/string position where we were asked
7595 to get the next display element, and produce that. */
7596 bidi_move_to_visually_next (&it->bidi_it);
7598 while (it->bidi_it.bytepos != orig_bytepos
7599 && it->bidi_it.charpos < eob);
7602 /* Adjust IT's position information to where we ended up. */
7603 if (STRINGP (it->string))
7605 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7608 else
7610 IT_CHARPOS (*it) = it->bidi_it.charpos;
7611 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7614 if (STRINGP (it->string) || !it->s)
7616 ptrdiff_t stop, charpos, bytepos;
7618 if (STRINGP (it->string))
7620 eassert (!it->s);
7621 stop = SCHARS (it->string);
7622 if (stop > it->end_charpos)
7623 stop = it->end_charpos;
7624 charpos = IT_STRING_CHARPOS (*it);
7625 bytepos = IT_STRING_BYTEPOS (*it);
7627 else
7629 stop = it->end_charpos;
7630 charpos = IT_CHARPOS (*it);
7631 bytepos = IT_BYTEPOS (*it);
7633 if (it->bidi_it.scan_dir < 0)
7634 stop = -1;
7635 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7636 it->string);
7640 /* Load IT with the next display element from Lisp string IT->string.
7641 IT->current.string_pos is the current position within the string.
7642 If IT->current.overlay_string_index >= 0, the Lisp string is an
7643 overlay string. */
7645 static int
7646 next_element_from_string (struct it *it)
7648 struct text_pos position;
7650 eassert (STRINGP (it->string));
7651 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7652 eassert (IT_STRING_CHARPOS (*it) >= 0);
7653 position = it->current.string_pos;
7655 /* With bidi reordering, the character to display might not be the
7656 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7657 that we were reseat()ed to a new string, whose paragraph
7658 direction is not known. */
7659 if (it->bidi_p && it->bidi_it.first_elt)
7661 get_visually_first_element (it);
7662 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7665 /* Time to check for invisible text? */
7666 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7668 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7670 if (!(!it->bidi_p
7671 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7672 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7674 /* With bidi non-linear iteration, we could find
7675 ourselves far beyond the last computed stop_charpos,
7676 with several other stop positions in between that we
7677 missed. Scan them all now, in buffer's logical
7678 order, until we find and handle the last stop_charpos
7679 that precedes our current position. */
7680 handle_stop_backwards (it, it->stop_charpos);
7681 return GET_NEXT_DISPLAY_ELEMENT (it);
7683 else
7685 if (it->bidi_p)
7687 /* Take note of the stop position we just moved
7688 across, for when we will move back across it. */
7689 it->prev_stop = it->stop_charpos;
7690 /* If we are at base paragraph embedding level, take
7691 note of the last stop position seen at this
7692 level. */
7693 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7694 it->base_level_stop = it->stop_charpos;
7696 handle_stop (it);
7698 /* Since a handler may have changed IT->method, we must
7699 recurse here. */
7700 return GET_NEXT_DISPLAY_ELEMENT (it);
7703 else if (it->bidi_p
7704 /* If we are before prev_stop, we may have overstepped
7705 on our way backwards a stop_pos, and if so, we need
7706 to handle that stop_pos. */
7707 && IT_STRING_CHARPOS (*it) < it->prev_stop
7708 /* We can sometimes back up for reasons that have nothing
7709 to do with bidi reordering. E.g., compositions. The
7710 code below is only needed when we are above the base
7711 embedding level, so test for that explicitly. */
7712 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7714 /* If we lost track of base_level_stop, we have no better
7715 place for handle_stop_backwards to start from than string
7716 beginning. This happens, e.g., when we were reseated to
7717 the previous screenful of text by vertical-motion. */
7718 if (it->base_level_stop <= 0
7719 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7720 it->base_level_stop = 0;
7721 handle_stop_backwards (it, it->base_level_stop);
7722 return GET_NEXT_DISPLAY_ELEMENT (it);
7726 if (it->current.overlay_string_index >= 0)
7728 /* Get the next character from an overlay string. In overlay
7729 strings, there is no field width or padding with spaces to
7730 do. */
7731 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7733 it->what = IT_EOB;
7734 return 0;
7736 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7737 IT_STRING_BYTEPOS (*it),
7738 it->bidi_it.scan_dir < 0
7739 ? -1
7740 : SCHARS (it->string))
7741 && next_element_from_composition (it))
7743 return 1;
7745 else if (STRING_MULTIBYTE (it->string))
7747 const unsigned char *s = (SDATA (it->string)
7748 + IT_STRING_BYTEPOS (*it));
7749 it->c = string_char_and_length (s, &it->len);
7751 else
7753 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7754 it->len = 1;
7757 else
7759 /* Get the next character from a Lisp string that is not an
7760 overlay string. Such strings come from the mode line, for
7761 example. We may have to pad with spaces, or truncate the
7762 string. See also next_element_from_c_string. */
7763 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7765 it->what = IT_EOB;
7766 return 0;
7768 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7770 /* Pad with spaces. */
7771 it->c = ' ', it->len = 1;
7772 CHARPOS (position) = BYTEPOS (position) = -1;
7774 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7775 IT_STRING_BYTEPOS (*it),
7776 it->bidi_it.scan_dir < 0
7777 ? -1
7778 : it->string_nchars)
7779 && next_element_from_composition (it))
7781 return 1;
7783 else if (STRING_MULTIBYTE (it->string))
7785 const unsigned char *s = (SDATA (it->string)
7786 + IT_STRING_BYTEPOS (*it));
7787 it->c = string_char_and_length (s, &it->len);
7789 else
7791 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7792 it->len = 1;
7796 /* Record what we have and where it came from. */
7797 it->what = IT_CHARACTER;
7798 it->object = it->string;
7799 it->position = position;
7800 return 1;
7804 /* Load IT with next display element from C string IT->s.
7805 IT->string_nchars is the maximum number of characters to return
7806 from the string. IT->end_charpos may be greater than
7807 IT->string_nchars when this function is called, in which case we
7808 may have to return padding spaces. Value is zero if end of string
7809 reached, including padding spaces. */
7811 static int
7812 next_element_from_c_string (struct it *it)
7814 bool success_p = true;
7816 eassert (it->s);
7817 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7818 it->what = IT_CHARACTER;
7819 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7820 it->object = Qnil;
7822 /* With bidi reordering, the character to display might not be the
7823 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7824 we were reseated to a new string, whose paragraph direction is
7825 not known. */
7826 if (it->bidi_p && it->bidi_it.first_elt)
7827 get_visually_first_element (it);
7829 /* IT's position can be greater than IT->string_nchars in case a
7830 field width or precision has been specified when the iterator was
7831 initialized. */
7832 if (IT_CHARPOS (*it) >= it->end_charpos)
7834 /* End of the game. */
7835 it->what = IT_EOB;
7836 success_p = 0;
7838 else if (IT_CHARPOS (*it) >= it->string_nchars)
7840 /* Pad with spaces. */
7841 it->c = ' ', it->len = 1;
7842 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7844 else if (it->multibyte_p)
7845 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7846 else
7847 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7849 return success_p;
7853 /* Set up IT to return characters from an ellipsis, if appropriate.
7854 The definition of the ellipsis glyphs may come from a display table
7855 entry. This function fills IT with the first glyph from the
7856 ellipsis if an ellipsis is to be displayed. */
7858 static int
7859 next_element_from_ellipsis (struct it *it)
7861 if (it->selective_display_ellipsis_p)
7862 setup_for_ellipsis (it, it->len);
7863 else
7865 /* The face at the current position may be different from the
7866 face we find after the invisible text. Remember what it
7867 was in IT->saved_face_id, and signal that it's there by
7868 setting face_before_selective_p. */
7869 it->saved_face_id = it->face_id;
7870 it->method = GET_FROM_BUFFER;
7871 it->object = it->w->contents;
7872 reseat_at_next_visible_line_start (it, 1);
7873 it->face_before_selective_p = true;
7876 return GET_NEXT_DISPLAY_ELEMENT (it);
7880 /* Deliver an image display element. The iterator IT is already
7881 filled with image information (done in handle_display_prop). Value
7882 is always 1. */
7885 static int
7886 next_element_from_image (struct it *it)
7888 it->what = IT_IMAGE;
7889 it->ignore_overlay_strings_at_pos_p = 0;
7890 return 1;
7894 /* Fill iterator IT with next display element from a stretch glyph
7895 property. IT->object is the value of the text property. Value is
7896 always 1. */
7898 static int
7899 next_element_from_stretch (struct it *it)
7901 it->what = IT_STRETCH;
7902 return 1;
7905 /* Scan backwards from IT's current position until we find a stop
7906 position, or until BEGV. This is called when we find ourself
7907 before both the last known prev_stop and base_level_stop while
7908 reordering bidirectional text. */
7910 static void
7911 compute_stop_pos_backwards (struct it *it)
7913 const int SCAN_BACK_LIMIT = 1000;
7914 struct text_pos pos;
7915 struct display_pos save_current = it->current;
7916 struct text_pos save_position = it->position;
7917 ptrdiff_t charpos = IT_CHARPOS (*it);
7918 ptrdiff_t where_we_are = charpos;
7919 ptrdiff_t save_stop_pos = it->stop_charpos;
7920 ptrdiff_t save_end_pos = it->end_charpos;
7922 eassert (NILP (it->string) && !it->s);
7923 eassert (it->bidi_p);
7924 it->bidi_p = 0;
7927 it->end_charpos = min (charpos + 1, ZV);
7928 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7929 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7930 reseat_1 (it, pos, 0);
7931 compute_stop_pos (it);
7932 /* We must advance forward, right? */
7933 if (it->stop_charpos <= charpos)
7934 emacs_abort ();
7936 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7938 if (it->stop_charpos <= where_we_are)
7939 it->prev_stop = it->stop_charpos;
7940 else
7941 it->prev_stop = BEGV;
7942 it->bidi_p = true;
7943 it->current = save_current;
7944 it->position = save_position;
7945 it->stop_charpos = save_stop_pos;
7946 it->end_charpos = save_end_pos;
7949 /* Scan forward from CHARPOS in the current buffer/string, until we
7950 find a stop position > current IT's position. Then handle the stop
7951 position before that. This is called when we bump into a stop
7952 position while reordering bidirectional text. CHARPOS should be
7953 the last previously processed stop_pos (or BEGV/0, if none were
7954 processed yet) whose position is less that IT's current
7955 position. */
7957 static void
7958 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7960 int bufp = !STRINGP (it->string);
7961 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7962 struct display_pos save_current = it->current;
7963 struct text_pos save_position = it->position;
7964 struct text_pos pos1;
7965 ptrdiff_t next_stop;
7967 /* Scan in strict logical order. */
7968 eassert (it->bidi_p);
7969 it->bidi_p = 0;
7972 it->prev_stop = charpos;
7973 if (bufp)
7975 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7976 reseat_1 (it, pos1, 0);
7978 else
7979 it->current.string_pos = string_pos (charpos, it->string);
7980 compute_stop_pos (it);
7981 /* We must advance forward, right? */
7982 if (it->stop_charpos <= it->prev_stop)
7983 emacs_abort ();
7984 charpos = it->stop_charpos;
7986 while (charpos <= where_we_are);
7988 it->bidi_p = true;
7989 it->current = save_current;
7990 it->position = save_position;
7991 next_stop = it->stop_charpos;
7992 it->stop_charpos = it->prev_stop;
7993 handle_stop (it);
7994 it->stop_charpos = next_stop;
7997 /* Load IT with the next display element from current_buffer. Value
7998 is zero if end of buffer reached. IT->stop_charpos is the next
7999 position at which to stop and check for text properties or buffer
8000 end. */
8002 static int
8003 next_element_from_buffer (struct it *it)
8005 bool success_p = true;
8007 eassert (IT_CHARPOS (*it) >= BEGV);
8008 eassert (NILP (it->string) && !it->s);
8009 eassert (!it->bidi_p
8010 || (EQ (it->bidi_it.string.lstring, Qnil)
8011 && it->bidi_it.string.s == NULL));
8013 /* With bidi reordering, the character to display might not be the
8014 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8015 we were reseat()ed to a new buffer position, which is potentially
8016 a different paragraph. */
8017 if (it->bidi_p && it->bidi_it.first_elt)
8019 get_visually_first_element (it);
8020 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8023 if (IT_CHARPOS (*it) >= it->stop_charpos)
8025 if (IT_CHARPOS (*it) >= it->end_charpos)
8027 int overlay_strings_follow_p;
8029 /* End of the game, except when overlay strings follow that
8030 haven't been returned yet. */
8031 if (it->overlay_strings_at_end_processed_p)
8032 overlay_strings_follow_p = 0;
8033 else
8035 it->overlay_strings_at_end_processed_p = true;
8036 overlay_strings_follow_p = get_overlay_strings (it, 0);
8039 if (overlay_strings_follow_p)
8040 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8041 else
8043 it->what = IT_EOB;
8044 it->position = it->current.pos;
8045 success_p = 0;
8048 else if (!(!it->bidi_p
8049 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8050 || IT_CHARPOS (*it) == it->stop_charpos))
8052 /* With bidi non-linear iteration, we could find ourselves
8053 far beyond the last computed stop_charpos, with several
8054 other stop positions in between that we missed. Scan
8055 them all now, in buffer's logical order, until we find
8056 and handle the last stop_charpos that precedes our
8057 current position. */
8058 handle_stop_backwards (it, it->stop_charpos);
8059 return GET_NEXT_DISPLAY_ELEMENT (it);
8061 else
8063 if (it->bidi_p)
8065 /* Take note of the stop position we just moved across,
8066 for when we will move back across it. */
8067 it->prev_stop = it->stop_charpos;
8068 /* If we are at base paragraph embedding level, take
8069 note of the last stop position seen at this
8070 level. */
8071 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8072 it->base_level_stop = it->stop_charpos;
8074 handle_stop (it);
8075 return GET_NEXT_DISPLAY_ELEMENT (it);
8078 else if (it->bidi_p
8079 /* If we are before prev_stop, we may have overstepped on
8080 our way backwards a stop_pos, and if so, we need to
8081 handle that stop_pos. */
8082 && IT_CHARPOS (*it) < it->prev_stop
8083 /* We can sometimes back up for reasons that have nothing
8084 to do with bidi reordering. E.g., compositions. The
8085 code below is only needed when we are above the base
8086 embedding level, so test for that explicitly. */
8087 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8089 if (it->base_level_stop <= 0
8090 || IT_CHARPOS (*it) < it->base_level_stop)
8092 /* If we lost track of base_level_stop, we need to find
8093 prev_stop by looking backwards. This happens, e.g., when
8094 we were reseated to the previous screenful of text by
8095 vertical-motion. */
8096 it->base_level_stop = BEGV;
8097 compute_stop_pos_backwards (it);
8098 handle_stop_backwards (it, it->prev_stop);
8100 else
8101 handle_stop_backwards (it, it->base_level_stop);
8102 return GET_NEXT_DISPLAY_ELEMENT (it);
8104 else
8106 /* No face changes, overlays etc. in sight, so just return a
8107 character from current_buffer. */
8108 unsigned char *p;
8109 ptrdiff_t stop;
8111 /* Maybe run the redisplay end trigger hook. Performance note:
8112 This doesn't seem to cost measurable time. */
8113 if (it->redisplay_end_trigger_charpos
8114 && it->glyph_row
8115 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8116 run_redisplay_end_trigger_hook (it);
8118 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8119 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8120 stop)
8121 && next_element_from_composition (it))
8123 return 1;
8126 /* Get the next character, maybe multibyte. */
8127 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8128 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8129 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8130 else
8131 it->c = *p, it->len = 1;
8133 /* Record what we have and where it came from. */
8134 it->what = IT_CHARACTER;
8135 it->object = it->w->contents;
8136 it->position = it->current.pos;
8138 /* Normally we return the character found above, except when we
8139 really want to return an ellipsis for selective display. */
8140 if (it->selective)
8142 if (it->c == '\n')
8144 /* A value of selective > 0 means hide lines indented more
8145 than that number of columns. */
8146 if (it->selective > 0
8147 && IT_CHARPOS (*it) + 1 < ZV
8148 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8149 IT_BYTEPOS (*it) + 1,
8150 it->selective))
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8156 else if (it->c == '\r' && it->selective == -1)
8158 /* A value of selective == -1 means that everything from the
8159 CR to the end of the line is invisible, with maybe an
8160 ellipsis displayed for it. */
8161 success_p = next_element_from_ellipsis (it);
8162 it->dpvec_char_len = -1;
8167 /* Value is zero if end of buffer reached. */
8168 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8169 return success_p;
8173 /* Run the redisplay end trigger hook for IT. */
8175 static void
8176 run_redisplay_end_trigger_hook (struct it *it)
8178 Lisp_Object args[3];
8180 /* IT->glyph_row should be non-null, i.e. we should be actually
8181 displaying something, or otherwise we should not run the hook. */
8182 eassert (it->glyph_row);
8184 /* Set up hook arguments. */
8185 args[0] = Qredisplay_end_trigger_functions;
8186 args[1] = it->window;
8187 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8188 it->redisplay_end_trigger_charpos = 0;
8190 /* Since we are *trying* to run these functions, don't try to run
8191 them again, even if they get an error. */
8192 wset_redisplay_end_trigger (it->w, Qnil);
8193 Frun_hook_with_args (3, args);
8195 /* Notice if it changed the face of the character we are on. */
8196 handle_face_prop (it);
8200 /* Deliver a composition display element. Unlike the other
8201 next_element_from_XXX, this function is not registered in the array
8202 get_next_element[]. It is called from next_element_from_buffer and
8203 next_element_from_string when necessary. */
8205 static int
8206 next_element_from_composition (struct it *it)
8208 it->what = IT_COMPOSITION;
8209 it->len = it->cmp_it.nbytes;
8210 if (STRINGP (it->string))
8212 if (it->c < 0)
8214 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8215 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8216 return 0;
8218 it->position = it->current.string_pos;
8219 it->object = it->string;
8220 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8221 IT_STRING_BYTEPOS (*it), it->string);
8223 else
8225 if (it->c < 0)
8227 IT_CHARPOS (*it) += it->cmp_it.nchars;
8228 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8229 if (it->bidi_p)
8231 if (it->bidi_it.new_paragraph)
8232 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8233 /* Resync the bidi iterator with IT's new position.
8234 FIXME: this doesn't support bidirectional text. */
8235 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8236 bidi_move_to_visually_next (&it->bidi_it);
8238 return 0;
8240 it->position = it->current.pos;
8241 it->object = it->w->contents;
8242 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8243 IT_BYTEPOS (*it), Qnil);
8245 return 1;
8250 /***********************************************************************
8251 Moving an iterator without producing glyphs
8252 ***********************************************************************/
8254 /* Check if iterator is at a position corresponding to a valid buffer
8255 position after some move_it_ call. */
8257 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8258 ((it)->method == GET_FROM_STRING \
8259 ? IT_STRING_CHARPOS (*it) == 0 \
8260 : 1)
8263 /* Move iterator IT to a specified buffer or X position within one
8264 line on the display without producing glyphs.
8266 OP should be a bit mask including some or all of these bits:
8267 MOVE_TO_X: Stop upon reaching x-position TO_X.
8268 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8269 Regardless of OP's value, stop upon reaching the end of the display line.
8271 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8272 This means, in particular, that TO_X includes window's horizontal
8273 scroll amount.
8275 The return value has several possible values that
8276 say what condition caused the scan to stop:
8278 MOVE_POS_MATCH_OR_ZV
8279 - when TO_POS or ZV was reached.
8281 MOVE_X_REACHED
8282 -when TO_X was reached before TO_POS or ZV were reached.
8284 MOVE_LINE_CONTINUED
8285 - when we reached the end of the display area and the line must
8286 be continued.
8288 MOVE_LINE_TRUNCATED
8289 - when we reached the end of the display area and the line is
8290 truncated.
8292 MOVE_NEWLINE_OR_CR
8293 - when we stopped at a line end, i.e. a newline or a CR and selective
8294 display is on. */
8296 static enum move_it_result
8297 move_it_in_display_line_to (struct it *it,
8298 ptrdiff_t to_charpos, int to_x,
8299 enum move_operation_enum op)
8301 enum move_it_result result = MOVE_UNDEFINED;
8302 struct glyph_row *saved_glyph_row;
8303 struct it wrap_it, atpos_it, atx_it, ppos_it;
8304 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8305 void *ppos_data = NULL;
8306 int may_wrap = 0;
8307 enum it_method prev_method = it->method;
8308 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8309 int saw_smaller_pos = prev_pos < to_charpos;
8311 /* Don't produce glyphs in produce_glyphs. */
8312 saved_glyph_row = it->glyph_row;
8313 it->glyph_row = NULL;
8315 /* Use wrap_it to save a copy of IT wherever a word wrap could
8316 occur. Use atpos_it to save a copy of IT at the desired buffer
8317 position, if found, so that we can scan ahead and check if the
8318 word later overshoots the window edge. Use atx_it similarly, for
8319 pixel positions. */
8320 wrap_it.sp = -1;
8321 atpos_it.sp = -1;
8322 atx_it.sp = -1;
8324 /* Use ppos_it under bidi reordering to save a copy of IT for the
8325 position > CHARPOS that is the closest to CHARPOS. We restore
8326 that position in IT when we have scanned the entire display line
8327 without finding a match for CHARPOS and all the character
8328 positions are greater than CHARPOS. */
8329 if (it->bidi_p)
8331 SAVE_IT (ppos_it, *it, ppos_data);
8332 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8333 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8334 SAVE_IT (ppos_it, *it, ppos_data);
8337 #define BUFFER_POS_REACHED_P() \
8338 ((op & MOVE_TO_POS) != 0 \
8339 && BUFFERP (it->object) \
8340 && (IT_CHARPOS (*it) == to_charpos \
8341 || ((!it->bidi_p \
8342 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8343 && IT_CHARPOS (*it) > to_charpos) \
8344 || (it->what == IT_COMPOSITION \
8345 && ((IT_CHARPOS (*it) > to_charpos \
8346 && to_charpos >= it->cmp_it.charpos) \
8347 || (IT_CHARPOS (*it) < to_charpos \
8348 && to_charpos <= it->cmp_it.charpos)))) \
8349 && (it->method == GET_FROM_BUFFER \
8350 || (it->method == GET_FROM_DISPLAY_VECTOR \
8351 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8353 /* If there's a line-/wrap-prefix, handle it. */
8354 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8355 && it->current_y < it->last_visible_y)
8356 handle_line_prefix (it);
8358 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8359 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8361 while (1)
8363 int x, i, ascent = 0, descent = 0;
8365 /* Utility macro to reset an iterator with x, ascent, and descent. */
8366 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8367 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8368 (IT)->max_descent = descent)
8370 /* Stop if we move beyond TO_CHARPOS (after an image or a
8371 display string or stretch glyph). */
8372 if ((op & MOVE_TO_POS) != 0
8373 && BUFFERP (it->object)
8374 && it->method == GET_FROM_BUFFER
8375 && (((!it->bidi_p
8376 /* When the iterator is at base embedding level, we
8377 are guaranteed that characters are delivered for
8378 display in strictly increasing order of their
8379 buffer positions. */
8380 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8381 && IT_CHARPOS (*it) > to_charpos)
8382 || (it->bidi_p
8383 && (prev_method == GET_FROM_IMAGE
8384 || prev_method == GET_FROM_STRETCH
8385 || prev_method == GET_FROM_STRING)
8386 /* Passed TO_CHARPOS from left to right. */
8387 && ((prev_pos < to_charpos
8388 && IT_CHARPOS (*it) > to_charpos)
8389 /* Passed TO_CHARPOS from right to left. */
8390 || (prev_pos > to_charpos
8391 && IT_CHARPOS (*it) < to_charpos)))))
8393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8395 result = MOVE_POS_MATCH_OR_ZV;
8396 break;
8398 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8399 /* If wrap_it is valid, the current position might be in a
8400 word that is wrapped. So, save the iterator in
8401 atpos_it and continue to see if wrapping happens. */
8402 SAVE_IT (atpos_it, *it, atpos_data);
8405 /* Stop when ZV reached.
8406 We used to stop here when TO_CHARPOS reached as well, but that is
8407 too soon if this glyph does not fit on this line. So we handle it
8408 explicitly below. */
8409 if (!get_next_display_element (it))
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 break;
8415 if (it->line_wrap == TRUNCATE)
8417 if (BUFFER_POS_REACHED_P ())
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8423 else
8425 if (it->line_wrap == WORD_WRAP)
8427 if (IT_DISPLAYING_WHITESPACE (it))
8428 may_wrap = 1;
8429 else if (may_wrap)
8431 /* We have reached a glyph that follows one or more
8432 whitespace characters. If the position is
8433 already found, we are done. */
8434 if (atpos_it.sp >= 0)
8436 RESTORE_IT (it, &atpos_it, atpos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8438 goto done;
8440 if (atx_it.sp >= 0)
8442 RESTORE_IT (it, &atx_it, atx_data);
8443 result = MOVE_X_REACHED;
8444 goto done;
8446 /* Otherwise, we can wrap here. */
8447 SAVE_IT (wrap_it, *it, wrap_data);
8448 may_wrap = 0;
8453 /* Remember the line height for the current line, in case
8454 the next element doesn't fit on the line. */
8455 ascent = it->max_ascent;
8456 descent = it->max_descent;
8458 /* The call to produce_glyphs will get the metrics of the
8459 display element IT is loaded with. Record the x-position
8460 before this display element, in case it doesn't fit on the
8461 line. */
8462 x = it->current_x;
8464 PRODUCE_GLYPHS (it);
8466 if (it->area != TEXT_AREA)
8468 prev_method = it->method;
8469 if (it->method == GET_FROM_BUFFER)
8470 prev_pos = IT_CHARPOS (*it);
8471 set_iterator_to_next (it, 1);
8472 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8473 SET_TEXT_POS (this_line_min_pos,
8474 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8475 if (it->bidi_p
8476 && (op & MOVE_TO_POS)
8477 && IT_CHARPOS (*it) > to_charpos
8478 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8479 SAVE_IT (ppos_it, *it, ppos_data);
8480 continue;
8483 /* The number of glyphs we get back in IT->nglyphs will normally
8484 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8485 character on a terminal frame, or (iii) a line end. For the
8486 second case, IT->nglyphs - 1 padding glyphs will be present.
8487 (On X frames, there is only one glyph produced for a
8488 composite character.)
8490 The behavior implemented below means, for continuation lines,
8491 that as many spaces of a TAB as fit on the current line are
8492 displayed there. For terminal frames, as many glyphs of a
8493 multi-glyph character are displayed in the current line, too.
8494 This is what the old redisplay code did, and we keep it that
8495 way. Under X, the whole shape of a complex character must
8496 fit on the line or it will be completely displayed in the
8497 next line.
8499 Note that both for tabs and padding glyphs, all glyphs have
8500 the same width. */
8501 if (it->nglyphs)
8503 /* More than one glyph or glyph doesn't fit on line. All
8504 glyphs have the same width. */
8505 int single_glyph_width = it->pixel_width / it->nglyphs;
8506 int new_x;
8507 int x_before_this_char = x;
8508 int hpos_before_this_char = it->hpos;
8510 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8512 new_x = x + single_glyph_width;
8514 /* We want to leave anything reaching TO_X to the caller. */
8515 if ((op & MOVE_TO_X) && new_x > to_x)
8517 if (BUFFER_POS_REACHED_P ())
8519 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8520 goto buffer_pos_reached;
8521 if (atpos_it.sp < 0)
8523 SAVE_IT (atpos_it, *it, atpos_data);
8524 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8527 else
8529 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8531 it->current_x = x;
8532 result = MOVE_X_REACHED;
8533 break;
8535 if (atx_it.sp < 0)
8537 SAVE_IT (atx_it, *it, atx_data);
8538 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8543 if (/* Lines are continued. */
8544 it->line_wrap != TRUNCATE
8545 && (/* And glyph doesn't fit on the line. */
8546 new_x > it->last_visible_x
8547 /* Or it fits exactly and we're on a window
8548 system frame. */
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)
8551 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8552 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8553 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8555 if (/* IT->hpos == 0 means the very first glyph
8556 doesn't fit on the line, e.g. a wide image. */
8557 it->hpos == 0
8558 || (new_x == it->last_visible_x
8559 && FRAME_WINDOW_P (it->f)))
8561 ++it->hpos;
8562 it->current_x = new_x;
8564 /* The character's last glyph just barely fits
8565 in this row. */
8566 if (i == it->nglyphs - 1)
8568 /* If this is the destination position,
8569 return a position *before* it in this row,
8570 now that we know it fits in this row. */
8571 if (BUFFER_POS_REACHED_P ())
8573 if (it->line_wrap != WORD_WRAP
8574 || wrap_it.sp < 0)
8576 it->hpos = hpos_before_this_char;
8577 it->current_x = x_before_this_char;
8578 result = MOVE_POS_MATCH_OR_ZV;
8579 break;
8581 if (it->line_wrap == WORD_WRAP
8582 && atpos_it.sp < 0)
8584 SAVE_IT (atpos_it, *it, atpos_data);
8585 atpos_it.current_x = x_before_this_char;
8586 atpos_it.hpos = hpos_before_this_char;
8590 prev_method = it->method;
8591 if (it->method == GET_FROM_BUFFER)
8592 prev_pos = IT_CHARPOS (*it);
8593 set_iterator_to_next (it, 1);
8594 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8595 SET_TEXT_POS (this_line_min_pos,
8596 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8597 /* On graphical terminals, newlines may
8598 "overflow" into the fringe if
8599 overflow-newline-into-fringe is non-nil.
8600 On text terminals, and on graphical
8601 terminals with no right margin, newlines
8602 may overflow into the last glyph on the
8603 display line.*/
8604 if (!FRAME_WINDOW_P (it->f)
8605 || ((it->bidi_p
8606 && it->bidi_it.paragraph_dir == R2L)
8607 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8608 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8609 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8611 if (!get_next_display_element (it))
8613 result = MOVE_POS_MATCH_OR_ZV;
8614 break;
8616 if (BUFFER_POS_REACHED_P ())
8618 if (ITERATOR_AT_END_OF_LINE_P (it))
8619 result = MOVE_POS_MATCH_OR_ZV;
8620 else
8621 result = MOVE_LINE_CONTINUED;
8622 break;
8624 if (ITERATOR_AT_END_OF_LINE_P (it)
8625 && (it->line_wrap != WORD_WRAP
8626 || wrap_it.sp < 0))
8628 result = MOVE_NEWLINE_OR_CR;
8629 break;
8634 else
8635 IT_RESET_X_ASCENT_DESCENT (it);
8637 if (wrap_it.sp >= 0)
8639 RESTORE_IT (it, &wrap_it, wrap_data);
8640 atpos_it.sp = -1;
8641 atx_it.sp = -1;
8644 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8645 IT_CHARPOS (*it)));
8646 result = MOVE_LINE_CONTINUED;
8647 break;
8650 if (BUFFER_POS_REACHED_P ())
8652 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8653 goto buffer_pos_reached;
8654 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8656 SAVE_IT (atpos_it, *it, atpos_data);
8657 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8661 if (new_x > it->first_visible_x)
8663 /* Glyph is visible. Increment number of glyphs that
8664 would be displayed. */
8665 ++it->hpos;
8669 if (result != MOVE_UNDEFINED)
8670 break;
8672 else if (BUFFER_POS_REACHED_P ())
8674 buffer_pos_reached:
8675 IT_RESET_X_ASCENT_DESCENT (it);
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8679 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8681 /* Stop when TO_X specified and reached. This check is
8682 necessary here because of lines consisting of a line end,
8683 only. The line end will not produce any glyphs and we
8684 would never get MOVE_X_REACHED. */
8685 eassert (it->nglyphs == 0);
8686 result = MOVE_X_REACHED;
8687 break;
8690 /* Is this a line end? If yes, we're done. */
8691 if (ITERATOR_AT_END_OF_LINE_P (it))
8693 /* If we are past TO_CHARPOS, but never saw any character
8694 positions smaller than TO_CHARPOS, return
8695 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8696 did. */
8697 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8699 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8701 if (IT_CHARPOS (ppos_it) < ZV)
8703 RESTORE_IT (it, &ppos_it, ppos_data);
8704 result = MOVE_POS_MATCH_OR_ZV;
8706 else
8707 goto buffer_pos_reached;
8709 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8710 && IT_CHARPOS (*it) > to_charpos)
8711 goto buffer_pos_reached;
8712 else
8713 result = MOVE_NEWLINE_OR_CR;
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8717 break;
8720 prev_method = it->method;
8721 if (it->method == GET_FROM_BUFFER)
8722 prev_pos = IT_CHARPOS (*it);
8723 /* The current display element has been consumed. Advance
8724 to the next. */
8725 set_iterator_to_next (it, 1);
8726 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8727 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8728 if (IT_CHARPOS (*it) < to_charpos)
8729 saw_smaller_pos = 1;
8730 if (it->bidi_p
8731 && (op & MOVE_TO_POS)
8732 && IT_CHARPOS (*it) >= to_charpos
8733 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8734 SAVE_IT (ppos_it, *it, ppos_data);
8736 /* Stop if lines are truncated and IT's current x-position is
8737 past the right edge of the window now. */
8738 if (it->line_wrap == TRUNCATE
8739 && it->current_x >= it->last_visible_x)
8741 if (!FRAME_WINDOW_P (it->f)
8742 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8743 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8744 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8745 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8747 int at_eob_p = 0;
8749 if ((at_eob_p = !get_next_display_element (it))
8750 || BUFFER_POS_REACHED_P ()
8751 /* If we are past TO_CHARPOS, but never saw any
8752 character positions smaller than TO_CHARPOS,
8753 return MOVE_POS_MATCH_OR_ZV, like the
8754 unidirectional display did. */
8755 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8756 && !saw_smaller_pos
8757 && IT_CHARPOS (*it) > to_charpos))
8759 if (it->bidi_p
8760 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8761 RESTORE_IT (it, &ppos_it, ppos_data);
8762 result = MOVE_POS_MATCH_OR_ZV;
8763 break;
8765 if (ITERATOR_AT_END_OF_LINE_P (it))
8767 result = MOVE_NEWLINE_OR_CR;
8768 break;
8771 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8772 && !saw_smaller_pos
8773 && IT_CHARPOS (*it) > to_charpos)
8775 if (IT_CHARPOS (ppos_it) < ZV)
8776 RESTORE_IT (it, &ppos_it, ppos_data);
8777 result = MOVE_POS_MATCH_OR_ZV;
8778 break;
8780 result = MOVE_LINE_TRUNCATED;
8781 break;
8783 #undef IT_RESET_X_ASCENT_DESCENT
8786 #undef BUFFER_POS_REACHED_P
8788 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8789 restore the saved iterator. */
8790 if (atpos_it.sp >= 0)
8791 RESTORE_IT (it, &atpos_it, atpos_data);
8792 else if (atx_it.sp >= 0)
8793 RESTORE_IT (it, &atx_it, atx_data);
8795 done:
8797 if (atpos_data)
8798 bidi_unshelve_cache (atpos_data, 1);
8799 if (atx_data)
8800 bidi_unshelve_cache (atx_data, 1);
8801 if (wrap_data)
8802 bidi_unshelve_cache (wrap_data, 1);
8803 if (ppos_data)
8804 bidi_unshelve_cache (ppos_data, 1);
8806 /* Restore the iterator settings altered at the beginning of this
8807 function. */
8808 it->glyph_row = saved_glyph_row;
8809 return result;
8812 /* For external use. */
8813 void
8814 move_it_in_display_line (struct it *it,
8815 ptrdiff_t to_charpos, int to_x,
8816 enum move_operation_enum op)
8818 if (it->line_wrap == WORD_WRAP
8819 && (op & MOVE_TO_X))
8821 struct it save_it;
8822 void *save_data = NULL;
8823 int skip;
8825 SAVE_IT (save_it, *it, save_data);
8826 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8827 /* When word-wrap is on, TO_X may lie past the end
8828 of a wrapped line. Then it->current is the
8829 character on the next line, so backtrack to the
8830 space before the wrap point. */
8831 if (skip == MOVE_LINE_CONTINUED)
8833 int prev_x = max (it->current_x - 1, 0);
8834 RESTORE_IT (it, &save_it, save_data);
8835 move_it_in_display_line_to
8836 (it, -1, prev_x, MOVE_TO_X);
8838 else
8839 bidi_unshelve_cache (save_data, 1);
8841 else
8842 move_it_in_display_line_to (it, to_charpos, to_x, op);
8846 /* Move IT forward until it satisfies one or more of the criteria in
8847 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8849 OP is a bit-mask that specifies where to stop, and in particular,
8850 which of those four position arguments makes a difference. See the
8851 description of enum move_operation_enum.
8853 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8854 screen line, this function will set IT to the next position that is
8855 displayed to the right of TO_CHARPOS on the screen.
8857 Return the maximum pixel length of any line scanned but never more
8858 than it.last_visible_x. */
8861 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8863 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8864 int line_height, line_start_x = 0, reached = 0;
8865 int max_current_x = 0;
8866 void *backup_data = NULL;
8868 for (;;)
8870 if (op & MOVE_TO_VPOS)
8872 /* If no TO_CHARPOS and no TO_X specified, stop at the
8873 start of the line TO_VPOS. */
8874 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8876 if (it->vpos == to_vpos)
8878 reached = 1;
8879 break;
8881 else
8882 skip = move_it_in_display_line_to (it, -1, -1, 0);
8884 else
8886 /* TO_VPOS >= 0 means stop at TO_X in the line at
8887 TO_VPOS, or at TO_POS, whichever comes first. */
8888 if (it->vpos == to_vpos)
8890 reached = 2;
8891 break;
8894 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8896 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8898 reached = 3;
8899 break;
8901 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8903 /* We have reached TO_X but not in the line we want. */
8904 skip = move_it_in_display_line_to (it, to_charpos,
8905 -1, MOVE_TO_POS);
8906 if (skip == MOVE_POS_MATCH_OR_ZV)
8908 reached = 4;
8909 break;
8914 else if (op & MOVE_TO_Y)
8916 struct it it_backup;
8918 if (it->line_wrap == WORD_WRAP)
8919 SAVE_IT (it_backup, *it, backup_data);
8921 /* TO_Y specified means stop at TO_X in the line containing
8922 TO_Y---or at TO_CHARPOS if this is reached first. The
8923 problem is that we can't really tell whether the line
8924 contains TO_Y before we have completely scanned it, and
8925 this may skip past TO_X. What we do is to first scan to
8926 TO_X.
8928 If TO_X is not specified, use a TO_X of zero. The reason
8929 is to make the outcome of this function more predictable.
8930 If we didn't use TO_X == 0, we would stop at the end of
8931 the line which is probably not what a caller would expect
8932 to happen. */
8933 skip = move_it_in_display_line_to
8934 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8935 (MOVE_TO_X | (op & MOVE_TO_POS)));
8937 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8938 if (skip == MOVE_POS_MATCH_OR_ZV)
8939 reached = 5;
8940 else if (skip == MOVE_X_REACHED)
8942 /* If TO_X was reached, we want to know whether TO_Y is
8943 in the line. We know this is the case if the already
8944 scanned glyphs make the line tall enough. Otherwise,
8945 we must check by scanning the rest of the line. */
8946 line_height = it->max_ascent + it->max_descent;
8947 if (to_y >= it->current_y
8948 && to_y < it->current_y + line_height)
8950 reached = 6;
8951 break;
8953 SAVE_IT (it_backup, *it, backup_data);
8954 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8955 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8956 op & MOVE_TO_POS);
8957 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8958 line_height = it->max_ascent + it->max_descent;
8959 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8961 if (to_y >= it->current_y
8962 && to_y < it->current_y + line_height)
8964 /* If TO_Y is in this line and TO_X was reached
8965 above, we scanned too far. We have to restore
8966 IT's settings to the ones before skipping. But
8967 keep the more accurate values of max_ascent and
8968 max_descent we've found while skipping the rest
8969 of the line, for the sake of callers, such as
8970 pos_visible_p, that need to know the line
8971 height. */
8972 int max_ascent = it->max_ascent;
8973 int max_descent = it->max_descent;
8975 RESTORE_IT (it, &it_backup, backup_data);
8976 it->max_ascent = max_ascent;
8977 it->max_descent = max_descent;
8978 reached = 6;
8980 else
8982 skip = skip2;
8983 if (skip == MOVE_POS_MATCH_OR_ZV)
8984 reached = 7;
8987 else
8989 /* Check whether TO_Y is in this line. */
8990 line_height = it->max_ascent + it->max_descent;
8991 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8993 if (to_y >= it->current_y
8994 && to_y < it->current_y + line_height)
8996 if (to_y > it->current_y)
8997 max_current_x = max (it->current_x, max_current_x);
8999 /* When word-wrap is on, TO_X may lie past the end
9000 of a wrapped line. Then it->current is the
9001 character on the next line, so backtrack to the
9002 space before the wrap point. */
9003 if (skip == MOVE_LINE_CONTINUED
9004 && it->line_wrap == WORD_WRAP)
9006 int prev_x = max (it->current_x - 1, 0);
9007 RESTORE_IT (it, &it_backup, backup_data);
9008 skip = move_it_in_display_line_to
9009 (it, -1, prev_x, MOVE_TO_X);
9012 reached = 6;
9016 if (reached)
9018 max_current_x = max (it->current_x, max_current_x);
9019 break;
9022 else if (BUFFERP (it->object)
9023 && (it->method == GET_FROM_BUFFER
9024 || it->method == GET_FROM_STRETCH)
9025 && IT_CHARPOS (*it) >= to_charpos
9026 /* Under bidi iteration, a call to set_iterator_to_next
9027 can scan far beyond to_charpos if the initial
9028 portion of the next line needs to be reordered. In
9029 that case, give move_it_in_display_line_to another
9030 chance below. */
9031 && !(it->bidi_p
9032 && it->bidi_it.scan_dir == -1))
9033 skip = MOVE_POS_MATCH_OR_ZV;
9034 else
9035 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9037 switch (skip)
9039 case MOVE_POS_MATCH_OR_ZV:
9040 max_current_x = max (it->current_x, max_current_x);
9041 reached = 8;
9042 goto out;
9044 case MOVE_NEWLINE_OR_CR:
9045 max_current_x = max (it->current_x, max_current_x);
9046 set_iterator_to_next (it, 1);
9047 it->continuation_lines_width = 0;
9048 break;
9050 case MOVE_LINE_TRUNCATED:
9051 max_current_x = it->last_visible_x;
9052 it->continuation_lines_width = 0;
9053 reseat_at_next_visible_line_start (it, 0);
9054 if ((op & MOVE_TO_POS) != 0
9055 && IT_CHARPOS (*it) > to_charpos)
9057 reached = 9;
9058 goto out;
9060 break;
9062 case MOVE_LINE_CONTINUED:
9063 max_current_x = it->last_visible_x;
9064 /* For continued lines ending in a tab, some of the glyphs
9065 associated with the tab are displayed on the current
9066 line. Since it->current_x does not include these glyphs,
9067 we use it->last_visible_x instead. */
9068 if (it->c == '\t')
9070 it->continuation_lines_width += it->last_visible_x;
9071 /* When moving by vpos, ensure that the iterator really
9072 advances to the next line (bug#847, bug#969). Fixme:
9073 do we need to do this in other circumstances? */
9074 if (it->current_x != it->last_visible_x
9075 && (op & MOVE_TO_VPOS)
9076 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9078 line_start_x = it->current_x + it->pixel_width
9079 - it->last_visible_x;
9080 set_iterator_to_next (it, 0);
9083 else
9084 it->continuation_lines_width += it->current_x;
9085 break;
9087 default:
9088 emacs_abort ();
9091 /* Reset/increment for the next run. */
9092 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9093 it->current_x = line_start_x;
9094 line_start_x = 0;
9095 it->hpos = 0;
9096 it->current_y += it->max_ascent + it->max_descent;
9097 ++it->vpos;
9098 last_height = it->max_ascent + it->max_descent;
9099 last_max_ascent = it->max_ascent;
9100 it->max_ascent = it->max_descent = 0;
9103 out:
9105 /* On text terminals, we may stop at the end of a line in the middle
9106 of a multi-character glyph. If the glyph itself is continued,
9107 i.e. it is actually displayed on the next line, don't treat this
9108 stopping point as valid; move to the next line instead (unless
9109 that brings us offscreen). */
9110 if (!FRAME_WINDOW_P (it->f)
9111 && op & MOVE_TO_POS
9112 && IT_CHARPOS (*it) == to_charpos
9113 && it->what == IT_CHARACTER
9114 && it->nglyphs > 1
9115 && it->line_wrap == WINDOW_WRAP
9116 && it->current_x == it->last_visible_x - 1
9117 && it->c != '\n'
9118 && it->c != '\t'
9119 && it->vpos < it->w->window_end_vpos)
9121 it->continuation_lines_width += it->current_x;
9122 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9123 it->current_y += it->max_ascent + it->max_descent;
9124 ++it->vpos;
9125 last_height = it->max_ascent + it->max_descent;
9126 last_max_ascent = it->max_ascent;
9129 if (backup_data)
9130 bidi_unshelve_cache (backup_data, 1);
9132 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9134 return max_current_x;
9138 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9140 If DY > 0, move IT backward at least that many pixels. DY = 0
9141 means move IT backward to the preceding line start or BEGV. This
9142 function may move over more than DY pixels if IT->current_y - DY
9143 ends up in the middle of a line; in this case IT->current_y will be
9144 set to the top of the line moved to. */
9146 void
9147 move_it_vertically_backward (struct it *it, int dy)
9149 int nlines, h;
9150 struct it it2, it3;
9151 void *it2data = NULL, *it3data = NULL;
9152 ptrdiff_t start_pos;
9153 int nchars_per_row
9154 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9155 ptrdiff_t pos_limit;
9157 move_further_back:
9158 eassert (dy >= 0);
9160 start_pos = IT_CHARPOS (*it);
9162 /* Estimate how many newlines we must move back. */
9163 nlines = max (1, dy / default_line_pixel_height (it->w));
9164 if (it->line_wrap == TRUNCATE)
9165 pos_limit = BEGV;
9166 else
9167 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9169 /* Set the iterator's position that many lines back. But don't go
9170 back more than NLINES full screen lines -- this wins a day with
9171 buffers which have very long lines. */
9172 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9173 back_to_previous_visible_line_start (it);
9175 /* Reseat the iterator here. When moving backward, we don't want
9176 reseat to skip forward over invisible text, set up the iterator
9177 to deliver from overlay strings at the new position etc. So,
9178 use reseat_1 here. */
9179 reseat_1 (it, it->current.pos, 1);
9181 /* We are now surely at a line start. */
9182 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9183 reordering is in effect. */
9184 it->continuation_lines_width = 0;
9186 /* Move forward and see what y-distance we moved. First move to the
9187 start of the next line so that we get its height. We need this
9188 height to be able to tell whether we reached the specified
9189 y-distance. */
9190 SAVE_IT (it2, *it, it2data);
9191 it2.max_ascent = it2.max_descent = 0;
9194 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9195 MOVE_TO_POS | MOVE_TO_VPOS);
9197 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9198 /* If we are in a display string which starts at START_POS,
9199 and that display string includes a newline, and we are
9200 right after that newline (i.e. at the beginning of a
9201 display line), exit the loop, because otherwise we will
9202 infloop, since move_it_to will see that it is already at
9203 START_POS and will not move. */
9204 || (it2.method == GET_FROM_STRING
9205 && IT_CHARPOS (it2) == start_pos
9206 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9207 eassert (IT_CHARPOS (*it) >= BEGV);
9208 SAVE_IT (it3, it2, it3data);
9210 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9211 eassert (IT_CHARPOS (*it) >= BEGV);
9212 /* H is the actual vertical distance from the position in *IT
9213 and the starting position. */
9214 h = it2.current_y - it->current_y;
9215 /* NLINES is the distance in number of lines. */
9216 nlines = it2.vpos - it->vpos;
9218 /* Correct IT's y and vpos position
9219 so that they are relative to the starting point. */
9220 it->vpos -= nlines;
9221 it->current_y -= h;
9223 if (dy == 0)
9225 /* DY == 0 means move to the start of the screen line. The
9226 value of nlines is > 0 if continuation lines were involved,
9227 or if the original IT position was at start of a line. */
9228 RESTORE_IT (it, it, it2data);
9229 if (nlines > 0)
9230 move_it_by_lines (it, nlines);
9231 /* The above code moves us to some position NLINES down,
9232 usually to its first glyph (leftmost in an L2R line), but
9233 that's not necessarily the start of the line, under bidi
9234 reordering. We want to get to the character position
9235 that is immediately after the newline of the previous
9236 line. */
9237 if (it->bidi_p
9238 && !it->continuation_lines_width
9239 && !STRINGP (it->string)
9240 && IT_CHARPOS (*it) > BEGV
9241 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9245 DEC_BOTH (cp, bp);
9246 cp = find_newline_no_quit (cp, bp, -1, NULL);
9247 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9249 bidi_unshelve_cache (it3data, 1);
9251 else
9253 /* The y-position we try to reach, relative to *IT.
9254 Note that H has been subtracted in front of the if-statement. */
9255 int target_y = it->current_y + h - dy;
9256 int y0 = it3.current_y;
9257 int y1;
9258 int line_height;
9260 RESTORE_IT (&it3, &it3, it3data);
9261 y1 = line_bottom_y (&it3);
9262 line_height = y1 - y0;
9263 RESTORE_IT (it, it, it2data);
9264 /* If we did not reach target_y, try to move further backward if
9265 we can. If we moved too far backward, try to move forward. */
9266 if (target_y < it->current_y
9267 /* This is heuristic. In a window that's 3 lines high, with
9268 a line height of 13 pixels each, recentering with point
9269 on the bottom line will try to move -39/2 = 19 pixels
9270 backward. Try to avoid moving into the first line. */
9271 && (it->current_y - target_y
9272 > min (window_box_height (it->w), line_height * 2 / 3))
9273 && IT_CHARPOS (*it) > BEGV)
9275 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9276 target_y - it->current_y));
9277 dy = it->current_y - target_y;
9278 goto move_further_back;
9280 else if (target_y >= it->current_y + line_height
9281 && IT_CHARPOS (*it) < ZV)
9283 /* Should move forward by at least one line, maybe more.
9285 Note: Calling move_it_by_lines can be expensive on
9286 terminal frames, where compute_motion is used (via
9287 vmotion) to do the job, when there are very long lines
9288 and truncate-lines is nil. That's the reason for
9289 treating terminal frames specially here. */
9291 if (!FRAME_WINDOW_P (it->f))
9292 move_it_vertically (it, target_y - (it->current_y + line_height));
9293 else
9297 move_it_by_lines (it, 1);
9299 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9306 /* Move IT by a specified amount of pixel lines DY. DY negative means
9307 move backwards. DY = 0 means move to start of screen line. At the
9308 end, IT will be on the start of a screen line. */
9310 void
9311 move_it_vertically (struct it *it, int dy)
9313 if (dy <= 0)
9314 move_it_vertically_backward (it, -dy);
9315 else
9317 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9318 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9319 MOVE_TO_POS | MOVE_TO_Y);
9320 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9322 /* If buffer ends in ZV without a newline, move to the start of
9323 the line to satisfy the post-condition. */
9324 if (IT_CHARPOS (*it) == ZV
9325 && ZV > BEGV
9326 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9327 move_it_by_lines (it, 0);
9332 /* Move iterator IT past the end of the text line it is in. */
9334 void
9335 move_it_past_eol (struct it *it)
9337 enum move_it_result rc;
9339 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9340 if (rc == MOVE_NEWLINE_OR_CR)
9341 set_iterator_to_next (it, 0);
9345 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9346 negative means move up. DVPOS == 0 means move to the start of the
9347 screen line.
9349 Optimization idea: If we would know that IT->f doesn't use
9350 a face with proportional font, we could be faster for
9351 truncate-lines nil. */
9353 void
9354 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9357 /* The commented-out optimization uses vmotion on terminals. This
9358 gives bad results, because elements like it->what, on which
9359 callers such as pos_visible_p rely, aren't updated. */
9360 /* struct position pos;
9361 if (!FRAME_WINDOW_P (it->f))
9363 struct text_pos textpos;
9365 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9366 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9367 reseat (it, textpos, 1);
9368 it->vpos += pos.vpos;
9369 it->current_y += pos.vpos;
9371 else */
9373 if (dvpos == 0)
9375 /* DVPOS == 0 means move to the start of the screen line. */
9376 move_it_vertically_backward (it, 0);
9377 /* Let next call to line_bottom_y calculate real line height. */
9378 last_height = 0;
9380 else if (dvpos > 0)
9382 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9383 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9385 /* Only move to the next buffer position if we ended up in a
9386 string from display property, not in an overlay string
9387 (before-string or after-string). That is because the
9388 latter don't conceal the underlying buffer position, so
9389 we can ask to move the iterator to the exact position we
9390 are interested in. Note that, even if we are already at
9391 IT_CHARPOS (*it), the call below is not a no-op, as it
9392 will detect that we are at the end of the string, pop the
9393 iterator, and compute it->current_x and it->hpos
9394 correctly. */
9395 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9396 -1, -1, -1, MOVE_TO_POS);
9399 else
9401 struct it it2;
9402 void *it2data = NULL;
9403 ptrdiff_t start_charpos, i;
9404 int nchars_per_row
9405 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9406 ptrdiff_t pos_limit;
9408 /* Start at the beginning of the screen line containing IT's
9409 position. This may actually move vertically backwards,
9410 in case of overlays, so adjust dvpos accordingly. */
9411 dvpos += it->vpos;
9412 move_it_vertically_backward (it, 0);
9413 dvpos -= it->vpos;
9415 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9416 screen lines, and reseat the iterator there. */
9417 start_charpos = IT_CHARPOS (*it);
9418 if (it->line_wrap == TRUNCATE)
9419 pos_limit = BEGV;
9420 else
9421 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9422 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9423 back_to_previous_visible_line_start (it);
9424 reseat (it, it->current.pos, 1);
9426 /* Move further back if we end up in a string or an image. */
9427 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9429 /* First try to move to start of display line. */
9430 dvpos += it->vpos;
9431 move_it_vertically_backward (it, 0);
9432 dvpos -= it->vpos;
9433 if (IT_POS_VALID_AFTER_MOVE_P (it))
9434 break;
9435 /* If start of line is still in string or image,
9436 move further back. */
9437 back_to_previous_visible_line_start (it);
9438 reseat (it, it->current.pos, 1);
9439 dvpos--;
9442 it->current_x = it->hpos = 0;
9444 /* Above call may have moved too far if continuation lines
9445 are involved. Scan forward and see if it did. */
9446 SAVE_IT (it2, *it, it2data);
9447 it2.vpos = it2.current_y = 0;
9448 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9449 it->vpos -= it2.vpos;
9450 it->current_y -= it2.current_y;
9451 it->current_x = it->hpos = 0;
9453 /* If we moved too far back, move IT some lines forward. */
9454 if (it2.vpos > -dvpos)
9456 int delta = it2.vpos + dvpos;
9458 RESTORE_IT (&it2, &it2, it2data);
9459 SAVE_IT (it2, *it, it2data);
9460 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9461 /* Move back again if we got too far ahead. */
9462 if (IT_CHARPOS (*it) >= start_charpos)
9463 RESTORE_IT (it, &it2, it2data);
9464 else
9465 bidi_unshelve_cache (it2data, 1);
9467 else
9468 RESTORE_IT (it, it, it2data);
9472 /* Return true if IT points into the middle of a display vector. */
9474 bool
9475 in_display_vector_p (struct it *it)
9477 return (it->method == GET_FROM_DISPLAY_VECTOR
9478 && it->current.dpvec_index > 0
9479 && it->dpvec + it->current.dpvec_index != it->dpend);
9482 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9483 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9484 WINDOW must be a live window and defaults to the selected one. The
9485 return value is a cons of the maximum pixel-width of any text line and
9486 the maximum pixel-height of all text lines.
9488 The optional argument FROM, if non-nil, specifies the first text
9489 position and defaults to the minimum accessible position of the buffer.
9490 If FROM is t, use the minimum accessible position that is not a newline
9491 character. TO, if non-nil, specifies the last text position and
9492 defaults to the maximum accessible position of the buffer. If TO is t,
9493 use the maximum accessible position that is not a newline character.
9495 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9496 width that can be returned. X_LIMIT nil or omitted, means to use the
9497 pixel-width of WINDOW's body; use this if you do not intend to change
9498 the width of WINDOW. Use the maximum width WINDOW may assume if you
9499 intend to change WINDOW's width.
9501 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9502 height that can be returned. Text lines whose y-coordinate is beyond
9503 Y_LIMIT are ignored. Since calculating the text height of a large
9504 buffer can take some time, it makes sense to specify this argument if
9505 the size of the buffer is unknown.
9507 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9508 include the height of the mode- or header-line of WINDOW in the return
9509 value. If it is either the symbol `mode-line' or `header-line', include
9510 only the height of that line, if present, in the return value. If t,
9511 include the height of any of these lines in the return value. */)
9512 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9513 Lisp_Object mode_and_header_line)
9515 struct window *w = decode_live_window (window);
9516 Lisp_Object buf;
9517 struct buffer *b;
9518 struct it it;
9519 struct buffer *old_buffer = NULL;
9520 ptrdiff_t start, end, pos;
9521 struct text_pos startp;
9522 void *itdata = NULL;
9523 int c, max_y = -1, x = 0, y = 0;
9525 buf = w->contents;
9526 CHECK_BUFFER (buf);
9527 b = XBUFFER (buf);
9529 if (b != current_buffer)
9531 old_buffer = current_buffer;
9532 set_buffer_internal (b);
9535 if (NILP (from))
9536 start = BEGV;
9537 else if (EQ (from, Qt))
9539 start = pos = BEGV;
9540 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9541 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9542 start = pos;
9543 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9544 start = pos;
9546 else
9548 CHECK_NUMBER_COERCE_MARKER (from);
9549 start = min (max (XINT (from), BEGV), ZV);
9552 if (NILP (to))
9553 end = ZV;
9554 else if (EQ (to, Qt))
9556 end = pos = ZV;
9557 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9558 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9559 end = pos;
9560 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9561 end = pos;
9563 else
9565 CHECK_NUMBER_COERCE_MARKER (to);
9566 end = max (start, min (XINT (to), ZV));
9569 if (!NILP (y_limit))
9571 CHECK_NUMBER (y_limit);
9572 max_y = min (XINT (y_limit), INT_MAX);
9575 itdata = bidi_shelve_cache ();
9576 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9577 start_display (&it, w, startp);
9579 /** move_it_vertically_backward (&it, 0); **/
9580 if (NILP (x_limit))
9581 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9582 else
9584 CHECK_NUMBER (x_limit);
9585 it.last_visible_x = min (XINT (x_limit), INFINITY);
9586 /* Actually, we never want move_it_to stop at to_x. But to make
9587 sure that move_it_in_display_line_to always moves far enough,
9588 we set it to INT_MAX and specify MOVE_TO_X. */
9589 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9593 if (start == end)
9594 y = it.current_y;
9595 else
9597 /* Count last line. */
9598 last_height = 0;
9599 y = line_bottom_y (&it); /* - y; */
9602 if (!EQ (mode_and_header_line, Qheader_line)
9603 && !EQ (mode_and_header_line, Qt))
9604 /* Do not count the header-line which was counted automatically by
9605 start_display. */
9606 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9608 if (EQ (mode_and_header_line, Qmode_line)
9609 || EQ (mode_and_header_line, Qt))
9610 /* Do count the mode-line which is not included automatically by
9611 start_display. */
9612 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9614 bidi_unshelve_cache (itdata, 0);
9616 if (old_buffer)
9617 set_buffer_internal (old_buffer);
9619 return Fcons (make_number (x), make_number (y));
9622 /***********************************************************************
9623 Messages
9624 ***********************************************************************/
9627 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9628 to *Messages*. */
9630 void
9631 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9633 Lisp_Object args[3];
9634 Lisp_Object msg, fmt;
9635 char *buffer;
9636 ptrdiff_t len;
9637 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9638 USE_SAFE_ALLOCA;
9640 fmt = msg = Qnil;
9641 GCPRO4 (fmt, msg, arg1, arg2);
9643 args[0] = fmt = build_string (format);
9644 args[1] = arg1;
9645 args[2] = arg2;
9646 msg = Fformat (3, args);
9648 len = SBYTES (msg) + 1;
9649 buffer = SAFE_ALLOCA (len);
9650 memcpy (buffer, SDATA (msg), len);
9652 message_dolog (buffer, len - 1, 1, 0);
9653 SAFE_FREE ();
9655 UNGCPRO;
9659 /* Output a newline in the *Messages* buffer if "needs" one. */
9661 void
9662 message_log_maybe_newline (void)
9664 if (message_log_need_newline)
9665 message_dolog ("", 0, 1, 0);
9669 /* Add a string M of length NBYTES to the message log, optionally
9670 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9671 true, means interpret the contents of M as multibyte. This
9672 function calls low-level routines in order to bypass text property
9673 hooks, etc. which might not be safe to run.
9675 This may GC (insert may run before/after change hooks),
9676 so the buffer M must NOT point to a Lisp string. */
9678 void
9679 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9681 const unsigned char *msg = (const unsigned char *) m;
9683 if (!NILP (Vmemory_full))
9684 return;
9686 if (!NILP (Vmessage_log_max))
9688 struct buffer *oldbuf;
9689 Lisp_Object oldpoint, oldbegv, oldzv;
9690 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9691 ptrdiff_t point_at_end = 0;
9692 ptrdiff_t zv_at_end = 0;
9693 Lisp_Object old_deactivate_mark;
9694 struct gcpro gcpro1;
9696 old_deactivate_mark = Vdeactivate_mark;
9697 oldbuf = current_buffer;
9699 /* Ensure the Messages buffer exists, and switch to it.
9700 If we created it, set the major-mode. */
9702 int newbuffer = 0;
9703 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9705 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9707 if (newbuffer
9708 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9709 call0 (intern ("messages-buffer-mode"));
9712 bset_undo_list (current_buffer, Qt);
9713 bset_cache_long_scans (current_buffer, Qnil);
9715 oldpoint = message_dolog_marker1;
9716 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9717 oldbegv = message_dolog_marker2;
9718 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9719 oldzv = message_dolog_marker3;
9720 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9721 GCPRO1 (old_deactivate_mark);
9723 if (PT == Z)
9724 point_at_end = 1;
9725 if (ZV == Z)
9726 zv_at_end = 1;
9728 BEGV = BEG;
9729 BEGV_BYTE = BEG_BYTE;
9730 ZV = Z;
9731 ZV_BYTE = Z_BYTE;
9732 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9734 /* Insert the string--maybe converting multibyte to single byte
9735 or vice versa, so that all the text fits the buffer. */
9736 if (multibyte
9737 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9739 ptrdiff_t i;
9740 int c, char_bytes;
9741 char work[1];
9743 /* Convert a multibyte string to single-byte
9744 for the *Message* buffer. */
9745 for (i = 0; i < nbytes; i += char_bytes)
9747 c = string_char_and_length (msg + i, &char_bytes);
9748 work[0] = (ASCII_CHAR_P (c)
9750 : multibyte_char_to_unibyte (c));
9751 insert_1_both (work, 1, 1, 1, 0, 0);
9754 else if (! multibyte
9755 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9757 ptrdiff_t i;
9758 int c, char_bytes;
9759 unsigned char str[MAX_MULTIBYTE_LENGTH];
9760 /* Convert a single-byte string to multibyte
9761 for the *Message* buffer. */
9762 for (i = 0; i < nbytes; i++)
9764 c = msg[i];
9765 MAKE_CHAR_MULTIBYTE (c);
9766 char_bytes = CHAR_STRING (c, str);
9767 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9770 else if (nbytes)
9771 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9773 if (nlflag)
9775 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9776 printmax_t dups;
9778 insert_1_both ("\n", 1, 1, 1, 0, 0);
9780 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9781 this_bol = PT;
9782 this_bol_byte = PT_BYTE;
9784 /* See if this line duplicates the previous one.
9785 If so, combine duplicates. */
9786 if (this_bol > BEG)
9788 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9789 prev_bol = PT;
9790 prev_bol_byte = PT_BYTE;
9792 dups = message_log_check_duplicate (prev_bol_byte,
9793 this_bol_byte);
9794 if (dups)
9796 del_range_both (prev_bol, prev_bol_byte,
9797 this_bol, this_bol_byte, 0);
9798 if (dups > 1)
9800 char dupstr[sizeof " [ times]"
9801 + INT_STRLEN_BOUND (printmax_t)];
9803 /* If you change this format, don't forget to also
9804 change message_log_check_duplicate. */
9805 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9806 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9807 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9812 /* If we have more than the desired maximum number of lines
9813 in the *Messages* buffer now, delete the oldest ones.
9814 This is safe because we don't have undo in this buffer. */
9816 if (NATNUMP (Vmessage_log_max))
9818 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9819 -XFASTINT (Vmessage_log_max) - 1, 0);
9820 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9823 BEGV = marker_position (oldbegv);
9824 BEGV_BYTE = marker_byte_position (oldbegv);
9826 if (zv_at_end)
9828 ZV = Z;
9829 ZV_BYTE = Z_BYTE;
9831 else
9833 ZV = marker_position (oldzv);
9834 ZV_BYTE = marker_byte_position (oldzv);
9837 if (point_at_end)
9838 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9839 else
9840 /* We can't do Fgoto_char (oldpoint) because it will run some
9841 Lisp code. */
9842 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9843 marker_byte_position (oldpoint));
9845 UNGCPRO;
9846 unchain_marker (XMARKER (oldpoint));
9847 unchain_marker (XMARKER (oldbegv));
9848 unchain_marker (XMARKER (oldzv));
9850 /* We called insert_1_both above with its 5th argument (PREPARE)
9851 zero, which prevents insert_1_both from calling
9852 prepare_to_modify_buffer, which in turns prevents us from
9853 incrementing windows_or_buffers_changed even if *Messages* is
9854 shown in some window. So we must manually set
9855 windows_or_buffers_changed here to make up for that. */
9856 windows_or_buffers_changed = old_windows_or_buffers_changed;
9857 bset_redisplay (current_buffer);
9859 set_buffer_internal (oldbuf);
9861 message_log_need_newline = !nlflag;
9862 Vdeactivate_mark = old_deactivate_mark;
9867 /* We are at the end of the buffer after just having inserted a newline.
9868 (Note: We depend on the fact we won't be crossing the gap.)
9869 Check to see if the most recent message looks a lot like the previous one.
9870 Return 0 if different, 1 if the new one should just replace it, or a
9871 value N > 1 if we should also append " [N times]". */
9873 static intmax_t
9874 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9876 ptrdiff_t i;
9877 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9878 int seen_dots = 0;
9879 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9880 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9882 for (i = 0; i < len; i++)
9884 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9885 seen_dots = 1;
9886 if (p1[i] != p2[i])
9887 return seen_dots;
9889 p1 += len;
9890 if (*p1 == '\n')
9891 return 2;
9892 if (*p1++ == ' ' && *p1++ == '[')
9894 char *pend;
9895 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9896 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9897 return n + 1;
9899 return 0;
9903 /* Display an echo area message M with a specified length of NBYTES
9904 bytes. The string may include null characters. If M is not a
9905 string, clear out any existing message, and let the mini-buffer
9906 text show through.
9908 This function cancels echoing. */
9910 void
9911 message3 (Lisp_Object m)
9913 struct gcpro gcpro1;
9915 GCPRO1 (m);
9916 clear_message (true, true);
9917 cancel_echoing ();
9919 /* First flush out any partial line written with print. */
9920 message_log_maybe_newline ();
9921 if (STRINGP (m))
9923 ptrdiff_t nbytes = SBYTES (m);
9924 bool multibyte = STRING_MULTIBYTE (m);
9925 USE_SAFE_ALLOCA;
9926 char *buffer = SAFE_ALLOCA (nbytes);
9927 memcpy (buffer, SDATA (m), nbytes);
9928 message_dolog (buffer, nbytes, 1, multibyte);
9929 SAFE_FREE ();
9931 message3_nolog (m);
9933 UNGCPRO;
9937 /* The non-logging version of message3.
9938 This does not cancel echoing, because it is used for echoing.
9939 Perhaps we need to make a separate function for echoing
9940 and make this cancel echoing. */
9942 void
9943 message3_nolog (Lisp_Object m)
9945 struct frame *sf = SELECTED_FRAME ();
9947 if (FRAME_INITIAL_P (sf))
9949 if (noninteractive_need_newline)
9950 putc ('\n', stderr);
9951 noninteractive_need_newline = 0;
9952 if (STRINGP (m))
9954 Lisp_Object s = ENCODE_SYSTEM (m);
9956 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9958 if (cursor_in_echo_area == 0)
9959 fprintf (stderr, "\n");
9960 fflush (stderr);
9962 /* Error messages get reported properly by cmd_error, so this must be just an
9963 informative message; if the frame hasn't really been initialized yet, just
9964 toss it. */
9965 else if (INTERACTIVE && sf->glyphs_initialized_p)
9967 /* Get the frame containing the mini-buffer
9968 that the selected frame is using. */
9969 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9970 Lisp_Object frame = XWINDOW (mini_window)->frame;
9971 struct frame *f = XFRAME (frame);
9973 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9974 Fmake_frame_visible (frame);
9976 if (STRINGP (m) && SCHARS (m) > 0)
9978 set_message (m);
9979 if (minibuffer_auto_raise)
9980 Fraise_frame (frame);
9981 /* Assume we are not echoing.
9982 (If we are, echo_now will override this.) */
9983 echo_message_buffer = Qnil;
9985 else
9986 clear_message (true, true);
9988 do_pending_window_change (0);
9989 echo_area_display (1);
9990 do_pending_window_change (0);
9991 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9992 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9997 /* Display a null-terminated echo area message M. If M is 0, clear
9998 out any existing message, and let the mini-buffer text show through.
10000 The buffer M must continue to exist until after the echo area gets
10001 cleared or some other message gets displayed there. Do not pass
10002 text that is stored in a Lisp string. Do not pass text in a buffer
10003 that was alloca'd. */
10005 void
10006 message1 (const char *m)
10008 message3 (m ? build_unibyte_string (m) : Qnil);
10012 /* The non-logging counterpart of message1. */
10014 void
10015 message1_nolog (const char *m)
10017 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10020 /* Display a message M which contains a single %s
10021 which gets replaced with STRING. */
10023 void
10024 message_with_string (const char *m, Lisp_Object string, int log)
10026 CHECK_STRING (string);
10028 if (noninteractive)
10030 if (m)
10032 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10033 String whose data pointer might be passed to us in M. So
10034 we use a local copy. */
10035 char *fmt = xstrdup (m);
10037 if (noninteractive_need_newline)
10038 putc ('\n', stderr);
10039 noninteractive_need_newline = 0;
10040 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10041 if (!cursor_in_echo_area)
10042 fprintf (stderr, "\n");
10043 fflush (stderr);
10044 xfree (fmt);
10047 else if (INTERACTIVE)
10049 /* The frame whose minibuffer we're going to display the message on.
10050 It may be larger than the selected frame, so we need
10051 to use its buffer, not the selected frame's buffer. */
10052 Lisp_Object mini_window;
10053 struct frame *f, *sf = SELECTED_FRAME ();
10055 /* Get the frame containing the minibuffer
10056 that the selected frame is using. */
10057 mini_window = FRAME_MINIBUF_WINDOW (sf);
10058 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10060 /* Error messages get reported properly by cmd_error, so this must be
10061 just an informative message; if the frame hasn't really been
10062 initialized yet, just toss it. */
10063 if (f->glyphs_initialized_p)
10065 Lisp_Object args[2], msg;
10066 struct gcpro gcpro1, gcpro2;
10068 args[0] = build_string (m);
10069 args[1] = msg = string;
10070 GCPRO2 (args[0], msg);
10071 gcpro1.nvars = 2;
10073 msg = Fformat (2, args);
10075 if (log)
10076 message3 (msg);
10077 else
10078 message3_nolog (msg);
10080 UNGCPRO;
10082 /* Print should start at the beginning of the message
10083 buffer next time. */
10084 message_buf_print = 0;
10090 /* Dump an informative message to the minibuf. If M is 0, clear out
10091 any existing message, and let the mini-buffer text show through. */
10093 static void
10094 vmessage (const char *m, va_list ap)
10096 if (noninteractive)
10098 if (m)
10100 if (noninteractive_need_newline)
10101 putc ('\n', stderr);
10102 noninteractive_need_newline = 0;
10103 vfprintf (stderr, m, ap);
10104 if (cursor_in_echo_area == 0)
10105 fprintf (stderr, "\n");
10106 fflush (stderr);
10109 else if (INTERACTIVE)
10111 /* The frame whose mini-buffer we're going to display the message
10112 on. It may be larger than the selected frame, so we need to
10113 use its buffer, not the selected frame's buffer. */
10114 Lisp_Object mini_window;
10115 struct frame *f, *sf = SELECTED_FRAME ();
10117 /* Get the frame containing the mini-buffer
10118 that the selected frame is using. */
10119 mini_window = FRAME_MINIBUF_WINDOW (sf);
10120 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10122 /* Error messages get reported properly by cmd_error, so this must be
10123 just an informative message; if the frame hasn't really been
10124 initialized yet, just toss it. */
10125 if (f->glyphs_initialized_p)
10127 if (m)
10129 ptrdiff_t len;
10130 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10131 char *message_buf = alloca (maxsize + 1);
10133 len = doprnt (message_buf, maxsize, m, 0, ap);
10135 message3 (make_string (message_buf, len));
10137 else
10138 message1 (0);
10140 /* Print should start at the beginning of the message
10141 buffer next time. */
10142 message_buf_print = 0;
10147 void
10148 message (const char *m, ...)
10150 va_list ap;
10151 va_start (ap, m);
10152 vmessage (m, ap);
10153 va_end (ap);
10157 #if 0
10158 /* The non-logging version of message. */
10160 void
10161 message_nolog (const char *m, ...)
10163 Lisp_Object old_log_max;
10164 va_list ap;
10165 va_start (ap, m);
10166 old_log_max = Vmessage_log_max;
10167 Vmessage_log_max = Qnil;
10168 vmessage (m, ap);
10169 Vmessage_log_max = old_log_max;
10170 va_end (ap);
10172 #endif
10175 /* Display the current message in the current mini-buffer. This is
10176 only called from error handlers in process.c, and is not time
10177 critical. */
10179 void
10180 update_echo_area (void)
10182 if (!NILP (echo_area_buffer[0]))
10184 Lisp_Object string;
10185 string = Fcurrent_message ();
10186 message3 (string);
10191 /* Make sure echo area buffers in `echo_buffers' are live.
10192 If they aren't, make new ones. */
10194 static void
10195 ensure_echo_area_buffers (void)
10197 int i;
10199 for (i = 0; i < 2; ++i)
10200 if (!BUFFERP (echo_buffer[i])
10201 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10203 char name[30];
10204 Lisp_Object old_buffer;
10205 int j;
10207 old_buffer = echo_buffer[i];
10208 echo_buffer[i] = Fget_buffer_create
10209 (make_formatted_string (name, " *Echo Area %d*", i));
10210 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10211 /* to force word wrap in echo area -
10212 it was decided to postpone this*/
10213 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10215 for (j = 0; j < 2; ++j)
10216 if (EQ (old_buffer, echo_area_buffer[j]))
10217 echo_area_buffer[j] = echo_buffer[i];
10222 /* Call FN with args A1..A2 with either the current or last displayed
10223 echo_area_buffer as current buffer.
10225 WHICH zero means use the current message buffer
10226 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10227 from echo_buffer[] and clear it.
10229 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10230 suitable buffer from echo_buffer[] and clear it.
10232 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10233 that the current message becomes the last displayed one, make
10234 choose a suitable buffer for echo_area_buffer[0], and clear it.
10236 Value is what FN returns. */
10238 static int
10239 with_echo_area_buffer (struct window *w, int which,
10240 int (*fn) (ptrdiff_t, Lisp_Object),
10241 ptrdiff_t a1, Lisp_Object a2)
10243 Lisp_Object buffer;
10244 int this_one, the_other, clear_buffer_p, rc;
10245 ptrdiff_t count = SPECPDL_INDEX ();
10247 /* If buffers aren't live, make new ones. */
10248 ensure_echo_area_buffers ();
10250 clear_buffer_p = 0;
10252 if (which == 0)
10253 this_one = 0, the_other = 1;
10254 else if (which > 0)
10255 this_one = 1, the_other = 0;
10256 else
10258 this_one = 0, the_other = 1;
10259 clear_buffer_p = true;
10261 /* We need a fresh one in case the current echo buffer equals
10262 the one containing the last displayed echo area message. */
10263 if (!NILP (echo_area_buffer[this_one])
10264 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10265 echo_area_buffer[this_one] = Qnil;
10268 /* Choose a suitable buffer from echo_buffer[] is we don't
10269 have one. */
10270 if (NILP (echo_area_buffer[this_one]))
10272 echo_area_buffer[this_one]
10273 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10274 ? echo_buffer[the_other]
10275 : echo_buffer[this_one]);
10276 clear_buffer_p = true;
10279 buffer = echo_area_buffer[this_one];
10281 /* Don't get confused by reusing the buffer used for echoing
10282 for a different purpose. */
10283 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10284 cancel_echoing ();
10286 record_unwind_protect (unwind_with_echo_area_buffer,
10287 with_echo_area_buffer_unwind_data (w));
10289 /* Make the echo area buffer current. Note that for display
10290 purposes, it is not necessary that the displayed window's buffer
10291 == current_buffer, except for text property lookup. So, let's
10292 only set that buffer temporarily here without doing a full
10293 Fset_window_buffer. We must also change w->pointm, though,
10294 because otherwise an assertions in unshow_buffer fails, and Emacs
10295 aborts. */
10296 set_buffer_internal_1 (XBUFFER (buffer));
10297 if (w)
10299 wset_buffer (w, buffer);
10300 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10303 bset_undo_list (current_buffer, Qt);
10304 bset_read_only (current_buffer, Qnil);
10305 specbind (Qinhibit_read_only, Qt);
10306 specbind (Qinhibit_modification_hooks, Qt);
10308 if (clear_buffer_p && Z > BEG)
10309 del_range (BEG, Z);
10311 eassert (BEGV >= BEG);
10312 eassert (ZV <= Z && ZV >= BEGV);
10314 rc = fn (a1, a2);
10316 eassert (BEGV >= BEG);
10317 eassert (ZV <= Z && ZV >= BEGV);
10319 unbind_to (count, Qnil);
10320 return rc;
10324 /* Save state that should be preserved around the call to the function
10325 FN called in with_echo_area_buffer. */
10327 static Lisp_Object
10328 with_echo_area_buffer_unwind_data (struct window *w)
10330 int i = 0;
10331 Lisp_Object vector, tmp;
10333 /* Reduce consing by keeping one vector in
10334 Vwith_echo_area_save_vector. */
10335 vector = Vwith_echo_area_save_vector;
10336 Vwith_echo_area_save_vector = Qnil;
10338 if (NILP (vector))
10339 vector = Fmake_vector (make_number (9), Qnil);
10341 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10342 ASET (vector, i, Vdeactivate_mark); ++i;
10343 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10345 if (w)
10347 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10348 ASET (vector, i, w->contents); ++i;
10349 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10351 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10352 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10354 else
10356 int end = i + 6;
10357 for (; i < end; ++i)
10358 ASET (vector, i, Qnil);
10361 eassert (i == ASIZE (vector));
10362 return vector;
10366 /* Restore global state from VECTOR which was created by
10367 with_echo_area_buffer_unwind_data. */
10369 static void
10370 unwind_with_echo_area_buffer (Lisp_Object vector)
10372 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10373 Vdeactivate_mark = AREF (vector, 1);
10374 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10376 if (WINDOWP (AREF (vector, 3)))
10378 struct window *w;
10379 Lisp_Object buffer;
10381 w = XWINDOW (AREF (vector, 3));
10382 buffer = AREF (vector, 4);
10384 wset_buffer (w, buffer);
10385 set_marker_both (w->pointm, buffer,
10386 XFASTINT (AREF (vector, 5)),
10387 XFASTINT (AREF (vector, 6)));
10388 set_marker_both (w->start, buffer,
10389 XFASTINT (AREF (vector, 7)),
10390 XFASTINT (AREF (vector, 8)));
10393 Vwith_echo_area_save_vector = vector;
10397 /* Set up the echo area for use by print functions. MULTIBYTE_P
10398 non-zero means we will print multibyte. */
10400 void
10401 setup_echo_area_for_printing (int multibyte_p)
10403 /* If we can't find an echo area any more, exit. */
10404 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10405 Fkill_emacs (Qnil);
10407 ensure_echo_area_buffers ();
10409 if (!message_buf_print)
10411 /* A message has been output since the last time we printed.
10412 Choose a fresh echo area buffer. */
10413 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10414 echo_area_buffer[0] = echo_buffer[1];
10415 else
10416 echo_area_buffer[0] = echo_buffer[0];
10418 /* Switch to that buffer and clear it. */
10419 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10420 bset_truncate_lines (current_buffer, Qnil);
10422 if (Z > BEG)
10424 ptrdiff_t count = SPECPDL_INDEX ();
10425 specbind (Qinhibit_read_only, Qt);
10426 /* Note that undo recording is always disabled. */
10427 del_range (BEG, Z);
10428 unbind_to (count, Qnil);
10430 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10432 /* Set up the buffer for the multibyteness we need. */
10433 if (multibyte_p
10434 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10435 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10437 /* Raise the frame containing the echo area. */
10438 if (minibuffer_auto_raise)
10440 struct frame *sf = SELECTED_FRAME ();
10441 Lisp_Object mini_window;
10442 mini_window = FRAME_MINIBUF_WINDOW (sf);
10443 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10446 message_log_maybe_newline ();
10447 message_buf_print = 1;
10449 else
10451 if (NILP (echo_area_buffer[0]))
10453 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10454 echo_area_buffer[0] = echo_buffer[1];
10455 else
10456 echo_area_buffer[0] = echo_buffer[0];
10459 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10461 /* Someone switched buffers between print requests. */
10462 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10463 bset_truncate_lines (current_buffer, Qnil);
10469 /* Display an echo area message in window W. Value is non-zero if W's
10470 height is changed. If display_last_displayed_message_p is
10471 non-zero, display the message that was last displayed, otherwise
10472 display the current message. */
10474 static int
10475 display_echo_area (struct window *w)
10477 int i, no_message_p, window_height_changed_p;
10479 /* Temporarily disable garbage collections while displaying the echo
10480 area. This is done because a GC can print a message itself.
10481 That message would modify the echo area buffer's contents while a
10482 redisplay of the buffer is going on, and seriously confuse
10483 redisplay. */
10484 ptrdiff_t count = inhibit_garbage_collection ();
10486 /* If there is no message, we must call display_echo_area_1
10487 nevertheless because it resizes the window. But we will have to
10488 reset the echo_area_buffer in question to nil at the end because
10489 with_echo_area_buffer will sets it to an empty buffer. */
10490 i = display_last_displayed_message_p ? 1 : 0;
10491 no_message_p = NILP (echo_area_buffer[i]);
10493 window_height_changed_p
10494 = with_echo_area_buffer (w, display_last_displayed_message_p,
10495 display_echo_area_1,
10496 (intptr_t) w, Qnil);
10498 if (no_message_p)
10499 echo_area_buffer[i] = Qnil;
10501 unbind_to (count, Qnil);
10502 return window_height_changed_p;
10506 /* Helper for display_echo_area. Display the current buffer which
10507 contains the current echo area message in window W, a mini-window,
10508 a pointer to which is passed in A1. A2..A4 are currently not used.
10509 Change the height of W so that all of the message is displayed.
10510 Value is non-zero if height of W was changed. */
10512 static int
10513 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10515 intptr_t i1 = a1;
10516 struct window *w = (struct window *) i1;
10517 Lisp_Object window;
10518 struct text_pos start;
10519 int window_height_changed_p = 0;
10521 /* Do this before displaying, so that we have a large enough glyph
10522 matrix for the display. If we can't get enough space for the
10523 whole text, display the last N lines. That works by setting w->start. */
10524 window_height_changed_p = resize_mini_window (w, 0);
10526 /* Use the starting position chosen by resize_mini_window. */
10527 SET_TEXT_POS_FROM_MARKER (start, w->start);
10529 /* Display. */
10530 clear_glyph_matrix (w->desired_matrix);
10531 XSETWINDOW (window, w);
10532 try_window (window, start, 0);
10534 return window_height_changed_p;
10538 /* Resize the echo area window to exactly the size needed for the
10539 currently displayed message, if there is one. If a mini-buffer
10540 is active, don't shrink it. */
10542 void
10543 resize_echo_area_exactly (void)
10545 if (BUFFERP (echo_area_buffer[0])
10546 && WINDOWP (echo_area_window))
10548 struct window *w = XWINDOW (echo_area_window);
10549 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10550 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10551 (intptr_t) w, resize_exactly);
10552 if (resized_p)
10554 windows_or_buffers_changed = 42;
10555 update_mode_lines = 30;
10556 redisplay_internal ();
10562 /* Callback function for with_echo_area_buffer, when used from
10563 resize_echo_area_exactly. A1 contains a pointer to the window to
10564 resize, EXACTLY non-nil means resize the mini-window exactly to the
10565 size of the text displayed. A3 and A4 are not used. Value is what
10566 resize_mini_window returns. */
10568 static int
10569 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10571 intptr_t i1 = a1;
10572 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10576 /* Resize mini-window W to fit the size of its contents. EXACT_P
10577 means size the window exactly to the size needed. Otherwise, it's
10578 only enlarged until W's buffer is empty.
10580 Set W->start to the right place to begin display. If the whole
10581 contents fit, start at the beginning. Otherwise, start so as
10582 to make the end of the contents appear. This is particularly
10583 important for y-or-n-p, but seems desirable generally.
10585 Value is non-zero if the window height has been changed. */
10588 resize_mini_window (struct window *w, int exact_p)
10590 struct frame *f = XFRAME (w->frame);
10591 int window_height_changed_p = 0;
10593 eassert (MINI_WINDOW_P (w));
10595 /* By default, start display at the beginning. */
10596 set_marker_both (w->start, w->contents,
10597 BUF_BEGV (XBUFFER (w->contents)),
10598 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10600 /* Don't resize windows while redisplaying a window; it would
10601 confuse redisplay functions when the size of the window they are
10602 displaying changes from under them. Such a resizing can happen,
10603 for instance, when which-func prints a long message while
10604 we are running fontification-functions. We're running these
10605 functions with safe_call which binds inhibit-redisplay to t. */
10606 if (!NILP (Vinhibit_redisplay))
10607 return 0;
10609 /* Nil means don't try to resize. */
10610 if (NILP (Vresize_mini_windows)
10611 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10612 return 0;
10614 if (!FRAME_MINIBUF_ONLY_P (f))
10616 struct it it;
10617 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10618 + WINDOW_PIXEL_HEIGHT (w));
10619 int unit = FRAME_LINE_HEIGHT (f);
10620 int height, max_height;
10621 struct text_pos start;
10622 struct buffer *old_current_buffer = NULL;
10624 if (current_buffer != XBUFFER (w->contents))
10626 old_current_buffer = current_buffer;
10627 set_buffer_internal (XBUFFER (w->contents));
10630 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10632 /* Compute the max. number of lines specified by the user. */
10633 if (FLOATP (Vmax_mini_window_height))
10634 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10635 else if (INTEGERP (Vmax_mini_window_height))
10636 max_height = XINT (Vmax_mini_window_height) * unit;
10637 else
10638 max_height = total_height / 4;
10640 /* Correct that max. height if it's bogus. */
10641 max_height = clip_to_bounds (unit, max_height, total_height);
10643 /* Find out the height of the text in the window. */
10644 if (it.line_wrap == TRUNCATE)
10645 height = unit;
10646 else
10648 last_height = 0;
10649 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10650 if (it.max_ascent == 0 && it.max_descent == 0)
10651 height = it.current_y + last_height;
10652 else
10653 height = it.current_y + it.max_ascent + it.max_descent;
10654 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10657 /* Compute a suitable window start. */
10658 if (height > max_height)
10660 height = max_height;
10661 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10662 move_it_vertically_backward (&it, height);
10663 start = it.current.pos;
10665 else
10666 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10667 SET_MARKER_FROM_TEXT_POS (w->start, start);
10669 if (EQ (Vresize_mini_windows, Qgrow_only))
10671 /* Let it grow only, until we display an empty message, in which
10672 case the window shrinks again. */
10673 if (height > WINDOW_PIXEL_HEIGHT (w))
10675 int old_height = WINDOW_PIXEL_HEIGHT (w);
10677 FRAME_WINDOWS_FROZEN (f) = 1;
10678 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10679 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10681 else if (height < WINDOW_PIXEL_HEIGHT (w)
10682 && (exact_p || BEGV == ZV))
10684 int old_height = WINDOW_PIXEL_HEIGHT (w);
10686 FRAME_WINDOWS_FROZEN (f) = 0;
10687 shrink_mini_window (w, 1);
10688 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10691 else
10693 /* Always resize to exact size needed. */
10694 if (height > WINDOW_PIXEL_HEIGHT (w))
10696 int old_height = WINDOW_PIXEL_HEIGHT (w);
10698 FRAME_WINDOWS_FROZEN (f) = 1;
10699 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10700 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10702 else if (height < WINDOW_PIXEL_HEIGHT (w))
10704 int old_height = WINDOW_PIXEL_HEIGHT (w);
10706 FRAME_WINDOWS_FROZEN (f) = 0;
10707 shrink_mini_window (w, 1);
10709 if (height)
10711 FRAME_WINDOWS_FROZEN (f) = 1;
10712 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10715 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10719 if (old_current_buffer)
10720 set_buffer_internal (old_current_buffer);
10723 return window_height_changed_p;
10727 /* Value is the current message, a string, or nil if there is no
10728 current message. */
10730 Lisp_Object
10731 current_message (void)
10733 Lisp_Object msg;
10735 if (!BUFFERP (echo_area_buffer[0]))
10736 msg = Qnil;
10737 else
10739 with_echo_area_buffer (0, 0, current_message_1,
10740 (intptr_t) &msg, Qnil);
10741 if (NILP (msg))
10742 echo_area_buffer[0] = Qnil;
10745 return msg;
10749 static int
10750 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10752 intptr_t i1 = a1;
10753 Lisp_Object *msg = (Lisp_Object *) i1;
10755 if (Z > BEG)
10756 *msg = make_buffer_string (BEG, Z, 1);
10757 else
10758 *msg = Qnil;
10759 return 0;
10763 /* Push the current message on Vmessage_stack for later restoration
10764 by restore_message. Value is non-zero if the current message isn't
10765 empty. This is a relatively infrequent operation, so it's not
10766 worth optimizing. */
10768 bool
10769 push_message (void)
10771 Lisp_Object msg = current_message ();
10772 Vmessage_stack = Fcons (msg, Vmessage_stack);
10773 return STRINGP (msg);
10777 /* Restore message display from the top of Vmessage_stack. */
10779 void
10780 restore_message (void)
10782 eassert (CONSP (Vmessage_stack));
10783 message3_nolog (XCAR (Vmessage_stack));
10787 /* Handler for unwind-protect calling pop_message. */
10789 void
10790 pop_message_unwind (void)
10792 /* Pop the top-most entry off Vmessage_stack. */
10793 eassert (CONSP (Vmessage_stack));
10794 Vmessage_stack = XCDR (Vmessage_stack);
10798 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10799 exits. If the stack is not empty, we have a missing pop_message
10800 somewhere. */
10802 void
10803 check_message_stack (void)
10805 if (!NILP (Vmessage_stack))
10806 emacs_abort ();
10810 /* Truncate to NCHARS what will be displayed in the echo area the next
10811 time we display it---but don't redisplay it now. */
10813 void
10814 truncate_echo_area (ptrdiff_t nchars)
10816 if (nchars == 0)
10817 echo_area_buffer[0] = Qnil;
10818 else if (!noninteractive
10819 && INTERACTIVE
10820 && !NILP (echo_area_buffer[0]))
10822 struct frame *sf = SELECTED_FRAME ();
10823 /* Error messages get reported properly by cmd_error, so this must be
10824 just an informative message; if the frame hasn't really been
10825 initialized yet, just toss it. */
10826 if (sf->glyphs_initialized_p)
10827 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10832 /* Helper function for truncate_echo_area. Truncate the current
10833 message to at most NCHARS characters. */
10835 static int
10836 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10838 if (BEG + nchars < Z)
10839 del_range (BEG + nchars, Z);
10840 if (Z == BEG)
10841 echo_area_buffer[0] = Qnil;
10842 return 0;
10845 /* Set the current message to STRING. */
10847 static void
10848 set_message (Lisp_Object string)
10850 eassert (STRINGP (string));
10852 message_enable_multibyte = STRING_MULTIBYTE (string);
10854 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10855 message_buf_print = 0;
10856 help_echo_showing_p = 0;
10858 if (STRINGP (Vdebug_on_message)
10859 && STRINGP (string)
10860 && fast_string_match (Vdebug_on_message, string) >= 0)
10861 call_debugger (list2 (Qerror, string));
10865 /* Helper function for set_message. First argument is ignored and second
10866 argument has the same meaning as for set_message.
10867 This function is called with the echo area buffer being current. */
10869 static int
10870 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10872 eassert (STRINGP (string));
10874 /* Change multibyteness of the echo buffer appropriately. */
10875 if (message_enable_multibyte
10876 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10877 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10879 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10880 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10881 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10883 /* Insert new message at BEG. */
10884 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10886 /* This function takes care of single/multibyte conversion.
10887 We just have to ensure that the echo area buffer has the right
10888 setting of enable_multibyte_characters. */
10889 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10891 return 0;
10895 /* Clear messages. CURRENT_P non-zero means clear the current
10896 message. LAST_DISPLAYED_P non-zero means clear the message
10897 last displayed. */
10899 void
10900 clear_message (bool current_p, bool last_displayed_p)
10902 if (current_p)
10904 echo_area_buffer[0] = Qnil;
10905 message_cleared_p = true;
10908 if (last_displayed_p)
10909 echo_area_buffer[1] = Qnil;
10911 message_buf_print = 0;
10914 /* Clear garbaged frames.
10916 This function is used where the old redisplay called
10917 redraw_garbaged_frames which in turn called redraw_frame which in
10918 turn called clear_frame. The call to clear_frame was a source of
10919 flickering. I believe a clear_frame is not necessary. It should
10920 suffice in the new redisplay to invalidate all current matrices,
10921 and ensure a complete redisplay of all windows. */
10923 static void
10924 clear_garbaged_frames (void)
10926 if (frame_garbaged)
10928 Lisp_Object tail, frame;
10930 FOR_EACH_FRAME (tail, frame)
10932 struct frame *f = XFRAME (frame);
10934 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10936 if (f->resized_p)
10937 redraw_frame (f);
10938 else
10939 clear_current_matrices (f);
10940 fset_redisplay (f);
10941 f->garbaged = false;
10942 f->resized_p = false;
10946 frame_garbaged = false;
10951 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10952 is non-zero update selected_frame. Value is non-zero if the
10953 mini-windows height has been changed. */
10955 static int
10956 echo_area_display (int update_frame_p)
10958 Lisp_Object mini_window;
10959 struct window *w;
10960 struct frame *f;
10961 int window_height_changed_p = 0;
10962 struct frame *sf = SELECTED_FRAME ();
10964 mini_window = FRAME_MINIBUF_WINDOW (sf);
10965 w = XWINDOW (mini_window);
10966 f = XFRAME (WINDOW_FRAME (w));
10968 /* Don't display if frame is invisible or not yet initialized. */
10969 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10970 return 0;
10972 #ifdef HAVE_WINDOW_SYSTEM
10973 /* When Emacs starts, selected_frame may be the initial terminal
10974 frame. If we let this through, a message would be displayed on
10975 the terminal. */
10976 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10977 return 0;
10978 #endif /* HAVE_WINDOW_SYSTEM */
10980 /* Redraw garbaged frames. */
10981 clear_garbaged_frames ();
10983 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10985 echo_area_window = mini_window;
10986 window_height_changed_p = display_echo_area (w);
10987 w->must_be_updated_p = true;
10989 /* Update the display, unless called from redisplay_internal.
10990 Also don't update the screen during redisplay itself. The
10991 update will happen at the end of redisplay, and an update
10992 here could cause confusion. */
10993 if (update_frame_p && !redisplaying_p)
10995 int n = 0;
10997 /* If the display update has been interrupted by pending
10998 input, update mode lines in the frame. Due to the
10999 pending input, it might have been that redisplay hasn't
11000 been called, so that mode lines above the echo area are
11001 garbaged. This looks odd, so we prevent it here. */
11002 if (!display_completed)
11003 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11005 if (window_height_changed_p
11006 /* Don't do this if Emacs is shutting down. Redisplay
11007 needs to run hooks. */
11008 && !NILP (Vrun_hooks))
11010 /* Must update other windows. Likewise as in other
11011 cases, don't let this update be interrupted by
11012 pending input. */
11013 ptrdiff_t count = SPECPDL_INDEX ();
11014 specbind (Qredisplay_dont_pause, Qt);
11015 windows_or_buffers_changed = 44;
11016 redisplay_internal ();
11017 unbind_to (count, Qnil);
11019 else if (FRAME_WINDOW_P (f) && n == 0)
11021 /* Window configuration is the same as before.
11022 Can do with a display update of the echo area,
11023 unless we displayed some mode lines. */
11024 update_single_window (w, 1);
11025 flush_frame (f);
11027 else
11028 update_frame (f, 1, 1);
11030 /* If cursor is in the echo area, make sure that the next
11031 redisplay displays the minibuffer, so that the cursor will
11032 be replaced with what the minibuffer wants. */
11033 if (cursor_in_echo_area)
11034 wset_redisplay (XWINDOW (mini_window));
11037 else if (!EQ (mini_window, selected_window))
11038 wset_redisplay (XWINDOW (mini_window));
11040 /* Last displayed message is now the current message. */
11041 echo_area_buffer[1] = echo_area_buffer[0];
11042 /* Inform read_char that we're not echoing. */
11043 echo_message_buffer = Qnil;
11045 /* Prevent redisplay optimization in redisplay_internal by resetting
11046 this_line_start_pos. This is done because the mini-buffer now
11047 displays the message instead of its buffer text. */
11048 if (EQ (mini_window, selected_window))
11049 CHARPOS (this_line_start_pos) = 0;
11051 return window_height_changed_p;
11054 /* Nonzero if W's buffer was changed but not saved. */
11056 static int
11057 window_buffer_changed (struct window *w)
11059 struct buffer *b = XBUFFER (w->contents);
11061 eassert (BUFFER_LIVE_P (b));
11063 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11066 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11068 static int
11069 mode_line_update_needed (struct window *w)
11071 return (w->column_number_displayed != -1
11072 && !(PT == w->last_point && !window_outdated (w))
11073 && (w->column_number_displayed != current_column ()));
11076 /* Nonzero if window start of W is frozen and may not be changed during
11077 redisplay. */
11079 static bool
11080 window_frozen_p (struct window *w)
11082 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11084 Lisp_Object window;
11086 XSETWINDOW (window, w);
11087 if (MINI_WINDOW_P (w))
11088 return 0;
11089 else if (EQ (window, selected_window))
11090 return 0;
11091 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11092 && EQ (window, Vminibuf_scroll_window))
11093 /* This special window can't be frozen too. */
11094 return 0;
11095 else
11096 return 1;
11098 return 0;
11101 /***********************************************************************
11102 Mode Lines and Frame Titles
11103 ***********************************************************************/
11105 /* A buffer for constructing non-propertized mode-line strings and
11106 frame titles in it; allocated from the heap in init_xdisp and
11107 resized as needed in store_mode_line_noprop_char. */
11109 static char *mode_line_noprop_buf;
11111 /* The buffer's end, and a current output position in it. */
11113 static char *mode_line_noprop_buf_end;
11114 static char *mode_line_noprop_ptr;
11116 #define MODE_LINE_NOPROP_LEN(start) \
11117 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11119 static enum {
11120 MODE_LINE_DISPLAY = 0,
11121 MODE_LINE_TITLE,
11122 MODE_LINE_NOPROP,
11123 MODE_LINE_STRING
11124 } mode_line_target;
11126 /* Alist that caches the results of :propertize.
11127 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11128 static Lisp_Object mode_line_proptrans_alist;
11130 /* List of strings making up the mode-line. */
11131 static Lisp_Object mode_line_string_list;
11133 /* Base face property when building propertized mode line string. */
11134 static Lisp_Object mode_line_string_face;
11135 static Lisp_Object mode_line_string_face_prop;
11138 /* Unwind data for mode line strings */
11140 static Lisp_Object Vmode_line_unwind_vector;
11142 static Lisp_Object
11143 format_mode_line_unwind_data (struct frame *target_frame,
11144 struct buffer *obuf,
11145 Lisp_Object owin,
11146 int save_proptrans)
11148 Lisp_Object vector, tmp;
11150 /* Reduce consing by keeping one vector in
11151 Vwith_echo_area_save_vector. */
11152 vector = Vmode_line_unwind_vector;
11153 Vmode_line_unwind_vector = Qnil;
11155 if (NILP (vector))
11156 vector = Fmake_vector (make_number (10), Qnil);
11158 ASET (vector, 0, make_number (mode_line_target));
11159 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11160 ASET (vector, 2, mode_line_string_list);
11161 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11162 ASET (vector, 4, mode_line_string_face);
11163 ASET (vector, 5, mode_line_string_face_prop);
11165 if (obuf)
11166 XSETBUFFER (tmp, obuf);
11167 else
11168 tmp = Qnil;
11169 ASET (vector, 6, tmp);
11170 ASET (vector, 7, owin);
11171 if (target_frame)
11173 /* Similarly to `with-selected-window', if the operation selects
11174 a window on another frame, we must restore that frame's
11175 selected window, and (for a tty) the top-frame. */
11176 ASET (vector, 8, target_frame->selected_window);
11177 if (FRAME_TERMCAP_P (target_frame))
11178 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11181 return vector;
11184 static void
11185 unwind_format_mode_line (Lisp_Object vector)
11187 Lisp_Object old_window = AREF (vector, 7);
11188 Lisp_Object target_frame_window = AREF (vector, 8);
11189 Lisp_Object old_top_frame = AREF (vector, 9);
11191 mode_line_target = XINT (AREF (vector, 0));
11192 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11193 mode_line_string_list = AREF (vector, 2);
11194 if (! EQ (AREF (vector, 3), Qt))
11195 mode_line_proptrans_alist = AREF (vector, 3);
11196 mode_line_string_face = AREF (vector, 4);
11197 mode_line_string_face_prop = AREF (vector, 5);
11199 /* Select window before buffer, since it may change the buffer. */
11200 if (!NILP (old_window))
11202 /* If the operation that we are unwinding had selected a window
11203 on a different frame, reset its frame-selected-window. For a
11204 text terminal, reset its top-frame if necessary. */
11205 if (!NILP (target_frame_window))
11207 Lisp_Object frame
11208 = WINDOW_FRAME (XWINDOW (target_frame_window));
11210 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11211 Fselect_window (target_frame_window, Qt);
11213 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11214 Fselect_frame (old_top_frame, Qt);
11217 Fselect_window (old_window, Qt);
11220 if (!NILP (AREF (vector, 6)))
11222 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11223 ASET (vector, 6, Qnil);
11226 Vmode_line_unwind_vector = vector;
11230 /* Store a single character C for the frame title in mode_line_noprop_buf.
11231 Re-allocate mode_line_noprop_buf if necessary. */
11233 static void
11234 store_mode_line_noprop_char (char c)
11236 /* If output position has reached the end of the allocated buffer,
11237 increase the buffer's size. */
11238 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11240 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11241 ptrdiff_t size = len;
11242 mode_line_noprop_buf =
11243 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11244 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11245 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11248 *mode_line_noprop_ptr++ = c;
11252 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11253 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11254 characters that yield more columns than PRECISION; PRECISION <= 0
11255 means copy the whole string. Pad with spaces until FIELD_WIDTH
11256 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11257 pad. Called from display_mode_element when it is used to build a
11258 frame title. */
11260 static int
11261 store_mode_line_noprop (const char *string, int field_width, int precision)
11263 const unsigned char *str = (const unsigned char *) string;
11264 int n = 0;
11265 ptrdiff_t dummy, nbytes;
11267 /* Copy at most PRECISION chars from STR. */
11268 nbytes = strlen (string);
11269 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11270 while (nbytes--)
11271 store_mode_line_noprop_char (*str++);
11273 /* Fill up with spaces until FIELD_WIDTH reached. */
11274 while (field_width > 0
11275 && n < field_width)
11277 store_mode_line_noprop_char (' ');
11278 ++n;
11281 return n;
11284 /***********************************************************************
11285 Frame Titles
11286 ***********************************************************************/
11288 #ifdef HAVE_WINDOW_SYSTEM
11290 /* Set the title of FRAME, if it has changed. The title format is
11291 Vicon_title_format if FRAME is iconified, otherwise it is
11292 frame_title_format. */
11294 static void
11295 x_consider_frame_title (Lisp_Object frame)
11297 struct frame *f = XFRAME (frame);
11299 if (FRAME_WINDOW_P (f)
11300 || FRAME_MINIBUF_ONLY_P (f)
11301 || f->explicit_name)
11303 /* Do we have more than one visible frame on this X display? */
11304 Lisp_Object tail, other_frame, fmt;
11305 ptrdiff_t title_start;
11306 char *title;
11307 ptrdiff_t len;
11308 struct it it;
11309 ptrdiff_t count = SPECPDL_INDEX ();
11311 FOR_EACH_FRAME (tail, other_frame)
11313 struct frame *tf = XFRAME (other_frame);
11315 if (tf != f
11316 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11317 && !FRAME_MINIBUF_ONLY_P (tf)
11318 && !EQ (other_frame, tip_frame)
11319 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11320 break;
11323 /* Set global variable indicating that multiple frames exist. */
11324 multiple_frames = CONSP (tail);
11326 /* Switch to the buffer of selected window of the frame. Set up
11327 mode_line_target so that display_mode_element will output into
11328 mode_line_noprop_buf; then display the title. */
11329 record_unwind_protect (unwind_format_mode_line,
11330 format_mode_line_unwind_data
11331 (f, current_buffer, selected_window, 0));
11333 Fselect_window (f->selected_window, Qt);
11334 set_buffer_internal_1
11335 (XBUFFER (XWINDOW (f->selected_window)->contents));
11336 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11338 mode_line_target = MODE_LINE_TITLE;
11339 title_start = MODE_LINE_NOPROP_LEN (0);
11340 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11341 NULL, DEFAULT_FACE_ID);
11342 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11343 len = MODE_LINE_NOPROP_LEN (title_start);
11344 title = mode_line_noprop_buf + title_start;
11345 unbind_to (count, Qnil);
11347 /* Set the title only if it's changed. This avoids consing in
11348 the common case where it hasn't. (If it turns out that we've
11349 already wasted too much time by walking through the list with
11350 display_mode_element, then we might need to optimize at a
11351 higher level than this.) */
11352 if (! STRINGP (f->name)
11353 || SBYTES (f->name) != len
11354 || memcmp (title, SDATA (f->name), len) != 0)
11355 x_implicitly_set_name (f, make_string (title, len), Qnil);
11359 #endif /* not HAVE_WINDOW_SYSTEM */
11362 /***********************************************************************
11363 Menu Bars
11364 ***********************************************************************/
11366 /* Non-zero if we will not redisplay all visible windows. */
11367 #define REDISPLAY_SOME_P() \
11368 ((windows_or_buffers_changed == 0 \
11369 || windows_or_buffers_changed == REDISPLAY_SOME) \
11370 && (update_mode_lines == 0 \
11371 || update_mode_lines == REDISPLAY_SOME))
11373 /* Prepare for redisplay by updating menu-bar item lists when
11374 appropriate. This can call eval. */
11376 static void
11377 prepare_menu_bars (void)
11379 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11380 bool some_windows = REDISPLAY_SOME_P ();
11381 struct gcpro gcpro1, gcpro2;
11382 Lisp_Object tooltip_frame;
11384 #ifdef HAVE_WINDOW_SYSTEM
11385 tooltip_frame = tip_frame;
11386 #else
11387 tooltip_frame = Qnil;
11388 #endif
11390 if (FUNCTIONP (Vpre_redisplay_function))
11392 Lisp_Object windows = all_windows ? Qt : Qnil;
11393 if (all_windows && some_windows)
11395 Lisp_Object ws = window_list ();
11396 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11398 Lisp_Object this = XCAR (ws);
11399 struct window *w = XWINDOW (this);
11400 if (w->redisplay
11401 || XFRAME (w->frame)->redisplay
11402 || XBUFFER (w->contents)->text->redisplay)
11404 windows = Fcons (this, windows);
11408 safe_call1 (Vpre_redisplay_function, windows);
11411 /* Update all frame titles based on their buffer names, etc. We do
11412 this before the menu bars so that the buffer-menu will show the
11413 up-to-date frame titles. */
11414 #ifdef HAVE_WINDOW_SYSTEM
11415 if (all_windows)
11417 Lisp_Object tail, frame;
11419 FOR_EACH_FRAME (tail, frame)
11421 struct frame *f = XFRAME (frame);
11422 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11423 if (some_windows
11424 && !f->redisplay
11425 && !w->redisplay
11426 && !XBUFFER (w->contents)->text->redisplay)
11427 continue;
11429 if (!EQ (frame, tooltip_frame)
11430 && (FRAME_ICONIFIED_P (f)
11431 || FRAME_VISIBLE_P (f) == 1
11432 /* Exclude TTY frames that are obscured because they
11433 are not the top frame on their console. This is
11434 because x_consider_frame_title actually switches
11435 to the frame, which for TTY frames means it is
11436 marked as garbaged, and will be completely
11437 redrawn on the next redisplay cycle. This causes
11438 TTY frames to be completely redrawn, when there
11439 are more than one of them, even though nothing
11440 should be changed on display. */
11441 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11442 x_consider_frame_title (frame);
11445 #endif /* HAVE_WINDOW_SYSTEM */
11447 /* Update the menu bar item lists, if appropriate. This has to be
11448 done before any actual redisplay or generation of display lines. */
11450 if (all_windows)
11452 Lisp_Object tail, frame;
11453 ptrdiff_t count = SPECPDL_INDEX ();
11454 /* 1 means that update_menu_bar has run its hooks
11455 so any further calls to update_menu_bar shouldn't do so again. */
11456 int menu_bar_hooks_run = 0;
11458 record_unwind_save_match_data ();
11460 FOR_EACH_FRAME (tail, frame)
11462 struct frame *f = XFRAME (frame);
11463 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11465 /* Ignore tooltip frame. */
11466 if (EQ (frame, tooltip_frame))
11467 continue;
11469 if (some_windows
11470 && !f->redisplay
11471 && !w->redisplay
11472 && !XBUFFER (w->contents)->text->redisplay)
11473 continue;
11475 /* If a window on this frame changed size, report that to
11476 the user and clear the size-change flag. */
11477 if (FRAME_WINDOW_SIZES_CHANGED (f))
11479 Lisp_Object functions;
11481 /* Clear flag first in case we get an error below. */
11482 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11483 functions = Vwindow_size_change_functions;
11484 GCPRO2 (tail, functions);
11486 while (CONSP (functions))
11488 if (!EQ (XCAR (functions), Qt))
11489 call1 (XCAR (functions), frame);
11490 functions = XCDR (functions);
11492 UNGCPRO;
11495 GCPRO1 (tail);
11496 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11497 #ifdef HAVE_WINDOW_SYSTEM
11498 update_tool_bar (f, 0);
11499 #endif
11500 #ifdef HAVE_NS
11501 if (windows_or_buffers_changed
11502 && FRAME_NS_P (f))
11503 ns_set_doc_edited
11504 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11505 #endif
11506 UNGCPRO;
11509 unbind_to (count, Qnil);
11511 else
11513 struct frame *sf = SELECTED_FRAME ();
11514 update_menu_bar (sf, 1, 0);
11515 #ifdef HAVE_WINDOW_SYSTEM
11516 update_tool_bar (sf, 1);
11517 #endif
11522 /* Update the menu bar item list for frame F. This has to be done
11523 before we start to fill in any display lines, because it can call
11524 eval.
11526 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11528 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11529 already ran the menu bar hooks for this redisplay, so there
11530 is no need to run them again. The return value is the
11531 updated value of this flag, to pass to the next call. */
11533 static int
11534 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11536 Lisp_Object window;
11537 register struct window *w;
11539 /* If called recursively during a menu update, do nothing. This can
11540 happen when, for instance, an activate-menubar-hook causes a
11541 redisplay. */
11542 if (inhibit_menubar_update)
11543 return hooks_run;
11545 window = FRAME_SELECTED_WINDOW (f);
11546 w = XWINDOW (window);
11548 if (FRAME_WINDOW_P (f)
11550 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11551 || defined (HAVE_NS) || defined (USE_GTK)
11552 FRAME_EXTERNAL_MENU_BAR (f)
11553 #else
11554 FRAME_MENU_BAR_LINES (f) > 0
11555 #endif
11556 : FRAME_MENU_BAR_LINES (f) > 0)
11558 /* If the user has switched buffers or windows, we need to
11559 recompute to reflect the new bindings. But we'll
11560 recompute when update_mode_lines is set too; that means
11561 that people can use force-mode-line-update to request
11562 that the menu bar be recomputed. The adverse effect on
11563 the rest of the redisplay algorithm is about the same as
11564 windows_or_buffers_changed anyway. */
11565 if (windows_or_buffers_changed
11566 /* This used to test w->update_mode_line, but we believe
11567 there is no need to recompute the menu in that case. */
11568 || update_mode_lines
11569 || window_buffer_changed (w))
11571 struct buffer *prev = current_buffer;
11572 ptrdiff_t count = SPECPDL_INDEX ();
11574 specbind (Qinhibit_menubar_update, Qt);
11576 set_buffer_internal_1 (XBUFFER (w->contents));
11577 if (save_match_data)
11578 record_unwind_save_match_data ();
11579 if (NILP (Voverriding_local_map_menu_flag))
11581 specbind (Qoverriding_terminal_local_map, Qnil);
11582 specbind (Qoverriding_local_map, Qnil);
11585 if (!hooks_run)
11587 /* Run the Lucid hook. */
11588 safe_run_hooks (Qactivate_menubar_hook);
11590 /* If it has changed current-menubar from previous value,
11591 really recompute the menu-bar from the value. */
11592 if (! NILP (Vlucid_menu_bar_dirty_flag))
11593 call0 (Qrecompute_lucid_menubar);
11595 safe_run_hooks (Qmenu_bar_update_hook);
11597 hooks_run = 1;
11600 XSETFRAME (Vmenu_updating_frame, f);
11601 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11603 /* Redisplay the menu bar in case we changed it. */
11604 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11605 || defined (HAVE_NS) || defined (USE_GTK)
11606 if (FRAME_WINDOW_P (f))
11608 #if defined (HAVE_NS)
11609 /* All frames on Mac OS share the same menubar. So only
11610 the selected frame should be allowed to set it. */
11611 if (f == SELECTED_FRAME ())
11612 #endif
11613 set_frame_menubar (f, 0, 0);
11615 else
11616 /* On a terminal screen, the menu bar is an ordinary screen
11617 line, and this makes it get updated. */
11618 w->update_mode_line = 1;
11619 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11620 /* In the non-toolkit version, the menu bar is an ordinary screen
11621 line, and this makes it get updated. */
11622 w->update_mode_line = 1;
11623 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11625 unbind_to (count, Qnil);
11626 set_buffer_internal_1 (prev);
11630 return hooks_run;
11633 /***********************************************************************
11634 Tool-bars
11635 ***********************************************************************/
11637 #ifdef HAVE_WINDOW_SYSTEM
11639 /* Tool-bar item index of the item on which a mouse button was pressed
11640 or -1. */
11642 int last_tool_bar_item;
11644 /* Select `frame' temporarily without running all the code in
11645 do_switch_frame.
11646 FIXME: Maybe do_switch_frame should be trimmed down similarly
11647 when `norecord' is set. */
11648 static void
11649 fast_set_selected_frame (Lisp_Object frame)
11651 if (!EQ (selected_frame, frame))
11653 selected_frame = frame;
11654 selected_window = XFRAME (frame)->selected_window;
11658 /* Update the tool-bar item list for frame F. This has to be done
11659 before we start to fill in any display lines. Called from
11660 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11661 and restore it here. */
11663 static void
11664 update_tool_bar (struct frame *f, int save_match_data)
11666 #if defined (USE_GTK) || defined (HAVE_NS)
11667 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11668 #else
11669 int do_update = (WINDOWP (f->tool_bar_window)
11670 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11671 #endif
11673 if (do_update)
11675 Lisp_Object window;
11676 struct window *w;
11678 window = FRAME_SELECTED_WINDOW (f);
11679 w = XWINDOW (window);
11681 /* If the user has switched buffers or windows, we need to
11682 recompute to reflect the new bindings. But we'll
11683 recompute when update_mode_lines is set too; that means
11684 that people can use force-mode-line-update to request
11685 that the menu bar be recomputed. The adverse effect on
11686 the rest of the redisplay algorithm is about the same as
11687 windows_or_buffers_changed anyway. */
11688 if (windows_or_buffers_changed
11689 || w->update_mode_line
11690 || update_mode_lines
11691 || window_buffer_changed (w))
11693 struct buffer *prev = current_buffer;
11694 ptrdiff_t count = SPECPDL_INDEX ();
11695 Lisp_Object frame, new_tool_bar;
11696 int new_n_tool_bar;
11697 struct gcpro gcpro1;
11699 /* Set current_buffer to the buffer of the selected
11700 window of the frame, so that we get the right local
11701 keymaps. */
11702 set_buffer_internal_1 (XBUFFER (w->contents));
11704 /* Save match data, if we must. */
11705 if (save_match_data)
11706 record_unwind_save_match_data ();
11708 /* Make sure that we don't accidentally use bogus keymaps. */
11709 if (NILP (Voverriding_local_map_menu_flag))
11711 specbind (Qoverriding_terminal_local_map, Qnil);
11712 specbind (Qoverriding_local_map, Qnil);
11715 GCPRO1 (new_tool_bar);
11717 /* We must temporarily set the selected frame to this frame
11718 before calling tool_bar_items, because the calculation of
11719 the tool-bar keymap uses the selected frame (see
11720 `tool-bar-make-keymap' in tool-bar.el). */
11721 eassert (EQ (selected_window,
11722 /* Since we only explicitly preserve selected_frame,
11723 check that selected_window would be redundant. */
11724 XFRAME (selected_frame)->selected_window));
11725 record_unwind_protect (fast_set_selected_frame, selected_frame);
11726 XSETFRAME (frame, f);
11727 fast_set_selected_frame (frame);
11729 /* Build desired tool-bar items from keymaps. */
11730 new_tool_bar
11731 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11732 &new_n_tool_bar);
11734 /* Redisplay the tool-bar if we changed it. */
11735 if (new_n_tool_bar != f->n_tool_bar_items
11736 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11738 /* Redisplay that happens asynchronously due to an expose event
11739 may access f->tool_bar_items. Make sure we update both
11740 variables within BLOCK_INPUT so no such event interrupts. */
11741 block_input ();
11742 fset_tool_bar_items (f, new_tool_bar);
11743 f->n_tool_bar_items = new_n_tool_bar;
11744 w->update_mode_line = 1;
11745 unblock_input ();
11748 UNGCPRO;
11750 unbind_to (count, Qnil);
11751 set_buffer_internal_1 (prev);
11756 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11758 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11759 F's desired tool-bar contents. F->tool_bar_items must have
11760 been set up previously by calling prepare_menu_bars. */
11762 static void
11763 build_desired_tool_bar_string (struct frame *f)
11765 int i, size, size_needed;
11766 struct gcpro gcpro1, gcpro2, gcpro3;
11767 Lisp_Object image, plist, props;
11769 image = plist = props = Qnil;
11770 GCPRO3 (image, plist, props);
11772 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11773 Otherwise, make a new string. */
11775 /* The size of the string we might be able to reuse. */
11776 size = (STRINGP (f->desired_tool_bar_string)
11777 ? SCHARS (f->desired_tool_bar_string)
11778 : 0);
11780 /* We need one space in the string for each image. */
11781 size_needed = f->n_tool_bar_items;
11783 /* Reuse f->desired_tool_bar_string, if possible. */
11784 if (size < size_needed || NILP (f->desired_tool_bar_string))
11785 fset_desired_tool_bar_string
11786 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11787 else
11789 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11790 Fremove_text_properties (make_number (0), make_number (size),
11791 props, f->desired_tool_bar_string);
11794 /* Put a `display' property on the string for the images to display,
11795 put a `menu_item' property on tool-bar items with a value that
11796 is the index of the item in F's tool-bar item vector. */
11797 for (i = 0; i < f->n_tool_bar_items; ++i)
11799 #define PROP(IDX) \
11800 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11802 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11803 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11804 int hmargin, vmargin, relief, idx, end;
11806 /* If image is a vector, choose the image according to the
11807 button state. */
11808 image = PROP (TOOL_BAR_ITEM_IMAGES);
11809 if (VECTORP (image))
11811 if (enabled_p)
11812 idx = (selected_p
11813 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11814 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11815 else
11816 idx = (selected_p
11817 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11818 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11820 eassert (ASIZE (image) >= idx);
11821 image = AREF (image, idx);
11823 else
11824 idx = -1;
11826 /* Ignore invalid image specifications. */
11827 if (!valid_image_p (image))
11828 continue;
11830 /* Display the tool-bar button pressed, or depressed. */
11831 plist = Fcopy_sequence (XCDR (image));
11833 /* Compute margin and relief to draw. */
11834 relief = (tool_bar_button_relief >= 0
11835 ? tool_bar_button_relief
11836 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11837 hmargin = vmargin = relief;
11839 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11840 INT_MAX - max (hmargin, vmargin)))
11842 hmargin += XFASTINT (Vtool_bar_button_margin);
11843 vmargin += XFASTINT (Vtool_bar_button_margin);
11845 else if (CONSP (Vtool_bar_button_margin))
11847 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11848 INT_MAX - hmargin))
11849 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11851 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11852 INT_MAX - vmargin))
11853 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11856 if (auto_raise_tool_bar_buttons_p)
11858 /* Add a `:relief' property to the image spec if the item is
11859 selected. */
11860 if (selected_p)
11862 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11863 hmargin -= relief;
11864 vmargin -= relief;
11867 else
11869 /* If image is selected, display it pressed, i.e. with a
11870 negative relief. If it's not selected, display it with a
11871 raised relief. */
11872 plist = Fplist_put (plist, QCrelief,
11873 (selected_p
11874 ? make_number (-relief)
11875 : make_number (relief)));
11876 hmargin -= relief;
11877 vmargin -= relief;
11880 /* Put a margin around the image. */
11881 if (hmargin || vmargin)
11883 if (hmargin == vmargin)
11884 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11885 else
11886 plist = Fplist_put (plist, QCmargin,
11887 Fcons (make_number (hmargin),
11888 make_number (vmargin)));
11891 /* If button is not enabled, and we don't have special images
11892 for the disabled state, make the image appear disabled by
11893 applying an appropriate algorithm to it. */
11894 if (!enabled_p && idx < 0)
11895 plist = Fplist_put (plist, QCconversion, Qdisabled);
11897 /* Put a `display' text property on the string for the image to
11898 display. Put a `menu-item' property on the string that gives
11899 the start of this item's properties in the tool-bar items
11900 vector. */
11901 image = Fcons (Qimage, plist);
11902 props = list4 (Qdisplay, image,
11903 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11905 /* Let the last image hide all remaining spaces in the tool bar
11906 string. The string can be longer than needed when we reuse a
11907 previous string. */
11908 if (i + 1 == f->n_tool_bar_items)
11909 end = SCHARS (f->desired_tool_bar_string);
11910 else
11911 end = i + 1;
11912 Fadd_text_properties (make_number (i), make_number (end),
11913 props, f->desired_tool_bar_string);
11914 #undef PROP
11917 UNGCPRO;
11921 /* Display one line of the tool-bar of frame IT->f.
11923 HEIGHT specifies the desired height of the tool-bar line.
11924 If the actual height of the glyph row is less than HEIGHT, the
11925 row's height is increased to HEIGHT, and the icons are centered
11926 vertically in the new height.
11928 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11929 count a final empty row in case the tool-bar width exactly matches
11930 the window width.
11933 static void
11934 display_tool_bar_line (struct it *it, int height)
11936 struct glyph_row *row = it->glyph_row;
11937 int max_x = it->last_visible_x;
11938 struct glyph *last;
11940 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11941 clear_glyph_row (row);
11942 row->enabled_p = true;
11943 row->y = it->current_y;
11945 /* Note that this isn't made use of if the face hasn't a box,
11946 so there's no need to check the face here. */
11947 it->start_of_box_run_p = 1;
11949 while (it->current_x < max_x)
11951 int x, n_glyphs_before, i, nglyphs;
11952 struct it it_before;
11954 /* Get the next display element. */
11955 if (!get_next_display_element (it))
11957 /* Don't count empty row if we are counting needed tool-bar lines. */
11958 if (height < 0 && !it->hpos)
11959 return;
11960 break;
11963 /* Produce glyphs. */
11964 n_glyphs_before = row->used[TEXT_AREA];
11965 it_before = *it;
11967 PRODUCE_GLYPHS (it);
11969 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11970 i = 0;
11971 x = it_before.current_x;
11972 while (i < nglyphs)
11974 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11976 if (x + glyph->pixel_width > max_x)
11978 /* Glyph doesn't fit on line. Backtrack. */
11979 row->used[TEXT_AREA] = n_glyphs_before;
11980 *it = it_before;
11981 /* If this is the only glyph on this line, it will never fit on the
11982 tool-bar, so skip it. But ensure there is at least one glyph,
11983 so we don't accidentally disable the tool-bar. */
11984 if (n_glyphs_before == 0
11985 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11986 break;
11987 goto out;
11990 ++it->hpos;
11991 x += glyph->pixel_width;
11992 ++i;
11995 /* Stop at line end. */
11996 if (ITERATOR_AT_END_OF_LINE_P (it))
11997 break;
11999 set_iterator_to_next (it, 1);
12002 out:;
12004 row->displays_text_p = row->used[TEXT_AREA] != 0;
12006 /* Use default face for the border below the tool bar.
12008 FIXME: When auto-resize-tool-bars is grow-only, there is
12009 no additional border below the possibly empty tool-bar lines.
12010 So to make the extra empty lines look "normal", we have to
12011 use the tool-bar face for the border too. */
12012 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12013 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12014 it->face_id = DEFAULT_FACE_ID;
12016 extend_face_to_end_of_line (it);
12017 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12018 last->right_box_line_p = 1;
12019 if (last == row->glyphs[TEXT_AREA])
12020 last->left_box_line_p = 1;
12022 /* Make line the desired height and center it vertically. */
12023 if ((height -= it->max_ascent + it->max_descent) > 0)
12025 /* Don't add more than one line height. */
12026 height %= FRAME_LINE_HEIGHT (it->f);
12027 it->max_ascent += height / 2;
12028 it->max_descent += (height + 1) / 2;
12031 compute_line_metrics (it);
12033 /* If line is empty, make it occupy the rest of the tool-bar. */
12034 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12036 row->height = row->phys_height = it->last_visible_y - row->y;
12037 row->visible_height = row->height;
12038 row->ascent = row->phys_ascent = 0;
12039 row->extra_line_spacing = 0;
12042 row->full_width_p = 1;
12043 row->continued_p = 0;
12044 row->truncated_on_left_p = 0;
12045 row->truncated_on_right_p = 0;
12047 it->current_x = it->hpos = 0;
12048 it->current_y += row->height;
12049 ++it->vpos;
12050 ++it->glyph_row;
12054 /* Max tool-bar height. Basically, this is what makes all other windows
12055 disappear when the frame gets too small. Rethink this! */
12057 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12058 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12060 /* Value is the number of pixels needed to make all tool-bar items of
12061 frame F visible. The actual number of glyph rows needed is
12062 returned in *N_ROWS if non-NULL. */
12064 static int
12065 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12067 struct window *w = XWINDOW (f->tool_bar_window);
12068 struct it it;
12069 /* tool_bar_height is called from redisplay_tool_bar after building
12070 the desired matrix, so use (unused) mode-line row as temporary row to
12071 avoid destroying the first tool-bar row. */
12072 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12074 /* Initialize an iterator for iteration over
12075 F->desired_tool_bar_string in the tool-bar window of frame F. */
12076 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12077 it.first_visible_x = 0;
12078 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12079 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12080 it.paragraph_embedding = L2R;
12082 while (!ITERATOR_AT_END_P (&it))
12084 clear_glyph_row (temp_row);
12085 it.glyph_row = temp_row;
12086 display_tool_bar_line (&it, -1);
12088 clear_glyph_row (temp_row);
12090 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12091 if (n_rows)
12092 *n_rows = it.vpos > 0 ? it.vpos : -1;
12094 if (pixelwise)
12095 return it.current_y;
12096 else
12097 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12100 #endif /* !USE_GTK && !HAVE_NS */
12102 #if defined USE_GTK || defined HAVE_NS
12103 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12104 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12105 #endif
12107 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12108 0, 2, 0,
12109 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12110 If FRAME is nil or omitted, use the selected frame. Optional argument
12111 PIXELWISE non-nil means return the height of the tool bar inpixels. */)
12112 (Lisp_Object frame, Lisp_Object pixelwise)
12114 int height = 0;
12116 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12117 struct frame *f = decode_any_frame (frame);
12119 if (WINDOWP (f->tool_bar_window)
12120 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12122 update_tool_bar (f, 1);
12123 if (f->n_tool_bar_items)
12125 build_desired_tool_bar_string (f);
12126 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12129 #endif
12131 return make_number (height);
12135 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12136 height should be changed. */
12138 static int
12139 redisplay_tool_bar (struct frame *f)
12141 #if defined (USE_GTK) || defined (HAVE_NS)
12143 if (FRAME_EXTERNAL_TOOL_BAR (f))
12144 update_frame_tool_bar (f);
12145 return 0;
12147 #else /* !USE_GTK && !HAVE_NS */
12149 struct window *w;
12150 struct it it;
12151 struct glyph_row *row;
12153 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12154 do anything. This means you must start with tool-bar-lines
12155 non-zero to get the auto-sizing effect. Or in other words, you
12156 can turn off tool-bars by specifying tool-bar-lines zero. */
12157 if (!WINDOWP (f->tool_bar_window)
12158 || (w = XWINDOW (f->tool_bar_window),
12159 WINDOW_PIXEL_HEIGHT (w) == 0))
12160 return 0;
12162 /* Set up an iterator for the tool-bar window. */
12163 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12164 it.first_visible_x = 0;
12165 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12166 row = it.glyph_row;
12168 /* Build a string that represents the contents of the tool-bar. */
12169 build_desired_tool_bar_string (f);
12170 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12171 /* FIXME: This should be controlled by a user option. But it
12172 doesn't make sense to have an R2L tool bar if the menu bar cannot
12173 be drawn also R2L, and making the menu bar R2L is tricky due
12174 toolkit-specific code that implements it. If an R2L tool bar is
12175 ever supported, display_tool_bar_line should also be augmented to
12176 call unproduce_glyphs like display_line and display_string
12177 do. */
12178 it.paragraph_embedding = L2R;
12180 if (f->n_tool_bar_rows == 0)
12182 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12184 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12186 Lisp_Object frame;
12187 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12188 / FRAME_LINE_HEIGHT (f));
12190 XSETFRAME (frame, f);
12191 Fmodify_frame_parameters (frame,
12192 list1 (Fcons (Qtool_bar_lines,
12193 make_number (new_lines))));
12194 /* Always do that now. */
12195 clear_glyph_matrix (w->desired_matrix);
12196 f->fonts_changed = 1;
12197 return 1;
12201 /* Display as many lines as needed to display all tool-bar items. */
12203 if (f->n_tool_bar_rows > 0)
12205 int border, rows, height, extra;
12207 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12208 border = XINT (Vtool_bar_border);
12209 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12210 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12211 else if (EQ (Vtool_bar_border, Qborder_width))
12212 border = f->border_width;
12213 else
12214 border = 0;
12215 if (border < 0)
12216 border = 0;
12218 rows = f->n_tool_bar_rows;
12219 height = max (1, (it.last_visible_y - border) / rows);
12220 extra = it.last_visible_y - border - height * rows;
12222 while (it.current_y < it.last_visible_y)
12224 int h = 0;
12225 if (extra > 0 && rows-- > 0)
12227 h = (extra + rows - 1) / rows;
12228 extra -= h;
12230 display_tool_bar_line (&it, height + h);
12233 else
12235 while (it.current_y < it.last_visible_y)
12236 display_tool_bar_line (&it, 0);
12239 /* It doesn't make much sense to try scrolling in the tool-bar
12240 window, so don't do it. */
12241 w->desired_matrix->no_scrolling_p = 1;
12242 w->must_be_updated_p = 1;
12244 if (!NILP (Vauto_resize_tool_bars))
12246 /* Do we really allow the toolbar to occupy the whole frame? */
12247 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12248 int change_height_p = 0;
12250 /* If we couldn't display everything, change the tool-bar's
12251 height if there is room for more. */
12252 if (IT_STRING_CHARPOS (it) < it.end_charpos
12253 && it.current_y < max_tool_bar_height)
12254 change_height_p = 1;
12256 /* We subtract 1 because display_tool_bar_line advances the
12257 glyph_row pointer before returning to its caller. We want to
12258 examine the last glyph row produced by
12259 display_tool_bar_line. */
12260 row = it.glyph_row - 1;
12262 /* If there are blank lines at the end, except for a partially
12263 visible blank line at the end that is smaller than
12264 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12265 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12266 && row->height >= FRAME_LINE_HEIGHT (f))
12267 change_height_p = 1;
12269 /* If row displays tool-bar items, but is partially visible,
12270 change the tool-bar's height. */
12271 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12272 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12273 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12274 change_height_p = 1;
12276 /* Resize windows as needed by changing the `tool-bar-lines'
12277 frame parameter. */
12278 if (change_height_p)
12280 Lisp_Object frame;
12281 int nrows;
12282 int new_height = tool_bar_height (f, &nrows, 1);
12284 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12285 && !f->minimize_tool_bar_window_p)
12286 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12287 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12288 f->minimize_tool_bar_window_p = 0;
12290 if (change_height_p)
12292 /* Current size of the tool-bar window in canonical line
12293 units. */
12294 int old_lines = WINDOW_TOTAL_LINES (w);
12295 /* Required size of the tool-bar window in canonical
12296 line units. */
12297 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12298 / FRAME_LINE_HEIGHT (f));
12299 /* Maximum size of the tool-bar window in canonical line
12300 units that this frame can allow. */
12301 int max_lines =
12302 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12304 /* Don't try to change the tool-bar window size and set
12305 the fonts_changed flag unless really necessary. That
12306 flag causes redisplay to give up and retry
12307 redisplaying the frame from scratch, so setting it
12308 unnecessarily can lead to nasty redisplay loops. */
12309 if (new_lines <= max_lines
12310 && eabs (new_lines - old_lines) >= 1)
12312 XSETFRAME (frame, f);
12313 Fmodify_frame_parameters (frame,
12314 list1 (Fcons (Qtool_bar_lines,
12315 make_number (new_lines))));
12316 clear_glyph_matrix (w->desired_matrix);
12317 f->n_tool_bar_rows = nrows;
12318 f->fonts_changed = 1;
12319 return 1;
12325 f->minimize_tool_bar_window_p = 0;
12326 return 0;
12328 #endif /* USE_GTK || HAVE_NS */
12331 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12333 /* Get information about the tool-bar item which is displayed in GLYPH
12334 on frame F. Return in *PROP_IDX the index where tool-bar item
12335 properties start in F->tool_bar_items. Value is zero if
12336 GLYPH doesn't display a tool-bar item. */
12338 static int
12339 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12341 Lisp_Object prop;
12342 int success_p;
12343 int charpos;
12345 /* This function can be called asynchronously, which means we must
12346 exclude any possibility that Fget_text_property signals an
12347 error. */
12348 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12349 charpos = max (0, charpos);
12351 /* Get the text property `menu-item' at pos. The value of that
12352 property is the start index of this item's properties in
12353 F->tool_bar_items. */
12354 prop = Fget_text_property (make_number (charpos),
12355 Qmenu_item, f->current_tool_bar_string);
12356 if (INTEGERP (prop))
12358 *prop_idx = XINT (prop);
12359 success_p = 1;
12361 else
12362 success_p = 0;
12364 return success_p;
12368 /* Get information about the tool-bar item at position X/Y on frame F.
12369 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12370 the current matrix of the tool-bar window of F, or NULL if not
12371 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12372 item in F->tool_bar_items. Value is
12374 -1 if X/Y is not on a tool-bar item
12375 0 if X/Y is on the same item that was highlighted before.
12376 1 otherwise. */
12378 static int
12379 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12380 int *hpos, int *vpos, int *prop_idx)
12382 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12383 struct window *w = XWINDOW (f->tool_bar_window);
12384 int area;
12386 /* Find the glyph under X/Y. */
12387 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12388 if (*glyph == NULL)
12389 return -1;
12391 /* Get the start of this tool-bar item's properties in
12392 f->tool_bar_items. */
12393 if (!tool_bar_item_info (f, *glyph, prop_idx))
12394 return -1;
12396 /* Is mouse on the highlighted item? */
12397 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12398 && *vpos >= hlinfo->mouse_face_beg_row
12399 && *vpos <= hlinfo->mouse_face_end_row
12400 && (*vpos > hlinfo->mouse_face_beg_row
12401 || *hpos >= hlinfo->mouse_face_beg_col)
12402 && (*vpos < hlinfo->mouse_face_end_row
12403 || *hpos < hlinfo->mouse_face_end_col
12404 || hlinfo->mouse_face_past_end))
12405 return 0;
12407 return 1;
12411 /* EXPORT:
12412 Handle mouse button event on the tool-bar of frame F, at
12413 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12414 0 for button release. MODIFIERS is event modifiers for button
12415 release. */
12417 void
12418 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12419 int modifiers)
12421 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12422 struct window *w = XWINDOW (f->tool_bar_window);
12423 int hpos, vpos, prop_idx;
12424 struct glyph *glyph;
12425 Lisp_Object enabled_p;
12426 int ts;
12428 /* If not on the highlighted tool-bar item, and mouse-highlight is
12429 non-nil, return. This is so we generate the tool-bar button
12430 click only when the mouse button is released on the same item as
12431 where it was pressed. However, when mouse-highlight is disabled,
12432 generate the click when the button is released regardless of the
12433 highlight, since tool-bar items are not highlighted in that
12434 case. */
12435 frame_to_window_pixel_xy (w, &x, &y);
12436 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12437 if (ts == -1
12438 || (ts != 0 && !NILP (Vmouse_highlight)))
12439 return;
12441 /* When mouse-highlight is off, generate the click for the item
12442 where the button was pressed, disregarding where it was
12443 released. */
12444 if (NILP (Vmouse_highlight) && !down_p)
12445 prop_idx = last_tool_bar_item;
12447 /* If item is disabled, do nothing. */
12448 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12449 if (NILP (enabled_p))
12450 return;
12452 if (down_p)
12454 /* Show item in pressed state. */
12455 if (!NILP (Vmouse_highlight))
12456 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12457 last_tool_bar_item = prop_idx;
12459 else
12461 Lisp_Object key, frame;
12462 struct input_event event;
12463 EVENT_INIT (event);
12465 /* Show item in released state. */
12466 if (!NILP (Vmouse_highlight))
12467 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12469 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12471 XSETFRAME (frame, f);
12472 event.kind = TOOL_BAR_EVENT;
12473 event.frame_or_window = frame;
12474 event.arg = frame;
12475 kbd_buffer_store_event (&event);
12477 event.kind = TOOL_BAR_EVENT;
12478 event.frame_or_window = frame;
12479 event.arg = key;
12480 event.modifiers = modifiers;
12481 kbd_buffer_store_event (&event);
12482 last_tool_bar_item = -1;
12487 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12488 tool-bar window-relative coordinates X/Y. Called from
12489 note_mouse_highlight. */
12491 static void
12492 note_tool_bar_highlight (struct frame *f, int x, int y)
12494 Lisp_Object window = f->tool_bar_window;
12495 struct window *w = XWINDOW (window);
12496 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12497 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12498 int hpos, vpos;
12499 struct glyph *glyph;
12500 struct glyph_row *row;
12501 int i;
12502 Lisp_Object enabled_p;
12503 int prop_idx;
12504 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12505 int mouse_down_p, rc;
12507 /* Function note_mouse_highlight is called with negative X/Y
12508 values when mouse moves outside of the frame. */
12509 if (x <= 0 || y <= 0)
12511 clear_mouse_face (hlinfo);
12512 return;
12515 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12516 if (rc < 0)
12518 /* Not on tool-bar item. */
12519 clear_mouse_face (hlinfo);
12520 return;
12522 else if (rc == 0)
12523 /* On same tool-bar item as before. */
12524 goto set_help_echo;
12526 clear_mouse_face (hlinfo);
12528 /* Mouse is down, but on different tool-bar item? */
12529 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12530 && f == dpyinfo->last_mouse_frame);
12532 if (mouse_down_p
12533 && last_tool_bar_item != prop_idx)
12534 return;
12536 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12538 /* If tool-bar item is not enabled, don't highlight it. */
12539 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12540 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12542 /* Compute the x-position of the glyph. In front and past the
12543 image is a space. We include this in the highlighted area. */
12544 row = MATRIX_ROW (w->current_matrix, vpos);
12545 for (i = x = 0; i < hpos; ++i)
12546 x += row->glyphs[TEXT_AREA][i].pixel_width;
12548 /* Record this as the current active region. */
12549 hlinfo->mouse_face_beg_col = hpos;
12550 hlinfo->mouse_face_beg_row = vpos;
12551 hlinfo->mouse_face_beg_x = x;
12552 hlinfo->mouse_face_past_end = 0;
12554 hlinfo->mouse_face_end_col = hpos + 1;
12555 hlinfo->mouse_face_end_row = vpos;
12556 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12557 hlinfo->mouse_face_window = window;
12558 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12560 /* Display it as active. */
12561 show_mouse_face (hlinfo, draw);
12564 set_help_echo:
12566 /* Set help_echo_string to a help string to display for this tool-bar item.
12567 XTread_socket does the rest. */
12568 help_echo_object = help_echo_window = Qnil;
12569 help_echo_pos = -1;
12570 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12571 if (NILP (help_echo_string))
12572 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12575 #endif /* !USE_GTK && !HAVE_NS */
12577 #endif /* HAVE_WINDOW_SYSTEM */
12581 /************************************************************************
12582 Horizontal scrolling
12583 ************************************************************************/
12585 static int hscroll_window_tree (Lisp_Object);
12586 static int hscroll_windows (Lisp_Object);
12588 /* For all leaf windows in the window tree rooted at WINDOW, set their
12589 hscroll value so that PT is (i) visible in the window, and (ii) so
12590 that it is not within a certain margin at the window's left and
12591 right border. Value is non-zero if any window's hscroll has been
12592 changed. */
12594 static int
12595 hscroll_window_tree (Lisp_Object window)
12597 int hscrolled_p = 0;
12598 int hscroll_relative_p = FLOATP (Vhscroll_step);
12599 int hscroll_step_abs = 0;
12600 double hscroll_step_rel = 0;
12602 if (hscroll_relative_p)
12604 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12605 if (hscroll_step_rel < 0)
12607 hscroll_relative_p = 0;
12608 hscroll_step_abs = 0;
12611 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12613 hscroll_step_abs = XINT (Vhscroll_step);
12614 if (hscroll_step_abs < 0)
12615 hscroll_step_abs = 0;
12617 else
12618 hscroll_step_abs = 0;
12620 while (WINDOWP (window))
12622 struct window *w = XWINDOW (window);
12624 if (WINDOWP (w->contents))
12625 hscrolled_p |= hscroll_window_tree (w->contents);
12626 else if (w->cursor.vpos >= 0)
12628 int h_margin;
12629 int text_area_width;
12630 struct glyph_row *cursor_row;
12631 struct glyph_row *bottom_row;
12632 int row_r2l_p;
12634 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12635 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12636 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12637 else
12638 cursor_row = bottom_row - 1;
12640 if (!cursor_row->enabled_p)
12642 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12643 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12644 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12645 else
12646 cursor_row = bottom_row - 1;
12648 row_r2l_p = cursor_row->reversed_p;
12650 text_area_width = window_box_width (w, TEXT_AREA);
12652 /* Scroll when cursor is inside this scroll margin. */
12653 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12655 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12656 /* For left-to-right rows, hscroll when cursor is either
12657 (i) inside the right hscroll margin, or (ii) if it is
12658 inside the left margin and the window is already
12659 hscrolled. */
12660 && ((!row_r2l_p
12661 && ((w->hscroll
12662 && w->cursor.x <= h_margin)
12663 || (cursor_row->enabled_p
12664 && cursor_row->truncated_on_right_p
12665 && (w->cursor.x >= text_area_width - h_margin))))
12666 /* For right-to-left rows, the logic is similar,
12667 except that rules for scrolling to left and right
12668 are reversed. E.g., if cursor.x <= h_margin, we
12669 need to hscroll "to the right" unconditionally,
12670 and that will scroll the screen to the left so as
12671 to reveal the next portion of the row. */
12672 || (row_r2l_p
12673 && ((cursor_row->enabled_p
12674 /* FIXME: It is confusing to set the
12675 truncated_on_right_p flag when R2L rows
12676 are actually truncated on the left. */
12677 && cursor_row->truncated_on_right_p
12678 && w->cursor.x <= h_margin)
12679 || (w->hscroll
12680 && (w->cursor.x >= text_area_width - h_margin))))))
12682 struct it it;
12683 ptrdiff_t hscroll;
12684 struct buffer *saved_current_buffer;
12685 ptrdiff_t pt;
12686 int wanted_x;
12688 /* Find point in a display of infinite width. */
12689 saved_current_buffer = current_buffer;
12690 current_buffer = XBUFFER (w->contents);
12692 if (w == XWINDOW (selected_window))
12693 pt = PT;
12694 else
12695 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12697 /* Move iterator to pt starting at cursor_row->start in
12698 a line with infinite width. */
12699 init_to_row_start (&it, w, cursor_row);
12700 it.last_visible_x = INFINITY;
12701 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12702 current_buffer = saved_current_buffer;
12704 /* Position cursor in window. */
12705 if (!hscroll_relative_p && hscroll_step_abs == 0)
12706 hscroll = max (0, (it.current_x
12707 - (ITERATOR_AT_END_OF_LINE_P (&it)
12708 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12709 : (text_area_width / 2))))
12710 / FRAME_COLUMN_WIDTH (it.f);
12711 else if ((!row_r2l_p
12712 && w->cursor.x >= text_area_width - h_margin)
12713 || (row_r2l_p && w->cursor.x <= h_margin))
12715 if (hscroll_relative_p)
12716 wanted_x = text_area_width * (1 - hscroll_step_rel)
12717 - h_margin;
12718 else
12719 wanted_x = text_area_width
12720 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12721 - h_margin;
12722 hscroll
12723 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12725 else
12727 if (hscroll_relative_p)
12728 wanted_x = text_area_width * hscroll_step_rel
12729 + h_margin;
12730 else
12731 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12732 + h_margin;
12733 hscroll
12734 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12736 hscroll = max (hscroll, w->min_hscroll);
12738 /* Don't prevent redisplay optimizations if hscroll
12739 hasn't changed, as it will unnecessarily slow down
12740 redisplay. */
12741 if (w->hscroll != hscroll)
12743 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12744 w->hscroll = hscroll;
12745 hscrolled_p = 1;
12750 window = w->next;
12753 /* Value is non-zero if hscroll of any leaf window has been changed. */
12754 return hscrolled_p;
12758 /* Set hscroll so that cursor is visible and not inside horizontal
12759 scroll margins for all windows in the tree rooted at WINDOW. See
12760 also hscroll_window_tree above. Value is non-zero if any window's
12761 hscroll has been changed. If it has, desired matrices on the frame
12762 of WINDOW are cleared. */
12764 static int
12765 hscroll_windows (Lisp_Object window)
12767 int hscrolled_p = hscroll_window_tree (window);
12768 if (hscrolled_p)
12769 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12770 return hscrolled_p;
12775 /************************************************************************
12776 Redisplay
12777 ************************************************************************/
12779 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12780 to a non-zero value. This is sometimes handy to have in a debugger
12781 session. */
12783 #ifdef GLYPH_DEBUG
12785 /* First and last unchanged row for try_window_id. */
12787 static int debug_first_unchanged_at_end_vpos;
12788 static int debug_last_unchanged_at_beg_vpos;
12790 /* Delta vpos and y. */
12792 static int debug_dvpos, debug_dy;
12794 /* Delta in characters and bytes for try_window_id. */
12796 static ptrdiff_t debug_delta, debug_delta_bytes;
12798 /* Values of window_end_pos and window_end_vpos at the end of
12799 try_window_id. */
12801 static ptrdiff_t debug_end_vpos;
12803 /* Append a string to W->desired_matrix->method. FMT is a printf
12804 format string. If trace_redisplay_p is true also printf the
12805 resulting string to stderr. */
12807 static void debug_method_add (struct window *, char const *, ...)
12808 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12810 static void
12811 debug_method_add (struct window *w, char const *fmt, ...)
12813 void *ptr = w;
12814 char *method = w->desired_matrix->method;
12815 int len = strlen (method);
12816 int size = sizeof w->desired_matrix->method;
12817 int remaining = size - len - 1;
12818 va_list ap;
12820 if (len && remaining)
12822 method[len] = '|';
12823 --remaining, ++len;
12826 va_start (ap, fmt);
12827 vsnprintf (method + len, remaining + 1, fmt, ap);
12828 va_end (ap);
12830 if (trace_redisplay_p)
12831 fprintf (stderr, "%p (%s): %s\n",
12832 ptr,
12833 ((BUFFERP (w->contents)
12834 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12835 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12836 : "no buffer"),
12837 method + len);
12840 #endif /* GLYPH_DEBUG */
12843 /* Value is non-zero if all changes in window W, which displays
12844 current_buffer, are in the text between START and END. START is a
12845 buffer position, END is given as a distance from Z. Used in
12846 redisplay_internal for display optimization. */
12848 static int
12849 text_outside_line_unchanged_p (struct window *w,
12850 ptrdiff_t start, ptrdiff_t end)
12852 int unchanged_p = 1;
12854 /* If text or overlays have changed, see where. */
12855 if (window_outdated (w))
12857 /* Gap in the line? */
12858 if (GPT < start || Z - GPT < end)
12859 unchanged_p = 0;
12861 /* Changes start in front of the line, or end after it? */
12862 if (unchanged_p
12863 && (BEG_UNCHANGED < start - 1
12864 || END_UNCHANGED < end))
12865 unchanged_p = 0;
12867 /* If selective display, can't optimize if changes start at the
12868 beginning of the line. */
12869 if (unchanged_p
12870 && INTEGERP (BVAR (current_buffer, selective_display))
12871 && XINT (BVAR (current_buffer, selective_display)) > 0
12872 && (BEG_UNCHANGED < start || GPT <= start))
12873 unchanged_p = 0;
12875 /* If there are overlays at the start or end of the line, these
12876 may have overlay strings with newlines in them. A change at
12877 START, for instance, may actually concern the display of such
12878 overlay strings as well, and they are displayed on different
12879 lines. So, quickly rule out this case. (For the future, it
12880 might be desirable to implement something more telling than
12881 just BEG/END_UNCHANGED.) */
12882 if (unchanged_p)
12884 if (BEG + BEG_UNCHANGED == start
12885 && overlay_touches_p (start))
12886 unchanged_p = 0;
12887 if (END_UNCHANGED == end
12888 && overlay_touches_p (Z - end))
12889 unchanged_p = 0;
12892 /* Under bidi reordering, adding or deleting a character in the
12893 beginning of a paragraph, before the first strong directional
12894 character, can change the base direction of the paragraph (unless
12895 the buffer specifies a fixed paragraph direction), which will
12896 require to redisplay the whole paragraph. It might be worthwhile
12897 to find the paragraph limits and widen the range of redisplayed
12898 lines to that, but for now just give up this optimization. */
12899 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12900 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12901 unchanged_p = 0;
12904 return unchanged_p;
12908 /* Do a frame update, taking possible shortcuts into account. This is
12909 the main external entry point for redisplay.
12911 If the last redisplay displayed an echo area message and that message
12912 is no longer requested, we clear the echo area or bring back the
12913 mini-buffer if that is in use. */
12915 void
12916 redisplay (void)
12918 redisplay_internal ();
12922 static Lisp_Object
12923 overlay_arrow_string_or_property (Lisp_Object var)
12925 Lisp_Object val;
12927 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12928 return val;
12930 return Voverlay_arrow_string;
12933 /* Return 1 if there are any overlay-arrows in current_buffer. */
12934 static int
12935 overlay_arrow_in_current_buffer_p (void)
12937 Lisp_Object vlist;
12939 for (vlist = Voverlay_arrow_variable_list;
12940 CONSP (vlist);
12941 vlist = XCDR (vlist))
12943 Lisp_Object var = XCAR (vlist);
12944 Lisp_Object val;
12946 if (!SYMBOLP (var))
12947 continue;
12948 val = find_symbol_value (var);
12949 if (MARKERP (val)
12950 && current_buffer == XMARKER (val)->buffer)
12951 return 1;
12953 return 0;
12957 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12958 has changed. */
12960 static int
12961 overlay_arrows_changed_p (void)
12963 Lisp_Object vlist;
12965 for (vlist = Voverlay_arrow_variable_list;
12966 CONSP (vlist);
12967 vlist = XCDR (vlist))
12969 Lisp_Object var = XCAR (vlist);
12970 Lisp_Object val, pstr;
12972 if (!SYMBOLP (var))
12973 continue;
12974 val = find_symbol_value (var);
12975 if (!MARKERP (val))
12976 continue;
12977 if (! EQ (COERCE_MARKER (val),
12978 Fget (var, Qlast_arrow_position))
12979 || ! (pstr = overlay_arrow_string_or_property (var),
12980 EQ (pstr, Fget (var, Qlast_arrow_string))))
12981 return 1;
12983 return 0;
12986 /* Mark overlay arrows to be updated on next redisplay. */
12988 static void
12989 update_overlay_arrows (int up_to_date)
12991 Lisp_Object vlist;
12993 for (vlist = Voverlay_arrow_variable_list;
12994 CONSP (vlist);
12995 vlist = XCDR (vlist))
12997 Lisp_Object var = XCAR (vlist);
12999 if (!SYMBOLP (var))
13000 continue;
13002 if (up_to_date > 0)
13004 Lisp_Object val = find_symbol_value (var);
13005 Fput (var, Qlast_arrow_position,
13006 COERCE_MARKER (val));
13007 Fput (var, Qlast_arrow_string,
13008 overlay_arrow_string_or_property (var));
13010 else if (up_to_date < 0
13011 || !NILP (Fget (var, Qlast_arrow_position)))
13013 Fput (var, Qlast_arrow_position, Qt);
13014 Fput (var, Qlast_arrow_string, Qt);
13020 /* Return overlay arrow string to display at row.
13021 Return integer (bitmap number) for arrow bitmap in left fringe.
13022 Return nil if no overlay arrow. */
13024 static Lisp_Object
13025 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13027 Lisp_Object vlist;
13029 for (vlist = Voverlay_arrow_variable_list;
13030 CONSP (vlist);
13031 vlist = XCDR (vlist))
13033 Lisp_Object var = XCAR (vlist);
13034 Lisp_Object val;
13036 if (!SYMBOLP (var))
13037 continue;
13039 val = find_symbol_value (var);
13041 if (MARKERP (val)
13042 && current_buffer == XMARKER (val)->buffer
13043 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13045 if (FRAME_WINDOW_P (it->f)
13046 /* FIXME: if ROW->reversed_p is set, this should test
13047 the right fringe, not the left one. */
13048 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13050 #ifdef HAVE_WINDOW_SYSTEM
13051 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13053 int fringe_bitmap;
13054 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13055 return make_number (fringe_bitmap);
13057 #endif
13058 return make_number (-1); /* Use default arrow bitmap. */
13060 return overlay_arrow_string_or_property (var);
13064 return Qnil;
13067 /* Return 1 if point moved out of or into a composition. Otherwise
13068 return 0. PREV_BUF and PREV_PT are the last point buffer and
13069 position. BUF and PT are the current point buffer and position. */
13071 static int
13072 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13073 struct buffer *buf, ptrdiff_t pt)
13075 ptrdiff_t start, end;
13076 Lisp_Object prop;
13077 Lisp_Object buffer;
13079 XSETBUFFER (buffer, buf);
13080 /* Check a composition at the last point if point moved within the
13081 same buffer. */
13082 if (prev_buf == buf)
13084 if (prev_pt == pt)
13085 /* Point didn't move. */
13086 return 0;
13088 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13089 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13090 && composition_valid_p (start, end, prop)
13091 && start < prev_pt && end > prev_pt)
13092 /* The last point was within the composition. Return 1 iff
13093 point moved out of the composition. */
13094 return (pt <= start || pt >= end);
13097 /* Check a composition at the current point. */
13098 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13099 && find_composition (pt, -1, &start, &end, &prop, buffer)
13100 && composition_valid_p (start, end, prop)
13101 && start < pt && end > pt);
13104 /* Reconsider the clip changes of buffer which is displayed in W. */
13106 static void
13107 reconsider_clip_changes (struct window *w)
13109 struct buffer *b = XBUFFER (w->contents);
13111 if (b->clip_changed
13112 && w->window_end_valid
13113 && w->current_matrix->buffer == b
13114 && w->current_matrix->zv == BUF_ZV (b)
13115 && w->current_matrix->begv == BUF_BEGV (b))
13116 b->clip_changed = 0;
13118 /* If display wasn't paused, and W is not a tool bar window, see if
13119 point has been moved into or out of a composition. In that case,
13120 we set b->clip_changed to 1 to force updating the screen. If
13121 b->clip_changed has already been set to 1, we can skip this
13122 check. */
13123 if (!b->clip_changed && w->window_end_valid)
13125 ptrdiff_t pt = (w == XWINDOW (selected_window)
13126 ? PT : marker_position (w->pointm));
13128 if ((w->current_matrix->buffer != b || pt != w->last_point)
13129 && check_point_in_composition (w->current_matrix->buffer,
13130 w->last_point, b, pt))
13131 b->clip_changed = 1;
13135 static void
13136 propagate_buffer_redisplay (void)
13137 { /* Resetting b->text->redisplay is problematic!
13138 We can't just reset it in the case that some window that displays
13139 it has not been redisplayed; and such a window can stay
13140 unredisplayed for a long time if it's currently invisible.
13141 But we do want to reset it at the end of redisplay otherwise
13142 its displayed windows will keep being redisplayed over and over
13143 again.
13144 So we copy all b->text->redisplay flags up to their windows here,
13145 such that mark_window_display_accurate can safely reset
13146 b->text->redisplay. */
13147 Lisp_Object ws = window_list ();
13148 for (; CONSP (ws); ws = XCDR (ws))
13150 struct window *thisw = XWINDOW (XCAR (ws));
13151 struct buffer *thisb = XBUFFER (thisw->contents);
13152 if (thisb->text->redisplay)
13153 thisw->redisplay = true;
13157 #define STOP_POLLING \
13158 do { if (! polling_stopped_here) stop_polling (); \
13159 polling_stopped_here = 1; } while (0)
13161 #define RESUME_POLLING \
13162 do { if (polling_stopped_here) start_polling (); \
13163 polling_stopped_here = 0; } while (0)
13166 /* Perhaps in the future avoid recentering windows if it
13167 is not necessary; currently that causes some problems. */
13169 static void
13170 redisplay_internal (void)
13172 struct window *w = XWINDOW (selected_window);
13173 struct window *sw;
13174 struct frame *fr;
13175 int pending;
13176 bool must_finish = 0, match_p;
13177 struct text_pos tlbufpos, tlendpos;
13178 int number_of_visible_frames;
13179 ptrdiff_t count;
13180 struct frame *sf;
13181 int polling_stopped_here = 0;
13182 Lisp_Object tail, frame;
13184 /* True means redisplay has to consider all windows on all
13185 frames. False, only selected_window is considered. */
13186 bool consider_all_windows_p;
13188 /* True means redisplay has to redisplay the miniwindow. */
13189 bool update_miniwindow_p = false;
13191 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13193 /* No redisplay if running in batch mode or frame is not yet fully
13194 initialized, or redisplay is explicitly turned off by setting
13195 Vinhibit_redisplay. */
13196 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13197 || !NILP (Vinhibit_redisplay))
13198 return;
13200 /* Don't examine these until after testing Vinhibit_redisplay.
13201 When Emacs is shutting down, perhaps because its connection to
13202 X has dropped, we should not look at them at all. */
13203 fr = XFRAME (w->frame);
13204 sf = SELECTED_FRAME ();
13206 if (!fr->glyphs_initialized_p)
13207 return;
13209 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13210 if (popup_activated ())
13211 return;
13212 #endif
13214 /* I don't think this happens but let's be paranoid. */
13215 if (redisplaying_p)
13216 return;
13218 /* Record a function that clears redisplaying_p
13219 when we leave this function. */
13220 count = SPECPDL_INDEX ();
13221 record_unwind_protect_void (unwind_redisplay);
13222 redisplaying_p = 1;
13223 specbind (Qinhibit_free_realized_faces, Qnil);
13225 /* Record this function, so it appears on the profiler's backtraces. */
13226 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13228 FOR_EACH_FRAME (tail, frame)
13229 XFRAME (frame)->already_hscrolled_p = 0;
13231 retry:
13232 /* Remember the currently selected window. */
13233 sw = w;
13235 pending = 0;
13236 last_escape_glyph_frame = NULL;
13237 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13238 last_glyphless_glyph_frame = NULL;
13239 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13241 /* If face_change_count is non-zero, init_iterator will free all
13242 realized faces, which includes the faces referenced from current
13243 matrices. So, we can't reuse current matrices in this case. */
13244 if (face_change_count)
13245 windows_or_buffers_changed = 47;
13247 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13248 && FRAME_TTY (sf)->previous_frame != sf)
13250 /* Since frames on a single ASCII terminal share the same
13251 display area, displaying a different frame means redisplay
13252 the whole thing. */
13253 SET_FRAME_GARBAGED (sf);
13254 #ifndef DOS_NT
13255 set_tty_color_mode (FRAME_TTY (sf), sf);
13256 #endif
13257 FRAME_TTY (sf)->previous_frame = sf;
13260 /* Set the visible flags for all frames. Do this before checking for
13261 resized or garbaged frames; they want to know if their frames are
13262 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13263 number_of_visible_frames = 0;
13265 FOR_EACH_FRAME (tail, frame)
13267 struct frame *f = XFRAME (frame);
13269 if (FRAME_VISIBLE_P (f))
13271 ++number_of_visible_frames;
13272 /* Adjust matrices for visible frames only. */
13273 if (f->fonts_changed)
13275 adjust_frame_glyphs (f);
13276 f->fonts_changed = 0;
13278 /* If cursor type has been changed on the frame
13279 other than selected, consider all frames. */
13280 if (f != sf && f->cursor_type_changed)
13281 update_mode_lines = 31;
13283 clear_desired_matrices (f);
13286 /* Notice any pending interrupt request to change frame size. */
13287 do_pending_window_change (1);
13289 /* do_pending_window_change could change the selected_window due to
13290 frame resizing which makes the selected window too small. */
13291 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13292 sw = w;
13294 /* Clear frames marked as garbaged. */
13295 clear_garbaged_frames ();
13297 /* Build menubar and tool-bar items. */
13298 if (NILP (Vmemory_full))
13299 prepare_menu_bars ();
13301 reconsider_clip_changes (w);
13303 /* In most cases selected window displays current buffer. */
13304 match_p = XBUFFER (w->contents) == current_buffer;
13305 if (match_p)
13307 /* Detect case that we need to write or remove a star in the mode line. */
13308 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13309 w->update_mode_line = 1;
13311 if (mode_line_update_needed (w))
13312 w->update_mode_line = 1;
13315 /* Normally the message* functions will have already displayed and
13316 updated the echo area, but the frame may have been trashed, or
13317 the update may have been preempted, so display the echo area
13318 again here. Checking message_cleared_p captures the case that
13319 the echo area should be cleared. */
13320 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13321 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13322 || (message_cleared_p
13323 && minibuf_level == 0
13324 /* If the mini-window is currently selected, this means the
13325 echo-area doesn't show through. */
13326 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13328 int window_height_changed_p = echo_area_display (0);
13330 if (message_cleared_p)
13331 update_miniwindow_p = true;
13333 must_finish = 1;
13335 /* If we don't display the current message, don't clear the
13336 message_cleared_p flag, because, if we did, we wouldn't clear
13337 the echo area in the next redisplay which doesn't preserve
13338 the echo area. */
13339 if (!display_last_displayed_message_p)
13340 message_cleared_p = 0;
13342 if (window_height_changed_p)
13344 windows_or_buffers_changed = 50;
13346 /* If window configuration was changed, frames may have been
13347 marked garbaged. Clear them or we will experience
13348 surprises wrt scrolling. */
13349 clear_garbaged_frames ();
13352 else if (EQ (selected_window, minibuf_window)
13353 && (current_buffer->clip_changed || window_outdated (w))
13354 && resize_mini_window (w, 0))
13356 /* Resized active mini-window to fit the size of what it is
13357 showing if its contents might have changed. */
13358 must_finish = 1;
13360 /* If window configuration was changed, frames may have been
13361 marked garbaged. Clear them or we will experience
13362 surprises wrt scrolling. */
13363 clear_garbaged_frames ();
13366 if (windows_or_buffers_changed && !update_mode_lines)
13367 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13368 only the windows's contents needs to be refreshed, or whether the
13369 mode-lines also need a refresh. */
13370 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13371 ? REDISPLAY_SOME : 32);
13373 /* If specs for an arrow have changed, do thorough redisplay
13374 to ensure we remove any arrow that should no longer exist. */
13375 if (overlay_arrows_changed_p ())
13376 /* Apparently, this is the only case where we update other windows,
13377 without updating other mode-lines. */
13378 windows_or_buffers_changed = 49;
13380 consider_all_windows_p = (update_mode_lines
13381 || windows_or_buffers_changed);
13383 #define AINC(a,i) \
13384 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13385 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13387 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13388 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13390 /* Optimize the case that only the line containing the cursor in the
13391 selected window has changed. Variables starting with this_ are
13392 set in display_line and record information about the line
13393 containing the cursor. */
13394 tlbufpos = this_line_start_pos;
13395 tlendpos = this_line_end_pos;
13396 if (!consider_all_windows_p
13397 && CHARPOS (tlbufpos) > 0
13398 && !w->update_mode_line
13399 && !current_buffer->clip_changed
13400 && !current_buffer->prevent_redisplay_optimizations_p
13401 && FRAME_VISIBLE_P (XFRAME (w->frame))
13402 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13403 && !XFRAME (w->frame)->cursor_type_changed
13404 /* Make sure recorded data applies to current buffer, etc. */
13405 && this_line_buffer == current_buffer
13406 && match_p
13407 && !w->force_start
13408 && !w->optional_new_start
13409 /* Point must be on the line that we have info recorded about. */
13410 && PT >= CHARPOS (tlbufpos)
13411 && PT <= Z - CHARPOS (tlendpos)
13412 /* All text outside that line, including its final newline,
13413 must be unchanged. */
13414 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13415 CHARPOS (tlendpos)))
13417 if (CHARPOS (tlbufpos) > BEGV
13418 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13419 && (CHARPOS (tlbufpos) == ZV
13420 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13421 /* Former continuation line has disappeared by becoming empty. */
13422 goto cancel;
13423 else if (window_outdated (w) || MINI_WINDOW_P (w))
13425 /* We have to handle the case of continuation around a
13426 wide-column character (see the comment in indent.c around
13427 line 1340).
13429 For instance, in the following case:
13431 -------- Insert --------
13432 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13433 J_I_ ==> J_I_ `^^' are cursors.
13434 ^^ ^^
13435 -------- --------
13437 As we have to redraw the line above, we cannot use this
13438 optimization. */
13440 struct it it;
13441 int line_height_before = this_line_pixel_height;
13443 /* Note that start_display will handle the case that the
13444 line starting at tlbufpos is a continuation line. */
13445 start_display (&it, w, tlbufpos);
13447 /* Implementation note: It this still necessary? */
13448 if (it.current_x != this_line_start_x)
13449 goto cancel;
13451 TRACE ((stderr, "trying display optimization 1\n"));
13452 w->cursor.vpos = -1;
13453 overlay_arrow_seen = 0;
13454 it.vpos = this_line_vpos;
13455 it.current_y = this_line_y;
13456 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13457 display_line (&it);
13459 /* If line contains point, is not continued,
13460 and ends at same distance from eob as before, we win. */
13461 if (w->cursor.vpos >= 0
13462 /* Line is not continued, otherwise this_line_start_pos
13463 would have been set to 0 in display_line. */
13464 && CHARPOS (this_line_start_pos)
13465 /* Line ends as before. */
13466 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13467 /* Line has same height as before. Otherwise other lines
13468 would have to be shifted up or down. */
13469 && this_line_pixel_height == line_height_before)
13471 /* If this is not the window's last line, we must adjust
13472 the charstarts of the lines below. */
13473 if (it.current_y < it.last_visible_y)
13475 struct glyph_row *row
13476 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13477 ptrdiff_t delta, delta_bytes;
13479 /* We used to distinguish between two cases here,
13480 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13481 when the line ends in a newline or the end of the
13482 buffer's accessible portion. But both cases did
13483 the same, so they were collapsed. */
13484 delta = (Z
13485 - CHARPOS (tlendpos)
13486 - MATRIX_ROW_START_CHARPOS (row));
13487 delta_bytes = (Z_BYTE
13488 - BYTEPOS (tlendpos)
13489 - MATRIX_ROW_START_BYTEPOS (row));
13491 increment_matrix_positions (w->current_matrix,
13492 this_line_vpos + 1,
13493 w->current_matrix->nrows,
13494 delta, delta_bytes);
13497 /* If this row displays text now but previously didn't,
13498 or vice versa, w->window_end_vpos may have to be
13499 adjusted. */
13500 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13502 if (w->window_end_vpos < this_line_vpos)
13503 w->window_end_vpos = this_line_vpos;
13505 else if (w->window_end_vpos == this_line_vpos
13506 && this_line_vpos > 0)
13507 w->window_end_vpos = this_line_vpos - 1;
13508 w->window_end_valid = 0;
13510 /* Update hint: No need to try to scroll in update_window. */
13511 w->desired_matrix->no_scrolling_p = 1;
13513 #ifdef GLYPH_DEBUG
13514 *w->desired_matrix->method = 0;
13515 debug_method_add (w, "optimization 1");
13516 #endif
13517 #ifdef HAVE_WINDOW_SYSTEM
13518 update_window_fringes (w, 0);
13519 #endif
13520 goto update;
13522 else
13523 goto cancel;
13525 else if (/* Cursor position hasn't changed. */
13526 PT == w->last_point
13527 /* Make sure the cursor was last displayed
13528 in this window. Otherwise we have to reposition it. */
13530 /* PXW: Must be converted to pixels, probably. */
13531 && 0 <= w->cursor.vpos
13532 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13534 if (!must_finish)
13536 do_pending_window_change (1);
13537 /* If selected_window changed, redisplay again. */
13538 if (WINDOWP (selected_window)
13539 && (w = XWINDOW (selected_window)) != sw)
13540 goto retry;
13542 /* We used to always goto end_of_redisplay here, but this
13543 isn't enough if we have a blinking cursor. */
13544 if (w->cursor_off_p == w->last_cursor_off_p)
13545 goto end_of_redisplay;
13547 goto update;
13549 /* If highlighting the region, or if the cursor is in the echo area,
13550 then we can't just move the cursor. */
13551 else if (NILP (Vshow_trailing_whitespace)
13552 && !cursor_in_echo_area)
13554 struct it it;
13555 struct glyph_row *row;
13557 /* Skip from tlbufpos to PT and see where it is. Note that
13558 PT may be in invisible text. If so, we will end at the
13559 next visible position. */
13560 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13561 NULL, DEFAULT_FACE_ID);
13562 it.current_x = this_line_start_x;
13563 it.current_y = this_line_y;
13564 it.vpos = this_line_vpos;
13566 /* The call to move_it_to stops in front of PT, but
13567 moves over before-strings. */
13568 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13570 if (it.vpos == this_line_vpos
13571 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13572 row->enabled_p))
13574 eassert (this_line_vpos == it.vpos);
13575 eassert (this_line_y == it.current_y);
13576 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13577 #ifdef GLYPH_DEBUG
13578 *w->desired_matrix->method = 0;
13579 debug_method_add (w, "optimization 3");
13580 #endif
13581 goto update;
13583 else
13584 goto cancel;
13587 cancel:
13588 /* Text changed drastically or point moved off of line. */
13589 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13592 CHARPOS (this_line_start_pos) = 0;
13593 ++clear_face_cache_count;
13594 #ifdef HAVE_WINDOW_SYSTEM
13595 ++clear_image_cache_count;
13596 #endif
13598 /* Build desired matrices, and update the display. If
13599 consider_all_windows_p is non-zero, do it for all windows on all
13600 frames. Otherwise do it for selected_window, only. */
13602 if (consider_all_windows_p)
13604 FOR_EACH_FRAME (tail, frame)
13605 XFRAME (frame)->updated_p = 0;
13607 propagate_buffer_redisplay ();
13609 FOR_EACH_FRAME (tail, frame)
13611 struct frame *f = XFRAME (frame);
13613 /* We don't have to do anything for unselected terminal
13614 frames. */
13615 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13616 && !EQ (FRAME_TTY (f)->top_frame, frame))
13617 continue;
13619 retry_frame:
13621 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13623 bool gcscrollbars
13624 /* Only GC scrollbars when we redisplay the whole frame. */
13625 = f->redisplay || !REDISPLAY_SOME_P ();
13626 /* Mark all the scroll bars to be removed; we'll redeem
13627 the ones we want when we redisplay their windows. */
13628 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13629 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13631 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13632 redisplay_windows (FRAME_ROOT_WINDOW (f));
13633 /* Remember that the invisible frames need to be redisplayed next
13634 time they're visible. */
13635 else if (!REDISPLAY_SOME_P ())
13636 f->redisplay = true;
13638 /* The X error handler may have deleted that frame. */
13639 if (!FRAME_LIVE_P (f))
13640 continue;
13642 /* Any scroll bars which redisplay_windows should have
13643 nuked should now go away. */
13644 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13645 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13647 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13649 /* If fonts changed on visible frame, display again. */
13650 if (f->fonts_changed)
13652 adjust_frame_glyphs (f);
13653 f->fonts_changed = 0;
13654 goto retry_frame;
13657 /* See if we have to hscroll. */
13658 if (!f->already_hscrolled_p)
13660 f->already_hscrolled_p = 1;
13661 if (hscroll_windows (f->root_window))
13662 goto retry_frame;
13665 /* Prevent various kinds of signals during display
13666 update. stdio is not robust about handling
13667 signals, which can cause an apparent I/O error. */
13668 if (interrupt_input)
13669 unrequest_sigio ();
13670 STOP_POLLING;
13672 pending |= update_frame (f, 0, 0);
13673 f->cursor_type_changed = 0;
13674 f->updated_p = 1;
13679 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13681 if (!pending)
13683 /* Do the mark_window_display_accurate after all windows have
13684 been redisplayed because this call resets flags in buffers
13685 which are needed for proper redisplay. */
13686 FOR_EACH_FRAME (tail, frame)
13688 struct frame *f = XFRAME (frame);
13689 if (f->updated_p)
13691 f->redisplay = false;
13692 mark_window_display_accurate (f->root_window, 1);
13693 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13694 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13699 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13701 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13702 struct frame *mini_frame;
13704 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13705 /* Use list_of_error, not Qerror, so that
13706 we catch only errors and don't run the debugger. */
13707 internal_condition_case_1 (redisplay_window_1, selected_window,
13708 list_of_error,
13709 redisplay_window_error);
13710 if (update_miniwindow_p)
13711 internal_condition_case_1 (redisplay_window_1, mini_window,
13712 list_of_error,
13713 redisplay_window_error);
13715 /* Compare desired and current matrices, perform output. */
13717 update:
13718 /* If fonts changed, display again. */
13719 if (sf->fonts_changed)
13720 goto retry;
13722 /* Prevent various kinds of signals during display update.
13723 stdio is not robust about handling signals,
13724 which can cause an apparent I/O error. */
13725 if (interrupt_input)
13726 unrequest_sigio ();
13727 STOP_POLLING;
13729 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13731 if (hscroll_windows (selected_window))
13732 goto retry;
13734 XWINDOW (selected_window)->must_be_updated_p = true;
13735 pending = update_frame (sf, 0, 0);
13736 sf->cursor_type_changed = 0;
13739 /* We may have called echo_area_display at the top of this
13740 function. If the echo area is on another frame, that may
13741 have put text on a frame other than the selected one, so the
13742 above call to update_frame would not have caught it. Catch
13743 it here. */
13744 mini_window = FRAME_MINIBUF_WINDOW (sf);
13745 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13747 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13749 XWINDOW (mini_window)->must_be_updated_p = true;
13750 pending |= update_frame (mini_frame, 0, 0);
13751 mini_frame->cursor_type_changed = 0;
13752 if (!pending && hscroll_windows (mini_window))
13753 goto retry;
13757 /* If display was paused because of pending input, make sure we do a
13758 thorough update the next time. */
13759 if (pending)
13761 /* Prevent the optimization at the beginning of
13762 redisplay_internal that tries a single-line update of the
13763 line containing the cursor in the selected window. */
13764 CHARPOS (this_line_start_pos) = 0;
13766 /* Let the overlay arrow be updated the next time. */
13767 update_overlay_arrows (0);
13769 /* If we pause after scrolling, some rows in the current
13770 matrices of some windows are not valid. */
13771 if (!WINDOW_FULL_WIDTH_P (w)
13772 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13773 update_mode_lines = 36;
13775 else
13777 if (!consider_all_windows_p)
13779 /* This has already been done above if
13780 consider_all_windows_p is set. */
13781 if (XBUFFER (w->contents)->text->redisplay
13782 && buffer_window_count (XBUFFER (w->contents)) > 1)
13783 /* This can happen if b->text->redisplay was set during
13784 jit-lock. */
13785 propagate_buffer_redisplay ();
13786 mark_window_display_accurate_1 (w, 1);
13788 /* Say overlay arrows are up to date. */
13789 update_overlay_arrows (1);
13791 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13792 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13795 update_mode_lines = 0;
13796 windows_or_buffers_changed = 0;
13799 /* Start SIGIO interrupts coming again. Having them off during the
13800 code above makes it less likely one will discard output, but not
13801 impossible, since there might be stuff in the system buffer here.
13802 But it is much hairier to try to do anything about that. */
13803 if (interrupt_input)
13804 request_sigio ();
13805 RESUME_POLLING;
13807 /* If a frame has become visible which was not before, redisplay
13808 again, so that we display it. Expose events for such a frame
13809 (which it gets when becoming visible) don't call the parts of
13810 redisplay constructing glyphs, so simply exposing a frame won't
13811 display anything in this case. So, we have to display these
13812 frames here explicitly. */
13813 if (!pending)
13815 int new_count = 0;
13817 FOR_EACH_FRAME (tail, frame)
13819 if (XFRAME (frame)->visible)
13820 new_count++;
13823 if (new_count != number_of_visible_frames)
13824 windows_or_buffers_changed = 52;
13827 /* Change frame size now if a change is pending. */
13828 do_pending_window_change (1);
13830 /* If we just did a pending size change, or have additional
13831 visible frames, or selected_window changed, redisplay again. */
13832 if ((windows_or_buffers_changed && !pending)
13833 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13834 goto retry;
13836 /* Clear the face and image caches.
13838 We used to do this only if consider_all_windows_p. But the cache
13839 needs to be cleared if a timer creates images in the current
13840 buffer (e.g. the test case in Bug#6230). */
13842 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13844 clear_face_cache (0);
13845 clear_face_cache_count = 0;
13848 #ifdef HAVE_WINDOW_SYSTEM
13849 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13851 clear_image_caches (Qnil);
13852 clear_image_cache_count = 0;
13854 #endif /* HAVE_WINDOW_SYSTEM */
13856 end_of_redisplay:
13857 if (interrupt_input && interrupts_deferred)
13858 request_sigio ();
13860 unbind_to (count, Qnil);
13861 RESUME_POLLING;
13865 /* Redisplay, but leave alone any recent echo area message unless
13866 another message has been requested in its place.
13868 This is useful in situations where you need to redisplay but no
13869 user action has occurred, making it inappropriate for the message
13870 area to be cleared. See tracking_off and
13871 wait_reading_process_output for examples of these situations.
13873 FROM_WHERE is an integer saying from where this function was
13874 called. This is useful for debugging. */
13876 void
13877 redisplay_preserve_echo_area (int from_where)
13879 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13881 if (!NILP (echo_area_buffer[1]))
13883 /* We have a previously displayed message, but no current
13884 message. Redisplay the previous message. */
13885 display_last_displayed_message_p = 1;
13886 redisplay_internal ();
13887 display_last_displayed_message_p = 0;
13889 else
13890 redisplay_internal ();
13892 flush_frame (SELECTED_FRAME ());
13896 /* Function registered with record_unwind_protect in redisplay_internal. */
13898 static void
13899 unwind_redisplay (void)
13901 redisplaying_p = 0;
13905 /* Mark the display of leaf window W as accurate or inaccurate.
13906 If ACCURATE_P is non-zero mark display of W as accurate. If
13907 ACCURATE_P is zero, arrange for W to be redisplayed the next
13908 time redisplay_internal is called. */
13910 static void
13911 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13913 struct buffer *b = XBUFFER (w->contents);
13915 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13916 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13917 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13919 if (accurate_p)
13921 b->clip_changed = false;
13922 b->prevent_redisplay_optimizations_p = false;
13923 eassert (buffer_window_count (b) > 0);
13924 /* Resetting b->text->redisplay is problematic!
13925 In order to make it safer to do it here, redisplay_internal must
13926 have copied all b->text->redisplay to their respective windows. */
13927 b->text->redisplay = false;
13929 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13930 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13931 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13932 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13934 w->current_matrix->buffer = b;
13935 w->current_matrix->begv = BUF_BEGV (b);
13936 w->current_matrix->zv = BUF_ZV (b);
13938 w->last_cursor_vpos = w->cursor.vpos;
13939 w->last_cursor_off_p = w->cursor_off_p;
13941 if (w == XWINDOW (selected_window))
13942 w->last_point = BUF_PT (b);
13943 else
13944 w->last_point = marker_position (w->pointm);
13946 w->window_end_valid = true;
13947 w->update_mode_line = false;
13950 w->redisplay = !accurate_p;
13954 /* Mark the display of windows in the window tree rooted at WINDOW as
13955 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13956 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13957 be redisplayed the next time redisplay_internal is called. */
13959 void
13960 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13962 struct window *w;
13964 for (; !NILP (window); window = w->next)
13966 w = XWINDOW (window);
13967 if (WINDOWP (w->contents))
13968 mark_window_display_accurate (w->contents, accurate_p);
13969 else
13970 mark_window_display_accurate_1 (w, accurate_p);
13973 if (accurate_p)
13974 update_overlay_arrows (1);
13975 else
13976 /* Force a thorough redisplay the next time by setting
13977 last_arrow_position and last_arrow_string to t, which is
13978 unequal to any useful value of Voverlay_arrow_... */
13979 update_overlay_arrows (-1);
13983 /* Return value in display table DP (Lisp_Char_Table *) for character
13984 C. Since a display table doesn't have any parent, we don't have to
13985 follow parent. Do not call this function directly but use the
13986 macro DISP_CHAR_VECTOR. */
13988 Lisp_Object
13989 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13991 Lisp_Object val;
13993 if (ASCII_CHAR_P (c))
13995 val = dp->ascii;
13996 if (SUB_CHAR_TABLE_P (val))
13997 val = XSUB_CHAR_TABLE (val)->contents[c];
13999 else
14001 Lisp_Object table;
14003 XSETCHAR_TABLE (table, dp);
14004 val = char_table_ref (table, c);
14006 if (NILP (val))
14007 val = dp->defalt;
14008 return val;
14013 /***********************************************************************
14014 Window Redisplay
14015 ***********************************************************************/
14017 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14019 static void
14020 redisplay_windows (Lisp_Object window)
14022 while (!NILP (window))
14024 struct window *w = XWINDOW (window);
14026 if (WINDOWP (w->contents))
14027 redisplay_windows (w->contents);
14028 else if (BUFFERP (w->contents))
14030 displayed_buffer = XBUFFER (w->contents);
14031 /* Use list_of_error, not Qerror, so that
14032 we catch only errors and don't run the debugger. */
14033 internal_condition_case_1 (redisplay_window_0, window,
14034 list_of_error,
14035 redisplay_window_error);
14038 window = w->next;
14042 static Lisp_Object
14043 redisplay_window_error (Lisp_Object ignore)
14045 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14046 return Qnil;
14049 static Lisp_Object
14050 redisplay_window_0 (Lisp_Object window)
14052 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14053 redisplay_window (window, false);
14054 return Qnil;
14057 static Lisp_Object
14058 redisplay_window_1 (Lisp_Object window)
14060 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14061 redisplay_window (window, true);
14062 return Qnil;
14066 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14067 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14068 which positions recorded in ROW differ from current buffer
14069 positions.
14071 Return 0 if cursor is not on this row, 1 otherwise. */
14073 static int
14074 set_cursor_from_row (struct window *w, struct glyph_row *row,
14075 struct glyph_matrix *matrix,
14076 ptrdiff_t delta, ptrdiff_t delta_bytes,
14077 int dy, int dvpos)
14079 struct glyph *glyph = row->glyphs[TEXT_AREA];
14080 struct glyph *end = glyph + row->used[TEXT_AREA];
14081 struct glyph *cursor = NULL;
14082 /* The last known character position in row. */
14083 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14084 int x = row->x;
14085 ptrdiff_t pt_old = PT - delta;
14086 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14087 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14088 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14089 /* A glyph beyond the edge of TEXT_AREA which we should never
14090 touch. */
14091 struct glyph *glyphs_end = end;
14092 /* Non-zero means we've found a match for cursor position, but that
14093 glyph has the avoid_cursor_p flag set. */
14094 int match_with_avoid_cursor = 0;
14095 /* Non-zero means we've seen at least one glyph that came from a
14096 display string. */
14097 int string_seen = 0;
14098 /* Largest and smallest buffer positions seen so far during scan of
14099 glyph row. */
14100 ptrdiff_t bpos_max = pos_before;
14101 ptrdiff_t bpos_min = pos_after;
14102 /* Last buffer position covered by an overlay string with an integer
14103 `cursor' property. */
14104 ptrdiff_t bpos_covered = 0;
14105 /* Non-zero means the display string on which to display the cursor
14106 comes from a text property, not from an overlay. */
14107 int string_from_text_prop = 0;
14109 /* Don't even try doing anything if called for a mode-line or
14110 header-line row, since the rest of the code isn't prepared to
14111 deal with such calamities. */
14112 eassert (!row->mode_line_p);
14113 if (row->mode_line_p)
14114 return 0;
14116 /* Skip over glyphs not having an object at the start and the end of
14117 the row. These are special glyphs like truncation marks on
14118 terminal frames. */
14119 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14121 if (!row->reversed_p)
14123 while (glyph < end
14124 && INTEGERP (glyph->object)
14125 && glyph->charpos < 0)
14127 x += glyph->pixel_width;
14128 ++glyph;
14130 while (end > glyph
14131 && INTEGERP ((end - 1)->object)
14132 /* CHARPOS is zero for blanks and stretch glyphs
14133 inserted by extend_face_to_end_of_line. */
14134 && (end - 1)->charpos <= 0)
14135 --end;
14136 glyph_before = glyph - 1;
14137 glyph_after = end;
14139 else
14141 struct glyph *g;
14143 /* If the glyph row is reversed, we need to process it from back
14144 to front, so swap the edge pointers. */
14145 glyphs_end = end = glyph - 1;
14146 glyph += row->used[TEXT_AREA] - 1;
14148 while (glyph > end + 1
14149 && INTEGERP (glyph->object)
14150 && glyph->charpos < 0)
14152 --glyph;
14153 x -= glyph->pixel_width;
14155 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14156 --glyph;
14157 /* By default, in reversed rows we put the cursor on the
14158 rightmost (first in the reading order) glyph. */
14159 for (g = end + 1; g < glyph; g++)
14160 x += g->pixel_width;
14161 while (end < glyph
14162 && INTEGERP ((end + 1)->object)
14163 && (end + 1)->charpos <= 0)
14164 ++end;
14165 glyph_before = glyph + 1;
14166 glyph_after = end;
14169 else if (row->reversed_p)
14171 /* In R2L rows that don't display text, put the cursor on the
14172 rightmost glyph. Case in point: an empty last line that is
14173 part of an R2L paragraph. */
14174 cursor = end - 1;
14175 /* Avoid placing the cursor on the last glyph of the row, where
14176 on terminal frames we hold the vertical border between
14177 adjacent windows. */
14178 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14179 && !WINDOW_RIGHTMOST_P (w)
14180 && cursor == row->glyphs[LAST_AREA] - 1)
14181 cursor--;
14182 x = -1; /* will be computed below, at label compute_x */
14185 /* Step 1: Try to find the glyph whose character position
14186 corresponds to point. If that's not possible, find 2 glyphs
14187 whose character positions are the closest to point, one before
14188 point, the other after it. */
14189 if (!row->reversed_p)
14190 while (/* not marched to end of glyph row */
14191 glyph < end
14192 /* glyph was not inserted by redisplay for internal purposes */
14193 && !INTEGERP (glyph->object))
14195 if (BUFFERP (glyph->object))
14197 ptrdiff_t dpos = glyph->charpos - pt_old;
14199 if (glyph->charpos > bpos_max)
14200 bpos_max = glyph->charpos;
14201 if (glyph->charpos < bpos_min)
14202 bpos_min = glyph->charpos;
14203 if (!glyph->avoid_cursor_p)
14205 /* If we hit point, we've found the glyph on which to
14206 display the cursor. */
14207 if (dpos == 0)
14209 match_with_avoid_cursor = 0;
14210 break;
14212 /* See if we've found a better approximation to
14213 POS_BEFORE or to POS_AFTER. */
14214 if (0 > dpos && dpos > pos_before - pt_old)
14216 pos_before = glyph->charpos;
14217 glyph_before = glyph;
14219 else if (0 < dpos && dpos < pos_after - pt_old)
14221 pos_after = glyph->charpos;
14222 glyph_after = glyph;
14225 else if (dpos == 0)
14226 match_with_avoid_cursor = 1;
14228 else if (STRINGP (glyph->object))
14230 Lisp_Object chprop;
14231 ptrdiff_t glyph_pos = glyph->charpos;
14233 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14234 glyph->object);
14235 if (!NILP (chprop))
14237 /* If the string came from a `display' text property,
14238 look up the buffer position of that property and
14239 use that position to update bpos_max, as if we
14240 actually saw such a position in one of the row's
14241 glyphs. This helps with supporting integer values
14242 of `cursor' property on the display string in
14243 situations where most or all of the row's buffer
14244 text is completely covered by display properties,
14245 so that no glyph with valid buffer positions is
14246 ever seen in the row. */
14247 ptrdiff_t prop_pos =
14248 string_buffer_position_lim (glyph->object, pos_before,
14249 pos_after, 0);
14251 if (prop_pos >= pos_before)
14252 bpos_max = prop_pos - 1;
14254 if (INTEGERP (chprop))
14256 bpos_covered = bpos_max + XINT (chprop);
14257 /* If the `cursor' property covers buffer positions up
14258 to and including point, we should display cursor on
14259 this glyph. Note that, if a `cursor' property on one
14260 of the string's characters has an integer value, we
14261 will break out of the loop below _before_ we get to
14262 the position match above. IOW, integer values of
14263 the `cursor' property override the "exact match for
14264 point" strategy of positioning the cursor. */
14265 /* Implementation note: bpos_max == pt_old when, e.g.,
14266 we are in an empty line, where bpos_max is set to
14267 MATRIX_ROW_START_CHARPOS, see above. */
14268 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14270 cursor = glyph;
14271 break;
14275 string_seen = 1;
14277 x += glyph->pixel_width;
14278 ++glyph;
14280 else if (glyph > end) /* row is reversed */
14281 while (!INTEGERP (glyph->object))
14283 if (BUFFERP (glyph->object))
14285 ptrdiff_t dpos = glyph->charpos - pt_old;
14287 if (glyph->charpos > bpos_max)
14288 bpos_max = glyph->charpos;
14289 if (glyph->charpos < bpos_min)
14290 bpos_min = glyph->charpos;
14291 if (!glyph->avoid_cursor_p)
14293 if (dpos == 0)
14295 match_with_avoid_cursor = 0;
14296 break;
14298 if (0 > dpos && dpos > pos_before - pt_old)
14300 pos_before = glyph->charpos;
14301 glyph_before = glyph;
14303 else if (0 < dpos && dpos < pos_after - pt_old)
14305 pos_after = glyph->charpos;
14306 glyph_after = glyph;
14309 else if (dpos == 0)
14310 match_with_avoid_cursor = 1;
14312 else if (STRINGP (glyph->object))
14314 Lisp_Object chprop;
14315 ptrdiff_t glyph_pos = glyph->charpos;
14317 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14318 glyph->object);
14319 if (!NILP (chprop))
14321 ptrdiff_t prop_pos =
14322 string_buffer_position_lim (glyph->object, pos_before,
14323 pos_after, 0);
14325 if (prop_pos >= pos_before)
14326 bpos_max = prop_pos - 1;
14328 if (INTEGERP (chprop))
14330 bpos_covered = bpos_max + XINT (chprop);
14331 /* If the `cursor' property covers buffer positions up
14332 to and including point, we should display cursor on
14333 this glyph. */
14334 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14336 cursor = glyph;
14337 break;
14340 string_seen = 1;
14342 --glyph;
14343 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14345 x--; /* can't use any pixel_width */
14346 break;
14348 x -= glyph->pixel_width;
14351 /* Step 2: If we didn't find an exact match for point, we need to
14352 look for a proper place to put the cursor among glyphs between
14353 GLYPH_BEFORE and GLYPH_AFTER. */
14354 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14355 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14356 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14358 /* An empty line has a single glyph whose OBJECT is zero and
14359 whose CHARPOS is the position of a newline on that line.
14360 Note that on a TTY, there are more glyphs after that, which
14361 were produced by extend_face_to_end_of_line, but their
14362 CHARPOS is zero or negative. */
14363 int empty_line_p =
14364 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14365 && INTEGERP (glyph->object) && glyph->charpos > 0
14366 /* On a TTY, continued and truncated rows also have a glyph at
14367 their end whose OBJECT is zero and whose CHARPOS is
14368 positive (the continuation and truncation glyphs), but such
14369 rows are obviously not "empty". */
14370 && !(row->continued_p || row->truncated_on_right_p);
14372 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14374 ptrdiff_t ellipsis_pos;
14376 /* Scan back over the ellipsis glyphs. */
14377 if (!row->reversed_p)
14379 ellipsis_pos = (glyph - 1)->charpos;
14380 while (glyph > row->glyphs[TEXT_AREA]
14381 && (glyph - 1)->charpos == ellipsis_pos)
14382 glyph--, x -= glyph->pixel_width;
14383 /* That loop always goes one position too far, including
14384 the glyph before the ellipsis. So scan forward over
14385 that one. */
14386 x += glyph->pixel_width;
14387 glyph++;
14389 else /* row is reversed */
14391 ellipsis_pos = (glyph + 1)->charpos;
14392 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14393 && (glyph + 1)->charpos == ellipsis_pos)
14394 glyph++, x += glyph->pixel_width;
14395 x -= glyph->pixel_width;
14396 glyph--;
14399 else if (match_with_avoid_cursor)
14401 cursor = glyph_after;
14402 x = -1;
14404 else if (string_seen)
14406 int incr = row->reversed_p ? -1 : +1;
14408 /* Need to find the glyph that came out of a string which is
14409 present at point. That glyph is somewhere between
14410 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14411 positioned between POS_BEFORE and POS_AFTER in the
14412 buffer. */
14413 struct glyph *start, *stop;
14414 ptrdiff_t pos = pos_before;
14416 x = -1;
14418 /* If the row ends in a newline from a display string,
14419 reordering could have moved the glyphs belonging to the
14420 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14421 in this case we extend the search to the last glyph in
14422 the row that was not inserted by redisplay. */
14423 if (row->ends_in_newline_from_string_p)
14425 glyph_after = end;
14426 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14429 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14430 correspond to POS_BEFORE and POS_AFTER, respectively. We
14431 need START and STOP in the order that corresponds to the
14432 row's direction as given by its reversed_p flag. If the
14433 directionality of characters between POS_BEFORE and
14434 POS_AFTER is the opposite of the row's base direction,
14435 these characters will have been reordered for display,
14436 and we need to reverse START and STOP. */
14437 if (!row->reversed_p)
14439 start = min (glyph_before, glyph_after);
14440 stop = max (glyph_before, glyph_after);
14442 else
14444 start = max (glyph_before, glyph_after);
14445 stop = min (glyph_before, glyph_after);
14447 for (glyph = start + incr;
14448 row->reversed_p ? glyph > stop : glyph < stop; )
14451 /* Any glyphs that come from the buffer are here because
14452 of bidi reordering. Skip them, and only pay
14453 attention to glyphs that came from some string. */
14454 if (STRINGP (glyph->object))
14456 Lisp_Object str;
14457 ptrdiff_t tem;
14458 /* If the display property covers the newline, we
14459 need to search for it one position farther. */
14460 ptrdiff_t lim = pos_after
14461 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14463 string_from_text_prop = 0;
14464 str = glyph->object;
14465 tem = string_buffer_position_lim (str, pos, lim, 0);
14466 if (tem == 0 /* from overlay */
14467 || pos <= tem)
14469 /* If the string from which this glyph came is
14470 found in the buffer at point, or at position
14471 that is closer to point than pos_after, then
14472 we've found the glyph we've been looking for.
14473 If it comes from an overlay (tem == 0), and
14474 it has the `cursor' property on one of its
14475 glyphs, record that glyph as a candidate for
14476 displaying the cursor. (As in the
14477 unidirectional version, we will display the
14478 cursor on the last candidate we find.) */
14479 if (tem == 0
14480 || tem == pt_old
14481 || (tem - pt_old > 0 && tem < pos_after))
14483 /* The glyphs from this string could have
14484 been reordered. Find the one with the
14485 smallest string position. Or there could
14486 be a character in the string with the
14487 `cursor' property, which means display
14488 cursor on that character's glyph. */
14489 ptrdiff_t strpos = glyph->charpos;
14491 if (tem)
14493 cursor = glyph;
14494 string_from_text_prop = 1;
14496 for ( ;
14497 (row->reversed_p ? glyph > stop : glyph < stop)
14498 && EQ (glyph->object, str);
14499 glyph += incr)
14501 Lisp_Object cprop;
14502 ptrdiff_t gpos = glyph->charpos;
14504 cprop = Fget_char_property (make_number (gpos),
14505 Qcursor,
14506 glyph->object);
14507 if (!NILP (cprop))
14509 cursor = glyph;
14510 break;
14512 if (tem && glyph->charpos < strpos)
14514 strpos = glyph->charpos;
14515 cursor = glyph;
14519 if (tem == pt_old
14520 || (tem - pt_old > 0 && tem < pos_after))
14521 goto compute_x;
14523 if (tem)
14524 pos = tem + 1; /* don't find previous instances */
14526 /* This string is not what we want; skip all of the
14527 glyphs that came from it. */
14528 while ((row->reversed_p ? glyph > stop : glyph < stop)
14529 && EQ (glyph->object, str))
14530 glyph += incr;
14532 else
14533 glyph += incr;
14536 /* If we reached the end of the line, and END was from a string,
14537 the cursor is not on this line. */
14538 if (cursor == NULL
14539 && (row->reversed_p ? glyph <= end : glyph >= end)
14540 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14541 && STRINGP (end->object)
14542 && row->continued_p)
14543 return 0;
14545 /* A truncated row may not include PT among its character positions.
14546 Setting the cursor inside the scroll margin will trigger
14547 recalculation of hscroll in hscroll_window_tree. But if a
14548 display string covers point, defer to the string-handling
14549 code below to figure this out. */
14550 else if (row->truncated_on_left_p && pt_old < bpos_min)
14552 cursor = glyph_before;
14553 x = -1;
14555 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14556 /* Zero-width characters produce no glyphs. */
14557 || (!empty_line_p
14558 && (row->reversed_p
14559 ? glyph_after > glyphs_end
14560 : glyph_after < glyphs_end)))
14562 cursor = glyph_after;
14563 x = -1;
14567 compute_x:
14568 if (cursor != NULL)
14569 glyph = cursor;
14570 else if (glyph == glyphs_end
14571 && pos_before == pos_after
14572 && STRINGP ((row->reversed_p
14573 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14574 : row->glyphs[TEXT_AREA])->object))
14576 /* If all the glyphs of this row came from strings, put the
14577 cursor on the first glyph of the row. This avoids having the
14578 cursor outside of the text area in this very rare and hard
14579 use case. */
14580 glyph =
14581 row->reversed_p
14582 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14583 : row->glyphs[TEXT_AREA];
14585 if (x < 0)
14587 struct glyph *g;
14589 /* Need to compute x that corresponds to GLYPH. */
14590 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14592 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14593 emacs_abort ();
14594 x += g->pixel_width;
14598 /* ROW could be part of a continued line, which, under bidi
14599 reordering, might have other rows whose start and end charpos
14600 occlude point. Only set w->cursor if we found a better
14601 approximation to the cursor position than we have from previously
14602 examined candidate rows belonging to the same continued line. */
14603 if (/* We already have a candidate row. */
14604 w->cursor.vpos >= 0
14605 /* That candidate is not the row we are processing. */
14606 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14607 /* Make sure cursor.vpos specifies a row whose start and end
14608 charpos occlude point, and it is valid candidate for being a
14609 cursor-row. This is because some callers of this function
14610 leave cursor.vpos at the row where the cursor was displayed
14611 during the last redisplay cycle. */
14612 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14613 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14614 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14616 struct glyph *g1
14617 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14619 /* Don't consider glyphs that are outside TEXT_AREA. */
14620 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14621 return 0;
14622 /* Keep the candidate whose buffer position is the closest to
14623 point or has the `cursor' property. */
14624 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14625 w->cursor.hpos >= 0
14626 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14627 && ((BUFFERP (g1->object)
14628 && (g1->charpos == pt_old /* An exact match always wins. */
14629 || (BUFFERP (glyph->object)
14630 && eabs (g1->charpos - pt_old)
14631 < eabs (glyph->charpos - pt_old))))
14632 /* Previous candidate is a glyph from a string that has
14633 a non-nil `cursor' property. */
14634 || (STRINGP (g1->object)
14635 && (!NILP (Fget_char_property (make_number (g1->charpos),
14636 Qcursor, g1->object))
14637 /* Previous candidate is from the same display
14638 string as this one, and the display string
14639 came from a text property. */
14640 || (EQ (g1->object, glyph->object)
14641 && string_from_text_prop)
14642 /* this candidate is from newline and its
14643 position is not an exact match */
14644 || (INTEGERP (glyph->object)
14645 && glyph->charpos != pt_old)))))
14646 return 0;
14647 /* If this candidate gives an exact match, use that. */
14648 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14649 /* If this candidate is a glyph created for the
14650 terminating newline of a line, and point is on that
14651 newline, it wins because it's an exact match. */
14652 || (!row->continued_p
14653 && INTEGERP (glyph->object)
14654 && glyph->charpos == 0
14655 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14656 /* Otherwise, keep the candidate that comes from a row
14657 spanning less buffer positions. This may win when one or
14658 both candidate positions are on glyphs that came from
14659 display strings, for which we cannot compare buffer
14660 positions. */
14661 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14662 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14663 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14664 return 0;
14666 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14667 w->cursor.x = x;
14668 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14669 w->cursor.y = row->y + dy;
14671 if (w == XWINDOW (selected_window))
14673 if (!row->continued_p
14674 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14675 && row->x == 0)
14677 this_line_buffer = XBUFFER (w->contents);
14679 CHARPOS (this_line_start_pos)
14680 = MATRIX_ROW_START_CHARPOS (row) + delta;
14681 BYTEPOS (this_line_start_pos)
14682 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14684 CHARPOS (this_line_end_pos)
14685 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14686 BYTEPOS (this_line_end_pos)
14687 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14689 this_line_y = w->cursor.y;
14690 this_line_pixel_height = row->height;
14691 this_line_vpos = w->cursor.vpos;
14692 this_line_start_x = row->x;
14694 else
14695 CHARPOS (this_line_start_pos) = 0;
14698 return 1;
14702 /* Run window scroll functions, if any, for WINDOW with new window
14703 start STARTP. Sets the window start of WINDOW to that position.
14705 We assume that the window's buffer is really current. */
14707 static struct text_pos
14708 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14710 struct window *w = XWINDOW (window);
14711 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14713 eassert (current_buffer == XBUFFER (w->contents));
14715 if (!NILP (Vwindow_scroll_functions))
14717 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14718 make_number (CHARPOS (startp)));
14719 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14720 /* In case the hook functions switch buffers. */
14721 set_buffer_internal (XBUFFER (w->contents));
14724 return startp;
14728 /* Make sure the line containing the cursor is fully visible.
14729 A value of 1 means there is nothing to be done.
14730 (Either the line is fully visible, or it cannot be made so,
14731 or we cannot tell.)
14733 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14734 is higher than window.
14736 A value of 0 means the caller should do scrolling
14737 as if point had gone off the screen. */
14739 static int
14740 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14742 struct glyph_matrix *matrix;
14743 struct glyph_row *row;
14744 int window_height;
14746 if (!make_cursor_line_fully_visible_p)
14747 return 1;
14749 /* It's not always possible to find the cursor, e.g, when a window
14750 is full of overlay strings. Don't do anything in that case. */
14751 if (w->cursor.vpos < 0)
14752 return 1;
14754 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14755 row = MATRIX_ROW (matrix, w->cursor.vpos);
14757 /* If the cursor row is not partially visible, there's nothing to do. */
14758 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14759 return 1;
14761 /* If the row the cursor is in is taller than the window's height,
14762 it's not clear what to do, so do nothing. */
14763 window_height = window_box_height (w);
14764 if (row->height >= window_height)
14766 if (!force_p || MINI_WINDOW_P (w)
14767 || w->vscroll || w->cursor.vpos == 0)
14768 return 1;
14770 return 0;
14774 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14775 non-zero means only WINDOW is redisplayed in redisplay_internal.
14776 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14777 in redisplay_window to bring a partially visible line into view in
14778 the case that only the cursor has moved.
14780 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14781 last screen line's vertical height extends past the end of the screen.
14783 Value is
14785 1 if scrolling succeeded
14787 0 if scrolling didn't find point.
14789 -1 if new fonts have been loaded so that we must interrupt
14790 redisplay, adjust glyph matrices, and try again. */
14792 enum
14794 SCROLLING_SUCCESS,
14795 SCROLLING_FAILED,
14796 SCROLLING_NEED_LARGER_MATRICES
14799 /* If scroll-conservatively is more than this, never recenter.
14801 If you change this, don't forget to update the doc string of
14802 `scroll-conservatively' and the Emacs manual. */
14803 #define SCROLL_LIMIT 100
14805 static int
14806 try_scrolling (Lisp_Object window, int just_this_one_p,
14807 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14808 int temp_scroll_step, int last_line_misfit)
14810 struct window *w = XWINDOW (window);
14811 struct frame *f = XFRAME (w->frame);
14812 struct text_pos pos, startp;
14813 struct it it;
14814 int this_scroll_margin, scroll_max, rc, height;
14815 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14816 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14817 Lisp_Object aggressive;
14818 /* We will never try scrolling more than this number of lines. */
14819 int scroll_limit = SCROLL_LIMIT;
14820 int frame_line_height = default_line_pixel_height (w);
14821 int window_total_lines
14822 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14824 #ifdef GLYPH_DEBUG
14825 debug_method_add (w, "try_scrolling");
14826 #endif
14828 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14830 /* Compute scroll margin height in pixels. We scroll when point is
14831 within this distance from the top or bottom of the window. */
14832 if (scroll_margin > 0)
14833 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14834 * frame_line_height;
14835 else
14836 this_scroll_margin = 0;
14838 /* Force arg_scroll_conservatively to have a reasonable value, to
14839 avoid scrolling too far away with slow move_it_* functions. Note
14840 that the user can supply scroll-conservatively equal to
14841 `most-positive-fixnum', which can be larger than INT_MAX. */
14842 if (arg_scroll_conservatively > scroll_limit)
14844 arg_scroll_conservatively = scroll_limit + 1;
14845 scroll_max = scroll_limit * frame_line_height;
14847 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14848 /* Compute how much we should try to scroll maximally to bring
14849 point into view. */
14850 scroll_max = (max (scroll_step,
14851 max (arg_scroll_conservatively, temp_scroll_step))
14852 * frame_line_height);
14853 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14854 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14855 /* We're trying to scroll because of aggressive scrolling but no
14856 scroll_step is set. Choose an arbitrary one. */
14857 scroll_max = 10 * frame_line_height;
14858 else
14859 scroll_max = 0;
14861 too_near_end:
14863 /* Decide whether to scroll down. */
14864 if (PT > CHARPOS (startp))
14866 int scroll_margin_y;
14868 /* Compute the pixel ypos of the scroll margin, then move IT to
14869 either that ypos or PT, whichever comes first. */
14870 start_display (&it, w, startp);
14871 scroll_margin_y = it.last_visible_y - this_scroll_margin
14872 - frame_line_height * extra_scroll_margin_lines;
14873 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14874 (MOVE_TO_POS | MOVE_TO_Y));
14876 if (PT > CHARPOS (it.current.pos))
14878 int y0 = line_bottom_y (&it);
14879 /* Compute how many pixels below window bottom to stop searching
14880 for PT. This avoids costly search for PT that is far away if
14881 the user limited scrolling by a small number of lines, but
14882 always finds PT if scroll_conservatively is set to a large
14883 number, such as most-positive-fixnum. */
14884 int slack = max (scroll_max, 10 * frame_line_height);
14885 int y_to_move = it.last_visible_y + slack;
14887 /* Compute the distance from the scroll margin to PT or to
14888 the scroll limit, whichever comes first. This should
14889 include the height of the cursor line, to make that line
14890 fully visible. */
14891 move_it_to (&it, PT, -1, y_to_move,
14892 -1, MOVE_TO_POS | MOVE_TO_Y);
14893 dy = line_bottom_y (&it) - y0;
14895 if (dy > scroll_max)
14896 return SCROLLING_FAILED;
14898 if (dy > 0)
14899 scroll_down_p = 1;
14903 if (scroll_down_p)
14905 /* Point is in or below the bottom scroll margin, so move the
14906 window start down. If scrolling conservatively, move it just
14907 enough down to make point visible. If scroll_step is set,
14908 move it down by scroll_step. */
14909 if (arg_scroll_conservatively)
14910 amount_to_scroll
14911 = min (max (dy, frame_line_height),
14912 frame_line_height * arg_scroll_conservatively);
14913 else if (scroll_step || temp_scroll_step)
14914 amount_to_scroll = scroll_max;
14915 else
14917 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14918 height = WINDOW_BOX_TEXT_HEIGHT (w);
14919 if (NUMBERP (aggressive))
14921 double float_amount = XFLOATINT (aggressive) * height;
14922 int aggressive_scroll = float_amount;
14923 if (aggressive_scroll == 0 && float_amount > 0)
14924 aggressive_scroll = 1;
14925 /* Don't let point enter the scroll margin near top of
14926 the window. This could happen if the value of
14927 scroll_up_aggressively is too large and there are
14928 non-zero margins, because scroll_up_aggressively
14929 means put point that fraction of window height
14930 _from_the_bottom_margin_. */
14931 if (aggressive_scroll + 2*this_scroll_margin > height)
14932 aggressive_scroll = height - 2*this_scroll_margin;
14933 amount_to_scroll = dy + aggressive_scroll;
14937 if (amount_to_scroll <= 0)
14938 return SCROLLING_FAILED;
14940 start_display (&it, w, startp);
14941 if (arg_scroll_conservatively <= scroll_limit)
14942 move_it_vertically (&it, amount_to_scroll);
14943 else
14945 /* Extra precision for users who set scroll-conservatively
14946 to a large number: make sure the amount we scroll
14947 the window start is never less than amount_to_scroll,
14948 which was computed as distance from window bottom to
14949 point. This matters when lines at window top and lines
14950 below window bottom have different height. */
14951 struct it it1;
14952 void *it1data = NULL;
14953 /* We use a temporary it1 because line_bottom_y can modify
14954 its argument, if it moves one line down; see there. */
14955 int start_y;
14957 SAVE_IT (it1, it, it1data);
14958 start_y = line_bottom_y (&it1);
14959 do {
14960 RESTORE_IT (&it, &it, it1data);
14961 move_it_by_lines (&it, 1);
14962 SAVE_IT (it1, it, it1data);
14963 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14966 /* If STARTP is unchanged, move it down another screen line. */
14967 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14968 move_it_by_lines (&it, 1);
14969 startp = it.current.pos;
14971 else
14973 struct text_pos scroll_margin_pos = startp;
14974 int y_offset = 0;
14976 /* See if point is inside the scroll margin at the top of the
14977 window. */
14978 if (this_scroll_margin)
14980 int y_start;
14982 start_display (&it, w, startp);
14983 y_start = it.current_y;
14984 move_it_vertically (&it, this_scroll_margin);
14985 scroll_margin_pos = it.current.pos;
14986 /* If we didn't move enough before hitting ZV, request
14987 additional amount of scroll, to move point out of the
14988 scroll margin. */
14989 if (IT_CHARPOS (it) == ZV
14990 && it.current_y - y_start < this_scroll_margin)
14991 y_offset = this_scroll_margin - (it.current_y - y_start);
14994 if (PT < CHARPOS (scroll_margin_pos))
14996 /* Point is in the scroll margin at the top of the window or
14997 above what is displayed in the window. */
14998 int y0, y_to_move;
15000 /* Compute the vertical distance from PT to the scroll
15001 margin position. Move as far as scroll_max allows, or
15002 one screenful, or 10 screen lines, whichever is largest.
15003 Give up if distance is greater than scroll_max or if we
15004 didn't reach the scroll margin position. */
15005 SET_TEXT_POS (pos, PT, PT_BYTE);
15006 start_display (&it, w, pos);
15007 y0 = it.current_y;
15008 y_to_move = max (it.last_visible_y,
15009 max (scroll_max, 10 * frame_line_height));
15010 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15011 y_to_move, -1,
15012 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15013 dy = it.current_y - y0;
15014 if (dy > scroll_max
15015 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15016 return SCROLLING_FAILED;
15018 /* Additional scroll for when ZV was too close to point. */
15019 dy += y_offset;
15021 /* Compute new window start. */
15022 start_display (&it, w, startp);
15024 if (arg_scroll_conservatively)
15025 amount_to_scroll = max (dy, frame_line_height *
15026 max (scroll_step, temp_scroll_step));
15027 else if (scroll_step || temp_scroll_step)
15028 amount_to_scroll = scroll_max;
15029 else
15031 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15032 height = WINDOW_BOX_TEXT_HEIGHT (w);
15033 if (NUMBERP (aggressive))
15035 double float_amount = XFLOATINT (aggressive) * height;
15036 int aggressive_scroll = float_amount;
15037 if (aggressive_scroll == 0 && float_amount > 0)
15038 aggressive_scroll = 1;
15039 /* Don't let point enter the scroll margin near
15040 bottom of the window, if the value of
15041 scroll_down_aggressively happens to be too
15042 large. */
15043 if (aggressive_scroll + 2*this_scroll_margin > height)
15044 aggressive_scroll = height - 2*this_scroll_margin;
15045 amount_to_scroll = dy + aggressive_scroll;
15049 if (amount_to_scroll <= 0)
15050 return SCROLLING_FAILED;
15052 move_it_vertically_backward (&it, amount_to_scroll);
15053 startp = it.current.pos;
15057 /* Run window scroll functions. */
15058 startp = run_window_scroll_functions (window, startp);
15060 /* Display the window. Give up if new fonts are loaded, or if point
15061 doesn't appear. */
15062 if (!try_window (window, startp, 0))
15063 rc = SCROLLING_NEED_LARGER_MATRICES;
15064 else if (w->cursor.vpos < 0)
15066 clear_glyph_matrix (w->desired_matrix);
15067 rc = SCROLLING_FAILED;
15069 else
15071 /* Maybe forget recorded base line for line number display. */
15072 if (!just_this_one_p
15073 || current_buffer->clip_changed
15074 || BEG_UNCHANGED < CHARPOS (startp))
15075 w->base_line_number = 0;
15077 /* If cursor ends up on a partially visible line,
15078 treat that as being off the bottom of the screen. */
15079 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15080 /* It's possible that the cursor is on the first line of the
15081 buffer, which is partially obscured due to a vscroll
15082 (Bug#7537). In that case, avoid looping forever. */
15083 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15085 clear_glyph_matrix (w->desired_matrix);
15086 ++extra_scroll_margin_lines;
15087 goto too_near_end;
15089 rc = SCROLLING_SUCCESS;
15092 return rc;
15096 /* Compute a suitable window start for window W if display of W starts
15097 on a continuation line. Value is non-zero if a new window start
15098 was computed.
15100 The new window start will be computed, based on W's width, starting
15101 from the start of the continued line. It is the start of the
15102 screen line with the minimum distance from the old start W->start. */
15104 static int
15105 compute_window_start_on_continuation_line (struct window *w)
15107 struct text_pos pos, start_pos;
15108 int window_start_changed_p = 0;
15110 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15112 /* If window start is on a continuation line... Window start may be
15113 < BEGV in case there's invisible text at the start of the
15114 buffer (M-x rmail, for example). */
15115 if (CHARPOS (start_pos) > BEGV
15116 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15118 struct it it;
15119 struct glyph_row *row;
15121 /* Handle the case that the window start is out of range. */
15122 if (CHARPOS (start_pos) < BEGV)
15123 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15124 else if (CHARPOS (start_pos) > ZV)
15125 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15127 /* Find the start of the continued line. This should be fast
15128 because find_newline is fast (newline cache). */
15129 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15130 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15131 row, DEFAULT_FACE_ID);
15132 reseat_at_previous_visible_line_start (&it);
15134 /* If the line start is "too far" away from the window start,
15135 say it takes too much time to compute a new window start. */
15136 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15137 /* PXW: Do we need upper bounds here? */
15138 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15140 int min_distance, distance;
15142 /* Move forward by display lines to find the new window
15143 start. If window width was enlarged, the new start can
15144 be expected to be > the old start. If window width was
15145 decreased, the new window start will be < the old start.
15146 So, we're looking for the display line start with the
15147 minimum distance from the old window start. */
15148 pos = it.current.pos;
15149 min_distance = INFINITY;
15150 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15151 distance < min_distance)
15153 min_distance = distance;
15154 pos = it.current.pos;
15155 if (it.line_wrap == WORD_WRAP)
15157 /* Under WORD_WRAP, move_it_by_lines is likely to
15158 overshoot and stop not at the first, but the
15159 second character from the left margin. So in
15160 that case, we need a more tight control on the X
15161 coordinate of the iterator than move_it_by_lines
15162 promises in its contract. The method is to first
15163 go to the last (rightmost) visible character of a
15164 line, then move to the leftmost character on the
15165 next line in a separate call. */
15166 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15167 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15168 move_it_to (&it, ZV, 0,
15169 it.current_y + it.max_ascent + it.max_descent, -1,
15170 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15172 else
15173 move_it_by_lines (&it, 1);
15176 /* Set the window start there. */
15177 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15178 window_start_changed_p = 1;
15182 return window_start_changed_p;
15186 /* Try cursor movement in case text has not changed in window WINDOW,
15187 with window start STARTP. Value is
15189 CURSOR_MOVEMENT_SUCCESS if successful
15191 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15193 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15194 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15195 we want to scroll as if scroll-step were set to 1. See the code.
15197 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15198 which case we have to abort this redisplay, and adjust matrices
15199 first. */
15201 enum
15203 CURSOR_MOVEMENT_SUCCESS,
15204 CURSOR_MOVEMENT_CANNOT_BE_USED,
15205 CURSOR_MOVEMENT_MUST_SCROLL,
15206 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15209 static int
15210 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15212 struct window *w = XWINDOW (window);
15213 struct frame *f = XFRAME (w->frame);
15214 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15216 #ifdef GLYPH_DEBUG
15217 if (inhibit_try_cursor_movement)
15218 return rc;
15219 #endif
15221 /* Previously, there was a check for Lisp integer in the
15222 if-statement below. Now, this field is converted to
15223 ptrdiff_t, thus zero means invalid position in a buffer. */
15224 eassert (w->last_point > 0);
15225 /* Likewise there was a check whether window_end_vpos is nil or larger
15226 than the window. Now window_end_vpos is int and so never nil, but
15227 let's leave eassert to check whether it fits in the window. */
15228 eassert (w->window_end_vpos < w->current_matrix->nrows);
15230 /* Handle case where text has not changed, only point, and it has
15231 not moved off the frame. */
15232 if (/* Point may be in this window. */
15233 PT >= CHARPOS (startp)
15234 /* Selective display hasn't changed. */
15235 && !current_buffer->clip_changed
15236 /* Function force-mode-line-update is used to force a thorough
15237 redisplay. It sets either windows_or_buffers_changed or
15238 update_mode_lines. So don't take a shortcut here for these
15239 cases. */
15240 && !update_mode_lines
15241 && !windows_or_buffers_changed
15242 && !f->cursor_type_changed
15243 && NILP (Vshow_trailing_whitespace)
15244 /* This code is not used for mini-buffer for the sake of the case
15245 of redisplaying to replace an echo area message; since in
15246 that case the mini-buffer contents per se are usually
15247 unchanged. This code is of no real use in the mini-buffer
15248 since the handling of this_line_start_pos, etc., in redisplay
15249 handles the same cases. */
15250 && !EQ (window, minibuf_window)
15251 && (FRAME_WINDOW_P (f)
15252 || !overlay_arrow_in_current_buffer_p ()))
15254 int this_scroll_margin, top_scroll_margin;
15255 struct glyph_row *row = NULL;
15256 int frame_line_height = default_line_pixel_height (w);
15257 int window_total_lines
15258 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15260 #ifdef GLYPH_DEBUG
15261 debug_method_add (w, "cursor movement");
15262 #endif
15264 /* Scroll if point within this distance from the top or bottom
15265 of the window. This is a pixel value. */
15266 if (scroll_margin > 0)
15268 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15269 this_scroll_margin *= frame_line_height;
15271 else
15272 this_scroll_margin = 0;
15274 top_scroll_margin = this_scroll_margin;
15275 if (WINDOW_WANTS_HEADER_LINE_P (w))
15276 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15278 /* Start with the row the cursor was displayed during the last
15279 not paused redisplay. Give up if that row is not valid. */
15280 if (w->last_cursor_vpos < 0
15281 || w->last_cursor_vpos >= w->current_matrix->nrows)
15282 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15283 else
15285 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15286 if (row->mode_line_p)
15287 ++row;
15288 if (!row->enabled_p)
15289 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15292 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15294 int scroll_p = 0, must_scroll = 0;
15295 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15297 if (PT > w->last_point)
15299 /* Point has moved forward. */
15300 while (MATRIX_ROW_END_CHARPOS (row) < PT
15301 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15303 eassert (row->enabled_p);
15304 ++row;
15307 /* If the end position of a row equals the start
15308 position of the next row, and PT is at that position,
15309 we would rather display cursor in the next line. */
15310 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15311 && MATRIX_ROW_END_CHARPOS (row) == PT
15312 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15313 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15314 && !cursor_row_p (row))
15315 ++row;
15317 /* If within the scroll margin, scroll. Note that
15318 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15319 the next line would be drawn, and that
15320 this_scroll_margin can be zero. */
15321 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15322 || PT > MATRIX_ROW_END_CHARPOS (row)
15323 /* Line is completely visible last line in window
15324 and PT is to be set in the next line. */
15325 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15326 && PT == MATRIX_ROW_END_CHARPOS (row)
15327 && !row->ends_at_zv_p
15328 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15329 scroll_p = 1;
15331 else if (PT < w->last_point)
15333 /* Cursor has to be moved backward. Note that PT >=
15334 CHARPOS (startp) because of the outer if-statement. */
15335 while (!row->mode_line_p
15336 && (MATRIX_ROW_START_CHARPOS (row) > PT
15337 || (MATRIX_ROW_START_CHARPOS (row) == PT
15338 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15339 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15340 row > w->current_matrix->rows
15341 && (row-1)->ends_in_newline_from_string_p))))
15342 && (row->y > top_scroll_margin
15343 || CHARPOS (startp) == BEGV))
15345 eassert (row->enabled_p);
15346 --row;
15349 /* Consider the following case: Window starts at BEGV,
15350 there is invisible, intangible text at BEGV, so that
15351 display starts at some point START > BEGV. It can
15352 happen that we are called with PT somewhere between
15353 BEGV and START. Try to handle that case. */
15354 if (row < w->current_matrix->rows
15355 || row->mode_line_p)
15357 row = w->current_matrix->rows;
15358 if (row->mode_line_p)
15359 ++row;
15362 /* Due to newlines in overlay strings, we may have to
15363 skip forward over overlay strings. */
15364 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15365 && MATRIX_ROW_END_CHARPOS (row) == PT
15366 && !cursor_row_p (row))
15367 ++row;
15369 /* If within the scroll margin, scroll. */
15370 if (row->y < top_scroll_margin
15371 && CHARPOS (startp) != BEGV)
15372 scroll_p = 1;
15374 else
15376 /* Cursor did not move. So don't scroll even if cursor line
15377 is partially visible, as it was so before. */
15378 rc = CURSOR_MOVEMENT_SUCCESS;
15381 if (PT < MATRIX_ROW_START_CHARPOS (row)
15382 || PT > MATRIX_ROW_END_CHARPOS (row))
15384 /* if PT is not in the glyph row, give up. */
15385 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15386 must_scroll = 1;
15388 else if (rc != CURSOR_MOVEMENT_SUCCESS
15389 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15391 struct glyph_row *row1;
15393 /* If rows are bidi-reordered and point moved, back up
15394 until we find a row that does not belong to a
15395 continuation line. This is because we must consider
15396 all rows of a continued line as candidates for the
15397 new cursor positioning, since row start and end
15398 positions change non-linearly with vertical position
15399 in such rows. */
15400 /* FIXME: Revisit this when glyph ``spilling'' in
15401 continuation lines' rows is implemented for
15402 bidi-reordered rows. */
15403 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15404 MATRIX_ROW_CONTINUATION_LINE_P (row);
15405 --row)
15407 /* If we hit the beginning of the displayed portion
15408 without finding the first row of a continued
15409 line, give up. */
15410 if (row <= row1)
15412 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15413 break;
15415 eassert (row->enabled_p);
15418 if (must_scroll)
15420 else if (rc != CURSOR_MOVEMENT_SUCCESS
15421 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15422 /* Make sure this isn't a header line by any chance, since
15423 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15424 && !row->mode_line_p
15425 && make_cursor_line_fully_visible_p)
15427 if (PT == MATRIX_ROW_END_CHARPOS (row)
15428 && !row->ends_at_zv_p
15429 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15430 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15431 else if (row->height > window_box_height (w))
15433 /* If we end up in a partially visible line, let's
15434 make it fully visible, except when it's taller
15435 than the window, in which case we can't do much
15436 about it. */
15437 *scroll_step = 1;
15438 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15440 else
15442 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15443 if (!cursor_row_fully_visible_p (w, 0, 1))
15444 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15445 else
15446 rc = CURSOR_MOVEMENT_SUCCESS;
15449 else if (scroll_p)
15450 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15451 else if (rc != CURSOR_MOVEMENT_SUCCESS
15452 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15454 /* With bidi-reordered rows, there could be more than
15455 one candidate row whose start and end positions
15456 occlude point. We need to let set_cursor_from_row
15457 find the best candidate. */
15458 /* FIXME: Revisit this when glyph ``spilling'' in
15459 continuation lines' rows is implemented for
15460 bidi-reordered rows. */
15461 int rv = 0;
15465 int at_zv_p = 0, exact_match_p = 0;
15467 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15468 && PT <= MATRIX_ROW_END_CHARPOS (row)
15469 && cursor_row_p (row))
15470 rv |= set_cursor_from_row (w, row, w->current_matrix,
15471 0, 0, 0, 0);
15472 /* As soon as we've found the exact match for point,
15473 or the first suitable row whose ends_at_zv_p flag
15474 is set, we are done. */
15475 at_zv_p =
15476 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15477 if (rv && !at_zv_p
15478 && w->cursor.hpos >= 0
15479 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15480 w->cursor.vpos))
15482 struct glyph_row *candidate =
15483 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15484 struct glyph *g =
15485 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15486 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15488 exact_match_p =
15489 (BUFFERP (g->object) && g->charpos == PT)
15490 || (INTEGERP (g->object)
15491 && (g->charpos == PT
15492 || (g->charpos == 0 && endpos - 1 == PT)));
15494 if (rv && (at_zv_p || exact_match_p))
15496 rc = CURSOR_MOVEMENT_SUCCESS;
15497 break;
15499 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15500 break;
15501 ++row;
15503 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15504 || row->continued_p)
15505 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15506 || (MATRIX_ROW_START_CHARPOS (row) == PT
15507 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15508 /* If we didn't find any candidate rows, or exited the
15509 loop before all the candidates were examined, signal
15510 to the caller that this method failed. */
15511 if (rc != CURSOR_MOVEMENT_SUCCESS
15512 && !(rv
15513 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15514 && !row->continued_p))
15515 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15516 else if (rv)
15517 rc = CURSOR_MOVEMENT_SUCCESS;
15519 else
15523 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15525 rc = CURSOR_MOVEMENT_SUCCESS;
15526 break;
15528 ++row;
15530 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15531 && MATRIX_ROW_START_CHARPOS (row) == PT
15532 && cursor_row_p (row));
15537 return rc;
15540 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15541 static
15542 #endif
15543 void
15544 set_vertical_scroll_bar (struct window *w)
15546 ptrdiff_t start, end, whole;
15548 /* Calculate the start and end positions for the current window.
15549 At some point, it would be nice to choose between scrollbars
15550 which reflect the whole buffer size, with special markers
15551 indicating narrowing, and scrollbars which reflect only the
15552 visible region.
15554 Note that mini-buffers sometimes aren't displaying any text. */
15555 if (!MINI_WINDOW_P (w)
15556 || (w == XWINDOW (minibuf_window)
15557 && NILP (echo_area_buffer[0])))
15559 struct buffer *buf = XBUFFER (w->contents);
15560 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15561 start = marker_position (w->start) - BUF_BEGV (buf);
15562 /* I don't think this is guaranteed to be right. For the
15563 moment, we'll pretend it is. */
15564 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15566 if (end < start)
15567 end = start;
15568 if (whole < (end - start))
15569 whole = end - start;
15571 else
15572 start = end = whole = 0;
15574 /* Indicate what this scroll bar ought to be displaying now. */
15575 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15576 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15577 (w, end - start, whole, start);
15581 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15582 selected_window is redisplayed.
15584 We can return without actually redisplaying the window if fonts has been
15585 changed on window's frame. In that case, redisplay_internal will retry. */
15587 static void
15588 redisplay_window (Lisp_Object window, bool just_this_one_p)
15590 struct window *w = XWINDOW (window);
15591 struct frame *f = XFRAME (w->frame);
15592 struct buffer *buffer = XBUFFER (w->contents);
15593 struct buffer *old = current_buffer;
15594 struct text_pos lpoint, opoint, startp;
15595 int update_mode_line;
15596 int tem;
15597 struct it it;
15598 /* Record it now because it's overwritten. */
15599 bool current_matrix_up_to_date_p = false;
15600 bool used_current_matrix_p = false;
15601 /* This is less strict than current_matrix_up_to_date_p.
15602 It indicates that the buffer contents and narrowing are unchanged. */
15603 bool buffer_unchanged_p = false;
15604 int temp_scroll_step = 0;
15605 ptrdiff_t count = SPECPDL_INDEX ();
15606 int rc;
15607 int centering_position = -1;
15608 int last_line_misfit = 0;
15609 ptrdiff_t beg_unchanged, end_unchanged;
15610 int frame_line_height;
15612 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15613 opoint = lpoint;
15615 #ifdef GLYPH_DEBUG
15616 *w->desired_matrix->method = 0;
15617 #endif
15619 if (!just_this_one_p
15620 && REDISPLAY_SOME_P ()
15621 && !w->redisplay
15622 && !f->redisplay
15623 && !buffer->text->redisplay)
15624 return;
15626 /* Make sure that both W's markers are valid. */
15627 eassert (XMARKER (w->start)->buffer == buffer);
15628 eassert (XMARKER (w->pointm)->buffer == buffer);
15630 restart:
15631 reconsider_clip_changes (w);
15632 frame_line_height = default_line_pixel_height (w);
15634 /* Has the mode line to be updated? */
15635 update_mode_line = (w->update_mode_line
15636 || update_mode_lines
15637 || buffer->clip_changed
15638 || buffer->prevent_redisplay_optimizations_p);
15640 if (!just_this_one_p)
15641 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15642 cleverly elsewhere. */
15643 w->must_be_updated_p = true;
15645 if (MINI_WINDOW_P (w))
15647 if (w == XWINDOW (echo_area_window)
15648 && !NILP (echo_area_buffer[0]))
15650 if (update_mode_line)
15651 /* We may have to update a tty frame's menu bar or a
15652 tool-bar. Example `M-x C-h C-h C-g'. */
15653 goto finish_menu_bars;
15654 else
15655 /* We've already displayed the echo area glyphs in this window. */
15656 goto finish_scroll_bars;
15658 else if ((w != XWINDOW (minibuf_window)
15659 || minibuf_level == 0)
15660 /* When buffer is nonempty, redisplay window normally. */
15661 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15662 /* Quail displays non-mini buffers in minibuffer window.
15663 In that case, redisplay the window normally. */
15664 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15666 /* W is a mini-buffer window, but it's not active, so clear
15667 it. */
15668 int yb = window_text_bottom_y (w);
15669 struct glyph_row *row;
15670 int y;
15672 for (y = 0, row = w->desired_matrix->rows;
15673 y < yb;
15674 y += row->height, ++row)
15675 blank_row (w, row, y);
15676 goto finish_scroll_bars;
15679 clear_glyph_matrix (w->desired_matrix);
15682 /* Otherwise set up data on this window; select its buffer and point
15683 value. */
15684 /* Really select the buffer, for the sake of buffer-local
15685 variables. */
15686 set_buffer_internal_1 (XBUFFER (w->contents));
15688 current_matrix_up_to_date_p
15689 = (w->window_end_valid
15690 && !current_buffer->clip_changed
15691 && !current_buffer->prevent_redisplay_optimizations_p
15692 && !window_outdated (w));
15694 /* Run the window-bottom-change-functions
15695 if it is possible that the text on the screen has changed
15696 (either due to modification of the text, or any other reason). */
15697 if (!current_matrix_up_to_date_p
15698 && !NILP (Vwindow_text_change_functions))
15700 safe_run_hooks (Qwindow_text_change_functions);
15701 goto restart;
15704 beg_unchanged = BEG_UNCHANGED;
15705 end_unchanged = END_UNCHANGED;
15707 SET_TEXT_POS (opoint, PT, PT_BYTE);
15709 specbind (Qinhibit_point_motion_hooks, Qt);
15711 buffer_unchanged_p
15712 = (w->window_end_valid
15713 && !current_buffer->clip_changed
15714 && !window_outdated (w));
15716 /* When windows_or_buffers_changed is non-zero, we can't rely
15717 on the window end being valid, so set it to zero there. */
15718 if (windows_or_buffers_changed)
15720 /* If window starts on a continuation line, maybe adjust the
15721 window start in case the window's width changed. */
15722 if (XMARKER (w->start)->buffer == current_buffer)
15723 compute_window_start_on_continuation_line (w);
15725 w->window_end_valid = false;
15726 /* If so, we also can't rely on current matrix
15727 and should not fool try_cursor_movement below. */
15728 current_matrix_up_to_date_p = false;
15731 /* Some sanity checks. */
15732 CHECK_WINDOW_END (w);
15733 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15734 emacs_abort ();
15735 if (BYTEPOS (opoint) < CHARPOS (opoint))
15736 emacs_abort ();
15738 if (mode_line_update_needed (w))
15739 update_mode_line = 1;
15741 /* Point refers normally to the selected window. For any other
15742 window, set up appropriate value. */
15743 if (!EQ (window, selected_window))
15745 ptrdiff_t new_pt = marker_position (w->pointm);
15746 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15747 if (new_pt < BEGV)
15749 new_pt = BEGV;
15750 new_pt_byte = BEGV_BYTE;
15751 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15753 else if (new_pt > (ZV - 1))
15755 new_pt = ZV;
15756 new_pt_byte = ZV_BYTE;
15757 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15760 /* We don't use SET_PT so that the point-motion hooks don't run. */
15761 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15764 /* If any of the character widths specified in the display table
15765 have changed, invalidate the width run cache. It's true that
15766 this may be a bit late to catch such changes, but the rest of
15767 redisplay goes (non-fatally) haywire when the display table is
15768 changed, so why should we worry about doing any better? */
15769 if (current_buffer->width_run_cache)
15771 struct Lisp_Char_Table *disptab = buffer_display_table ();
15773 if (! disptab_matches_widthtab
15774 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15776 invalidate_region_cache (current_buffer,
15777 current_buffer->width_run_cache,
15778 BEG, Z);
15779 recompute_width_table (current_buffer, disptab);
15783 /* If window-start is screwed up, choose a new one. */
15784 if (XMARKER (w->start)->buffer != current_buffer)
15785 goto recenter;
15787 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15789 /* If someone specified a new starting point but did not insist,
15790 check whether it can be used. */
15791 if (w->optional_new_start
15792 && CHARPOS (startp) >= BEGV
15793 && CHARPOS (startp) <= ZV)
15795 w->optional_new_start = 0;
15796 start_display (&it, w, startp);
15797 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15798 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15799 if (IT_CHARPOS (it) == PT)
15800 w->force_start = 1;
15801 /* IT may overshoot PT if text at PT is invisible. */
15802 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15803 w->force_start = 1;
15806 force_start:
15808 /* Handle case where place to start displaying has been specified,
15809 unless the specified location is outside the accessible range. */
15810 if (w->force_start || window_frozen_p (w))
15812 /* We set this later on if we have to adjust point. */
15813 int new_vpos = -1;
15815 w->force_start = 0;
15816 w->vscroll = 0;
15817 w->window_end_valid = 0;
15819 /* Forget any recorded base line for line number display. */
15820 if (!buffer_unchanged_p)
15821 w->base_line_number = 0;
15823 /* Redisplay the mode line. Select the buffer properly for that.
15824 Also, run the hook window-scroll-functions
15825 because we have scrolled. */
15826 /* Note, we do this after clearing force_start because
15827 if there's an error, it is better to forget about force_start
15828 than to get into an infinite loop calling the hook functions
15829 and having them get more errors. */
15830 if (!update_mode_line
15831 || ! NILP (Vwindow_scroll_functions))
15833 update_mode_line = 1;
15834 w->update_mode_line = 1;
15835 startp = run_window_scroll_functions (window, startp);
15838 if (CHARPOS (startp) < BEGV)
15839 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15840 else if (CHARPOS (startp) > ZV)
15841 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15843 /* Redisplay, then check if cursor has been set during the
15844 redisplay. Give up if new fonts were loaded. */
15845 /* We used to issue a CHECK_MARGINS argument to try_window here,
15846 but this causes scrolling to fail when point begins inside
15847 the scroll margin (bug#148) -- cyd */
15848 if (!try_window (window, startp, 0))
15850 w->force_start = 1;
15851 clear_glyph_matrix (w->desired_matrix);
15852 goto need_larger_matrices;
15855 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15857 /* If point does not appear, try to move point so it does
15858 appear. The desired matrix has been built above, so we
15859 can use it here. */
15860 new_vpos = window_box_height (w) / 2;
15863 if (!cursor_row_fully_visible_p (w, 0, 0))
15865 /* Point does appear, but on a line partly visible at end of window.
15866 Move it back to a fully-visible line. */
15867 new_vpos = window_box_height (w);
15869 else if (w->cursor.vpos >= 0)
15871 /* Some people insist on not letting point enter the scroll
15872 margin, even though this part handles windows that didn't
15873 scroll at all. */
15874 int window_total_lines
15875 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15876 int margin = min (scroll_margin, window_total_lines / 4);
15877 int pixel_margin = margin * frame_line_height;
15878 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15880 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15881 below, which finds the row to move point to, advances by
15882 the Y coordinate of the _next_ row, see the definition of
15883 MATRIX_ROW_BOTTOM_Y. */
15884 if (w->cursor.vpos < margin + header_line)
15886 w->cursor.vpos = -1;
15887 clear_glyph_matrix (w->desired_matrix);
15888 goto try_to_scroll;
15890 else
15892 int window_height = window_box_height (w);
15894 if (header_line)
15895 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15896 if (w->cursor.y >= window_height - pixel_margin)
15898 w->cursor.vpos = -1;
15899 clear_glyph_matrix (w->desired_matrix);
15900 goto try_to_scroll;
15905 /* If we need to move point for either of the above reasons,
15906 now actually do it. */
15907 if (new_vpos >= 0)
15909 struct glyph_row *row;
15911 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15912 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15913 ++row;
15915 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15916 MATRIX_ROW_START_BYTEPOS (row));
15918 if (w != XWINDOW (selected_window))
15919 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15920 else if (current_buffer == old)
15921 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15923 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15925 /* If we are highlighting the region, then we just changed
15926 the region, so redisplay to show it. */
15927 /* FIXME: We need to (re)run pre-redisplay-function! */
15928 /* if (markpos_of_region () >= 0)
15930 clear_glyph_matrix (w->desired_matrix);
15931 if (!try_window (window, startp, 0))
15932 goto need_larger_matrices;
15937 #ifdef GLYPH_DEBUG
15938 debug_method_add (w, "forced window start");
15939 #endif
15940 goto done;
15943 /* Handle case where text has not changed, only point, and it has
15944 not moved off the frame, and we are not retrying after hscroll.
15945 (current_matrix_up_to_date_p is nonzero when retrying.) */
15946 if (current_matrix_up_to_date_p
15947 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15948 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15950 switch (rc)
15952 case CURSOR_MOVEMENT_SUCCESS:
15953 used_current_matrix_p = 1;
15954 goto done;
15956 case CURSOR_MOVEMENT_MUST_SCROLL:
15957 goto try_to_scroll;
15959 default:
15960 emacs_abort ();
15963 /* If current starting point was originally the beginning of a line
15964 but no longer is, find a new starting point. */
15965 else if (w->start_at_line_beg
15966 && !(CHARPOS (startp) <= BEGV
15967 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15969 #ifdef GLYPH_DEBUG
15970 debug_method_add (w, "recenter 1");
15971 #endif
15972 goto recenter;
15975 /* Try scrolling with try_window_id. Value is > 0 if update has
15976 been done, it is -1 if we know that the same window start will
15977 not work. It is 0 if unsuccessful for some other reason. */
15978 else if ((tem = try_window_id (w)) != 0)
15980 #ifdef GLYPH_DEBUG
15981 debug_method_add (w, "try_window_id %d", tem);
15982 #endif
15984 if (f->fonts_changed)
15985 goto need_larger_matrices;
15986 if (tem > 0)
15987 goto done;
15989 /* Otherwise try_window_id has returned -1 which means that we
15990 don't want the alternative below this comment to execute. */
15992 else if (CHARPOS (startp) >= BEGV
15993 && CHARPOS (startp) <= ZV
15994 && PT >= CHARPOS (startp)
15995 && (CHARPOS (startp) < ZV
15996 /* Avoid starting at end of buffer. */
15997 || CHARPOS (startp) == BEGV
15998 || !window_outdated (w)))
16000 int d1, d2, d3, d4, d5, d6;
16002 /* If first window line is a continuation line, and window start
16003 is inside the modified region, but the first change is before
16004 current window start, we must select a new window start.
16006 However, if this is the result of a down-mouse event (e.g. by
16007 extending the mouse-drag-overlay), we don't want to select a
16008 new window start, since that would change the position under
16009 the mouse, resulting in an unwanted mouse-movement rather
16010 than a simple mouse-click. */
16011 if (!w->start_at_line_beg
16012 && NILP (do_mouse_tracking)
16013 && CHARPOS (startp) > BEGV
16014 && CHARPOS (startp) > BEG + beg_unchanged
16015 && CHARPOS (startp) <= Z - end_unchanged
16016 /* Even if w->start_at_line_beg is nil, a new window may
16017 start at a line_beg, since that's how set_buffer_window
16018 sets it. So, we need to check the return value of
16019 compute_window_start_on_continuation_line. (See also
16020 bug#197). */
16021 && XMARKER (w->start)->buffer == current_buffer
16022 && compute_window_start_on_continuation_line (w)
16023 /* It doesn't make sense to force the window start like we
16024 do at label force_start if it is already known that point
16025 will not be visible in the resulting window, because
16026 doing so will move point from its correct position
16027 instead of scrolling the window to bring point into view.
16028 See bug#9324. */
16029 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16031 w->force_start = 1;
16032 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16033 goto force_start;
16036 #ifdef GLYPH_DEBUG
16037 debug_method_add (w, "same window start");
16038 #endif
16040 /* Try to redisplay starting at same place as before.
16041 If point has not moved off frame, accept the results. */
16042 if (!current_matrix_up_to_date_p
16043 /* Don't use try_window_reusing_current_matrix in this case
16044 because a window scroll function can have changed the
16045 buffer. */
16046 || !NILP (Vwindow_scroll_functions)
16047 || MINI_WINDOW_P (w)
16048 || !(used_current_matrix_p
16049 = try_window_reusing_current_matrix (w)))
16051 IF_DEBUG (debug_method_add (w, "1"));
16052 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16053 /* -1 means we need to scroll.
16054 0 means we need new matrices, but fonts_changed
16055 is set in that case, so we will detect it below. */
16056 goto try_to_scroll;
16059 if (f->fonts_changed)
16060 goto need_larger_matrices;
16062 if (w->cursor.vpos >= 0)
16064 if (!just_this_one_p
16065 || current_buffer->clip_changed
16066 || BEG_UNCHANGED < CHARPOS (startp))
16067 /* Forget any recorded base line for line number display. */
16068 w->base_line_number = 0;
16070 if (!cursor_row_fully_visible_p (w, 1, 0))
16072 clear_glyph_matrix (w->desired_matrix);
16073 last_line_misfit = 1;
16075 /* Drop through and scroll. */
16076 else
16077 goto done;
16079 else
16080 clear_glyph_matrix (w->desired_matrix);
16083 try_to_scroll:
16085 /* Redisplay the mode line. Select the buffer properly for that. */
16086 if (!update_mode_line)
16088 update_mode_line = 1;
16089 w->update_mode_line = 1;
16092 /* Try to scroll by specified few lines. */
16093 if ((scroll_conservatively
16094 || emacs_scroll_step
16095 || temp_scroll_step
16096 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16097 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16098 && CHARPOS (startp) >= BEGV
16099 && CHARPOS (startp) <= ZV)
16101 /* The function returns -1 if new fonts were loaded, 1 if
16102 successful, 0 if not successful. */
16103 int ss = try_scrolling (window, just_this_one_p,
16104 scroll_conservatively,
16105 emacs_scroll_step,
16106 temp_scroll_step, last_line_misfit);
16107 switch (ss)
16109 case SCROLLING_SUCCESS:
16110 goto done;
16112 case SCROLLING_NEED_LARGER_MATRICES:
16113 goto need_larger_matrices;
16115 case SCROLLING_FAILED:
16116 break;
16118 default:
16119 emacs_abort ();
16123 /* Finally, just choose a place to start which positions point
16124 according to user preferences. */
16126 recenter:
16128 #ifdef GLYPH_DEBUG
16129 debug_method_add (w, "recenter");
16130 #endif
16132 /* Forget any previously recorded base line for line number display. */
16133 if (!buffer_unchanged_p)
16134 w->base_line_number = 0;
16136 /* Determine the window start relative to point. */
16137 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16138 it.current_y = it.last_visible_y;
16139 if (centering_position < 0)
16141 int window_total_lines
16142 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16143 int margin =
16144 scroll_margin > 0
16145 ? min (scroll_margin, window_total_lines / 4)
16146 : 0;
16147 ptrdiff_t margin_pos = CHARPOS (startp);
16148 Lisp_Object aggressive;
16149 int scrolling_up;
16151 /* If there is a scroll margin at the top of the window, find
16152 its character position. */
16153 if (margin
16154 /* Cannot call start_display if startp is not in the
16155 accessible region of the buffer. This can happen when we
16156 have just switched to a different buffer and/or changed
16157 its restriction. In that case, startp is initialized to
16158 the character position 1 (BEGV) because we did not yet
16159 have chance to display the buffer even once. */
16160 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16162 struct it it1;
16163 void *it1data = NULL;
16165 SAVE_IT (it1, it, it1data);
16166 start_display (&it1, w, startp);
16167 move_it_vertically (&it1, margin * frame_line_height);
16168 margin_pos = IT_CHARPOS (it1);
16169 RESTORE_IT (&it, &it, it1data);
16171 scrolling_up = PT > margin_pos;
16172 aggressive =
16173 scrolling_up
16174 ? BVAR (current_buffer, scroll_up_aggressively)
16175 : BVAR (current_buffer, scroll_down_aggressively);
16177 if (!MINI_WINDOW_P (w)
16178 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16180 int pt_offset = 0;
16182 /* Setting scroll-conservatively overrides
16183 scroll-*-aggressively. */
16184 if (!scroll_conservatively && NUMBERP (aggressive))
16186 double float_amount = XFLOATINT (aggressive);
16188 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16189 if (pt_offset == 0 && float_amount > 0)
16190 pt_offset = 1;
16191 if (pt_offset && margin > 0)
16192 margin -= 1;
16194 /* Compute how much to move the window start backward from
16195 point so that point will be displayed where the user
16196 wants it. */
16197 if (scrolling_up)
16199 centering_position = it.last_visible_y;
16200 if (pt_offset)
16201 centering_position -= pt_offset;
16202 centering_position -=
16203 frame_line_height * (1 + margin + (last_line_misfit != 0))
16204 + WINDOW_HEADER_LINE_HEIGHT (w);
16205 /* Don't let point enter the scroll margin near top of
16206 the window. */
16207 if (centering_position < margin * frame_line_height)
16208 centering_position = margin * frame_line_height;
16210 else
16211 centering_position = margin * frame_line_height + pt_offset;
16213 else
16214 /* Set the window start half the height of the window backward
16215 from point. */
16216 centering_position = window_box_height (w) / 2;
16218 move_it_vertically_backward (&it, centering_position);
16220 eassert (IT_CHARPOS (it) >= BEGV);
16222 /* The function move_it_vertically_backward may move over more
16223 than the specified y-distance. If it->w is small, e.g. a
16224 mini-buffer window, we may end up in front of the window's
16225 display area. Start displaying at the start of the line
16226 containing PT in this case. */
16227 if (it.current_y <= 0)
16229 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16230 move_it_vertically_backward (&it, 0);
16231 it.current_y = 0;
16234 it.current_x = it.hpos = 0;
16236 /* Set the window start position here explicitly, to avoid an
16237 infinite loop in case the functions in window-scroll-functions
16238 get errors. */
16239 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16241 /* Run scroll hooks. */
16242 startp = run_window_scroll_functions (window, it.current.pos);
16244 /* Redisplay the window. */
16245 if (!current_matrix_up_to_date_p
16246 || windows_or_buffers_changed
16247 || f->cursor_type_changed
16248 /* Don't use try_window_reusing_current_matrix in this case
16249 because it can have changed the buffer. */
16250 || !NILP (Vwindow_scroll_functions)
16251 || !just_this_one_p
16252 || MINI_WINDOW_P (w)
16253 || !(used_current_matrix_p
16254 = try_window_reusing_current_matrix (w)))
16255 try_window (window, startp, 0);
16257 /* If new fonts have been loaded (due to fontsets), give up. We
16258 have to start a new redisplay since we need to re-adjust glyph
16259 matrices. */
16260 if (f->fonts_changed)
16261 goto need_larger_matrices;
16263 /* If cursor did not appear assume that the middle of the window is
16264 in the first line of the window. Do it again with the next line.
16265 (Imagine a window of height 100, displaying two lines of height
16266 60. Moving back 50 from it->last_visible_y will end in the first
16267 line.) */
16268 if (w->cursor.vpos < 0)
16270 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16272 clear_glyph_matrix (w->desired_matrix);
16273 move_it_by_lines (&it, 1);
16274 try_window (window, it.current.pos, 0);
16276 else if (PT < IT_CHARPOS (it))
16278 clear_glyph_matrix (w->desired_matrix);
16279 move_it_by_lines (&it, -1);
16280 try_window (window, it.current.pos, 0);
16282 else
16284 /* Not much we can do about it. */
16288 /* Consider the following case: Window starts at BEGV, there is
16289 invisible, intangible text at BEGV, so that display starts at
16290 some point START > BEGV. It can happen that we are called with
16291 PT somewhere between BEGV and START. Try to handle that case. */
16292 if (w->cursor.vpos < 0)
16294 struct glyph_row *row = w->current_matrix->rows;
16295 if (row->mode_line_p)
16296 ++row;
16297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16300 if (!cursor_row_fully_visible_p (w, 0, 0))
16302 /* If vscroll is enabled, disable it and try again. */
16303 if (w->vscroll)
16305 w->vscroll = 0;
16306 clear_glyph_matrix (w->desired_matrix);
16307 goto recenter;
16310 /* Users who set scroll-conservatively to a large number want
16311 point just above/below the scroll margin. If we ended up
16312 with point's row partially visible, move the window start to
16313 make that row fully visible and out of the margin. */
16314 if (scroll_conservatively > SCROLL_LIMIT)
16316 int window_total_lines
16317 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16318 int margin =
16319 scroll_margin > 0
16320 ? min (scroll_margin, window_total_lines / 4)
16321 : 0;
16322 int move_down = w->cursor.vpos >= window_total_lines / 2;
16324 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16325 clear_glyph_matrix (w->desired_matrix);
16326 if (1 == try_window (window, it.current.pos,
16327 TRY_WINDOW_CHECK_MARGINS))
16328 goto done;
16331 /* If centering point failed to make the whole line visible,
16332 put point at the top instead. That has to make the whole line
16333 visible, if it can be done. */
16334 if (centering_position == 0)
16335 goto done;
16337 clear_glyph_matrix (w->desired_matrix);
16338 centering_position = 0;
16339 goto recenter;
16342 done:
16344 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16345 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16346 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16348 /* Display the mode line, if we must. */
16349 if ((update_mode_line
16350 /* If window not full width, must redo its mode line
16351 if (a) the window to its side is being redone and
16352 (b) we do a frame-based redisplay. This is a consequence
16353 of how inverted lines are drawn in frame-based redisplay. */
16354 || (!just_this_one_p
16355 && !FRAME_WINDOW_P (f)
16356 && !WINDOW_FULL_WIDTH_P (w))
16357 /* Line number to display. */
16358 || w->base_line_pos > 0
16359 /* Column number is displayed and different from the one displayed. */
16360 || (w->column_number_displayed != -1
16361 && (w->column_number_displayed != current_column ())))
16362 /* This means that the window has a mode line. */
16363 && (WINDOW_WANTS_MODELINE_P (w)
16364 || WINDOW_WANTS_HEADER_LINE_P (w)))
16367 display_mode_lines (w);
16369 /* If mode line height has changed, arrange for a thorough
16370 immediate redisplay using the correct mode line height. */
16371 if (WINDOW_WANTS_MODELINE_P (w)
16372 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16374 f->fonts_changed = 1;
16375 w->mode_line_height = -1;
16376 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16377 = DESIRED_MODE_LINE_HEIGHT (w);
16380 /* If header line height has changed, arrange for a thorough
16381 immediate redisplay using the correct header line height. */
16382 if (WINDOW_WANTS_HEADER_LINE_P (w)
16383 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16385 f->fonts_changed = 1;
16386 w->header_line_height = -1;
16387 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16388 = DESIRED_HEADER_LINE_HEIGHT (w);
16391 if (f->fonts_changed)
16392 goto need_larger_matrices;
16395 if (!line_number_displayed && w->base_line_pos != -1)
16397 w->base_line_pos = 0;
16398 w->base_line_number = 0;
16401 finish_menu_bars:
16403 /* When we reach a frame's selected window, redo the frame's menu bar. */
16404 if (update_mode_line
16405 && EQ (FRAME_SELECTED_WINDOW (f), window))
16407 int redisplay_menu_p = 0;
16409 if (FRAME_WINDOW_P (f))
16411 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16412 || defined (HAVE_NS) || defined (USE_GTK)
16413 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16414 #else
16415 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16416 #endif
16418 else
16419 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16421 if (redisplay_menu_p)
16422 display_menu_bar (w);
16424 #ifdef HAVE_WINDOW_SYSTEM
16425 if (FRAME_WINDOW_P (f))
16427 #if defined (USE_GTK) || defined (HAVE_NS)
16428 if (FRAME_EXTERNAL_TOOL_BAR (f))
16429 redisplay_tool_bar (f);
16430 #else
16431 if (WINDOWP (f->tool_bar_window)
16432 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16433 || !NILP (Vauto_resize_tool_bars))
16434 && redisplay_tool_bar (f))
16435 ignore_mouse_drag_p = 1;
16436 #endif
16438 #endif
16441 #ifdef HAVE_WINDOW_SYSTEM
16442 if (FRAME_WINDOW_P (f)
16443 && update_window_fringes (w, (just_this_one_p
16444 || (!used_current_matrix_p && !overlay_arrow_seen)
16445 || w->pseudo_window_p)))
16447 update_begin (f);
16448 block_input ();
16449 if (draw_window_fringes (w, 1))
16451 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16452 x_draw_right_divider (w);
16453 else
16454 x_draw_vertical_border (w);
16456 unblock_input ();
16457 update_end (f);
16460 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16461 x_draw_bottom_divider (w);
16462 #endif /* HAVE_WINDOW_SYSTEM */
16464 /* We go to this label, with fonts_changed set, if it is
16465 necessary to try again using larger glyph matrices.
16466 We have to redeem the scroll bar even in this case,
16467 because the loop in redisplay_internal expects that. */
16468 need_larger_matrices:
16470 finish_scroll_bars:
16472 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16474 /* Set the thumb's position and size. */
16475 set_vertical_scroll_bar (w);
16477 /* Note that we actually used the scroll bar attached to this
16478 window, so it shouldn't be deleted at the end of redisplay. */
16479 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16480 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16483 /* Restore current_buffer and value of point in it. The window
16484 update may have changed the buffer, so first make sure `opoint'
16485 is still valid (Bug#6177). */
16486 if (CHARPOS (opoint) < BEGV)
16487 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16488 else if (CHARPOS (opoint) > ZV)
16489 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16490 else
16491 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16493 set_buffer_internal_1 (old);
16494 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16495 shorter. This can be caused by log truncation in *Messages*. */
16496 if (CHARPOS (lpoint) <= ZV)
16497 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16499 unbind_to (count, Qnil);
16503 /* Build the complete desired matrix of WINDOW with a window start
16504 buffer position POS.
16506 Value is 1 if successful. It is zero if fonts were loaded during
16507 redisplay which makes re-adjusting glyph matrices necessary, and -1
16508 if point would appear in the scroll margins.
16509 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16510 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16511 set in FLAGS.) */
16514 try_window (Lisp_Object window, struct text_pos pos, int flags)
16516 struct window *w = XWINDOW (window);
16517 struct it it;
16518 struct glyph_row *last_text_row = NULL;
16519 struct frame *f = XFRAME (w->frame);
16520 int frame_line_height = default_line_pixel_height (w);
16522 /* Make POS the new window start. */
16523 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16525 /* Mark cursor position as unknown. No overlay arrow seen. */
16526 w->cursor.vpos = -1;
16527 overlay_arrow_seen = 0;
16529 /* Initialize iterator and info to start at POS. */
16530 start_display (&it, w, pos);
16532 /* Display all lines of W. */
16533 while (it.current_y < it.last_visible_y)
16535 if (display_line (&it))
16536 last_text_row = it.glyph_row - 1;
16537 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16538 return 0;
16541 /* Don't let the cursor end in the scroll margins. */
16542 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16543 && !MINI_WINDOW_P (w))
16545 int this_scroll_margin;
16546 int window_total_lines
16547 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16549 if (scroll_margin > 0)
16551 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16552 this_scroll_margin *= frame_line_height;
16554 else
16555 this_scroll_margin = 0;
16557 if ((w->cursor.y >= 0 /* not vscrolled */
16558 && w->cursor.y < this_scroll_margin
16559 && CHARPOS (pos) > BEGV
16560 && IT_CHARPOS (it) < ZV)
16561 /* rms: considering make_cursor_line_fully_visible_p here
16562 seems to give wrong results. We don't want to recenter
16563 when the last line is partly visible, we want to allow
16564 that case to be handled in the usual way. */
16565 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16567 w->cursor.vpos = -1;
16568 clear_glyph_matrix (w->desired_matrix);
16569 return -1;
16573 /* If bottom moved off end of frame, change mode line percentage. */
16574 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16575 w->update_mode_line = 1;
16577 /* Set window_end_pos to the offset of the last character displayed
16578 on the window from the end of current_buffer. Set
16579 window_end_vpos to its row number. */
16580 if (last_text_row)
16582 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16583 adjust_window_ends (w, last_text_row, 0);
16584 eassert
16585 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16586 w->window_end_vpos)));
16588 else
16590 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16591 w->window_end_pos = Z - ZV;
16592 w->window_end_vpos = 0;
16595 /* But that is not valid info until redisplay finishes. */
16596 w->window_end_valid = 0;
16597 return 1;
16602 /************************************************************************
16603 Window redisplay reusing current matrix when buffer has not changed
16604 ************************************************************************/
16606 /* Try redisplay of window W showing an unchanged buffer with a
16607 different window start than the last time it was displayed by
16608 reusing its current matrix. Value is non-zero if successful.
16609 W->start is the new window start. */
16611 static int
16612 try_window_reusing_current_matrix (struct window *w)
16614 struct frame *f = XFRAME (w->frame);
16615 struct glyph_row *bottom_row;
16616 struct it it;
16617 struct run run;
16618 struct text_pos start, new_start;
16619 int nrows_scrolled, i;
16620 struct glyph_row *last_text_row;
16621 struct glyph_row *last_reused_text_row;
16622 struct glyph_row *start_row;
16623 int start_vpos, min_y, max_y;
16625 #ifdef GLYPH_DEBUG
16626 if (inhibit_try_window_reusing)
16627 return 0;
16628 #endif
16630 if (/* This function doesn't handle terminal frames. */
16631 !FRAME_WINDOW_P (f)
16632 /* Don't try to reuse the display if windows have been split
16633 or such. */
16634 || windows_or_buffers_changed
16635 || f->cursor_type_changed)
16636 return 0;
16638 /* Can't do this if showing trailing whitespace. */
16639 if (!NILP (Vshow_trailing_whitespace))
16640 return 0;
16642 /* If top-line visibility has changed, give up. */
16643 if (WINDOW_WANTS_HEADER_LINE_P (w)
16644 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16645 return 0;
16647 /* Give up if old or new display is scrolled vertically. We could
16648 make this function handle this, but right now it doesn't. */
16649 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16650 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16651 return 0;
16653 /* The variable new_start now holds the new window start. The old
16654 start `start' can be determined from the current matrix. */
16655 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16656 start = start_row->minpos;
16657 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16659 /* Clear the desired matrix for the display below. */
16660 clear_glyph_matrix (w->desired_matrix);
16662 if (CHARPOS (new_start) <= CHARPOS (start))
16664 /* Don't use this method if the display starts with an ellipsis
16665 displayed for invisible text. It's not easy to handle that case
16666 below, and it's certainly not worth the effort since this is
16667 not a frequent case. */
16668 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16669 return 0;
16671 IF_DEBUG (debug_method_add (w, "twu1"));
16673 /* Display up to a row that can be reused. The variable
16674 last_text_row is set to the last row displayed that displays
16675 text. Note that it.vpos == 0 if or if not there is a
16676 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16677 start_display (&it, w, new_start);
16678 w->cursor.vpos = -1;
16679 last_text_row = last_reused_text_row = NULL;
16681 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16683 /* If we have reached into the characters in the START row,
16684 that means the line boundaries have changed. So we
16685 can't start copying with the row START. Maybe it will
16686 work to start copying with the following row. */
16687 while (IT_CHARPOS (it) > CHARPOS (start))
16689 /* Advance to the next row as the "start". */
16690 start_row++;
16691 start = start_row->minpos;
16692 /* If there are no more rows to try, or just one, give up. */
16693 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16694 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16695 || CHARPOS (start) == ZV)
16697 clear_glyph_matrix (w->desired_matrix);
16698 return 0;
16701 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16703 /* If we have reached alignment, we can copy the rest of the
16704 rows. */
16705 if (IT_CHARPOS (it) == CHARPOS (start)
16706 /* Don't accept "alignment" inside a display vector,
16707 since start_row could have started in the middle of
16708 that same display vector (thus their character
16709 positions match), and we have no way of telling if
16710 that is the case. */
16711 && it.current.dpvec_index < 0)
16712 break;
16714 if (display_line (&it))
16715 last_text_row = it.glyph_row - 1;
16719 /* A value of current_y < last_visible_y means that we stopped
16720 at the previous window start, which in turn means that we
16721 have at least one reusable row. */
16722 if (it.current_y < it.last_visible_y)
16724 struct glyph_row *row;
16726 /* IT.vpos always starts from 0; it counts text lines. */
16727 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16729 /* Find PT if not already found in the lines displayed. */
16730 if (w->cursor.vpos < 0)
16732 int dy = it.current_y - start_row->y;
16734 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16735 row = row_containing_pos (w, PT, row, NULL, dy);
16736 if (row)
16737 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16738 dy, nrows_scrolled);
16739 else
16741 clear_glyph_matrix (w->desired_matrix);
16742 return 0;
16746 /* Scroll the display. Do it before the current matrix is
16747 changed. The problem here is that update has not yet
16748 run, i.e. part of the current matrix is not up to date.
16749 scroll_run_hook will clear the cursor, and use the
16750 current matrix to get the height of the row the cursor is
16751 in. */
16752 run.current_y = start_row->y;
16753 run.desired_y = it.current_y;
16754 run.height = it.last_visible_y - it.current_y;
16756 if (run.height > 0 && run.current_y != run.desired_y)
16758 update_begin (f);
16759 FRAME_RIF (f)->update_window_begin_hook (w);
16760 FRAME_RIF (f)->clear_window_mouse_face (w);
16761 FRAME_RIF (f)->scroll_run_hook (w, &run);
16762 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16763 update_end (f);
16766 /* Shift current matrix down by nrows_scrolled lines. */
16767 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16768 rotate_matrix (w->current_matrix,
16769 start_vpos,
16770 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16771 nrows_scrolled);
16773 /* Disable lines that must be updated. */
16774 for (i = 0; i < nrows_scrolled; ++i)
16775 (start_row + i)->enabled_p = false;
16777 /* Re-compute Y positions. */
16778 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16779 max_y = it.last_visible_y;
16780 for (row = start_row + nrows_scrolled;
16781 row < bottom_row;
16782 ++row)
16784 row->y = it.current_y;
16785 row->visible_height = row->height;
16787 if (row->y < min_y)
16788 row->visible_height -= min_y - row->y;
16789 if (row->y + row->height > max_y)
16790 row->visible_height -= row->y + row->height - max_y;
16791 if (row->fringe_bitmap_periodic_p)
16792 row->redraw_fringe_bitmaps_p = 1;
16794 it.current_y += row->height;
16796 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16797 last_reused_text_row = row;
16798 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16799 break;
16802 /* Disable lines in the current matrix which are now
16803 below the window. */
16804 for (++row; row < bottom_row; ++row)
16805 row->enabled_p = row->mode_line_p = 0;
16808 /* Update window_end_pos etc.; last_reused_text_row is the last
16809 reused row from the current matrix containing text, if any.
16810 The value of last_text_row is the last displayed line
16811 containing text. */
16812 if (last_reused_text_row)
16813 adjust_window_ends (w, last_reused_text_row, 1);
16814 else if (last_text_row)
16815 adjust_window_ends (w, last_text_row, 0);
16816 else
16818 /* This window must be completely empty. */
16819 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16820 w->window_end_pos = Z - ZV;
16821 w->window_end_vpos = 0;
16823 w->window_end_valid = 0;
16825 /* Update hint: don't try scrolling again in update_window. */
16826 w->desired_matrix->no_scrolling_p = 1;
16828 #ifdef GLYPH_DEBUG
16829 debug_method_add (w, "try_window_reusing_current_matrix 1");
16830 #endif
16831 return 1;
16833 else if (CHARPOS (new_start) > CHARPOS (start))
16835 struct glyph_row *pt_row, *row;
16836 struct glyph_row *first_reusable_row;
16837 struct glyph_row *first_row_to_display;
16838 int dy;
16839 int yb = window_text_bottom_y (w);
16841 /* Find the row starting at new_start, if there is one. Don't
16842 reuse a partially visible line at the end. */
16843 first_reusable_row = start_row;
16844 while (first_reusable_row->enabled_p
16845 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16846 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16847 < CHARPOS (new_start)))
16848 ++first_reusable_row;
16850 /* Give up if there is no row to reuse. */
16851 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16852 || !first_reusable_row->enabled_p
16853 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16854 != CHARPOS (new_start)))
16855 return 0;
16857 /* We can reuse fully visible rows beginning with
16858 first_reusable_row to the end of the window. Set
16859 first_row_to_display to the first row that cannot be reused.
16860 Set pt_row to the row containing point, if there is any. */
16861 pt_row = NULL;
16862 for (first_row_to_display = first_reusable_row;
16863 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16864 ++first_row_to_display)
16866 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16867 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16868 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16869 && first_row_to_display->ends_at_zv_p
16870 && pt_row == NULL)))
16871 pt_row = first_row_to_display;
16874 /* Start displaying at the start of first_row_to_display. */
16875 eassert (first_row_to_display->y < yb);
16876 init_to_row_start (&it, w, first_row_to_display);
16878 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16879 - start_vpos);
16880 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16881 - nrows_scrolled);
16882 it.current_y = (first_row_to_display->y - first_reusable_row->y
16883 + WINDOW_HEADER_LINE_HEIGHT (w));
16885 /* Display lines beginning with first_row_to_display in the
16886 desired matrix. Set last_text_row to the last row displayed
16887 that displays text. */
16888 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16889 if (pt_row == NULL)
16890 w->cursor.vpos = -1;
16891 last_text_row = NULL;
16892 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16893 if (display_line (&it))
16894 last_text_row = it.glyph_row - 1;
16896 /* If point is in a reused row, adjust y and vpos of the cursor
16897 position. */
16898 if (pt_row)
16900 w->cursor.vpos -= nrows_scrolled;
16901 w->cursor.y -= first_reusable_row->y - start_row->y;
16904 /* Give up if point isn't in a row displayed or reused. (This
16905 also handles the case where w->cursor.vpos < nrows_scrolled
16906 after the calls to display_line, which can happen with scroll
16907 margins. See bug#1295.) */
16908 if (w->cursor.vpos < 0)
16910 clear_glyph_matrix (w->desired_matrix);
16911 return 0;
16914 /* Scroll the display. */
16915 run.current_y = first_reusable_row->y;
16916 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16917 run.height = it.last_visible_y - run.current_y;
16918 dy = run.current_y - run.desired_y;
16920 if (run.height)
16922 update_begin (f);
16923 FRAME_RIF (f)->update_window_begin_hook (w);
16924 FRAME_RIF (f)->clear_window_mouse_face (w);
16925 FRAME_RIF (f)->scroll_run_hook (w, &run);
16926 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16927 update_end (f);
16930 /* Adjust Y positions of reused rows. */
16931 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16932 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16933 max_y = it.last_visible_y;
16934 for (row = first_reusable_row; row < first_row_to_display; ++row)
16936 row->y -= dy;
16937 row->visible_height = row->height;
16938 if (row->y < min_y)
16939 row->visible_height -= min_y - row->y;
16940 if (row->y + row->height > max_y)
16941 row->visible_height -= row->y + row->height - max_y;
16942 if (row->fringe_bitmap_periodic_p)
16943 row->redraw_fringe_bitmaps_p = 1;
16946 /* Scroll the current matrix. */
16947 eassert (nrows_scrolled > 0);
16948 rotate_matrix (w->current_matrix,
16949 start_vpos,
16950 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16951 -nrows_scrolled);
16953 /* Disable rows not reused. */
16954 for (row -= nrows_scrolled; row < bottom_row; ++row)
16955 row->enabled_p = false;
16957 /* Point may have moved to a different line, so we cannot assume that
16958 the previous cursor position is valid; locate the correct row. */
16959 if (pt_row)
16961 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16962 row < bottom_row
16963 && PT >= MATRIX_ROW_END_CHARPOS (row)
16964 && !row->ends_at_zv_p;
16965 row++)
16967 w->cursor.vpos++;
16968 w->cursor.y = row->y;
16970 if (row < bottom_row)
16972 /* Can't simply scan the row for point with
16973 bidi-reordered glyph rows. Let set_cursor_from_row
16974 figure out where to put the cursor, and if it fails,
16975 give up. */
16976 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16978 if (!set_cursor_from_row (w, row, w->current_matrix,
16979 0, 0, 0, 0))
16981 clear_glyph_matrix (w->desired_matrix);
16982 return 0;
16985 else
16987 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16988 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16990 for (; glyph < end
16991 && (!BUFFERP (glyph->object)
16992 || glyph->charpos < PT);
16993 glyph++)
16995 w->cursor.hpos++;
16996 w->cursor.x += glyph->pixel_width;
17002 /* Adjust window end. A null value of last_text_row means that
17003 the window end is in reused rows which in turn means that
17004 only its vpos can have changed. */
17005 if (last_text_row)
17006 adjust_window_ends (w, last_text_row, 0);
17007 else
17008 w->window_end_vpos -= nrows_scrolled;
17010 w->window_end_valid = 0;
17011 w->desired_matrix->no_scrolling_p = 1;
17013 #ifdef GLYPH_DEBUG
17014 debug_method_add (w, "try_window_reusing_current_matrix 2");
17015 #endif
17016 return 1;
17019 return 0;
17024 /************************************************************************
17025 Window redisplay reusing current matrix when buffer has changed
17026 ************************************************************************/
17028 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17029 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17030 ptrdiff_t *, ptrdiff_t *);
17031 static struct glyph_row *
17032 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17033 struct glyph_row *);
17036 /* Return the last row in MATRIX displaying text. If row START is
17037 non-null, start searching with that row. IT gives the dimensions
17038 of the display. Value is null if matrix is empty; otherwise it is
17039 a pointer to the row found. */
17041 static struct glyph_row *
17042 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17043 struct glyph_row *start)
17045 struct glyph_row *row, *row_found;
17047 /* Set row_found to the last row in IT->w's current matrix
17048 displaying text. The loop looks funny but think of partially
17049 visible lines. */
17050 row_found = NULL;
17051 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17052 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17054 eassert (row->enabled_p);
17055 row_found = row;
17056 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17057 break;
17058 ++row;
17061 return row_found;
17065 /* Return the last row in the current matrix of W that is not affected
17066 by changes at the start of current_buffer that occurred since W's
17067 current matrix was built. Value is null if no such row exists.
17069 BEG_UNCHANGED us the number of characters unchanged at the start of
17070 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17071 first changed character in current_buffer. Characters at positions <
17072 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17073 when the current matrix was built. */
17075 static struct glyph_row *
17076 find_last_unchanged_at_beg_row (struct window *w)
17078 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17079 struct glyph_row *row;
17080 struct glyph_row *row_found = NULL;
17081 int yb = window_text_bottom_y (w);
17083 /* Find the last row displaying unchanged text. */
17084 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17085 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17086 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17087 ++row)
17089 if (/* If row ends before first_changed_pos, it is unchanged,
17090 except in some case. */
17091 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17092 /* When row ends in ZV and we write at ZV it is not
17093 unchanged. */
17094 && !row->ends_at_zv_p
17095 /* When first_changed_pos is the end of a continued line,
17096 row is not unchanged because it may be no longer
17097 continued. */
17098 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17099 && (row->continued_p
17100 || row->exact_window_width_line_p))
17101 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17102 needs to be recomputed, so don't consider this row as
17103 unchanged. This happens when the last line was
17104 bidi-reordered and was killed immediately before this
17105 redisplay cycle. In that case, ROW->end stores the
17106 buffer position of the first visual-order character of
17107 the killed text, which is now beyond ZV. */
17108 && CHARPOS (row->end.pos) <= ZV)
17109 row_found = row;
17111 /* Stop if last visible row. */
17112 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17113 break;
17116 return row_found;
17120 /* Find the first glyph row in the current matrix of W that is not
17121 affected by changes at the end of current_buffer since the
17122 time W's current matrix was built.
17124 Return in *DELTA the number of chars by which buffer positions in
17125 unchanged text at the end of current_buffer must be adjusted.
17127 Return in *DELTA_BYTES the corresponding number of bytes.
17129 Value is null if no such row exists, i.e. all rows are affected by
17130 changes. */
17132 static struct glyph_row *
17133 find_first_unchanged_at_end_row (struct window *w,
17134 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17136 struct glyph_row *row;
17137 struct glyph_row *row_found = NULL;
17139 *delta = *delta_bytes = 0;
17141 /* Display must not have been paused, otherwise the current matrix
17142 is not up to date. */
17143 eassert (w->window_end_valid);
17145 /* A value of window_end_pos >= END_UNCHANGED means that the window
17146 end is in the range of changed text. If so, there is no
17147 unchanged row at the end of W's current matrix. */
17148 if (w->window_end_pos >= END_UNCHANGED)
17149 return NULL;
17151 /* Set row to the last row in W's current matrix displaying text. */
17152 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17154 /* If matrix is entirely empty, no unchanged row exists. */
17155 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17157 /* The value of row is the last glyph row in the matrix having a
17158 meaningful buffer position in it. The end position of row
17159 corresponds to window_end_pos. This allows us to translate
17160 buffer positions in the current matrix to current buffer
17161 positions for characters not in changed text. */
17162 ptrdiff_t Z_old =
17163 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17164 ptrdiff_t Z_BYTE_old =
17165 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17166 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17167 struct glyph_row *first_text_row
17168 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17170 *delta = Z - Z_old;
17171 *delta_bytes = Z_BYTE - Z_BYTE_old;
17173 /* Set last_unchanged_pos to the buffer position of the last
17174 character in the buffer that has not been changed. Z is the
17175 index + 1 of the last character in current_buffer, i.e. by
17176 subtracting END_UNCHANGED we get the index of the last
17177 unchanged character, and we have to add BEG to get its buffer
17178 position. */
17179 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17180 last_unchanged_pos_old = last_unchanged_pos - *delta;
17182 /* Search backward from ROW for a row displaying a line that
17183 starts at a minimum position >= last_unchanged_pos_old. */
17184 for (; row > first_text_row; --row)
17186 /* This used to abort, but it can happen.
17187 It is ok to just stop the search instead here. KFS. */
17188 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17189 break;
17191 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17192 row_found = row;
17196 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17198 return row_found;
17202 /* Make sure that glyph rows in the current matrix of window W
17203 reference the same glyph memory as corresponding rows in the
17204 frame's frame matrix. This function is called after scrolling W's
17205 current matrix on a terminal frame in try_window_id and
17206 try_window_reusing_current_matrix. */
17208 static void
17209 sync_frame_with_window_matrix_rows (struct window *w)
17211 struct frame *f = XFRAME (w->frame);
17212 struct glyph_row *window_row, *window_row_end, *frame_row;
17214 /* Preconditions: W must be a leaf window and full-width. Its frame
17215 must have a frame matrix. */
17216 eassert (BUFFERP (w->contents));
17217 eassert (WINDOW_FULL_WIDTH_P (w));
17218 eassert (!FRAME_WINDOW_P (f));
17220 /* If W is a full-width window, glyph pointers in W's current matrix
17221 have, by definition, to be the same as glyph pointers in the
17222 corresponding frame matrix. Note that frame matrices have no
17223 marginal areas (see build_frame_matrix). */
17224 window_row = w->current_matrix->rows;
17225 window_row_end = window_row + w->current_matrix->nrows;
17226 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17227 while (window_row < window_row_end)
17229 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17230 struct glyph *end = window_row->glyphs[LAST_AREA];
17232 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17233 frame_row->glyphs[TEXT_AREA] = start;
17234 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17235 frame_row->glyphs[LAST_AREA] = end;
17237 /* Disable frame rows whose corresponding window rows have
17238 been disabled in try_window_id. */
17239 if (!window_row->enabled_p)
17240 frame_row->enabled_p = false;
17242 ++window_row, ++frame_row;
17247 /* Find the glyph row in window W containing CHARPOS. Consider all
17248 rows between START and END (not inclusive). END null means search
17249 all rows to the end of the display area of W. Value is the row
17250 containing CHARPOS or null. */
17252 struct glyph_row *
17253 row_containing_pos (struct window *w, ptrdiff_t charpos,
17254 struct glyph_row *start, struct glyph_row *end, int dy)
17256 struct glyph_row *row = start;
17257 struct glyph_row *best_row = NULL;
17258 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17259 int last_y;
17261 /* If we happen to start on a header-line, skip that. */
17262 if (row->mode_line_p)
17263 ++row;
17265 if ((end && row >= end) || !row->enabled_p)
17266 return NULL;
17268 last_y = window_text_bottom_y (w) - dy;
17270 while (1)
17272 /* Give up if we have gone too far. */
17273 if (end && row >= end)
17274 return NULL;
17275 /* This formerly returned if they were equal.
17276 I think that both quantities are of a "last plus one" type;
17277 if so, when they are equal, the row is within the screen. -- rms. */
17278 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17279 return NULL;
17281 /* If it is in this row, return this row. */
17282 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17283 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17284 /* The end position of a row equals the start
17285 position of the next row. If CHARPOS is there, we
17286 would rather consider it displayed in the next
17287 line, except when this line ends in ZV. */
17288 && !row_for_charpos_p (row, charpos)))
17289 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17291 struct glyph *g;
17293 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17294 || (!best_row && !row->continued_p))
17295 return row;
17296 /* In bidi-reordered rows, there could be several rows whose
17297 edges surround CHARPOS, all of these rows belonging to
17298 the same continued line. We need to find the row which
17299 fits CHARPOS the best. */
17300 for (g = row->glyphs[TEXT_AREA];
17301 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17302 g++)
17304 if (!STRINGP (g->object))
17306 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17308 mindif = eabs (g->charpos - charpos);
17309 best_row = row;
17310 /* Exact match always wins. */
17311 if (mindif == 0)
17312 return best_row;
17317 else if (best_row && !row->continued_p)
17318 return best_row;
17319 ++row;
17324 /* Try to redisplay window W by reusing its existing display. W's
17325 current matrix must be up to date when this function is called,
17326 i.e. window_end_valid must be nonzero.
17328 Value is
17330 1 if display has been updated
17331 0 if otherwise unsuccessful
17332 -1 if redisplay with same window start is known not to succeed
17334 The following steps are performed:
17336 1. Find the last row in the current matrix of W that is not
17337 affected by changes at the start of current_buffer. If no such row
17338 is found, give up.
17340 2. Find the first row in W's current matrix that is not affected by
17341 changes at the end of current_buffer. Maybe there is no such row.
17343 3. Display lines beginning with the row + 1 found in step 1 to the
17344 row found in step 2 or, if step 2 didn't find a row, to the end of
17345 the window.
17347 4. If cursor is not known to appear on the window, give up.
17349 5. If display stopped at the row found in step 2, scroll the
17350 display and current matrix as needed.
17352 6. Maybe display some lines at the end of W, if we must. This can
17353 happen under various circumstances, like a partially visible line
17354 becoming fully visible, or because newly displayed lines are displayed
17355 in smaller font sizes.
17357 7. Update W's window end information. */
17359 static int
17360 try_window_id (struct window *w)
17362 struct frame *f = XFRAME (w->frame);
17363 struct glyph_matrix *current_matrix = w->current_matrix;
17364 struct glyph_matrix *desired_matrix = w->desired_matrix;
17365 struct glyph_row *last_unchanged_at_beg_row;
17366 struct glyph_row *first_unchanged_at_end_row;
17367 struct glyph_row *row;
17368 struct glyph_row *bottom_row;
17369 int bottom_vpos;
17370 struct it it;
17371 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17372 int dvpos, dy;
17373 struct text_pos start_pos;
17374 struct run run;
17375 int first_unchanged_at_end_vpos = 0;
17376 struct glyph_row *last_text_row, *last_text_row_at_end;
17377 struct text_pos start;
17378 ptrdiff_t first_changed_charpos, last_changed_charpos;
17380 #ifdef GLYPH_DEBUG
17381 if (inhibit_try_window_id)
17382 return 0;
17383 #endif
17385 /* This is handy for debugging. */
17386 #if 0
17387 #define GIVE_UP(X) \
17388 do { \
17389 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17390 return 0; \
17391 } while (0)
17392 #else
17393 #define GIVE_UP(X) return 0
17394 #endif
17396 SET_TEXT_POS_FROM_MARKER (start, w->start);
17398 /* Don't use this for mini-windows because these can show
17399 messages and mini-buffers, and we don't handle that here. */
17400 if (MINI_WINDOW_P (w))
17401 GIVE_UP (1);
17403 /* This flag is used to prevent redisplay optimizations. */
17404 if (windows_or_buffers_changed || f->cursor_type_changed)
17405 GIVE_UP (2);
17407 /* Verify that narrowing has not changed.
17408 Also verify that we were not told to prevent redisplay optimizations.
17409 It would be nice to further
17410 reduce the number of cases where this prevents try_window_id. */
17411 if (current_buffer->clip_changed
17412 || current_buffer->prevent_redisplay_optimizations_p)
17413 GIVE_UP (3);
17415 /* Window must either use window-based redisplay or be full width. */
17416 if (!FRAME_WINDOW_P (f)
17417 && (!FRAME_LINE_INS_DEL_OK (f)
17418 || !WINDOW_FULL_WIDTH_P (w)))
17419 GIVE_UP (4);
17421 /* Give up if point is known NOT to appear in W. */
17422 if (PT < CHARPOS (start))
17423 GIVE_UP (5);
17425 /* Another way to prevent redisplay optimizations. */
17426 if (w->last_modified == 0)
17427 GIVE_UP (6);
17429 /* Verify that window is not hscrolled. */
17430 if (w->hscroll != 0)
17431 GIVE_UP (7);
17433 /* Verify that display wasn't paused. */
17434 if (!w->window_end_valid)
17435 GIVE_UP (8);
17437 /* Likewise if highlighting trailing whitespace. */
17438 if (!NILP (Vshow_trailing_whitespace))
17439 GIVE_UP (11);
17441 /* Can't use this if overlay arrow position and/or string have
17442 changed. */
17443 if (overlay_arrows_changed_p ())
17444 GIVE_UP (12);
17446 /* When word-wrap is on, adding a space to the first word of a
17447 wrapped line can change the wrap position, altering the line
17448 above it. It might be worthwhile to handle this more
17449 intelligently, but for now just redisplay from scratch. */
17450 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17451 GIVE_UP (21);
17453 /* Under bidi reordering, adding or deleting a character in the
17454 beginning of a paragraph, before the first strong directional
17455 character, can change the base direction of the paragraph (unless
17456 the buffer specifies a fixed paragraph direction), which will
17457 require to redisplay the whole paragraph. It might be worthwhile
17458 to find the paragraph limits and widen the range of redisplayed
17459 lines to that, but for now just give up this optimization and
17460 redisplay from scratch. */
17461 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17462 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17463 GIVE_UP (22);
17465 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17466 only if buffer has really changed. The reason is that the gap is
17467 initially at Z for freshly visited files. The code below would
17468 set end_unchanged to 0 in that case. */
17469 if (MODIFF > SAVE_MODIFF
17470 /* This seems to happen sometimes after saving a buffer. */
17471 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17473 if (GPT - BEG < BEG_UNCHANGED)
17474 BEG_UNCHANGED = GPT - BEG;
17475 if (Z - GPT < END_UNCHANGED)
17476 END_UNCHANGED = Z - GPT;
17479 /* The position of the first and last character that has been changed. */
17480 first_changed_charpos = BEG + BEG_UNCHANGED;
17481 last_changed_charpos = Z - END_UNCHANGED;
17483 /* If window starts after a line end, and the last change is in
17484 front of that newline, then changes don't affect the display.
17485 This case happens with stealth-fontification. Note that although
17486 the display is unchanged, glyph positions in the matrix have to
17487 be adjusted, of course. */
17488 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17489 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17490 && ((last_changed_charpos < CHARPOS (start)
17491 && CHARPOS (start) == BEGV)
17492 || (last_changed_charpos < CHARPOS (start) - 1
17493 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17495 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17496 struct glyph_row *r0;
17498 /* Compute how many chars/bytes have been added to or removed
17499 from the buffer. */
17500 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17501 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17502 Z_delta = Z - Z_old;
17503 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17505 /* Give up if PT is not in the window. Note that it already has
17506 been checked at the start of try_window_id that PT is not in
17507 front of the window start. */
17508 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17509 GIVE_UP (13);
17511 /* If window start is unchanged, we can reuse the whole matrix
17512 as is, after adjusting glyph positions. No need to compute
17513 the window end again, since its offset from Z hasn't changed. */
17514 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17515 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17516 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17517 /* PT must not be in a partially visible line. */
17518 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17519 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17521 /* Adjust positions in the glyph matrix. */
17522 if (Z_delta || Z_delta_bytes)
17524 struct glyph_row *r1
17525 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17526 increment_matrix_positions (w->current_matrix,
17527 MATRIX_ROW_VPOS (r0, current_matrix),
17528 MATRIX_ROW_VPOS (r1, current_matrix),
17529 Z_delta, Z_delta_bytes);
17532 /* Set the cursor. */
17533 row = row_containing_pos (w, PT, r0, NULL, 0);
17534 if (row)
17535 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17536 return 1;
17540 /* Handle the case that changes are all below what is displayed in
17541 the window, and that PT is in the window. This shortcut cannot
17542 be taken if ZV is visible in the window, and text has been added
17543 there that is visible in the window. */
17544 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17545 /* ZV is not visible in the window, or there are no
17546 changes at ZV, actually. */
17547 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17548 || first_changed_charpos == last_changed_charpos))
17550 struct glyph_row *r0;
17552 /* Give up if PT is not in the window. Note that it already has
17553 been checked at the start of try_window_id that PT is not in
17554 front of the window start. */
17555 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17556 GIVE_UP (14);
17558 /* If window start is unchanged, we can reuse the whole matrix
17559 as is, without changing glyph positions since no text has
17560 been added/removed in front of the window end. */
17561 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17562 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17563 /* PT must not be in a partially visible line. */
17564 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17565 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17567 /* We have to compute the window end anew since text
17568 could have been added/removed after it. */
17569 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17570 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17572 /* Set the cursor. */
17573 row = row_containing_pos (w, PT, r0, NULL, 0);
17574 if (row)
17575 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17576 return 2;
17580 /* Give up if window start is in the changed area.
17582 The condition used to read
17584 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17586 but why that was tested escapes me at the moment. */
17587 if (CHARPOS (start) >= first_changed_charpos
17588 && CHARPOS (start) <= last_changed_charpos)
17589 GIVE_UP (15);
17591 /* Check that window start agrees with the start of the first glyph
17592 row in its current matrix. Check this after we know the window
17593 start is not in changed text, otherwise positions would not be
17594 comparable. */
17595 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17596 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17597 GIVE_UP (16);
17599 /* Give up if the window ends in strings. Overlay strings
17600 at the end are difficult to handle, so don't try. */
17601 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17602 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17603 GIVE_UP (20);
17605 /* Compute the position at which we have to start displaying new
17606 lines. Some of the lines at the top of the window might be
17607 reusable because they are not displaying changed text. Find the
17608 last row in W's current matrix not affected by changes at the
17609 start of current_buffer. Value is null if changes start in the
17610 first line of window. */
17611 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17612 if (last_unchanged_at_beg_row)
17614 /* Avoid starting to display in the middle of a character, a TAB
17615 for instance. This is easier than to set up the iterator
17616 exactly, and it's not a frequent case, so the additional
17617 effort wouldn't really pay off. */
17618 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17619 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17620 && last_unchanged_at_beg_row > w->current_matrix->rows)
17621 --last_unchanged_at_beg_row;
17623 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17624 GIVE_UP (17);
17626 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17627 GIVE_UP (18);
17628 start_pos = it.current.pos;
17630 /* Start displaying new lines in the desired matrix at the same
17631 vpos we would use in the current matrix, i.e. below
17632 last_unchanged_at_beg_row. */
17633 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17634 current_matrix);
17635 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17636 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17638 eassert (it.hpos == 0 && it.current_x == 0);
17640 else
17642 /* There are no reusable lines at the start of the window.
17643 Start displaying in the first text line. */
17644 start_display (&it, w, start);
17645 it.vpos = it.first_vpos;
17646 start_pos = it.current.pos;
17649 /* Find the first row that is not affected by changes at the end of
17650 the buffer. Value will be null if there is no unchanged row, in
17651 which case we must redisplay to the end of the window. delta
17652 will be set to the value by which buffer positions beginning with
17653 first_unchanged_at_end_row have to be adjusted due to text
17654 changes. */
17655 first_unchanged_at_end_row
17656 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17657 IF_DEBUG (debug_delta = delta);
17658 IF_DEBUG (debug_delta_bytes = delta_bytes);
17660 /* Set stop_pos to the buffer position up to which we will have to
17661 display new lines. If first_unchanged_at_end_row != NULL, this
17662 is the buffer position of the start of the line displayed in that
17663 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17664 that we don't stop at a buffer position. */
17665 stop_pos = 0;
17666 if (first_unchanged_at_end_row)
17668 eassert (last_unchanged_at_beg_row == NULL
17669 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17671 /* If this is a continuation line, move forward to the next one
17672 that isn't. Changes in lines above affect this line.
17673 Caution: this may move first_unchanged_at_end_row to a row
17674 not displaying text. */
17675 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17676 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17677 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17678 < it.last_visible_y))
17679 ++first_unchanged_at_end_row;
17681 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17682 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17683 >= it.last_visible_y))
17684 first_unchanged_at_end_row = NULL;
17685 else
17687 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17688 + delta);
17689 first_unchanged_at_end_vpos
17690 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17691 eassert (stop_pos >= Z - END_UNCHANGED);
17694 else if (last_unchanged_at_beg_row == NULL)
17695 GIVE_UP (19);
17698 #ifdef GLYPH_DEBUG
17700 /* Either there is no unchanged row at the end, or the one we have
17701 now displays text. This is a necessary condition for the window
17702 end pos calculation at the end of this function. */
17703 eassert (first_unchanged_at_end_row == NULL
17704 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17706 debug_last_unchanged_at_beg_vpos
17707 = (last_unchanged_at_beg_row
17708 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17709 : -1);
17710 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17712 #endif /* GLYPH_DEBUG */
17715 /* Display new lines. Set last_text_row to the last new line
17716 displayed which has text on it, i.e. might end up as being the
17717 line where the window_end_vpos is. */
17718 w->cursor.vpos = -1;
17719 last_text_row = NULL;
17720 overlay_arrow_seen = 0;
17721 while (it.current_y < it.last_visible_y
17722 && !f->fonts_changed
17723 && (first_unchanged_at_end_row == NULL
17724 || IT_CHARPOS (it) < stop_pos))
17726 if (display_line (&it))
17727 last_text_row = it.glyph_row - 1;
17730 if (f->fonts_changed)
17731 return -1;
17734 /* Compute differences in buffer positions, y-positions etc. for
17735 lines reused at the bottom of the window. Compute what we can
17736 scroll. */
17737 if (first_unchanged_at_end_row
17738 /* No lines reused because we displayed everything up to the
17739 bottom of the window. */
17740 && it.current_y < it.last_visible_y)
17742 dvpos = (it.vpos
17743 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17744 current_matrix));
17745 dy = it.current_y - first_unchanged_at_end_row->y;
17746 run.current_y = first_unchanged_at_end_row->y;
17747 run.desired_y = run.current_y + dy;
17748 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17750 else
17752 delta = delta_bytes = dvpos = dy
17753 = run.current_y = run.desired_y = run.height = 0;
17754 first_unchanged_at_end_row = NULL;
17756 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17759 /* Find the cursor if not already found. We have to decide whether
17760 PT will appear on this window (it sometimes doesn't, but this is
17761 not a very frequent case.) This decision has to be made before
17762 the current matrix is altered. A value of cursor.vpos < 0 means
17763 that PT is either in one of the lines beginning at
17764 first_unchanged_at_end_row or below the window. Don't care for
17765 lines that might be displayed later at the window end; as
17766 mentioned, this is not a frequent case. */
17767 if (w->cursor.vpos < 0)
17769 /* Cursor in unchanged rows at the top? */
17770 if (PT < CHARPOS (start_pos)
17771 && last_unchanged_at_beg_row)
17773 row = row_containing_pos (w, PT,
17774 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17775 last_unchanged_at_beg_row + 1, 0);
17776 if (row)
17777 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17780 /* Start from first_unchanged_at_end_row looking for PT. */
17781 else if (first_unchanged_at_end_row)
17783 row = row_containing_pos (w, PT - delta,
17784 first_unchanged_at_end_row, NULL, 0);
17785 if (row)
17786 set_cursor_from_row (w, row, w->current_matrix, delta,
17787 delta_bytes, dy, dvpos);
17790 /* Give up if cursor was not found. */
17791 if (w->cursor.vpos < 0)
17793 clear_glyph_matrix (w->desired_matrix);
17794 return -1;
17798 /* Don't let the cursor end in the scroll margins. */
17800 int this_scroll_margin, cursor_height;
17801 int frame_line_height = default_line_pixel_height (w);
17802 int window_total_lines
17803 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17805 this_scroll_margin =
17806 max (0, min (scroll_margin, window_total_lines / 4));
17807 this_scroll_margin *= frame_line_height;
17808 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17810 if ((w->cursor.y < this_scroll_margin
17811 && CHARPOS (start) > BEGV)
17812 /* Old redisplay didn't take scroll margin into account at the bottom,
17813 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17814 || (w->cursor.y + (make_cursor_line_fully_visible_p
17815 ? cursor_height + this_scroll_margin
17816 : 1)) > it.last_visible_y)
17818 w->cursor.vpos = -1;
17819 clear_glyph_matrix (w->desired_matrix);
17820 return -1;
17824 /* Scroll the display. Do it before changing the current matrix so
17825 that xterm.c doesn't get confused about where the cursor glyph is
17826 found. */
17827 if (dy && run.height)
17829 update_begin (f);
17831 if (FRAME_WINDOW_P (f))
17833 FRAME_RIF (f)->update_window_begin_hook (w);
17834 FRAME_RIF (f)->clear_window_mouse_face (w);
17835 FRAME_RIF (f)->scroll_run_hook (w, &run);
17836 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17838 else
17840 /* Terminal frame. In this case, dvpos gives the number of
17841 lines to scroll by; dvpos < 0 means scroll up. */
17842 int from_vpos
17843 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17844 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17845 int end = (WINDOW_TOP_EDGE_LINE (w)
17846 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17847 + window_internal_height (w));
17849 #if defined (HAVE_GPM) || defined (MSDOS)
17850 x_clear_window_mouse_face (w);
17851 #endif
17852 /* Perform the operation on the screen. */
17853 if (dvpos > 0)
17855 /* Scroll last_unchanged_at_beg_row to the end of the
17856 window down dvpos lines. */
17857 set_terminal_window (f, end);
17859 /* On dumb terminals delete dvpos lines at the end
17860 before inserting dvpos empty lines. */
17861 if (!FRAME_SCROLL_REGION_OK (f))
17862 ins_del_lines (f, end - dvpos, -dvpos);
17864 /* Insert dvpos empty lines in front of
17865 last_unchanged_at_beg_row. */
17866 ins_del_lines (f, from, dvpos);
17868 else if (dvpos < 0)
17870 /* Scroll up last_unchanged_at_beg_vpos to the end of
17871 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17872 set_terminal_window (f, end);
17874 /* Delete dvpos lines in front of
17875 last_unchanged_at_beg_vpos. ins_del_lines will set
17876 the cursor to the given vpos and emit |dvpos| delete
17877 line sequences. */
17878 ins_del_lines (f, from + dvpos, dvpos);
17880 /* On a dumb terminal insert dvpos empty lines at the
17881 end. */
17882 if (!FRAME_SCROLL_REGION_OK (f))
17883 ins_del_lines (f, end + dvpos, -dvpos);
17886 set_terminal_window (f, 0);
17889 update_end (f);
17892 /* Shift reused rows of the current matrix to the right position.
17893 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17894 text. */
17895 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17896 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17897 if (dvpos < 0)
17899 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17900 bottom_vpos, dvpos);
17901 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17902 bottom_vpos);
17904 else if (dvpos > 0)
17906 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17907 bottom_vpos, dvpos);
17908 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17909 first_unchanged_at_end_vpos + dvpos);
17912 /* For frame-based redisplay, make sure that current frame and window
17913 matrix are in sync with respect to glyph memory. */
17914 if (!FRAME_WINDOW_P (f))
17915 sync_frame_with_window_matrix_rows (w);
17917 /* Adjust buffer positions in reused rows. */
17918 if (delta || delta_bytes)
17919 increment_matrix_positions (current_matrix,
17920 first_unchanged_at_end_vpos + dvpos,
17921 bottom_vpos, delta, delta_bytes);
17923 /* Adjust Y positions. */
17924 if (dy)
17925 shift_glyph_matrix (w, current_matrix,
17926 first_unchanged_at_end_vpos + dvpos,
17927 bottom_vpos, dy);
17929 if (first_unchanged_at_end_row)
17931 first_unchanged_at_end_row += dvpos;
17932 if (first_unchanged_at_end_row->y >= it.last_visible_y
17933 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17934 first_unchanged_at_end_row = NULL;
17937 /* If scrolling up, there may be some lines to display at the end of
17938 the window. */
17939 last_text_row_at_end = NULL;
17940 if (dy < 0)
17942 /* Scrolling up can leave for example a partially visible line
17943 at the end of the window to be redisplayed. */
17944 /* Set last_row to the glyph row in the current matrix where the
17945 window end line is found. It has been moved up or down in
17946 the matrix by dvpos. */
17947 int last_vpos = w->window_end_vpos + dvpos;
17948 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17950 /* If last_row is the window end line, it should display text. */
17951 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17953 /* If window end line was partially visible before, begin
17954 displaying at that line. Otherwise begin displaying with the
17955 line following it. */
17956 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17958 init_to_row_start (&it, w, last_row);
17959 it.vpos = last_vpos;
17960 it.current_y = last_row->y;
17962 else
17964 init_to_row_end (&it, w, last_row);
17965 it.vpos = 1 + last_vpos;
17966 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17967 ++last_row;
17970 /* We may start in a continuation line. If so, we have to
17971 get the right continuation_lines_width and current_x. */
17972 it.continuation_lines_width = last_row->continuation_lines_width;
17973 it.hpos = it.current_x = 0;
17975 /* Display the rest of the lines at the window end. */
17976 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17977 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17979 /* Is it always sure that the display agrees with lines in
17980 the current matrix? I don't think so, so we mark rows
17981 displayed invalid in the current matrix by setting their
17982 enabled_p flag to zero. */
17983 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
17984 if (display_line (&it))
17985 last_text_row_at_end = it.glyph_row - 1;
17989 /* Update window_end_pos and window_end_vpos. */
17990 if (first_unchanged_at_end_row && !last_text_row_at_end)
17992 /* Window end line if one of the preserved rows from the current
17993 matrix. Set row to the last row displaying text in current
17994 matrix starting at first_unchanged_at_end_row, after
17995 scrolling. */
17996 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17997 row = find_last_row_displaying_text (w->current_matrix, &it,
17998 first_unchanged_at_end_row);
17999 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18000 adjust_window_ends (w, row, 1);
18001 eassert (w->window_end_bytepos >= 0);
18002 IF_DEBUG (debug_method_add (w, "A"));
18004 else if (last_text_row_at_end)
18006 adjust_window_ends (w, last_text_row_at_end, 0);
18007 eassert (w->window_end_bytepos >= 0);
18008 IF_DEBUG (debug_method_add (w, "B"));
18010 else if (last_text_row)
18012 /* We have displayed either to the end of the window or at the
18013 end of the window, i.e. the last row with text is to be found
18014 in the desired matrix. */
18015 adjust_window_ends (w, last_text_row, 0);
18016 eassert (w->window_end_bytepos >= 0);
18018 else if (first_unchanged_at_end_row == NULL
18019 && last_text_row == NULL
18020 && last_text_row_at_end == NULL)
18022 /* Displayed to end of window, but no line containing text was
18023 displayed. Lines were deleted at the end of the window. */
18024 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18025 int vpos = w->window_end_vpos;
18026 struct glyph_row *current_row = current_matrix->rows + vpos;
18027 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18029 for (row = NULL;
18030 row == NULL && vpos >= first_vpos;
18031 --vpos, --current_row, --desired_row)
18033 if (desired_row->enabled_p)
18035 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18036 row = desired_row;
18038 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18039 row = current_row;
18042 eassert (row != NULL);
18043 w->window_end_vpos = vpos + 1;
18044 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18045 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18046 eassert (w->window_end_bytepos >= 0);
18047 IF_DEBUG (debug_method_add (w, "C"));
18049 else
18050 emacs_abort ();
18052 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18053 debug_end_vpos = w->window_end_vpos));
18055 /* Record that display has not been completed. */
18056 w->window_end_valid = 0;
18057 w->desired_matrix->no_scrolling_p = 1;
18058 return 3;
18060 #undef GIVE_UP
18065 /***********************************************************************
18066 More debugging support
18067 ***********************************************************************/
18069 #ifdef GLYPH_DEBUG
18071 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18072 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18073 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18076 /* Dump the contents of glyph matrix MATRIX on stderr.
18078 GLYPHS 0 means don't show glyph contents.
18079 GLYPHS 1 means show glyphs in short form
18080 GLYPHS > 1 means show glyphs in long form. */
18082 void
18083 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18085 int i;
18086 for (i = 0; i < matrix->nrows; ++i)
18087 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18091 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18092 the glyph row and area where the glyph comes from. */
18094 void
18095 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18097 if (glyph->type == CHAR_GLYPH
18098 || glyph->type == GLYPHLESS_GLYPH)
18100 fprintf (stderr,
18101 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18102 glyph - row->glyphs[TEXT_AREA],
18103 (glyph->type == CHAR_GLYPH
18104 ? 'C'
18105 : 'G'),
18106 glyph->charpos,
18107 (BUFFERP (glyph->object)
18108 ? 'B'
18109 : (STRINGP (glyph->object)
18110 ? 'S'
18111 : (INTEGERP (glyph->object)
18112 ? '0'
18113 : '-'))),
18114 glyph->pixel_width,
18115 glyph->u.ch,
18116 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18117 ? glyph->u.ch
18118 : '.'),
18119 glyph->face_id,
18120 glyph->left_box_line_p,
18121 glyph->right_box_line_p);
18123 else if (glyph->type == STRETCH_GLYPH)
18125 fprintf (stderr,
18126 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18127 glyph - row->glyphs[TEXT_AREA],
18128 'S',
18129 glyph->charpos,
18130 (BUFFERP (glyph->object)
18131 ? 'B'
18132 : (STRINGP (glyph->object)
18133 ? 'S'
18134 : (INTEGERP (glyph->object)
18135 ? '0'
18136 : '-'))),
18137 glyph->pixel_width,
18139 ' ',
18140 glyph->face_id,
18141 glyph->left_box_line_p,
18142 glyph->right_box_line_p);
18144 else if (glyph->type == IMAGE_GLYPH)
18146 fprintf (stderr,
18147 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18148 glyph - row->glyphs[TEXT_AREA],
18149 'I',
18150 glyph->charpos,
18151 (BUFFERP (glyph->object)
18152 ? 'B'
18153 : (STRINGP (glyph->object)
18154 ? 'S'
18155 : (INTEGERP (glyph->object)
18156 ? '0'
18157 : '-'))),
18158 glyph->pixel_width,
18159 glyph->u.img_id,
18160 '.',
18161 glyph->face_id,
18162 glyph->left_box_line_p,
18163 glyph->right_box_line_p);
18165 else if (glyph->type == COMPOSITE_GLYPH)
18167 fprintf (stderr,
18168 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18169 glyph - row->glyphs[TEXT_AREA],
18170 '+',
18171 glyph->charpos,
18172 (BUFFERP (glyph->object)
18173 ? 'B'
18174 : (STRINGP (glyph->object)
18175 ? 'S'
18176 : (INTEGERP (glyph->object)
18177 ? '0'
18178 : '-'))),
18179 glyph->pixel_width,
18180 glyph->u.cmp.id);
18181 if (glyph->u.cmp.automatic)
18182 fprintf (stderr,
18183 "[%d-%d]",
18184 glyph->slice.cmp.from, glyph->slice.cmp.to);
18185 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18186 glyph->face_id,
18187 glyph->left_box_line_p,
18188 glyph->right_box_line_p);
18193 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18194 GLYPHS 0 means don't show glyph contents.
18195 GLYPHS 1 means show glyphs in short form
18196 GLYPHS > 1 means show glyphs in long form. */
18198 void
18199 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18201 if (glyphs != 1)
18203 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18204 fprintf (stderr, "==============================================================================\n");
18206 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18207 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18208 vpos,
18209 MATRIX_ROW_START_CHARPOS (row),
18210 MATRIX_ROW_END_CHARPOS (row),
18211 row->used[TEXT_AREA],
18212 row->contains_overlapping_glyphs_p,
18213 row->enabled_p,
18214 row->truncated_on_left_p,
18215 row->truncated_on_right_p,
18216 row->continued_p,
18217 MATRIX_ROW_CONTINUATION_LINE_P (row),
18218 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18219 row->ends_at_zv_p,
18220 row->fill_line_p,
18221 row->ends_in_middle_of_char_p,
18222 row->starts_in_middle_of_char_p,
18223 row->mouse_face_p,
18224 row->x,
18225 row->y,
18226 row->pixel_width,
18227 row->height,
18228 row->visible_height,
18229 row->ascent,
18230 row->phys_ascent);
18231 /* The next 3 lines should align to "Start" in the header. */
18232 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18233 row->end.overlay_string_index,
18234 row->continuation_lines_width);
18235 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18236 CHARPOS (row->start.string_pos),
18237 CHARPOS (row->end.string_pos));
18238 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18239 row->end.dpvec_index);
18242 if (glyphs > 1)
18244 int area;
18246 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18248 struct glyph *glyph = row->glyphs[area];
18249 struct glyph *glyph_end = glyph + row->used[area];
18251 /* Glyph for a line end in text. */
18252 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18253 ++glyph_end;
18255 if (glyph < glyph_end)
18256 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18258 for (; glyph < glyph_end; ++glyph)
18259 dump_glyph (row, glyph, area);
18262 else if (glyphs == 1)
18264 int area;
18266 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18268 char *s = alloca (row->used[area] + 4);
18269 int i;
18271 for (i = 0; i < row->used[area]; ++i)
18273 struct glyph *glyph = row->glyphs[area] + i;
18274 if (i == row->used[area] - 1
18275 && area == TEXT_AREA
18276 && INTEGERP (glyph->object)
18277 && glyph->type == CHAR_GLYPH
18278 && glyph->u.ch == ' ')
18280 strcpy (&s[i], "[\\n]");
18281 i += 4;
18283 else if (glyph->type == CHAR_GLYPH
18284 && glyph->u.ch < 0x80
18285 && glyph->u.ch >= ' ')
18286 s[i] = glyph->u.ch;
18287 else
18288 s[i] = '.';
18291 s[i] = '\0';
18292 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18298 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18299 Sdump_glyph_matrix, 0, 1, "p",
18300 doc: /* Dump the current matrix of the selected window to stderr.
18301 Shows contents of glyph row structures. With non-nil
18302 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18303 glyphs in short form, otherwise show glyphs in long form. */)
18304 (Lisp_Object glyphs)
18306 struct window *w = XWINDOW (selected_window);
18307 struct buffer *buffer = XBUFFER (w->contents);
18309 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18310 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18311 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18312 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18313 fprintf (stderr, "=============================================\n");
18314 dump_glyph_matrix (w->current_matrix,
18315 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18316 return Qnil;
18320 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18321 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18322 (void)
18324 struct frame *f = XFRAME (selected_frame);
18325 dump_glyph_matrix (f->current_matrix, 1);
18326 return Qnil;
18330 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18331 doc: /* Dump glyph row ROW to stderr.
18332 GLYPH 0 means don't dump glyphs.
18333 GLYPH 1 means dump glyphs in short form.
18334 GLYPH > 1 or omitted means dump glyphs in long form. */)
18335 (Lisp_Object row, Lisp_Object glyphs)
18337 struct glyph_matrix *matrix;
18338 EMACS_INT vpos;
18340 CHECK_NUMBER (row);
18341 matrix = XWINDOW (selected_window)->current_matrix;
18342 vpos = XINT (row);
18343 if (vpos >= 0 && vpos < matrix->nrows)
18344 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18345 vpos,
18346 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18347 return Qnil;
18351 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18352 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18353 GLYPH 0 means don't dump glyphs.
18354 GLYPH 1 means dump glyphs in short form.
18355 GLYPH > 1 or omitted means dump glyphs in long form.
18357 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18358 do nothing. */)
18359 (Lisp_Object row, Lisp_Object glyphs)
18361 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18362 struct frame *sf = SELECTED_FRAME ();
18363 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18364 EMACS_INT vpos;
18366 CHECK_NUMBER (row);
18367 vpos = XINT (row);
18368 if (vpos >= 0 && vpos < m->nrows)
18369 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18370 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18371 #endif
18372 return Qnil;
18376 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18377 doc: /* Toggle tracing of redisplay.
18378 With ARG, turn tracing on if and only if ARG is positive. */)
18379 (Lisp_Object arg)
18381 if (NILP (arg))
18382 trace_redisplay_p = !trace_redisplay_p;
18383 else
18385 arg = Fprefix_numeric_value (arg);
18386 trace_redisplay_p = XINT (arg) > 0;
18389 return Qnil;
18393 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18394 doc: /* Like `format', but print result to stderr.
18395 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18396 (ptrdiff_t nargs, Lisp_Object *args)
18398 Lisp_Object s = Fformat (nargs, args);
18399 fprintf (stderr, "%s", SDATA (s));
18400 return Qnil;
18403 #endif /* GLYPH_DEBUG */
18407 /***********************************************************************
18408 Building Desired Matrix Rows
18409 ***********************************************************************/
18411 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18412 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18414 static struct glyph_row *
18415 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18417 struct frame *f = XFRAME (WINDOW_FRAME (w));
18418 struct buffer *buffer = XBUFFER (w->contents);
18419 struct buffer *old = current_buffer;
18420 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18421 int arrow_len = SCHARS (overlay_arrow_string);
18422 const unsigned char *arrow_end = arrow_string + arrow_len;
18423 const unsigned char *p;
18424 struct it it;
18425 bool multibyte_p;
18426 int n_glyphs_before;
18428 set_buffer_temp (buffer);
18429 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18430 it.glyph_row->used[TEXT_AREA] = 0;
18431 SET_TEXT_POS (it.position, 0, 0);
18433 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18434 p = arrow_string;
18435 while (p < arrow_end)
18437 Lisp_Object face, ilisp;
18439 /* Get the next character. */
18440 if (multibyte_p)
18441 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18442 else
18444 it.c = it.char_to_display = *p, it.len = 1;
18445 if (! ASCII_CHAR_P (it.c))
18446 it.char_to_display = BYTE8_TO_CHAR (it.c);
18448 p += it.len;
18450 /* Get its face. */
18451 ilisp = make_number (p - arrow_string);
18452 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18453 it.face_id = compute_char_face (f, it.char_to_display, face);
18455 /* Compute its width, get its glyphs. */
18456 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18457 SET_TEXT_POS (it.position, -1, -1);
18458 PRODUCE_GLYPHS (&it);
18460 /* If this character doesn't fit any more in the line, we have
18461 to remove some glyphs. */
18462 if (it.current_x > it.last_visible_x)
18464 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18465 break;
18469 set_buffer_temp (old);
18470 return it.glyph_row;
18474 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18475 glyphs to insert is determined by produce_special_glyphs. */
18477 static void
18478 insert_left_trunc_glyphs (struct it *it)
18480 struct it truncate_it;
18481 struct glyph *from, *end, *to, *toend;
18483 eassert (!FRAME_WINDOW_P (it->f)
18484 || (!it->glyph_row->reversed_p
18485 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18486 || (it->glyph_row->reversed_p
18487 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18489 /* Get the truncation glyphs. */
18490 truncate_it = *it;
18491 truncate_it.current_x = 0;
18492 truncate_it.face_id = DEFAULT_FACE_ID;
18493 truncate_it.glyph_row = &scratch_glyph_row;
18494 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18495 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18496 truncate_it.object = make_number (0);
18497 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18499 /* Overwrite glyphs from IT with truncation glyphs. */
18500 if (!it->glyph_row->reversed_p)
18502 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18504 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18505 end = from + tused;
18506 to = it->glyph_row->glyphs[TEXT_AREA];
18507 toend = to + it->glyph_row->used[TEXT_AREA];
18508 if (FRAME_WINDOW_P (it->f))
18510 /* On GUI frames, when variable-size fonts are displayed,
18511 the truncation glyphs may need more pixels than the row's
18512 glyphs they overwrite. We overwrite more glyphs to free
18513 enough screen real estate, and enlarge the stretch glyph
18514 on the right (see display_line), if there is one, to
18515 preserve the screen position of the truncation glyphs on
18516 the right. */
18517 int w = 0;
18518 struct glyph *g = to;
18519 short used;
18521 /* The first glyph could be partially visible, in which case
18522 it->glyph_row->x will be negative. But we want the left
18523 truncation glyphs to be aligned at the left margin of the
18524 window, so we override the x coordinate at which the row
18525 will begin. */
18526 it->glyph_row->x = 0;
18527 while (g < toend && w < it->truncation_pixel_width)
18529 w += g->pixel_width;
18530 ++g;
18532 if (g - to - tused > 0)
18534 memmove (to + tused, g, (toend - g) * sizeof(*g));
18535 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18537 used = it->glyph_row->used[TEXT_AREA];
18538 if (it->glyph_row->truncated_on_right_p
18539 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18540 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18541 == STRETCH_GLYPH)
18543 int extra = w - it->truncation_pixel_width;
18545 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18549 while (from < end)
18550 *to++ = *from++;
18552 /* There may be padding glyphs left over. Overwrite them too. */
18553 if (!FRAME_WINDOW_P (it->f))
18555 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18557 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18558 while (from < end)
18559 *to++ = *from++;
18563 if (to > toend)
18564 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18566 else
18568 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18570 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18571 that back to front. */
18572 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18573 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18574 toend = it->glyph_row->glyphs[TEXT_AREA];
18575 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18576 if (FRAME_WINDOW_P (it->f))
18578 int w = 0;
18579 struct glyph *g = to;
18581 while (g >= toend && w < it->truncation_pixel_width)
18583 w += g->pixel_width;
18584 --g;
18586 if (to - g - tused > 0)
18587 to = g + tused;
18588 if (it->glyph_row->truncated_on_right_p
18589 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18590 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18592 int extra = w - it->truncation_pixel_width;
18594 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18598 while (from >= end && to >= toend)
18599 *to-- = *from--;
18600 if (!FRAME_WINDOW_P (it->f))
18602 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18604 from =
18605 truncate_it.glyph_row->glyphs[TEXT_AREA]
18606 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18607 while (from >= end && to >= toend)
18608 *to-- = *from--;
18611 if (from >= end)
18613 /* Need to free some room before prepending additional
18614 glyphs. */
18615 int move_by = from - end + 1;
18616 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18617 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18619 for ( ; g >= g0; g--)
18620 g[move_by] = *g;
18621 while (from >= end)
18622 *to-- = *from--;
18623 it->glyph_row->used[TEXT_AREA] += move_by;
18628 /* Compute the hash code for ROW. */
18629 unsigned
18630 row_hash (struct glyph_row *row)
18632 int area, k;
18633 unsigned hashval = 0;
18635 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18636 for (k = 0; k < row->used[area]; ++k)
18637 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18638 + row->glyphs[area][k].u.val
18639 + row->glyphs[area][k].face_id
18640 + row->glyphs[area][k].padding_p
18641 + (row->glyphs[area][k].type << 2));
18643 return hashval;
18646 /* Compute the pixel height and width of IT->glyph_row.
18648 Most of the time, ascent and height of a display line will be equal
18649 to the max_ascent and max_height values of the display iterator
18650 structure. This is not the case if
18652 1. We hit ZV without displaying anything. In this case, max_ascent
18653 and max_height will be zero.
18655 2. We have some glyphs that don't contribute to the line height.
18656 (The glyph row flag contributes_to_line_height_p is for future
18657 pixmap extensions).
18659 The first case is easily covered by using default values because in
18660 these cases, the line height does not really matter, except that it
18661 must not be zero. */
18663 static void
18664 compute_line_metrics (struct it *it)
18666 struct glyph_row *row = it->glyph_row;
18668 if (FRAME_WINDOW_P (it->f))
18670 int i, min_y, max_y;
18672 /* The line may consist of one space only, that was added to
18673 place the cursor on it. If so, the row's height hasn't been
18674 computed yet. */
18675 if (row->height == 0)
18677 if (it->max_ascent + it->max_descent == 0)
18678 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18679 row->ascent = it->max_ascent;
18680 row->height = it->max_ascent + it->max_descent;
18681 row->phys_ascent = it->max_phys_ascent;
18682 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18683 row->extra_line_spacing = it->max_extra_line_spacing;
18686 /* Compute the width of this line. */
18687 row->pixel_width = row->x;
18688 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18689 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18691 eassert (row->pixel_width >= 0);
18692 eassert (row->ascent >= 0 && row->height > 0);
18694 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18695 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18697 /* If first line's physical ascent is larger than its logical
18698 ascent, use the physical ascent, and make the row taller.
18699 This makes accented characters fully visible. */
18700 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18701 && row->phys_ascent > row->ascent)
18703 row->height += row->phys_ascent - row->ascent;
18704 row->ascent = row->phys_ascent;
18707 /* Compute how much of the line is visible. */
18708 row->visible_height = row->height;
18710 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18711 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18713 if (row->y < min_y)
18714 row->visible_height -= min_y - row->y;
18715 if (row->y + row->height > max_y)
18716 row->visible_height -= row->y + row->height - max_y;
18718 else
18720 row->pixel_width = row->used[TEXT_AREA];
18721 if (row->continued_p)
18722 row->pixel_width -= it->continuation_pixel_width;
18723 else if (row->truncated_on_right_p)
18724 row->pixel_width -= it->truncation_pixel_width;
18725 row->ascent = row->phys_ascent = 0;
18726 row->height = row->phys_height = row->visible_height = 1;
18727 row->extra_line_spacing = 0;
18730 /* Compute a hash code for this row. */
18731 row->hash = row_hash (row);
18733 it->max_ascent = it->max_descent = 0;
18734 it->max_phys_ascent = it->max_phys_descent = 0;
18738 /* Append one space to the glyph row of iterator IT if doing a
18739 window-based redisplay. The space has the same face as
18740 IT->face_id. Value is non-zero if a space was added.
18742 This function is called to make sure that there is always one glyph
18743 at the end of a glyph row that the cursor can be set on under
18744 window-systems. (If there weren't such a glyph we would not know
18745 how wide and tall a box cursor should be displayed).
18747 At the same time this space let's a nicely handle clearing to the
18748 end of the line if the row ends in italic text. */
18750 static int
18751 append_space_for_newline (struct it *it, int default_face_p)
18753 if (FRAME_WINDOW_P (it->f))
18755 int n = it->glyph_row->used[TEXT_AREA];
18757 if (it->glyph_row->glyphs[TEXT_AREA] + n
18758 < it->glyph_row->glyphs[1 + TEXT_AREA])
18760 /* Save some values that must not be changed.
18761 Must save IT->c and IT->len because otherwise
18762 ITERATOR_AT_END_P wouldn't work anymore after
18763 append_space_for_newline has been called. */
18764 enum display_element_type saved_what = it->what;
18765 int saved_c = it->c, saved_len = it->len;
18766 int saved_char_to_display = it->char_to_display;
18767 int saved_x = it->current_x;
18768 int saved_face_id = it->face_id;
18769 int saved_box_end = it->end_of_box_run_p;
18770 struct text_pos saved_pos;
18771 Lisp_Object saved_object;
18772 struct face *face;
18774 saved_object = it->object;
18775 saved_pos = it->position;
18777 it->what = IT_CHARACTER;
18778 memset (&it->position, 0, sizeof it->position);
18779 it->object = make_number (0);
18780 it->c = it->char_to_display = ' ';
18781 it->len = 1;
18783 /* If the default face was remapped, be sure to use the
18784 remapped face for the appended newline. */
18785 if (default_face_p)
18786 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18787 else if (it->face_before_selective_p)
18788 it->face_id = it->saved_face_id;
18789 face = FACE_FROM_ID (it->f, it->face_id);
18790 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18791 /* In R2L rows, we will prepend a stretch glyph that will
18792 have the end_of_box_run_p flag set for it, so there's no
18793 need for the appended newline glyph to have that flag
18794 set. */
18795 if (it->glyph_row->reversed_p
18796 /* But if the appended newline glyph goes all the way to
18797 the end of the row, there will be no stretch glyph,
18798 so leave the box flag set. */
18799 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18800 it->end_of_box_run_p = 0;
18802 PRODUCE_GLYPHS (it);
18804 it->override_ascent = -1;
18805 it->constrain_row_ascent_descent_p = 0;
18806 it->current_x = saved_x;
18807 it->object = saved_object;
18808 it->position = saved_pos;
18809 it->what = saved_what;
18810 it->face_id = saved_face_id;
18811 it->len = saved_len;
18812 it->c = saved_c;
18813 it->char_to_display = saved_char_to_display;
18814 it->end_of_box_run_p = saved_box_end;
18815 return 1;
18819 return 0;
18823 /* Extend the face of the last glyph in the text area of IT->glyph_row
18824 to the end of the display line. Called from display_line. If the
18825 glyph row is empty, add a space glyph to it so that we know the
18826 face to draw. Set the glyph row flag fill_line_p. If the glyph
18827 row is R2L, prepend a stretch glyph to cover the empty space to the
18828 left of the leftmost glyph. */
18830 static void
18831 extend_face_to_end_of_line (struct it *it)
18833 struct face *face, *default_face;
18834 struct frame *f = it->f;
18836 /* If line is already filled, do nothing. Non window-system frames
18837 get a grace of one more ``pixel'' because their characters are
18838 1-``pixel'' wide, so they hit the equality too early. This grace
18839 is needed only for R2L rows that are not continued, to produce
18840 one extra blank where we could display the cursor. */
18841 if ((it->current_x >= it->last_visible_x
18842 + (!FRAME_WINDOW_P (f)
18843 && it->glyph_row->reversed_p
18844 && !it->glyph_row->continued_p))
18845 /* If the window has display margins, we will need to extend
18846 their face even if the text area is filled. */
18847 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18848 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18849 return;
18851 /* The default face, possibly remapped. */
18852 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18854 /* Face extension extends the background and box of IT->face_id
18855 to the end of the line. If the background equals the background
18856 of the frame, we don't have to do anything. */
18857 if (it->face_before_selective_p)
18858 face = FACE_FROM_ID (f, it->saved_face_id);
18859 else
18860 face = FACE_FROM_ID (f, it->face_id);
18862 if (FRAME_WINDOW_P (f)
18863 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18864 && face->box == FACE_NO_BOX
18865 && face->background == FRAME_BACKGROUND_PIXEL (f)
18866 #ifdef HAVE_WINDOW_SYSTEM
18867 && !face->stipple
18868 #endif
18869 && !it->glyph_row->reversed_p)
18870 return;
18872 /* Set the glyph row flag indicating that the face of the last glyph
18873 in the text area has to be drawn to the end of the text area. */
18874 it->glyph_row->fill_line_p = 1;
18876 /* If current character of IT is not ASCII, make sure we have the
18877 ASCII face. This will be automatically undone the next time
18878 get_next_display_element returns a multibyte character. Note
18879 that the character will always be single byte in unibyte
18880 text. */
18881 if (!ASCII_CHAR_P (it->c))
18883 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18886 if (FRAME_WINDOW_P (f))
18888 /* If the row is empty, add a space with the current face of IT,
18889 so that we know which face to draw. */
18890 if (it->glyph_row->used[TEXT_AREA] == 0)
18892 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18893 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18894 it->glyph_row->used[TEXT_AREA] = 1;
18896 /* Mode line and the header line don't have margins, and
18897 likewise the frame's tool-bar window, if there is any. */
18898 if (!(it->glyph_row->mode_line_p
18899 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18900 || (WINDOWP (f->tool_bar_window)
18901 && it->w == XWINDOW (f->tool_bar_window))
18902 #endif
18905 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18906 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18908 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18909 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18910 default_face->id;
18911 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18913 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18914 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18916 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18917 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18918 default_face->id;
18919 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18922 #ifdef HAVE_WINDOW_SYSTEM
18923 if (it->glyph_row->reversed_p)
18925 /* Prepend a stretch glyph to the row, such that the
18926 rightmost glyph will be drawn flushed all the way to the
18927 right margin of the window. The stretch glyph that will
18928 occupy the empty space, if any, to the left of the
18929 glyphs. */
18930 struct font *font = face->font ? face->font : FRAME_FONT (f);
18931 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18932 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18933 struct glyph *g;
18934 int row_width, stretch_ascent, stretch_width;
18935 struct text_pos saved_pos;
18936 int saved_face_id, saved_avoid_cursor, saved_box_start;
18938 for (row_width = 0, g = row_start; g < row_end; g++)
18939 row_width += g->pixel_width;
18940 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18941 if (stretch_width > 0)
18943 stretch_ascent =
18944 (((it->ascent + it->descent)
18945 * FONT_BASE (font)) / FONT_HEIGHT (font));
18946 saved_pos = it->position;
18947 memset (&it->position, 0, sizeof it->position);
18948 saved_avoid_cursor = it->avoid_cursor_p;
18949 it->avoid_cursor_p = 1;
18950 saved_face_id = it->face_id;
18951 saved_box_start = it->start_of_box_run_p;
18952 /* The last row's stretch glyph should get the default
18953 face, to avoid painting the rest of the window with
18954 the region face, if the region ends at ZV. */
18955 if (it->glyph_row->ends_at_zv_p)
18956 it->face_id = default_face->id;
18957 else
18958 it->face_id = face->id;
18959 it->start_of_box_run_p = 0;
18960 append_stretch_glyph (it, make_number (0), stretch_width,
18961 it->ascent + it->descent, stretch_ascent);
18962 it->position = saved_pos;
18963 it->avoid_cursor_p = saved_avoid_cursor;
18964 it->face_id = saved_face_id;
18965 it->start_of_box_run_p = saved_box_start;
18968 #endif /* HAVE_WINDOW_SYSTEM */
18970 else
18972 /* Save some values that must not be changed. */
18973 int saved_x = it->current_x;
18974 struct text_pos saved_pos;
18975 Lisp_Object saved_object;
18976 enum display_element_type saved_what = it->what;
18977 int saved_face_id = it->face_id;
18979 saved_object = it->object;
18980 saved_pos = it->position;
18982 it->what = IT_CHARACTER;
18983 memset (&it->position, 0, sizeof it->position);
18984 it->object = make_number (0);
18985 it->c = it->char_to_display = ' ';
18986 it->len = 1;
18988 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18989 && (it->glyph_row->used[LEFT_MARGIN_AREA]
18990 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
18991 && !it->glyph_row->mode_line_p
18992 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
18994 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
18995 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
18997 for (it->current_x = 0; g < e; g++)
18998 it->current_x += g->pixel_width;
19000 it->area = LEFT_MARGIN_AREA;
19001 it->face_id = default_face->id;
19002 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19003 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19005 PRODUCE_GLYPHS (it);
19006 /* term.c:produce_glyphs advances it->current_x only for
19007 TEXT_AREA. */
19008 it->current_x += it->pixel_width;
19011 it->current_x = saved_x;
19012 it->area = TEXT_AREA;
19015 /* The last row's blank glyphs should get the default face, to
19016 avoid painting the rest of the window with the region face,
19017 if the region ends at ZV. */
19018 if (it->glyph_row->ends_at_zv_p)
19019 it->face_id = default_face->id;
19020 else
19021 it->face_id = face->id;
19022 PRODUCE_GLYPHS (it);
19024 while (it->current_x <= it->last_visible_x)
19025 PRODUCE_GLYPHS (it);
19027 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19028 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19029 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19030 && !it->glyph_row->mode_line_p
19031 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19033 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19034 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19036 for ( ; g < e; g++)
19037 it->current_x += g->pixel_width;
19039 it->area = RIGHT_MARGIN_AREA;
19040 it->face_id = default_face->id;
19041 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19042 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19044 PRODUCE_GLYPHS (it);
19045 it->current_x += it->pixel_width;
19048 it->area = TEXT_AREA;
19051 /* Don't count these blanks really. It would let us insert a left
19052 truncation glyph below and make us set the cursor on them, maybe. */
19053 it->current_x = saved_x;
19054 it->object = saved_object;
19055 it->position = saved_pos;
19056 it->what = saved_what;
19057 it->face_id = saved_face_id;
19062 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19063 trailing whitespace. */
19065 static int
19066 trailing_whitespace_p (ptrdiff_t charpos)
19068 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19069 int c = 0;
19071 while (bytepos < ZV_BYTE
19072 && (c = FETCH_CHAR (bytepos),
19073 c == ' ' || c == '\t'))
19074 ++bytepos;
19076 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19078 if (bytepos != PT_BYTE)
19079 return 1;
19081 return 0;
19085 /* Highlight trailing whitespace, if any, in ROW. */
19087 static void
19088 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19090 int used = row->used[TEXT_AREA];
19092 if (used)
19094 struct glyph *start = row->glyphs[TEXT_AREA];
19095 struct glyph *glyph = start + used - 1;
19097 if (row->reversed_p)
19099 /* Right-to-left rows need to be processed in the opposite
19100 direction, so swap the edge pointers. */
19101 glyph = start;
19102 start = row->glyphs[TEXT_AREA] + used - 1;
19105 /* Skip over glyphs inserted to display the cursor at the
19106 end of a line, for extending the face of the last glyph
19107 to the end of the line on terminals, and for truncation
19108 and continuation glyphs. */
19109 if (!row->reversed_p)
19111 while (glyph >= start
19112 && glyph->type == CHAR_GLYPH
19113 && INTEGERP (glyph->object))
19114 --glyph;
19116 else
19118 while (glyph <= start
19119 && glyph->type == CHAR_GLYPH
19120 && INTEGERP (glyph->object))
19121 ++glyph;
19124 /* If last glyph is a space or stretch, and it's trailing
19125 whitespace, set the face of all trailing whitespace glyphs in
19126 IT->glyph_row to `trailing-whitespace'. */
19127 if ((row->reversed_p ? glyph <= start : glyph >= start)
19128 && BUFFERP (glyph->object)
19129 && (glyph->type == STRETCH_GLYPH
19130 || (glyph->type == CHAR_GLYPH
19131 && glyph->u.ch == ' '))
19132 && trailing_whitespace_p (glyph->charpos))
19134 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19135 if (face_id < 0)
19136 return;
19138 if (!row->reversed_p)
19140 while (glyph >= start
19141 && BUFFERP (glyph->object)
19142 && (glyph->type == STRETCH_GLYPH
19143 || (glyph->type == CHAR_GLYPH
19144 && glyph->u.ch == ' ')))
19145 (glyph--)->face_id = face_id;
19147 else
19149 while (glyph <= start
19150 && BUFFERP (glyph->object)
19151 && (glyph->type == STRETCH_GLYPH
19152 || (glyph->type == CHAR_GLYPH
19153 && glyph->u.ch == ' ')))
19154 (glyph++)->face_id = face_id;
19161 /* Value is non-zero if glyph row ROW should be
19162 considered to hold the buffer position CHARPOS. */
19164 static int
19165 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19167 int result = 1;
19169 if (charpos == CHARPOS (row->end.pos)
19170 || charpos == MATRIX_ROW_END_CHARPOS (row))
19172 /* Suppose the row ends on a string.
19173 Unless the row is continued, that means it ends on a newline
19174 in the string. If it's anything other than a display string
19175 (e.g., a before-string from an overlay), we don't want the
19176 cursor there. (This heuristic seems to give the optimal
19177 behavior for the various types of multi-line strings.)
19178 One exception: if the string has `cursor' property on one of
19179 its characters, we _do_ want the cursor there. */
19180 if (CHARPOS (row->end.string_pos) >= 0)
19182 if (row->continued_p)
19183 result = 1;
19184 else
19186 /* Check for `display' property. */
19187 struct glyph *beg = row->glyphs[TEXT_AREA];
19188 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19189 struct glyph *glyph;
19191 result = 0;
19192 for (glyph = end; glyph >= beg; --glyph)
19193 if (STRINGP (glyph->object))
19195 Lisp_Object prop
19196 = Fget_char_property (make_number (charpos),
19197 Qdisplay, Qnil);
19198 result =
19199 (!NILP (prop)
19200 && display_prop_string_p (prop, glyph->object));
19201 /* If there's a `cursor' property on one of the
19202 string's characters, this row is a cursor row,
19203 even though this is not a display string. */
19204 if (!result)
19206 Lisp_Object s = glyph->object;
19208 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19210 ptrdiff_t gpos = glyph->charpos;
19212 if (!NILP (Fget_char_property (make_number (gpos),
19213 Qcursor, s)))
19215 result = 1;
19216 break;
19220 break;
19224 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19226 /* If the row ends in middle of a real character,
19227 and the line is continued, we want the cursor here.
19228 That's because CHARPOS (ROW->end.pos) would equal
19229 PT if PT is before the character. */
19230 if (!row->ends_in_ellipsis_p)
19231 result = row->continued_p;
19232 else
19233 /* If the row ends in an ellipsis, then
19234 CHARPOS (ROW->end.pos) will equal point after the
19235 invisible text. We want that position to be displayed
19236 after the ellipsis. */
19237 result = 0;
19239 /* If the row ends at ZV, display the cursor at the end of that
19240 row instead of at the start of the row below. */
19241 else if (row->ends_at_zv_p)
19242 result = 1;
19243 else
19244 result = 0;
19247 return result;
19250 /* Value is non-zero if glyph row ROW should be
19251 used to hold the cursor. */
19253 static int
19254 cursor_row_p (struct glyph_row *row)
19256 return row_for_charpos_p (row, PT);
19261 /* Push the property PROP so that it will be rendered at the current
19262 position in IT. Return 1 if PROP was successfully pushed, 0
19263 otherwise. Called from handle_line_prefix to handle the
19264 `line-prefix' and `wrap-prefix' properties. */
19266 static int
19267 push_prefix_prop (struct it *it, Lisp_Object prop)
19269 struct text_pos pos =
19270 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19272 eassert (it->method == GET_FROM_BUFFER
19273 || it->method == GET_FROM_DISPLAY_VECTOR
19274 || it->method == GET_FROM_STRING);
19276 /* We need to save the current buffer/string position, so it will be
19277 restored by pop_it, because iterate_out_of_display_property
19278 depends on that being set correctly, but some situations leave
19279 it->position not yet set when this function is called. */
19280 push_it (it, &pos);
19282 if (STRINGP (prop))
19284 if (SCHARS (prop) == 0)
19286 pop_it (it);
19287 return 0;
19290 it->string = prop;
19291 it->string_from_prefix_prop_p = 1;
19292 it->multibyte_p = STRING_MULTIBYTE (it->string);
19293 it->current.overlay_string_index = -1;
19294 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19295 it->end_charpos = it->string_nchars = SCHARS (it->string);
19296 it->method = GET_FROM_STRING;
19297 it->stop_charpos = 0;
19298 it->prev_stop = 0;
19299 it->base_level_stop = 0;
19301 /* Force paragraph direction to be that of the parent
19302 buffer/string. */
19303 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19304 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19305 else
19306 it->paragraph_embedding = L2R;
19308 /* Set up the bidi iterator for this display string. */
19309 if (it->bidi_p)
19311 it->bidi_it.string.lstring = it->string;
19312 it->bidi_it.string.s = NULL;
19313 it->bidi_it.string.schars = it->end_charpos;
19314 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19315 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19316 it->bidi_it.string.unibyte = !it->multibyte_p;
19317 it->bidi_it.w = it->w;
19318 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19321 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19323 it->method = GET_FROM_STRETCH;
19324 it->object = prop;
19326 #ifdef HAVE_WINDOW_SYSTEM
19327 else if (IMAGEP (prop))
19329 it->what = IT_IMAGE;
19330 it->image_id = lookup_image (it->f, prop);
19331 it->method = GET_FROM_IMAGE;
19333 #endif /* HAVE_WINDOW_SYSTEM */
19334 else
19336 pop_it (it); /* bogus display property, give up */
19337 return 0;
19340 return 1;
19343 /* Return the character-property PROP at the current position in IT. */
19345 static Lisp_Object
19346 get_it_property (struct it *it, Lisp_Object prop)
19348 Lisp_Object position, object = it->object;
19350 if (STRINGP (object))
19351 position = make_number (IT_STRING_CHARPOS (*it));
19352 else if (BUFFERP (object))
19354 position = make_number (IT_CHARPOS (*it));
19355 object = it->window;
19357 else
19358 return Qnil;
19360 return Fget_char_property (position, prop, object);
19363 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19365 static void
19366 handle_line_prefix (struct it *it)
19368 Lisp_Object prefix;
19370 if (it->continuation_lines_width > 0)
19372 prefix = get_it_property (it, Qwrap_prefix);
19373 if (NILP (prefix))
19374 prefix = Vwrap_prefix;
19376 else
19378 prefix = get_it_property (it, Qline_prefix);
19379 if (NILP (prefix))
19380 prefix = Vline_prefix;
19382 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19384 /* If the prefix is wider than the window, and we try to wrap
19385 it, it would acquire its own wrap prefix, and so on till the
19386 iterator stack overflows. So, don't wrap the prefix. */
19387 it->line_wrap = TRUNCATE;
19388 it->avoid_cursor_p = 1;
19394 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19395 only for R2L lines from display_line and display_string, when they
19396 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19397 the line/string needs to be continued on the next glyph row. */
19398 static void
19399 unproduce_glyphs (struct it *it, int n)
19401 struct glyph *glyph, *end;
19403 eassert (it->glyph_row);
19404 eassert (it->glyph_row->reversed_p);
19405 eassert (it->area == TEXT_AREA);
19406 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19408 if (n > it->glyph_row->used[TEXT_AREA])
19409 n = it->glyph_row->used[TEXT_AREA];
19410 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19411 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19412 for ( ; glyph < end; glyph++)
19413 glyph[-n] = *glyph;
19416 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19417 and ROW->maxpos. */
19418 static void
19419 find_row_edges (struct it *it, struct glyph_row *row,
19420 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19421 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19423 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19424 lines' rows is implemented for bidi-reordered rows. */
19426 /* ROW->minpos is the value of min_pos, the minimal buffer position
19427 we have in ROW, or ROW->start.pos if that is smaller. */
19428 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19429 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19430 else
19431 /* We didn't find buffer positions smaller than ROW->start, or
19432 didn't find _any_ valid buffer positions in any of the glyphs,
19433 so we must trust the iterator's computed positions. */
19434 row->minpos = row->start.pos;
19435 if (max_pos <= 0)
19437 max_pos = CHARPOS (it->current.pos);
19438 max_bpos = BYTEPOS (it->current.pos);
19441 /* Here are the various use-cases for ending the row, and the
19442 corresponding values for ROW->maxpos:
19444 Line ends in a newline from buffer eol_pos + 1
19445 Line is continued from buffer max_pos + 1
19446 Line is truncated on right it->current.pos
19447 Line ends in a newline from string max_pos + 1(*)
19448 (*) + 1 only when line ends in a forward scan
19449 Line is continued from string max_pos
19450 Line is continued from display vector max_pos
19451 Line is entirely from a string min_pos == max_pos
19452 Line is entirely from a display vector min_pos == max_pos
19453 Line that ends at ZV ZV
19455 If you discover other use-cases, please add them here as
19456 appropriate. */
19457 if (row->ends_at_zv_p)
19458 row->maxpos = it->current.pos;
19459 else if (row->used[TEXT_AREA])
19461 int seen_this_string = 0;
19462 struct glyph_row *r1 = row - 1;
19464 /* Did we see the same display string on the previous row? */
19465 if (STRINGP (it->object)
19466 /* this is not the first row */
19467 && row > it->w->desired_matrix->rows
19468 /* previous row is not the header line */
19469 && !r1->mode_line_p
19470 /* previous row also ends in a newline from a string */
19471 && r1->ends_in_newline_from_string_p)
19473 struct glyph *start, *end;
19475 /* Search for the last glyph of the previous row that came
19476 from buffer or string. Depending on whether the row is
19477 L2R or R2L, we need to process it front to back or the
19478 other way round. */
19479 if (!r1->reversed_p)
19481 start = r1->glyphs[TEXT_AREA];
19482 end = start + r1->used[TEXT_AREA];
19483 /* Glyphs inserted by redisplay have an integer (zero)
19484 as their object. */
19485 while (end > start
19486 && INTEGERP ((end - 1)->object)
19487 && (end - 1)->charpos <= 0)
19488 --end;
19489 if (end > start)
19491 if (EQ ((end - 1)->object, it->object))
19492 seen_this_string = 1;
19494 else
19495 /* If all the glyphs of the previous row were inserted
19496 by redisplay, it means the previous row was
19497 produced from a single newline, which is only
19498 possible if that newline came from the same string
19499 as the one which produced this ROW. */
19500 seen_this_string = 1;
19502 else
19504 end = r1->glyphs[TEXT_AREA] - 1;
19505 start = end + r1->used[TEXT_AREA];
19506 while (end < start
19507 && INTEGERP ((end + 1)->object)
19508 && (end + 1)->charpos <= 0)
19509 ++end;
19510 if (end < start)
19512 if (EQ ((end + 1)->object, it->object))
19513 seen_this_string = 1;
19515 else
19516 seen_this_string = 1;
19519 /* Take note of each display string that covers a newline only
19520 once, the first time we see it. This is for when a display
19521 string includes more than one newline in it. */
19522 if (row->ends_in_newline_from_string_p && !seen_this_string)
19524 /* If we were scanning the buffer forward when we displayed
19525 the string, we want to account for at least one buffer
19526 position that belongs to this row (position covered by
19527 the display string), so that cursor positioning will
19528 consider this row as a candidate when point is at the end
19529 of the visual line represented by this row. This is not
19530 required when scanning back, because max_pos will already
19531 have a much larger value. */
19532 if (CHARPOS (row->end.pos) > max_pos)
19533 INC_BOTH (max_pos, max_bpos);
19534 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19536 else if (CHARPOS (it->eol_pos) > 0)
19537 SET_TEXT_POS (row->maxpos,
19538 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19539 else if (row->continued_p)
19541 /* If max_pos is different from IT's current position, it
19542 means IT->method does not belong to the display element
19543 at max_pos. However, it also means that the display
19544 element at max_pos was displayed in its entirety on this
19545 line, which is equivalent to saying that the next line
19546 starts at the next buffer position. */
19547 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19548 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19549 else
19551 INC_BOTH (max_pos, max_bpos);
19552 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19555 else if (row->truncated_on_right_p)
19556 /* display_line already called reseat_at_next_visible_line_start,
19557 which puts the iterator at the beginning of the next line, in
19558 the logical order. */
19559 row->maxpos = it->current.pos;
19560 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19561 /* A line that is entirely from a string/image/stretch... */
19562 row->maxpos = row->minpos;
19563 else
19564 emacs_abort ();
19566 else
19567 row->maxpos = it->current.pos;
19570 /* Construct the glyph row IT->glyph_row in the desired matrix of
19571 IT->w from text at the current position of IT. See dispextern.h
19572 for an overview of struct it. Value is non-zero if
19573 IT->glyph_row displays text, as opposed to a line displaying ZV
19574 only. */
19576 static int
19577 display_line (struct it *it)
19579 struct glyph_row *row = it->glyph_row;
19580 Lisp_Object overlay_arrow_string;
19581 struct it wrap_it;
19582 void *wrap_data = NULL;
19583 int may_wrap = 0, wrap_x IF_LINT (= 0);
19584 int wrap_row_used = -1;
19585 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19586 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19587 int wrap_row_extra_line_spacing IF_LINT (= 0);
19588 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19589 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19590 int cvpos;
19591 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19592 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19594 /* We always start displaying at hpos zero even if hscrolled. */
19595 eassert (it->hpos == 0 && it->current_x == 0);
19597 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19598 >= it->w->desired_matrix->nrows)
19600 it->w->nrows_scale_factor++;
19601 it->f->fonts_changed = 1;
19602 return 0;
19605 /* Clear the result glyph row and enable it. */
19606 prepare_desired_row (row);
19608 row->y = it->current_y;
19609 row->start = it->start;
19610 row->continuation_lines_width = it->continuation_lines_width;
19611 row->displays_text_p = 1;
19612 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19613 it->starts_in_middle_of_char_p = 0;
19615 /* Arrange the overlays nicely for our purposes. Usually, we call
19616 display_line on only one line at a time, in which case this
19617 can't really hurt too much, or we call it on lines which appear
19618 one after another in the buffer, in which case all calls to
19619 recenter_overlay_lists but the first will be pretty cheap. */
19620 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19622 /* Move over display elements that are not visible because we are
19623 hscrolled. This may stop at an x-position < IT->first_visible_x
19624 if the first glyph is partially visible or if we hit a line end. */
19625 if (it->current_x < it->first_visible_x)
19627 enum move_it_result move_result;
19629 this_line_min_pos = row->start.pos;
19630 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19631 MOVE_TO_POS | MOVE_TO_X);
19632 /* If we are under a large hscroll, move_it_in_display_line_to
19633 could hit the end of the line without reaching
19634 it->first_visible_x. Pretend that we did reach it. This is
19635 especially important on a TTY, where we will call
19636 extend_face_to_end_of_line, which needs to know how many
19637 blank glyphs to produce. */
19638 if (it->current_x < it->first_visible_x
19639 && (move_result == MOVE_NEWLINE_OR_CR
19640 || move_result == MOVE_POS_MATCH_OR_ZV))
19641 it->current_x = it->first_visible_x;
19643 /* Record the smallest positions seen while we moved over
19644 display elements that are not visible. This is needed by
19645 redisplay_internal for optimizing the case where the cursor
19646 stays inside the same line. The rest of this function only
19647 considers positions that are actually displayed, so
19648 RECORD_MAX_MIN_POS will not otherwise record positions that
19649 are hscrolled to the left of the left edge of the window. */
19650 min_pos = CHARPOS (this_line_min_pos);
19651 min_bpos = BYTEPOS (this_line_min_pos);
19653 else
19655 /* We only do this when not calling `move_it_in_display_line_to'
19656 above, because move_it_in_display_line_to calls
19657 handle_line_prefix itself. */
19658 handle_line_prefix (it);
19661 /* Get the initial row height. This is either the height of the
19662 text hscrolled, if there is any, or zero. */
19663 row->ascent = it->max_ascent;
19664 row->height = it->max_ascent + it->max_descent;
19665 row->phys_ascent = it->max_phys_ascent;
19666 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19667 row->extra_line_spacing = it->max_extra_line_spacing;
19669 /* Utility macro to record max and min buffer positions seen until now. */
19670 #define RECORD_MAX_MIN_POS(IT) \
19671 do \
19673 int composition_p = !STRINGP ((IT)->string) \
19674 && ((IT)->what == IT_COMPOSITION); \
19675 ptrdiff_t current_pos = \
19676 composition_p ? (IT)->cmp_it.charpos \
19677 : IT_CHARPOS (*(IT)); \
19678 ptrdiff_t current_bpos = \
19679 composition_p ? CHAR_TO_BYTE (current_pos) \
19680 : IT_BYTEPOS (*(IT)); \
19681 if (current_pos < min_pos) \
19683 min_pos = current_pos; \
19684 min_bpos = current_bpos; \
19686 if (IT_CHARPOS (*it) > max_pos) \
19688 max_pos = IT_CHARPOS (*it); \
19689 max_bpos = IT_BYTEPOS (*it); \
19692 while (0)
19694 /* Loop generating characters. The loop is left with IT on the next
19695 character to display. */
19696 while (1)
19698 int n_glyphs_before, hpos_before, x_before;
19699 int x, nglyphs;
19700 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19702 /* Retrieve the next thing to display. Value is zero if end of
19703 buffer reached. */
19704 if (!get_next_display_element (it))
19706 /* Maybe add a space at the end of this line that is used to
19707 display the cursor there under X. Set the charpos of the
19708 first glyph of blank lines not corresponding to any text
19709 to -1. */
19710 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19711 row->exact_window_width_line_p = 1;
19712 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19713 || row->used[TEXT_AREA] == 0)
19715 row->glyphs[TEXT_AREA]->charpos = -1;
19716 row->displays_text_p = 0;
19718 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19719 && (!MINI_WINDOW_P (it->w)
19720 || (minibuf_level && EQ (it->window, minibuf_window))))
19721 row->indicate_empty_line_p = 1;
19724 it->continuation_lines_width = 0;
19725 row->ends_at_zv_p = 1;
19726 /* A row that displays right-to-left text must always have
19727 its last face extended all the way to the end of line,
19728 even if this row ends in ZV, because we still write to
19729 the screen left to right. We also need to extend the
19730 last face if the default face is remapped to some
19731 different face, otherwise the functions that clear
19732 portions of the screen will clear with the default face's
19733 background color. */
19734 if (row->reversed_p
19735 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19736 extend_face_to_end_of_line (it);
19737 break;
19740 /* Now, get the metrics of what we want to display. This also
19741 generates glyphs in `row' (which is IT->glyph_row). */
19742 n_glyphs_before = row->used[TEXT_AREA];
19743 x = it->current_x;
19745 /* Remember the line height so far in case the next element doesn't
19746 fit on the line. */
19747 if (it->line_wrap != TRUNCATE)
19749 ascent = it->max_ascent;
19750 descent = it->max_descent;
19751 phys_ascent = it->max_phys_ascent;
19752 phys_descent = it->max_phys_descent;
19754 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19756 if (IT_DISPLAYING_WHITESPACE (it))
19757 may_wrap = 1;
19758 else if (may_wrap)
19760 SAVE_IT (wrap_it, *it, wrap_data);
19761 wrap_x = x;
19762 wrap_row_used = row->used[TEXT_AREA];
19763 wrap_row_ascent = row->ascent;
19764 wrap_row_height = row->height;
19765 wrap_row_phys_ascent = row->phys_ascent;
19766 wrap_row_phys_height = row->phys_height;
19767 wrap_row_extra_line_spacing = row->extra_line_spacing;
19768 wrap_row_min_pos = min_pos;
19769 wrap_row_min_bpos = min_bpos;
19770 wrap_row_max_pos = max_pos;
19771 wrap_row_max_bpos = max_bpos;
19772 may_wrap = 0;
19777 PRODUCE_GLYPHS (it);
19779 /* If this display element was in marginal areas, continue with
19780 the next one. */
19781 if (it->area != TEXT_AREA)
19783 row->ascent = max (row->ascent, it->max_ascent);
19784 row->height = max (row->height, it->max_ascent + it->max_descent);
19785 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19786 row->phys_height = max (row->phys_height,
19787 it->max_phys_ascent + it->max_phys_descent);
19788 row->extra_line_spacing = max (row->extra_line_spacing,
19789 it->max_extra_line_spacing);
19790 set_iterator_to_next (it, 1);
19791 continue;
19794 /* Does the display element fit on the line? If we truncate
19795 lines, we should draw past the right edge of the window. If
19796 we don't truncate, we want to stop so that we can display the
19797 continuation glyph before the right margin. If lines are
19798 continued, there are two possible strategies for characters
19799 resulting in more than 1 glyph (e.g. tabs): Display as many
19800 glyphs as possible in this line and leave the rest for the
19801 continuation line, or display the whole element in the next
19802 line. Original redisplay did the former, so we do it also. */
19803 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19804 hpos_before = it->hpos;
19805 x_before = x;
19807 if (/* Not a newline. */
19808 nglyphs > 0
19809 /* Glyphs produced fit entirely in the line. */
19810 && it->current_x < it->last_visible_x)
19812 it->hpos += nglyphs;
19813 row->ascent = max (row->ascent, it->max_ascent);
19814 row->height = max (row->height, it->max_ascent + it->max_descent);
19815 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19816 row->phys_height = max (row->phys_height,
19817 it->max_phys_ascent + it->max_phys_descent);
19818 row->extra_line_spacing = max (row->extra_line_spacing,
19819 it->max_extra_line_spacing);
19820 if (it->current_x - it->pixel_width < it->first_visible_x)
19821 row->x = x - it->first_visible_x;
19822 /* Record the maximum and minimum buffer positions seen so
19823 far in glyphs that will be displayed by this row. */
19824 if (it->bidi_p)
19825 RECORD_MAX_MIN_POS (it);
19827 else
19829 int i, new_x;
19830 struct glyph *glyph;
19832 for (i = 0; i < nglyphs; ++i, x = new_x)
19834 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19835 new_x = x + glyph->pixel_width;
19837 if (/* Lines are continued. */
19838 it->line_wrap != TRUNCATE
19839 && (/* Glyph doesn't fit on the line. */
19840 new_x > it->last_visible_x
19841 /* Or it fits exactly on a window system frame. */
19842 || (new_x == it->last_visible_x
19843 && FRAME_WINDOW_P (it->f)
19844 && (row->reversed_p
19845 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19846 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19848 /* End of a continued line. */
19850 if (it->hpos == 0
19851 || (new_x == it->last_visible_x
19852 && FRAME_WINDOW_P (it->f)
19853 && (row->reversed_p
19854 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19855 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19857 /* Current glyph is the only one on the line or
19858 fits exactly on the line. We must continue
19859 the line because we can't draw the cursor
19860 after the glyph. */
19861 row->continued_p = 1;
19862 it->current_x = new_x;
19863 it->continuation_lines_width += new_x;
19864 ++it->hpos;
19865 if (i == nglyphs - 1)
19867 /* If line-wrap is on, check if a previous
19868 wrap point was found. */
19869 if (wrap_row_used > 0
19870 /* Even if there is a previous wrap
19871 point, continue the line here as
19872 usual, if (i) the previous character
19873 was a space or tab AND (ii) the
19874 current character is not. */
19875 && (!may_wrap
19876 || IT_DISPLAYING_WHITESPACE (it)))
19877 goto back_to_wrap;
19879 /* Record the maximum and minimum buffer
19880 positions seen so far in glyphs that will be
19881 displayed by this row. */
19882 if (it->bidi_p)
19883 RECORD_MAX_MIN_POS (it);
19884 set_iterator_to_next (it, 1);
19885 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19887 if (!get_next_display_element (it))
19889 row->exact_window_width_line_p = 1;
19890 it->continuation_lines_width = 0;
19891 row->continued_p = 0;
19892 row->ends_at_zv_p = 1;
19894 else if (ITERATOR_AT_END_OF_LINE_P (it))
19896 row->continued_p = 0;
19897 row->exact_window_width_line_p = 1;
19901 else if (it->bidi_p)
19902 RECORD_MAX_MIN_POS (it);
19903 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19904 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19905 extend_face_to_end_of_line (it);
19907 else if (CHAR_GLYPH_PADDING_P (*glyph)
19908 && !FRAME_WINDOW_P (it->f))
19910 /* A padding glyph that doesn't fit on this line.
19911 This means the whole character doesn't fit
19912 on the line. */
19913 if (row->reversed_p)
19914 unproduce_glyphs (it, row->used[TEXT_AREA]
19915 - n_glyphs_before);
19916 row->used[TEXT_AREA] = n_glyphs_before;
19918 /* Fill the rest of the row with continuation
19919 glyphs like in 20.x. */
19920 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19921 < row->glyphs[1 + TEXT_AREA])
19922 produce_special_glyphs (it, IT_CONTINUATION);
19924 row->continued_p = 1;
19925 it->current_x = x_before;
19926 it->continuation_lines_width += x_before;
19928 /* Restore the height to what it was before the
19929 element not fitting on the line. */
19930 it->max_ascent = ascent;
19931 it->max_descent = descent;
19932 it->max_phys_ascent = phys_ascent;
19933 it->max_phys_descent = phys_descent;
19934 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19935 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19936 extend_face_to_end_of_line (it);
19938 else if (wrap_row_used > 0)
19940 back_to_wrap:
19941 if (row->reversed_p)
19942 unproduce_glyphs (it,
19943 row->used[TEXT_AREA] - wrap_row_used);
19944 RESTORE_IT (it, &wrap_it, wrap_data);
19945 it->continuation_lines_width += wrap_x;
19946 row->used[TEXT_AREA] = wrap_row_used;
19947 row->ascent = wrap_row_ascent;
19948 row->height = wrap_row_height;
19949 row->phys_ascent = wrap_row_phys_ascent;
19950 row->phys_height = wrap_row_phys_height;
19951 row->extra_line_spacing = wrap_row_extra_line_spacing;
19952 min_pos = wrap_row_min_pos;
19953 min_bpos = wrap_row_min_bpos;
19954 max_pos = wrap_row_max_pos;
19955 max_bpos = wrap_row_max_bpos;
19956 row->continued_p = 1;
19957 row->ends_at_zv_p = 0;
19958 row->exact_window_width_line_p = 0;
19959 it->continuation_lines_width += x;
19961 /* Make sure that a non-default face is extended
19962 up to the right margin of the window. */
19963 extend_face_to_end_of_line (it);
19965 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19967 /* A TAB that extends past the right edge of the
19968 window. This produces a single glyph on
19969 window system frames. We leave the glyph in
19970 this row and let it fill the row, but don't
19971 consume the TAB. */
19972 if ((row->reversed_p
19973 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19974 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19975 produce_special_glyphs (it, IT_CONTINUATION);
19976 it->continuation_lines_width += it->last_visible_x;
19977 row->ends_in_middle_of_char_p = 1;
19978 row->continued_p = 1;
19979 glyph->pixel_width = it->last_visible_x - x;
19980 it->starts_in_middle_of_char_p = 1;
19981 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19982 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19983 extend_face_to_end_of_line (it);
19985 else
19987 /* Something other than a TAB that draws past
19988 the right edge of the window. Restore
19989 positions to values before the element. */
19990 if (row->reversed_p)
19991 unproduce_glyphs (it, row->used[TEXT_AREA]
19992 - (n_glyphs_before + i));
19993 row->used[TEXT_AREA] = n_glyphs_before + i;
19995 /* Display continuation glyphs. */
19996 it->current_x = x_before;
19997 it->continuation_lines_width += x;
19998 if (!FRAME_WINDOW_P (it->f)
19999 || (row->reversed_p
20000 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20001 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20002 produce_special_glyphs (it, IT_CONTINUATION);
20003 row->continued_p = 1;
20005 extend_face_to_end_of_line (it);
20007 if (nglyphs > 1 && i > 0)
20009 row->ends_in_middle_of_char_p = 1;
20010 it->starts_in_middle_of_char_p = 1;
20013 /* Restore the height to what it was before the
20014 element not fitting on the line. */
20015 it->max_ascent = ascent;
20016 it->max_descent = descent;
20017 it->max_phys_ascent = phys_ascent;
20018 it->max_phys_descent = phys_descent;
20021 break;
20023 else if (new_x > it->first_visible_x)
20025 /* Increment number of glyphs actually displayed. */
20026 ++it->hpos;
20028 /* Record the maximum and minimum buffer positions
20029 seen so far in glyphs that will be displayed by
20030 this row. */
20031 if (it->bidi_p)
20032 RECORD_MAX_MIN_POS (it);
20034 if (x < it->first_visible_x)
20035 /* Glyph is partially visible, i.e. row starts at
20036 negative X position. */
20037 row->x = x - it->first_visible_x;
20039 else
20041 /* Glyph is completely off the left margin of the
20042 window. This should not happen because of the
20043 move_it_in_display_line at the start of this
20044 function, unless the text display area of the
20045 window is empty. */
20046 eassert (it->first_visible_x <= it->last_visible_x);
20049 /* Even if this display element produced no glyphs at all,
20050 we want to record its position. */
20051 if (it->bidi_p && nglyphs == 0)
20052 RECORD_MAX_MIN_POS (it);
20054 row->ascent = max (row->ascent, it->max_ascent);
20055 row->height = max (row->height, it->max_ascent + it->max_descent);
20056 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20057 row->phys_height = max (row->phys_height,
20058 it->max_phys_ascent + it->max_phys_descent);
20059 row->extra_line_spacing = max (row->extra_line_spacing,
20060 it->max_extra_line_spacing);
20062 /* End of this display line if row is continued. */
20063 if (row->continued_p || row->ends_at_zv_p)
20064 break;
20067 at_end_of_line:
20068 /* Is this a line end? If yes, we're also done, after making
20069 sure that a non-default face is extended up to the right
20070 margin of the window. */
20071 if (ITERATOR_AT_END_OF_LINE_P (it))
20073 int used_before = row->used[TEXT_AREA];
20075 row->ends_in_newline_from_string_p = STRINGP (it->object);
20077 /* Add a space at the end of the line that is used to
20078 display the cursor there. */
20079 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20080 append_space_for_newline (it, 0);
20082 /* Extend the face to the end of the line. */
20083 extend_face_to_end_of_line (it);
20085 /* Make sure we have the position. */
20086 if (used_before == 0)
20087 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20089 /* Record the position of the newline, for use in
20090 find_row_edges. */
20091 it->eol_pos = it->current.pos;
20093 /* Consume the line end. This skips over invisible lines. */
20094 set_iterator_to_next (it, 1);
20095 it->continuation_lines_width = 0;
20096 break;
20099 /* Proceed with next display element. Note that this skips
20100 over lines invisible because of selective display. */
20101 set_iterator_to_next (it, 1);
20103 /* If we truncate lines, we are done when the last displayed
20104 glyphs reach past the right margin of the window. */
20105 if (it->line_wrap == TRUNCATE
20106 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20107 ? (it->current_x >= it->last_visible_x)
20108 : (it->current_x > it->last_visible_x)))
20110 /* Maybe add truncation glyphs. */
20111 if (!FRAME_WINDOW_P (it->f)
20112 || (row->reversed_p
20113 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20114 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20116 int i, n;
20118 if (!row->reversed_p)
20120 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20121 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20122 break;
20124 else
20126 for (i = 0; i < row->used[TEXT_AREA]; i++)
20127 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20128 break;
20129 /* Remove any padding glyphs at the front of ROW, to
20130 make room for the truncation glyphs we will be
20131 adding below. The loop below always inserts at
20132 least one truncation glyph, so also remove the
20133 last glyph added to ROW. */
20134 unproduce_glyphs (it, i + 1);
20135 /* Adjust i for the loop below. */
20136 i = row->used[TEXT_AREA] - (i + 1);
20139 it->current_x = x_before;
20140 if (!FRAME_WINDOW_P (it->f))
20142 for (n = row->used[TEXT_AREA]; i < n; ++i)
20144 row->used[TEXT_AREA] = i;
20145 produce_special_glyphs (it, IT_TRUNCATION);
20148 else
20150 row->used[TEXT_AREA] = i;
20151 produce_special_glyphs (it, IT_TRUNCATION);
20154 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20156 /* Don't truncate if we can overflow newline into fringe. */
20157 if (!get_next_display_element (it))
20159 it->continuation_lines_width = 0;
20160 row->ends_at_zv_p = 1;
20161 row->exact_window_width_line_p = 1;
20162 break;
20164 if (ITERATOR_AT_END_OF_LINE_P (it))
20166 row->exact_window_width_line_p = 1;
20167 goto at_end_of_line;
20169 it->current_x = x_before;
20172 row->truncated_on_right_p = 1;
20173 it->continuation_lines_width = 0;
20174 reseat_at_next_visible_line_start (it, 0);
20175 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20176 it->hpos = hpos_before;
20177 break;
20181 if (wrap_data)
20182 bidi_unshelve_cache (wrap_data, 1);
20184 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20185 at the left window margin. */
20186 if (it->first_visible_x
20187 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20189 if (!FRAME_WINDOW_P (it->f)
20190 || (row->reversed_p
20191 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20192 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20193 insert_left_trunc_glyphs (it);
20194 row->truncated_on_left_p = 1;
20197 /* Remember the position at which this line ends.
20199 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20200 cannot be before the call to find_row_edges below, since that is
20201 where these positions are determined. */
20202 row->end = it->current;
20203 if (!it->bidi_p)
20205 row->minpos = row->start.pos;
20206 row->maxpos = row->end.pos;
20208 else
20210 /* ROW->minpos and ROW->maxpos must be the smallest and
20211 `1 + the largest' buffer positions in ROW. But if ROW was
20212 bidi-reordered, these two positions can be anywhere in the
20213 row, so we must determine them now. */
20214 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20217 /* If the start of this line is the overlay arrow-position, then
20218 mark this glyph row as the one containing the overlay arrow.
20219 This is clearly a mess with variable size fonts. It would be
20220 better to let it be displayed like cursors under X. */
20221 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20222 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20223 !NILP (overlay_arrow_string)))
20225 /* Overlay arrow in window redisplay is a fringe bitmap. */
20226 if (STRINGP (overlay_arrow_string))
20228 struct glyph_row *arrow_row
20229 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20230 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20231 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20232 struct glyph *p = row->glyphs[TEXT_AREA];
20233 struct glyph *p2, *end;
20235 /* Copy the arrow glyphs. */
20236 while (glyph < arrow_end)
20237 *p++ = *glyph++;
20239 /* Throw away padding glyphs. */
20240 p2 = p;
20241 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20242 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20243 ++p2;
20244 if (p2 > p)
20246 while (p2 < end)
20247 *p++ = *p2++;
20248 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20251 else
20253 eassert (INTEGERP (overlay_arrow_string));
20254 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20256 overlay_arrow_seen = 1;
20259 /* Highlight trailing whitespace. */
20260 if (!NILP (Vshow_trailing_whitespace))
20261 highlight_trailing_whitespace (it->f, it->glyph_row);
20263 /* Compute pixel dimensions of this line. */
20264 compute_line_metrics (it);
20266 /* Implementation note: No changes in the glyphs of ROW or in their
20267 faces can be done past this point, because compute_line_metrics
20268 computes ROW's hash value and stores it within the glyph_row
20269 structure. */
20271 /* Record whether this row ends inside an ellipsis. */
20272 row->ends_in_ellipsis_p
20273 = (it->method == GET_FROM_DISPLAY_VECTOR
20274 && it->ellipsis_p);
20276 /* Save fringe bitmaps in this row. */
20277 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20278 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20279 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20280 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20282 it->left_user_fringe_bitmap = 0;
20283 it->left_user_fringe_face_id = 0;
20284 it->right_user_fringe_bitmap = 0;
20285 it->right_user_fringe_face_id = 0;
20287 /* Maybe set the cursor. */
20288 cvpos = it->w->cursor.vpos;
20289 if ((cvpos < 0
20290 /* In bidi-reordered rows, keep checking for proper cursor
20291 position even if one has been found already, because buffer
20292 positions in such rows change non-linearly with ROW->VPOS,
20293 when a line is continued. One exception: when we are at ZV,
20294 display cursor on the first suitable glyph row, since all
20295 the empty rows after that also have their position set to ZV. */
20296 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20297 lines' rows is implemented for bidi-reordered rows. */
20298 || (it->bidi_p
20299 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20300 && PT >= MATRIX_ROW_START_CHARPOS (row)
20301 && PT <= MATRIX_ROW_END_CHARPOS (row)
20302 && cursor_row_p (row))
20303 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20305 /* Prepare for the next line. This line starts horizontally at (X
20306 HPOS) = (0 0). Vertical positions are incremented. As a
20307 convenience for the caller, IT->glyph_row is set to the next
20308 row to be used. */
20309 it->current_x = it->hpos = 0;
20310 it->current_y += row->height;
20311 SET_TEXT_POS (it->eol_pos, 0, 0);
20312 ++it->vpos;
20313 ++it->glyph_row;
20314 /* The next row should by default use the same value of the
20315 reversed_p flag as this one. set_iterator_to_next decides when
20316 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20317 the flag accordingly. */
20318 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20319 it->glyph_row->reversed_p = row->reversed_p;
20320 it->start = row->end;
20321 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20323 #undef RECORD_MAX_MIN_POS
20326 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20327 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20328 doc: /* Return paragraph direction at point in BUFFER.
20329 Value is either `left-to-right' or `right-to-left'.
20330 If BUFFER is omitted or nil, it defaults to the current buffer.
20332 Paragraph direction determines how the text in the paragraph is displayed.
20333 In left-to-right paragraphs, text begins at the left margin of the window
20334 and the reading direction is generally left to right. In right-to-left
20335 paragraphs, text begins at the right margin and is read from right to left.
20337 See also `bidi-paragraph-direction'. */)
20338 (Lisp_Object buffer)
20340 struct buffer *buf = current_buffer;
20341 struct buffer *old = buf;
20343 if (! NILP (buffer))
20345 CHECK_BUFFER (buffer);
20346 buf = XBUFFER (buffer);
20349 if (NILP (BVAR (buf, bidi_display_reordering))
20350 || NILP (BVAR (buf, enable_multibyte_characters))
20351 /* When we are loading loadup.el, the character property tables
20352 needed for bidi iteration are not yet available. */
20353 || !NILP (Vpurify_flag))
20354 return Qleft_to_right;
20355 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20356 return BVAR (buf, bidi_paragraph_direction);
20357 else
20359 /* Determine the direction from buffer text. We could try to
20360 use current_matrix if it is up to date, but this seems fast
20361 enough as it is. */
20362 struct bidi_it itb;
20363 ptrdiff_t pos = BUF_PT (buf);
20364 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20365 int c;
20366 void *itb_data = bidi_shelve_cache ();
20368 set_buffer_temp (buf);
20369 /* bidi_paragraph_init finds the base direction of the paragraph
20370 by searching forward from paragraph start. We need the base
20371 direction of the current or _previous_ paragraph, so we need
20372 to make sure we are within that paragraph. To that end, find
20373 the previous non-empty line. */
20374 if (pos >= ZV && pos > BEGV)
20375 DEC_BOTH (pos, bytepos);
20376 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20377 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20379 while ((c = FETCH_BYTE (bytepos)) == '\n'
20380 || c == ' ' || c == '\t' || c == '\f')
20382 if (bytepos <= BEGV_BYTE)
20383 break;
20384 bytepos--;
20385 pos--;
20387 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20388 bytepos--;
20390 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20391 itb.paragraph_dir = NEUTRAL_DIR;
20392 itb.string.s = NULL;
20393 itb.string.lstring = Qnil;
20394 itb.string.bufpos = 0;
20395 itb.string.unibyte = 0;
20396 /* We have no window to use here for ignoring window-specific
20397 overlays. Using NULL for window pointer will cause
20398 compute_display_string_pos to use the current buffer. */
20399 itb.w = NULL;
20400 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20401 bidi_unshelve_cache (itb_data, 0);
20402 set_buffer_temp (old);
20403 switch (itb.paragraph_dir)
20405 case L2R:
20406 return Qleft_to_right;
20407 break;
20408 case R2L:
20409 return Qright_to_left;
20410 break;
20411 default:
20412 emacs_abort ();
20417 DEFUN ("move-point-visually", Fmove_point_visually,
20418 Smove_point_visually, 1, 1, 0,
20419 doc: /* Move point in the visual order in the specified DIRECTION.
20420 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20421 left.
20423 Value is the new character position of point. */)
20424 (Lisp_Object direction)
20426 struct window *w = XWINDOW (selected_window);
20427 struct buffer *b = XBUFFER (w->contents);
20428 struct glyph_row *row;
20429 int dir;
20430 Lisp_Object paragraph_dir;
20432 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20433 (!(ROW)->continued_p \
20434 && INTEGERP ((GLYPH)->object) \
20435 && (GLYPH)->type == CHAR_GLYPH \
20436 && (GLYPH)->u.ch == ' ' \
20437 && (GLYPH)->charpos >= 0 \
20438 && !(GLYPH)->avoid_cursor_p)
20440 CHECK_NUMBER (direction);
20441 dir = XINT (direction);
20442 if (dir > 0)
20443 dir = 1;
20444 else
20445 dir = -1;
20447 /* If current matrix is up-to-date, we can use the information
20448 recorded in the glyphs, at least as long as the goal is on the
20449 screen. */
20450 if (w->window_end_valid
20451 && !windows_or_buffers_changed
20452 && b
20453 && !b->clip_changed
20454 && !b->prevent_redisplay_optimizations_p
20455 && !window_outdated (w)
20456 && w->cursor.vpos >= 0
20457 && w->cursor.vpos < w->current_matrix->nrows
20458 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20460 struct glyph *g = row->glyphs[TEXT_AREA];
20461 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20462 struct glyph *gpt = g + w->cursor.hpos;
20464 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20466 if (BUFFERP (g->object) && g->charpos != PT)
20468 SET_PT (g->charpos);
20469 w->cursor.vpos = -1;
20470 return make_number (PT);
20472 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20474 ptrdiff_t new_pos;
20476 if (BUFFERP (gpt->object))
20478 new_pos = PT;
20479 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20480 new_pos += (row->reversed_p ? -dir : dir);
20481 else
20482 new_pos -= (row->reversed_p ? -dir : dir);;
20484 else if (BUFFERP (g->object))
20485 new_pos = g->charpos;
20486 else
20487 break;
20488 SET_PT (new_pos);
20489 w->cursor.vpos = -1;
20490 return make_number (PT);
20492 else if (ROW_GLYPH_NEWLINE_P (row, g))
20494 /* Glyphs inserted at the end of a non-empty line for
20495 positioning the cursor have zero charpos, so we must
20496 deduce the value of point by other means. */
20497 if (g->charpos > 0)
20498 SET_PT (g->charpos);
20499 else if (row->ends_at_zv_p && PT != ZV)
20500 SET_PT (ZV);
20501 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20502 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20503 else
20504 break;
20505 w->cursor.vpos = -1;
20506 return make_number (PT);
20509 if (g == e || INTEGERP (g->object))
20511 if (row->truncated_on_left_p || row->truncated_on_right_p)
20512 goto simulate_display;
20513 if (!row->reversed_p)
20514 row += dir;
20515 else
20516 row -= dir;
20517 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20518 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20519 goto simulate_display;
20521 if (dir > 0)
20523 if (row->reversed_p && !row->continued_p)
20525 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20526 w->cursor.vpos = -1;
20527 return make_number (PT);
20529 g = row->glyphs[TEXT_AREA];
20530 e = g + row->used[TEXT_AREA];
20531 for ( ; g < e; g++)
20533 if (BUFFERP (g->object)
20534 /* Empty lines have only one glyph, which stands
20535 for the newline, and whose charpos is the
20536 buffer position of the newline. */
20537 || ROW_GLYPH_NEWLINE_P (row, g)
20538 /* When the buffer ends in a newline, the line at
20539 EOB also has one glyph, but its charpos is -1. */
20540 || (row->ends_at_zv_p
20541 && !row->reversed_p
20542 && INTEGERP (g->object)
20543 && g->type == CHAR_GLYPH
20544 && g->u.ch == ' '))
20546 if (g->charpos > 0)
20547 SET_PT (g->charpos);
20548 else if (!row->reversed_p
20549 && row->ends_at_zv_p
20550 && PT != ZV)
20551 SET_PT (ZV);
20552 else
20553 continue;
20554 w->cursor.vpos = -1;
20555 return make_number (PT);
20559 else
20561 if (!row->reversed_p && !row->continued_p)
20563 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20564 w->cursor.vpos = -1;
20565 return make_number (PT);
20567 e = row->glyphs[TEXT_AREA];
20568 g = e + row->used[TEXT_AREA] - 1;
20569 for ( ; g >= e; g--)
20571 if (BUFFERP (g->object)
20572 || (ROW_GLYPH_NEWLINE_P (row, g)
20573 && g->charpos > 0)
20574 /* Empty R2L lines on GUI frames have the buffer
20575 position of the newline stored in the stretch
20576 glyph. */
20577 || g->type == STRETCH_GLYPH
20578 || (row->ends_at_zv_p
20579 && row->reversed_p
20580 && INTEGERP (g->object)
20581 && g->type == CHAR_GLYPH
20582 && g->u.ch == ' '))
20584 if (g->charpos > 0)
20585 SET_PT (g->charpos);
20586 else if (row->reversed_p
20587 && row->ends_at_zv_p
20588 && PT != ZV)
20589 SET_PT (ZV);
20590 else
20591 continue;
20592 w->cursor.vpos = -1;
20593 return make_number (PT);
20600 simulate_display:
20602 /* If we wind up here, we failed to move by using the glyphs, so we
20603 need to simulate display instead. */
20605 if (b)
20606 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20607 else
20608 paragraph_dir = Qleft_to_right;
20609 if (EQ (paragraph_dir, Qright_to_left))
20610 dir = -dir;
20611 if (PT <= BEGV && dir < 0)
20612 xsignal0 (Qbeginning_of_buffer);
20613 else if (PT >= ZV && dir > 0)
20614 xsignal0 (Qend_of_buffer);
20615 else
20617 struct text_pos pt;
20618 struct it it;
20619 int pt_x, target_x, pixel_width, pt_vpos;
20620 bool at_eol_p;
20621 bool overshoot_expected = false;
20622 bool target_is_eol_p = false;
20624 /* Setup the arena. */
20625 SET_TEXT_POS (pt, PT, PT_BYTE);
20626 start_display (&it, w, pt);
20628 if (it.cmp_it.id < 0
20629 && it.method == GET_FROM_STRING
20630 && it.area == TEXT_AREA
20631 && it.string_from_display_prop_p
20632 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20633 overshoot_expected = true;
20635 /* Find the X coordinate of point. We start from the beginning
20636 of this or previous line to make sure we are before point in
20637 the logical order (since the move_it_* functions can only
20638 move forward). */
20639 reseat:
20640 reseat_at_previous_visible_line_start (&it);
20641 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20642 if (IT_CHARPOS (it) != PT)
20644 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20645 -1, -1, -1, MOVE_TO_POS);
20646 /* If we missed point because the character there is
20647 displayed out of a display vector that has more than one
20648 glyph, retry expecting overshoot. */
20649 if (it.method == GET_FROM_DISPLAY_VECTOR
20650 && it.current.dpvec_index > 0
20651 && !overshoot_expected)
20653 overshoot_expected = true;
20654 goto reseat;
20656 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20657 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20659 pt_x = it.current_x;
20660 pt_vpos = it.vpos;
20661 if (dir > 0 || overshoot_expected)
20663 struct glyph_row *row = it.glyph_row;
20665 /* When point is at beginning of line, we don't have
20666 information about the glyph there loaded into struct
20667 it. Calling get_next_display_element fixes that. */
20668 if (pt_x == 0)
20669 get_next_display_element (&it);
20670 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20671 it.glyph_row = NULL;
20672 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20673 it.glyph_row = row;
20674 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20675 it, lest it will become out of sync with it's buffer
20676 position. */
20677 it.current_x = pt_x;
20679 else
20680 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20681 pixel_width = it.pixel_width;
20682 if (overshoot_expected && at_eol_p)
20683 pixel_width = 0;
20684 else if (pixel_width <= 0)
20685 pixel_width = 1;
20687 /* If there's a display string (or something similar) at point,
20688 we are actually at the glyph to the left of point, so we need
20689 to correct the X coordinate. */
20690 if (overshoot_expected)
20692 if (it.bidi_p)
20693 pt_x += pixel_width * it.bidi_it.scan_dir;
20694 else
20695 pt_x += pixel_width;
20698 /* Compute target X coordinate, either to the left or to the
20699 right of point. On TTY frames, all characters have the same
20700 pixel width of 1, so we can use that. On GUI frames we don't
20701 have an easy way of getting at the pixel width of the
20702 character to the left of point, so we use a different method
20703 of getting to that place. */
20704 if (dir > 0)
20705 target_x = pt_x + pixel_width;
20706 else
20707 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20709 /* Target X coordinate could be one line above or below the line
20710 of point, in which case we need to adjust the target X
20711 coordinate. Also, if moving to the left, we need to begin at
20712 the left edge of the point's screen line. */
20713 if (dir < 0)
20715 if (pt_x > 0)
20717 start_display (&it, w, pt);
20718 reseat_at_previous_visible_line_start (&it);
20719 it.current_x = it.current_y = it.hpos = 0;
20720 if (pt_vpos != 0)
20721 move_it_by_lines (&it, pt_vpos);
20723 else
20725 move_it_by_lines (&it, -1);
20726 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20727 target_is_eol_p = true;
20730 else
20732 if (at_eol_p
20733 || (target_x >= it.last_visible_x
20734 && it.line_wrap != TRUNCATE))
20736 if (pt_x > 0)
20737 move_it_by_lines (&it, 0);
20738 move_it_by_lines (&it, 1);
20739 target_x = 0;
20743 /* Move to the target X coordinate. */
20744 #ifdef HAVE_WINDOW_SYSTEM
20745 /* On GUI frames, as we don't know the X coordinate of the
20746 character to the left of point, moving point to the left
20747 requires walking, one grapheme cluster at a time, until we
20748 find ourself at a place immediately to the left of the
20749 character at point. */
20750 if (FRAME_WINDOW_P (it.f) && dir < 0)
20752 struct text_pos new_pos;
20753 enum move_it_result rc = MOVE_X_REACHED;
20755 if (it.current_x == 0)
20756 get_next_display_element (&it);
20757 if (it.what == IT_COMPOSITION)
20759 new_pos.charpos = it.cmp_it.charpos;
20760 new_pos.bytepos = -1;
20762 else
20763 new_pos = it.current.pos;
20765 while (it.current_x + it.pixel_width <= target_x
20766 && rc == MOVE_X_REACHED)
20768 int new_x = it.current_x + it.pixel_width;
20770 /* For composed characters, we want the position of the
20771 first character in the grapheme cluster (usually, the
20772 composition's base character), whereas it.current
20773 might give us the position of the _last_ one, e.g. if
20774 the composition is rendered in reverse due to bidi
20775 reordering. */
20776 if (it.what == IT_COMPOSITION)
20778 new_pos.charpos = it.cmp_it.charpos;
20779 new_pos.bytepos = -1;
20781 else
20782 new_pos = it.current.pos;
20783 if (new_x == it.current_x)
20784 new_x++;
20785 rc = move_it_in_display_line_to (&it, ZV, new_x,
20786 MOVE_TO_POS | MOVE_TO_X);
20787 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20788 break;
20790 /* The previous position we saw in the loop is the one we
20791 want. */
20792 if (new_pos.bytepos == -1)
20793 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20794 it.current.pos = new_pos;
20796 else
20797 #endif
20798 if (it.current_x != target_x)
20799 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20801 /* When lines are truncated, the above loop will stop at the
20802 window edge. But we want to get to the end of line, even if
20803 it is beyond the window edge; automatic hscroll will then
20804 scroll the window to show point as appropriate. */
20805 if (target_is_eol_p && it.line_wrap == TRUNCATE
20806 && get_next_display_element (&it))
20808 struct text_pos new_pos = it.current.pos;
20810 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20812 set_iterator_to_next (&it, 0);
20813 if (it.method == GET_FROM_BUFFER)
20814 new_pos = it.current.pos;
20815 if (!get_next_display_element (&it))
20816 break;
20819 it.current.pos = new_pos;
20822 /* If we ended up in a display string that covers point, move to
20823 buffer position to the right in the visual order. */
20824 if (dir > 0)
20826 while (IT_CHARPOS (it) == PT)
20828 set_iterator_to_next (&it, 0);
20829 if (!get_next_display_element (&it))
20830 break;
20834 /* Move point to that position. */
20835 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20838 return make_number (PT);
20840 #undef ROW_GLYPH_NEWLINE_P
20844 /***********************************************************************
20845 Menu Bar
20846 ***********************************************************************/
20848 /* Redisplay the menu bar in the frame for window W.
20850 The menu bar of X frames that don't have X toolkit support is
20851 displayed in a special window W->frame->menu_bar_window.
20853 The menu bar of terminal frames is treated specially as far as
20854 glyph matrices are concerned. Menu bar lines are not part of
20855 windows, so the update is done directly on the frame matrix rows
20856 for the menu bar. */
20858 static void
20859 display_menu_bar (struct window *w)
20861 struct frame *f = XFRAME (WINDOW_FRAME (w));
20862 struct it it;
20863 Lisp_Object items;
20864 int i;
20866 /* Don't do all this for graphical frames. */
20867 #ifdef HAVE_NTGUI
20868 if (FRAME_W32_P (f))
20869 return;
20870 #endif
20871 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20872 if (FRAME_X_P (f))
20873 return;
20874 #endif
20876 #ifdef HAVE_NS
20877 if (FRAME_NS_P (f))
20878 return;
20879 #endif /* HAVE_NS */
20881 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20882 eassert (!FRAME_WINDOW_P (f));
20883 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20884 it.first_visible_x = 0;
20885 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20886 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_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 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20898 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20900 else
20901 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20903 /* This is a TTY frame, i.e. character hpos/vpos are used as
20904 pixel x/y. */
20905 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20906 MENU_FACE_ID);
20907 it.first_visible_x = 0;
20908 it.last_visible_x = FRAME_COLS (f);
20911 /* FIXME: This should be controlled by a user option. See the
20912 comments in redisplay_tool_bar and display_mode_line about
20913 this. */
20914 it.paragraph_embedding = L2R;
20916 /* Clear all rows of the menu bar. */
20917 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20919 struct glyph_row *row = it.glyph_row + i;
20920 clear_glyph_row (row);
20921 row->enabled_p = true;
20922 row->full_width_p = 1;
20925 /* Display all items of the menu bar. */
20926 items = FRAME_MENU_BAR_ITEMS (it.f);
20927 for (i = 0; i < ASIZE (items); i += 4)
20929 Lisp_Object string;
20931 /* Stop at nil string. */
20932 string = AREF (items, i + 1);
20933 if (NILP (string))
20934 break;
20936 /* Remember where item was displayed. */
20937 ASET (items, i + 3, make_number (it.hpos));
20939 /* Display the item, pad with one space. */
20940 if (it.current_x < it.last_visible_x)
20941 display_string (NULL, string, Qnil, 0, 0, &it,
20942 SCHARS (string) + 1, 0, 0, -1);
20945 /* Fill out the line with spaces. */
20946 if (it.current_x < it.last_visible_x)
20947 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20949 /* Compute the total height of the lines. */
20950 compute_line_metrics (&it);
20953 /* Deep copy of a glyph row, including the glyphs. */
20954 static void
20955 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20957 struct glyph *pointers[1 + LAST_AREA];
20958 int to_used = to->used[TEXT_AREA];
20960 /* Save glyph pointers of TO. */
20961 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20963 /* Do a structure assignment. */
20964 *to = *from;
20966 /* Restore original glyph pointers of TO. */
20967 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20969 /* Copy the glyphs. */
20970 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20971 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20973 /* If we filled only part of the TO row, fill the rest with
20974 space_glyph (which will display as empty space). */
20975 if (to_used > from->used[TEXT_AREA])
20976 fill_up_frame_row_with_spaces (to, to_used);
20979 /* Display one menu item on a TTY, by overwriting the glyphs in the
20980 frame F's desired glyph matrix with glyphs produced from the menu
20981 item text. Called from term.c to display TTY drop-down menus one
20982 item at a time.
20984 ITEM_TEXT is the menu item text as a C string.
20986 FACE_ID is the face ID to be used for this menu item. FACE_ID
20987 could specify one of 3 faces: a face for an enabled item, a face
20988 for a disabled item, or a face for a selected item.
20990 X and Y are coordinates of the first glyph in the frame's desired
20991 matrix to be overwritten by the menu item. Since this is a TTY, Y
20992 is the zero-based number of the glyph row and X is the zero-based
20993 glyph number in the row, starting from left, where to start
20994 displaying the item.
20996 SUBMENU non-zero means this menu item drops down a submenu, which
20997 should be indicated by displaying a proper visual cue after the
20998 item text. */
21000 void
21001 display_tty_menu_item (const char *item_text, int width, int face_id,
21002 int x, int y, int submenu)
21004 struct it it;
21005 struct frame *f = SELECTED_FRAME ();
21006 struct window *w = XWINDOW (f->selected_window);
21007 int saved_used, saved_truncated, saved_width, saved_reversed;
21008 struct glyph_row *row;
21009 size_t item_len = strlen (item_text);
21011 eassert (FRAME_TERMCAP_P (f));
21013 /* Don't write beyond the matrix's last row. This can happen for
21014 TTY screens that are not high enough to show the entire menu.
21015 (This is actually a bit of defensive programming, as
21016 tty_menu_display already limits the number of menu items to one
21017 less than the number of screen lines.) */
21018 if (y >= f->desired_matrix->nrows)
21019 return;
21021 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21022 it.first_visible_x = 0;
21023 it.last_visible_x = FRAME_COLS (f) - 1;
21024 row = it.glyph_row;
21025 /* Start with the row contents from the current matrix. */
21026 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21027 saved_width = row->full_width_p;
21028 row->full_width_p = 1;
21029 saved_reversed = row->reversed_p;
21030 row->reversed_p = 0;
21031 row->enabled_p = true;
21033 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21034 desired face. */
21035 eassert (x < f->desired_matrix->matrix_w);
21036 it.current_x = it.hpos = x;
21037 it.current_y = it.vpos = y;
21038 saved_used = row->used[TEXT_AREA];
21039 saved_truncated = row->truncated_on_right_p;
21040 row->used[TEXT_AREA] = x;
21041 it.face_id = face_id;
21042 it.line_wrap = TRUNCATE;
21044 /* FIXME: This should be controlled by a user option. See the
21045 comments in redisplay_tool_bar and display_mode_line about this.
21046 Also, if paragraph_embedding could ever be R2L, changes will be
21047 needed to avoid shifting to the right the row characters in
21048 term.c:append_glyph. */
21049 it.paragraph_embedding = L2R;
21051 /* Pad with a space on the left. */
21052 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21053 width--;
21054 /* Display the menu item, pad with spaces to WIDTH. */
21055 if (submenu)
21057 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21058 item_len, 0, FRAME_COLS (f) - 1, -1);
21059 width -= item_len;
21060 /* Indicate with " >" that there's a submenu. */
21061 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21062 FRAME_COLS (f) - 1, -1);
21064 else
21065 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21066 width, 0, FRAME_COLS (f) - 1, -1);
21068 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21069 row->truncated_on_right_p = saved_truncated;
21070 row->hash = row_hash (row);
21071 row->full_width_p = saved_width;
21072 row->reversed_p = saved_reversed;
21075 /***********************************************************************
21076 Mode Line
21077 ***********************************************************************/
21079 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21080 FORCE is non-zero, redisplay mode lines unconditionally.
21081 Otherwise, redisplay only mode lines that are garbaged. Value is
21082 the number of windows whose mode lines were redisplayed. */
21084 static int
21085 redisplay_mode_lines (Lisp_Object window, bool force)
21087 int nwindows = 0;
21089 while (!NILP (window))
21091 struct window *w = XWINDOW (window);
21093 if (WINDOWP (w->contents))
21094 nwindows += redisplay_mode_lines (w->contents, force);
21095 else if (force
21096 || FRAME_GARBAGED_P (XFRAME (w->frame))
21097 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21099 struct text_pos lpoint;
21100 struct buffer *old = current_buffer;
21102 /* Set the window's buffer for the mode line display. */
21103 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21104 set_buffer_internal_1 (XBUFFER (w->contents));
21106 /* Point refers normally to the selected window. For any
21107 other window, set up appropriate value. */
21108 if (!EQ (window, selected_window))
21110 struct text_pos pt;
21112 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21113 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21116 /* Display mode lines. */
21117 clear_glyph_matrix (w->desired_matrix);
21118 if (display_mode_lines (w))
21119 ++nwindows;
21121 /* Restore old settings. */
21122 set_buffer_internal_1 (old);
21123 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21126 window = w->next;
21129 return nwindows;
21133 /* Display the mode and/or header line of window W. Value is the
21134 sum number of mode lines and header lines displayed. */
21136 static int
21137 display_mode_lines (struct window *w)
21139 Lisp_Object old_selected_window = selected_window;
21140 Lisp_Object old_selected_frame = selected_frame;
21141 Lisp_Object new_frame = w->frame;
21142 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21143 int n = 0;
21145 selected_frame = new_frame;
21146 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21147 or window's point, then we'd need select_window_1 here as well. */
21148 XSETWINDOW (selected_window, w);
21149 XFRAME (new_frame)->selected_window = selected_window;
21151 /* These will be set while the mode line specs are processed. */
21152 line_number_displayed = 0;
21153 w->column_number_displayed = -1;
21155 if (WINDOW_WANTS_MODELINE_P (w))
21157 struct window *sel_w = XWINDOW (old_selected_window);
21159 /* Select mode line face based on the real selected window. */
21160 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21161 BVAR (current_buffer, mode_line_format));
21162 ++n;
21165 if (WINDOW_WANTS_HEADER_LINE_P (w))
21167 display_mode_line (w, HEADER_LINE_FACE_ID,
21168 BVAR (current_buffer, header_line_format));
21169 ++n;
21172 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21173 selected_frame = old_selected_frame;
21174 selected_window = old_selected_window;
21175 if (n > 0)
21176 w->must_be_updated_p = true;
21177 return n;
21181 /* Display mode or header line of window W. FACE_ID specifies which
21182 line to display; it is either MODE_LINE_FACE_ID or
21183 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21184 display. Value is the pixel height of the mode/header line
21185 displayed. */
21187 static int
21188 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21190 struct it it;
21191 struct face *face;
21192 ptrdiff_t count = SPECPDL_INDEX ();
21194 init_iterator (&it, w, -1, -1, NULL, face_id);
21195 /* Don't extend on a previously drawn mode-line.
21196 This may happen if called from pos_visible_p. */
21197 it.glyph_row->enabled_p = false;
21198 prepare_desired_row (it.glyph_row);
21200 it.glyph_row->mode_line_p = 1;
21202 /* FIXME: This should be controlled by a user option. But
21203 supporting such an option is not trivial, since the mode line is
21204 made up of many separate strings. */
21205 it.paragraph_embedding = L2R;
21207 record_unwind_protect (unwind_format_mode_line,
21208 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21210 mode_line_target = MODE_LINE_DISPLAY;
21212 /* Temporarily make frame's keyboard the current kboard so that
21213 kboard-local variables in the mode_line_format will get the right
21214 values. */
21215 push_kboard (FRAME_KBOARD (it.f));
21216 record_unwind_save_match_data ();
21217 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21218 pop_kboard ();
21220 unbind_to (count, Qnil);
21222 /* Fill up with spaces. */
21223 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21225 compute_line_metrics (&it);
21226 it.glyph_row->full_width_p = 1;
21227 it.glyph_row->continued_p = 0;
21228 it.glyph_row->truncated_on_left_p = 0;
21229 it.glyph_row->truncated_on_right_p = 0;
21231 /* Make a 3D mode-line have a shadow at its right end. */
21232 face = FACE_FROM_ID (it.f, face_id);
21233 extend_face_to_end_of_line (&it);
21234 if (face->box != FACE_NO_BOX)
21236 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21237 + it.glyph_row->used[TEXT_AREA] - 1);
21238 last->right_box_line_p = 1;
21241 return it.glyph_row->height;
21244 /* Move element ELT in LIST to the front of LIST.
21245 Return the updated list. */
21247 static Lisp_Object
21248 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21250 register Lisp_Object tail, prev;
21251 register Lisp_Object tem;
21253 tail = list;
21254 prev = Qnil;
21255 while (CONSP (tail))
21257 tem = XCAR (tail);
21259 if (EQ (elt, tem))
21261 /* Splice out the link TAIL. */
21262 if (NILP (prev))
21263 list = XCDR (tail);
21264 else
21265 Fsetcdr (prev, XCDR (tail));
21267 /* Now make it the first. */
21268 Fsetcdr (tail, list);
21269 return tail;
21271 else
21272 prev = tail;
21273 tail = XCDR (tail);
21274 QUIT;
21277 /* Not found--return unchanged LIST. */
21278 return list;
21281 /* Contribute ELT to the mode line for window IT->w. How it
21282 translates into text depends on its data type.
21284 IT describes the display environment in which we display, as usual.
21286 DEPTH is the depth in recursion. It is used to prevent
21287 infinite recursion here.
21289 FIELD_WIDTH is the number of characters the display of ELT should
21290 occupy in the mode line, and PRECISION is the maximum number of
21291 characters to display from ELT's representation. See
21292 display_string for details.
21294 Returns the hpos of the end of the text generated by ELT.
21296 PROPS is a property list to add to any string we encounter.
21298 If RISKY is nonzero, remove (disregard) any properties in any string
21299 we encounter, and ignore :eval and :propertize.
21301 The global variable `mode_line_target' determines whether the
21302 output is passed to `store_mode_line_noprop',
21303 `store_mode_line_string', or `display_string'. */
21305 static int
21306 display_mode_element (struct it *it, int depth, int field_width, int precision,
21307 Lisp_Object elt, Lisp_Object props, int risky)
21309 int n = 0, field, prec;
21310 int literal = 0;
21312 tail_recurse:
21313 if (depth > 100)
21314 elt = build_string ("*too-deep*");
21316 depth++;
21318 switch (XTYPE (elt))
21320 case Lisp_String:
21322 /* A string: output it and check for %-constructs within it. */
21323 unsigned char c;
21324 ptrdiff_t offset = 0;
21326 if (SCHARS (elt) > 0
21327 && (!NILP (props) || risky))
21329 Lisp_Object oprops, aelt;
21330 oprops = Ftext_properties_at (make_number (0), elt);
21332 /* If the starting string's properties are not what
21333 we want, translate the string. Also, if the string
21334 is risky, do that anyway. */
21336 if (NILP (Fequal (props, oprops)) || risky)
21338 /* If the starting string has properties,
21339 merge the specified ones onto the existing ones. */
21340 if (! NILP (oprops) && !risky)
21342 Lisp_Object tem;
21344 oprops = Fcopy_sequence (oprops);
21345 tem = props;
21346 while (CONSP (tem))
21348 oprops = Fplist_put (oprops, XCAR (tem),
21349 XCAR (XCDR (tem)));
21350 tem = XCDR (XCDR (tem));
21352 props = oprops;
21355 aelt = Fassoc (elt, mode_line_proptrans_alist);
21356 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21358 /* AELT is what we want. Move it to the front
21359 without consing. */
21360 elt = XCAR (aelt);
21361 mode_line_proptrans_alist
21362 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21364 else
21366 Lisp_Object tem;
21368 /* If AELT has the wrong props, it is useless.
21369 so get rid of it. */
21370 if (! NILP (aelt))
21371 mode_line_proptrans_alist
21372 = Fdelq (aelt, mode_line_proptrans_alist);
21374 elt = Fcopy_sequence (elt);
21375 Fset_text_properties (make_number (0), Flength (elt),
21376 props, elt);
21377 /* Add this item to mode_line_proptrans_alist. */
21378 mode_line_proptrans_alist
21379 = Fcons (Fcons (elt, props),
21380 mode_line_proptrans_alist);
21381 /* Truncate mode_line_proptrans_alist
21382 to at most 50 elements. */
21383 tem = Fnthcdr (make_number (50),
21384 mode_line_proptrans_alist);
21385 if (! NILP (tem))
21386 XSETCDR (tem, Qnil);
21391 offset = 0;
21393 if (literal)
21395 prec = precision - n;
21396 switch (mode_line_target)
21398 case MODE_LINE_NOPROP:
21399 case MODE_LINE_TITLE:
21400 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21401 break;
21402 case MODE_LINE_STRING:
21403 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21404 break;
21405 case MODE_LINE_DISPLAY:
21406 n += display_string (NULL, elt, Qnil, 0, 0, it,
21407 0, prec, 0, STRING_MULTIBYTE (elt));
21408 break;
21411 break;
21414 /* Handle the non-literal case. */
21416 while ((precision <= 0 || n < precision)
21417 && SREF (elt, offset) != 0
21418 && (mode_line_target != MODE_LINE_DISPLAY
21419 || it->current_x < it->last_visible_x))
21421 ptrdiff_t last_offset = offset;
21423 /* Advance to end of string or next format specifier. */
21424 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21427 if (offset - 1 != last_offset)
21429 ptrdiff_t nchars, nbytes;
21431 /* Output to end of string or up to '%'. Field width
21432 is length of string. Don't output more than
21433 PRECISION allows us. */
21434 offset--;
21436 prec = c_string_width (SDATA (elt) + last_offset,
21437 offset - last_offset, precision - n,
21438 &nchars, &nbytes);
21440 switch (mode_line_target)
21442 case MODE_LINE_NOPROP:
21443 case MODE_LINE_TITLE:
21444 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21445 break;
21446 case MODE_LINE_STRING:
21448 ptrdiff_t bytepos = last_offset;
21449 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21450 ptrdiff_t endpos = (precision <= 0
21451 ? string_byte_to_char (elt, offset)
21452 : charpos + nchars);
21454 n += store_mode_line_string (NULL,
21455 Fsubstring (elt, make_number (charpos),
21456 make_number (endpos)),
21457 0, 0, 0, Qnil);
21459 break;
21460 case MODE_LINE_DISPLAY:
21462 ptrdiff_t bytepos = last_offset;
21463 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21465 if (precision <= 0)
21466 nchars = string_byte_to_char (elt, offset) - charpos;
21467 n += display_string (NULL, elt, Qnil, 0, charpos,
21468 it, 0, nchars, 0,
21469 STRING_MULTIBYTE (elt));
21471 break;
21474 else /* c == '%' */
21476 ptrdiff_t percent_position = offset;
21478 /* Get the specified minimum width. Zero means
21479 don't pad. */
21480 field = 0;
21481 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21482 field = field * 10 + c - '0';
21484 /* Don't pad beyond the total padding allowed. */
21485 if (field_width - n > 0 && field > field_width - n)
21486 field = field_width - n;
21488 /* Note that either PRECISION <= 0 or N < PRECISION. */
21489 prec = precision - n;
21491 if (c == 'M')
21492 n += display_mode_element (it, depth, field, prec,
21493 Vglobal_mode_string, props,
21494 risky);
21495 else if (c != 0)
21497 bool multibyte;
21498 ptrdiff_t bytepos, charpos;
21499 const char *spec;
21500 Lisp_Object string;
21502 bytepos = percent_position;
21503 charpos = (STRING_MULTIBYTE (elt)
21504 ? string_byte_to_char (elt, bytepos)
21505 : bytepos);
21506 spec = decode_mode_spec (it->w, c, field, &string);
21507 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21509 switch (mode_line_target)
21511 case MODE_LINE_NOPROP:
21512 case MODE_LINE_TITLE:
21513 n += store_mode_line_noprop (spec, field, prec);
21514 break;
21515 case MODE_LINE_STRING:
21517 Lisp_Object tem = build_string (spec);
21518 props = Ftext_properties_at (make_number (charpos), elt);
21519 /* Should only keep face property in props */
21520 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21522 break;
21523 case MODE_LINE_DISPLAY:
21525 int nglyphs_before, nwritten;
21527 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21528 nwritten = display_string (spec, string, elt,
21529 charpos, 0, it,
21530 field, prec, 0,
21531 multibyte);
21533 /* Assign to the glyphs written above the
21534 string where the `%x' came from, position
21535 of the `%'. */
21536 if (nwritten > 0)
21538 struct glyph *glyph
21539 = (it->glyph_row->glyphs[TEXT_AREA]
21540 + nglyphs_before);
21541 int i;
21543 for (i = 0; i < nwritten; ++i)
21545 glyph[i].object = elt;
21546 glyph[i].charpos = charpos;
21549 n += nwritten;
21552 break;
21555 else /* c == 0 */
21556 break;
21560 break;
21562 case Lisp_Symbol:
21563 /* A symbol: process the value of the symbol recursively
21564 as if it appeared here directly. Avoid error if symbol void.
21565 Special case: if value of symbol is a string, output the string
21566 literally. */
21568 register Lisp_Object tem;
21570 /* If the variable is not marked as risky to set
21571 then its contents are risky to use. */
21572 if (NILP (Fget (elt, Qrisky_local_variable)))
21573 risky = 1;
21575 tem = Fboundp (elt);
21576 if (!NILP (tem))
21578 tem = Fsymbol_value (elt);
21579 /* If value is a string, output that string literally:
21580 don't check for % within it. */
21581 if (STRINGP (tem))
21582 literal = 1;
21584 if (!EQ (tem, elt))
21586 /* Give up right away for nil or t. */
21587 elt = tem;
21588 goto tail_recurse;
21592 break;
21594 case Lisp_Cons:
21596 register Lisp_Object car, tem;
21598 /* A cons cell: five distinct cases.
21599 If first element is :eval or :propertize, do something special.
21600 If first element is a string or a cons, process all the elements
21601 and effectively concatenate them.
21602 If first element is a negative number, truncate displaying cdr to
21603 at most that many characters. If positive, pad (with spaces)
21604 to at least that many characters.
21605 If first element is a symbol, process the cadr or caddr recursively
21606 according to whether the symbol's value is non-nil or nil. */
21607 car = XCAR (elt);
21608 if (EQ (car, QCeval))
21610 /* An element of the form (:eval FORM) means evaluate FORM
21611 and use the result as mode line elements. */
21613 if (risky)
21614 break;
21616 if (CONSP (XCDR (elt)))
21618 Lisp_Object spec;
21619 spec = safe_eval (XCAR (XCDR (elt)));
21620 n += display_mode_element (it, depth, field_width - n,
21621 precision - n, spec, props,
21622 risky);
21625 else if (EQ (car, QCpropertize))
21627 /* An element of the form (:propertize ELT PROPS...)
21628 means display ELT but applying properties PROPS. */
21630 if (risky)
21631 break;
21633 if (CONSP (XCDR (elt)))
21634 n += display_mode_element (it, depth, field_width - n,
21635 precision - n, XCAR (XCDR (elt)),
21636 XCDR (XCDR (elt)), risky);
21638 else if (SYMBOLP (car))
21640 tem = Fboundp (car);
21641 elt = XCDR (elt);
21642 if (!CONSP (elt))
21643 goto invalid;
21644 /* elt is now the cdr, and we know it is a cons cell.
21645 Use its car if CAR has a non-nil value. */
21646 if (!NILP (tem))
21648 tem = Fsymbol_value (car);
21649 if (!NILP (tem))
21651 elt = XCAR (elt);
21652 goto tail_recurse;
21655 /* Symbol's value is nil (or symbol is unbound)
21656 Get the cddr of the original list
21657 and if possible find the caddr and use that. */
21658 elt = XCDR (elt);
21659 if (NILP (elt))
21660 break;
21661 else if (!CONSP (elt))
21662 goto invalid;
21663 elt = XCAR (elt);
21664 goto tail_recurse;
21666 else if (INTEGERP (car))
21668 register int lim = XINT (car);
21669 elt = XCDR (elt);
21670 if (lim < 0)
21672 /* Negative int means reduce maximum width. */
21673 if (precision <= 0)
21674 precision = -lim;
21675 else
21676 precision = min (precision, -lim);
21678 else if (lim > 0)
21680 /* Padding specified. Don't let it be more than
21681 current maximum. */
21682 if (precision > 0)
21683 lim = min (precision, lim);
21685 /* If that's more padding than already wanted, queue it.
21686 But don't reduce padding already specified even if
21687 that is beyond the current truncation point. */
21688 field_width = max (lim, field_width);
21690 goto tail_recurse;
21692 else if (STRINGP (car) || CONSP (car))
21694 Lisp_Object halftail = elt;
21695 int len = 0;
21697 while (CONSP (elt)
21698 && (precision <= 0 || n < precision))
21700 n += display_mode_element (it, depth,
21701 /* Do padding only after the last
21702 element in the list. */
21703 (! CONSP (XCDR (elt))
21704 ? field_width - n
21705 : 0),
21706 precision - n, XCAR (elt),
21707 props, risky);
21708 elt = XCDR (elt);
21709 len++;
21710 if ((len & 1) == 0)
21711 halftail = XCDR (halftail);
21712 /* Check for cycle. */
21713 if (EQ (halftail, elt))
21714 break;
21718 break;
21720 default:
21721 invalid:
21722 elt = build_string ("*invalid*");
21723 goto tail_recurse;
21726 /* Pad to FIELD_WIDTH. */
21727 if (field_width > 0 && n < field_width)
21729 switch (mode_line_target)
21731 case MODE_LINE_NOPROP:
21732 case MODE_LINE_TITLE:
21733 n += store_mode_line_noprop ("", field_width - n, 0);
21734 break;
21735 case MODE_LINE_STRING:
21736 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21737 break;
21738 case MODE_LINE_DISPLAY:
21739 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21740 0, 0, 0);
21741 break;
21745 return n;
21748 /* Store a mode-line string element in mode_line_string_list.
21750 If STRING is non-null, display that C string. Otherwise, the Lisp
21751 string LISP_STRING is displayed.
21753 FIELD_WIDTH is the minimum number of output glyphs to produce.
21754 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21755 with spaces. FIELD_WIDTH <= 0 means don't pad.
21757 PRECISION is the maximum number of characters to output from
21758 STRING. PRECISION <= 0 means don't truncate the string.
21760 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21761 properties to the string.
21763 PROPS are the properties to add to the string.
21764 The mode_line_string_face face property is always added to the string.
21767 static int
21768 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21769 int field_width, int precision, Lisp_Object props)
21771 ptrdiff_t len;
21772 int n = 0;
21774 if (string != NULL)
21776 len = strlen (string);
21777 if (precision > 0 && len > precision)
21778 len = precision;
21779 lisp_string = make_string (string, len);
21780 if (NILP (props))
21781 props = mode_line_string_face_prop;
21782 else if (!NILP (mode_line_string_face))
21784 Lisp_Object face = Fplist_get (props, Qface);
21785 props = Fcopy_sequence (props);
21786 if (NILP (face))
21787 face = mode_line_string_face;
21788 else
21789 face = list2 (face, mode_line_string_face);
21790 props = Fplist_put (props, Qface, face);
21792 Fadd_text_properties (make_number (0), make_number (len),
21793 props, lisp_string);
21795 else
21797 len = XFASTINT (Flength (lisp_string));
21798 if (precision > 0 && len > precision)
21800 len = precision;
21801 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21802 precision = -1;
21804 if (!NILP (mode_line_string_face))
21806 Lisp_Object face;
21807 if (NILP (props))
21808 props = Ftext_properties_at (make_number (0), lisp_string);
21809 face = Fplist_get (props, Qface);
21810 if (NILP (face))
21811 face = mode_line_string_face;
21812 else
21813 face = list2 (face, mode_line_string_face);
21814 props = list2 (Qface, face);
21815 if (copy_string)
21816 lisp_string = Fcopy_sequence (lisp_string);
21818 if (!NILP (props))
21819 Fadd_text_properties (make_number (0), make_number (len),
21820 props, lisp_string);
21823 if (len > 0)
21825 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21826 n += len;
21829 if (field_width > len)
21831 field_width -= len;
21832 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21833 if (!NILP (props))
21834 Fadd_text_properties (make_number (0), make_number (field_width),
21835 props, lisp_string);
21836 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21837 n += field_width;
21840 return n;
21844 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21845 1, 4, 0,
21846 doc: /* Format a string out of a mode line format specification.
21847 First arg FORMAT specifies the mode line format (see `mode-line-format'
21848 for details) to use.
21850 By default, the format is evaluated for the currently selected window.
21852 Optional second arg FACE specifies the face property to put on all
21853 characters for which no face is specified. The value nil means the
21854 default face. The value t means whatever face the window's mode line
21855 currently uses (either `mode-line' or `mode-line-inactive',
21856 depending on whether the window is the selected window or not).
21857 An integer value means the value string has no text
21858 properties.
21860 Optional third and fourth args WINDOW and BUFFER specify the window
21861 and buffer to use as the context for the formatting (defaults
21862 are the selected window and the WINDOW's buffer). */)
21863 (Lisp_Object format, Lisp_Object face,
21864 Lisp_Object window, Lisp_Object buffer)
21866 struct it it;
21867 int len;
21868 struct window *w;
21869 struct buffer *old_buffer = NULL;
21870 int face_id;
21871 int no_props = INTEGERP (face);
21872 ptrdiff_t count = SPECPDL_INDEX ();
21873 Lisp_Object str;
21874 int string_start = 0;
21876 w = decode_any_window (window);
21877 XSETWINDOW (window, w);
21879 if (NILP (buffer))
21880 buffer = w->contents;
21881 CHECK_BUFFER (buffer);
21883 /* Make formatting the modeline a non-op when noninteractive, otherwise
21884 there will be problems later caused by a partially initialized frame. */
21885 if (NILP (format) || noninteractive)
21886 return empty_unibyte_string;
21888 if (no_props)
21889 face = Qnil;
21891 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21892 : EQ (face, Qt) ? (EQ (window, selected_window)
21893 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21894 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21895 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21896 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21897 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21898 : DEFAULT_FACE_ID;
21900 old_buffer = current_buffer;
21902 /* Save things including mode_line_proptrans_alist,
21903 and set that to nil so that we don't alter the outer value. */
21904 record_unwind_protect (unwind_format_mode_line,
21905 format_mode_line_unwind_data
21906 (XFRAME (WINDOW_FRAME (w)),
21907 old_buffer, selected_window, 1));
21908 mode_line_proptrans_alist = Qnil;
21910 Fselect_window (window, Qt);
21911 set_buffer_internal_1 (XBUFFER (buffer));
21913 init_iterator (&it, w, -1, -1, NULL, face_id);
21915 if (no_props)
21917 mode_line_target = MODE_LINE_NOPROP;
21918 mode_line_string_face_prop = Qnil;
21919 mode_line_string_list = Qnil;
21920 string_start = MODE_LINE_NOPROP_LEN (0);
21922 else
21924 mode_line_target = MODE_LINE_STRING;
21925 mode_line_string_list = Qnil;
21926 mode_line_string_face = face;
21927 mode_line_string_face_prop
21928 = NILP (face) ? Qnil : list2 (Qface, face);
21931 push_kboard (FRAME_KBOARD (it.f));
21932 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21933 pop_kboard ();
21935 if (no_props)
21937 len = MODE_LINE_NOPROP_LEN (string_start);
21938 str = make_string (mode_line_noprop_buf + string_start, len);
21940 else
21942 mode_line_string_list = Fnreverse (mode_line_string_list);
21943 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21944 empty_unibyte_string);
21947 unbind_to (count, Qnil);
21948 return str;
21951 /* Write a null-terminated, right justified decimal representation of
21952 the positive integer D to BUF using a minimal field width WIDTH. */
21954 static void
21955 pint2str (register char *buf, register int width, register ptrdiff_t d)
21957 register char *p = buf;
21959 if (d <= 0)
21960 *p++ = '0';
21961 else
21963 while (d > 0)
21965 *p++ = d % 10 + '0';
21966 d /= 10;
21970 for (width -= (int) (p - buf); width > 0; --width)
21971 *p++ = ' ';
21972 *p-- = '\0';
21973 while (p > buf)
21975 d = *buf;
21976 *buf++ = *p;
21977 *p-- = d;
21981 /* Write a null-terminated, right justified decimal and "human
21982 readable" representation of the nonnegative integer D to BUF using
21983 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21985 static const char power_letter[] =
21987 0, /* no letter */
21988 'k', /* kilo */
21989 'M', /* mega */
21990 'G', /* giga */
21991 'T', /* tera */
21992 'P', /* peta */
21993 'E', /* exa */
21994 'Z', /* zetta */
21995 'Y' /* yotta */
21998 static void
21999 pint2hrstr (char *buf, int width, ptrdiff_t d)
22001 /* We aim to represent the nonnegative integer D as
22002 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22003 ptrdiff_t quotient = d;
22004 int remainder = 0;
22005 /* -1 means: do not use TENTHS. */
22006 int tenths = -1;
22007 int exponent = 0;
22009 /* Length of QUOTIENT.TENTHS as a string. */
22010 int length;
22012 char * psuffix;
22013 char * p;
22015 if (quotient >= 1000)
22017 /* Scale to the appropriate EXPONENT. */
22020 remainder = quotient % 1000;
22021 quotient /= 1000;
22022 exponent++;
22024 while (quotient >= 1000);
22026 /* Round to nearest and decide whether to use TENTHS or not. */
22027 if (quotient <= 9)
22029 tenths = remainder / 100;
22030 if (remainder % 100 >= 50)
22032 if (tenths < 9)
22033 tenths++;
22034 else
22036 quotient++;
22037 if (quotient == 10)
22038 tenths = -1;
22039 else
22040 tenths = 0;
22044 else
22045 if (remainder >= 500)
22047 if (quotient < 999)
22048 quotient++;
22049 else
22051 quotient = 1;
22052 exponent++;
22053 tenths = 0;
22058 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22059 if (tenths == -1 && quotient <= 99)
22060 if (quotient <= 9)
22061 length = 1;
22062 else
22063 length = 2;
22064 else
22065 length = 3;
22066 p = psuffix = buf + max (width, length);
22068 /* Print EXPONENT. */
22069 *psuffix++ = power_letter[exponent];
22070 *psuffix = '\0';
22072 /* Print TENTHS. */
22073 if (tenths >= 0)
22075 *--p = '0' + tenths;
22076 *--p = '.';
22079 /* Print QUOTIENT. */
22082 int digit = quotient % 10;
22083 *--p = '0' + digit;
22085 while ((quotient /= 10) != 0);
22087 /* Print leading spaces. */
22088 while (buf < p)
22089 *--p = ' ';
22092 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22093 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22094 type of CODING_SYSTEM. Return updated pointer into BUF. */
22096 static unsigned char invalid_eol_type[] = "(*invalid*)";
22098 static char *
22099 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22101 Lisp_Object val;
22102 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22103 const unsigned char *eol_str;
22104 int eol_str_len;
22105 /* The EOL conversion we are using. */
22106 Lisp_Object eoltype;
22108 val = CODING_SYSTEM_SPEC (coding_system);
22109 eoltype = Qnil;
22111 if (!VECTORP (val)) /* Not yet decided. */
22113 *buf++ = multibyte ? '-' : ' ';
22114 if (eol_flag)
22115 eoltype = eol_mnemonic_undecided;
22116 /* Don't mention EOL conversion if it isn't decided. */
22118 else
22120 Lisp_Object attrs;
22121 Lisp_Object eolvalue;
22123 attrs = AREF (val, 0);
22124 eolvalue = AREF (val, 2);
22126 *buf++ = multibyte
22127 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22128 : ' ';
22130 if (eol_flag)
22132 /* The EOL conversion that is normal on this system. */
22134 if (NILP (eolvalue)) /* Not yet decided. */
22135 eoltype = eol_mnemonic_undecided;
22136 else if (VECTORP (eolvalue)) /* Not yet decided. */
22137 eoltype = eol_mnemonic_undecided;
22138 else /* eolvalue is Qunix, Qdos, or Qmac. */
22139 eoltype = (EQ (eolvalue, Qunix)
22140 ? eol_mnemonic_unix
22141 : (EQ (eolvalue, Qdos) == 1
22142 ? eol_mnemonic_dos : eol_mnemonic_mac));
22146 if (eol_flag)
22148 /* Mention the EOL conversion if it is not the usual one. */
22149 if (STRINGP (eoltype))
22151 eol_str = SDATA (eoltype);
22152 eol_str_len = SBYTES (eoltype);
22154 else if (CHARACTERP (eoltype))
22156 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22157 int c = XFASTINT (eoltype);
22158 eol_str_len = CHAR_STRING (c, tmp);
22159 eol_str = tmp;
22161 else
22163 eol_str = invalid_eol_type;
22164 eol_str_len = sizeof (invalid_eol_type) - 1;
22166 memcpy (buf, eol_str, eol_str_len);
22167 buf += eol_str_len;
22170 return buf;
22173 /* Return a string for the output of a mode line %-spec for window W,
22174 generated by character C. FIELD_WIDTH > 0 means pad the string
22175 returned with spaces to that value. Return a Lisp string in
22176 *STRING if the resulting string is taken from that Lisp string.
22178 Note we operate on the current buffer for most purposes. */
22180 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22182 static const char *
22183 decode_mode_spec (struct window *w, register int c, int field_width,
22184 Lisp_Object *string)
22186 Lisp_Object obj;
22187 struct frame *f = XFRAME (WINDOW_FRAME (w));
22188 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22189 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22190 produce strings from numerical values, so limit preposterously
22191 large values of FIELD_WIDTH to avoid overrunning the buffer's
22192 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22193 bytes plus the terminating null. */
22194 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22195 struct buffer *b = current_buffer;
22197 obj = Qnil;
22198 *string = Qnil;
22200 switch (c)
22202 case '*':
22203 if (!NILP (BVAR (b, read_only)))
22204 return "%";
22205 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22206 return "*";
22207 return "-";
22209 case '+':
22210 /* This differs from %* only for a modified read-only buffer. */
22211 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22212 return "*";
22213 if (!NILP (BVAR (b, read_only)))
22214 return "%";
22215 return "-";
22217 case '&':
22218 /* This differs from %* in ignoring read-only-ness. */
22219 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22220 return "*";
22221 return "-";
22223 case '%':
22224 return "%";
22226 case '[':
22228 int i;
22229 char *p;
22231 if (command_loop_level > 5)
22232 return "[[[... ";
22233 p = decode_mode_spec_buf;
22234 for (i = 0; i < command_loop_level; i++)
22235 *p++ = '[';
22236 *p = 0;
22237 return decode_mode_spec_buf;
22240 case ']':
22242 int i;
22243 char *p;
22245 if (command_loop_level > 5)
22246 return " ...]]]";
22247 p = decode_mode_spec_buf;
22248 for (i = 0; i < command_loop_level; i++)
22249 *p++ = ']';
22250 *p = 0;
22251 return decode_mode_spec_buf;
22254 case '-':
22256 register int i;
22258 /* Let lots_of_dashes be a string of infinite length. */
22259 if (mode_line_target == MODE_LINE_NOPROP
22260 || mode_line_target == MODE_LINE_STRING)
22261 return "--";
22262 if (field_width <= 0
22263 || field_width > sizeof (lots_of_dashes))
22265 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22266 decode_mode_spec_buf[i] = '-';
22267 decode_mode_spec_buf[i] = '\0';
22268 return decode_mode_spec_buf;
22270 else
22271 return lots_of_dashes;
22274 case 'b':
22275 obj = BVAR (b, name);
22276 break;
22278 case 'c':
22279 /* %c and %l are ignored in `frame-title-format'.
22280 (In redisplay_internal, the frame title is drawn _before_ the
22281 windows are updated, so the stuff which depends on actual
22282 window contents (such as %l) may fail to render properly, or
22283 even crash emacs.) */
22284 if (mode_line_target == MODE_LINE_TITLE)
22285 return "";
22286 else
22288 ptrdiff_t col = current_column ();
22289 w->column_number_displayed = col;
22290 pint2str (decode_mode_spec_buf, width, col);
22291 return decode_mode_spec_buf;
22294 case 'e':
22295 #ifndef SYSTEM_MALLOC
22297 if (NILP (Vmemory_full))
22298 return "";
22299 else
22300 return "!MEM FULL! ";
22302 #else
22303 return "";
22304 #endif
22306 case 'F':
22307 /* %F displays the frame name. */
22308 if (!NILP (f->title))
22309 return SSDATA (f->title);
22310 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22311 return SSDATA (f->name);
22312 return "Emacs";
22314 case 'f':
22315 obj = BVAR (b, filename);
22316 break;
22318 case 'i':
22320 ptrdiff_t size = ZV - BEGV;
22321 pint2str (decode_mode_spec_buf, width, size);
22322 return decode_mode_spec_buf;
22325 case 'I':
22327 ptrdiff_t size = ZV - BEGV;
22328 pint2hrstr (decode_mode_spec_buf, width, size);
22329 return decode_mode_spec_buf;
22332 case 'l':
22334 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22335 ptrdiff_t topline, nlines, height;
22336 ptrdiff_t junk;
22338 /* %c and %l are ignored in `frame-title-format'. */
22339 if (mode_line_target == MODE_LINE_TITLE)
22340 return "";
22342 startpos = marker_position (w->start);
22343 startpos_byte = marker_byte_position (w->start);
22344 height = WINDOW_TOTAL_LINES (w);
22346 /* If we decided that this buffer isn't suitable for line numbers,
22347 don't forget that too fast. */
22348 if (w->base_line_pos == -1)
22349 goto no_value;
22351 /* If the buffer is very big, don't waste time. */
22352 if (INTEGERP (Vline_number_display_limit)
22353 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22355 w->base_line_pos = 0;
22356 w->base_line_number = 0;
22357 goto no_value;
22360 if (w->base_line_number > 0
22361 && w->base_line_pos > 0
22362 && w->base_line_pos <= startpos)
22364 line = w->base_line_number;
22365 linepos = w->base_line_pos;
22366 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22368 else
22370 line = 1;
22371 linepos = BUF_BEGV (b);
22372 linepos_byte = BUF_BEGV_BYTE (b);
22375 /* Count lines from base line to window start position. */
22376 nlines = display_count_lines (linepos_byte,
22377 startpos_byte,
22378 startpos, &junk);
22380 topline = nlines + line;
22382 /* Determine a new base line, if the old one is too close
22383 or too far away, or if we did not have one.
22384 "Too close" means it's plausible a scroll-down would
22385 go back past it. */
22386 if (startpos == BUF_BEGV (b))
22388 w->base_line_number = topline;
22389 w->base_line_pos = BUF_BEGV (b);
22391 else if (nlines < height + 25 || nlines > height * 3 + 50
22392 || linepos == BUF_BEGV (b))
22394 ptrdiff_t limit = BUF_BEGV (b);
22395 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22396 ptrdiff_t position;
22397 ptrdiff_t distance =
22398 (height * 2 + 30) * line_number_display_limit_width;
22400 if (startpos - distance > limit)
22402 limit = startpos - distance;
22403 limit_byte = CHAR_TO_BYTE (limit);
22406 nlines = display_count_lines (startpos_byte,
22407 limit_byte,
22408 - (height * 2 + 30),
22409 &position);
22410 /* If we couldn't find the lines we wanted within
22411 line_number_display_limit_width chars per line,
22412 give up on line numbers for this window. */
22413 if (position == limit_byte && limit == startpos - distance)
22415 w->base_line_pos = -1;
22416 w->base_line_number = 0;
22417 goto no_value;
22420 w->base_line_number = topline - nlines;
22421 w->base_line_pos = BYTE_TO_CHAR (position);
22424 /* Now count lines from the start pos to point. */
22425 nlines = display_count_lines (startpos_byte,
22426 PT_BYTE, PT, &junk);
22428 /* Record that we did display the line number. */
22429 line_number_displayed = 1;
22431 /* Make the string to show. */
22432 pint2str (decode_mode_spec_buf, width, topline + nlines);
22433 return decode_mode_spec_buf;
22434 no_value:
22436 char* p = decode_mode_spec_buf;
22437 int pad = width - 2;
22438 while (pad-- > 0)
22439 *p++ = ' ';
22440 *p++ = '?';
22441 *p++ = '?';
22442 *p = '\0';
22443 return decode_mode_spec_buf;
22446 break;
22448 case 'm':
22449 obj = BVAR (b, mode_name);
22450 break;
22452 case 'n':
22453 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22454 return " Narrow";
22455 break;
22457 case 'p':
22459 ptrdiff_t pos = marker_position (w->start);
22460 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22462 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22464 if (pos <= BUF_BEGV (b))
22465 return "All";
22466 else
22467 return "Bottom";
22469 else if (pos <= BUF_BEGV (b))
22470 return "Top";
22471 else
22473 if (total > 1000000)
22474 /* Do it differently for a large value, to avoid overflow. */
22475 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22476 else
22477 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22478 /* We can't normally display a 3-digit number,
22479 so get us a 2-digit number that is close. */
22480 if (total == 100)
22481 total = 99;
22482 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22483 return decode_mode_spec_buf;
22487 /* Display percentage of size above the bottom of the screen. */
22488 case 'P':
22490 ptrdiff_t toppos = marker_position (w->start);
22491 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22492 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22494 if (botpos >= BUF_ZV (b))
22496 if (toppos <= BUF_BEGV (b))
22497 return "All";
22498 else
22499 return "Bottom";
22501 else
22503 if (total > 1000000)
22504 /* Do it differently for a large value, to avoid overflow. */
22505 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22506 else
22507 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22508 /* We can't normally display a 3-digit number,
22509 so get us a 2-digit number that is close. */
22510 if (total == 100)
22511 total = 99;
22512 if (toppos <= BUF_BEGV (b))
22513 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22514 else
22515 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22516 return decode_mode_spec_buf;
22520 case 's':
22521 /* status of process */
22522 obj = Fget_buffer_process (Fcurrent_buffer ());
22523 if (NILP (obj))
22524 return "no process";
22525 #ifndef MSDOS
22526 obj = Fsymbol_name (Fprocess_status (obj));
22527 #endif
22528 break;
22530 case '@':
22532 ptrdiff_t count = inhibit_garbage_collection ();
22533 Lisp_Object val = call1 (intern ("file-remote-p"),
22534 BVAR (current_buffer, directory));
22535 unbind_to (count, Qnil);
22537 if (NILP (val))
22538 return "-";
22539 else
22540 return "@";
22543 case 'z':
22544 /* coding-system (not including end-of-line format) */
22545 case 'Z':
22546 /* coding-system (including end-of-line type) */
22548 int eol_flag = (c == 'Z');
22549 char *p = decode_mode_spec_buf;
22551 if (! FRAME_WINDOW_P (f))
22553 /* No need to mention EOL here--the terminal never needs
22554 to do EOL conversion. */
22555 p = decode_mode_spec_coding (CODING_ID_NAME
22556 (FRAME_KEYBOARD_CODING (f)->id),
22557 p, 0);
22558 p = decode_mode_spec_coding (CODING_ID_NAME
22559 (FRAME_TERMINAL_CODING (f)->id),
22560 p, 0);
22562 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22563 p, eol_flag);
22565 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22566 #ifdef subprocesses
22567 obj = Fget_buffer_process (Fcurrent_buffer ());
22568 if (PROCESSP (obj))
22570 p = decode_mode_spec_coding
22571 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22572 p = decode_mode_spec_coding
22573 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22575 #endif /* subprocesses */
22576 #endif /* 0 */
22577 *p = 0;
22578 return decode_mode_spec_buf;
22582 if (STRINGP (obj))
22584 *string = obj;
22585 return SSDATA (obj);
22587 else
22588 return "";
22592 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22593 means count lines back from START_BYTE. But don't go beyond
22594 LIMIT_BYTE. Return the number of lines thus found (always
22595 nonnegative).
22597 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22598 either the position COUNT lines after/before START_BYTE, if we
22599 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22600 COUNT lines. */
22602 static ptrdiff_t
22603 display_count_lines (ptrdiff_t start_byte,
22604 ptrdiff_t limit_byte, ptrdiff_t count,
22605 ptrdiff_t *byte_pos_ptr)
22607 register unsigned char *cursor;
22608 unsigned char *base;
22610 register ptrdiff_t ceiling;
22611 register unsigned char *ceiling_addr;
22612 ptrdiff_t orig_count = count;
22614 /* If we are not in selective display mode,
22615 check only for newlines. */
22616 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22617 && !INTEGERP (BVAR (current_buffer, selective_display)));
22619 if (count > 0)
22621 while (start_byte < limit_byte)
22623 ceiling = BUFFER_CEILING_OF (start_byte);
22624 ceiling = min (limit_byte - 1, ceiling);
22625 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22626 base = (cursor = BYTE_POS_ADDR (start_byte));
22630 if (selective_display)
22632 while (*cursor != '\n' && *cursor != 015
22633 && ++cursor != ceiling_addr)
22634 continue;
22635 if (cursor == ceiling_addr)
22636 break;
22638 else
22640 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22641 if (! cursor)
22642 break;
22645 cursor++;
22647 if (--count == 0)
22649 start_byte += cursor - base;
22650 *byte_pos_ptr = start_byte;
22651 return orig_count;
22654 while (cursor < ceiling_addr);
22656 start_byte += ceiling_addr - base;
22659 else
22661 while (start_byte > limit_byte)
22663 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22664 ceiling = max (limit_byte, ceiling);
22665 ceiling_addr = BYTE_POS_ADDR (ceiling);
22666 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22667 while (1)
22669 if (selective_display)
22671 while (--cursor >= ceiling_addr
22672 && *cursor != '\n' && *cursor != 015)
22673 continue;
22674 if (cursor < ceiling_addr)
22675 break;
22677 else
22679 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22680 if (! cursor)
22681 break;
22684 if (++count == 0)
22686 start_byte += cursor - base + 1;
22687 *byte_pos_ptr = start_byte;
22688 /* When scanning backwards, we should
22689 not count the newline posterior to which we stop. */
22690 return - orig_count - 1;
22693 start_byte += ceiling_addr - base;
22697 *byte_pos_ptr = limit_byte;
22699 if (count < 0)
22700 return - orig_count + count;
22701 return orig_count - count;
22707 /***********************************************************************
22708 Displaying strings
22709 ***********************************************************************/
22711 /* Display a NUL-terminated string, starting with index START.
22713 If STRING is non-null, display that C string. Otherwise, the Lisp
22714 string LISP_STRING is displayed. There's a case that STRING is
22715 non-null and LISP_STRING is not nil. It means STRING is a string
22716 data of LISP_STRING. In that case, we display LISP_STRING while
22717 ignoring its text properties.
22719 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22720 FACE_STRING. Display STRING or LISP_STRING with the face at
22721 FACE_STRING_POS in FACE_STRING:
22723 Display the string in the environment given by IT, but use the
22724 standard display table, temporarily.
22726 FIELD_WIDTH is the minimum number of output glyphs to produce.
22727 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22728 with spaces. If STRING has more characters, more than FIELD_WIDTH
22729 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22731 PRECISION is the maximum number of characters to output from
22732 STRING. PRECISION < 0 means don't truncate the string.
22734 This is roughly equivalent to printf format specifiers:
22736 FIELD_WIDTH PRECISION PRINTF
22737 ----------------------------------------
22738 -1 -1 %s
22739 -1 10 %.10s
22740 10 -1 %10s
22741 20 10 %20.10s
22743 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22744 display them, and < 0 means obey the current buffer's value of
22745 enable_multibyte_characters.
22747 Value is the number of columns displayed. */
22749 static int
22750 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22751 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22752 int field_width, int precision, int max_x, int multibyte)
22754 int hpos_at_start = it->hpos;
22755 int saved_face_id = it->face_id;
22756 struct glyph_row *row = it->glyph_row;
22757 ptrdiff_t it_charpos;
22759 /* Initialize the iterator IT for iteration over STRING beginning
22760 with index START. */
22761 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22762 precision, field_width, multibyte);
22763 if (string && STRINGP (lisp_string))
22764 /* LISP_STRING is the one returned by decode_mode_spec. We should
22765 ignore its text properties. */
22766 it->stop_charpos = it->end_charpos;
22768 /* If displaying STRING, set up the face of the iterator from
22769 FACE_STRING, if that's given. */
22770 if (STRINGP (face_string))
22772 ptrdiff_t endptr;
22773 struct face *face;
22775 it->face_id
22776 = face_at_string_position (it->w, face_string, face_string_pos,
22777 0, &endptr, it->base_face_id, 0);
22778 face = FACE_FROM_ID (it->f, it->face_id);
22779 it->face_box_p = face->box != FACE_NO_BOX;
22782 /* Set max_x to the maximum allowed X position. Don't let it go
22783 beyond the right edge of the window. */
22784 if (max_x <= 0)
22785 max_x = it->last_visible_x;
22786 else
22787 max_x = min (max_x, it->last_visible_x);
22789 /* Skip over display elements that are not visible. because IT->w is
22790 hscrolled. */
22791 if (it->current_x < it->first_visible_x)
22792 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22793 MOVE_TO_POS | MOVE_TO_X);
22795 row->ascent = it->max_ascent;
22796 row->height = it->max_ascent + it->max_descent;
22797 row->phys_ascent = it->max_phys_ascent;
22798 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22799 row->extra_line_spacing = it->max_extra_line_spacing;
22801 if (STRINGP (it->string))
22802 it_charpos = IT_STRING_CHARPOS (*it);
22803 else
22804 it_charpos = IT_CHARPOS (*it);
22806 /* This condition is for the case that we are called with current_x
22807 past last_visible_x. */
22808 while (it->current_x < max_x)
22810 int x_before, x, n_glyphs_before, i, nglyphs;
22812 /* Get the next display element. */
22813 if (!get_next_display_element (it))
22814 break;
22816 /* Produce glyphs. */
22817 x_before = it->current_x;
22818 n_glyphs_before = row->used[TEXT_AREA];
22819 PRODUCE_GLYPHS (it);
22821 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22822 i = 0;
22823 x = x_before;
22824 while (i < nglyphs)
22826 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22828 if (it->line_wrap != TRUNCATE
22829 && x + glyph->pixel_width > max_x)
22831 /* End of continued line or max_x reached. */
22832 if (CHAR_GLYPH_PADDING_P (*glyph))
22834 /* A wide character is unbreakable. */
22835 if (row->reversed_p)
22836 unproduce_glyphs (it, row->used[TEXT_AREA]
22837 - n_glyphs_before);
22838 row->used[TEXT_AREA] = n_glyphs_before;
22839 it->current_x = x_before;
22841 else
22843 if (row->reversed_p)
22844 unproduce_glyphs (it, row->used[TEXT_AREA]
22845 - (n_glyphs_before + i));
22846 row->used[TEXT_AREA] = n_glyphs_before + i;
22847 it->current_x = x;
22849 break;
22851 else if (x + glyph->pixel_width >= it->first_visible_x)
22853 /* Glyph is at least partially visible. */
22854 ++it->hpos;
22855 if (x < it->first_visible_x)
22856 row->x = x - it->first_visible_x;
22858 else
22860 /* Glyph is off the left margin of the display area.
22861 Should not happen. */
22862 emacs_abort ();
22865 row->ascent = max (row->ascent, it->max_ascent);
22866 row->height = max (row->height, it->max_ascent + it->max_descent);
22867 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22868 row->phys_height = max (row->phys_height,
22869 it->max_phys_ascent + it->max_phys_descent);
22870 row->extra_line_spacing = max (row->extra_line_spacing,
22871 it->max_extra_line_spacing);
22872 x += glyph->pixel_width;
22873 ++i;
22876 /* Stop if max_x reached. */
22877 if (i < nglyphs)
22878 break;
22880 /* Stop at line ends. */
22881 if (ITERATOR_AT_END_OF_LINE_P (it))
22883 it->continuation_lines_width = 0;
22884 break;
22887 set_iterator_to_next (it, 1);
22888 if (STRINGP (it->string))
22889 it_charpos = IT_STRING_CHARPOS (*it);
22890 else
22891 it_charpos = IT_CHARPOS (*it);
22893 /* Stop if truncating at the right edge. */
22894 if (it->line_wrap == TRUNCATE
22895 && it->current_x >= it->last_visible_x)
22897 /* Add truncation mark, but don't do it if the line is
22898 truncated at a padding space. */
22899 if (it_charpos < it->string_nchars)
22901 if (!FRAME_WINDOW_P (it->f))
22903 int ii, n;
22905 if (it->current_x > it->last_visible_x)
22907 if (!row->reversed_p)
22909 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22910 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22911 break;
22913 else
22915 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22916 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22917 break;
22918 unproduce_glyphs (it, ii + 1);
22919 ii = row->used[TEXT_AREA] - (ii + 1);
22921 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22923 row->used[TEXT_AREA] = ii;
22924 produce_special_glyphs (it, IT_TRUNCATION);
22927 produce_special_glyphs (it, IT_TRUNCATION);
22929 row->truncated_on_right_p = 1;
22931 break;
22935 /* Maybe insert a truncation at the left. */
22936 if (it->first_visible_x
22937 && it_charpos > 0)
22939 if (!FRAME_WINDOW_P (it->f)
22940 || (row->reversed_p
22941 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22942 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22943 insert_left_trunc_glyphs (it);
22944 row->truncated_on_left_p = 1;
22947 it->face_id = saved_face_id;
22949 /* Value is number of columns displayed. */
22950 return it->hpos - hpos_at_start;
22955 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22956 appears as an element of LIST or as the car of an element of LIST.
22957 If PROPVAL is a list, compare each element against LIST in that
22958 way, and return 1/2 if any element of PROPVAL is found in LIST.
22959 Otherwise return 0. This function cannot quit.
22960 The return value is 2 if the text is invisible but with an ellipsis
22961 and 1 if it's invisible and without an ellipsis. */
22964 invisible_p (register Lisp_Object propval, Lisp_Object list)
22966 register Lisp_Object tail, proptail;
22968 for (tail = list; CONSP (tail); tail = XCDR (tail))
22970 register Lisp_Object tem;
22971 tem = XCAR (tail);
22972 if (EQ (propval, tem))
22973 return 1;
22974 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22975 return NILP (XCDR (tem)) ? 1 : 2;
22978 if (CONSP (propval))
22980 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22982 Lisp_Object propelt;
22983 propelt = XCAR (proptail);
22984 for (tail = list; CONSP (tail); tail = XCDR (tail))
22986 register Lisp_Object tem;
22987 tem = XCAR (tail);
22988 if (EQ (propelt, tem))
22989 return 1;
22990 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22991 return NILP (XCDR (tem)) ? 1 : 2;
22996 return 0;
22999 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23000 doc: /* Non-nil if the property makes the text invisible.
23001 POS-OR-PROP can be a marker or number, in which case it is taken to be
23002 a position in the current buffer and the value of the `invisible' property
23003 is checked; or it can be some other value, which is then presumed to be the
23004 value of the `invisible' property of the text of interest.
23005 The non-nil value returned can be t for truly invisible text or something
23006 else if the text is replaced by an ellipsis. */)
23007 (Lisp_Object pos_or_prop)
23009 Lisp_Object prop
23010 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23011 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23012 : pos_or_prop);
23013 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23014 return (invis == 0 ? Qnil
23015 : invis == 1 ? Qt
23016 : make_number (invis));
23019 /* Calculate a width or height in pixels from a specification using
23020 the following elements:
23022 SPEC ::=
23023 NUM - a (fractional) multiple of the default font width/height
23024 (NUM) - specifies exactly NUM pixels
23025 UNIT - a fixed number of pixels, see below.
23026 ELEMENT - size of a display element in pixels, see below.
23027 (NUM . SPEC) - equals NUM * SPEC
23028 (+ SPEC SPEC ...) - add pixel values
23029 (- SPEC SPEC ...) - subtract pixel values
23030 (- SPEC) - negate pixel value
23032 NUM ::=
23033 INT or FLOAT - a number constant
23034 SYMBOL - use symbol's (buffer local) variable binding.
23036 UNIT ::=
23037 in - pixels per inch *)
23038 mm - pixels per 1/1000 meter *)
23039 cm - pixels per 1/100 meter *)
23040 width - width of current font in pixels.
23041 height - height of current font in pixels.
23043 *) using the ratio(s) defined in display-pixels-per-inch.
23045 ELEMENT ::=
23047 left-fringe - left fringe width in pixels
23048 right-fringe - right fringe width in pixels
23050 left-margin - left margin width in pixels
23051 right-margin - right margin width in pixels
23053 scroll-bar - scroll-bar area width in pixels
23055 Examples:
23057 Pixels corresponding to 5 inches:
23058 (5 . in)
23060 Total width of non-text areas on left side of window (if scroll-bar is on left):
23061 '(space :width (+ left-fringe left-margin scroll-bar))
23063 Align to first text column (in header line):
23064 '(space :align-to 0)
23066 Align to middle of text area minus half the width of variable `my-image'
23067 containing a loaded image:
23068 '(space :align-to (0.5 . (- text my-image)))
23070 Width of left margin minus width of 1 character in the default font:
23071 '(space :width (- left-margin 1))
23073 Width of left margin minus width of 2 characters in the current font:
23074 '(space :width (- left-margin (2 . width)))
23076 Center 1 character over left-margin (in header line):
23077 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23079 Different ways to express width of left fringe plus left margin minus one pixel:
23080 '(space :width (- (+ left-fringe left-margin) (1)))
23081 '(space :width (+ left-fringe left-margin (- (1))))
23082 '(space :width (+ left-fringe left-margin (-1)))
23086 static int
23087 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23088 struct font *font, int width_p, int *align_to)
23090 double pixels;
23092 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23093 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23095 if (NILP (prop))
23096 return OK_PIXELS (0);
23098 eassert (FRAME_LIVE_P (it->f));
23100 if (SYMBOLP (prop))
23102 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23104 char *unit = SSDATA (SYMBOL_NAME (prop));
23106 if (unit[0] == 'i' && unit[1] == 'n')
23107 pixels = 1.0;
23108 else if (unit[0] == 'm' && unit[1] == 'm')
23109 pixels = 25.4;
23110 else if (unit[0] == 'c' && unit[1] == 'm')
23111 pixels = 2.54;
23112 else
23113 pixels = 0;
23114 if (pixels > 0)
23116 double ppi = (width_p ? FRAME_RES_X (it->f)
23117 : FRAME_RES_Y (it->f));
23119 if (ppi > 0)
23120 return OK_PIXELS (ppi / pixels);
23121 return 0;
23125 #ifdef HAVE_WINDOW_SYSTEM
23126 if (EQ (prop, Qheight))
23127 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23128 if (EQ (prop, Qwidth))
23129 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23130 #else
23131 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23132 return OK_PIXELS (1);
23133 #endif
23135 if (EQ (prop, Qtext))
23136 return OK_PIXELS (width_p
23137 ? window_box_width (it->w, TEXT_AREA)
23138 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23140 if (align_to && *align_to < 0)
23142 *res = 0;
23143 if (EQ (prop, Qleft))
23144 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23145 if (EQ (prop, Qright))
23146 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23147 if (EQ (prop, Qcenter))
23148 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23149 + window_box_width (it->w, TEXT_AREA) / 2);
23150 if (EQ (prop, Qleft_fringe))
23151 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23152 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23153 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23154 if (EQ (prop, Qright_fringe))
23155 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23156 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23157 : window_box_right_offset (it->w, TEXT_AREA));
23158 if (EQ (prop, Qleft_margin))
23159 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23160 if (EQ (prop, Qright_margin))
23161 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23162 if (EQ (prop, Qscroll_bar))
23163 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23165 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23166 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23167 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23168 : 0)));
23170 else
23172 if (EQ (prop, Qleft_fringe))
23173 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23174 if (EQ (prop, Qright_fringe))
23175 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23176 if (EQ (prop, Qleft_margin))
23177 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23178 if (EQ (prop, Qright_margin))
23179 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23180 if (EQ (prop, Qscroll_bar))
23181 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23184 prop = buffer_local_value_1 (prop, it->w->contents);
23185 if (EQ (prop, Qunbound))
23186 prop = Qnil;
23189 if (INTEGERP (prop) || FLOATP (prop))
23191 int base_unit = (width_p
23192 ? FRAME_COLUMN_WIDTH (it->f)
23193 : FRAME_LINE_HEIGHT (it->f));
23194 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23197 if (CONSP (prop))
23199 Lisp_Object car = XCAR (prop);
23200 Lisp_Object cdr = XCDR (prop);
23202 if (SYMBOLP (car))
23204 #ifdef HAVE_WINDOW_SYSTEM
23205 if (FRAME_WINDOW_P (it->f)
23206 && valid_image_p (prop))
23208 ptrdiff_t id = lookup_image (it->f, prop);
23209 struct image *img = IMAGE_FROM_ID (it->f, id);
23211 return OK_PIXELS (width_p ? img->width : img->height);
23213 #endif
23214 if (EQ (car, Qplus) || EQ (car, Qminus))
23216 int first = 1;
23217 double px;
23219 pixels = 0;
23220 while (CONSP (cdr))
23222 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23223 font, width_p, align_to))
23224 return 0;
23225 if (first)
23226 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23227 else
23228 pixels += px;
23229 cdr = XCDR (cdr);
23231 if (EQ (car, Qminus))
23232 pixels = -pixels;
23233 return OK_PIXELS (pixels);
23236 car = buffer_local_value_1 (car, it->w->contents);
23237 if (EQ (car, Qunbound))
23238 car = Qnil;
23241 if (INTEGERP (car) || FLOATP (car))
23243 double fact;
23244 pixels = XFLOATINT (car);
23245 if (NILP (cdr))
23246 return OK_PIXELS (pixels);
23247 if (calc_pixel_width_or_height (&fact, it, cdr,
23248 font, width_p, align_to))
23249 return OK_PIXELS (pixels * fact);
23250 return 0;
23253 return 0;
23256 return 0;
23260 /***********************************************************************
23261 Glyph Display
23262 ***********************************************************************/
23264 #ifdef HAVE_WINDOW_SYSTEM
23266 #ifdef GLYPH_DEBUG
23268 void
23269 dump_glyph_string (struct glyph_string *s)
23271 fprintf (stderr, "glyph string\n");
23272 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23273 s->x, s->y, s->width, s->height);
23274 fprintf (stderr, " ybase = %d\n", s->ybase);
23275 fprintf (stderr, " hl = %d\n", s->hl);
23276 fprintf (stderr, " left overhang = %d, right = %d\n",
23277 s->left_overhang, s->right_overhang);
23278 fprintf (stderr, " nchars = %d\n", s->nchars);
23279 fprintf (stderr, " extends to end of line = %d\n",
23280 s->extends_to_end_of_line_p);
23281 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23282 fprintf (stderr, " bg width = %d\n", s->background_width);
23285 #endif /* GLYPH_DEBUG */
23287 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23288 of XChar2b structures for S; it can't be allocated in
23289 init_glyph_string because it must be allocated via `alloca'. W
23290 is the window on which S is drawn. ROW and AREA are the glyph row
23291 and area within the row from which S is constructed. START is the
23292 index of the first glyph structure covered by S. HL is a
23293 face-override for drawing S. */
23295 #ifdef HAVE_NTGUI
23296 #define OPTIONAL_HDC(hdc) HDC hdc,
23297 #define DECLARE_HDC(hdc) HDC hdc;
23298 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23299 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23300 #endif
23302 #ifndef OPTIONAL_HDC
23303 #define OPTIONAL_HDC(hdc)
23304 #define DECLARE_HDC(hdc)
23305 #define ALLOCATE_HDC(hdc, f)
23306 #define RELEASE_HDC(hdc, f)
23307 #endif
23309 static void
23310 init_glyph_string (struct glyph_string *s,
23311 OPTIONAL_HDC (hdc)
23312 XChar2b *char2b, struct window *w, struct glyph_row *row,
23313 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23315 memset (s, 0, sizeof *s);
23316 s->w = w;
23317 s->f = XFRAME (w->frame);
23318 #ifdef HAVE_NTGUI
23319 s->hdc = hdc;
23320 #endif
23321 s->display = FRAME_X_DISPLAY (s->f);
23322 s->window = FRAME_X_WINDOW (s->f);
23323 s->char2b = char2b;
23324 s->hl = hl;
23325 s->row = row;
23326 s->area = area;
23327 s->first_glyph = row->glyphs[area] + start;
23328 s->height = row->height;
23329 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23330 s->ybase = s->y + row->ascent;
23334 /* Append the list of glyph strings with head H and tail T to the list
23335 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23337 static void
23338 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23339 struct glyph_string *h, struct glyph_string *t)
23341 if (h)
23343 if (*head)
23344 (*tail)->next = h;
23345 else
23346 *head = h;
23347 h->prev = *tail;
23348 *tail = t;
23353 /* Prepend the list of glyph strings with head H and tail T to the
23354 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23355 result. */
23357 static void
23358 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23359 struct glyph_string *h, struct glyph_string *t)
23361 if (h)
23363 if (*head)
23364 (*head)->prev = t;
23365 else
23366 *tail = t;
23367 t->next = *head;
23368 *head = h;
23373 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23374 Set *HEAD and *TAIL to the resulting list. */
23376 static void
23377 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23378 struct glyph_string *s)
23380 s->next = s->prev = NULL;
23381 append_glyph_string_lists (head, tail, s, s);
23385 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23386 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23387 make sure that X resources for the face returned are allocated.
23388 Value is a pointer to a realized face that is ready for display if
23389 DISPLAY_P is non-zero. */
23391 static struct face *
23392 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23393 XChar2b *char2b, int display_p)
23395 struct face *face = FACE_FROM_ID (f, face_id);
23396 unsigned code = 0;
23398 if (face->font)
23400 code = face->font->driver->encode_char (face->font, c);
23402 if (code == FONT_INVALID_CODE)
23403 code = 0;
23405 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23407 /* Make sure X resources of the face are allocated. */
23408 #ifdef HAVE_X_WINDOWS
23409 if (display_p)
23410 #endif
23412 eassert (face != NULL);
23413 PREPARE_FACE_FOR_DISPLAY (f, face);
23416 return face;
23420 /* Get face and two-byte form of character glyph GLYPH on frame F.
23421 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23422 a pointer to a realized face that is ready for display. */
23424 static struct face *
23425 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23426 XChar2b *char2b, int *two_byte_p)
23428 struct face *face;
23429 unsigned code = 0;
23431 eassert (glyph->type == CHAR_GLYPH);
23432 face = FACE_FROM_ID (f, glyph->face_id);
23434 /* Make sure X resources of the face are allocated. */
23435 eassert (face != NULL);
23436 PREPARE_FACE_FOR_DISPLAY (f, face);
23438 if (two_byte_p)
23439 *two_byte_p = 0;
23441 if (face->font)
23443 if (CHAR_BYTE8_P (glyph->u.ch))
23444 code = CHAR_TO_BYTE8 (glyph->u.ch);
23445 else
23446 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23448 if (code == FONT_INVALID_CODE)
23449 code = 0;
23452 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23453 return face;
23457 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23458 Return 1 if FONT has a glyph for C, otherwise return 0. */
23460 static int
23461 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23463 unsigned code;
23465 if (CHAR_BYTE8_P (c))
23466 code = CHAR_TO_BYTE8 (c);
23467 else
23468 code = font->driver->encode_char (font, c);
23470 if (code == FONT_INVALID_CODE)
23471 return 0;
23472 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23473 return 1;
23477 /* Fill glyph string S with composition components specified by S->cmp.
23479 BASE_FACE is the base face of the composition.
23480 S->cmp_from is the index of the first component for S.
23482 OVERLAPS non-zero means S should draw the foreground only, and use
23483 its physical height for clipping. See also draw_glyphs.
23485 Value is the index of a component not in S. */
23487 static int
23488 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23489 int overlaps)
23491 int i;
23492 /* For all glyphs of this composition, starting at the offset
23493 S->cmp_from, until we reach the end of the definition or encounter a
23494 glyph that requires the different face, add it to S. */
23495 struct face *face;
23497 eassert (s);
23499 s->for_overlaps = overlaps;
23500 s->face = NULL;
23501 s->font = NULL;
23502 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23504 int c = COMPOSITION_GLYPH (s->cmp, i);
23506 /* TAB in a composition means display glyphs with padding space
23507 on the left or right. */
23508 if (c != '\t')
23510 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23511 -1, Qnil);
23513 face = get_char_face_and_encoding (s->f, c, face_id,
23514 s->char2b + i, 1);
23515 if (face)
23517 if (! s->face)
23519 s->face = face;
23520 s->font = s->face->font;
23522 else if (s->face != face)
23523 break;
23526 ++s->nchars;
23528 s->cmp_to = i;
23530 if (s->face == NULL)
23532 s->face = base_face->ascii_face;
23533 s->font = s->face->font;
23536 /* All glyph strings for the same composition has the same width,
23537 i.e. the width set for the first component of the composition. */
23538 s->width = s->first_glyph->pixel_width;
23540 /* If the specified font could not be loaded, use the frame's
23541 default font, but record the fact that we couldn't load it in
23542 the glyph string so that we can draw rectangles for the
23543 characters of the glyph string. */
23544 if (s->font == NULL)
23546 s->font_not_found_p = 1;
23547 s->font = FRAME_FONT (s->f);
23550 /* Adjust base line for subscript/superscript text. */
23551 s->ybase += s->first_glyph->voffset;
23553 /* This glyph string must always be drawn with 16-bit functions. */
23554 s->two_byte_p = 1;
23556 return s->cmp_to;
23559 static int
23560 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23561 int start, int end, int overlaps)
23563 struct glyph *glyph, *last;
23564 Lisp_Object lgstring;
23565 int i;
23567 s->for_overlaps = overlaps;
23568 glyph = s->row->glyphs[s->area] + start;
23569 last = s->row->glyphs[s->area] + end;
23570 s->cmp_id = glyph->u.cmp.id;
23571 s->cmp_from = glyph->slice.cmp.from;
23572 s->cmp_to = glyph->slice.cmp.to + 1;
23573 s->face = FACE_FROM_ID (s->f, face_id);
23574 lgstring = composition_gstring_from_id (s->cmp_id);
23575 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23576 glyph++;
23577 while (glyph < last
23578 && glyph->u.cmp.automatic
23579 && glyph->u.cmp.id == s->cmp_id
23580 && s->cmp_to == glyph->slice.cmp.from)
23581 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23583 for (i = s->cmp_from; i < s->cmp_to; i++)
23585 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23586 unsigned code = LGLYPH_CODE (lglyph);
23588 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23590 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23591 return glyph - s->row->glyphs[s->area];
23595 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23596 See the comment of fill_glyph_string for arguments.
23597 Value is the index of the first glyph not in S. */
23600 static int
23601 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23602 int start, int end, int overlaps)
23604 struct glyph *glyph, *last;
23605 int voffset;
23607 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23608 s->for_overlaps = overlaps;
23609 glyph = s->row->glyphs[s->area] + start;
23610 last = s->row->glyphs[s->area] + end;
23611 voffset = glyph->voffset;
23612 s->face = FACE_FROM_ID (s->f, face_id);
23613 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23614 s->nchars = 1;
23615 s->width = glyph->pixel_width;
23616 glyph++;
23617 while (glyph < last
23618 && glyph->type == GLYPHLESS_GLYPH
23619 && glyph->voffset == voffset
23620 && glyph->face_id == face_id)
23622 s->nchars++;
23623 s->width += glyph->pixel_width;
23624 glyph++;
23626 s->ybase += voffset;
23627 return glyph - s->row->glyphs[s->area];
23631 /* Fill glyph string S from a sequence of character glyphs.
23633 FACE_ID is the face id of the string. START is the index of the
23634 first glyph to consider, END is the index of the last + 1.
23635 OVERLAPS non-zero means S should draw the foreground only, and use
23636 its physical height for clipping. See also draw_glyphs.
23638 Value is the index of the first glyph not in S. */
23640 static int
23641 fill_glyph_string (struct glyph_string *s, int face_id,
23642 int start, int end, int overlaps)
23644 struct glyph *glyph, *last;
23645 int voffset;
23646 int glyph_not_available_p;
23648 eassert (s->f == XFRAME (s->w->frame));
23649 eassert (s->nchars == 0);
23650 eassert (start >= 0 && end > start);
23652 s->for_overlaps = overlaps;
23653 glyph = s->row->glyphs[s->area] + start;
23654 last = s->row->glyphs[s->area] + end;
23655 voffset = glyph->voffset;
23656 s->padding_p = glyph->padding_p;
23657 glyph_not_available_p = glyph->glyph_not_available_p;
23659 while (glyph < last
23660 && glyph->type == CHAR_GLYPH
23661 && glyph->voffset == voffset
23662 /* Same face id implies same font, nowadays. */
23663 && glyph->face_id == face_id
23664 && glyph->glyph_not_available_p == glyph_not_available_p)
23666 int two_byte_p;
23668 s->face = get_glyph_face_and_encoding (s->f, glyph,
23669 s->char2b + s->nchars,
23670 &two_byte_p);
23671 s->two_byte_p = two_byte_p;
23672 ++s->nchars;
23673 eassert (s->nchars <= end - start);
23674 s->width += glyph->pixel_width;
23675 if (glyph++->padding_p != s->padding_p)
23676 break;
23679 s->font = s->face->font;
23681 /* If the specified font could not be loaded, use the frame's font,
23682 but record the fact that we couldn't load it in
23683 S->font_not_found_p so that we can draw rectangles for the
23684 characters of the glyph string. */
23685 if (s->font == NULL || glyph_not_available_p)
23687 s->font_not_found_p = 1;
23688 s->font = FRAME_FONT (s->f);
23691 /* Adjust base line for subscript/superscript text. */
23692 s->ybase += voffset;
23694 eassert (s->face && s->face->gc);
23695 return glyph - s->row->glyphs[s->area];
23699 /* Fill glyph string S from image glyph S->first_glyph. */
23701 static void
23702 fill_image_glyph_string (struct glyph_string *s)
23704 eassert (s->first_glyph->type == IMAGE_GLYPH);
23705 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23706 eassert (s->img);
23707 s->slice = s->first_glyph->slice.img;
23708 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23709 s->font = s->face->font;
23710 s->width = s->first_glyph->pixel_width;
23712 /* Adjust base line for subscript/superscript text. */
23713 s->ybase += s->first_glyph->voffset;
23717 /* Fill glyph string S from a sequence of stretch glyphs.
23719 START is the index of the first glyph to consider,
23720 END is the index of the last + 1.
23722 Value is the index of the first glyph not in S. */
23724 static int
23725 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23727 struct glyph *glyph, *last;
23728 int voffset, face_id;
23730 eassert (s->first_glyph->type == STRETCH_GLYPH);
23732 glyph = s->row->glyphs[s->area] + start;
23733 last = s->row->glyphs[s->area] + end;
23734 face_id = glyph->face_id;
23735 s->face = FACE_FROM_ID (s->f, face_id);
23736 s->font = s->face->font;
23737 s->width = glyph->pixel_width;
23738 s->nchars = 1;
23739 voffset = glyph->voffset;
23741 for (++glyph;
23742 (glyph < last
23743 && glyph->type == STRETCH_GLYPH
23744 && glyph->voffset == voffset
23745 && glyph->face_id == face_id);
23746 ++glyph)
23747 s->width += glyph->pixel_width;
23749 /* Adjust base line for subscript/superscript text. */
23750 s->ybase += voffset;
23752 /* The case that face->gc == 0 is handled when drawing the glyph
23753 string by calling PREPARE_FACE_FOR_DISPLAY. */
23754 eassert (s->face);
23755 return glyph - s->row->glyphs[s->area];
23758 static struct font_metrics *
23759 get_per_char_metric (struct font *font, XChar2b *char2b)
23761 static struct font_metrics metrics;
23762 unsigned code;
23764 if (! font)
23765 return NULL;
23766 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23767 if (code == FONT_INVALID_CODE)
23768 return NULL;
23769 font->driver->text_extents (font, &code, 1, &metrics);
23770 return &metrics;
23773 /* EXPORT for RIF:
23774 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23775 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23776 assumed to be zero. */
23778 void
23779 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23781 *left = *right = 0;
23783 if (glyph->type == CHAR_GLYPH)
23785 struct face *face;
23786 XChar2b char2b;
23787 struct font_metrics *pcm;
23789 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23790 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23792 if (pcm->rbearing > pcm->width)
23793 *right = pcm->rbearing - pcm->width;
23794 if (pcm->lbearing < 0)
23795 *left = -pcm->lbearing;
23798 else if (glyph->type == COMPOSITE_GLYPH)
23800 if (! glyph->u.cmp.automatic)
23802 struct composition *cmp = composition_table[glyph->u.cmp.id];
23804 if (cmp->rbearing > cmp->pixel_width)
23805 *right = cmp->rbearing - cmp->pixel_width;
23806 if (cmp->lbearing < 0)
23807 *left = - cmp->lbearing;
23809 else
23811 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23812 struct font_metrics metrics;
23814 composition_gstring_width (gstring, glyph->slice.cmp.from,
23815 glyph->slice.cmp.to + 1, &metrics);
23816 if (metrics.rbearing > metrics.width)
23817 *right = metrics.rbearing - metrics.width;
23818 if (metrics.lbearing < 0)
23819 *left = - metrics.lbearing;
23825 /* Return the index of the first glyph preceding glyph string S that
23826 is overwritten by S because of S's left overhang. Value is -1
23827 if no glyphs are overwritten. */
23829 static int
23830 left_overwritten (struct glyph_string *s)
23832 int k;
23834 if (s->left_overhang)
23836 int x = 0, i;
23837 struct glyph *glyphs = s->row->glyphs[s->area];
23838 int first = s->first_glyph - glyphs;
23840 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23841 x -= glyphs[i].pixel_width;
23843 k = i + 1;
23845 else
23846 k = -1;
23848 return k;
23852 /* Return the index of the first glyph preceding glyph string S that
23853 is overwriting S because of its right overhang. Value is -1 if no
23854 glyph in front of S overwrites S. */
23856 static int
23857 left_overwriting (struct glyph_string *s)
23859 int i, k, x;
23860 struct glyph *glyphs = s->row->glyphs[s->area];
23861 int first = s->first_glyph - glyphs;
23863 k = -1;
23864 x = 0;
23865 for (i = first - 1; i >= 0; --i)
23867 int left, right;
23868 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23869 if (x + right > 0)
23870 k = i;
23871 x -= glyphs[i].pixel_width;
23874 return k;
23878 /* Return the index of the last glyph following glyph string S that is
23879 overwritten by S because of S's right overhang. Value is -1 if
23880 no such glyph is found. */
23882 static int
23883 right_overwritten (struct glyph_string *s)
23885 int k = -1;
23887 if (s->right_overhang)
23889 int x = 0, i;
23890 struct glyph *glyphs = s->row->glyphs[s->area];
23891 int first = (s->first_glyph - glyphs
23892 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23893 int end = s->row->used[s->area];
23895 for (i = first; i < end && s->right_overhang > x; ++i)
23896 x += glyphs[i].pixel_width;
23898 k = i;
23901 return k;
23905 /* Return the index of the last glyph following glyph string S that
23906 overwrites S because of its left overhang. Value is negative
23907 if no such glyph is found. */
23909 static int
23910 right_overwriting (struct glyph_string *s)
23912 int i, k, x;
23913 int end = s->row->used[s->area];
23914 struct glyph *glyphs = s->row->glyphs[s->area];
23915 int first = (s->first_glyph - glyphs
23916 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23918 k = -1;
23919 x = 0;
23920 for (i = first; i < end; ++i)
23922 int left, right;
23923 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23924 if (x - left < 0)
23925 k = i;
23926 x += glyphs[i].pixel_width;
23929 return k;
23933 /* Set background width of glyph string S. START is the index of the
23934 first glyph following S. LAST_X is the right-most x-position + 1
23935 in the drawing area. */
23937 static void
23938 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23940 /* If the face of this glyph string has to be drawn to the end of
23941 the drawing area, set S->extends_to_end_of_line_p. */
23943 if (start == s->row->used[s->area]
23944 && ((s->row->fill_line_p
23945 && (s->hl == DRAW_NORMAL_TEXT
23946 || s->hl == DRAW_IMAGE_RAISED
23947 || s->hl == DRAW_IMAGE_SUNKEN))
23948 || s->hl == DRAW_MOUSE_FACE))
23949 s->extends_to_end_of_line_p = 1;
23951 /* If S extends its face to the end of the line, set its
23952 background_width to the distance to the right edge of the drawing
23953 area. */
23954 if (s->extends_to_end_of_line_p)
23955 s->background_width = last_x - s->x + 1;
23956 else
23957 s->background_width = s->width;
23961 /* Compute overhangs and x-positions for glyph string S and its
23962 predecessors, or successors. X is the starting x-position for S.
23963 BACKWARD_P non-zero means process predecessors. */
23965 static void
23966 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23968 if (backward_p)
23970 while (s)
23972 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23973 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23974 x -= s->width;
23975 s->x = x;
23976 s = s->prev;
23979 else
23981 while (s)
23983 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23984 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23985 s->x = x;
23986 x += s->width;
23987 s = s->next;
23994 /* The following macros are only called from draw_glyphs below.
23995 They reference the following parameters of that function directly:
23996 `w', `row', `area', and `overlap_p'
23997 as well as the following local variables:
23998 `s', `f', and `hdc' (in W32) */
24000 #ifdef HAVE_NTGUI
24001 /* On W32, silently add local `hdc' variable to argument list of
24002 init_glyph_string. */
24003 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24004 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24005 #else
24006 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24007 init_glyph_string (s, char2b, w, row, area, start, hl)
24008 #endif
24010 /* Add a glyph string for a stretch glyph to the list of strings
24011 between HEAD and TAIL. START is the index of the stretch glyph in
24012 row area AREA of glyph row ROW. END is the index of the last glyph
24013 in that glyph row area. X is the current output position assigned
24014 to the new glyph string constructed. HL overrides that face of the
24015 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24016 is the right-most x-position of the drawing area. */
24018 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24019 and below -- keep them on one line. */
24020 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24021 do \
24023 s = alloca (sizeof *s); \
24024 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24025 START = fill_stretch_glyph_string (s, START, END); \
24026 append_glyph_string (&HEAD, &TAIL, s); \
24027 s->x = (X); \
24029 while (0)
24032 /* Add a glyph string for an image glyph to the list of strings
24033 between HEAD and TAIL. START is the index of the image glyph in
24034 row area AREA of glyph row ROW. END is the index of the last glyph
24035 in that glyph row area. X is the current output position assigned
24036 to the new glyph string constructed. HL overrides that face of the
24037 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24038 is the right-most x-position of the drawing area. */
24040 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24041 do \
24043 s = alloca (sizeof *s); \
24044 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24045 fill_image_glyph_string (s); \
24046 append_glyph_string (&HEAD, &TAIL, s); \
24047 ++START; \
24048 s->x = (X); \
24050 while (0)
24053 /* Add a glyph string for a sequence of character glyphs to the list
24054 of strings between HEAD and TAIL. START is the index of the first
24055 glyph in row area AREA of glyph row ROW that is part of the new
24056 glyph string. END is the index of the last glyph in that glyph row
24057 area. X is the current output position assigned to the new glyph
24058 string constructed. HL overrides that face of the glyph; e.g. it
24059 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24060 right-most x-position of the drawing area. */
24062 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24063 do \
24065 int face_id; \
24066 XChar2b *char2b; \
24068 face_id = (row)->glyphs[area][START].face_id; \
24070 s = alloca (sizeof *s); \
24071 char2b = alloca ((END - START) * sizeof *char2b); \
24072 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24073 append_glyph_string (&HEAD, &TAIL, s); \
24074 s->x = (X); \
24075 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24077 while (0)
24080 /* Add a glyph string for a composite sequence to the list of strings
24081 between HEAD and TAIL. START is the index of the first glyph in
24082 row area AREA of glyph row ROW that is part of the new glyph
24083 string. END is the index of the last glyph in that glyph row area.
24084 X is the current output position assigned to the new glyph string
24085 constructed. HL overrides that face of the glyph; e.g. it is
24086 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24087 x-position of the drawing area. */
24089 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24090 do { \
24091 int face_id = (row)->glyphs[area][START].face_id; \
24092 struct face *base_face = FACE_FROM_ID (f, face_id); \
24093 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24094 struct composition *cmp = composition_table[cmp_id]; \
24095 XChar2b *char2b; \
24096 struct glyph_string *first_s = NULL; \
24097 int n; \
24099 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24101 /* Make glyph_strings for each glyph sequence that is drawable by \
24102 the same face, and append them to HEAD/TAIL. */ \
24103 for (n = 0; n < cmp->glyph_len;) \
24105 s = alloca (sizeof *s); \
24106 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24107 append_glyph_string (&(HEAD), &(TAIL), s); \
24108 s->cmp = cmp; \
24109 s->cmp_from = n; \
24110 s->x = (X); \
24111 if (n == 0) \
24112 first_s = s; \
24113 n = fill_composite_glyph_string (s, base_face, overlaps); \
24116 ++START; \
24117 s = first_s; \
24118 } while (0)
24121 /* Add a glyph string for a glyph-string sequence to the list of strings
24122 between HEAD and TAIL. */
24124 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24125 do { \
24126 int face_id; \
24127 XChar2b *char2b; \
24128 Lisp_Object gstring; \
24130 face_id = (row)->glyphs[area][START].face_id; \
24131 gstring = (composition_gstring_from_id \
24132 ((row)->glyphs[area][START].u.cmp.id)); \
24133 s = alloca (sizeof *s); \
24134 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24135 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24136 append_glyph_string (&(HEAD), &(TAIL), s); \
24137 s->x = (X); \
24138 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24139 } while (0)
24142 /* Add a glyph string for a sequence of glyphless character's glyphs
24143 to the list of strings between HEAD and TAIL. The meanings of
24144 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24146 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24147 do \
24149 int face_id; \
24151 face_id = (row)->glyphs[area][START].face_id; \
24153 s = alloca (sizeof *s); \
24154 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24155 append_glyph_string (&HEAD, &TAIL, s); \
24156 s->x = (X); \
24157 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24158 overlaps); \
24160 while (0)
24163 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24164 of AREA of glyph row ROW on window W between indices START and END.
24165 HL overrides the face for drawing glyph strings, e.g. it is
24166 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24167 x-positions of the drawing area.
24169 This is an ugly monster macro construct because we must use alloca
24170 to allocate glyph strings (because draw_glyphs can be called
24171 asynchronously). */
24173 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24174 do \
24176 HEAD = TAIL = NULL; \
24177 while (START < END) \
24179 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24180 switch (first_glyph->type) \
24182 case CHAR_GLYPH: \
24183 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24184 HL, X, LAST_X); \
24185 break; \
24187 case COMPOSITE_GLYPH: \
24188 if (first_glyph->u.cmp.automatic) \
24189 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24190 HL, X, LAST_X); \
24191 else \
24192 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24193 HL, X, LAST_X); \
24194 break; \
24196 case STRETCH_GLYPH: \
24197 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24198 HL, X, LAST_X); \
24199 break; \
24201 case IMAGE_GLYPH: \
24202 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24203 HL, X, LAST_X); \
24204 break; \
24206 case GLYPHLESS_GLYPH: \
24207 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24208 HL, X, LAST_X); \
24209 break; \
24211 default: \
24212 emacs_abort (); \
24215 if (s) \
24217 set_glyph_string_background_width (s, START, LAST_X); \
24218 (X) += s->width; \
24221 } while (0)
24224 /* Draw glyphs between START and END in AREA of ROW on window W,
24225 starting at x-position X. X is relative to AREA in W. HL is a
24226 face-override with the following meaning:
24228 DRAW_NORMAL_TEXT draw normally
24229 DRAW_CURSOR draw in cursor face
24230 DRAW_MOUSE_FACE draw in mouse face.
24231 DRAW_INVERSE_VIDEO draw in mode line face
24232 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24233 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24235 If OVERLAPS is non-zero, draw only the foreground of characters and
24236 clip to the physical height of ROW. Non-zero value also defines
24237 the overlapping part to be drawn:
24239 OVERLAPS_PRED overlap with preceding rows
24240 OVERLAPS_SUCC overlap with succeeding rows
24241 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24242 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24244 Value is the x-position reached, relative to AREA of W. */
24246 static int
24247 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24248 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24249 enum draw_glyphs_face hl, int overlaps)
24251 struct glyph_string *head, *tail;
24252 struct glyph_string *s;
24253 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24254 int i, j, x_reached, last_x, area_left = 0;
24255 struct frame *f = XFRAME (WINDOW_FRAME (w));
24256 DECLARE_HDC (hdc);
24258 ALLOCATE_HDC (hdc, f);
24260 /* Let's rather be paranoid than getting a SEGV. */
24261 end = min (end, row->used[area]);
24262 start = clip_to_bounds (0, start, end);
24264 /* Translate X to frame coordinates. Set last_x to the right
24265 end of the drawing area. */
24266 if (row->full_width_p)
24268 /* X is relative to the left edge of W, without scroll bars
24269 or fringes. */
24270 area_left = WINDOW_LEFT_EDGE_X (w);
24271 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24272 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24274 else
24276 area_left = window_box_left (w, area);
24277 last_x = area_left + window_box_width (w, area);
24279 x += area_left;
24281 /* Build a doubly-linked list of glyph_string structures between
24282 head and tail from what we have to draw. Note that the macro
24283 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24284 the reason we use a separate variable `i'. */
24285 i = start;
24286 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24287 if (tail)
24288 x_reached = tail->x + tail->background_width;
24289 else
24290 x_reached = x;
24292 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24293 the row, redraw some glyphs in front or following the glyph
24294 strings built above. */
24295 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24297 struct glyph_string *h, *t;
24298 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24299 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24300 int check_mouse_face = 0;
24301 int dummy_x = 0;
24303 /* If mouse highlighting is on, we may need to draw adjacent
24304 glyphs using mouse-face highlighting. */
24305 if (area == TEXT_AREA && row->mouse_face_p
24306 && hlinfo->mouse_face_beg_row >= 0
24307 && hlinfo->mouse_face_end_row >= 0)
24309 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24311 if (row_vpos >= hlinfo->mouse_face_beg_row
24312 && row_vpos <= hlinfo->mouse_face_end_row)
24314 check_mouse_face = 1;
24315 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24316 ? hlinfo->mouse_face_beg_col : 0;
24317 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24318 ? hlinfo->mouse_face_end_col
24319 : row->used[TEXT_AREA];
24323 /* Compute overhangs for all glyph strings. */
24324 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24325 for (s = head; s; s = s->next)
24326 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24328 /* Prepend glyph strings for glyphs in front of the first glyph
24329 string that are overwritten because of the first glyph
24330 string's left overhang. The background of all strings
24331 prepended must be drawn because the first glyph string
24332 draws over it. */
24333 i = left_overwritten (head);
24334 if (i >= 0)
24336 enum draw_glyphs_face overlap_hl;
24338 /* If this row contains mouse highlighting, attempt to draw
24339 the overlapped glyphs with the correct highlight. This
24340 code fails if the overlap encompasses more than one glyph
24341 and mouse-highlight spans only some of these glyphs.
24342 However, making it work perfectly involves a lot more
24343 code, and I don't know if the pathological case occurs in
24344 practice, so we'll stick to this for now. --- cyd */
24345 if (check_mouse_face
24346 && mouse_beg_col < start && mouse_end_col > i)
24347 overlap_hl = DRAW_MOUSE_FACE;
24348 else
24349 overlap_hl = DRAW_NORMAL_TEXT;
24351 j = i;
24352 BUILD_GLYPH_STRINGS (j, start, h, t,
24353 overlap_hl, dummy_x, last_x);
24354 start = i;
24355 compute_overhangs_and_x (t, head->x, 1);
24356 prepend_glyph_string_lists (&head, &tail, h, t);
24357 clip_head = head;
24360 /* Prepend glyph strings for glyphs in front of the first glyph
24361 string that overwrite that glyph string because of their
24362 right overhang. For these strings, only the foreground must
24363 be drawn, because it draws over the glyph string at `head'.
24364 The background must not be drawn because this would overwrite
24365 right overhangs of preceding glyphs for which no glyph
24366 strings exist. */
24367 i = left_overwriting (head);
24368 if (i >= 0)
24370 enum draw_glyphs_face overlap_hl;
24372 if (check_mouse_face
24373 && mouse_beg_col < start && mouse_end_col > i)
24374 overlap_hl = DRAW_MOUSE_FACE;
24375 else
24376 overlap_hl = DRAW_NORMAL_TEXT;
24378 clip_head = head;
24379 BUILD_GLYPH_STRINGS (i, start, h, t,
24380 overlap_hl, dummy_x, last_x);
24381 for (s = h; s; s = s->next)
24382 s->background_filled_p = 1;
24383 compute_overhangs_and_x (t, head->x, 1);
24384 prepend_glyph_string_lists (&head, &tail, h, t);
24387 /* Append glyphs strings for glyphs following the last glyph
24388 string tail that are overwritten by tail. The background of
24389 these strings has to be drawn because tail's foreground draws
24390 over it. */
24391 i = right_overwritten (tail);
24392 if (i >= 0)
24394 enum draw_glyphs_face overlap_hl;
24396 if (check_mouse_face
24397 && mouse_beg_col < i && mouse_end_col > end)
24398 overlap_hl = DRAW_MOUSE_FACE;
24399 else
24400 overlap_hl = DRAW_NORMAL_TEXT;
24402 BUILD_GLYPH_STRINGS (end, i, h, t,
24403 overlap_hl, x, last_x);
24404 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24405 we don't have `end = i;' here. */
24406 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24407 append_glyph_string_lists (&head, &tail, h, t);
24408 clip_tail = tail;
24411 /* Append glyph strings for glyphs following the last glyph
24412 string tail that overwrite tail. The foreground of such
24413 glyphs has to be drawn because it writes into the background
24414 of tail. The background must not be drawn because it could
24415 paint over the foreground of following glyphs. */
24416 i = right_overwriting (tail);
24417 if (i >= 0)
24419 enum draw_glyphs_face overlap_hl;
24420 if (check_mouse_face
24421 && mouse_beg_col < i && mouse_end_col > end)
24422 overlap_hl = DRAW_MOUSE_FACE;
24423 else
24424 overlap_hl = DRAW_NORMAL_TEXT;
24426 clip_tail = tail;
24427 i++; /* We must include the Ith glyph. */
24428 BUILD_GLYPH_STRINGS (end, i, h, t,
24429 overlap_hl, x, last_x);
24430 for (s = h; s; s = s->next)
24431 s->background_filled_p = 1;
24432 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24433 append_glyph_string_lists (&head, &tail, h, t);
24435 if (clip_head || clip_tail)
24436 for (s = head; s; s = s->next)
24438 s->clip_head = clip_head;
24439 s->clip_tail = clip_tail;
24443 /* Draw all strings. */
24444 for (s = head; s; s = s->next)
24445 FRAME_RIF (f)->draw_glyph_string (s);
24447 #ifndef HAVE_NS
24448 /* When focus a sole frame and move horizontally, this sets on_p to 0
24449 causing a failure to erase prev cursor position. */
24450 if (area == TEXT_AREA
24451 && !row->full_width_p
24452 /* When drawing overlapping rows, only the glyph strings'
24453 foreground is drawn, which doesn't erase a cursor
24454 completely. */
24455 && !overlaps)
24457 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24458 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24459 : (tail ? tail->x + tail->background_width : x));
24460 x0 -= area_left;
24461 x1 -= area_left;
24463 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24464 row->y, MATRIX_ROW_BOTTOM_Y (row));
24466 #endif
24468 /* Value is the x-position up to which drawn, relative to AREA of W.
24469 This doesn't include parts drawn because of overhangs. */
24470 if (row->full_width_p)
24471 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24472 else
24473 x_reached -= area_left;
24475 RELEASE_HDC (hdc, f);
24477 return x_reached;
24480 /* Expand row matrix if too narrow. Don't expand if area
24481 is not present. */
24483 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24485 if (!it->f->fonts_changed \
24486 && (it->glyph_row->glyphs[area] \
24487 < it->glyph_row->glyphs[area + 1])) \
24489 it->w->ncols_scale_factor++; \
24490 it->f->fonts_changed = 1; \
24494 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24495 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24497 static void
24498 append_glyph (struct it *it)
24500 struct glyph *glyph;
24501 enum glyph_row_area area = it->area;
24503 eassert (it->glyph_row);
24504 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24506 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24507 if (glyph < it->glyph_row->glyphs[area + 1])
24509 /* If the glyph row is reversed, we need to prepend the glyph
24510 rather than append it. */
24511 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24513 struct glyph *g;
24515 /* Make room for the additional glyph. */
24516 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24517 g[1] = *g;
24518 glyph = it->glyph_row->glyphs[area];
24520 glyph->charpos = CHARPOS (it->position);
24521 glyph->object = it->object;
24522 if (it->pixel_width > 0)
24524 glyph->pixel_width = it->pixel_width;
24525 glyph->padding_p = 0;
24527 else
24529 /* Assure at least 1-pixel width. Otherwise, cursor can't
24530 be displayed correctly. */
24531 glyph->pixel_width = 1;
24532 glyph->padding_p = 1;
24534 glyph->ascent = it->ascent;
24535 glyph->descent = it->descent;
24536 glyph->voffset = it->voffset;
24537 glyph->type = CHAR_GLYPH;
24538 glyph->avoid_cursor_p = it->avoid_cursor_p;
24539 glyph->multibyte_p = it->multibyte_p;
24540 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24542 /* In R2L rows, the left and the right box edges need to be
24543 drawn in reverse direction. */
24544 glyph->right_box_line_p = it->start_of_box_run_p;
24545 glyph->left_box_line_p = it->end_of_box_run_p;
24547 else
24549 glyph->left_box_line_p = it->start_of_box_run_p;
24550 glyph->right_box_line_p = it->end_of_box_run_p;
24552 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24553 || it->phys_descent > it->descent);
24554 glyph->glyph_not_available_p = it->glyph_not_available_p;
24555 glyph->face_id = it->face_id;
24556 glyph->u.ch = it->char_to_display;
24557 glyph->slice.img = null_glyph_slice;
24558 glyph->font_type = FONT_TYPE_UNKNOWN;
24559 if (it->bidi_p)
24561 glyph->resolved_level = it->bidi_it.resolved_level;
24562 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24563 emacs_abort ();
24564 glyph->bidi_type = it->bidi_it.type;
24566 else
24568 glyph->resolved_level = 0;
24569 glyph->bidi_type = UNKNOWN_BT;
24571 ++it->glyph_row->used[area];
24573 else
24574 IT_EXPAND_MATRIX_WIDTH (it, area);
24577 /* Store one glyph for the composition IT->cmp_it.id in
24578 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24579 non-null. */
24581 static void
24582 append_composite_glyph (struct it *it)
24584 struct glyph *glyph;
24585 enum glyph_row_area area = it->area;
24587 eassert (it->glyph_row);
24589 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24590 if (glyph < it->glyph_row->glyphs[area + 1])
24592 /* If the glyph row is reversed, we need to prepend the glyph
24593 rather than append it. */
24594 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24596 struct glyph *g;
24598 /* Make room for the new glyph. */
24599 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24600 g[1] = *g;
24601 glyph = it->glyph_row->glyphs[it->area];
24603 glyph->charpos = it->cmp_it.charpos;
24604 glyph->object = it->object;
24605 glyph->pixel_width = it->pixel_width;
24606 glyph->ascent = it->ascent;
24607 glyph->descent = it->descent;
24608 glyph->voffset = it->voffset;
24609 glyph->type = COMPOSITE_GLYPH;
24610 if (it->cmp_it.ch < 0)
24612 glyph->u.cmp.automatic = 0;
24613 glyph->u.cmp.id = it->cmp_it.id;
24614 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24616 else
24618 glyph->u.cmp.automatic = 1;
24619 glyph->u.cmp.id = it->cmp_it.id;
24620 glyph->slice.cmp.from = it->cmp_it.from;
24621 glyph->slice.cmp.to = it->cmp_it.to - 1;
24623 glyph->avoid_cursor_p = it->avoid_cursor_p;
24624 glyph->multibyte_p = it->multibyte_p;
24625 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24627 /* In R2L rows, the left and the right box edges need to be
24628 drawn in reverse direction. */
24629 glyph->right_box_line_p = it->start_of_box_run_p;
24630 glyph->left_box_line_p = it->end_of_box_run_p;
24632 else
24634 glyph->left_box_line_p = it->start_of_box_run_p;
24635 glyph->right_box_line_p = it->end_of_box_run_p;
24637 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24638 || it->phys_descent > it->descent);
24639 glyph->padding_p = 0;
24640 glyph->glyph_not_available_p = 0;
24641 glyph->face_id = it->face_id;
24642 glyph->font_type = FONT_TYPE_UNKNOWN;
24643 if (it->bidi_p)
24645 glyph->resolved_level = it->bidi_it.resolved_level;
24646 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24647 emacs_abort ();
24648 glyph->bidi_type = it->bidi_it.type;
24650 ++it->glyph_row->used[area];
24652 else
24653 IT_EXPAND_MATRIX_WIDTH (it, area);
24657 /* Change IT->ascent and IT->height according to the setting of
24658 IT->voffset. */
24660 static void
24661 take_vertical_position_into_account (struct it *it)
24663 if (it->voffset)
24665 if (it->voffset < 0)
24666 /* Increase the ascent so that we can display the text higher
24667 in the line. */
24668 it->ascent -= it->voffset;
24669 else
24670 /* Increase the descent so that we can display the text lower
24671 in the line. */
24672 it->descent += it->voffset;
24677 /* Produce glyphs/get display metrics for the image IT is loaded with.
24678 See the description of struct display_iterator in dispextern.h for
24679 an overview of struct display_iterator. */
24681 static void
24682 produce_image_glyph (struct it *it)
24684 struct image *img;
24685 struct face *face;
24686 int glyph_ascent, crop;
24687 struct glyph_slice slice;
24689 eassert (it->what == IT_IMAGE);
24691 face = FACE_FROM_ID (it->f, it->face_id);
24692 eassert (face);
24693 /* Make sure X resources of the face is loaded. */
24694 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24696 if (it->image_id < 0)
24698 /* Fringe bitmap. */
24699 it->ascent = it->phys_ascent = 0;
24700 it->descent = it->phys_descent = 0;
24701 it->pixel_width = 0;
24702 it->nglyphs = 0;
24703 return;
24706 img = IMAGE_FROM_ID (it->f, it->image_id);
24707 eassert (img);
24708 /* Make sure X resources of the image is loaded. */
24709 prepare_image_for_display (it->f, img);
24711 slice.x = slice.y = 0;
24712 slice.width = img->width;
24713 slice.height = img->height;
24715 if (INTEGERP (it->slice.x))
24716 slice.x = XINT (it->slice.x);
24717 else if (FLOATP (it->slice.x))
24718 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24720 if (INTEGERP (it->slice.y))
24721 slice.y = XINT (it->slice.y);
24722 else if (FLOATP (it->slice.y))
24723 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24725 if (INTEGERP (it->slice.width))
24726 slice.width = XINT (it->slice.width);
24727 else if (FLOATP (it->slice.width))
24728 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24730 if (INTEGERP (it->slice.height))
24731 slice.height = XINT (it->slice.height);
24732 else if (FLOATP (it->slice.height))
24733 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24735 if (slice.x >= img->width)
24736 slice.x = img->width;
24737 if (slice.y >= img->height)
24738 slice.y = img->height;
24739 if (slice.x + slice.width >= img->width)
24740 slice.width = img->width - slice.x;
24741 if (slice.y + slice.height > img->height)
24742 slice.height = img->height - slice.y;
24744 if (slice.width == 0 || slice.height == 0)
24745 return;
24747 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24749 it->descent = slice.height - glyph_ascent;
24750 if (slice.y == 0)
24751 it->descent += img->vmargin;
24752 if (slice.y + slice.height == img->height)
24753 it->descent += img->vmargin;
24754 it->phys_descent = it->descent;
24756 it->pixel_width = slice.width;
24757 if (slice.x == 0)
24758 it->pixel_width += img->hmargin;
24759 if (slice.x + slice.width == img->width)
24760 it->pixel_width += img->hmargin;
24762 /* It's quite possible for images to have an ascent greater than
24763 their height, so don't get confused in that case. */
24764 if (it->descent < 0)
24765 it->descent = 0;
24767 it->nglyphs = 1;
24769 if (face->box != FACE_NO_BOX)
24771 if (face->box_line_width > 0)
24773 if (slice.y == 0)
24774 it->ascent += face->box_line_width;
24775 if (slice.y + slice.height == img->height)
24776 it->descent += face->box_line_width;
24779 if (it->start_of_box_run_p && slice.x == 0)
24780 it->pixel_width += eabs (face->box_line_width);
24781 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24782 it->pixel_width += eabs (face->box_line_width);
24785 take_vertical_position_into_account (it);
24787 /* Automatically crop wide image glyphs at right edge so we can
24788 draw the cursor on same display row. */
24789 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24790 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24792 it->pixel_width -= crop;
24793 slice.width -= crop;
24796 if (it->glyph_row)
24798 struct glyph *glyph;
24799 enum glyph_row_area area = it->area;
24801 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24802 if (glyph < it->glyph_row->glyphs[area + 1])
24804 glyph->charpos = CHARPOS (it->position);
24805 glyph->object = it->object;
24806 glyph->pixel_width = it->pixel_width;
24807 glyph->ascent = glyph_ascent;
24808 glyph->descent = it->descent;
24809 glyph->voffset = it->voffset;
24810 glyph->type = IMAGE_GLYPH;
24811 glyph->avoid_cursor_p = it->avoid_cursor_p;
24812 glyph->multibyte_p = it->multibyte_p;
24813 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24815 /* In R2L rows, the left and the right box edges need to be
24816 drawn in reverse direction. */
24817 glyph->right_box_line_p = it->start_of_box_run_p;
24818 glyph->left_box_line_p = it->end_of_box_run_p;
24820 else
24822 glyph->left_box_line_p = it->start_of_box_run_p;
24823 glyph->right_box_line_p = it->end_of_box_run_p;
24825 glyph->overlaps_vertically_p = 0;
24826 glyph->padding_p = 0;
24827 glyph->glyph_not_available_p = 0;
24828 glyph->face_id = it->face_id;
24829 glyph->u.img_id = img->id;
24830 glyph->slice.img = slice;
24831 glyph->font_type = FONT_TYPE_UNKNOWN;
24832 if (it->bidi_p)
24834 glyph->resolved_level = it->bidi_it.resolved_level;
24835 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24836 emacs_abort ();
24837 glyph->bidi_type = it->bidi_it.type;
24839 ++it->glyph_row->used[area];
24841 else
24842 IT_EXPAND_MATRIX_WIDTH (it, area);
24847 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24848 of the glyph, WIDTH and HEIGHT are the width and height of the
24849 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24851 static void
24852 append_stretch_glyph (struct it *it, Lisp_Object object,
24853 int width, int height, int ascent)
24855 struct glyph *glyph;
24856 enum glyph_row_area area = it->area;
24858 eassert (ascent >= 0 && ascent <= height);
24860 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24861 if (glyph < it->glyph_row->glyphs[area + 1])
24863 /* If the glyph row is reversed, we need to prepend the glyph
24864 rather than append it. */
24865 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24867 struct glyph *g;
24869 /* Make room for the additional glyph. */
24870 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24871 g[1] = *g;
24872 glyph = it->glyph_row->glyphs[area];
24874 glyph->charpos = CHARPOS (it->position);
24875 glyph->object = object;
24876 glyph->pixel_width = width;
24877 glyph->ascent = ascent;
24878 glyph->descent = height - ascent;
24879 glyph->voffset = it->voffset;
24880 glyph->type = STRETCH_GLYPH;
24881 glyph->avoid_cursor_p = it->avoid_cursor_p;
24882 glyph->multibyte_p = it->multibyte_p;
24883 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24885 /* In R2L rows, the left and the right box edges need to be
24886 drawn in reverse direction. */
24887 glyph->right_box_line_p = it->start_of_box_run_p;
24888 glyph->left_box_line_p = it->end_of_box_run_p;
24890 else
24892 glyph->left_box_line_p = it->start_of_box_run_p;
24893 glyph->right_box_line_p = it->end_of_box_run_p;
24895 glyph->overlaps_vertically_p = 0;
24896 glyph->padding_p = 0;
24897 glyph->glyph_not_available_p = 0;
24898 glyph->face_id = it->face_id;
24899 glyph->u.stretch.ascent = ascent;
24900 glyph->u.stretch.height = height;
24901 glyph->slice.img = null_glyph_slice;
24902 glyph->font_type = FONT_TYPE_UNKNOWN;
24903 if (it->bidi_p)
24905 glyph->resolved_level = it->bidi_it.resolved_level;
24906 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24907 emacs_abort ();
24908 glyph->bidi_type = it->bidi_it.type;
24910 else
24912 glyph->resolved_level = 0;
24913 glyph->bidi_type = UNKNOWN_BT;
24915 ++it->glyph_row->used[area];
24917 else
24918 IT_EXPAND_MATRIX_WIDTH (it, area);
24921 #endif /* HAVE_WINDOW_SYSTEM */
24923 /* Produce a stretch glyph for iterator IT. IT->object is the value
24924 of the glyph property displayed. The value must be a list
24925 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24926 being recognized:
24928 1. `:width WIDTH' specifies that the space should be WIDTH *
24929 canonical char width wide. WIDTH may be an integer or floating
24930 point number.
24932 2. `:relative-width FACTOR' specifies that the width of the stretch
24933 should be computed from the width of the first character having the
24934 `glyph' property, and should be FACTOR times that width.
24936 3. `:align-to HPOS' specifies that the space should be wide enough
24937 to reach HPOS, a value in canonical character units.
24939 Exactly one of the above pairs must be present.
24941 4. `:height HEIGHT' specifies that the height of the stretch produced
24942 should be HEIGHT, measured in canonical character units.
24944 5. `:relative-height FACTOR' specifies that the height of the
24945 stretch should be FACTOR times the height of the characters having
24946 the glyph property.
24948 Either none or exactly one of 4 or 5 must be present.
24950 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24951 of the stretch should be used for the ascent of the stretch.
24952 ASCENT must be in the range 0 <= ASCENT <= 100. */
24954 void
24955 produce_stretch_glyph (struct it *it)
24957 /* (space :width WIDTH :height HEIGHT ...) */
24958 Lisp_Object prop, plist;
24959 int width = 0, height = 0, align_to = -1;
24960 int zero_width_ok_p = 0;
24961 double tem;
24962 struct font *font = NULL;
24964 #ifdef HAVE_WINDOW_SYSTEM
24965 int ascent = 0;
24966 int zero_height_ok_p = 0;
24968 if (FRAME_WINDOW_P (it->f))
24970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24971 font = face->font ? face->font : FRAME_FONT (it->f);
24972 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24974 #endif
24976 /* List should start with `space'. */
24977 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24978 plist = XCDR (it->object);
24980 /* Compute the width of the stretch. */
24981 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24982 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24984 /* Absolute width `:width WIDTH' specified and valid. */
24985 zero_width_ok_p = 1;
24986 width = (int)tem;
24988 #ifdef HAVE_WINDOW_SYSTEM
24989 else if (FRAME_WINDOW_P (it->f)
24990 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24992 /* Relative width `:relative-width FACTOR' specified and valid.
24993 Compute the width of the characters having the `glyph'
24994 property. */
24995 struct it it2;
24996 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24998 it2 = *it;
24999 if (it->multibyte_p)
25000 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25001 else
25003 it2.c = it2.char_to_display = *p, it2.len = 1;
25004 if (! ASCII_CHAR_P (it2.c))
25005 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25008 it2.glyph_row = NULL;
25009 it2.what = IT_CHARACTER;
25010 x_produce_glyphs (&it2);
25011 width = NUMVAL (prop) * it2.pixel_width;
25013 #endif /* HAVE_WINDOW_SYSTEM */
25014 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25015 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25017 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25018 align_to = (align_to < 0
25020 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25021 else if (align_to < 0)
25022 align_to = window_box_left_offset (it->w, TEXT_AREA);
25023 width = max (0, (int)tem + align_to - it->current_x);
25024 zero_width_ok_p = 1;
25026 else
25027 /* Nothing specified -> width defaults to canonical char width. */
25028 width = FRAME_COLUMN_WIDTH (it->f);
25030 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25031 width = 1;
25033 #ifdef HAVE_WINDOW_SYSTEM
25034 /* Compute height. */
25035 if (FRAME_WINDOW_P (it->f))
25037 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25038 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25040 height = (int)tem;
25041 zero_height_ok_p = 1;
25043 else if (prop = Fplist_get (plist, QCrelative_height),
25044 NUMVAL (prop) > 0)
25045 height = FONT_HEIGHT (font) * NUMVAL (prop);
25046 else
25047 height = FONT_HEIGHT (font);
25049 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25050 height = 1;
25052 /* Compute percentage of height used for ascent. If
25053 `:ascent ASCENT' is present and valid, use that. Otherwise,
25054 derive the ascent from the font in use. */
25055 if (prop = Fplist_get (plist, QCascent),
25056 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25057 ascent = height * NUMVAL (prop) / 100.0;
25058 else if (!NILP (prop)
25059 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25060 ascent = min (max (0, (int)tem), height);
25061 else
25062 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25064 else
25065 #endif /* HAVE_WINDOW_SYSTEM */
25066 height = 1;
25068 if (width > 0 && it->line_wrap != TRUNCATE
25069 && it->current_x + width > it->last_visible_x)
25071 width = it->last_visible_x - it->current_x;
25072 #ifdef HAVE_WINDOW_SYSTEM
25073 /* Subtract one more pixel from the stretch width, but only on
25074 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25075 width -= FRAME_WINDOW_P (it->f);
25076 #endif
25079 if (width > 0 && height > 0 && it->glyph_row)
25081 Lisp_Object o_object = it->object;
25082 Lisp_Object object = it->stack[it->sp - 1].string;
25083 int n = width;
25085 if (!STRINGP (object))
25086 object = it->w->contents;
25087 #ifdef HAVE_WINDOW_SYSTEM
25088 if (FRAME_WINDOW_P (it->f))
25089 append_stretch_glyph (it, object, width, height, ascent);
25090 else
25091 #endif
25093 it->object = object;
25094 it->char_to_display = ' ';
25095 it->pixel_width = it->len = 1;
25096 while (n--)
25097 tty_append_glyph (it);
25098 it->object = o_object;
25102 it->pixel_width = width;
25103 #ifdef HAVE_WINDOW_SYSTEM
25104 if (FRAME_WINDOW_P (it->f))
25106 it->ascent = it->phys_ascent = ascent;
25107 it->descent = it->phys_descent = height - it->ascent;
25108 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25109 take_vertical_position_into_account (it);
25111 else
25112 #endif
25113 it->nglyphs = width;
25116 /* Get information about special display element WHAT in an
25117 environment described by IT. WHAT is one of IT_TRUNCATION or
25118 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25119 non-null glyph_row member. This function ensures that fields like
25120 face_id, c, len of IT are left untouched. */
25122 static void
25123 produce_special_glyphs (struct it *it, enum display_element_type what)
25125 struct it temp_it;
25126 Lisp_Object gc;
25127 GLYPH glyph;
25129 temp_it = *it;
25130 temp_it.object = make_number (0);
25131 memset (&temp_it.current, 0, sizeof temp_it.current);
25133 if (what == IT_CONTINUATION)
25135 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25136 if (it->bidi_it.paragraph_dir == R2L)
25137 SET_GLYPH_FROM_CHAR (glyph, '/');
25138 else
25139 SET_GLYPH_FROM_CHAR (glyph, '\\');
25140 if (it->dp
25141 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25143 /* FIXME: Should we mirror GC for R2L lines? */
25144 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25145 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25148 else if (what == IT_TRUNCATION)
25150 /* Truncation glyph. */
25151 SET_GLYPH_FROM_CHAR (glyph, '$');
25152 if (it->dp
25153 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25155 /* FIXME: Should we mirror GC for R2L lines? */
25156 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25157 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25160 else
25161 emacs_abort ();
25163 #ifdef HAVE_WINDOW_SYSTEM
25164 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25165 is turned off, we precede the truncation/continuation glyphs by a
25166 stretch glyph whose width is computed such that these special
25167 glyphs are aligned at the window margin, even when very different
25168 fonts are used in different glyph rows. */
25169 if (FRAME_WINDOW_P (temp_it.f)
25170 /* init_iterator calls this with it->glyph_row == NULL, and it
25171 wants only the pixel width of the truncation/continuation
25172 glyphs. */
25173 && temp_it.glyph_row
25174 /* insert_left_trunc_glyphs calls us at the beginning of the
25175 row, and it has its own calculation of the stretch glyph
25176 width. */
25177 && temp_it.glyph_row->used[TEXT_AREA] > 0
25178 && (temp_it.glyph_row->reversed_p
25179 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25180 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25182 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25184 if (stretch_width > 0)
25186 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25187 struct font *font =
25188 face->font ? face->font : FRAME_FONT (temp_it.f);
25189 int stretch_ascent =
25190 (((temp_it.ascent + temp_it.descent)
25191 * FONT_BASE (font)) / FONT_HEIGHT (font));
25193 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25194 temp_it.ascent + temp_it.descent,
25195 stretch_ascent);
25198 #endif
25200 temp_it.dp = NULL;
25201 temp_it.what = IT_CHARACTER;
25202 temp_it.len = 1;
25203 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25204 temp_it.face_id = GLYPH_FACE (glyph);
25205 temp_it.len = CHAR_BYTES (temp_it.c);
25207 PRODUCE_GLYPHS (&temp_it);
25208 it->pixel_width = temp_it.pixel_width;
25209 it->nglyphs = temp_it.pixel_width;
25212 #ifdef HAVE_WINDOW_SYSTEM
25214 /* Calculate line-height and line-spacing properties.
25215 An integer value specifies explicit pixel value.
25216 A float value specifies relative value to current face height.
25217 A cons (float . face-name) specifies relative value to
25218 height of specified face font.
25220 Returns height in pixels, or nil. */
25223 static Lisp_Object
25224 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25225 int boff, int override)
25227 Lisp_Object face_name = Qnil;
25228 int ascent, descent, height;
25230 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25231 return val;
25233 if (CONSP (val))
25235 face_name = XCAR (val);
25236 val = XCDR (val);
25237 if (!NUMBERP (val))
25238 val = make_number (1);
25239 if (NILP (face_name))
25241 height = it->ascent + it->descent;
25242 goto scale;
25246 if (NILP (face_name))
25248 font = FRAME_FONT (it->f);
25249 boff = FRAME_BASELINE_OFFSET (it->f);
25251 else if (EQ (face_name, Qt))
25253 override = 0;
25255 else
25257 int face_id;
25258 struct face *face;
25260 face_id = lookup_named_face (it->f, face_name, 0);
25261 if (face_id < 0)
25262 return make_number (-1);
25264 face = FACE_FROM_ID (it->f, face_id);
25265 font = face->font;
25266 if (font == NULL)
25267 return make_number (-1);
25268 boff = font->baseline_offset;
25269 if (font->vertical_centering)
25270 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25273 ascent = FONT_BASE (font) + boff;
25274 descent = FONT_DESCENT (font) - boff;
25276 if (override)
25278 it->override_ascent = ascent;
25279 it->override_descent = descent;
25280 it->override_boff = boff;
25283 height = ascent + descent;
25285 scale:
25286 if (FLOATP (val))
25287 height = (int)(XFLOAT_DATA (val) * height);
25288 else if (INTEGERP (val))
25289 height *= XINT (val);
25291 return make_number (height);
25295 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25296 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25297 and only if this is for a character for which no font was found.
25299 If the display method (it->glyphless_method) is
25300 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25301 length of the acronym or the hexadecimal string, UPPER_XOFF and
25302 UPPER_YOFF are pixel offsets for the upper part of the string,
25303 LOWER_XOFF and LOWER_YOFF are for the lower part.
25305 For the other display methods, LEN through LOWER_YOFF are zero. */
25307 static void
25308 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25309 short upper_xoff, short upper_yoff,
25310 short lower_xoff, short lower_yoff)
25312 struct glyph *glyph;
25313 enum glyph_row_area area = it->area;
25315 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25316 if (glyph < it->glyph_row->glyphs[area + 1])
25318 /* If the glyph row is reversed, we need to prepend the glyph
25319 rather than append it. */
25320 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25322 struct glyph *g;
25324 /* Make room for the additional glyph. */
25325 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25326 g[1] = *g;
25327 glyph = it->glyph_row->glyphs[area];
25329 glyph->charpos = CHARPOS (it->position);
25330 glyph->object = it->object;
25331 glyph->pixel_width = it->pixel_width;
25332 glyph->ascent = it->ascent;
25333 glyph->descent = it->descent;
25334 glyph->voffset = it->voffset;
25335 glyph->type = GLYPHLESS_GLYPH;
25336 glyph->u.glyphless.method = it->glyphless_method;
25337 glyph->u.glyphless.for_no_font = for_no_font;
25338 glyph->u.glyphless.len = len;
25339 glyph->u.glyphless.ch = it->c;
25340 glyph->slice.glyphless.upper_xoff = upper_xoff;
25341 glyph->slice.glyphless.upper_yoff = upper_yoff;
25342 glyph->slice.glyphless.lower_xoff = lower_xoff;
25343 glyph->slice.glyphless.lower_yoff = lower_yoff;
25344 glyph->avoid_cursor_p = it->avoid_cursor_p;
25345 glyph->multibyte_p = it->multibyte_p;
25346 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25348 /* In R2L rows, the left and the right box edges need to be
25349 drawn in reverse direction. */
25350 glyph->right_box_line_p = it->start_of_box_run_p;
25351 glyph->left_box_line_p = it->end_of_box_run_p;
25353 else
25355 glyph->left_box_line_p = it->start_of_box_run_p;
25356 glyph->right_box_line_p = it->end_of_box_run_p;
25358 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25359 || it->phys_descent > it->descent);
25360 glyph->padding_p = 0;
25361 glyph->glyph_not_available_p = 0;
25362 glyph->face_id = face_id;
25363 glyph->font_type = FONT_TYPE_UNKNOWN;
25364 if (it->bidi_p)
25366 glyph->resolved_level = it->bidi_it.resolved_level;
25367 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25368 emacs_abort ();
25369 glyph->bidi_type = it->bidi_it.type;
25371 ++it->glyph_row->used[area];
25373 else
25374 IT_EXPAND_MATRIX_WIDTH (it, area);
25378 /* Produce a glyph for a glyphless character for iterator IT.
25379 IT->glyphless_method specifies which method to use for displaying
25380 the character. See the description of enum
25381 glyphless_display_method in dispextern.h for the detail.
25383 FOR_NO_FONT is nonzero if and only if this is for a character for
25384 which no font was found. ACRONYM, if non-nil, is an acronym string
25385 for the character. */
25387 static void
25388 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25390 int face_id;
25391 struct face *face;
25392 struct font *font;
25393 int base_width, base_height, width, height;
25394 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25395 int len;
25397 /* Get the metrics of the base font. We always refer to the current
25398 ASCII face. */
25399 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25400 font = face->font ? face->font : FRAME_FONT (it->f);
25401 it->ascent = FONT_BASE (font) + font->baseline_offset;
25402 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25403 base_height = it->ascent + it->descent;
25404 base_width = font->average_width;
25406 face_id = merge_glyphless_glyph_face (it);
25408 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25410 it->pixel_width = THIN_SPACE_WIDTH;
25411 len = 0;
25412 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25414 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25416 width = CHAR_WIDTH (it->c);
25417 if (width == 0)
25418 width = 1;
25419 else if (width > 4)
25420 width = 4;
25421 it->pixel_width = base_width * width;
25422 len = 0;
25423 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25425 else
25427 char buf[7];
25428 const char *str;
25429 unsigned int code[6];
25430 int upper_len;
25431 int ascent, descent;
25432 struct font_metrics metrics_upper, metrics_lower;
25434 face = FACE_FROM_ID (it->f, face_id);
25435 font = face->font ? face->font : FRAME_FONT (it->f);
25436 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25438 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25440 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25441 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25442 if (CONSP (acronym))
25443 acronym = XCAR (acronym);
25444 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25446 else
25448 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25449 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25450 str = buf;
25452 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25453 code[len] = font->driver->encode_char (font, str[len]);
25454 upper_len = (len + 1) / 2;
25455 font->driver->text_extents (font, code, upper_len,
25456 &metrics_upper);
25457 font->driver->text_extents (font, code + upper_len, len - upper_len,
25458 &metrics_lower);
25462 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25463 width = max (metrics_upper.width, metrics_lower.width) + 4;
25464 upper_xoff = upper_yoff = 2; /* the typical case */
25465 if (base_width >= width)
25467 /* Align the upper to the left, the lower to the right. */
25468 it->pixel_width = base_width;
25469 lower_xoff = base_width - 2 - metrics_lower.width;
25471 else
25473 /* Center the shorter one. */
25474 it->pixel_width = width;
25475 if (metrics_upper.width >= metrics_lower.width)
25476 lower_xoff = (width - metrics_lower.width) / 2;
25477 else
25479 /* FIXME: This code doesn't look right. It formerly was
25480 missing the "lower_xoff = 0;", which couldn't have
25481 been right since it left lower_xoff uninitialized. */
25482 lower_xoff = 0;
25483 upper_xoff = (width - metrics_upper.width) / 2;
25487 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25488 top, bottom, and between upper and lower strings. */
25489 height = (metrics_upper.ascent + metrics_upper.descent
25490 + metrics_lower.ascent + metrics_lower.descent) + 5;
25491 /* Center vertically.
25492 H:base_height, D:base_descent
25493 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25495 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25496 descent = D - H/2 + h/2;
25497 lower_yoff = descent - 2 - ld;
25498 upper_yoff = lower_yoff - la - 1 - ud; */
25499 ascent = - (it->descent - (base_height + height + 1) / 2);
25500 descent = it->descent - (base_height - height) / 2;
25501 lower_yoff = descent - 2 - metrics_lower.descent;
25502 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25503 - metrics_upper.descent);
25504 /* Don't make the height shorter than the base height. */
25505 if (height > base_height)
25507 it->ascent = ascent;
25508 it->descent = descent;
25512 it->phys_ascent = it->ascent;
25513 it->phys_descent = it->descent;
25514 if (it->glyph_row)
25515 append_glyphless_glyph (it, face_id, for_no_font, len,
25516 upper_xoff, upper_yoff,
25517 lower_xoff, lower_yoff);
25518 it->nglyphs = 1;
25519 take_vertical_position_into_account (it);
25523 /* RIF:
25524 Produce glyphs/get display metrics for the display element IT is
25525 loaded with. See the description of struct it in dispextern.h
25526 for an overview of struct it. */
25528 void
25529 x_produce_glyphs (struct it *it)
25531 int extra_line_spacing = it->extra_line_spacing;
25533 it->glyph_not_available_p = 0;
25535 if (it->what == IT_CHARACTER)
25537 XChar2b char2b;
25538 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25539 struct font *font = face->font;
25540 struct font_metrics *pcm = NULL;
25541 int boff; /* Baseline offset. */
25543 if (font == NULL)
25545 /* When no suitable font is found, display this character by
25546 the method specified in the first extra slot of
25547 Vglyphless_char_display. */
25548 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25550 eassert (it->what == IT_GLYPHLESS);
25551 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25552 goto done;
25555 boff = font->baseline_offset;
25556 if (font->vertical_centering)
25557 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25559 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25561 int stretched_p;
25563 it->nglyphs = 1;
25565 if (it->override_ascent >= 0)
25567 it->ascent = it->override_ascent;
25568 it->descent = it->override_descent;
25569 boff = it->override_boff;
25571 else
25573 it->ascent = FONT_BASE (font) + boff;
25574 it->descent = FONT_DESCENT (font) - boff;
25577 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25579 pcm = get_per_char_metric (font, &char2b);
25580 if (pcm->width == 0
25581 && pcm->rbearing == 0 && pcm->lbearing == 0)
25582 pcm = NULL;
25585 if (pcm)
25587 it->phys_ascent = pcm->ascent + boff;
25588 it->phys_descent = pcm->descent - boff;
25589 it->pixel_width = pcm->width;
25591 else
25593 it->glyph_not_available_p = 1;
25594 it->phys_ascent = it->ascent;
25595 it->phys_descent = it->descent;
25596 it->pixel_width = font->space_width;
25599 if (it->constrain_row_ascent_descent_p)
25601 if (it->descent > it->max_descent)
25603 it->ascent += it->descent - it->max_descent;
25604 it->descent = it->max_descent;
25606 if (it->ascent > it->max_ascent)
25608 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25609 it->ascent = it->max_ascent;
25611 it->phys_ascent = min (it->phys_ascent, it->ascent);
25612 it->phys_descent = min (it->phys_descent, it->descent);
25613 extra_line_spacing = 0;
25616 /* If this is a space inside a region of text with
25617 `space-width' property, change its width. */
25618 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25619 if (stretched_p)
25620 it->pixel_width *= XFLOATINT (it->space_width);
25622 /* If face has a box, add the box thickness to the character
25623 height. If character has a box line to the left and/or
25624 right, add the box line width to the character's width. */
25625 if (face->box != FACE_NO_BOX)
25627 int thick = face->box_line_width;
25629 if (thick > 0)
25631 it->ascent += thick;
25632 it->descent += thick;
25634 else
25635 thick = -thick;
25637 if (it->start_of_box_run_p)
25638 it->pixel_width += thick;
25639 if (it->end_of_box_run_p)
25640 it->pixel_width += thick;
25643 /* If face has an overline, add the height of the overline
25644 (1 pixel) and a 1 pixel margin to the character height. */
25645 if (face->overline_p)
25646 it->ascent += overline_margin;
25648 if (it->constrain_row_ascent_descent_p)
25650 if (it->ascent > it->max_ascent)
25651 it->ascent = it->max_ascent;
25652 if (it->descent > it->max_descent)
25653 it->descent = it->max_descent;
25656 take_vertical_position_into_account (it);
25658 /* If we have to actually produce glyphs, do it. */
25659 if (it->glyph_row)
25661 if (stretched_p)
25663 /* Translate a space with a `space-width' property
25664 into a stretch glyph. */
25665 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25666 / FONT_HEIGHT (font));
25667 append_stretch_glyph (it, it->object, it->pixel_width,
25668 it->ascent + it->descent, ascent);
25670 else
25671 append_glyph (it);
25673 /* If characters with lbearing or rbearing are displayed
25674 in this line, record that fact in a flag of the
25675 glyph row. This is used to optimize X output code. */
25676 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25677 it->glyph_row->contains_overlapping_glyphs_p = 1;
25679 if (! stretched_p && it->pixel_width == 0)
25680 /* We assure that all visible glyphs have at least 1-pixel
25681 width. */
25682 it->pixel_width = 1;
25684 else if (it->char_to_display == '\n')
25686 /* A newline has no width, but we need the height of the
25687 line. But if previous part of the line sets a height,
25688 don't increase that height. */
25690 Lisp_Object height;
25691 Lisp_Object total_height = Qnil;
25693 it->override_ascent = -1;
25694 it->pixel_width = 0;
25695 it->nglyphs = 0;
25697 height = get_it_property (it, Qline_height);
25698 /* Split (line-height total-height) list. */
25699 if (CONSP (height)
25700 && CONSP (XCDR (height))
25701 && NILP (XCDR (XCDR (height))))
25703 total_height = XCAR (XCDR (height));
25704 height = XCAR (height);
25706 height = calc_line_height_property (it, height, font, boff, 1);
25708 if (it->override_ascent >= 0)
25710 it->ascent = it->override_ascent;
25711 it->descent = it->override_descent;
25712 boff = it->override_boff;
25714 else
25716 it->ascent = FONT_BASE (font) + boff;
25717 it->descent = FONT_DESCENT (font) - boff;
25720 if (EQ (height, Qt))
25722 if (it->descent > it->max_descent)
25724 it->ascent += it->descent - it->max_descent;
25725 it->descent = it->max_descent;
25727 if (it->ascent > it->max_ascent)
25729 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25730 it->ascent = it->max_ascent;
25732 it->phys_ascent = min (it->phys_ascent, it->ascent);
25733 it->phys_descent = min (it->phys_descent, it->descent);
25734 it->constrain_row_ascent_descent_p = 1;
25735 extra_line_spacing = 0;
25737 else
25739 Lisp_Object spacing;
25741 it->phys_ascent = it->ascent;
25742 it->phys_descent = it->descent;
25744 if ((it->max_ascent > 0 || it->max_descent > 0)
25745 && face->box != FACE_NO_BOX
25746 && face->box_line_width > 0)
25748 it->ascent += face->box_line_width;
25749 it->descent += face->box_line_width;
25751 if (!NILP (height)
25752 && XINT (height) > it->ascent + it->descent)
25753 it->ascent = XINT (height) - it->descent;
25755 if (!NILP (total_height))
25756 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25757 else
25759 spacing = get_it_property (it, Qline_spacing);
25760 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25762 if (INTEGERP (spacing))
25764 extra_line_spacing = XINT (spacing);
25765 if (!NILP (total_height))
25766 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25770 else /* i.e. (it->char_to_display == '\t') */
25772 if (font->space_width > 0)
25774 int tab_width = it->tab_width * font->space_width;
25775 int x = it->current_x + it->continuation_lines_width;
25776 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25778 /* If the distance from the current position to the next tab
25779 stop is less than a space character width, use the
25780 tab stop after that. */
25781 if (next_tab_x - x < font->space_width)
25782 next_tab_x += tab_width;
25784 it->pixel_width = next_tab_x - x;
25785 it->nglyphs = 1;
25786 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25787 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25789 if (it->glyph_row)
25791 append_stretch_glyph (it, it->object, it->pixel_width,
25792 it->ascent + it->descent, it->ascent);
25795 else
25797 it->pixel_width = 0;
25798 it->nglyphs = 1;
25802 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25804 /* A static composition.
25806 Note: A composition is represented as one glyph in the
25807 glyph matrix. There are no padding glyphs.
25809 Important note: pixel_width, ascent, and descent are the
25810 values of what is drawn by draw_glyphs (i.e. the values of
25811 the overall glyphs composed). */
25812 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25813 int boff; /* baseline offset */
25814 struct composition *cmp = composition_table[it->cmp_it.id];
25815 int glyph_len = cmp->glyph_len;
25816 struct font *font = face->font;
25818 it->nglyphs = 1;
25820 /* If we have not yet calculated pixel size data of glyphs of
25821 the composition for the current face font, calculate them
25822 now. Theoretically, we have to check all fonts for the
25823 glyphs, but that requires much time and memory space. So,
25824 here we check only the font of the first glyph. This may
25825 lead to incorrect display, but it's very rare, and C-l
25826 (recenter-top-bottom) can correct the display anyway. */
25827 if (! cmp->font || cmp->font != font)
25829 /* Ascent and descent of the font of the first character
25830 of this composition (adjusted by baseline offset).
25831 Ascent and descent of overall glyphs should not be less
25832 than these, respectively. */
25833 int font_ascent, font_descent, font_height;
25834 /* Bounding box of the overall glyphs. */
25835 int leftmost, rightmost, lowest, highest;
25836 int lbearing, rbearing;
25837 int i, width, ascent, descent;
25838 int left_padded = 0, right_padded = 0;
25839 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25840 XChar2b char2b;
25841 struct font_metrics *pcm;
25842 int font_not_found_p;
25843 ptrdiff_t pos;
25845 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25846 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25847 break;
25848 if (glyph_len < cmp->glyph_len)
25849 right_padded = 1;
25850 for (i = 0; i < glyph_len; i++)
25852 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25853 break;
25854 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25856 if (i > 0)
25857 left_padded = 1;
25859 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25860 : IT_CHARPOS (*it));
25861 /* If no suitable font is found, use the default font. */
25862 font_not_found_p = font == NULL;
25863 if (font_not_found_p)
25865 face = face->ascii_face;
25866 font = face->font;
25868 boff = font->baseline_offset;
25869 if (font->vertical_centering)
25870 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25871 font_ascent = FONT_BASE (font) + boff;
25872 font_descent = FONT_DESCENT (font) - boff;
25873 font_height = FONT_HEIGHT (font);
25875 cmp->font = font;
25877 pcm = NULL;
25878 if (! font_not_found_p)
25880 get_char_face_and_encoding (it->f, c, it->face_id,
25881 &char2b, 0);
25882 pcm = get_per_char_metric (font, &char2b);
25885 /* Initialize the bounding box. */
25886 if (pcm)
25888 width = cmp->glyph_len > 0 ? pcm->width : 0;
25889 ascent = pcm->ascent;
25890 descent = pcm->descent;
25891 lbearing = pcm->lbearing;
25892 rbearing = pcm->rbearing;
25894 else
25896 width = cmp->glyph_len > 0 ? font->space_width : 0;
25897 ascent = FONT_BASE (font);
25898 descent = FONT_DESCENT (font);
25899 lbearing = 0;
25900 rbearing = width;
25903 rightmost = width;
25904 leftmost = 0;
25905 lowest = - descent + boff;
25906 highest = ascent + boff;
25908 if (! font_not_found_p
25909 && font->default_ascent
25910 && CHAR_TABLE_P (Vuse_default_ascent)
25911 && !NILP (Faref (Vuse_default_ascent,
25912 make_number (it->char_to_display))))
25913 highest = font->default_ascent + boff;
25915 /* Draw the first glyph at the normal position. It may be
25916 shifted to right later if some other glyphs are drawn
25917 at the left. */
25918 cmp->offsets[i * 2] = 0;
25919 cmp->offsets[i * 2 + 1] = boff;
25920 cmp->lbearing = lbearing;
25921 cmp->rbearing = rbearing;
25923 /* Set cmp->offsets for the remaining glyphs. */
25924 for (i++; i < glyph_len; i++)
25926 int left, right, btm, top;
25927 int ch = COMPOSITION_GLYPH (cmp, i);
25928 int face_id;
25929 struct face *this_face;
25931 if (ch == '\t')
25932 ch = ' ';
25933 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25934 this_face = FACE_FROM_ID (it->f, face_id);
25935 font = this_face->font;
25937 if (font == NULL)
25938 pcm = NULL;
25939 else
25941 get_char_face_and_encoding (it->f, ch, face_id,
25942 &char2b, 0);
25943 pcm = get_per_char_metric (font, &char2b);
25945 if (! pcm)
25946 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25947 else
25949 width = pcm->width;
25950 ascent = pcm->ascent;
25951 descent = pcm->descent;
25952 lbearing = pcm->lbearing;
25953 rbearing = pcm->rbearing;
25954 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25956 /* Relative composition with or without
25957 alternate chars. */
25958 left = (leftmost + rightmost - width) / 2;
25959 btm = - descent + boff;
25960 if (font->relative_compose
25961 && (! CHAR_TABLE_P (Vignore_relative_composition)
25962 || NILP (Faref (Vignore_relative_composition,
25963 make_number (ch)))))
25966 if (- descent >= font->relative_compose)
25967 /* One extra pixel between two glyphs. */
25968 btm = highest + 1;
25969 else if (ascent <= 0)
25970 /* One extra pixel between two glyphs. */
25971 btm = lowest - 1 - ascent - descent;
25974 else
25976 /* A composition rule is specified by an integer
25977 value that encodes global and new reference
25978 points (GREF and NREF). GREF and NREF are
25979 specified by numbers as below:
25981 0---1---2 -- ascent
25985 9--10--11 -- center
25987 ---3---4---5--- baseline
25989 6---7---8 -- descent
25991 int rule = COMPOSITION_RULE (cmp, i);
25992 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25994 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25995 grefx = gref % 3, nrefx = nref % 3;
25996 grefy = gref / 3, nrefy = nref / 3;
25997 if (xoff)
25998 xoff = font_height * (xoff - 128) / 256;
25999 if (yoff)
26000 yoff = font_height * (yoff - 128) / 256;
26002 left = (leftmost
26003 + grefx * (rightmost - leftmost) / 2
26004 - nrefx * width / 2
26005 + xoff);
26007 btm = ((grefy == 0 ? highest
26008 : grefy == 1 ? 0
26009 : grefy == 2 ? lowest
26010 : (highest + lowest) / 2)
26011 - (nrefy == 0 ? ascent + descent
26012 : nrefy == 1 ? descent - boff
26013 : nrefy == 2 ? 0
26014 : (ascent + descent) / 2)
26015 + yoff);
26018 cmp->offsets[i * 2] = left;
26019 cmp->offsets[i * 2 + 1] = btm + descent;
26021 /* Update the bounding box of the overall glyphs. */
26022 if (width > 0)
26024 right = left + width;
26025 if (left < leftmost)
26026 leftmost = left;
26027 if (right > rightmost)
26028 rightmost = right;
26030 top = btm + descent + ascent;
26031 if (top > highest)
26032 highest = top;
26033 if (btm < lowest)
26034 lowest = btm;
26036 if (cmp->lbearing > left + lbearing)
26037 cmp->lbearing = left + lbearing;
26038 if (cmp->rbearing < left + rbearing)
26039 cmp->rbearing = left + rbearing;
26043 /* If there are glyphs whose x-offsets are negative,
26044 shift all glyphs to the right and make all x-offsets
26045 non-negative. */
26046 if (leftmost < 0)
26048 for (i = 0; i < cmp->glyph_len; i++)
26049 cmp->offsets[i * 2] -= leftmost;
26050 rightmost -= leftmost;
26051 cmp->lbearing -= leftmost;
26052 cmp->rbearing -= leftmost;
26055 if (left_padded && cmp->lbearing < 0)
26057 for (i = 0; i < cmp->glyph_len; i++)
26058 cmp->offsets[i * 2] -= cmp->lbearing;
26059 rightmost -= cmp->lbearing;
26060 cmp->rbearing -= cmp->lbearing;
26061 cmp->lbearing = 0;
26063 if (right_padded && rightmost < cmp->rbearing)
26065 rightmost = cmp->rbearing;
26068 cmp->pixel_width = rightmost;
26069 cmp->ascent = highest;
26070 cmp->descent = - lowest;
26071 if (cmp->ascent < font_ascent)
26072 cmp->ascent = font_ascent;
26073 if (cmp->descent < font_descent)
26074 cmp->descent = font_descent;
26077 if (it->glyph_row
26078 && (cmp->lbearing < 0
26079 || cmp->rbearing > cmp->pixel_width))
26080 it->glyph_row->contains_overlapping_glyphs_p = 1;
26082 it->pixel_width = cmp->pixel_width;
26083 it->ascent = it->phys_ascent = cmp->ascent;
26084 it->descent = it->phys_descent = cmp->descent;
26085 if (face->box != FACE_NO_BOX)
26087 int thick = face->box_line_width;
26089 if (thick > 0)
26091 it->ascent += thick;
26092 it->descent += thick;
26094 else
26095 thick = - thick;
26097 if (it->start_of_box_run_p)
26098 it->pixel_width += thick;
26099 if (it->end_of_box_run_p)
26100 it->pixel_width += thick;
26103 /* If face has an overline, add the height of the overline
26104 (1 pixel) and a 1 pixel margin to the character height. */
26105 if (face->overline_p)
26106 it->ascent += overline_margin;
26108 take_vertical_position_into_account (it);
26109 if (it->ascent < 0)
26110 it->ascent = 0;
26111 if (it->descent < 0)
26112 it->descent = 0;
26114 if (it->glyph_row && cmp->glyph_len > 0)
26115 append_composite_glyph (it);
26117 else if (it->what == IT_COMPOSITION)
26119 /* A dynamic (automatic) composition. */
26120 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26121 Lisp_Object gstring;
26122 struct font_metrics metrics;
26124 it->nglyphs = 1;
26126 gstring = composition_gstring_from_id (it->cmp_it.id);
26127 it->pixel_width
26128 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26129 &metrics);
26130 if (it->glyph_row
26131 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26132 it->glyph_row->contains_overlapping_glyphs_p = 1;
26133 it->ascent = it->phys_ascent = metrics.ascent;
26134 it->descent = it->phys_descent = metrics.descent;
26135 if (face->box != FACE_NO_BOX)
26137 int thick = face->box_line_width;
26139 if (thick > 0)
26141 it->ascent += thick;
26142 it->descent += thick;
26144 else
26145 thick = - thick;
26147 if (it->start_of_box_run_p)
26148 it->pixel_width += thick;
26149 if (it->end_of_box_run_p)
26150 it->pixel_width += thick;
26152 /* If face has an overline, add the height of the overline
26153 (1 pixel) and a 1 pixel margin to the character height. */
26154 if (face->overline_p)
26155 it->ascent += overline_margin;
26156 take_vertical_position_into_account (it);
26157 if (it->ascent < 0)
26158 it->ascent = 0;
26159 if (it->descent < 0)
26160 it->descent = 0;
26162 if (it->glyph_row)
26163 append_composite_glyph (it);
26165 else if (it->what == IT_GLYPHLESS)
26166 produce_glyphless_glyph (it, 0, Qnil);
26167 else if (it->what == IT_IMAGE)
26168 produce_image_glyph (it);
26169 else if (it->what == IT_STRETCH)
26170 produce_stretch_glyph (it);
26172 done:
26173 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26174 because this isn't true for images with `:ascent 100'. */
26175 eassert (it->ascent >= 0 && it->descent >= 0);
26176 if (it->area == TEXT_AREA)
26177 it->current_x += it->pixel_width;
26179 if (extra_line_spacing > 0)
26181 it->descent += extra_line_spacing;
26182 if (extra_line_spacing > it->max_extra_line_spacing)
26183 it->max_extra_line_spacing = extra_line_spacing;
26186 it->max_ascent = max (it->max_ascent, it->ascent);
26187 it->max_descent = max (it->max_descent, it->descent);
26188 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26189 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26192 /* EXPORT for RIF:
26193 Output LEN glyphs starting at START at the nominal cursor position.
26194 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26195 being updated, and UPDATED_AREA is the area of that row being updated. */
26197 void
26198 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26199 struct glyph *start, enum glyph_row_area updated_area, int len)
26201 int x, hpos, chpos = w->phys_cursor.hpos;
26203 eassert (updated_row);
26204 /* When the window is hscrolled, cursor hpos can legitimately be out
26205 of bounds, but we draw the cursor at the corresponding window
26206 margin in that case. */
26207 if (!updated_row->reversed_p && chpos < 0)
26208 chpos = 0;
26209 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26210 chpos = updated_row->used[TEXT_AREA] - 1;
26212 block_input ();
26214 /* Write glyphs. */
26216 hpos = start - updated_row->glyphs[updated_area];
26217 x = draw_glyphs (w, w->output_cursor.x,
26218 updated_row, updated_area,
26219 hpos, hpos + len,
26220 DRAW_NORMAL_TEXT, 0);
26222 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26223 if (updated_area == TEXT_AREA
26224 && w->phys_cursor_on_p
26225 && w->phys_cursor.vpos == w->output_cursor.vpos
26226 && chpos >= hpos
26227 && chpos < hpos + len)
26228 w->phys_cursor_on_p = 0;
26230 unblock_input ();
26232 /* Advance the output cursor. */
26233 w->output_cursor.hpos += len;
26234 w->output_cursor.x = x;
26238 /* EXPORT for RIF:
26239 Insert LEN glyphs from START at the nominal cursor position. */
26241 void
26242 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26243 struct glyph *start, enum glyph_row_area updated_area, int len)
26245 struct frame *f;
26246 int line_height, shift_by_width, shifted_region_width;
26247 struct glyph_row *row;
26248 struct glyph *glyph;
26249 int frame_x, frame_y;
26250 ptrdiff_t hpos;
26252 eassert (updated_row);
26253 block_input ();
26254 f = XFRAME (WINDOW_FRAME (w));
26256 /* Get the height of the line we are in. */
26257 row = updated_row;
26258 line_height = row->height;
26260 /* Get the width of the glyphs to insert. */
26261 shift_by_width = 0;
26262 for (glyph = start; glyph < start + len; ++glyph)
26263 shift_by_width += glyph->pixel_width;
26265 /* Get the width of the region to shift right. */
26266 shifted_region_width = (window_box_width (w, updated_area)
26267 - w->output_cursor.x
26268 - shift_by_width);
26270 /* Shift right. */
26271 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26272 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26274 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26275 line_height, shift_by_width);
26277 /* Write the glyphs. */
26278 hpos = start - row->glyphs[updated_area];
26279 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26280 hpos, hpos + len,
26281 DRAW_NORMAL_TEXT, 0);
26283 /* Advance the output cursor. */
26284 w->output_cursor.hpos += len;
26285 w->output_cursor.x += shift_by_width;
26286 unblock_input ();
26290 /* EXPORT for RIF:
26291 Erase the current text line from the nominal cursor position
26292 (inclusive) to pixel column TO_X (exclusive). The idea is that
26293 everything from TO_X onward is already erased.
26295 TO_X is a pixel position relative to UPDATED_AREA of currently
26296 updated window W. TO_X == -1 means clear to the end of this area. */
26298 void
26299 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26300 enum glyph_row_area updated_area, int to_x)
26302 struct frame *f;
26303 int max_x, min_y, max_y;
26304 int from_x, from_y, to_y;
26306 eassert (updated_row);
26307 f = XFRAME (w->frame);
26309 if (updated_row->full_width_p)
26310 max_x = (WINDOW_PIXEL_WIDTH (w)
26311 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26312 else
26313 max_x = window_box_width (w, updated_area);
26314 max_y = window_text_bottom_y (w);
26316 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26317 of window. For TO_X > 0, truncate to end of drawing area. */
26318 if (to_x == 0)
26319 return;
26320 else if (to_x < 0)
26321 to_x = max_x;
26322 else
26323 to_x = min (to_x, max_x);
26325 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26327 /* Notice if the cursor will be cleared by this operation. */
26328 if (!updated_row->full_width_p)
26329 notice_overwritten_cursor (w, updated_area,
26330 w->output_cursor.x, -1,
26331 updated_row->y,
26332 MATRIX_ROW_BOTTOM_Y (updated_row));
26334 from_x = w->output_cursor.x;
26336 /* Translate to frame coordinates. */
26337 if (updated_row->full_width_p)
26339 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26340 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26342 else
26344 int area_left = window_box_left (w, updated_area);
26345 from_x += area_left;
26346 to_x += area_left;
26349 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26350 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26351 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26353 /* Prevent inadvertently clearing to end of the X window. */
26354 if (to_x > from_x && to_y > from_y)
26356 block_input ();
26357 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26358 to_x - from_x, to_y - from_y);
26359 unblock_input ();
26363 #endif /* HAVE_WINDOW_SYSTEM */
26367 /***********************************************************************
26368 Cursor types
26369 ***********************************************************************/
26371 /* Value is the internal representation of the specified cursor type
26372 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26373 of the bar cursor. */
26375 static enum text_cursor_kinds
26376 get_specified_cursor_type (Lisp_Object arg, int *width)
26378 enum text_cursor_kinds type;
26380 if (NILP (arg))
26381 return NO_CURSOR;
26383 if (EQ (arg, Qbox))
26384 return FILLED_BOX_CURSOR;
26386 if (EQ (arg, Qhollow))
26387 return HOLLOW_BOX_CURSOR;
26389 if (EQ (arg, Qbar))
26391 *width = 2;
26392 return BAR_CURSOR;
26395 if (CONSP (arg)
26396 && EQ (XCAR (arg), Qbar)
26397 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26399 *width = XINT (XCDR (arg));
26400 return BAR_CURSOR;
26403 if (EQ (arg, Qhbar))
26405 *width = 2;
26406 return HBAR_CURSOR;
26409 if (CONSP (arg)
26410 && EQ (XCAR (arg), Qhbar)
26411 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26413 *width = XINT (XCDR (arg));
26414 return HBAR_CURSOR;
26417 /* Treat anything unknown as "hollow box cursor".
26418 It was bad to signal an error; people have trouble fixing
26419 .Xdefaults with Emacs, when it has something bad in it. */
26420 type = HOLLOW_BOX_CURSOR;
26422 return type;
26425 /* Set the default cursor types for specified frame. */
26426 void
26427 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26429 int width = 1;
26430 Lisp_Object tem;
26432 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26433 FRAME_CURSOR_WIDTH (f) = width;
26435 /* By default, set up the blink-off state depending on the on-state. */
26437 tem = Fassoc (arg, Vblink_cursor_alist);
26438 if (!NILP (tem))
26440 FRAME_BLINK_OFF_CURSOR (f)
26441 = get_specified_cursor_type (XCDR (tem), &width);
26442 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26444 else
26445 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26447 /* Make sure the cursor gets redrawn. */
26448 f->cursor_type_changed = 1;
26452 #ifdef HAVE_WINDOW_SYSTEM
26454 /* Return the cursor we want to be displayed in window W. Return
26455 width of bar/hbar cursor through WIDTH arg. Return with
26456 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26457 (i.e. if the `system caret' should track this cursor).
26459 In a mini-buffer window, we want the cursor only to appear if we
26460 are reading input from this window. For the selected window, we
26461 want the cursor type given by the frame parameter or buffer local
26462 setting of cursor-type. If explicitly marked off, draw no cursor.
26463 In all other cases, we want a hollow box cursor. */
26465 static enum text_cursor_kinds
26466 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26467 int *active_cursor)
26469 struct frame *f = XFRAME (w->frame);
26470 struct buffer *b = XBUFFER (w->contents);
26471 int cursor_type = DEFAULT_CURSOR;
26472 Lisp_Object alt_cursor;
26473 int non_selected = 0;
26475 *active_cursor = 1;
26477 /* Echo area */
26478 if (cursor_in_echo_area
26479 && FRAME_HAS_MINIBUF_P (f)
26480 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26482 if (w == XWINDOW (echo_area_window))
26484 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26486 *width = FRAME_CURSOR_WIDTH (f);
26487 return FRAME_DESIRED_CURSOR (f);
26489 else
26490 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26493 *active_cursor = 0;
26494 non_selected = 1;
26497 /* Detect a nonselected window or nonselected frame. */
26498 else if (w != XWINDOW (f->selected_window)
26499 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26501 *active_cursor = 0;
26503 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26504 return NO_CURSOR;
26506 non_selected = 1;
26509 /* Never display a cursor in a window in which cursor-type is nil. */
26510 if (NILP (BVAR (b, cursor_type)))
26511 return NO_CURSOR;
26513 /* Get the normal cursor type for this window. */
26514 if (EQ (BVAR (b, cursor_type), Qt))
26516 cursor_type = FRAME_DESIRED_CURSOR (f);
26517 *width = FRAME_CURSOR_WIDTH (f);
26519 else
26520 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26522 /* Use cursor-in-non-selected-windows instead
26523 for non-selected window or frame. */
26524 if (non_selected)
26526 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26527 if (!EQ (Qt, alt_cursor))
26528 return get_specified_cursor_type (alt_cursor, width);
26529 /* t means modify the normal cursor type. */
26530 if (cursor_type == FILLED_BOX_CURSOR)
26531 cursor_type = HOLLOW_BOX_CURSOR;
26532 else if (cursor_type == BAR_CURSOR && *width > 1)
26533 --*width;
26534 return cursor_type;
26537 /* Use normal cursor if not blinked off. */
26538 if (!w->cursor_off_p)
26540 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26542 if (cursor_type == FILLED_BOX_CURSOR)
26544 /* Using a block cursor on large images can be very annoying.
26545 So use a hollow cursor for "large" images.
26546 If image is not transparent (no mask), also use hollow cursor. */
26547 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26548 if (img != NULL && IMAGEP (img->spec))
26550 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26551 where N = size of default frame font size.
26552 This should cover most of the "tiny" icons people may use. */
26553 if (!img->mask
26554 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26555 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26556 cursor_type = HOLLOW_BOX_CURSOR;
26559 else if (cursor_type != NO_CURSOR)
26561 /* Display current only supports BOX and HOLLOW cursors for images.
26562 So for now, unconditionally use a HOLLOW cursor when cursor is
26563 not a solid box cursor. */
26564 cursor_type = HOLLOW_BOX_CURSOR;
26567 return cursor_type;
26570 /* Cursor is blinked off, so determine how to "toggle" it. */
26572 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26573 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26574 return get_specified_cursor_type (XCDR (alt_cursor), width);
26576 /* Then see if frame has specified a specific blink off cursor type. */
26577 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26579 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26580 return FRAME_BLINK_OFF_CURSOR (f);
26583 #if 0
26584 /* Some people liked having a permanently visible blinking cursor,
26585 while others had very strong opinions against it. So it was
26586 decided to remove it. KFS 2003-09-03 */
26588 /* Finally perform built-in cursor blinking:
26589 filled box <-> hollow box
26590 wide [h]bar <-> narrow [h]bar
26591 narrow [h]bar <-> no cursor
26592 other type <-> no cursor */
26594 if (cursor_type == FILLED_BOX_CURSOR)
26595 return HOLLOW_BOX_CURSOR;
26597 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26599 *width = 1;
26600 return cursor_type;
26602 #endif
26604 return NO_CURSOR;
26608 /* Notice when the text cursor of window W has been completely
26609 overwritten by a drawing operation that outputs glyphs in AREA
26610 starting at X0 and ending at X1 in the line starting at Y0 and
26611 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26612 the rest of the line after X0 has been written. Y coordinates
26613 are window-relative. */
26615 static void
26616 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26617 int x0, int x1, int y0, int y1)
26619 int cx0, cx1, cy0, cy1;
26620 struct glyph_row *row;
26622 if (!w->phys_cursor_on_p)
26623 return;
26624 if (area != TEXT_AREA)
26625 return;
26627 if (w->phys_cursor.vpos < 0
26628 || w->phys_cursor.vpos >= w->current_matrix->nrows
26629 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26630 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26631 return;
26633 if (row->cursor_in_fringe_p)
26635 row->cursor_in_fringe_p = 0;
26636 draw_fringe_bitmap (w, row, row->reversed_p);
26637 w->phys_cursor_on_p = 0;
26638 return;
26641 cx0 = w->phys_cursor.x;
26642 cx1 = cx0 + w->phys_cursor_width;
26643 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26644 return;
26646 /* The cursor image will be completely removed from the
26647 screen if the output area intersects the cursor area in
26648 y-direction. When we draw in [y0 y1[, and some part of
26649 the cursor is at y < y0, that part must have been drawn
26650 before. When scrolling, the cursor is erased before
26651 actually scrolling, so we don't come here. When not
26652 scrolling, the rows above the old cursor row must have
26653 changed, and in this case these rows must have written
26654 over the cursor image.
26656 Likewise if part of the cursor is below y1, with the
26657 exception of the cursor being in the first blank row at
26658 the buffer and window end because update_text_area
26659 doesn't draw that row. (Except when it does, but
26660 that's handled in update_text_area.) */
26662 cy0 = w->phys_cursor.y;
26663 cy1 = cy0 + w->phys_cursor_height;
26664 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26665 return;
26667 w->phys_cursor_on_p = 0;
26670 #endif /* HAVE_WINDOW_SYSTEM */
26673 /************************************************************************
26674 Mouse Face
26675 ************************************************************************/
26677 #ifdef HAVE_WINDOW_SYSTEM
26679 /* EXPORT for RIF:
26680 Fix the display of area AREA of overlapping row ROW in window W
26681 with respect to the overlapping part OVERLAPS. */
26683 void
26684 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26685 enum glyph_row_area area, int overlaps)
26687 int i, x;
26689 block_input ();
26691 x = 0;
26692 for (i = 0; i < row->used[area];)
26694 if (row->glyphs[area][i].overlaps_vertically_p)
26696 int start = i, start_x = x;
26700 x += row->glyphs[area][i].pixel_width;
26701 ++i;
26703 while (i < row->used[area]
26704 && row->glyphs[area][i].overlaps_vertically_p);
26706 draw_glyphs (w, start_x, row, area,
26707 start, i,
26708 DRAW_NORMAL_TEXT, overlaps);
26710 else
26712 x += row->glyphs[area][i].pixel_width;
26713 ++i;
26717 unblock_input ();
26721 /* EXPORT:
26722 Draw the cursor glyph of window W in glyph row ROW. See the
26723 comment of draw_glyphs for the meaning of HL. */
26725 void
26726 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26727 enum draw_glyphs_face hl)
26729 /* If cursor hpos is out of bounds, don't draw garbage. This can
26730 happen in mini-buffer windows when switching between echo area
26731 glyphs and mini-buffer. */
26732 if ((row->reversed_p
26733 ? (w->phys_cursor.hpos >= 0)
26734 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26736 int on_p = w->phys_cursor_on_p;
26737 int x1;
26738 int hpos = w->phys_cursor.hpos;
26740 /* When the window is hscrolled, cursor hpos can legitimately be
26741 out of bounds, but we draw the cursor at the corresponding
26742 window margin in that case. */
26743 if (!row->reversed_p && hpos < 0)
26744 hpos = 0;
26745 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26746 hpos = row->used[TEXT_AREA] - 1;
26748 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26749 hl, 0);
26750 w->phys_cursor_on_p = on_p;
26752 if (hl == DRAW_CURSOR)
26753 w->phys_cursor_width = x1 - w->phys_cursor.x;
26754 /* When we erase the cursor, and ROW is overlapped by other
26755 rows, make sure that these overlapping parts of other rows
26756 are redrawn. */
26757 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26759 w->phys_cursor_width = x1 - w->phys_cursor.x;
26761 if (row > w->current_matrix->rows
26762 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26763 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26764 OVERLAPS_ERASED_CURSOR);
26766 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26767 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26768 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26769 OVERLAPS_ERASED_CURSOR);
26775 /* Erase the image of a cursor of window W from the screen. */
26777 #ifndef HAVE_NTGUI
26778 static
26779 #endif
26780 void
26781 erase_phys_cursor (struct window *w)
26783 struct frame *f = XFRAME (w->frame);
26784 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26785 int hpos = w->phys_cursor.hpos;
26786 int vpos = w->phys_cursor.vpos;
26787 int mouse_face_here_p = 0;
26788 struct glyph_matrix *active_glyphs = w->current_matrix;
26789 struct glyph_row *cursor_row;
26790 struct glyph *cursor_glyph;
26791 enum draw_glyphs_face hl;
26793 /* No cursor displayed or row invalidated => nothing to do on the
26794 screen. */
26795 if (w->phys_cursor_type == NO_CURSOR)
26796 goto mark_cursor_off;
26798 /* VPOS >= active_glyphs->nrows means that window has been resized.
26799 Don't bother to erase the cursor. */
26800 if (vpos >= active_glyphs->nrows)
26801 goto mark_cursor_off;
26803 /* If row containing cursor is marked invalid, there is nothing we
26804 can do. */
26805 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26806 if (!cursor_row->enabled_p)
26807 goto mark_cursor_off;
26809 /* If line spacing is > 0, old cursor may only be partially visible in
26810 window after split-window. So adjust visible height. */
26811 cursor_row->visible_height = min (cursor_row->visible_height,
26812 window_text_bottom_y (w) - cursor_row->y);
26814 /* If row is completely invisible, don't attempt to delete a cursor which
26815 isn't there. This can happen if cursor is at top of a window, and
26816 we switch to a buffer with a header line in that window. */
26817 if (cursor_row->visible_height <= 0)
26818 goto mark_cursor_off;
26820 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26821 if (cursor_row->cursor_in_fringe_p)
26823 cursor_row->cursor_in_fringe_p = 0;
26824 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26825 goto mark_cursor_off;
26828 /* This can happen when the new row is shorter than the old one.
26829 In this case, either draw_glyphs or clear_end_of_line
26830 should have cleared the cursor. Note that we wouldn't be
26831 able to erase the cursor in this case because we don't have a
26832 cursor glyph at hand. */
26833 if ((cursor_row->reversed_p
26834 ? (w->phys_cursor.hpos < 0)
26835 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26836 goto mark_cursor_off;
26838 /* When the window is hscrolled, cursor hpos can legitimately be out
26839 of bounds, but we draw the cursor at the corresponding window
26840 margin in that case. */
26841 if (!cursor_row->reversed_p && hpos < 0)
26842 hpos = 0;
26843 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26844 hpos = cursor_row->used[TEXT_AREA] - 1;
26846 /* If the cursor is in the mouse face area, redisplay that when
26847 we clear the cursor. */
26848 if (! NILP (hlinfo->mouse_face_window)
26849 && coords_in_mouse_face_p (w, hpos, vpos)
26850 /* Don't redraw the cursor's spot in mouse face if it is at the
26851 end of a line (on a newline). The cursor appears there, but
26852 mouse highlighting does not. */
26853 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26854 mouse_face_here_p = 1;
26856 /* Maybe clear the display under the cursor. */
26857 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26859 int x, y, left_x;
26860 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26861 int width;
26863 cursor_glyph = get_phys_cursor_glyph (w);
26864 if (cursor_glyph == NULL)
26865 goto mark_cursor_off;
26867 width = cursor_glyph->pixel_width;
26868 left_x = window_box_left_offset (w, TEXT_AREA);
26869 x = w->phys_cursor.x;
26870 if (x < left_x)
26871 width -= left_x - x;
26872 width = min (width, window_box_width (w, TEXT_AREA) - x);
26873 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26874 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26876 if (width > 0)
26877 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26880 /* Erase the cursor by redrawing the character underneath it. */
26881 if (mouse_face_here_p)
26882 hl = DRAW_MOUSE_FACE;
26883 else
26884 hl = DRAW_NORMAL_TEXT;
26885 draw_phys_cursor_glyph (w, cursor_row, hl);
26887 mark_cursor_off:
26888 w->phys_cursor_on_p = 0;
26889 w->phys_cursor_type = NO_CURSOR;
26893 /* EXPORT:
26894 Display or clear cursor of window W. If ON is zero, clear the
26895 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26896 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26898 void
26899 display_and_set_cursor (struct window *w, bool on,
26900 int hpos, int vpos, int x, int y)
26902 struct frame *f = XFRAME (w->frame);
26903 int new_cursor_type;
26904 int new_cursor_width;
26905 int active_cursor;
26906 struct glyph_row *glyph_row;
26907 struct glyph *glyph;
26909 /* This is pointless on invisible frames, and dangerous on garbaged
26910 windows and frames; in the latter case, the frame or window may
26911 be in the midst of changing its size, and x and y may be off the
26912 window. */
26913 if (! FRAME_VISIBLE_P (f)
26914 || FRAME_GARBAGED_P (f)
26915 || vpos >= w->current_matrix->nrows
26916 || hpos >= w->current_matrix->matrix_w)
26917 return;
26919 /* If cursor is off and we want it off, return quickly. */
26920 if (!on && !w->phys_cursor_on_p)
26921 return;
26923 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26924 /* If cursor row is not enabled, we don't really know where to
26925 display the cursor. */
26926 if (!glyph_row->enabled_p)
26928 w->phys_cursor_on_p = 0;
26929 return;
26932 glyph = NULL;
26933 if (!glyph_row->exact_window_width_line_p
26934 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26935 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26937 eassert (input_blocked_p ());
26939 /* Set new_cursor_type to the cursor we want to be displayed. */
26940 new_cursor_type = get_window_cursor_type (w, glyph,
26941 &new_cursor_width, &active_cursor);
26943 /* If cursor is currently being shown and we don't want it to be or
26944 it is in the wrong place, or the cursor type is not what we want,
26945 erase it. */
26946 if (w->phys_cursor_on_p
26947 && (!on
26948 || w->phys_cursor.x != x
26949 || w->phys_cursor.y != y
26950 || new_cursor_type != w->phys_cursor_type
26951 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26952 && new_cursor_width != w->phys_cursor_width)))
26953 erase_phys_cursor (w);
26955 /* Don't check phys_cursor_on_p here because that flag is only set
26956 to zero in some cases where we know that the cursor has been
26957 completely erased, to avoid the extra work of erasing the cursor
26958 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26959 still not be visible, or it has only been partly erased. */
26960 if (on)
26962 w->phys_cursor_ascent = glyph_row->ascent;
26963 w->phys_cursor_height = glyph_row->height;
26965 /* Set phys_cursor_.* before x_draw_.* is called because some
26966 of them may need the information. */
26967 w->phys_cursor.x = x;
26968 w->phys_cursor.y = glyph_row->y;
26969 w->phys_cursor.hpos = hpos;
26970 w->phys_cursor.vpos = vpos;
26973 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26974 new_cursor_type, new_cursor_width,
26975 on, active_cursor);
26979 /* Switch the display of W's cursor on or off, according to the value
26980 of ON. */
26982 static void
26983 update_window_cursor (struct window *w, bool on)
26985 /* Don't update cursor in windows whose frame is in the process
26986 of being deleted. */
26987 if (w->current_matrix)
26989 int hpos = w->phys_cursor.hpos;
26990 int vpos = w->phys_cursor.vpos;
26991 struct glyph_row *row;
26993 if (vpos >= w->current_matrix->nrows
26994 || hpos >= w->current_matrix->matrix_w)
26995 return;
26997 row = MATRIX_ROW (w->current_matrix, vpos);
26999 /* When the window is hscrolled, cursor hpos can legitimately be
27000 out of bounds, but we draw the cursor at the corresponding
27001 window margin in that case. */
27002 if (!row->reversed_p && hpos < 0)
27003 hpos = 0;
27004 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27005 hpos = row->used[TEXT_AREA] - 1;
27007 block_input ();
27008 display_and_set_cursor (w, on, hpos, vpos,
27009 w->phys_cursor.x, w->phys_cursor.y);
27010 unblock_input ();
27015 /* Call update_window_cursor with parameter ON_P on all leaf windows
27016 in the window tree rooted at W. */
27018 static void
27019 update_cursor_in_window_tree (struct window *w, bool on_p)
27021 while (w)
27023 if (WINDOWP (w->contents))
27024 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27025 else
27026 update_window_cursor (w, on_p);
27028 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27033 /* EXPORT:
27034 Display the cursor on window W, or clear it, according to ON_P.
27035 Don't change the cursor's position. */
27037 void
27038 x_update_cursor (struct frame *f, bool on_p)
27040 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27044 /* EXPORT:
27045 Clear the cursor of window W to background color, and mark the
27046 cursor as not shown. This is used when the text where the cursor
27047 is about to be rewritten. */
27049 void
27050 x_clear_cursor (struct window *w)
27052 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27053 update_window_cursor (w, 0);
27056 #endif /* HAVE_WINDOW_SYSTEM */
27058 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27059 and MSDOS. */
27060 static void
27061 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27062 int start_hpos, int end_hpos,
27063 enum draw_glyphs_face draw)
27065 #ifdef HAVE_WINDOW_SYSTEM
27066 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27068 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27069 return;
27071 #endif
27072 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27073 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27074 #endif
27077 /* Display the active region described by mouse_face_* according to DRAW. */
27079 static void
27080 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27082 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27083 struct frame *f = XFRAME (WINDOW_FRAME (w));
27085 if (/* If window is in the process of being destroyed, don't bother
27086 to do anything. */
27087 w->current_matrix != NULL
27088 /* Don't update mouse highlight if hidden */
27089 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27090 /* Recognize when we are called to operate on rows that don't exist
27091 anymore. This can happen when a window is split. */
27092 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27094 int phys_cursor_on_p = w->phys_cursor_on_p;
27095 struct glyph_row *row, *first, *last;
27097 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27098 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27100 for (row = first; row <= last && row->enabled_p; ++row)
27102 int start_hpos, end_hpos, start_x;
27104 /* For all but the first row, the highlight starts at column 0. */
27105 if (row == first)
27107 /* R2L rows have BEG and END in reversed order, but the
27108 screen drawing geometry is always left to right. So
27109 we need to mirror the beginning and end of the
27110 highlighted area in R2L rows. */
27111 if (!row->reversed_p)
27113 start_hpos = hlinfo->mouse_face_beg_col;
27114 start_x = hlinfo->mouse_face_beg_x;
27116 else if (row == last)
27118 start_hpos = hlinfo->mouse_face_end_col;
27119 start_x = hlinfo->mouse_face_end_x;
27121 else
27123 start_hpos = 0;
27124 start_x = 0;
27127 else if (row->reversed_p && row == last)
27129 start_hpos = hlinfo->mouse_face_end_col;
27130 start_x = hlinfo->mouse_face_end_x;
27132 else
27134 start_hpos = 0;
27135 start_x = 0;
27138 if (row == last)
27140 if (!row->reversed_p)
27141 end_hpos = hlinfo->mouse_face_end_col;
27142 else if (row == first)
27143 end_hpos = hlinfo->mouse_face_beg_col;
27144 else
27146 end_hpos = row->used[TEXT_AREA];
27147 if (draw == DRAW_NORMAL_TEXT)
27148 row->fill_line_p = 1; /* Clear to end of line */
27151 else if (row->reversed_p && row == first)
27152 end_hpos = hlinfo->mouse_face_beg_col;
27153 else
27155 end_hpos = row->used[TEXT_AREA];
27156 if (draw == DRAW_NORMAL_TEXT)
27157 row->fill_line_p = 1; /* Clear to end of line */
27160 if (end_hpos > start_hpos)
27162 draw_row_with_mouse_face (w, start_x, row,
27163 start_hpos, end_hpos, draw);
27165 row->mouse_face_p
27166 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27170 #ifdef HAVE_WINDOW_SYSTEM
27171 /* When we've written over the cursor, arrange for it to
27172 be displayed again. */
27173 if (FRAME_WINDOW_P (f)
27174 && phys_cursor_on_p && !w->phys_cursor_on_p)
27176 int hpos = w->phys_cursor.hpos;
27178 /* When the window is hscrolled, cursor hpos can legitimately be
27179 out of bounds, but we draw the cursor at the corresponding
27180 window margin in that case. */
27181 if (!row->reversed_p && hpos < 0)
27182 hpos = 0;
27183 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27184 hpos = row->used[TEXT_AREA] - 1;
27186 block_input ();
27187 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27188 w->phys_cursor.x, w->phys_cursor.y);
27189 unblock_input ();
27191 #endif /* HAVE_WINDOW_SYSTEM */
27194 #ifdef HAVE_WINDOW_SYSTEM
27195 /* Change the mouse cursor. */
27196 if (FRAME_WINDOW_P (f))
27198 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27199 if (draw == DRAW_NORMAL_TEXT
27200 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27201 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27202 else
27203 #endif
27204 if (draw == DRAW_MOUSE_FACE)
27205 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27206 else
27207 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27209 #endif /* HAVE_WINDOW_SYSTEM */
27212 /* EXPORT:
27213 Clear out the mouse-highlighted active region.
27214 Redraw it un-highlighted first. Value is non-zero if mouse
27215 face was actually drawn unhighlighted. */
27218 clear_mouse_face (Mouse_HLInfo *hlinfo)
27220 int cleared = 0;
27222 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27224 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27225 cleared = 1;
27228 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27229 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27230 hlinfo->mouse_face_window = Qnil;
27231 hlinfo->mouse_face_overlay = Qnil;
27232 return cleared;
27235 /* Return true if the coordinates HPOS and VPOS on windows W are
27236 within the mouse face on that window. */
27237 static bool
27238 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27240 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27242 /* Quickly resolve the easy cases. */
27243 if (!(WINDOWP (hlinfo->mouse_face_window)
27244 && XWINDOW (hlinfo->mouse_face_window) == w))
27245 return false;
27246 if (vpos < hlinfo->mouse_face_beg_row
27247 || vpos > hlinfo->mouse_face_end_row)
27248 return false;
27249 if (vpos > hlinfo->mouse_face_beg_row
27250 && vpos < hlinfo->mouse_face_end_row)
27251 return true;
27253 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27255 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27257 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27258 return true;
27260 else if ((vpos == hlinfo->mouse_face_beg_row
27261 && hpos >= hlinfo->mouse_face_beg_col)
27262 || (vpos == hlinfo->mouse_face_end_row
27263 && hpos < hlinfo->mouse_face_end_col))
27264 return true;
27266 else
27268 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27270 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27271 return true;
27273 else if ((vpos == hlinfo->mouse_face_beg_row
27274 && hpos <= hlinfo->mouse_face_beg_col)
27275 || (vpos == hlinfo->mouse_face_end_row
27276 && hpos > hlinfo->mouse_face_end_col))
27277 return true;
27279 return false;
27283 /* EXPORT:
27284 True if physical cursor of window W is within mouse face. */
27286 bool
27287 cursor_in_mouse_face_p (struct window *w)
27289 int hpos = w->phys_cursor.hpos;
27290 int vpos = w->phys_cursor.vpos;
27291 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27293 /* When the window is hscrolled, cursor hpos can legitimately be out
27294 of bounds, but we draw the cursor at the corresponding window
27295 margin in that case. */
27296 if (!row->reversed_p && hpos < 0)
27297 hpos = 0;
27298 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27299 hpos = row->used[TEXT_AREA] - 1;
27301 return coords_in_mouse_face_p (w, hpos, vpos);
27306 /* Find the glyph rows START_ROW and END_ROW of window W that display
27307 characters between buffer positions START_CHARPOS and END_CHARPOS
27308 (excluding END_CHARPOS). DISP_STRING is a display string that
27309 covers these buffer positions. This is similar to
27310 row_containing_pos, but is more accurate when bidi reordering makes
27311 buffer positions change non-linearly with glyph rows. */
27312 static void
27313 rows_from_pos_range (struct window *w,
27314 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27315 Lisp_Object disp_string,
27316 struct glyph_row **start, struct glyph_row **end)
27318 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27319 int last_y = window_text_bottom_y (w);
27320 struct glyph_row *row;
27322 *start = NULL;
27323 *end = NULL;
27325 while (!first->enabled_p
27326 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27327 first++;
27329 /* Find the START row. */
27330 for (row = first;
27331 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27332 row++)
27334 /* A row can potentially be the START row if the range of the
27335 characters it displays intersects the range
27336 [START_CHARPOS..END_CHARPOS). */
27337 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27338 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27339 /* See the commentary in row_containing_pos, for the
27340 explanation of the complicated way to check whether
27341 some position is beyond the end of the characters
27342 displayed by a row. */
27343 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27344 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27345 && !row->ends_at_zv_p
27346 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27347 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27348 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27349 && !row->ends_at_zv_p
27350 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27352 /* Found a candidate row. Now make sure at least one of the
27353 glyphs it displays has a charpos from the range
27354 [START_CHARPOS..END_CHARPOS).
27356 This is not obvious because bidi reordering could make
27357 buffer positions of a row be 1,2,3,102,101,100, and if we
27358 want to highlight characters in [50..60), we don't want
27359 this row, even though [50..60) does intersect [1..103),
27360 the range of character positions given by the row's start
27361 and end positions. */
27362 struct glyph *g = row->glyphs[TEXT_AREA];
27363 struct glyph *e = g + row->used[TEXT_AREA];
27365 while (g < e)
27367 if (((BUFFERP (g->object) || INTEGERP (g->object))
27368 && start_charpos <= g->charpos && g->charpos < end_charpos)
27369 /* A glyph that comes from DISP_STRING is by
27370 definition to be highlighted. */
27371 || EQ (g->object, disp_string))
27372 *start = row;
27373 g++;
27375 if (*start)
27376 break;
27380 /* Find the END row. */
27381 if (!*start
27382 /* If the last row is partially visible, start looking for END
27383 from that row, instead of starting from FIRST. */
27384 && !(row->enabled_p
27385 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27386 row = first;
27387 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27389 struct glyph_row *next = row + 1;
27390 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27392 if (!next->enabled_p
27393 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27394 /* The first row >= START whose range of displayed characters
27395 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27396 is the row END + 1. */
27397 || (start_charpos < next_start
27398 && end_charpos < next_start)
27399 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27400 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27401 && !next->ends_at_zv_p
27402 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27403 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27404 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27405 && !next->ends_at_zv_p
27406 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27408 *end = row;
27409 break;
27411 else
27413 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27414 but none of the characters it displays are in the range, it is
27415 also END + 1. */
27416 struct glyph *g = next->glyphs[TEXT_AREA];
27417 struct glyph *s = g;
27418 struct glyph *e = g + next->used[TEXT_AREA];
27420 while (g < e)
27422 if (((BUFFERP (g->object) || INTEGERP (g->object))
27423 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27424 /* If the buffer position of the first glyph in
27425 the row is equal to END_CHARPOS, it means
27426 the last character to be highlighted is the
27427 newline of ROW, and we must consider NEXT as
27428 END, not END+1. */
27429 || (((!next->reversed_p && g == s)
27430 || (next->reversed_p && g == e - 1))
27431 && (g->charpos == end_charpos
27432 /* Special case for when NEXT is an
27433 empty line at ZV. */
27434 || (g->charpos == -1
27435 && !row->ends_at_zv_p
27436 && next_start == end_charpos)))))
27437 /* A glyph that comes from DISP_STRING is by
27438 definition to be highlighted. */
27439 || EQ (g->object, disp_string))
27440 break;
27441 g++;
27443 if (g == e)
27445 *end = row;
27446 break;
27448 /* The first row that ends at ZV must be the last to be
27449 highlighted. */
27450 else if (next->ends_at_zv_p)
27452 *end = next;
27453 break;
27459 /* This function sets the mouse_face_* elements of HLINFO, assuming
27460 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27461 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27462 for the overlay or run of text properties specifying the mouse
27463 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27464 before-string and after-string that must also be highlighted.
27465 DISP_STRING, if non-nil, is a display string that may cover some
27466 or all of the highlighted text. */
27468 static void
27469 mouse_face_from_buffer_pos (Lisp_Object window,
27470 Mouse_HLInfo *hlinfo,
27471 ptrdiff_t mouse_charpos,
27472 ptrdiff_t start_charpos,
27473 ptrdiff_t end_charpos,
27474 Lisp_Object before_string,
27475 Lisp_Object after_string,
27476 Lisp_Object disp_string)
27478 struct window *w = XWINDOW (window);
27479 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27480 struct glyph_row *r1, *r2;
27481 struct glyph *glyph, *end;
27482 ptrdiff_t ignore, pos;
27483 int x;
27485 eassert (NILP (disp_string) || STRINGP (disp_string));
27486 eassert (NILP (before_string) || STRINGP (before_string));
27487 eassert (NILP (after_string) || STRINGP (after_string));
27489 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27490 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27491 if (r1 == NULL)
27492 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27493 /* If the before-string or display-string contains newlines,
27494 rows_from_pos_range skips to its last row. Move back. */
27495 if (!NILP (before_string) || !NILP (disp_string))
27497 struct glyph_row *prev;
27498 while ((prev = r1 - 1, prev >= first)
27499 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27500 && prev->used[TEXT_AREA] > 0)
27502 struct glyph *beg = prev->glyphs[TEXT_AREA];
27503 glyph = beg + prev->used[TEXT_AREA];
27504 while (--glyph >= beg && INTEGERP (glyph->object));
27505 if (glyph < beg
27506 || !(EQ (glyph->object, before_string)
27507 || EQ (glyph->object, disp_string)))
27508 break;
27509 r1 = prev;
27512 if (r2 == NULL)
27514 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27515 hlinfo->mouse_face_past_end = 1;
27517 else if (!NILP (after_string))
27519 /* If the after-string has newlines, advance to its last row. */
27520 struct glyph_row *next;
27521 struct glyph_row *last
27522 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27524 for (next = r2 + 1;
27525 next <= last
27526 && next->used[TEXT_AREA] > 0
27527 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27528 ++next)
27529 r2 = next;
27531 /* The rest of the display engine assumes that mouse_face_beg_row is
27532 either above mouse_face_end_row or identical to it. But with
27533 bidi-reordered continued lines, the row for START_CHARPOS could
27534 be below the row for END_CHARPOS. If so, swap the rows and store
27535 them in correct order. */
27536 if (r1->y > r2->y)
27538 struct glyph_row *tem = r2;
27540 r2 = r1;
27541 r1 = tem;
27544 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27545 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27547 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27548 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27549 could be anywhere in the row and in any order. The strategy
27550 below is to find the leftmost and the rightmost glyph that
27551 belongs to either of these 3 strings, or whose position is
27552 between START_CHARPOS and END_CHARPOS, and highlight all the
27553 glyphs between those two. This may cover more than just the text
27554 between START_CHARPOS and END_CHARPOS if the range of characters
27555 strides the bidi level boundary, e.g. if the beginning is in R2L
27556 text while the end is in L2R text or vice versa. */
27557 if (!r1->reversed_p)
27559 /* This row is in a left to right paragraph. Scan it left to
27560 right. */
27561 glyph = r1->glyphs[TEXT_AREA];
27562 end = glyph + r1->used[TEXT_AREA];
27563 x = r1->x;
27565 /* Skip truncation glyphs at the start of the glyph row. */
27566 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27567 for (; glyph < end
27568 && INTEGERP (glyph->object)
27569 && glyph->charpos < 0;
27570 ++glyph)
27571 x += glyph->pixel_width;
27573 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27574 or DISP_STRING, and the first glyph from buffer whose
27575 position is between START_CHARPOS and END_CHARPOS. */
27576 for (; glyph < end
27577 && !INTEGERP (glyph->object)
27578 && !EQ (glyph->object, disp_string)
27579 && !(BUFFERP (glyph->object)
27580 && (glyph->charpos >= start_charpos
27581 && glyph->charpos < end_charpos));
27582 ++glyph)
27584 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27585 are present at buffer positions between START_CHARPOS and
27586 END_CHARPOS, or if they come from an overlay. */
27587 if (EQ (glyph->object, before_string))
27589 pos = string_buffer_position (before_string,
27590 start_charpos);
27591 /* If pos == 0, it means before_string came from an
27592 overlay, not from a buffer position. */
27593 if (!pos || (pos >= start_charpos && pos < end_charpos))
27594 break;
27596 else if (EQ (glyph->object, after_string))
27598 pos = string_buffer_position (after_string, end_charpos);
27599 if (!pos || (pos >= start_charpos && pos < end_charpos))
27600 break;
27602 x += glyph->pixel_width;
27604 hlinfo->mouse_face_beg_x = x;
27605 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27607 else
27609 /* This row is in a right to left paragraph. Scan it right to
27610 left. */
27611 struct glyph *g;
27613 end = r1->glyphs[TEXT_AREA] - 1;
27614 glyph = end + r1->used[TEXT_AREA];
27616 /* Skip truncation glyphs at the start of the glyph row. */
27617 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27618 for (; glyph > end
27619 && INTEGERP (glyph->object)
27620 && glyph->charpos < 0;
27621 --glyph)
27624 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27625 or DISP_STRING, and the first glyph from buffer whose
27626 position is between START_CHARPOS and END_CHARPOS. */
27627 for (; glyph > end
27628 && !INTEGERP (glyph->object)
27629 && !EQ (glyph->object, disp_string)
27630 && !(BUFFERP (glyph->object)
27631 && (glyph->charpos >= start_charpos
27632 && glyph->charpos < end_charpos));
27633 --glyph)
27635 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27636 are present at buffer positions between START_CHARPOS and
27637 END_CHARPOS, or if they come from an overlay. */
27638 if (EQ (glyph->object, before_string))
27640 pos = string_buffer_position (before_string, start_charpos);
27641 /* If pos == 0, it means before_string came from an
27642 overlay, not from a buffer position. */
27643 if (!pos || (pos >= start_charpos && pos < end_charpos))
27644 break;
27646 else if (EQ (glyph->object, after_string))
27648 pos = string_buffer_position (after_string, end_charpos);
27649 if (!pos || (pos >= start_charpos && pos < end_charpos))
27650 break;
27654 glyph++; /* first glyph to the right of the highlighted area */
27655 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27656 x += g->pixel_width;
27657 hlinfo->mouse_face_beg_x = x;
27658 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27661 /* If the highlight ends in a different row, compute GLYPH and END
27662 for the end row. Otherwise, reuse the values computed above for
27663 the row where the highlight begins. */
27664 if (r2 != r1)
27666 if (!r2->reversed_p)
27668 glyph = r2->glyphs[TEXT_AREA];
27669 end = glyph + r2->used[TEXT_AREA];
27670 x = r2->x;
27672 else
27674 end = r2->glyphs[TEXT_AREA] - 1;
27675 glyph = end + r2->used[TEXT_AREA];
27679 if (!r2->reversed_p)
27681 /* Skip truncation and continuation glyphs near the end of the
27682 row, and also blanks and stretch glyphs inserted by
27683 extend_face_to_end_of_line. */
27684 while (end > glyph
27685 && INTEGERP ((end - 1)->object))
27686 --end;
27687 /* Scan the rest of the glyph row from the end, looking for the
27688 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27689 DISP_STRING, or whose position is between START_CHARPOS
27690 and END_CHARPOS */
27691 for (--end;
27692 end > glyph
27693 && !INTEGERP (end->object)
27694 && !EQ (end->object, disp_string)
27695 && !(BUFFERP (end->object)
27696 && (end->charpos >= start_charpos
27697 && end->charpos < end_charpos));
27698 --end)
27700 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27701 are present at buffer positions between START_CHARPOS and
27702 END_CHARPOS, or if they come from an overlay. */
27703 if (EQ (end->object, before_string))
27705 pos = string_buffer_position (before_string, start_charpos);
27706 if (!pos || (pos >= start_charpos && pos < end_charpos))
27707 break;
27709 else if (EQ (end->object, after_string))
27711 pos = string_buffer_position (after_string, end_charpos);
27712 if (!pos || (pos >= start_charpos && pos < end_charpos))
27713 break;
27716 /* Find the X coordinate of the last glyph to be highlighted. */
27717 for (; glyph <= end; ++glyph)
27718 x += glyph->pixel_width;
27720 hlinfo->mouse_face_end_x = x;
27721 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27723 else
27725 /* Skip truncation and continuation glyphs near the end of the
27726 row, and also blanks and stretch glyphs inserted by
27727 extend_face_to_end_of_line. */
27728 x = r2->x;
27729 end++;
27730 while (end < glyph
27731 && INTEGERP (end->object))
27733 x += end->pixel_width;
27734 ++end;
27736 /* Scan the rest of the glyph row from the end, looking for the
27737 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27738 DISP_STRING, or whose position is between START_CHARPOS
27739 and END_CHARPOS */
27740 for ( ;
27741 end < glyph
27742 && !INTEGERP (end->object)
27743 && !EQ (end->object, disp_string)
27744 && !(BUFFERP (end->object)
27745 && (end->charpos >= start_charpos
27746 && end->charpos < end_charpos));
27747 ++end)
27749 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27750 are present at buffer positions between START_CHARPOS and
27751 END_CHARPOS, or if they come from an overlay. */
27752 if (EQ (end->object, before_string))
27754 pos = string_buffer_position (before_string, start_charpos);
27755 if (!pos || (pos >= start_charpos && pos < end_charpos))
27756 break;
27758 else if (EQ (end->object, after_string))
27760 pos = string_buffer_position (after_string, end_charpos);
27761 if (!pos || (pos >= start_charpos && pos < end_charpos))
27762 break;
27764 x += end->pixel_width;
27766 /* If we exited the above loop because we arrived at the last
27767 glyph of the row, and its buffer position is still not in
27768 range, it means the last character in range is the preceding
27769 newline. Bump the end column and x values to get past the
27770 last glyph. */
27771 if (end == glyph
27772 && BUFFERP (end->object)
27773 && (end->charpos < start_charpos
27774 || end->charpos >= end_charpos))
27776 x += end->pixel_width;
27777 ++end;
27779 hlinfo->mouse_face_end_x = x;
27780 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27783 hlinfo->mouse_face_window = window;
27784 hlinfo->mouse_face_face_id
27785 = face_at_buffer_position (w, mouse_charpos, &ignore,
27786 mouse_charpos + 1,
27787 !hlinfo->mouse_face_hidden, -1);
27788 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27791 /* The following function is not used anymore (replaced with
27792 mouse_face_from_string_pos), but I leave it here for the time
27793 being, in case someone would. */
27795 #if 0 /* not used */
27797 /* Find the position of the glyph for position POS in OBJECT in
27798 window W's current matrix, and return in *X, *Y the pixel
27799 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27801 RIGHT_P non-zero means return the position of the right edge of the
27802 glyph, RIGHT_P zero means return the left edge position.
27804 If no glyph for POS exists in the matrix, return the position of
27805 the glyph with the next smaller position that is in the matrix, if
27806 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27807 exists in the matrix, return the position of the glyph with the
27808 next larger position in OBJECT.
27810 Value is non-zero if a glyph was found. */
27812 static int
27813 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27814 int *hpos, int *vpos, int *x, int *y, int right_p)
27816 int yb = window_text_bottom_y (w);
27817 struct glyph_row *r;
27818 struct glyph *best_glyph = NULL;
27819 struct glyph_row *best_row = NULL;
27820 int best_x = 0;
27822 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27823 r->enabled_p && r->y < yb;
27824 ++r)
27826 struct glyph *g = r->glyphs[TEXT_AREA];
27827 struct glyph *e = g + r->used[TEXT_AREA];
27828 int gx;
27830 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27831 if (EQ (g->object, object))
27833 if (g->charpos == pos)
27835 best_glyph = g;
27836 best_x = gx;
27837 best_row = r;
27838 goto found;
27840 else if (best_glyph == NULL
27841 || ((eabs (g->charpos - pos)
27842 < eabs (best_glyph->charpos - pos))
27843 && (right_p
27844 ? g->charpos < pos
27845 : g->charpos > pos)))
27847 best_glyph = g;
27848 best_x = gx;
27849 best_row = r;
27854 found:
27856 if (best_glyph)
27858 *x = best_x;
27859 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27861 if (right_p)
27863 *x += best_glyph->pixel_width;
27864 ++*hpos;
27867 *y = best_row->y;
27868 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27871 return best_glyph != NULL;
27873 #endif /* not used */
27875 /* Find the positions of the first and the last glyphs in window W's
27876 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27877 (assumed to be a string), and return in HLINFO's mouse_face_*
27878 members the pixel and column/row coordinates of those glyphs. */
27880 static void
27881 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27882 Lisp_Object object,
27883 ptrdiff_t startpos, ptrdiff_t endpos)
27885 int yb = window_text_bottom_y (w);
27886 struct glyph_row *r;
27887 struct glyph *g, *e;
27888 int gx;
27889 int found = 0;
27891 /* Find the glyph row with at least one position in the range
27892 [STARTPOS..ENDPOS), and the first glyph in that row whose
27893 position belongs to that range. */
27894 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27895 r->enabled_p && r->y < yb;
27896 ++r)
27898 if (!r->reversed_p)
27900 g = r->glyphs[TEXT_AREA];
27901 e = g + r->used[TEXT_AREA];
27902 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27903 if (EQ (g->object, object)
27904 && startpos <= g->charpos && g->charpos < endpos)
27906 hlinfo->mouse_face_beg_row
27907 = MATRIX_ROW_VPOS (r, w->current_matrix);
27908 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27909 hlinfo->mouse_face_beg_x = gx;
27910 found = 1;
27911 break;
27914 else
27916 struct glyph *g1;
27918 e = r->glyphs[TEXT_AREA];
27919 g = e + r->used[TEXT_AREA];
27920 for ( ; g > e; --g)
27921 if (EQ ((g-1)->object, object)
27922 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27924 hlinfo->mouse_face_beg_row
27925 = MATRIX_ROW_VPOS (r, w->current_matrix);
27926 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27927 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27928 gx += g1->pixel_width;
27929 hlinfo->mouse_face_beg_x = gx;
27930 found = 1;
27931 break;
27934 if (found)
27935 break;
27938 if (!found)
27939 return;
27941 /* Starting with the next row, look for the first row which does NOT
27942 include any glyphs whose positions are in the range. */
27943 for (++r; r->enabled_p && r->y < yb; ++r)
27945 g = r->glyphs[TEXT_AREA];
27946 e = g + r->used[TEXT_AREA];
27947 found = 0;
27948 for ( ; g < e; ++g)
27949 if (EQ (g->object, object)
27950 && startpos <= g->charpos && g->charpos < endpos)
27952 found = 1;
27953 break;
27955 if (!found)
27956 break;
27959 /* The highlighted region ends on the previous row. */
27960 r--;
27962 /* Set the end row. */
27963 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27965 /* Compute and set the end column and the end column's horizontal
27966 pixel coordinate. */
27967 if (!r->reversed_p)
27969 g = r->glyphs[TEXT_AREA];
27970 e = g + r->used[TEXT_AREA];
27971 for ( ; e > g; --e)
27972 if (EQ ((e-1)->object, object)
27973 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27974 break;
27975 hlinfo->mouse_face_end_col = e - g;
27977 for (gx = r->x; g < e; ++g)
27978 gx += g->pixel_width;
27979 hlinfo->mouse_face_end_x = gx;
27981 else
27983 e = r->glyphs[TEXT_AREA];
27984 g = e + r->used[TEXT_AREA];
27985 for (gx = r->x ; e < g; ++e)
27987 if (EQ (e->object, object)
27988 && startpos <= e->charpos && e->charpos < endpos)
27989 break;
27990 gx += e->pixel_width;
27992 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27993 hlinfo->mouse_face_end_x = gx;
27997 #ifdef HAVE_WINDOW_SYSTEM
27999 /* See if position X, Y is within a hot-spot of an image. */
28001 static int
28002 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28004 if (!CONSP (hot_spot))
28005 return 0;
28007 if (EQ (XCAR (hot_spot), Qrect))
28009 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28010 Lisp_Object rect = XCDR (hot_spot);
28011 Lisp_Object tem;
28012 if (!CONSP (rect))
28013 return 0;
28014 if (!CONSP (XCAR (rect)))
28015 return 0;
28016 if (!CONSP (XCDR (rect)))
28017 return 0;
28018 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28019 return 0;
28020 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28021 return 0;
28022 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28023 return 0;
28024 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28025 return 0;
28026 return 1;
28028 else if (EQ (XCAR (hot_spot), Qcircle))
28030 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28031 Lisp_Object circ = XCDR (hot_spot);
28032 Lisp_Object lr, lx0, ly0;
28033 if (CONSP (circ)
28034 && CONSP (XCAR (circ))
28035 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28036 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28037 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28039 double r = XFLOATINT (lr);
28040 double dx = XINT (lx0) - x;
28041 double dy = XINT (ly0) - y;
28042 return (dx * dx + dy * dy <= r * r);
28045 else if (EQ (XCAR (hot_spot), Qpoly))
28047 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28048 if (VECTORP (XCDR (hot_spot)))
28050 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28051 Lisp_Object *poly = v->contents;
28052 ptrdiff_t n = v->header.size;
28053 ptrdiff_t i;
28054 int inside = 0;
28055 Lisp_Object lx, ly;
28056 int x0, y0;
28058 /* Need an even number of coordinates, and at least 3 edges. */
28059 if (n < 6 || n & 1)
28060 return 0;
28062 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28063 If count is odd, we are inside polygon. Pixels on edges
28064 may or may not be included depending on actual geometry of the
28065 polygon. */
28066 if ((lx = poly[n-2], !INTEGERP (lx))
28067 || (ly = poly[n-1], !INTEGERP (lx)))
28068 return 0;
28069 x0 = XINT (lx), y0 = XINT (ly);
28070 for (i = 0; i < n; i += 2)
28072 int x1 = x0, y1 = y0;
28073 if ((lx = poly[i], !INTEGERP (lx))
28074 || (ly = poly[i+1], !INTEGERP (ly)))
28075 return 0;
28076 x0 = XINT (lx), y0 = XINT (ly);
28078 /* Does this segment cross the X line? */
28079 if (x0 >= x)
28081 if (x1 >= x)
28082 continue;
28084 else if (x1 < x)
28085 continue;
28086 if (y > y0 && y > y1)
28087 continue;
28088 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28089 inside = !inside;
28091 return inside;
28094 return 0;
28097 Lisp_Object
28098 find_hot_spot (Lisp_Object map, int x, int y)
28100 while (CONSP (map))
28102 if (CONSP (XCAR (map))
28103 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28104 return XCAR (map);
28105 map = XCDR (map);
28108 return Qnil;
28111 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28112 3, 3, 0,
28113 doc: /* Lookup in image map MAP coordinates X and Y.
28114 An image map is an alist where each element has the format (AREA ID PLIST).
28115 An AREA is specified as either a rectangle, a circle, or a polygon:
28116 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28117 pixel coordinates of the upper left and bottom right corners.
28118 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28119 and the radius of the circle; r may be a float or integer.
28120 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28121 vector describes one corner in the polygon.
28122 Returns the alist element for the first matching AREA in MAP. */)
28123 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28125 if (NILP (map))
28126 return Qnil;
28128 CHECK_NUMBER (x);
28129 CHECK_NUMBER (y);
28131 return find_hot_spot (map,
28132 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28133 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28137 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28138 static void
28139 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28141 /* Do not change cursor shape while dragging mouse. */
28142 if (!NILP (do_mouse_tracking))
28143 return;
28145 if (!NILP (pointer))
28147 if (EQ (pointer, Qarrow))
28148 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28149 else if (EQ (pointer, Qhand))
28150 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28151 else if (EQ (pointer, Qtext))
28152 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28153 else if (EQ (pointer, intern ("hdrag")))
28154 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28155 else if (EQ (pointer, intern ("nhdrag")))
28156 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28157 #ifdef HAVE_X_WINDOWS
28158 else if (EQ (pointer, intern ("vdrag")))
28159 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28160 #endif
28161 else if (EQ (pointer, intern ("hourglass")))
28162 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28163 else if (EQ (pointer, Qmodeline))
28164 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28165 else
28166 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28169 if (cursor != No_Cursor)
28170 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28173 #endif /* HAVE_WINDOW_SYSTEM */
28175 /* Take proper action when mouse has moved to the mode or header line
28176 or marginal area AREA of window W, x-position X and y-position Y.
28177 X is relative to the start of the text display area of W, so the
28178 width of bitmap areas and scroll bars must be subtracted to get a
28179 position relative to the start of the mode line. */
28181 static void
28182 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28183 enum window_part area)
28185 struct window *w = XWINDOW (window);
28186 struct frame *f = XFRAME (w->frame);
28187 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28188 #ifdef HAVE_WINDOW_SYSTEM
28189 Display_Info *dpyinfo;
28190 #endif
28191 Cursor cursor = No_Cursor;
28192 Lisp_Object pointer = Qnil;
28193 int dx, dy, width, height;
28194 ptrdiff_t charpos;
28195 Lisp_Object string, object = Qnil;
28196 Lisp_Object pos IF_LINT (= Qnil), help;
28198 Lisp_Object mouse_face;
28199 int original_x_pixel = x;
28200 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28201 struct glyph_row *row IF_LINT (= 0);
28203 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28205 int x0;
28206 struct glyph *end;
28208 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28209 returns them in row/column units! */
28210 string = mode_line_string (w, area, &x, &y, &charpos,
28211 &object, &dx, &dy, &width, &height);
28213 row = (area == ON_MODE_LINE
28214 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28215 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28217 /* Find the glyph under the mouse pointer. */
28218 if (row->mode_line_p && row->enabled_p)
28220 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28221 end = glyph + row->used[TEXT_AREA];
28223 for (x0 = original_x_pixel;
28224 glyph < end && x0 >= glyph->pixel_width;
28225 ++glyph)
28226 x0 -= glyph->pixel_width;
28228 if (glyph >= end)
28229 glyph = NULL;
28232 else
28234 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28235 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28236 returns them in row/column units! */
28237 string = marginal_area_string (w, area, &x, &y, &charpos,
28238 &object, &dx, &dy, &width, &height);
28241 help = Qnil;
28243 #ifdef HAVE_WINDOW_SYSTEM
28244 if (IMAGEP (object))
28246 Lisp_Object image_map, hotspot;
28247 if ((image_map = Fplist_get (XCDR (object), QCmap),
28248 !NILP (image_map))
28249 && (hotspot = find_hot_spot (image_map, dx, dy),
28250 CONSP (hotspot))
28251 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28253 Lisp_Object plist;
28255 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28256 If so, we could look for mouse-enter, mouse-leave
28257 properties in PLIST (and do something...). */
28258 hotspot = XCDR (hotspot);
28259 if (CONSP (hotspot)
28260 && (plist = XCAR (hotspot), CONSP (plist)))
28262 pointer = Fplist_get (plist, Qpointer);
28263 if (NILP (pointer))
28264 pointer = Qhand;
28265 help = Fplist_get (plist, Qhelp_echo);
28266 if (!NILP (help))
28268 help_echo_string = help;
28269 XSETWINDOW (help_echo_window, w);
28270 help_echo_object = w->contents;
28271 help_echo_pos = charpos;
28275 if (NILP (pointer))
28276 pointer = Fplist_get (XCDR (object), QCpointer);
28278 #endif /* HAVE_WINDOW_SYSTEM */
28280 if (STRINGP (string))
28281 pos = make_number (charpos);
28283 /* Set the help text and mouse pointer. If the mouse is on a part
28284 of the mode line without any text (e.g. past the right edge of
28285 the mode line text), use the default help text and pointer. */
28286 if (STRINGP (string) || area == ON_MODE_LINE)
28288 /* Arrange to display the help by setting the global variables
28289 help_echo_string, help_echo_object, and help_echo_pos. */
28290 if (NILP (help))
28292 if (STRINGP (string))
28293 help = Fget_text_property (pos, Qhelp_echo, string);
28295 if (!NILP (help))
28297 help_echo_string = help;
28298 XSETWINDOW (help_echo_window, w);
28299 help_echo_object = string;
28300 help_echo_pos = charpos;
28302 else if (area == ON_MODE_LINE)
28304 Lisp_Object default_help
28305 = buffer_local_value_1 (Qmode_line_default_help_echo,
28306 w->contents);
28308 if (STRINGP (default_help))
28310 help_echo_string = default_help;
28311 XSETWINDOW (help_echo_window, w);
28312 help_echo_object = Qnil;
28313 help_echo_pos = -1;
28318 #ifdef HAVE_WINDOW_SYSTEM
28319 /* Change the mouse pointer according to what is under it. */
28320 if (FRAME_WINDOW_P (f))
28322 dpyinfo = FRAME_DISPLAY_INFO (f);
28323 if (STRINGP (string))
28325 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28327 if (NILP (pointer))
28328 pointer = Fget_text_property (pos, Qpointer, string);
28330 /* Change the mouse pointer according to what is under X/Y. */
28331 if (NILP (pointer)
28332 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28334 Lisp_Object map;
28335 map = Fget_text_property (pos, Qlocal_map, string);
28336 if (!KEYMAPP (map))
28337 map = Fget_text_property (pos, Qkeymap, string);
28338 if (!KEYMAPP (map))
28339 cursor = dpyinfo->vertical_scroll_bar_cursor;
28342 else
28343 /* Default mode-line pointer. */
28344 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28346 #endif
28349 /* Change the mouse face according to what is under X/Y. */
28350 if (STRINGP (string))
28352 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28353 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28354 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28355 && glyph)
28357 Lisp_Object b, e;
28359 struct glyph * tmp_glyph;
28361 int gpos;
28362 int gseq_length;
28363 int total_pixel_width;
28364 ptrdiff_t begpos, endpos, ignore;
28366 int vpos, hpos;
28368 b = Fprevious_single_property_change (make_number (charpos + 1),
28369 Qmouse_face, string, Qnil);
28370 if (NILP (b))
28371 begpos = 0;
28372 else
28373 begpos = XINT (b);
28375 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28376 if (NILP (e))
28377 endpos = SCHARS (string);
28378 else
28379 endpos = XINT (e);
28381 /* Calculate the glyph position GPOS of GLYPH in the
28382 displayed string, relative to the beginning of the
28383 highlighted part of the string.
28385 Note: GPOS is different from CHARPOS. CHARPOS is the
28386 position of GLYPH in the internal string object. A mode
28387 line string format has structures which are converted to
28388 a flattened string by the Emacs Lisp interpreter. The
28389 internal string is an element of those structures. The
28390 displayed string is the flattened string. */
28391 tmp_glyph = row_start_glyph;
28392 while (tmp_glyph < glyph
28393 && (!(EQ (tmp_glyph->object, glyph->object)
28394 && begpos <= tmp_glyph->charpos
28395 && tmp_glyph->charpos < endpos)))
28396 tmp_glyph++;
28397 gpos = glyph - tmp_glyph;
28399 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28400 the highlighted part of the displayed string to which
28401 GLYPH belongs. Note: GSEQ_LENGTH is different from
28402 SCHARS (STRING), because the latter returns the length of
28403 the internal string. */
28404 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28405 tmp_glyph > glyph
28406 && (!(EQ (tmp_glyph->object, glyph->object)
28407 && begpos <= tmp_glyph->charpos
28408 && tmp_glyph->charpos < endpos));
28409 tmp_glyph--)
28411 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28413 /* Calculate the total pixel width of all the glyphs between
28414 the beginning of the highlighted area and GLYPH. */
28415 total_pixel_width = 0;
28416 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28417 total_pixel_width += tmp_glyph->pixel_width;
28419 /* Pre calculation of re-rendering position. Note: X is in
28420 column units here, after the call to mode_line_string or
28421 marginal_area_string. */
28422 hpos = x - gpos;
28423 vpos = (area == ON_MODE_LINE
28424 ? (w->current_matrix)->nrows - 1
28425 : 0);
28427 /* If GLYPH's position is included in the region that is
28428 already drawn in mouse face, we have nothing to do. */
28429 if ( EQ (window, hlinfo->mouse_face_window)
28430 && (!row->reversed_p
28431 ? (hlinfo->mouse_face_beg_col <= hpos
28432 && hpos < hlinfo->mouse_face_end_col)
28433 /* In R2L rows we swap BEG and END, see below. */
28434 : (hlinfo->mouse_face_end_col <= hpos
28435 && hpos < hlinfo->mouse_face_beg_col))
28436 && hlinfo->mouse_face_beg_row == vpos )
28437 return;
28439 if (clear_mouse_face (hlinfo))
28440 cursor = No_Cursor;
28442 if (!row->reversed_p)
28444 hlinfo->mouse_face_beg_col = hpos;
28445 hlinfo->mouse_face_beg_x = original_x_pixel
28446 - (total_pixel_width + dx);
28447 hlinfo->mouse_face_end_col = hpos + gseq_length;
28448 hlinfo->mouse_face_end_x = 0;
28450 else
28452 /* In R2L rows, show_mouse_face expects BEG and END
28453 coordinates to be swapped. */
28454 hlinfo->mouse_face_end_col = hpos;
28455 hlinfo->mouse_face_end_x = original_x_pixel
28456 - (total_pixel_width + dx);
28457 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28458 hlinfo->mouse_face_beg_x = 0;
28461 hlinfo->mouse_face_beg_row = vpos;
28462 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28463 hlinfo->mouse_face_past_end = 0;
28464 hlinfo->mouse_face_window = window;
28466 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28467 charpos,
28468 0, &ignore,
28469 glyph->face_id,
28471 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28473 if (NILP (pointer))
28474 pointer = Qhand;
28476 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28477 clear_mouse_face (hlinfo);
28479 #ifdef HAVE_WINDOW_SYSTEM
28480 if (FRAME_WINDOW_P (f))
28481 define_frame_cursor1 (f, cursor, pointer);
28482 #endif
28486 /* EXPORT:
28487 Take proper action when the mouse has moved to position X, Y on
28488 frame F with regards to highlighting portions of display that have
28489 mouse-face properties. Also de-highlight portions of display where
28490 the mouse was before, set the mouse pointer shape as appropriate
28491 for the mouse coordinates, and activate help echo (tooltips).
28492 X and Y can be negative or out of range. */
28494 void
28495 note_mouse_highlight (struct frame *f, int x, int y)
28497 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28498 enum window_part part = ON_NOTHING;
28499 Lisp_Object window;
28500 struct window *w;
28501 Cursor cursor = No_Cursor;
28502 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28503 struct buffer *b;
28505 /* When a menu is active, don't highlight because this looks odd. */
28506 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28507 if (popup_activated ())
28508 return;
28509 #endif
28511 if (!f->glyphs_initialized_p
28512 || f->pointer_invisible)
28513 return;
28515 hlinfo->mouse_face_mouse_x = x;
28516 hlinfo->mouse_face_mouse_y = y;
28517 hlinfo->mouse_face_mouse_frame = f;
28519 if (hlinfo->mouse_face_defer)
28520 return;
28522 /* Which window is that in? */
28523 window = window_from_coordinates (f, x, y, &part, 1);
28525 /* If displaying active text in another window, clear that. */
28526 if (! EQ (window, hlinfo->mouse_face_window)
28527 /* Also clear if we move out of text area in same window. */
28528 || (!NILP (hlinfo->mouse_face_window)
28529 && !NILP (window)
28530 && part != ON_TEXT
28531 && part != ON_MODE_LINE
28532 && part != ON_HEADER_LINE))
28533 clear_mouse_face (hlinfo);
28535 /* Not on a window -> return. */
28536 if (!WINDOWP (window))
28537 return;
28539 /* Reset help_echo_string. It will get recomputed below. */
28540 help_echo_string = Qnil;
28542 /* Convert to window-relative pixel coordinates. */
28543 w = XWINDOW (window);
28544 frame_to_window_pixel_xy (w, &x, &y);
28546 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28547 /* Handle tool-bar window differently since it doesn't display a
28548 buffer. */
28549 if (EQ (window, f->tool_bar_window))
28551 note_tool_bar_highlight (f, x, y);
28552 return;
28554 #endif
28556 /* Mouse is on the mode, header line or margin? */
28557 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28558 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28560 note_mode_line_or_margin_highlight (window, x, y, part);
28561 return;
28564 #ifdef HAVE_WINDOW_SYSTEM
28565 if (part == ON_VERTICAL_BORDER)
28567 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28568 help_echo_string = build_string ("drag-mouse-1: resize");
28570 else if (part == ON_RIGHT_DIVIDER)
28572 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28573 help_echo_string = build_string ("drag-mouse-1: resize");
28575 else if (part == ON_BOTTOM_DIVIDER)
28577 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28578 help_echo_string = build_string ("drag-mouse-1: resize");
28580 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28581 || part == ON_SCROLL_BAR)
28582 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28583 else
28584 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28585 #endif
28587 /* Are we in a window whose display is up to date?
28588 And verify the buffer's text has not changed. */
28589 b = XBUFFER (w->contents);
28590 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28592 int hpos, vpos, dx, dy, area = LAST_AREA;
28593 ptrdiff_t pos;
28594 struct glyph *glyph;
28595 Lisp_Object object;
28596 Lisp_Object mouse_face = Qnil, position;
28597 Lisp_Object *overlay_vec = NULL;
28598 ptrdiff_t i, noverlays;
28599 struct buffer *obuf;
28600 ptrdiff_t obegv, ozv;
28601 int same_region;
28603 /* Find the glyph under X/Y. */
28604 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28606 #ifdef HAVE_WINDOW_SYSTEM
28607 /* Look for :pointer property on image. */
28608 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28610 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28611 if (img != NULL && IMAGEP (img->spec))
28613 Lisp_Object image_map, hotspot;
28614 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28615 !NILP (image_map))
28616 && (hotspot = find_hot_spot (image_map,
28617 glyph->slice.img.x + dx,
28618 glyph->slice.img.y + dy),
28619 CONSP (hotspot))
28620 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28622 Lisp_Object plist;
28624 /* Could check XCAR (hotspot) to see if we enter/leave
28625 this hot-spot.
28626 If so, we could look for mouse-enter, mouse-leave
28627 properties in PLIST (and do something...). */
28628 hotspot = XCDR (hotspot);
28629 if (CONSP (hotspot)
28630 && (plist = XCAR (hotspot), CONSP (plist)))
28632 pointer = Fplist_get (plist, Qpointer);
28633 if (NILP (pointer))
28634 pointer = Qhand;
28635 help_echo_string = Fplist_get (plist, Qhelp_echo);
28636 if (!NILP (help_echo_string))
28638 help_echo_window = window;
28639 help_echo_object = glyph->object;
28640 help_echo_pos = glyph->charpos;
28644 if (NILP (pointer))
28645 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28648 #endif /* HAVE_WINDOW_SYSTEM */
28650 /* Clear mouse face if X/Y not over text. */
28651 if (glyph == NULL
28652 || area != TEXT_AREA
28653 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28654 /* Glyph's OBJECT is an integer for glyphs inserted by the
28655 display engine for its internal purposes, like truncation
28656 and continuation glyphs and blanks beyond the end of
28657 line's text on text terminals. If we are over such a
28658 glyph, we are not over any text. */
28659 || INTEGERP (glyph->object)
28660 /* R2L rows have a stretch glyph at their front, which
28661 stands for no text, whereas L2R rows have no glyphs at
28662 all beyond the end of text. Treat such stretch glyphs
28663 like we do with NULL glyphs in L2R rows. */
28664 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28665 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28666 && glyph->type == STRETCH_GLYPH
28667 && glyph->avoid_cursor_p))
28669 if (clear_mouse_face (hlinfo))
28670 cursor = No_Cursor;
28671 #ifdef HAVE_WINDOW_SYSTEM
28672 if (FRAME_WINDOW_P (f) && NILP (pointer))
28674 if (area != TEXT_AREA)
28675 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28676 else
28677 pointer = Vvoid_text_area_pointer;
28679 #endif
28680 goto set_cursor;
28683 pos = glyph->charpos;
28684 object = glyph->object;
28685 if (!STRINGP (object) && !BUFFERP (object))
28686 goto set_cursor;
28688 /* If we get an out-of-range value, return now; avoid an error. */
28689 if (BUFFERP (object) && pos > BUF_Z (b))
28690 goto set_cursor;
28692 /* Make the window's buffer temporarily current for
28693 overlays_at and compute_char_face. */
28694 obuf = current_buffer;
28695 current_buffer = b;
28696 obegv = BEGV;
28697 ozv = ZV;
28698 BEGV = BEG;
28699 ZV = Z;
28701 /* Is this char mouse-active or does it have help-echo? */
28702 position = make_number (pos);
28704 if (BUFFERP (object))
28706 /* Put all the overlays we want in a vector in overlay_vec. */
28707 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28708 /* Sort overlays into increasing priority order. */
28709 noverlays = sort_overlays (overlay_vec, noverlays, w);
28711 else
28712 noverlays = 0;
28714 if (NILP (Vmouse_highlight))
28716 clear_mouse_face (hlinfo);
28717 goto check_help_echo;
28720 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28722 if (same_region)
28723 cursor = No_Cursor;
28725 /* Check mouse-face highlighting. */
28726 if (! same_region
28727 /* If there exists an overlay with mouse-face overlapping
28728 the one we are currently highlighting, we have to
28729 check if we enter the overlapping overlay, and then
28730 highlight only that. */
28731 || (OVERLAYP (hlinfo->mouse_face_overlay)
28732 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28734 /* Find the highest priority overlay with a mouse-face. */
28735 Lisp_Object overlay = Qnil;
28736 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28738 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28739 if (!NILP (mouse_face))
28740 overlay = overlay_vec[i];
28743 /* If we're highlighting the same overlay as before, there's
28744 no need to do that again. */
28745 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28746 goto check_help_echo;
28747 hlinfo->mouse_face_overlay = overlay;
28749 /* Clear the display of the old active region, if any. */
28750 if (clear_mouse_face (hlinfo))
28751 cursor = No_Cursor;
28753 /* If no overlay applies, get a text property. */
28754 if (NILP (overlay))
28755 mouse_face = Fget_text_property (position, Qmouse_face, object);
28757 /* Next, compute the bounds of the mouse highlighting and
28758 display it. */
28759 if (!NILP (mouse_face) && STRINGP (object))
28761 /* The mouse-highlighting comes from a display string
28762 with a mouse-face. */
28763 Lisp_Object s, e;
28764 ptrdiff_t ignore;
28766 s = Fprevious_single_property_change
28767 (make_number (pos + 1), Qmouse_face, object, Qnil);
28768 e = Fnext_single_property_change
28769 (position, Qmouse_face, object, Qnil);
28770 if (NILP (s))
28771 s = make_number (0);
28772 if (NILP (e))
28773 e = make_number (SCHARS (object));
28774 mouse_face_from_string_pos (w, hlinfo, object,
28775 XINT (s), XINT (e));
28776 hlinfo->mouse_face_past_end = 0;
28777 hlinfo->mouse_face_window = window;
28778 hlinfo->mouse_face_face_id
28779 = face_at_string_position (w, object, pos, 0, &ignore,
28780 glyph->face_id, 1);
28781 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28782 cursor = No_Cursor;
28784 else
28786 /* The mouse-highlighting, if any, comes from an overlay
28787 or text property in the buffer. */
28788 Lisp_Object buffer IF_LINT (= Qnil);
28789 Lisp_Object disp_string IF_LINT (= Qnil);
28791 if (STRINGP (object))
28793 /* If we are on a display string with no mouse-face,
28794 check if the text under it has one. */
28795 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28796 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28797 pos = string_buffer_position (object, start);
28798 if (pos > 0)
28800 mouse_face = get_char_property_and_overlay
28801 (make_number (pos), Qmouse_face, w->contents, &overlay);
28802 buffer = w->contents;
28803 disp_string = object;
28806 else
28808 buffer = object;
28809 disp_string = Qnil;
28812 if (!NILP (mouse_face))
28814 Lisp_Object before, after;
28815 Lisp_Object before_string, after_string;
28816 /* To correctly find the limits of mouse highlight
28817 in a bidi-reordered buffer, we must not use the
28818 optimization of limiting the search in
28819 previous-single-property-change and
28820 next-single-property-change, because
28821 rows_from_pos_range needs the real start and end
28822 positions to DTRT in this case. That's because
28823 the first row visible in a window does not
28824 necessarily display the character whose position
28825 is the smallest. */
28826 Lisp_Object lim1
28827 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28828 ? Fmarker_position (w->start)
28829 : Qnil;
28830 Lisp_Object lim2
28831 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28832 ? make_number (BUF_Z (XBUFFER (buffer))
28833 - w->window_end_pos)
28834 : Qnil;
28836 if (NILP (overlay))
28838 /* Handle the text property case. */
28839 before = Fprevious_single_property_change
28840 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28841 after = Fnext_single_property_change
28842 (make_number (pos), Qmouse_face, buffer, lim2);
28843 before_string = after_string = Qnil;
28845 else
28847 /* Handle the overlay case. */
28848 before = Foverlay_start (overlay);
28849 after = Foverlay_end (overlay);
28850 before_string = Foverlay_get (overlay, Qbefore_string);
28851 after_string = Foverlay_get (overlay, Qafter_string);
28853 if (!STRINGP (before_string)) before_string = Qnil;
28854 if (!STRINGP (after_string)) after_string = Qnil;
28857 mouse_face_from_buffer_pos (window, hlinfo, pos,
28858 NILP (before)
28860 : XFASTINT (before),
28861 NILP (after)
28862 ? BUF_Z (XBUFFER (buffer))
28863 : XFASTINT (after),
28864 before_string, after_string,
28865 disp_string);
28866 cursor = No_Cursor;
28871 check_help_echo:
28873 /* Look for a `help-echo' property. */
28874 if (NILP (help_echo_string)) {
28875 Lisp_Object help, overlay;
28877 /* Check overlays first. */
28878 help = overlay = Qnil;
28879 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28881 overlay = overlay_vec[i];
28882 help = Foverlay_get (overlay, Qhelp_echo);
28885 if (!NILP (help))
28887 help_echo_string = help;
28888 help_echo_window = window;
28889 help_echo_object = overlay;
28890 help_echo_pos = pos;
28892 else
28894 Lisp_Object obj = glyph->object;
28895 ptrdiff_t charpos = glyph->charpos;
28897 /* Try text properties. */
28898 if (STRINGP (obj)
28899 && charpos >= 0
28900 && charpos < SCHARS (obj))
28902 help = Fget_text_property (make_number (charpos),
28903 Qhelp_echo, obj);
28904 if (NILP (help))
28906 /* If the string itself doesn't specify a help-echo,
28907 see if the buffer text ``under'' it does. */
28908 struct glyph_row *r
28909 = MATRIX_ROW (w->current_matrix, vpos);
28910 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28911 ptrdiff_t p = string_buffer_position (obj, start);
28912 if (p > 0)
28914 help = Fget_char_property (make_number (p),
28915 Qhelp_echo, w->contents);
28916 if (!NILP (help))
28918 charpos = p;
28919 obj = w->contents;
28924 else if (BUFFERP (obj)
28925 && charpos >= BEGV
28926 && charpos < ZV)
28927 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28928 obj);
28930 if (!NILP (help))
28932 help_echo_string = help;
28933 help_echo_window = window;
28934 help_echo_object = obj;
28935 help_echo_pos = charpos;
28940 #ifdef HAVE_WINDOW_SYSTEM
28941 /* Look for a `pointer' property. */
28942 if (FRAME_WINDOW_P (f) && NILP (pointer))
28944 /* Check overlays first. */
28945 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28946 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28948 if (NILP (pointer))
28950 Lisp_Object obj = glyph->object;
28951 ptrdiff_t charpos = glyph->charpos;
28953 /* Try text properties. */
28954 if (STRINGP (obj)
28955 && charpos >= 0
28956 && charpos < SCHARS (obj))
28958 pointer = Fget_text_property (make_number (charpos),
28959 Qpointer, obj);
28960 if (NILP (pointer))
28962 /* If the string itself doesn't specify a pointer,
28963 see if the buffer text ``under'' it does. */
28964 struct glyph_row *r
28965 = MATRIX_ROW (w->current_matrix, vpos);
28966 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28967 ptrdiff_t p = string_buffer_position (obj, start);
28968 if (p > 0)
28969 pointer = Fget_char_property (make_number (p),
28970 Qpointer, w->contents);
28973 else if (BUFFERP (obj)
28974 && charpos >= BEGV
28975 && charpos < ZV)
28976 pointer = Fget_text_property (make_number (charpos),
28977 Qpointer, obj);
28980 #endif /* HAVE_WINDOW_SYSTEM */
28982 BEGV = obegv;
28983 ZV = ozv;
28984 current_buffer = obuf;
28987 set_cursor:
28989 #ifdef HAVE_WINDOW_SYSTEM
28990 if (FRAME_WINDOW_P (f))
28991 define_frame_cursor1 (f, cursor, pointer);
28992 #else
28993 /* This is here to prevent a compiler error, about "label at end of
28994 compound statement". */
28995 return;
28996 #endif
29000 /* EXPORT for RIF:
29001 Clear any mouse-face on window W. This function is part of the
29002 redisplay interface, and is called from try_window_id and similar
29003 functions to ensure the mouse-highlight is off. */
29005 void
29006 x_clear_window_mouse_face (struct window *w)
29008 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29009 Lisp_Object window;
29011 block_input ();
29012 XSETWINDOW (window, w);
29013 if (EQ (window, hlinfo->mouse_face_window))
29014 clear_mouse_face (hlinfo);
29015 unblock_input ();
29019 /* EXPORT:
29020 Just discard the mouse face information for frame F, if any.
29021 This is used when the size of F is changed. */
29023 void
29024 cancel_mouse_face (struct frame *f)
29026 Lisp_Object window;
29027 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29029 window = hlinfo->mouse_face_window;
29030 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29031 reset_mouse_highlight (hlinfo);
29036 /***********************************************************************
29037 Exposure Events
29038 ***********************************************************************/
29040 #ifdef HAVE_WINDOW_SYSTEM
29042 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29043 which intersects rectangle R. R is in window-relative coordinates. */
29045 static void
29046 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29047 enum glyph_row_area area)
29049 struct glyph *first = row->glyphs[area];
29050 struct glyph *end = row->glyphs[area] + row->used[area];
29051 struct glyph *last;
29052 int first_x, start_x, x;
29054 if (area == TEXT_AREA && row->fill_line_p)
29055 /* If row extends face to end of line write the whole line. */
29056 draw_glyphs (w, 0, row, area,
29057 0, row->used[area],
29058 DRAW_NORMAL_TEXT, 0);
29059 else
29061 /* Set START_X to the window-relative start position for drawing glyphs of
29062 AREA. The first glyph of the text area can be partially visible.
29063 The first glyphs of other areas cannot. */
29064 start_x = window_box_left_offset (w, area);
29065 x = start_x;
29066 if (area == TEXT_AREA)
29067 x += row->x;
29069 /* Find the first glyph that must be redrawn. */
29070 while (first < end
29071 && x + first->pixel_width < r->x)
29073 x += first->pixel_width;
29074 ++first;
29077 /* Find the last one. */
29078 last = first;
29079 first_x = x;
29080 while (last < end
29081 && x < r->x + r->width)
29083 x += last->pixel_width;
29084 ++last;
29087 /* Repaint. */
29088 if (last > first)
29089 draw_glyphs (w, first_x - start_x, row, area,
29090 first - row->glyphs[area], last - row->glyphs[area],
29091 DRAW_NORMAL_TEXT, 0);
29096 /* Redraw the parts of the glyph row ROW on window W intersecting
29097 rectangle R. R is in window-relative coordinates. Value is
29098 non-zero if mouse-face was overwritten. */
29100 static int
29101 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29103 eassert (row->enabled_p);
29105 if (row->mode_line_p || w->pseudo_window_p)
29106 draw_glyphs (w, 0, row, TEXT_AREA,
29107 0, row->used[TEXT_AREA],
29108 DRAW_NORMAL_TEXT, 0);
29109 else
29111 if (row->used[LEFT_MARGIN_AREA])
29112 expose_area (w, row, r, LEFT_MARGIN_AREA);
29113 if (row->used[TEXT_AREA])
29114 expose_area (w, row, r, TEXT_AREA);
29115 if (row->used[RIGHT_MARGIN_AREA])
29116 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29117 draw_row_fringe_bitmaps (w, row);
29120 return row->mouse_face_p;
29124 /* Redraw those parts of glyphs rows during expose event handling that
29125 overlap other rows. Redrawing of an exposed line writes over parts
29126 of lines overlapping that exposed line; this function fixes that.
29128 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29129 row in W's current matrix that is exposed and overlaps other rows.
29130 LAST_OVERLAPPING_ROW is the last such row. */
29132 static void
29133 expose_overlaps (struct window *w,
29134 struct glyph_row *first_overlapping_row,
29135 struct glyph_row *last_overlapping_row,
29136 XRectangle *r)
29138 struct glyph_row *row;
29140 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29141 if (row->overlapping_p)
29143 eassert (row->enabled_p && !row->mode_line_p);
29145 row->clip = r;
29146 if (row->used[LEFT_MARGIN_AREA])
29147 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29149 if (row->used[TEXT_AREA])
29150 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29152 if (row->used[RIGHT_MARGIN_AREA])
29153 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29154 row->clip = NULL;
29159 /* Return non-zero if W's cursor intersects rectangle R. */
29161 static int
29162 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29164 XRectangle cr, result;
29165 struct glyph *cursor_glyph;
29166 struct glyph_row *row;
29168 if (w->phys_cursor.vpos >= 0
29169 && w->phys_cursor.vpos < w->current_matrix->nrows
29170 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29171 row->enabled_p)
29172 && row->cursor_in_fringe_p)
29174 /* Cursor is in the fringe. */
29175 cr.x = window_box_right_offset (w,
29176 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29177 ? RIGHT_MARGIN_AREA
29178 : TEXT_AREA));
29179 cr.y = row->y;
29180 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29181 cr.height = row->height;
29182 return x_intersect_rectangles (&cr, r, &result);
29185 cursor_glyph = get_phys_cursor_glyph (w);
29186 if (cursor_glyph)
29188 /* r is relative to W's box, but w->phys_cursor.x is relative
29189 to left edge of W's TEXT area. Adjust it. */
29190 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29191 cr.y = w->phys_cursor.y;
29192 cr.width = cursor_glyph->pixel_width;
29193 cr.height = w->phys_cursor_height;
29194 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29195 I assume the effect is the same -- and this is portable. */
29196 return x_intersect_rectangles (&cr, r, &result);
29198 /* If we don't understand the format, pretend we're not in the hot-spot. */
29199 return 0;
29203 /* EXPORT:
29204 Draw a vertical window border to the right of window W if W doesn't
29205 have vertical scroll bars. */
29207 void
29208 x_draw_vertical_border (struct window *w)
29210 struct frame *f = XFRAME (WINDOW_FRAME (w));
29212 /* We could do better, if we knew what type of scroll-bar the adjacent
29213 windows (on either side) have... But we don't :-(
29214 However, I think this works ok. ++KFS 2003-04-25 */
29216 /* Redraw borders between horizontally adjacent windows. Don't
29217 do it for frames with vertical scroll bars because either the
29218 right scroll bar of a window, or the left scroll bar of its
29219 neighbor will suffice as a border. */
29220 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29221 return;
29223 /* Note: It is necessary to redraw both the left and the right
29224 borders, for when only this single window W is being
29225 redisplayed. */
29226 if (!WINDOW_RIGHTMOST_P (w)
29227 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29229 int x0, x1, y0, y1;
29231 window_box_edges (w, &x0, &y0, &x1, &y1);
29232 y1 -= 1;
29234 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29235 x1 -= 1;
29237 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29240 if (!WINDOW_LEFTMOST_P (w)
29241 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29243 int x0, x1, y0, y1;
29245 window_box_edges (w, &x0, &y0, &x1, &y1);
29246 y1 -= 1;
29248 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29249 x0 -= 1;
29251 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29256 /* Draw window dividers for window W. */
29258 void
29259 x_draw_right_divider (struct window *w)
29261 struct frame *f = WINDOW_XFRAME (w);
29263 if (w->mini || w->pseudo_window_p)
29264 return;
29265 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29267 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29268 int x1 = WINDOW_RIGHT_EDGE_X (w);
29269 int y0 = WINDOW_TOP_EDGE_Y (w);
29270 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29272 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29276 static void
29277 x_draw_bottom_divider (struct window *w)
29279 struct frame *f = XFRAME (WINDOW_FRAME (w));
29281 if (w->mini || w->pseudo_window_p)
29282 return;
29283 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29285 int x0 = WINDOW_LEFT_EDGE_X (w);
29286 int x1 = WINDOW_RIGHT_EDGE_X (w);
29287 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29288 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29290 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29294 /* Redraw the part of window W intersection rectangle FR. Pixel
29295 coordinates in FR are frame-relative. Call this function with
29296 input blocked. Value is non-zero if the exposure overwrites
29297 mouse-face. */
29299 static int
29300 expose_window (struct window *w, XRectangle *fr)
29302 struct frame *f = XFRAME (w->frame);
29303 XRectangle wr, r;
29304 int mouse_face_overwritten_p = 0;
29306 /* If window is not yet fully initialized, do nothing. This can
29307 happen when toolkit scroll bars are used and a window is split.
29308 Reconfiguring the scroll bar will generate an expose for a newly
29309 created window. */
29310 if (w->current_matrix == NULL)
29311 return 0;
29313 /* When we're currently updating the window, display and current
29314 matrix usually don't agree. Arrange for a thorough display
29315 later. */
29316 if (w->must_be_updated_p)
29318 SET_FRAME_GARBAGED (f);
29319 return 0;
29322 /* Frame-relative pixel rectangle of W. */
29323 wr.x = WINDOW_LEFT_EDGE_X (w);
29324 wr.y = WINDOW_TOP_EDGE_Y (w);
29325 wr.width = WINDOW_PIXEL_WIDTH (w);
29326 wr.height = WINDOW_PIXEL_HEIGHT (w);
29328 if (x_intersect_rectangles (fr, &wr, &r))
29330 int yb = window_text_bottom_y (w);
29331 struct glyph_row *row;
29332 int cursor_cleared_p, phys_cursor_on_p;
29333 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29335 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29336 r.x, r.y, r.width, r.height));
29338 /* Convert to window coordinates. */
29339 r.x -= WINDOW_LEFT_EDGE_X (w);
29340 r.y -= WINDOW_TOP_EDGE_Y (w);
29342 /* Turn off the cursor. */
29343 if (!w->pseudo_window_p
29344 && phys_cursor_in_rect_p (w, &r))
29346 x_clear_cursor (w);
29347 cursor_cleared_p = 1;
29349 else
29350 cursor_cleared_p = 0;
29352 /* If the row containing the cursor extends face to end of line,
29353 then expose_area might overwrite the cursor outside the
29354 rectangle and thus notice_overwritten_cursor might clear
29355 w->phys_cursor_on_p. We remember the original value and
29356 check later if it is changed. */
29357 phys_cursor_on_p = w->phys_cursor_on_p;
29359 /* Update lines intersecting rectangle R. */
29360 first_overlapping_row = last_overlapping_row = NULL;
29361 for (row = w->current_matrix->rows;
29362 row->enabled_p;
29363 ++row)
29365 int y0 = row->y;
29366 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29368 if ((y0 >= r.y && y0 < r.y + r.height)
29369 || (y1 > r.y && y1 < r.y + r.height)
29370 || (r.y >= y0 && r.y < y1)
29371 || (r.y + r.height > y0 && r.y + r.height < y1))
29373 /* A header line may be overlapping, but there is no need
29374 to fix overlapping areas for them. KFS 2005-02-12 */
29375 if (row->overlapping_p && !row->mode_line_p)
29377 if (first_overlapping_row == NULL)
29378 first_overlapping_row = row;
29379 last_overlapping_row = row;
29382 row->clip = fr;
29383 if (expose_line (w, row, &r))
29384 mouse_face_overwritten_p = 1;
29385 row->clip = NULL;
29387 else if (row->overlapping_p)
29389 /* We must redraw a row overlapping the exposed area. */
29390 if (y0 < r.y
29391 ? y0 + row->phys_height > r.y
29392 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29394 if (first_overlapping_row == NULL)
29395 first_overlapping_row = row;
29396 last_overlapping_row = row;
29400 if (y1 >= yb)
29401 break;
29404 /* Display the mode line if there is one. */
29405 if (WINDOW_WANTS_MODELINE_P (w)
29406 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29407 row->enabled_p)
29408 && row->y < r.y + r.height)
29410 if (expose_line (w, row, &r))
29411 mouse_face_overwritten_p = 1;
29414 if (!w->pseudo_window_p)
29416 /* Fix the display of overlapping rows. */
29417 if (first_overlapping_row)
29418 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29419 fr);
29421 /* Draw border between windows. */
29422 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29423 x_draw_right_divider (w);
29424 else
29425 x_draw_vertical_border (w);
29427 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29428 x_draw_bottom_divider (w);
29430 /* Turn the cursor on again. */
29431 if (cursor_cleared_p
29432 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29433 update_window_cursor (w, 1);
29437 return mouse_face_overwritten_p;
29442 /* Redraw (parts) of all windows in the window tree rooted at W that
29443 intersect R. R contains frame pixel coordinates. Value is
29444 non-zero if the exposure overwrites mouse-face. */
29446 static int
29447 expose_window_tree (struct window *w, XRectangle *r)
29449 struct frame *f = XFRAME (w->frame);
29450 int mouse_face_overwritten_p = 0;
29452 while (w && !FRAME_GARBAGED_P (f))
29454 if (WINDOWP (w->contents))
29455 mouse_face_overwritten_p
29456 |= expose_window_tree (XWINDOW (w->contents), r);
29457 else
29458 mouse_face_overwritten_p |= expose_window (w, r);
29460 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29463 return mouse_face_overwritten_p;
29467 /* EXPORT:
29468 Redisplay an exposed area of frame F. X and Y are the upper-left
29469 corner of the exposed rectangle. W and H are width and height of
29470 the exposed area. All are pixel values. W or H zero means redraw
29471 the entire frame. */
29473 void
29474 expose_frame (struct frame *f, int x, int y, int w, int h)
29476 XRectangle r;
29477 int mouse_face_overwritten_p = 0;
29479 TRACE ((stderr, "expose_frame "));
29481 /* No need to redraw if frame will be redrawn soon. */
29482 if (FRAME_GARBAGED_P (f))
29484 TRACE ((stderr, " garbaged\n"));
29485 return;
29488 /* If basic faces haven't been realized yet, there is no point in
29489 trying to redraw anything. This can happen when we get an expose
29490 event while Emacs is starting, e.g. by moving another window. */
29491 if (FRAME_FACE_CACHE (f) == NULL
29492 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29494 TRACE ((stderr, " no faces\n"));
29495 return;
29498 if (w == 0 || h == 0)
29500 r.x = r.y = 0;
29501 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29502 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29504 else
29506 r.x = x;
29507 r.y = y;
29508 r.width = w;
29509 r.height = h;
29512 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29513 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29515 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29516 if (WINDOWP (f->tool_bar_window))
29517 mouse_face_overwritten_p
29518 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29519 #endif
29521 #ifdef HAVE_X_WINDOWS
29522 #ifndef MSDOS
29523 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29524 if (WINDOWP (f->menu_bar_window))
29525 mouse_face_overwritten_p
29526 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29527 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29528 #endif
29529 #endif
29531 /* Some window managers support a focus-follows-mouse style with
29532 delayed raising of frames. Imagine a partially obscured frame,
29533 and moving the mouse into partially obscured mouse-face on that
29534 frame. The visible part of the mouse-face will be highlighted,
29535 then the WM raises the obscured frame. With at least one WM, KDE
29536 2.1, Emacs is not getting any event for the raising of the frame
29537 (even tried with SubstructureRedirectMask), only Expose events.
29538 These expose events will draw text normally, i.e. not
29539 highlighted. Which means we must redo the highlight here.
29540 Subsume it under ``we love X''. --gerd 2001-08-15 */
29541 /* Included in Windows version because Windows most likely does not
29542 do the right thing if any third party tool offers
29543 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29544 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29546 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29547 if (f == hlinfo->mouse_face_mouse_frame)
29549 int mouse_x = hlinfo->mouse_face_mouse_x;
29550 int mouse_y = hlinfo->mouse_face_mouse_y;
29551 clear_mouse_face (hlinfo);
29552 note_mouse_highlight (f, mouse_x, mouse_y);
29558 /* EXPORT:
29559 Determine the intersection of two rectangles R1 and R2. Return
29560 the intersection in *RESULT. Value is non-zero if RESULT is not
29561 empty. */
29564 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29566 XRectangle *left, *right;
29567 XRectangle *upper, *lower;
29568 int intersection_p = 0;
29570 /* Rearrange so that R1 is the left-most rectangle. */
29571 if (r1->x < r2->x)
29572 left = r1, right = r2;
29573 else
29574 left = r2, right = r1;
29576 /* X0 of the intersection is right.x0, if this is inside R1,
29577 otherwise there is no intersection. */
29578 if (right->x <= left->x + left->width)
29580 result->x = right->x;
29582 /* The right end of the intersection is the minimum of
29583 the right ends of left and right. */
29584 result->width = (min (left->x + left->width, right->x + right->width)
29585 - result->x);
29587 /* Same game for Y. */
29588 if (r1->y < r2->y)
29589 upper = r1, lower = r2;
29590 else
29591 upper = r2, lower = r1;
29593 /* The upper end of the intersection is lower.y0, if this is inside
29594 of upper. Otherwise, there is no intersection. */
29595 if (lower->y <= upper->y + upper->height)
29597 result->y = lower->y;
29599 /* The lower end of the intersection is the minimum of the lower
29600 ends of upper and lower. */
29601 result->height = (min (lower->y + lower->height,
29602 upper->y + upper->height)
29603 - result->y);
29604 intersection_p = 1;
29608 return intersection_p;
29611 #endif /* HAVE_WINDOW_SYSTEM */
29614 /***********************************************************************
29615 Initialization
29616 ***********************************************************************/
29618 void
29619 syms_of_xdisp (void)
29621 Vwith_echo_area_save_vector = Qnil;
29622 staticpro (&Vwith_echo_area_save_vector);
29624 Vmessage_stack = Qnil;
29625 staticpro (&Vmessage_stack);
29627 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29628 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29630 message_dolog_marker1 = Fmake_marker ();
29631 staticpro (&message_dolog_marker1);
29632 message_dolog_marker2 = Fmake_marker ();
29633 staticpro (&message_dolog_marker2);
29634 message_dolog_marker3 = Fmake_marker ();
29635 staticpro (&message_dolog_marker3);
29637 #ifdef GLYPH_DEBUG
29638 defsubr (&Sdump_frame_glyph_matrix);
29639 defsubr (&Sdump_glyph_matrix);
29640 defsubr (&Sdump_glyph_row);
29641 defsubr (&Sdump_tool_bar_row);
29642 defsubr (&Strace_redisplay);
29643 defsubr (&Strace_to_stderr);
29644 #endif
29645 #ifdef HAVE_WINDOW_SYSTEM
29646 defsubr (&Stool_bar_height);
29647 defsubr (&Slookup_image_map);
29648 #endif
29649 defsubr (&Sline_pixel_height);
29650 defsubr (&Sformat_mode_line);
29651 defsubr (&Sinvisible_p);
29652 defsubr (&Scurrent_bidi_paragraph_direction);
29653 defsubr (&Swindow_text_pixel_size);
29654 defsubr (&Smove_point_visually);
29656 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29657 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29658 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29659 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29660 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29661 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29662 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29663 DEFSYM (Qeval, "eval");
29664 DEFSYM (QCdata, ":data");
29665 DEFSYM (Qdisplay, "display");
29666 DEFSYM (Qspace_width, "space-width");
29667 DEFSYM (Qraise, "raise");
29668 DEFSYM (Qslice, "slice");
29669 DEFSYM (Qspace, "space");
29670 DEFSYM (Qmargin, "margin");
29671 DEFSYM (Qpointer, "pointer");
29672 DEFSYM (Qleft_margin, "left-margin");
29673 DEFSYM (Qright_margin, "right-margin");
29674 DEFSYM (Qcenter, "center");
29675 DEFSYM (Qline_height, "line-height");
29676 DEFSYM (QCalign_to, ":align-to");
29677 DEFSYM (QCrelative_width, ":relative-width");
29678 DEFSYM (QCrelative_height, ":relative-height");
29679 DEFSYM (QCeval, ":eval");
29680 DEFSYM (QCpropertize, ":propertize");
29681 DEFSYM (QCfile, ":file");
29682 DEFSYM (Qfontified, "fontified");
29683 DEFSYM (Qfontification_functions, "fontification-functions");
29684 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29685 DEFSYM (Qescape_glyph, "escape-glyph");
29686 DEFSYM (Qnobreak_space, "nobreak-space");
29687 DEFSYM (Qimage, "image");
29688 DEFSYM (Qtext, "text");
29689 DEFSYM (Qboth, "both");
29690 DEFSYM (Qboth_horiz, "both-horiz");
29691 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29692 DEFSYM (QCmap, ":map");
29693 DEFSYM (QCpointer, ":pointer");
29694 DEFSYM (Qrect, "rect");
29695 DEFSYM (Qcircle, "circle");
29696 DEFSYM (Qpoly, "poly");
29697 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29698 DEFSYM (Qgrow_only, "grow-only");
29699 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29700 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29701 DEFSYM (Qposition, "position");
29702 DEFSYM (Qbuffer_position, "buffer-position");
29703 DEFSYM (Qobject, "object");
29704 DEFSYM (Qbar, "bar");
29705 DEFSYM (Qhbar, "hbar");
29706 DEFSYM (Qbox, "box");
29707 DEFSYM (Qhollow, "hollow");
29708 DEFSYM (Qhand, "hand");
29709 DEFSYM (Qarrow, "arrow");
29710 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29712 list_of_error = list1 (list2 (intern_c_string ("error"),
29713 intern_c_string ("void-variable")));
29714 staticpro (&list_of_error);
29716 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29717 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29718 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29719 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29721 echo_buffer[0] = echo_buffer[1] = Qnil;
29722 staticpro (&echo_buffer[0]);
29723 staticpro (&echo_buffer[1]);
29725 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29726 staticpro (&echo_area_buffer[0]);
29727 staticpro (&echo_area_buffer[1]);
29729 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29730 staticpro (&Vmessages_buffer_name);
29732 mode_line_proptrans_alist = Qnil;
29733 staticpro (&mode_line_proptrans_alist);
29734 mode_line_string_list = Qnil;
29735 staticpro (&mode_line_string_list);
29736 mode_line_string_face = Qnil;
29737 staticpro (&mode_line_string_face);
29738 mode_line_string_face_prop = Qnil;
29739 staticpro (&mode_line_string_face_prop);
29740 Vmode_line_unwind_vector = Qnil;
29741 staticpro (&Vmode_line_unwind_vector);
29743 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29745 help_echo_string = Qnil;
29746 staticpro (&help_echo_string);
29747 help_echo_object = Qnil;
29748 staticpro (&help_echo_object);
29749 help_echo_window = Qnil;
29750 staticpro (&help_echo_window);
29751 previous_help_echo_string = Qnil;
29752 staticpro (&previous_help_echo_string);
29753 help_echo_pos = -1;
29755 DEFSYM (Qright_to_left, "right-to-left");
29756 DEFSYM (Qleft_to_right, "left-to-right");
29758 #ifdef HAVE_WINDOW_SYSTEM
29759 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29760 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29761 For example, if a block cursor is over a tab, it will be drawn as
29762 wide as that tab on the display. */);
29763 x_stretch_cursor_p = 0;
29764 #endif
29766 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29767 doc: /* Non-nil means highlight trailing whitespace.
29768 The face used for trailing whitespace is `trailing-whitespace'. */);
29769 Vshow_trailing_whitespace = Qnil;
29771 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29772 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29773 If the value is t, Emacs highlights non-ASCII chars which have the
29774 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29775 or `escape-glyph' face respectively.
29777 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29778 U+2011 (non-breaking hyphen) are affected.
29780 Any other non-nil value means to display these characters as a escape
29781 glyph followed by an ordinary space or hyphen.
29783 A value of nil means no special handling of these characters. */);
29784 Vnobreak_char_display = Qt;
29786 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29787 doc: /* The pointer shape to show in void text areas.
29788 A value of nil means to show the text pointer. Other options are `arrow',
29789 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29790 Vvoid_text_area_pointer = Qarrow;
29792 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29793 doc: /* Non-nil means don't actually do any redisplay.
29794 This is used for internal purposes. */);
29795 Vinhibit_redisplay = Qnil;
29797 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29798 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29799 Vglobal_mode_string = Qnil;
29801 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29802 doc: /* Marker for where to display an arrow on top of the buffer text.
29803 This must be the beginning of a line in order to work.
29804 See also `overlay-arrow-string'. */);
29805 Voverlay_arrow_position = Qnil;
29807 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29808 doc: /* String to display as an arrow in non-window frames.
29809 See also `overlay-arrow-position'. */);
29810 Voverlay_arrow_string = build_pure_c_string ("=>");
29812 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29813 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29814 The symbols on this list are examined during redisplay to determine
29815 where to display overlay arrows. */);
29816 Voverlay_arrow_variable_list
29817 = list1 (intern_c_string ("overlay-arrow-position"));
29819 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29820 doc: /* The number of lines to try scrolling a window by when point moves out.
29821 If that fails to bring point back on frame, point is centered instead.
29822 If this is zero, point is always centered after it moves off frame.
29823 If you want scrolling to always be a line at a time, you should set
29824 `scroll-conservatively' to a large value rather than set this to 1. */);
29826 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29827 doc: /* Scroll up to this many lines, to bring point back on screen.
29828 If point moves off-screen, redisplay will scroll by up to
29829 `scroll-conservatively' lines in order to bring point just barely
29830 onto the screen again. If that cannot be done, then redisplay
29831 recenters point as usual.
29833 If the value is greater than 100, redisplay will never recenter point,
29834 but will always scroll just enough text to bring point into view, even
29835 if you move far away.
29837 A value of zero means always recenter point if it moves off screen. */);
29838 scroll_conservatively = 0;
29840 DEFVAR_INT ("scroll-margin", scroll_margin,
29841 doc: /* Number of lines of margin at the top and bottom of a window.
29842 Recenter the window whenever point gets within this many lines
29843 of the top or bottom of the window. */);
29844 scroll_margin = 0;
29846 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29847 doc: /* Pixels per inch value for non-window system displays.
29848 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29849 Vdisplay_pixels_per_inch = make_float (72.0);
29851 #ifdef GLYPH_DEBUG
29852 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29853 #endif
29855 DEFVAR_LISP ("truncate-partial-width-windows",
29856 Vtruncate_partial_width_windows,
29857 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29858 For an integer value, truncate lines in each window narrower than the
29859 full frame width, provided the window width is less than that integer;
29860 otherwise, respect the value of `truncate-lines'.
29862 For any other non-nil value, truncate lines in all windows that do
29863 not span the full frame width.
29865 A value of nil means to respect the value of `truncate-lines'.
29867 If `word-wrap' is enabled, you might want to reduce this. */);
29868 Vtruncate_partial_width_windows = make_number (50);
29870 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29871 doc: /* Maximum buffer size for which line number should be displayed.
29872 If the buffer is bigger than this, the line number does not appear
29873 in the mode line. A value of nil means no limit. */);
29874 Vline_number_display_limit = Qnil;
29876 DEFVAR_INT ("line-number-display-limit-width",
29877 line_number_display_limit_width,
29878 doc: /* Maximum line width (in characters) for line number display.
29879 If the average length of the lines near point is bigger than this, then the
29880 line number may be omitted from the mode line. */);
29881 line_number_display_limit_width = 200;
29883 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29884 doc: /* Non-nil means highlight region even in nonselected windows. */);
29885 highlight_nonselected_windows = 0;
29887 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29888 doc: /* Non-nil if more than one frame is visible on this display.
29889 Minibuffer-only frames don't count, but iconified frames do.
29890 This variable is not guaranteed to be accurate except while processing
29891 `frame-title-format' and `icon-title-format'. */);
29893 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29894 doc: /* Template for displaying the title bar of visible frames.
29895 \(Assuming the window manager supports this feature.)
29897 This variable has the same structure as `mode-line-format', except that
29898 the %c and %l constructs are ignored. It is used only on frames for
29899 which no explicit name has been set \(see `modify-frame-parameters'). */);
29901 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29902 doc: /* Template for displaying the title bar of an iconified frame.
29903 \(Assuming the window manager supports this feature.)
29904 This variable has the same structure as `mode-line-format' (which see),
29905 and is used only on frames for which no explicit name has been set
29906 \(see `modify-frame-parameters'). */);
29907 Vicon_title_format
29908 = Vframe_title_format
29909 = listn (CONSTYPE_PURE, 3,
29910 intern_c_string ("multiple-frames"),
29911 build_pure_c_string ("%b"),
29912 listn (CONSTYPE_PURE, 4,
29913 empty_unibyte_string,
29914 intern_c_string ("invocation-name"),
29915 build_pure_c_string ("@"),
29916 intern_c_string ("system-name")));
29918 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29919 doc: /* Maximum number of lines to keep in the message log buffer.
29920 If nil, disable message logging. If t, log messages but don't truncate
29921 the buffer when it becomes large. */);
29922 Vmessage_log_max = make_number (1000);
29924 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29925 doc: /* Functions called before redisplay, if window sizes have changed.
29926 The value should be a list of functions that take one argument.
29927 Just before redisplay, for each frame, if any of its windows have changed
29928 size since the last redisplay, or have been split or deleted,
29929 all the functions in the list are called, with the frame as argument. */);
29930 Vwindow_size_change_functions = Qnil;
29932 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29933 doc: /* List of functions to call before redisplaying a window with scrolling.
29934 Each function is called with two arguments, the window and its new
29935 display-start position. Note that these functions are also called by
29936 `set-window-buffer'. Also note that the value of `window-end' is not
29937 valid when these functions are called.
29939 Warning: Do not use this feature to alter the way the window
29940 is scrolled. It is not designed for that, and such use probably won't
29941 work. */);
29942 Vwindow_scroll_functions = Qnil;
29944 DEFVAR_LISP ("window-text-change-functions",
29945 Vwindow_text_change_functions,
29946 doc: /* Functions to call in redisplay when text in the window might change. */);
29947 Vwindow_text_change_functions = Qnil;
29949 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29950 doc: /* Functions called when redisplay of a window reaches the end trigger.
29951 Each function is called with two arguments, the window and the end trigger value.
29952 See `set-window-redisplay-end-trigger'. */);
29953 Vredisplay_end_trigger_functions = Qnil;
29955 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29956 doc: /* Non-nil means autoselect window with mouse pointer.
29957 If nil, do not autoselect windows.
29958 A positive number means delay autoselection by that many seconds: a
29959 window is autoselected only after the mouse has remained in that
29960 window for the duration of the delay.
29961 A negative number has a similar effect, but causes windows to be
29962 autoselected only after the mouse has stopped moving. \(Because of
29963 the way Emacs compares mouse events, you will occasionally wait twice
29964 that time before the window gets selected.\)
29965 Any other value means to autoselect window instantaneously when the
29966 mouse pointer enters it.
29968 Autoselection selects the minibuffer only if it is active, and never
29969 unselects the minibuffer if it is active.
29971 When customizing this variable make sure that the actual value of
29972 `focus-follows-mouse' matches the behavior of your window manager. */);
29973 Vmouse_autoselect_window = Qnil;
29975 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29976 doc: /* Non-nil means automatically resize tool-bars.
29977 This dynamically changes the tool-bar's height to the minimum height
29978 that is needed to make all tool-bar items visible.
29979 If value is `grow-only', the tool-bar's height is only increased
29980 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29981 Vauto_resize_tool_bars = Qt;
29983 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29984 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29985 auto_raise_tool_bar_buttons_p = 1;
29987 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29988 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29989 make_cursor_line_fully_visible_p = 1;
29991 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29992 doc: /* Border below tool-bar in pixels.
29993 If an integer, use it as the height of the border.
29994 If it is one of `internal-border-width' or `border-width', use the
29995 value of the corresponding frame parameter.
29996 Otherwise, no border is added below the tool-bar. */);
29997 Vtool_bar_border = Qinternal_border_width;
29999 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30000 doc: /* Margin around tool-bar buttons in pixels.
30001 If an integer, use that for both horizontal and vertical margins.
30002 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30003 HORZ specifying the horizontal margin, and VERT specifying the
30004 vertical margin. */);
30005 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30007 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30008 doc: /* Relief thickness of tool-bar buttons. */);
30009 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30011 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30012 doc: /* Tool bar style to use.
30013 It can be one of
30014 image - show images only
30015 text - show text only
30016 both - show both, text below image
30017 both-horiz - show text to the right of the image
30018 text-image-horiz - show text to the left of the image
30019 any other - use system default or image if no system default.
30021 This variable only affects the GTK+ toolkit version of Emacs. */);
30022 Vtool_bar_style = Qnil;
30024 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30025 doc: /* Maximum number of characters a label can have to be shown.
30026 The tool bar style must also show labels for this to have any effect, see
30027 `tool-bar-style'. */);
30028 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30030 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30031 doc: /* List of functions to call to fontify regions of text.
30032 Each function is called with one argument POS. Functions must
30033 fontify a region starting at POS in the current buffer, and give
30034 fontified regions the property `fontified'. */);
30035 Vfontification_functions = Qnil;
30036 Fmake_variable_buffer_local (Qfontification_functions);
30038 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30039 unibyte_display_via_language_environment,
30040 doc: /* Non-nil means display unibyte text according to language environment.
30041 Specifically, this means that raw bytes in the range 160-255 decimal
30042 are displayed by converting them to the equivalent multibyte characters
30043 according to the current language environment. As a result, they are
30044 displayed according to the current fontset.
30046 Note that this variable affects only how these bytes are displayed,
30047 but does not change the fact they are interpreted as raw bytes. */);
30048 unibyte_display_via_language_environment = 0;
30050 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30051 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30052 If a float, it specifies a fraction of the mini-window frame's height.
30053 If an integer, it specifies a number of lines. */);
30054 Vmax_mini_window_height = make_float (0.25);
30056 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30057 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30058 A value of nil means don't automatically resize mini-windows.
30059 A value of t means resize them to fit the text displayed in them.
30060 A value of `grow-only', the default, means let mini-windows grow only;
30061 they return to their normal size when the minibuffer is closed, or the
30062 echo area becomes empty. */);
30063 Vresize_mini_windows = Qgrow_only;
30065 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30066 doc: /* Alist specifying how to blink the cursor off.
30067 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30068 `cursor-type' frame-parameter or variable equals ON-STATE,
30069 comparing using `equal', Emacs uses OFF-STATE to specify
30070 how to blink it off. ON-STATE and OFF-STATE are values for
30071 the `cursor-type' frame parameter.
30073 If a frame's ON-STATE has no entry in this list,
30074 the frame's other specifications determine how to blink the cursor off. */);
30075 Vblink_cursor_alist = Qnil;
30077 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30078 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30079 If non-nil, windows are automatically scrolled horizontally to make
30080 point visible. */);
30081 automatic_hscrolling_p = 1;
30082 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30084 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30085 doc: /* How many columns away from the window edge point is allowed to get
30086 before automatic hscrolling will horizontally scroll the window. */);
30087 hscroll_margin = 5;
30089 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30090 doc: /* How many columns to scroll the window when point gets too close to the edge.
30091 When point is less than `hscroll-margin' columns from the window
30092 edge, automatic hscrolling will scroll the window by the amount of columns
30093 determined by this variable. If its value is a positive integer, scroll that
30094 many columns. If it's a positive floating-point number, it specifies the
30095 fraction of the window's width to scroll. If it's nil or zero, point will be
30096 centered horizontally after the scroll. Any other value, including negative
30097 numbers, are treated as if the value were zero.
30099 Automatic hscrolling always moves point outside the scroll margin, so if
30100 point was more than scroll step columns inside the margin, the window will
30101 scroll more than the value given by the scroll step.
30103 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30104 and `scroll-right' overrides this variable's effect. */);
30105 Vhscroll_step = make_number (0);
30107 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30108 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30109 Bind this around calls to `message' to let it take effect. */);
30110 message_truncate_lines = 0;
30112 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30113 doc: /* Normal hook run to update the menu bar definitions.
30114 Redisplay runs this hook before it redisplays the menu bar.
30115 This is used to update submenus such as Buffers,
30116 whose contents depend on various data. */);
30117 Vmenu_bar_update_hook = Qnil;
30119 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30120 doc: /* Frame for which we are updating a menu.
30121 The enable predicate for a menu binding should check this variable. */);
30122 Vmenu_updating_frame = Qnil;
30124 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30125 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30126 inhibit_menubar_update = 0;
30128 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30129 doc: /* Prefix prepended to all continuation lines at display time.
30130 The value may be a string, an image, or a stretch-glyph; it is
30131 interpreted in the same way as the value of a `display' text property.
30133 This variable is overridden by any `wrap-prefix' text or overlay
30134 property.
30136 To add a prefix to non-continuation lines, use `line-prefix'. */);
30137 Vwrap_prefix = Qnil;
30138 DEFSYM (Qwrap_prefix, "wrap-prefix");
30139 Fmake_variable_buffer_local (Qwrap_prefix);
30141 DEFVAR_LISP ("line-prefix", Vline_prefix,
30142 doc: /* Prefix prepended to all non-continuation lines at display time.
30143 The value may be a string, an image, or a stretch-glyph; it is
30144 interpreted in the same way as the value of a `display' text property.
30146 This variable is overridden by any `line-prefix' text or overlay
30147 property.
30149 To add a prefix to continuation lines, use `wrap-prefix'. */);
30150 Vline_prefix = Qnil;
30151 DEFSYM (Qline_prefix, "line-prefix");
30152 Fmake_variable_buffer_local (Qline_prefix);
30154 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30155 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30156 inhibit_eval_during_redisplay = 0;
30158 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30159 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30160 inhibit_free_realized_faces = 0;
30162 #ifdef GLYPH_DEBUG
30163 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30164 doc: /* Inhibit try_window_id display optimization. */);
30165 inhibit_try_window_id = 0;
30167 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30168 doc: /* Inhibit try_window_reusing display optimization. */);
30169 inhibit_try_window_reusing = 0;
30171 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30172 doc: /* Inhibit try_cursor_movement display optimization. */);
30173 inhibit_try_cursor_movement = 0;
30174 #endif /* GLYPH_DEBUG */
30176 DEFVAR_INT ("overline-margin", overline_margin,
30177 doc: /* Space between overline and text, in pixels.
30178 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30179 margin to the character height. */);
30180 overline_margin = 2;
30182 DEFVAR_INT ("underline-minimum-offset",
30183 underline_minimum_offset,
30184 doc: /* Minimum distance between baseline and underline.
30185 This can improve legibility of underlined text at small font sizes,
30186 particularly when using variable `x-use-underline-position-properties'
30187 with fonts that specify an UNDERLINE_POSITION relatively close to the
30188 baseline. The default value is 1. */);
30189 underline_minimum_offset = 1;
30191 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30192 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30193 This feature only works when on a window system that can change
30194 cursor shapes. */);
30195 display_hourglass_p = 1;
30197 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30198 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30199 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30201 #ifdef HAVE_WINDOW_SYSTEM
30202 hourglass_atimer = NULL;
30203 hourglass_shown_p = 0;
30204 #endif /* HAVE_WINDOW_SYSTEM */
30206 DEFSYM (Qglyphless_char, "glyphless-char");
30207 DEFSYM (Qhex_code, "hex-code");
30208 DEFSYM (Qempty_box, "empty-box");
30209 DEFSYM (Qthin_space, "thin-space");
30210 DEFSYM (Qzero_width, "zero-width");
30212 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30213 doc: /* Function run just before redisplay.
30214 It is called with one argument, which is the set of windows that are to
30215 be redisplayed. This set can be nil (meaning, only the selected window),
30216 or t (meaning all windows). */);
30217 Vpre_redisplay_function = intern ("ignore");
30219 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30220 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30222 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30223 doc: /* Char-table defining glyphless characters.
30224 Each element, if non-nil, should be one of the following:
30225 an ASCII acronym string: display this string in a box
30226 `hex-code': display the hexadecimal code of a character in a box
30227 `empty-box': display as an empty box
30228 `thin-space': display as 1-pixel width space
30229 `zero-width': don't display
30230 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30231 display method for graphical terminals and text terminals respectively.
30232 GRAPHICAL and TEXT should each have one of the values listed above.
30234 The char-table has one extra slot to control the display of a character for
30235 which no font is found. This slot only takes effect on graphical terminals.
30236 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30237 `thin-space'. The default is `empty-box'.
30239 If a character has a non-nil entry in an active display table, the
30240 display table takes effect; in this case, Emacs does not consult
30241 `glyphless-char-display' at all. */);
30242 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30243 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30244 Qempty_box);
30246 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30247 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30248 Vdebug_on_message = Qnil;
30250 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30251 doc: /* */);
30252 Vredisplay__all_windows_cause
30253 = Fmake_vector (make_number (100), make_number (0));
30255 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30256 doc: /* */);
30257 Vredisplay__mode_lines_cause
30258 = Fmake_vector (make_number (100), make_number (0));
30262 /* Initialize this module when Emacs starts. */
30264 void
30265 init_xdisp (void)
30267 CHARPOS (this_line_start_pos) = 0;
30269 if (!noninteractive)
30271 struct window *m = XWINDOW (minibuf_window);
30272 Lisp_Object frame = m->frame;
30273 struct frame *f = XFRAME (frame);
30274 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30275 struct window *r = XWINDOW (root);
30276 int i;
30278 echo_area_window = minibuf_window;
30280 r->top_line = FRAME_TOP_MARGIN (f);
30281 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30282 r->total_cols = FRAME_COLS (f);
30283 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30284 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30285 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30287 m->top_line = FRAME_LINES (f) - 1;
30288 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30289 m->total_cols = FRAME_COLS (f);
30290 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30291 m->total_lines = 1;
30292 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30294 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30295 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30296 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30298 /* The default ellipsis glyphs `...'. */
30299 for (i = 0; i < 3; ++i)
30300 default_invis_vector[i] = make_number ('.');
30304 /* Allocate the buffer for frame titles.
30305 Also used for `format-mode-line'. */
30306 int size = 100;
30307 mode_line_noprop_buf = xmalloc (size);
30308 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30309 mode_line_noprop_ptr = mode_line_noprop_buf;
30310 mode_line_target = MODE_LINE_DISPLAY;
30313 help_echo_showing_p = 0;
30316 #ifdef HAVE_WINDOW_SYSTEM
30318 /* Platform-independent portion of hourglass implementation. */
30320 /* Cancel a currently active hourglass timer, and start a new one. */
30321 void
30322 start_hourglass (void)
30324 struct timespec delay;
30326 cancel_hourglass ();
30328 if (INTEGERP (Vhourglass_delay)
30329 && XINT (Vhourglass_delay) > 0)
30330 delay = make_timespec (min (XINT (Vhourglass_delay),
30331 TYPE_MAXIMUM (time_t)),
30333 else if (FLOATP (Vhourglass_delay)
30334 && XFLOAT_DATA (Vhourglass_delay) > 0)
30335 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30336 else
30337 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30339 #ifdef HAVE_NTGUI
30341 extern void w32_note_current_window (void);
30342 w32_note_current_window ();
30344 #endif /* HAVE_NTGUI */
30346 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30347 show_hourglass, NULL);
30351 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30352 shown. */
30353 void
30354 cancel_hourglass (void)
30356 if (hourglass_atimer)
30358 cancel_atimer (hourglass_atimer);
30359 hourglass_atimer = NULL;
30362 if (hourglass_shown_p)
30363 hide_hourglass ();
30366 #endif /* HAVE_WINDOW_SYSTEM */