Fix minor problems found by static checking.
[emacs.git] / src / xdisp.c
blob6357f4f5f3b113376ae8551b1dddb04c5d6c823d
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_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 redisplay_other_windows ();
624 w->redisplay = true;
627 void
628 fset_redisplay (struct frame *f)
630 redisplay_other_windows ();
631 f->redisplay = true;
634 void
635 bset_redisplay (struct buffer *b)
637 int count = buffer_window_count (b);
638 if (count > 0)
640 /* ... it's visible in other window than selected, */
641 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
642 redisplay_other_windows ();
643 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
644 so that if we later set windows_or_buffers_changed, this buffer will
645 not be omitted. */
646 b->text->redisplay = true;
650 void
651 bset_update_mode_line (struct buffer *b)
653 if (!update_mode_lines)
654 update_mode_lines = REDISPLAY_SOME;
655 b->text->redisplay = true;
658 #ifdef GLYPH_DEBUG
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG defined. */
663 int trace_redisplay_p;
665 #endif /* GLYPH_DEBUG */
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
676 static Lisp_Object Qauto_hscroll_mode;
678 /* Buffer being redisplayed -- for redisplay_window_error. */
680 static struct buffer *displayed_buffer;
682 /* Value returned from text property handlers (see below). */
684 enum prop_handled
686 HANDLED_NORMALLY,
687 HANDLED_RECOMPUTE_PROPS,
688 HANDLED_OVERLAY_STRING_CONSUMED,
689 HANDLED_RETURN
692 /* A description of text properties that redisplay is interested
693 in. */
695 struct props
697 /* The name of the property. */
698 Lisp_Object *name;
700 /* A unique index for the property. */
701 enum prop_idx idx;
703 /* A handler function called to set up iterator IT from the property
704 at IT's current position. Value is used to steer handle_stop. */
705 enum prop_handled (*handler) (struct it *it);
708 static enum prop_handled handle_face_prop (struct it *);
709 static enum prop_handled handle_invisible_prop (struct it *);
710 static enum prop_handled handle_display_prop (struct it *);
711 static enum prop_handled handle_composition_prop (struct it *);
712 static enum prop_handled handle_overlay_change (struct it *);
713 static enum prop_handled handle_fontified_prop (struct it *);
715 /* Properties handled by iterators. */
717 static struct props it_props[] =
719 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
720 /* Handle `face' before `display' because some sub-properties of
721 `display' need to know the face. */
722 {&Qface, FACE_PROP_IDX, handle_face_prop},
723 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
724 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
725 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
726 {NULL, 0, NULL}
729 /* Value is the position described by X. If X is a marker, value is
730 the marker_position of X. Otherwise, value is X. */
732 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
734 /* Enumeration returned by some move_it_.* functions internally. */
736 enum move_it_result
738 /* Not used. Undefined value. */
739 MOVE_UNDEFINED,
741 /* Move ended at the requested buffer position or ZV. */
742 MOVE_POS_MATCH_OR_ZV,
744 /* Move ended at the requested X pixel position. */
745 MOVE_X_REACHED,
747 /* Move within a line ended at the end of a line that must be
748 continued. */
749 MOVE_LINE_CONTINUED,
751 /* Move within a line ended at the end of a line that would
752 be displayed truncated. */
753 MOVE_LINE_TRUNCATED,
755 /* Move within a line ended at a line end. */
756 MOVE_NEWLINE_OR_CR
759 /* This counter is used to clear the face cache every once in a while
760 in redisplay_internal. It is incremented for each redisplay.
761 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
762 cleared. */
764 #define CLEAR_FACE_CACHE_COUNT 500
765 static int clear_face_cache_count;
767 /* Similarly for the image cache. */
769 #ifdef HAVE_WINDOW_SYSTEM
770 #define CLEAR_IMAGE_CACHE_COUNT 101
771 static int clear_image_cache_count;
773 /* Null glyph slice */
774 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
775 #endif
777 /* True while redisplay_internal is in progress. */
779 bool redisplaying_p;
781 static Lisp_Object Qinhibit_free_realized_faces;
782 static Lisp_Object Qmode_line_default_help_echo;
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 ptrdiff_t help_echo_pos;
792 /* Temporary variable for XTread_socket. */
794 Lisp_Object previous_help_echo_string;
796 /* Platform-independent portion of hourglass implementation. */
798 #ifdef HAVE_WINDOW_SYSTEM
800 /* Non-zero means an hourglass cursor is currently shown. */
801 bool hourglass_shown_p;
803 /* If non-null, an asynchronous timer that, when it expires, displays
804 an hourglass cursor on all frames. */
805 struct atimer *hourglass_atimer;
807 #endif /* HAVE_WINDOW_SYSTEM */
809 /* Name of the face used to display glyphless characters. */
810 static Lisp_Object Qglyphless_char;
812 /* Symbol for the purpose of Vglyphless_char_display. */
813 static Lisp_Object Qglyphless_char_display;
815 /* Method symbols for Vglyphless_char_display. */
816 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
818 /* Default number of seconds to wait before displaying an hourglass
819 cursor. */
820 #define DEFAULT_HOURGLASS_DELAY 1
822 #ifdef HAVE_WINDOW_SYSTEM
824 /* Default pixel width of `thin-space' display method. */
825 #define THIN_SPACE_WIDTH 1
827 #endif /* HAVE_WINDOW_SYSTEM */
829 /* Function prototypes. */
831 static void setup_for_ellipsis (struct it *, int);
832 static void set_iterator_to_next (struct it *, int);
833 static void mark_window_display_accurate_1 (struct window *, int);
834 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
835 static int display_prop_string_p (Lisp_Object, Lisp_Object);
836 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
837 static int cursor_row_p (struct glyph_row *);
838 static int redisplay_mode_lines (Lisp_Object, int);
839 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
841 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
843 static void handle_line_prefix (struct it *);
845 static void pint2str (char *, int, ptrdiff_t);
846 static void pint2hrstr (char *, int, ptrdiff_t);
847 static struct text_pos run_window_scroll_functions (Lisp_Object,
848 struct text_pos);
849 static int text_outside_line_unchanged_p (struct window *,
850 ptrdiff_t, ptrdiff_t);
851 static void store_mode_line_noprop_char (char);
852 static int store_mode_line_noprop (const char *, int, int);
853 static void handle_stop (struct it *);
854 static void handle_stop_backwards (struct it *, ptrdiff_t);
855 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
856 static void ensure_echo_area_buffers (void);
857 static void unwind_with_echo_area_buffer (Lisp_Object);
858 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
859 static int with_echo_area_buffer (struct window *, int,
860 int (*) (ptrdiff_t, Lisp_Object),
861 ptrdiff_t, Lisp_Object);
862 static void clear_garbaged_frames (void);
863 static int current_message_1 (ptrdiff_t, Lisp_Object);
864 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
865 static void set_message (Lisp_Object);
866 static int set_message_1 (ptrdiff_t, Lisp_Object);
867 static int display_echo_area (struct window *);
868 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
869 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
870 static void unwind_redisplay (void);
871 static int string_char_and_length (const unsigned char *, int *);
872 static struct text_pos display_prop_end (struct it *, Lisp_Object,
873 struct text_pos);
874 static int compute_window_start_on_continuation_line (struct window *);
875 static void insert_left_trunc_glyphs (struct it *);
876 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
877 Lisp_Object);
878 static void extend_face_to_end_of_line (struct it *);
879 static int append_space_for_newline (struct it *, int);
880 static int cursor_row_fully_visible_p (struct window *, int, int);
881 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
882 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
883 static int trailing_whitespace_p (ptrdiff_t);
884 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
885 static void push_it (struct it *, struct text_pos *);
886 static void iterate_out_of_display_property (struct it *);
887 static void pop_it (struct it *);
888 static void sync_frame_with_window_matrix_rows (struct window *);
889 static void redisplay_internal (void);
890 static int echo_area_display (int);
891 static void redisplay_windows (Lisp_Object);
892 static void redisplay_window (Lisp_Object, int);
893 static Lisp_Object redisplay_window_error (Lisp_Object);
894 static Lisp_Object redisplay_window_0 (Lisp_Object);
895 static Lisp_Object redisplay_window_1 (Lisp_Object);
896 static int set_cursor_from_row (struct window *, struct glyph_row *,
897 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
898 int, int);
899 static int update_menu_bar (struct frame *, int, int);
900 static int try_window_reusing_current_matrix (struct window *);
901 static int try_window_id (struct window *);
902 static int display_line (struct it *);
903 static int display_mode_lines (struct window *);
904 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
905 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
906 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
907 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
908 static void display_menu_bar (struct window *);
909 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
910 ptrdiff_t *);
911 static int display_string (const char *, Lisp_Object, Lisp_Object,
912 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
913 static void compute_line_metrics (struct it *);
914 static void run_redisplay_end_trigger_hook (struct it *);
915 static int get_overlay_strings (struct it *, ptrdiff_t);
916 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
917 static void next_overlay_string (struct it *);
918 static void reseat (struct it *, struct text_pos, int);
919 static void reseat_1 (struct it *, struct text_pos, int);
920 static void back_to_previous_visible_line_start (struct it *);
921 static void reseat_at_next_visible_line_start (struct it *, int);
922 static int next_element_from_ellipsis (struct it *);
923 static int next_element_from_display_vector (struct it *);
924 static int next_element_from_string (struct it *);
925 static int next_element_from_c_string (struct it *);
926 static int next_element_from_buffer (struct it *);
927 static int next_element_from_composition (struct it *);
928 static int next_element_from_image (struct it *);
929 static int next_element_from_stretch (struct it *);
930 static void load_overlay_strings (struct it *, ptrdiff_t);
931 static int init_from_display_pos (struct it *, struct window *,
932 struct display_pos *);
933 static void reseat_to_string (struct it *, const char *,
934 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
935 static int get_next_display_element (struct it *);
936 static enum move_it_result
937 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
938 enum move_operation_enum);
939 static void get_visually_first_element (struct it *);
940 static void init_to_row_start (struct it *, struct window *,
941 struct glyph_row *);
942 static int init_to_row_end (struct it *, struct window *,
943 struct glyph_row *);
944 static void back_to_previous_line_start (struct it *);
945 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
946 static struct text_pos string_pos_nchars_ahead (struct text_pos,
947 Lisp_Object, ptrdiff_t);
948 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
949 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
950 static ptrdiff_t number_of_chars (const char *, bool);
951 static void compute_stop_pos (struct it *);
952 static void compute_string_pos (struct text_pos *, struct text_pos,
953 Lisp_Object);
954 static int face_before_or_after_it_pos (struct it *, int);
955 static ptrdiff_t next_overlay_change (ptrdiff_t);
956 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
957 Lisp_Object, struct text_pos *, ptrdiff_t, int);
958 static int handle_single_display_spec (struct it *, Lisp_Object,
959 Lisp_Object, Lisp_Object,
960 struct text_pos *, ptrdiff_t, int, int);
961 static int underlying_face_id (struct it *);
962 static int in_ellipses_for_invisible_text_p (struct display_pos *,
963 struct window *);
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
968 #ifdef HAVE_WINDOW_SYSTEM
970 static void x_consider_frame_title (Lisp_Object);
971 static void update_tool_bar (struct frame *, int);
972 static int redisplay_tool_bar (struct frame *);
973 static void notice_overwritten_cursor (struct window *,
974 enum glyph_row_area,
975 int, int, int, int);
976 static void append_stretch_glyph (struct it *, Lisp_Object,
977 int, int, int);
980 #endif /* HAVE_WINDOW_SYSTEM */
982 static void produce_special_glyphs (struct it *, enum display_element_type);
983 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
984 static int coords_in_mouse_face_p (struct window *, int, int);
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
996 This is the height of W minus the height of a mode line, if any. */
999 window_text_bottom_y (struct window *w)
1001 int height = WINDOW_TOTAL_HEIGHT (w);
1003 if (WINDOW_WANTS_MODELINE_P (w))
1004 height -= CURRENT_MODE_LINE_HEIGHT (w);
1005 return height;
1008 /* Return the pixel width of display area AREA of window W.
1009 ANY_AREA means return the total width of W, not including
1010 fringes to the left and right of the window. */
1013 window_box_width (struct window *w, enum glyph_row_area area)
1015 int cols = w->total_cols;
1016 int pixels = 0;
1018 if (!w->pseudo_window_p)
1020 cols -= WINDOW_SCROLL_BAR_COLS (w);
1022 if (area == TEXT_AREA)
1024 cols -= max (0, w->left_margin_cols);
1025 cols -= max (0, w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1028 else if (area == LEFT_MARGIN_AREA)
1030 cols = max (0, w->left_margin_cols);
1031 pixels = 0;
1033 else if (area == RIGHT_MARGIN_AREA)
1035 cols = max (0, w->right_margin_cols);
1036 pixels = 0;
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1048 window_box_height (struct window *w)
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1053 eassert (height >= 0);
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1061 if (WINDOW_WANTS_MODELINE_P (w))
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1097 int x;
1099 if (w->pseudo_window_p)
1100 return 0;
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1118 return x;
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1137 window_box_left (struct window *w, enum glyph_row_area area)
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1148 return x;
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1157 window_box_right (struct window *w, enum glyph_row_area area)
1159 return window_box_left (w, area) + window_box_width (w, area);
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1187 #ifdef HAVE_WINDOW_SYSTEM
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1206 #endif /* HAVE_WINDOW_SYSTEM */
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1216 line_bottom_y (struct it *it)
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1221 if (line_height == 0)
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1232 else
1234 struct glyph_row *row = it->glyph_row;
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1247 return line_top_y + line_height;
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1283 default_line_pixel_height (struct window *w)
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1303 if (addon >= 0)
1304 height += addon;
1307 else
1308 height += f->extra_line_spacing;
1311 return height;
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1319 if (CONSP (spec))
1321 while (CONSP (spec))
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1328 else if (VECTORP (spec))
1330 ptrdiff_t i;
1332 for (i = 0; i < ASIZE (spec); i++)
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1337 return Qnil;
1340 return spec;
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1357 return window_hscroll;
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1379 if (XBUFFER (w->contents) != current_buffer)
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = 1;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1455 it = save_it;
1457 if (visible_p)
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1505 else if (IT_CHARPOS (it) != charpos)
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1638 while (EQ ((g - 1)->object, string))
1640 --g;
1641 top_x -= g->pixel_width;
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1658 else
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1670 visible_p = 1;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1687 bidi_unshelve_cache (itdata, 0);
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1706 return visible_p;
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1718 int c;
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1727 return c;
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1738 eassert (STRINGP (string) && nchars >= 0);
1740 if (STRING_MULTIBYTE (string))
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1745 while (nchars--)
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1756 return pos;
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1781 struct text_pos pos;
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1786 if (multibyte_p)
1788 int len;
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1802 return pos;
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1812 ptrdiff_t nchars;
1814 if (multibyte_p)
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1820 for (nchars = 0; rest > 0; ++nchars)
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1826 else
1827 nchars = strlen (s);
1829 return nchars;
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1876 return height;
1878 #endif
1880 return 1;
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1913 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 r.width = WINDOW_TOTAL_WIDTH (s->w);
2055 /* Unless displaying a mode or menu bar line, which are always
2056 fully visible, clip to the visible part of the row. */
2057 if (s->w->pseudo_window_p)
2058 r.height = s->row->visible_height;
2059 else
2060 r.height = s->height;
2062 else
2064 /* This is a text line that may be partially visible. */
2065 r.x = window_box_left (s->w, s->area);
2066 r.width = window_box_width (s->w, s->area);
2067 r.height = s->row->visible_height;
2070 if (s->clip_head)
2071 if (r.x < s->clip_head->x)
2073 if (r.width >= s->clip_head->x - r.x)
2074 r.width -= s->clip_head->x - r.x;
2075 else
2076 r.width = 0;
2077 r.x = s->clip_head->x;
2079 if (s->clip_tail)
2080 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2082 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2083 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2084 else
2085 r.width = 0;
2088 /* If S draws overlapping rows, it's sufficient to use the top and
2089 bottom of the window for clipping because this glyph string
2090 intentionally draws over other lines. */
2091 if (s->for_overlaps)
2093 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2094 r.height = window_text_bottom_y (s->w) - r.y;
2096 /* Alas, the above simple strategy does not work for the
2097 environments with anti-aliased text: if the same text is
2098 drawn onto the same place multiple times, it gets thicker.
2099 If the overlap we are processing is for the erased cursor, we
2100 take the intersection with the rectangle of the cursor. */
2101 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2103 XRectangle rc, r_save = r;
2105 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2106 rc.y = s->w->phys_cursor.y;
2107 rc.width = s->w->phys_cursor_width;
2108 rc.height = s->w->phys_cursor_height;
2110 x_intersect_rectangles (&r_save, &rc, &r);
2113 else
2115 /* Don't use S->y for clipping because it doesn't take partially
2116 visible lines into account. For example, it can be negative for
2117 partially visible lines at the top of a window. */
2118 if (!s->row->full_width_p
2119 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2120 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2121 else
2122 r.y = max (0, s->row->y);
2125 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2127 /* If drawing the cursor, don't let glyph draw outside its
2128 advertised boundaries. Cleartype does this under some circumstances. */
2129 if (s->hl == DRAW_CURSOR)
2131 struct glyph *glyph = s->first_glyph;
2132 int height, max_y;
2134 if (s->x > r.x)
2136 r.width -= s->x - r.x;
2137 r.x = s->x;
2139 r.width = min (r.width, glyph->pixel_width);
2141 /* If r.y is below window bottom, ensure that we still see a cursor. */
2142 height = min (glyph->ascent + glyph->descent,
2143 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2144 max_y = window_text_bottom_y (s->w) - height;
2145 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2146 if (s->ybase - glyph->ascent > max_y)
2148 r.y = max_y;
2149 r.height = height;
2151 else
2153 /* Don't draw cursor glyph taller than our actual glyph. */
2154 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2155 if (height < r.height)
2157 max_y = r.y + r.height;
2158 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2159 r.height = min (max_y - r.y, height);
2164 if (s->row->clip)
2166 XRectangle r_save = r;
2168 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2169 r.width = 0;
2172 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2173 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2175 #ifdef CONVERT_FROM_XRECT
2176 CONVERT_FROM_XRECT (r, *rects);
2177 #else
2178 *rects = r;
2179 #endif
2180 return 1;
2182 else
2184 /* If we are processing overlapping and allowed to return
2185 multiple clipping rectangles, we exclude the row of the glyph
2186 string from the clipping rectangle. This is to avoid drawing
2187 the same text on the environment with anti-aliasing. */
2188 #ifdef CONVERT_FROM_XRECT
2189 XRectangle rs[2];
2190 #else
2191 XRectangle *rs = rects;
2192 #endif
2193 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2195 if (s->for_overlaps & OVERLAPS_PRED)
2197 rs[i] = r;
2198 if (r.y + r.height > row_y)
2200 if (r.y < row_y)
2201 rs[i].height = row_y - r.y;
2202 else
2203 rs[i].height = 0;
2205 i++;
2207 if (s->for_overlaps & OVERLAPS_SUCC)
2209 rs[i] = r;
2210 if (r.y < row_y + s->row->visible_height)
2212 if (r.y + r.height > row_y + s->row->visible_height)
2214 rs[i].y = row_y + s->row->visible_height;
2215 rs[i].height = r.y + r.height - rs[i].y;
2217 else
2218 rs[i].height = 0;
2220 i++;
2223 n = i;
2224 #ifdef CONVERT_FROM_XRECT
2225 for (i = 0; i < n; i++)
2226 CONVERT_FROM_XRECT (rs[i], rects[i]);
2227 #endif
2228 return n;
2232 /* EXPORT:
2233 Return in *NR the clipping rectangle for glyph string S. */
2235 void
2236 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2238 get_glyph_string_clip_rects (s, nr, 1);
2242 /* EXPORT:
2243 Return the position and height of the phys cursor in window W.
2244 Set w->phys_cursor_width to width of phys cursor.
2247 void
2248 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2249 struct glyph *glyph, int *xp, int *yp, int *heightp)
2251 struct frame *f = XFRAME (WINDOW_FRAME (w));
2252 int x, y, wd, h, h0, y0;
2254 /* Compute the width of the rectangle to draw. If on a stretch
2255 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2256 rectangle as wide as the glyph, but use a canonical character
2257 width instead. */
2258 wd = glyph->pixel_width - 1;
2259 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2260 wd++; /* Why? */
2261 #endif
2263 x = w->phys_cursor.x;
2264 if (x < 0)
2266 wd += x;
2267 x = 0;
2270 if (glyph->type == STRETCH_GLYPH
2271 && !x_stretch_cursor_p)
2272 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2273 w->phys_cursor_width = wd;
2275 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2277 /* If y is below window bottom, ensure that we still see a cursor. */
2278 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2280 h = max (h0, glyph->ascent + glyph->descent);
2281 h0 = min (h0, glyph->ascent + glyph->descent);
2283 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2284 if (y < y0)
2286 h = max (h - (y0 - y) + 1, h0);
2287 y = y0 - 1;
2289 else
2291 y0 = window_text_bottom_y (w) - h0;
2292 if (y > y0)
2294 h += y - y0;
2295 y = y0;
2299 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2300 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2301 *heightp = h;
2305 * Remember which glyph the mouse is over.
2308 void
2309 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2311 Lisp_Object window;
2312 struct window *w;
2313 struct glyph_row *r, *gr, *end_row;
2314 enum window_part part;
2315 enum glyph_row_area area;
2316 int x, y, width, height;
2318 /* Try to determine frame pixel position and size of the glyph under
2319 frame pixel coordinates X/Y on frame F. */
2321 if (!f->glyphs_initialized_p
2322 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2323 NILP (window)))
2325 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2326 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2327 goto virtual_glyph;
2330 w = XWINDOW (window);
2331 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2332 height = WINDOW_FRAME_LINE_HEIGHT (w);
2334 x = window_relative_x_coord (w, part, gx);
2335 y = gy - WINDOW_TOP_EDGE_Y (w);
2337 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2338 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2340 if (w->pseudo_window_p)
2342 area = TEXT_AREA;
2343 part = ON_MODE_LINE; /* Don't adjust margin. */
2344 goto text_glyph;
2347 switch (part)
2349 case ON_LEFT_MARGIN:
2350 area = LEFT_MARGIN_AREA;
2351 goto text_glyph;
2353 case ON_RIGHT_MARGIN:
2354 area = RIGHT_MARGIN_AREA;
2355 goto text_glyph;
2357 case ON_HEADER_LINE:
2358 case ON_MODE_LINE:
2359 gr = (part == ON_HEADER_LINE
2360 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2361 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2362 gy = gr->y;
2363 area = TEXT_AREA;
2364 goto text_glyph_row_found;
2366 case ON_TEXT:
2367 area = TEXT_AREA;
2369 text_glyph:
2370 gr = 0; gy = 0;
2371 for (; r <= end_row && r->enabled_p; ++r)
2372 if (r->y + r->height > y)
2374 gr = r; gy = r->y;
2375 break;
2378 text_glyph_row_found:
2379 if (gr && gy <= y)
2381 struct glyph *g = gr->glyphs[area];
2382 struct glyph *end = g + gr->used[area];
2384 height = gr->height;
2385 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2386 if (gx + g->pixel_width > x)
2387 break;
2389 if (g < end)
2391 if (g->type == IMAGE_GLYPH)
2393 /* Don't remember when mouse is over image, as
2394 image may have hot-spots. */
2395 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2396 return;
2398 width = g->pixel_width;
2400 else
2402 /* Use nominal char spacing at end of line. */
2403 x -= gx;
2404 gx += (x / width) * width;
2407 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2408 gx += window_box_left_offset (w, area);
2410 else
2412 /* Use nominal line height at end of window. */
2413 gx = (x / width) * width;
2414 y -= gy;
2415 gy += (y / height) * height;
2417 break;
2419 case ON_LEFT_FRINGE:
2420 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2421 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2422 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2423 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2424 goto row_glyph;
2426 case ON_RIGHT_FRINGE:
2427 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2428 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2429 : window_box_right_offset (w, TEXT_AREA));
2430 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2431 goto row_glyph;
2433 case ON_SCROLL_BAR:
2434 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2436 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2437 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2438 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2439 : 0)));
2440 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2442 row_glyph:
2443 gr = 0, gy = 0;
2444 for (; r <= end_row && r->enabled_p; ++r)
2445 if (r->y + r->height > y)
2447 gr = r; gy = r->y;
2448 break;
2451 if (gr && gy <= y)
2452 height = gr->height;
2453 else
2455 /* Use nominal line height at end of window. */
2456 y -= gy;
2457 gy += (y / height) * height;
2459 break;
2461 default:
2463 virtual_glyph:
2464 /* If there is no glyph under the mouse, then we divide the screen
2465 into a grid of the smallest glyph in the frame, and use that
2466 as our "glyph". */
2468 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2469 round down even for negative values. */
2470 if (gx < 0)
2471 gx -= width - 1;
2472 if (gy < 0)
2473 gy -= height - 1;
2475 gx = (gx / width) * width;
2476 gy = (gy / height) * height;
2478 goto store_rect;
2481 gx += WINDOW_LEFT_EDGE_X (w);
2482 gy += WINDOW_TOP_EDGE_Y (w);
2484 store_rect:
2485 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2487 /* Visible feedback for debugging. */
2488 #if 0
2489 #if HAVE_X_WINDOWS
2490 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2491 f->output_data.x->normal_gc,
2492 gx, gy, width, height);
2493 #endif
2494 #endif
2498 #endif /* HAVE_WINDOW_SYSTEM */
2500 static void
2501 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2503 eassert (w);
2504 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2505 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2506 w->window_end_vpos
2507 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2510 /***********************************************************************
2511 Lisp form evaluation
2512 ***********************************************************************/
2514 /* Error handler for safe_eval and safe_call. */
2516 static Lisp_Object
2517 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2519 add_to_log ("Error during redisplay: %S signaled %S",
2520 Flist (nargs, args), arg);
2521 return Qnil;
2524 /* Call function FUNC with the rest of NARGS - 1 arguments
2525 following. Return the result, or nil if something went
2526 wrong. Prevent redisplay during the evaluation. */
2528 Lisp_Object
2529 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2531 Lisp_Object val;
2533 if (inhibit_eval_during_redisplay)
2534 val = Qnil;
2535 else
2537 va_list ap;
2538 ptrdiff_t i;
2539 ptrdiff_t count = SPECPDL_INDEX ();
2540 struct gcpro gcpro1;
2541 Lisp_Object *args = alloca (nargs * word_size);
2543 args[0] = func;
2544 va_start (ap, func);
2545 for (i = 1; i < nargs; i++)
2546 args[i] = va_arg (ap, Lisp_Object);
2547 va_end (ap);
2549 GCPRO1 (args[0]);
2550 gcpro1.nvars = nargs;
2551 specbind (Qinhibit_redisplay, Qt);
2552 /* Use Qt to ensure debugger does not run,
2553 so there is no possibility of wanting to redisplay. */
2554 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2555 safe_eval_handler);
2556 UNGCPRO;
2557 val = unbind_to (count, val);
2560 return val;
2564 /* Call function FN with one argument ARG.
2565 Return the result, or nil if something went wrong. */
2567 Lisp_Object
2568 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2570 return safe_call (2, fn, arg);
2573 static Lisp_Object Qeval;
2575 Lisp_Object
2576 safe_eval (Lisp_Object sexpr)
2578 return safe_call1 (Qeval, sexpr);
2581 /* Call function FN with two arguments ARG1 and ARG2.
2582 Return the result, or nil if something went wrong. */
2584 Lisp_Object
2585 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2587 return safe_call (3, fn, arg1, arg2);
2592 /***********************************************************************
2593 Debugging
2594 ***********************************************************************/
2596 #if 0
2598 /* Define CHECK_IT to perform sanity checks on iterators.
2599 This is for debugging. It is too slow to do unconditionally. */
2601 static void
2602 check_it (struct it *it)
2604 if (it->method == GET_FROM_STRING)
2606 eassert (STRINGP (it->string));
2607 eassert (IT_STRING_CHARPOS (*it) >= 0);
2609 else
2611 eassert (IT_STRING_CHARPOS (*it) < 0);
2612 if (it->method == GET_FROM_BUFFER)
2614 /* Check that character and byte positions agree. */
2615 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2619 if (it->dpvec)
2620 eassert (it->current.dpvec_index >= 0);
2621 else
2622 eassert (it->current.dpvec_index < 0);
2625 #define CHECK_IT(IT) check_it ((IT))
2627 #else /* not 0 */
2629 #define CHECK_IT(IT) (void) 0
2631 #endif /* not 0 */
2634 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2636 /* Check that the window end of window W is what we expect it
2637 to be---the last row in the current matrix displaying text. */
2639 static void
2640 check_window_end (struct window *w)
2642 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2644 struct glyph_row *row;
2645 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2646 !row->enabled_p
2647 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2648 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2652 #define CHECK_WINDOW_END(W) check_window_end ((W))
2654 #else
2656 #define CHECK_WINDOW_END(W) (void) 0
2658 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2660 /***********************************************************************
2661 Iterator initialization
2662 ***********************************************************************/
2664 /* Initialize IT for displaying current_buffer in window W, starting
2665 at character position CHARPOS. CHARPOS < 0 means that no buffer
2666 position is specified which is useful when the iterator is assigned
2667 a position later. BYTEPOS is the byte position corresponding to
2668 CHARPOS.
2670 If ROW is not null, calls to produce_glyphs with IT as parameter
2671 will produce glyphs in that row.
2673 BASE_FACE_ID is the id of a base face to use. It must be one of
2674 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2675 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2676 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2678 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2679 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2680 will be initialized to use the corresponding mode line glyph row of
2681 the desired matrix of W. */
2683 void
2684 init_iterator (struct it *it, struct window *w,
2685 ptrdiff_t charpos, ptrdiff_t bytepos,
2686 struct glyph_row *row, enum face_id base_face_id)
2688 enum face_id remapped_base_face_id = base_face_id;
2690 /* Some precondition checks. */
2691 eassert (w != NULL && it != NULL);
2692 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2693 && charpos <= ZV));
2695 /* If face attributes have been changed since the last redisplay,
2696 free realized faces now because they depend on face definitions
2697 that might have changed. Don't free faces while there might be
2698 desired matrices pending which reference these faces. */
2699 if (face_change_count && !inhibit_free_realized_faces)
2701 face_change_count = 0;
2702 free_all_realized_faces (Qnil);
2705 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2706 if (! NILP (Vface_remapping_alist))
2707 remapped_base_face_id
2708 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2710 /* Use one of the mode line rows of W's desired matrix if
2711 appropriate. */
2712 if (row == NULL)
2714 if (base_face_id == MODE_LINE_FACE_ID
2715 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2716 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2717 else if (base_face_id == HEADER_LINE_FACE_ID)
2718 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2721 /* Clear IT. */
2722 memset (it, 0, sizeof *it);
2723 it->current.overlay_string_index = -1;
2724 it->current.dpvec_index = -1;
2725 it->base_face_id = remapped_base_face_id;
2726 it->string = Qnil;
2727 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2728 it->paragraph_embedding = L2R;
2729 it->bidi_it.string.lstring = Qnil;
2730 it->bidi_it.string.s = NULL;
2731 it->bidi_it.string.bufpos = 0;
2732 it->bidi_it.w = w;
2734 /* The window in which we iterate over current_buffer: */
2735 XSETWINDOW (it->window, w);
2736 it->w = w;
2737 it->f = XFRAME (w->frame);
2739 it->cmp_it.id = -1;
2741 /* Extra space between lines (on window systems only). */
2742 if (base_face_id == DEFAULT_FACE_ID
2743 && FRAME_WINDOW_P (it->f))
2745 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2746 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2747 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2748 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2749 * FRAME_LINE_HEIGHT (it->f));
2750 else if (it->f->extra_line_spacing > 0)
2751 it->extra_line_spacing = it->f->extra_line_spacing;
2752 it->max_extra_line_spacing = 0;
2755 /* If realized faces have been removed, e.g. because of face
2756 attribute changes of named faces, recompute them. When running
2757 in batch mode, the face cache of the initial frame is null. If
2758 we happen to get called, make a dummy face cache. */
2759 if (FRAME_FACE_CACHE (it->f) == NULL)
2760 init_frame_faces (it->f);
2761 if (FRAME_FACE_CACHE (it->f)->used == 0)
2762 recompute_basic_faces (it->f);
2764 /* Current value of the `slice', `space-width', and 'height' properties. */
2765 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2766 it->space_width = Qnil;
2767 it->font_height = Qnil;
2768 it->override_ascent = -1;
2770 /* Are control characters displayed as `^C'? */
2771 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2773 /* -1 means everything between a CR and the following line end
2774 is invisible. >0 means lines indented more than this value are
2775 invisible. */
2776 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2777 ? (clip_to_bounds
2778 (-1, XINT (BVAR (current_buffer, selective_display)),
2779 PTRDIFF_MAX))
2780 : (!NILP (BVAR (current_buffer, selective_display))
2781 ? -1 : 0));
2782 it->selective_display_ellipsis_p
2783 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2785 /* Display table to use. */
2786 it->dp = window_display_table (w);
2788 /* Are multibyte characters enabled in current_buffer? */
2789 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2791 /* Get the position at which the redisplay_end_trigger hook should
2792 be run, if it is to be run at all. */
2793 if (MARKERP (w->redisplay_end_trigger)
2794 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2795 it->redisplay_end_trigger_charpos
2796 = marker_position (w->redisplay_end_trigger);
2797 else if (INTEGERP (w->redisplay_end_trigger))
2798 it->redisplay_end_trigger_charpos =
2799 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2801 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2803 /* Are lines in the display truncated? */
2804 if (base_face_id != DEFAULT_FACE_ID
2805 || it->w->hscroll
2806 || (! WINDOW_FULL_WIDTH_P (it->w)
2807 && ((!NILP (Vtruncate_partial_width_windows)
2808 && !INTEGERP (Vtruncate_partial_width_windows))
2809 || (INTEGERP (Vtruncate_partial_width_windows)
2810 && (WINDOW_TOTAL_COLS (it->w)
2811 < XINT (Vtruncate_partial_width_windows))))))
2812 it->line_wrap = TRUNCATE;
2813 else if (NILP (BVAR (current_buffer, truncate_lines)))
2814 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2815 ? WINDOW_WRAP : WORD_WRAP;
2816 else
2817 it->line_wrap = TRUNCATE;
2819 /* Get dimensions of truncation and continuation glyphs. These are
2820 displayed as fringe bitmaps under X, but we need them for such
2821 frames when the fringes are turned off. But leave the dimensions
2822 zero for tooltip frames, as these glyphs look ugly there and also
2823 sabotage calculations of tooltip dimensions in x-show-tip. */
2824 #ifdef HAVE_WINDOW_SYSTEM
2825 if (!(FRAME_WINDOW_P (it->f)
2826 && FRAMEP (tip_frame)
2827 && it->f == XFRAME (tip_frame)))
2828 #endif
2830 if (it->line_wrap == TRUNCATE)
2832 /* We will need the truncation glyph. */
2833 eassert (it->glyph_row == NULL);
2834 produce_special_glyphs (it, IT_TRUNCATION);
2835 it->truncation_pixel_width = it->pixel_width;
2837 else
2839 /* We will need the continuation glyph. */
2840 eassert (it->glyph_row == NULL);
2841 produce_special_glyphs (it, IT_CONTINUATION);
2842 it->continuation_pixel_width = it->pixel_width;
2846 /* Reset these values to zero because the produce_special_glyphs
2847 above has changed them. */
2848 it->pixel_width = it->ascent = it->descent = 0;
2849 it->phys_ascent = it->phys_descent = 0;
2851 /* Set this after getting the dimensions of truncation and
2852 continuation glyphs, so that we don't produce glyphs when calling
2853 produce_special_glyphs, above. */
2854 it->glyph_row = row;
2855 it->area = TEXT_AREA;
2857 /* Forget any previous info about this row being reversed. */
2858 if (it->glyph_row)
2859 it->glyph_row->reversed_p = 0;
2861 /* Get the dimensions of the display area. The display area
2862 consists of the visible window area plus a horizontally scrolled
2863 part to the left of the window. All x-values are relative to the
2864 start of this total display area. */
2865 if (base_face_id != DEFAULT_FACE_ID)
2867 /* Mode lines, menu bar in terminal frames. */
2868 it->first_visible_x = 0;
2869 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2871 else
2873 it->first_visible_x =
2874 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2875 it->last_visible_x = (it->first_visible_x
2876 + window_box_width (w, TEXT_AREA));
2878 /* If we truncate lines, leave room for the truncation glyph(s) at
2879 the right margin. Otherwise, leave room for the continuation
2880 glyph(s). Done only if the window has no fringes. Since we
2881 don't know at this point whether there will be any R2L lines in
2882 the window, we reserve space for truncation/continuation glyphs
2883 even if only one of the fringes is absent. */
2884 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2885 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2887 if (it->line_wrap == TRUNCATE)
2888 it->last_visible_x -= it->truncation_pixel_width;
2889 else
2890 it->last_visible_x -= it->continuation_pixel_width;
2893 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2894 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2897 /* Leave room for a border glyph. */
2898 if (!FRAME_WINDOW_P (it->f)
2899 && !WINDOW_RIGHTMOST_P (it->w))
2900 it->last_visible_x -= 1;
2902 it->last_visible_y = window_text_bottom_y (w);
2904 /* For mode lines and alike, arrange for the first glyph having a
2905 left box line if the face specifies a box. */
2906 if (base_face_id != DEFAULT_FACE_ID)
2908 struct face *face;
2910 it->face_id = remapped_base_face_id;
2912 /* If we have a boxed mode line, make the first character appear
2913 with a left box line. */
2914 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2915 if (face->box != FACE_NO_BOX)
2916 it->start_of_box_run_p = 1;
2919 /* If a buffer position was specified, set the iterator there,
2920 getting overlays and face properties from that position. */
2921 if (charpos >= BUF_BEG (current_buffer))
2923 it->end_charpos = ZV;
2924 eassert (charpos == BYTE_TO_CHAR (bytepos));
2925 IT_CHARPOS (*it) = charpos;
2926 IT_BYTEPOS (*it) = bytepos;
2928 /* We will rely on `reseat' to set this up properly, via
2929 handle_face_prop. */
2930 it->face_id = it->base_face_id;
2932 it->start = it->current;
2933 /* Do we need to reorder bidirectional text? Not if this is a
2934 unibyte buffer: by definition, none of the single-byte
2935 characters are strong R2L, so no reordering is needed. And
2936 bidi.c doesn't support unibyte buffers anyway. Also, don't
2937 reorder while we are loading loadup.el, since the tables of
2938 character properties needed for reordering are not yet
2939 available. */
2940 it->bidi_p =
2941 NILP (Vpurify_flag)
2942 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2943 && it->multibyte_p;
2945 /* If we are to reorder bidirectional text, init the bidi
2946 iterator. */
2947 if (it->bidi_p)
2949 /* Note the paragraph direction that this buffer wants to
2950 use. */
2951 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2952 Qleft_to_right))
2953 it->paragraph_embedding = L2R;
2954 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2955 Qright_to_left))
2956 it->paragraph_embedding = R2L;
2957 else
2958 it->paragraph_embedding = NEUTRAL_DIR;
2959 bidi_unshelve_cache (NULL, 0);
2960 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2961 &it->bidi_it);
2964 /* Compute faces etc. */
2965 reseat (it, it->current.pos, 1);
2968 CHECK_IT (it);
2972 /* Initialize IT for the display of window W with window start POS. */
2974 void
2975 start_display (struct it *it, struct window *w, struct text_pos pos)
2977 struct glyph_row *row;
2978 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2980 row = w->desired_matrix->rows + first_vpos;
2981 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2982 it->first_vpos = first_vpos;
2984 /* Don't reseat to previous visible line start if current start
2985 position is in a string or image. */
2986 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2988 int start_at_line_beg_p;
2989 int first_y = it->current_y;
2991 /* If window start is not at a line start, skip forward to POS to
2992 get the correct continuation lines width. */
2993 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2994 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2995 if (!start_at_line_beg_p)
2997 int new_x;
2999 reseat_at_previous_visible_line_start (it);
3000 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3002 new_x = it->current_x + it->pixel_width;
3004 /* If lines are continued, this line may end in the middle
3005 of a multi-glyph character (e.g. a control character
3006 displayed as \003, or in the middle of an overlay
3007 string). In this case move_it_to above will not have
3008 taken us to the start of the continuation line but to the
3009 end of the continued line. */
3010 if (it->current_x > 0
3011 && it->line_wrap != TRUNCATE /* Lines are continued. */
3012 && (/* And glyph doesn't fit on the line. */
3013 new_x > it->last_visible_x
3014 /* Or it fits exactly and we're on a window
3015 system frame. */
3016 || (new_x == it->last_visible_x
3017 && FRAME_WINDOW_P (it->f)
3018 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3019 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3020 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3022 if ((it->current.dpvec_index >= 0
3023 || it->current.overlay_string_index >= 0)
3024 /* If we are on a newline from a display vector or
3025 overlay string, then we are already at the end of
3026 a screen line; no need to go to the next line in
3027 that case, as this line is not really continued.
3028 (If we do go to the next line, C-e will not DTRT.) */
3029 && it->c != '\n')
3031 set_iterator_to_next (it, 1);
3032 move_it_in_display_line_to (it, -1, -1, 0);
3035 it->continuation_lines_width += it->current_x;
3037 /* If the character at POS is displayed via a display
3038 vector, move_it_to above stops at the final glyph of
3039 IT->dpvec. To make the caller redisplay that character
3040 again (a.k.a. start at POS), we need to reset the
3041 dpvec_index to the beginning of IT->dpvec. */
3042 else if (it->current.dpvec_index >= 0)
3043 it->current.dpvec_index = 0;
3045 /* We're starting a new display line, not affected by the
3046 height of the continued line, so clear the appropriate
3047 fields in the iterator structure. */
3048 it->max_ascent = it->max_descent = 0;
3049 it->max_phys_ascent = it->max_phys_descent = 0;
3051 it->current_y = first_y;
3052 it->vpos = 0;
3053 it->current_x = it->hpos = 0;
3059 /* Return 1 if POS is a position in ellipses displayed for invisible
3060 text. W is the window we display, for text property lookup. */
3062 static int
3063 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3065 Lisp_Object prop, window;
3066 int ellipses_p = 0;
3067 ptrdiff_t charpos = CHARPOS (pos->pos);
3069 /* If POS specifies a position in a display vector, this might
3070 be for an ellipsis displayed for invisible text. We won't
3071 get the iterator set up for delivering that ellipsis unless
3072 we make sure that it gets aware of the invisible text. */
3073 if (pos->dpvec_index >= 0
3074 && pos->overlay_string_index < 0
3075 && CHARPOS (pos->string_pos) < 0
3076 && charpos > BEGV
3077 && (XSETWINDOW (window, w),
3078 prop = Fget_char_property (make_number (charpos),
3079 Qinvisible, window),
3080 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3082 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3083 window);
3084 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3087 return ellipses_p;
3091 /* Initialize IT for stepping through current_buffer in window W,
3092 starting at position POS that includes overlay string and display
3093 vector/ control character translation position information. Value
3094 is zero if there are overlay strings with newlines at POS. */
3096 static int
3097 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3099 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3100 int i, overlay_strings_with_newlines = 0;
3102 /* If POS specifies a position in a display vector, this might
3103 be for an ellipsis displayed for invisible text. We won't
3104 get the iterator set up for delivering that ellipsis unless
3105 we make sure that it gets aware of the invisible text. */
3106 if (in_ellipses_for_invisible_text_p (pos, w))
3108 --charpos;
3109 bytepos = 0;
3112 /* Keep in mind: the call to reseat in init_iterator skips invisible
3113 text, so we might end up at a position different from POS. This
3114 is only a problem when POS is a row start after a newline and an
3115 overlay starts there with an after-string, and the overlay has an
3116 invisible property. Since we don't skip invisible text in
3117 display_line and elsewhere immediately after consuming the
3118 newline before the row start, such a POS will not be in a string,
3119 but the call to init_iterator below will move us to the
3120 after-string. */
3121 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3123 /* This only scans the current chunk -- it should scan all chunks.
3124 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3125 to 16 in 22.1 to make this a lesser problem. */
3126 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3128 const char *s = SSDATA (it->overlay_strings[i]);
3129 const char *e = s + SBYTES (it->overlay_strings[i]);
3131 while (s < e && *s != '\n')
3132 ++s;
3134 if (s < e)
3136 overlay_strings_with_newlines = 1;
3137 break;
3141 /* If position is within an overlay string, set up IT to the right
3142 overlay string. */
3143 if (pos->overlay_string_index >= 0)
3145 int relative_index;
3147 /* If the first overlay string happens to have a `display'
3148 property for an image, the iterator will be set up for that
3149 image, and we have to undo that setup first before we can
3150 correct the overlay string index. */
3151 if (it->method == GET_FROM_IMAGE)
3152 pop_it (it);
3154 /* We already have the first chunk of overlay strings in
3155 IT->overlay_strings. Load more until the one for
3156 pos->overlay_string_index is in IT->overlay_strings. */
3157 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3159 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3160 it->current.overlay_string_index = 0;
3161 while (n--)
3163 load_overlay_strings (it, 0);
3164 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3168 it->current.overlay_string_index = pos->overlay_string_index;
3169 relative_index = (it->current.overlay_string_index
3170 % OVERLAY_STRING_CHUNK_SIZE);
3171 it->string = it->overlay_strings[relative_index];
3172 eassert (STRINGP (it->string));
3173 it->current.string_pos = pos->string_pos;
3174 it->method = GET_FROM_STRING;
3175 it->end_charpos = SCHARS (it->string);
3176 /* Set up the bidi iterator for this overlay string. */
3177 if (it->bidi_p)
3179 it->bidi_it.string.lstring = it->string;
3180 it->bidi_it.string.s = NULL;
3181 it->bidi_it.string.schars = SCHARS (it->string);
3182 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3183 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3184 it->bidi_it.string.unibyte = !it->multibyte_p;
3185 it->bidi_it.w = it->w;
3186 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3187 FRAME_WINDOW_P (it->f), &it->bidi_it);
3189 /* Synchronize the state of the bidi iterator with
3190 pos->string_pos. For any string position other than
3191 zero, this will be done automagically when we resume
3192 iteration over the string and get_visually_first_element
3193 is called. But if string_pos is zero, and the string is
3194 to be reordered for display, we need to resync manually,
3195 since it could be that the iteration state recorded in
3196 pos ended at string_pos of 0 moving backwards in string. */
3197 if (CHARPOS (pos->string_pos) == 0)
3199 get_visually_first_element (it);
3200 if (IT_STRING_CHARPOS (*it) != 0)
3201 do {
3202 /* Paranoia. */
3203 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3204 bidi_move_to_visually_next (&it->bidi_it);
3205 } while (it->bidi_it.charpos != 0);
3207 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3208 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3212 if (CHARPOS (pos->string_pos) >= 0)
3214 /* Recorded position is not in an overlay string, but in another
3215 string. This can only be a string from a `display' property.
3216 IT should already be filled with that string. */
3217 it->current.string_pos = pos->string_pos;
3218 eassert (STRINGP (it->string));
3219 if (it->bidi_p)
3220 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3221 FRAME_WINDOW_P (it->f), &it->bidi_it);
3224 /* Restore position in display vector translations, control
3225 character translations or ellipses. */
3226 if (pos->dpvec_index >= 0)
3228 if (it->dpvec == NULL)
3229 get_next_display_element (it);
3230 eassert (it->dpvec && it->current.dpvec_index == 0);
3231 it->current.dpvec_index = pos->dpvec_index;
3234 CHECK_IT (it);
3235 return !overlay_strings_with_newlines;
3239 /* Initialize IT for stepping through current_buffer in window W
3240 starting at ROW->start. */
3242 static void
3243 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3245 init_from_display_pos (it, w, &row->start);
3246 it->start = row->start;
3247 it->continuation_lines_width = row->continuation_lines_width;
3248 CHECK_IT (it);
3252 /* Initialize IT for stepping through current_buffer in window W
3253 starting in the line following ROW, i.e. starting at ROW->end.
3254 Value is zero if there are overlay strings with newlines at ROW's
3255 end position. */
3257 static int
3258 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3260 int success = 0;
3262 if (init_from_display_pos (it, w, &row->end))
3264 if (row->continued_p)
3265 it->continuation_lines_width
3266 = row->continuation_lines_width + row->pixel_width;
3267 CHECK_IT (it);
3268 success = 1;
3271 return success;
3277 /***********************************************************************
3278 Text properties
3279 ***********************************************************************/
3281 /* Called when IT reaches IT->stop_charpos. Handle text property and
3282 overlay changes. Set IT->stop_charpos to the next position where
3283 to stop. */
3285 static void
3286 handle_stop (struct it *it)
3288 enum prop_handled handled;
3289 int handle_overlay_change_p;
3290 struct props *p;
3292 it->dpvec = NULL;
3293 it->current.dpvec_index = -1;
3294 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3295 it->ignore_overlay_strings_at_pos_p = 0;
3296 it->ellipsis_p = 0;
3298 /* Use face of preceding text for ellipsis (if invisible) */
3299 if (it->selective_display_ellipsis_p)
3300 it->saved_face_id = it->face_id;
3304 handled = HANDLED_NORMALLY;
3306 /* Call text property handlers. */
3307 for (p = it_props; p->handler; ++p)
3309 handled = p->handler (it);
3311 if (handled == HANDLED_RECOMPUTE_PROPS)
3312 break;
3313 else if (handled == HANDLED_RETURN)
3315 /* We still want to show before and after strings from
3316 overlays even if the actual buffer text is replaced. */
3317 if (!handle_overlay_change_p
3318 || it->sp > 1
3319 /* Don't call get_overlay_strings_1 if we already
3320 have overlay strings loaded, because doing so
3321 will load them again and push the iterator state
3322 onto the stack one more time, which is not
3323 expected by the rest of the code that processes
3324 overlay strings. */
3325 || (it->current.overlay_string_index < 0
3326 ? !get_overlay_strings_1 (it, 0, 0)
3327 : 0))
3329 if (it->ellipsis_p)
3330 setup_for_ellipsis (it, 0);
3331 /* When handling a display spec, we might load an
3332 empty string. In that case, discard it here. We
3333 used to discard it in handle_single_display_spec,
3334 but that causes get_overlay_strings_1, above, to
3335 ignore overlay strings that we must check. */
3336 if (STRINGP (it->string) && !SCHARS (it->string))
3337 pop_it (it);
3338 return;
3340 else if (STRINGP (it->string) && !SCHARS (it->string))
3341 pop_it (it);
3342 else
3344 it->ignore_overlay_strings_at_pos_p = 1;
3345 it->string_from_display_prop_p = 0;
3346 it->from_disp_prop_p = 0;
3347 handle_overlay_change_p = 0;
3349 handled = HANDLED_RECOMPUTE_PROPS;
3350 break;
3352 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3353 handle_overlay_change_p = 0;
3356 if (handled != HANDLED_RECOMPUTE_PROPS)
3358 /* Don't check for overlay strings below when set to deliver
3359 characters from a display vector. */
3360 if (it->method == GET_FROM_DISPLAY_VECTOR)
3361 handle_overlay_change_p = 0;
3363 /* Handle overlay changes.
3364 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3365 if it finds overlays. */
3366 if (handle_overlay_change_p)
3367 handled = handle_overlay_change (it);
3370 if (it->ellipsis_p)
3372 setup_for_ellipsis (it, 0);
3373 break;
3376 while (handled == HANDLED_RECOMPUTE_PROPS);
3378 /* Determine where to stop next. */
3379 if (handled == HANDLED_NORMALLY)
3380 compute_stop_pos (it);
3384 /* Compute IT->stop_charpos from text property and overlay change
3385 information for IT's current position. */
3387 static void
3388 compute_stop_pos (struct it *it)
3390 register INTERVAL iv, next_iv;
3391 Lisp_Object object, limit, position;
3392 ptrdiff_t charpos, bytepos;
3394 if (STRINGP (it->string))
3396 /* Strings are usually short, so don't limit the search for
3397 properties. */
3398 it->stop_charpos = it->end_charpos;
3399 object = it->string;
3400 limit = Qnil;
3401 charpos = IT_STRING_CHARPOS (*it);
3402 bytepos = IT_STRING_BYTEPOS (*it);
3404 else
3406 ptrdiff_t pos;
3408 /* If end_charpos is out of range for some reason, such as a
3409 misbehaving display function, rationalize it (Bug#5984). */
3410 if (it->end_charpos > ZV)
3411 it->end_charpos = ZV;
3412 it->stop_charpos = it->end_charpos;
3414 /* If next overlay change is in front of the current stop pos
3415 (which is IT->end_charpos), stop there. Note: value of
3416 next_overlay_change is point-max if no overlay change
3417 follows. */
3418 charpos = IT_CHARPOS (*it);
3419 bytepos = IT_BYTEPOS (*it);
3420 pos = next_overlay_change (charpos);
3421 if (pos < it->stop_charpos)
3422 it->stop_charpos = pos;
3424 /* Set up variables for computing the stop position from text
3425 property changes. */
3426 XSETBUFFER (object, current_buffer);
3427 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3430 /* Get the interval containing IT's position. Value is a null
3431 interval if there isn't such an interval. */
3432 position = make_number (charpos);
3433 iv = validate_interval_range (object, &position, &position, 0);
3434 if (iv)
3436 Lisp_Object values_here[LAST_PROP_IDX];
3437 struct props *p;
3439 /* Get properties here. */
3440 for (p = it_props; p->handler; ++p)
3441 values_here[p->idx] = textget (iv->plist, *p->name);
3443 /* Look for an interval following iv that has different
3444 properties. */
3445 for (next_iv = next_interval (iv);
3446 (next_iv
3447 && (NILP (limit)
3448 || XFASTINT (limit) > next_iv->position));
3449 next_iv = next_interval (next_iv))
3451 for (p = it_props; p->handler; ++p)
3453 Lisp_Object new_value;
3455 new_value = textget (next_iv->plist, *p->name);
3456 if (!EQ (values_here[p->idx], new_value))
3457 break;
3460 if (p->handler)
3461 break;
3464 if (next_iv)
3466 if (INTEGERP (limit)
3467 && next_iv->position >= XFASTINT (limit))
3468 /* No text property change up to limit. */
3469 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3470 else
3471 /* Text properties change in next_iv. */
3472 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3476 if (it->cmp_it.id < 0)
3478 ptrdiff_t stoppos = it->end_charpos;
3480 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3481 stoppos = -1;
3482 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3483 stoppos, it->string);
3486 eassert (STRINGP (it->string)
3487 || (it->stop_charpos >= BEGV
3488 && it->stop_charpos >= IT_CHARPOS (*it)));
3492 /* Return the position of the next overlay change after POS in
3493 current_buffer. Value is point-max if no overlay change
3494 follows. This is like `next-overlay-change' but doesn't use
3495 xmalloc. */
3497 static ptrdiff_t
3498 next_overlay_change (ptrdiff_t pos)
3500 ptrdiff_t i, noverlays;
3501 ptrdiff_t endpos;
3502 Lisp_Object *overlays;
3504 /* Get all overlays at the given position. */
3505 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3507 /* If any of these overlays ends before endpos,
3508 use its ending point instead. */
3509 for (i = 0; i < noverlays; ++i)
3511 Lisp_Object oend;
3512 ptrdiff_t oendpos;
3514 oend = OVERLAY_END (overlays[i]);
3515 oendpos = OVERLAY_POSITION (oend);
3516 endpos = min (endpos, oendpos);
3519 return endpos;
3522 /* How many characters forward to search for a display property or
3523 display string. Searching too far forward makes the bidi display
3524 sluggish, especially in small windows. */
3525 #define MAX_DISP_SCAN 250
3527 /* Return the character position of a display string at or after
3528 position specified by POSITION. If no display string exists at or
3529 after POSITION, return ZV. A display string is either an overlay
3530 with `display' property whose value is a string, or a `display'
3531 text property whose value is a string. STRING is data about the
3532 string to iterate; if STRING->lstring is nil, we are iterating a
3533 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3534 on a GUI frame. DISP_PROP is set to zero if we searched
3535 MAX_DISP_SCAN characters forward without finding any display
3536 strings, non-zero otherwise. It is set to 2 if the display string
3537 uses any kind of `(space ...)' spec that will produce a stretch of
3538 white space in the text area. */
3539 ptrdiff_t
3540 compute_display_string_pos (struct text_pos *position,
3541 struct bidi_string_data *string,
3542 struct window *w,
3543 int frame_window_p, int *disp_prop)
3545 /* OBJECT = nil means current buffer. */
3546 Lisp_Object object, object1;
3547 Lisp_Object pos, spec, limpos;
3548 int string_p = (string && (STRINGP (string->lstring) || string->s));
3549 ptrdiff_t eob = string_p ? string->schars : ZV;
3550 ptrdiff_t begb = string_p ? 0 : BEGV;
3551 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3552 ptrdiff_t lim =
3553 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3554 struct text_pos tpos;
3555 int rv = 0;
3557 if (string && STRINGP (string->lstring))
3558 object1 = object = string->lstring;
3559 else if (w && !string_p)
3561 XSETWINDOW (object, w);
3562 object1 = Qnil;
3564 else
3565 object1 = object = Qnil;
3567 *disp_prop = 1;
3569 if (charpos >= eob
3570 /* We don't support display properties whose values are strings
3571 that have display string properties. */
3572 || string->from_disp_str
3573 /* C strings cannot have display properties. */
3574 || (string->s && !STRINGP (object)))
3576 *disp_prop = 0;
3577 return eob;
3580 /* If the character at CHARPOS is where the display string begins,
3581 return CHARPOS. */
3582 pos = make_number (charpos);
3583 if (STRINGP (object))
3584 bufpos = string->bufpos;
3585 else
3586 bufpos = charpos;
3587 tpos = *position;
3588 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3589 && (charpos <= begb
3590 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3591 object),
3592 spec))
3593 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3594 frame_window_p)))
3596 if (rv == 2)
3597 *disp_prop = 2;
3598 return charpos;
3601 /* Look forward for the first character with a `display' property
3602 that will replace the underlying text when displayed. */
3603 limpos = make_number (lim);
3604 do {
3605 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3606 CHARPOS (tpos) = XFASTINT (pos);
3607 if (CHARPOS (tpos) >= lim)
3609 *disp_prop = 0;
3610 break;
3612 if (STRINGP (object))
3613 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3614 else
3615 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3616 spec = Fget_char_property (pos, Qdisplay, object);
3617 if (!STRINGP (object))
3618 bufpos = CHARPOS (tpos);
3619 } while (NILP (spec)
3620 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3621 bufpos, frame_window_p)));
3622 if (rv == 2)
3623 *disp_prop = 2;
3625 return CHARPOS (tpos);
3628 /* Return the character position of the end of the display string that
3629 started at CHARPOS. If there's no display string at CHARPOS,
3630 return -1. A display string is either an overlay with `display'
3631 property whose value is a string or a `display' text property whose
3632 value is a string. */
3633 ptrdiff_t
3634 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3636 /* OBJECT = nil means current buffer. */
3637 Lisp_Object object =
3638 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3639 Lisp_Object pos = make_number (charpos);
3640 ptrdiff_t eob =
3641 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3643 if (charpos >= eob || (string->s && !STRINGP (object)))
3644 return eob;
3646 /* It could happen that the display property or overlay was removed
3647 since we found it in compute_display_string_pos above. One way
3648 this can happen is if JIT font-lock was called (through
3649 handle_fontified_prop), and jit-lock-functions remove text
3650 properties or overlays from the portion of buffer that includes
3651 CHARPOS. Muse mode is known to do that, for example. In this
3652 case, we return -1 to the caller, to signal that no display
3653 string is actually present at CHARPOS. See bidi_fetch_char for
3654 how this is handled.
3656 An alternative would be to never look for display properties past
3657 it->stop_charpos. But neither compute_display_string_pos nor
3658 bidi_fetch_char that calls it know or care where the next
3659 stop_charpos is. */
3660 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3661 return -1;
3663 /* Look forward for the first character where the `display' property
3664 changes. */
3665 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3667 return XFASTINT (pos);
3672 /***********************************************************************
3673 Fontification
3674 ***********************************************************************/
3676 /* Handle changes in the `fontified' property of the current buffer by
3677 calling hook functions from Qfontification_functions to fontify
3678 regions of text. */
3680 static enum prop_handled
3681 handle_fontified_prop (struct it *it)
3683 Lisp_Object prop, pos;
3684 enum prop_handled handled = HANDLED_NORMALLY;
3686 if (!NILP (Vmemory_full))
3687 return handled;
3689 /* Get the value of the `fontified' property at IT's current buffer
3690 position. (The `fontified' property doesn't have a special
3691 meaning in strings.) If the value is nil, call functions from
3692 Qfontification_functions. */
3693 if (!STRINGP (it->string)
3694 && it->s == NULL
3695 && !NILP (Vfontification_functions)
3696 && !NILP (Vrun_hooks)
3697 && (pos = make_number (IT_CHARPOS (*it)),
3698 prop = Fget_char_property (pos, Qfontified, Qnil),
3699 /* Ignore the special cased nil value always present at EOB since
3700 no amount of fontifying will be able to change it. */
3701 NILP (prop) && IT_CHARPOS (*it) < Z))
3703 ptrdiff_t count = SPECPDL_INDEX ();
3704 Lisp_Object val;
3705 struct buffer *obuf = current_buffer;
3706 ptrdiff_t begv = BEGV, zv = ZV;
3707 bool old_clip_changed = current_buffer->clip_changed;
3709 val = Vfontification_functions;
3710 specbind (Qfontification_functions, Qnil);
3712 eassert (it->end_charpos == ZV);
3714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3715 safe_call1 (val, pos);
3716 else
3718 Lisp_Object fns, fn;
3719 struct gcpro gcpro1, gcpro2;
3721 fns = Qnil;
3722 GCPRO2 (val, fns);
3724 for (; CONSP (val); val = XCDR (val))
3726 fn = XCAR (val);
3728 if (EQ (fn, Qt))
3730 /* A value of t indicates this hook has a local
3731 binding; it means to run the global binding too.
3732 In a global value, t should not occur. If it
3733 does, we must ignore it to avoid an endless
3734 loop. */
3735 for (fns = Fdefault_value (Qfontification_functions);
3736 CONSP (fns);
3737 fns = XCDR (fns))
3739 fn = XCAR (fns);
3740 if (!EQ (fn, Qt))
3741 safe_call1 (fn, pos);
3744 else
3745 safe_call1 (fn, pos);
3748 UNGCPRO;
3751 unbind_to (count, Qnil);
3753 /* Fontification functions routinely call `save-restriction'.
3754 Normally, this tags clip_changed, which can confuse redisplay
3755 (see discussion in Bug#6671). Since we don't perform any
3756 special handling of fontification changes in the case where
3757 `save-restriction' isn't called, there's no point doing so in
3758 this case either. So, if the buffer's restrictions are
3759 actually left unchanged, reset clip_changed. */
3760 if (obuf == current_buffer)
3762 if (begv == BEGV && zv == ZV)
3763 current_buffer->clip_changed = old_clip_changed;
3765 /* There isn't much we can reasonably do to protect against
3766 misbehaving fontification, but here's a fig leaf. */
3767 else if (BUFFER_LIVE_P (obuf))
3768 set_buffer_internal_1 (obuf);
3770 /* The fontification code may have added/removed text.
3771 It could do even a lot worse, but let's at least protect against
3772 the most obvious case where only the text past `pos' gets changed',
3773 as is/was done in grep.el where some escapes sequences are turned
3774 into face properties (bug#7876). */
3775 it->end_charpos = ZV;
3777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3778 something. This avoids an endless loop if they failed to
3779 fontify the text for which reason ever. */
3780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3781 handled = HANDLED_RECOMPUTE_PROPS;
3784 return handled;
3789 /***********************************************************************
3790 Faces
3791 ***********************************************************************/
3793 /* Set up iterator IT from face properties at its current position.
3794 Called from handle_stop. */
3796 static enum prop_handled
3797 handle_face_prop (struct it *it)
3799 int new_face_id;
3800 ptrdiff_t next_stop;
3802 if (!STRINGP (it->string))
3804 new_face_id
3805 = face_at_buffer_position (it->w,
3806 IT_CHARPOS (*it),
3807 &next_stop,
3808 (IT_CHARPOS (*it)
3809 + TEXT_PROP_DISTANCE_LIMIT),
3810 0, it->base_face_id);
3812 /* Is this a start of a run of characters with box face?
3813 Caveat: this can be called for a freshly initialized
3814 iterator; face_id is -1 in this case. We know that the new
3815 face will not change until limit, i.e. if the new face has a
3816 box, all characters up to limit will have one. But, as
3817 usual, we don't know whether limit is really the end. */
3818 if (new_face_id != it->face_id)
3820 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3821 /* If it->face_id is -1, old_face below will be NULL, see
3822 the definition of FACE_FROM_ID. This will happen if this
3823 is the initial call that gets the face. */
3824 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3826 /* If the value of face_id of the iterator is -1, we have to
3827 look in front of IT's position and see whether there is a
3828 face there that's different from new_face_id. */
3829 if (!old_face && IT_CHARPOS (*it) > BEG)
3831 int prev_face_id = face_before_it_pos (it);
3833 old_face = FACE_FROM_ID (it->f, prev_face_id);
3836 /* If the new face has a box, but the old face does not,
3837 this is the start of a run of characters with box face,
3838 i.e. this character has a shadow on the left side. */
3839 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3840 && (old_face == NULL || !old_face->box));
3841 it->face_box_p = new_face->box != FACE_NO_BOX;
3844 else
3846 int base_face_id;
3847 ptrdiff_t bufpos;
3848 int i;
3849 Lisp_Object from_overlay
3850 = (it->current.overlay_string_index >= 0
3851 ? it->string_overlays[it->current.overlay_string_index
3852 % OVERLAY_STRING_CHUNK_SIZE]
3853 : Qnil);
3855 /* See if we got to this string directly or indirectly from
3856 an overlay property. That includes the before-string or
3857 after-string of an overlay, strings in display properties
3858 provided by an overlay, their text properties, etc.
3860 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3861 if (! NILP (from_overlay))
3862 for (i = it->sp - 1; i >= 0; i--)
3864 if (it->stack[i].current.overlay_string_index >= 0)
3865 from_overlay
3866 = it->string_overlays[it->stack[i].current.overlay_string_index
3867 % OVERLAY_STRING_CHUNK_SIZE];
3868 else if (! NILP (it->stack[i].from_overlay))
3869 from_overlay = it->stack[i].from_overlay;
3871 if (!NILP (from_overlay))
3872 break;
3875 if (! NILP (from_overlay))
3877 bufpos = IT_CHARPOS (*it);
3878 /* For a string from an overlay, the base face depends
3879 only on text properties and ignores overlays. */
3880 base_face_id
3881 = face_for_overlay_string (it->w,
3882 IT_CHARPOS (*it),
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3887 from_overlay);
3889 else
3891 bufpos = 0;
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 &next_stop,
3912 base_face_id, 0);
3914 /* Is this a start of a run of characters with box? Caveat:
3915 this can be called for a freshly allocated iterator; face_id
3916 is -1 is this case. We know that the new face will not
3917 change until the next check pos, i.e. if the new face has a
3918 box, all characters up to that position will have a
3919 box. But, as usual, we don't know whether that position
3920 is really the end. */
3921 if (new_face_id != it->face_id)
3923 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3924 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3926 /* If new face has a box but old face hasn't, this is the
3927 start of a run of characters with box, i.e. it has a
3928 shadow on the left side. */
3929 it->start_of_box_run_p
3930 = new_face->box && (old_face == NULL || !old_face->box);
3931 it->face_box_p = new_face->box != FACE_NO_BOX;
3935 it->face_id = new_face_id;
3936 return HANDLED_NORMALLY;
3940 /* Return the ID of the face ``underlying'' IT's current position,
3941 which is in a string. If the iterator is associated with a
3942 buffer, return the face at IT's current buffer position.
3943 Otherwise, use the iterator's base_face_id. */
3945 static int
3946 underlying_face_id (struct it *it)
3948 int face_id = it->base_face_id, i;
3950 eassert (STRINGP (it->string));
3952 for (i = it->sp - 1; i >= 0; --i)
3953 if (NILP (it->stack[i].string))
3954 face_id = it->stack[i].face_id;
3956 return face_id;
3960 /* Compute the face one character before or after the current position
3961 of IT, in the visual order. BEFORE_P non-zero means get the face
3962 in front (to the left in L2R paragraphs, to the right in R2L
3963 paragraphs) of IT's screen position. Value is the ID of the face. */
3965 static int
3966 face_before_or_after_it_pos (struct it *it, int before_p)
3968 int face_id, limit;
3969 ptrdiff_t next_check_charpos;
3970 struct it it_copy;
3971 void *it_copy_data = NULL;
3973 eassert (it->s == NULL);
3975 if (STRINGP (it->string))
3977 ptrdiff_t bufpos, charpos;
3978 int base_face_id;
3980 /* No face change past the end of the string (for the case
3981 we are padding with spaces). No face change before the
3982 string start. */
3983 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3984 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3985 return it->face_id;
3987 if (!it->bidi_p)
3989 /* Set charpos to the position before or after IT's current
3990 position, in the logical order, which in the non-bidi
3991 case is the same as the visual order. */
3992 if (before_p)
3993 charpos = IT_STRING_CHARPOS (*it) - 1;
3994 else if (it->what == IT_COMPOSITION)
3995 /* For composition, we must check the character after the
3996 composition. */
3997 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3998 else
3999 charpos = IT_STRING_CHARPOS (*it) + 1;
4001 else
4003 if (before_p)
4005 /* With bidi iteration, the character before the current
4006 in the visual order cannot be found by simple
4007 iteration, because "reverse" reordering is not
4008 supported. Instead, we need to use the move_it_*
4009 family of functions. */
4010 /* Ignore face changes before the first visible
4011 character on this display line. */
4012 if (it->current_x <= it->first_visible_x)
4013 return it->face_id;
4014 SAVE_IT (it_copy, *it, it_copy_data);
4015 /* Implementation note: Since move_it_in_display_line
4016 works in the iterator geometry, and thinks the first
4017 character is always the leftmost, even in R2L lines,
4018 we don't need to distinguish between the R2L and L2R
4019 cases here. */
4020 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4021 it_copy.current_x - 1, MOVE_TO_X);
4022 charpos = IT_STRING_CHARPOS (it_copy);
4023 RESTORE_IT (it, it, it_copy_data);
4025 else
4027 /* Set charpos to the string position of the character
4028 that comes after IT's current position in the visual
4029 order. */
4030 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4032 it_copy = *it;
4033 while (n--)
4034 bidi_move_to_visually_next (&it_copy.bidi_it);
4036 charpos = it_copy.bidi_it.charpos;
4039 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4041 if (it->current.overlay_string_index >= 0)
4042 bufpos = IT_CHARPOS (*it);
4043 else
4044 bufpos = 0;
4046 base_face_id = underlying_face_id (it);
4048 /* Get the face for ASCII, or unibyte. */
4049 face_id = face_at_string_position (it->w,
4050 it->string,
4051 charpos,
4052 bufpos,
4053 &next_check_charpos,
4054 base_face_id, 0);
4056 /* Correct the face for charsets different from ASCII. Do it
4057 for the multibyte case only. The face returned above is
4058 suitable for unibyte text if IT->string is unibyte. */
4059 if (STRING_MULTIBYTE (it->string))
4061 struct text_pos pos1 = string_pos (charpos, it->string);
4062 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4063 int c, len;
4064 struct face *face = FACE_FROM_ID (it->f, face_id);
4066 c = string_char_and_length (p, &len);
4067 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4070 else
4072 struct text_pos pos;
4074 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4075 || (IT_CHARPOS (*it) <= BEGV && before_p))
4076 return it->face_id;
4078 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4079 pos = it->current.pos;
4081 if (!it->bidi_p)
4083 if (before_p)
4084 DEC_TEXT_POS (pos, it->multibyte_p);
4085 else
4087 if (it->what == IT_COMPOSITION)
4089 /* For composition, we must check the position after
4090 the composition. */
4091 pos.charpos += it->cmp_it.nchars;
4092 pos.bytepos += it->len;
4094 else
4095 INC_TEXT_POS (pos, it->multibyte_p);
4098 else
4100 if (before_p)
4102 /* With bidi iteration, the character before the current
4103 in the visual order cannot be found by simple
4104 iteration, because "reverse" reordering is not
4105 supported. Instead, we need to use the move_it_*
4106 family of functions. */
4107 /* Ignore face changes before the first visible
4108 character on this display line. */
4109 if (it->current_x <= it->first_visible_x)
4110 return it->face_id;
4111 SAVE_IT (it_copy, *it, it_copy_data);
4112 /* Implementation note: Since move_it_in_display_line
4113 works in the iterator geometry, and thinks the first
4114 character is always the leftmost, even in R2L lines,
4115 we don't need to distinguish between the R2L and L2R
4116 cases here. */
4117 move_it_in_display_line (&it_copy, ZV,
4118 it_copy.current_x - 1, MOVE_TO_X);
4119 pos = it_copy.current.pos;
4120 RESTORE_IT (it, it, it_copy_data);
4122 else
4124 /* Set charpos to the buffer position of the character
4125 that comes after IT's current position in the visual
4126 order. */
4127 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4129 it_copy = *it;
4130 while (n--)
4131 bidi_move_to_visually_next (&it_copy.bidi_it);
4133 SET_TEXT_POS (pos,
4134 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4137 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4139 /* Determine face for CHARSET_ASCII, or unibyte. */
4140 face_id = face_at_buffer_position (it->w,
4141 CHARPOS (pos),
4142 &next_check_charpos,
4143 limit, 0, -1);
4145 /* Correct the face for charsets different from ASCII. Do it
4146 for the multibyte case only. The face returned above is
4147 suitable for unibyte text if current_buffer is unibyte. */
4148 if (it->multibyte_p)
4150 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4151 struct face *face = FACE_FROM_ID (it->f, face_id);
4152 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4156 return face_id;
4161 /***********************************************************************
4162 Invisible text
4163 ***********************************************************************/
4165 /* Set up iterator IT from invisible properties at its current
4166 position. Called from handle_stop. */
4168 static enum prop_handled
4169 handle_invisible_prop (struct it *it)
4171 enum prop_handled handled = HANDLED_NORMALLY;
4172 int invis_p;
4173 Lisp_Object prop;
4175 if (STRINGP (it->string))
4177 Lisp_Object end_charpos, limit, charpos;
4179 /* Get the value of the invisible text property at the
4180 current position. Value will be nil if there is no such
4181 property. */
4182 charpos = make_number (IT_STRING_CHARPOS (*it));
4183 prop = Fget_text_property (charpos, Qinvisible, it->string);
4184 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4186 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4188 /* Record whether we have to display an ellipsis for the
4189 invisible text. */
4190 int display_ellipsis_p = (invis_p == 2);
4191 ptrdiff_t len, endpos;
4193 handled = HANDLED_RECOMPUTE_PROPS;
4195 /* Get the position at which the next visible text can be
4196 found in IT->string, if any. */
4197 endpos = len = SCHARS (it->string);
4198 XSETINT (limit, len);
4201 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4202 it->string, limit);
4203 if (INTEGERP (end_charpos))
4205 endpos = XFASTINT (end_charpos);
4206 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4208 if (invis_p == 2)
4209 display_ellipsis_p = 1;
4212 while (invis_p && endpos < len);
4214 if (display_ellipsis_p)
4215 it->ellipsis_p = 1;
4217 if (endpos < len)
4219 /* Text at END_CHARPOS is visible. Move IT there. */
4220 struct text_pos old;
4221 ptrdiff_t oldpos;
4223 old = it->current.string_pos;
4224 oldpos = CHARPOS (old);
4225 if (it->bidi_p)
4227 if (it->bidi_it.first_elt
4228 && it->bidi_it.charpos < SCHARS (it->string))
4229 bidi_paragraph_init (it->paragraph_embedding,
4230 &it->bidi_it, 1);
4231 /* Bidi-iterate out of the invisible text. */
4234 bidi_move_to_visually_next (&it->bidi_it);
4236 while (oldpos <= it->bidi_it.charpos
4237 && it->bidi_it.charpos < endpos);
4239 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4240 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4241 if (IT_CHARPOS (*it) >= endpos)
4242 it->prev_stop = endpos;
4244 else
4246 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4247 compute_string_pos (&it->current.string_pos, old, it->string);
4250 else
4252 /* The rest of the string is invisible. If this is an
4253 overlay string, proceed with the next overlay string
4254 or whatever comes and return a character from there. */
4255 if (it->current.overlay_string_index >= 0
4256 && !display_ellipsis_p)
4258 next_overlay_string (it);
4259 /* Don't check for overlay strings when we just
4260 finished processing them. */
4261 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4263 else
4265 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4266 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4271 else
4273 ptrdiff_t newpos, next_stop, start_charpos, tem;
4274 Lisp_Object pos, overlay;
4276 /* First of all, is there invisible text at this position? */
4277 tem = start_charpos = IT_CHARPOS (*it);
4278 pos = make_number (tem);
4279 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4280 &overlay);
4281 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4283 /* If we are on invisible text, skip over it. */
4284 if (invis_p && start_charpos < it->end_charpos)
4286 /* Record whether we have to display an ellipsis for the
4287 invisible text. */
4288 int display_ellipsis_p = invis_p == 2;
4290 handled = HANDLED_RECOMPUTE_PROPS;
4292 /* Loop skipping over invisible text. The loop is left at
4293 ZV or with IT on the first char being visible again. */
4296 /* Try to skip some invisible text. Return value is the
4297 position reached which can be equal to where we start
4298 if there is nothing invisible there. This skips both
4299 over invisible text properties and overlays with
4300 invisible property. */
4301 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4303 /* If we skipped nothing at all we weren't at invisible
4304 text in the first place. If everything to the end of
4305 the buffer was skipped, end the loop. */
4306 if (newpos == tem || newpos >= ZV)
4307 invis_p = 0;
4308 else
4310 /* We skipped some characters but not necessarily
4311 all there are. Check if we ended up on visible
4312 text. Fget_char_property returns the property of
4313 the char before the given position, i.e. if we
4314 get invis_p = 0, this means that the char at
4315 newpos is visible. */
4316 pos = make_number (newpos);
4317 prop = Fget_char_property (pos, Qinvisible, it->window);
4318 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4321 /* If we ended up on invisible text, proceed to
4322 skip starting with next_stop. */
4323 if (invis_p)
4324 tem = next_stop;
4326 /* If there are adjacent invisible texts, don't lose the
4327 second one's ellipsis. */
4328 if (invis_p == 2)
4329 display_ellipsis_p = 1;
4331 while (invis_p);
4333 /* The position newpos is now either ZV or on visible text. */
4334 if (it->bidi_p)
4336 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4337 int on_newline =
4338 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4339 int after_newline =
4340 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4342 /* If the invisible text ends on a newline or on a
4343 character after a newline, we can avoid the costly,
4344 character by character, bidi iteration to NEWPOS, and
4345 instead simply reseat the iterator there. That's
4346 because all bidi reordering information is tossed at
4347 the newline. This is a big win for modes that hide
4348 complete lines, like Outline, Org, etc. */
4349 if (on_newline || after_newline)
4351 struct text_pos tpos;
4352 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4354 SET_TEXT_POS (tpos, newpos, bpos);
4355 reseat_1 (it, tpos, 0);
4356 /* If we reseat on a newline/ZV, we need to prep the
4357 bidi iterator for advancing to the next character
4358 after the newline/EOB, keeping the current paragraph
4359 direction (so that PRODUCE_GLYPHS does TRT wrt
4360 prepending/appending glyphs to a glyph row). */
4361 if (on_newline)
4363 it->bidi_it.first_elt = 0;
4364 it->bidi_it.paragraph_dir = pdir;
4365 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4366 it->bidi_it.nchars = 1;
4367 it->bidi_it.ch_len = 1;
4370 else /* Must use the slow method. */
4372 /* With bidi iteration, the region of invisible text
4373 could start and/or end in the middle of a
4374 non-base embedding level. Therefore, we need to
4375 skip invisible text using the bidi iterator,
4376 starting at IT's current position, until we find
4377 ourselves outside of the invisible text.
4378 Skipping invisible text _after_ bidi iteration
4379 avoids affecting the visual order of the
4380 displayed text when invisible properties are
4381 added or removed. */
4382 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4384 /* If we were `reseat'ed to a new paragraph,
4385 determine the paragraph base direction. We
4386 need to do it now because
4387 next_element_from_buffer may not have a
4388 chance to do it, if we are going to skip any
4389 text at the beginning, which resets the
4390 FIRST_ELT flag. */
4391 bidi_paragraph_init (it->paragraph_embedding,
4392 &it->bidi_it, 1);
4396 bidi_move_to_visually_next (&it->bidi_it);
4398 while (it->stop_charpos <= it->bidi_it.charpos
4399 && it->bidi_it.charpos < newpos);
4400 IT_CHARPOS (*it) = it->bidi_it.charpos;
4401 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4402 /* If we overstepped NEWPOS, record its position in
4403 the iterator, so that we skip invisible text if
4404 later the bidi iteration lands us in the
4405 invisible region again. */
4406 if (IT_CHARPOS (*it) >= newpos)
4407 it->prev_stop = newpos;
4410 else
4412 IT_CHARPOS (*it) = newpos;
4413 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4416 /* If there are before-strings at the start of invisible
4417 text, and the text is invisible because of a text
4418 property, arrange to show before-strings because 20.x did
4419 it that way. (If the text is invisible because of an
4420 overlay property instead of a text property, this is
4421 already handled in the overlay code.) */
4422 if (NILP (overlay)
4423 && get_overlay_strings (it, it->stop_charpos))
4425 handled = HANDLED_RECOMPUTE_PROPS;
4426 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4428 else if (display_ellipsis_p)
4430 /* Make sure that the glyphs of the ellipsis will get
4431 correct `charpos' values. If we would not update
4432 it->position here, the glyphs would belong to the
4433 last visible character _before_ the invisible
4434 text, which confuses `set_cursor_from_row'.
4436 We use the last invisible position instead of the
4437 first because this way the cursor is always drawn on
4438 the first "." of the ellipsis, whenever PT is inside
4439 the invisible text. Otherwise the cursor would be
4440 placed _after_ the ellipsis when the point is after the
4441 first invisible character. */
4442 if (!STRINGP (it->object))
4444 it->position.charpos = newpos - 1;
4445 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4447 it->ellipsis_p = 1;
4448 /* Let the ellipsis display before
4449 considering any properties of the following char.
4450 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4451 handled = HANDLED_RETURN;
4456 return handled;
4460 /* Make iterator IT return `...' next.
4461 Replaces LEN characters from buffer. */
4463 static void
4464 setup_for_ellipsis (struct it *it, int len)
4466 /* Use the display table definition for `...'. Invalid glyphs
4467 will be handled by the method returning elements from dpvec. */
4468 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4470 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4471 it->dpvec = v->contents;
4472 it->dpend = v->contents + v->header.size;
4474 else
4476 /* Default `...'. */
4477 it->dpvec = default_invis_vector;
4478 it->dpend = default_invis_vector + 3;
4481 it->dpvec_char_len = len;
4482 it->current.dpvec_index = 0;
4483 it->dpvec_face_id = -1;
4485 /* Remember the current face id in case glyphs specify faces.
4486 IT's face is restored in set_iterator_to_next.
4487 saved_face_id was set to preceding char's face in handle_stop. */
4488 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4489 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4491 it->method = GET_FROM_DISPLAY_VECTOR;
4492 it->ellipsis_p = 1;
4497 /***********************************************************************
4498 'display' property
4499 ***********************************************************************/
4501 /* Set up iterator IT from `display' property at its current position.
4502 Called from handle_stop.
4503 We return HANDLED_RETURN if some part of the display property
4504 overrides the display of the buffer text itself.
4505 Otherwise we return HANDLED_NORMALLY. */
4507 static enum prop_handled
4508 handle_display_prop (struct it *it)
4510 Lisp_Object propval, object, overlay;
4511 struct text_pos *position;
4512 ptrdiff_t bufpos;
4513 /* Nonzero if some property replaces the display of the text itself. */
4514 int display_replaced_p = 0;
4516 if (STRINGP (it->string))
4518 object = it->string;
4519 position = &it->current.string_pos;
4520 bufpos = CHARPOS (it->current.pos);
4522 else
4524 XSETWINDOW (object, it->w);
4525 position = &it->current.pos;
4526 bufpos = CHARPOS (*position);
4529 /* Reset those iterator values set from display property values. */
4530 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4531 it->space_width = Qnil;
4532 it->font_height = Qnil;
4533 it->voffset = 0;
4535 /* We don't support recursive `display' properties, i.e. string
4536 values that have a string `display' property, that have a string
4537 `display' property etc. */
4538 if (!it->string_from_display_prop_p)
4539 it->area = TEXT_AREA;
4541 propval = get_char_property_and_overlay (make_number (position->charpos),
4542 Qdisplay, object, &overlay);
4543 if (NILP (propval))
4544 return HANDLED_NORMALLY;
4545 /* Now OVERLAY is the overlay that gave us this property, or nil
4546 if it was a text property. */
4548 if (!STRINGP (it->string))
4549 object = it->w->contents;
4551 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4552 position, bufpos,
4553 FRAME_WINDOW_P (it->f));
4555 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4558 /* Subroutine of handle_display_prop. Returns non-zero if the display
4559 specification in SPEC is a replacing specification, i.e. it would
4560 replace the text covered by `display' property with something else,
4561 such as an image or a display string. If SPEC includes any kind or
4562 `(space ...) specification, the value is 2; this is used by
4563 compute_display_string_pos, which see.
4565 See handle_single_display_spec for documentation of arguments.
4566 frame_window_p is non-zero if the window being redisplayed is on a
4567 GUI frame; this argument is used only if IT is NULL, see below.
4569 IT can be NULL, if this is called by the bidi reordering code
4570 through compute_display_string_pos, which see. In that case, this
4571 function only examines SPEC, but does not otherwise "handle" it, in
4572 the sense that it doesn't set up members of IT from the display
4573 spec. */
4574 static int
4575 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4576 Lisp_Object overlay, struct text_pos *position,
4577 ptrdiff_t bufpos, int frame_window_p)
4579 int replacing_p = 0;
4580 int rv;
4582 if (CONSP (spec)
4583 /* Simple specifications. */
4584 && !EQ (XCAR (spec), Qimage)
4585 && !EQ (XCAR (spec), Qspace)
4586 && !EQ (XCAR (spec), Qwhen)
4587 && !EQ (XCAR (spec), Qslice)
4588 && !EQ (XCAR (spec), Qspace_width)
4589 && !EQ (XCAR (spec), Qheight)
4590 && !EQ (XCAR (spec), Qraise)
4591 /* Marginal area specifications. */
4592 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4593 && !EQ (XCAR (spec), Qleft_fringe)
4594 && !EQ (XCAR (spec), Qright_fringe)
4595 && !NILP (XCAR (spec)))
4597 for (; CONSP (spec); spec = XCDR (spec))
4599 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4600 overlay, position, bufpos,
4601 replacing_p, frame_window_p)))
4603 replacing_p = rv;
4604 /* If some text in a string is replaced, `position' no
4605 longer points to the position of `object'. */
4606 if (!it || STRINGP (object))
4607 break;
4611 else if (VECTORP (spec))
4613 ptrdiff_t i;
4614 for (i = 0; i < ASIZE (spec); ++i)
4615 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4616 overlay, position, bufpos,
4617 replacing_p, frame_window_p)))
4619 replacing_p = rv;
4620 /* If some text in a string is replaced, `position' no
4621 longer points to the position of `object'. */
4622 if (!it || STRINGP (object))
4623 break;
4626 else
4628 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4629 position, bufpos, 0,
4630 frame_window_p)))
4631 replacing_p = rv;
4634 return replacing_p;
4637 /* Value is the position of the end of the `display' property starting
4638 at START_POS in OBJECT. */
4640 static struct text_pos
4641 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4643 Lisp_Object end;
4644 struct text_pos end_pos;
4646 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4647 Qdisplay, object, Qnil);
4648 CHARPOS (end_pos) = XFASTINT (end);
4649 if (STRINGP (object))
4650 compute_string_pos (&end_pos, start_pos, it->string);
4651 else
4652 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4654 return end_pos;
4658 /* Set up IT from a single `display' property specification SPEC. OBJECT
4659 is the object in which the `display' property was found. *POSITION
4660 is the position in OBJECT at which the `display' property was found.
4661 BUFPOS is the buffer position of OBJECT (different from POSITION if
4662 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4663 previously saw a display specification which already replaced text
4664 display with something else, for example an image; we ignore such
4665 properties after the first one has been processed.
4667 OVERLAY is the overlay this `display' property came from,
4668 or nil if it was a text property.
4670 If SPEC is a `space' or `image' specification, and in some other
4671 cases too, set *POSITION to the position where the `display'
4672 property ends.
4674 If IT is NULL, only examine the property specification in SPEC, but
4675 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4676 is intended to be displayed in a window on a GUI frame.
4678 Value is non-zero if something was found which replaces the display
4679 of buffer or string text. */
4681 static int
4682 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4683 Lisp_Object overlay, struct text_pos *position,
4684 ptrdiff_t bufpos, int display_replaced_p,
4685 int frame_window_p)
4687 Lisp_Object form;
4688 Lisp_Object location, value;
4689 struct text_pos start_pos = *position;
4690 int valid_p;
4692 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4693 If the result is non-nil, use VALUE instead of SPEC. */
4694 form = Qt;
4695 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4697 spec = XCDR (spec);
4698 if (!CONSP (spec))
4699 return 0;
4700 form = XCAR (spec);
4701 spec = XCDR (spec);
4704 if (!NILP (form) && !EQ (form, Qt))
4706 ptrdiff_t count = SPECPDL_INDEX ();
4707 struct gcpro gcpro1;
4709 /* Bind `object' to the object having the `display' property, a
4710 buffer or string. Bind `position' to the position in the
4711 object where the property was found, and `buffer-position'
4712 to the current position in the buffer. */
4714 if (NILP (object))
4715 XSETBUFFER (object, current_buffer);
4716 specbind (Qobject, object);
4717 specbind (Qposition, make_number (CHARPOS (*position)));
4718 specbind (Qbuffer_position, make_number (bufpos));
4719 GCPRO1 (form);
4720 form = safe_eval (form);
4721 UNGCPRO;
4722 unbind_to (count, Qnil);
4725 if (NILP (form))
4726 return 0;
4728 /* Handle `(height HEIGHT)' specifications. */
4729 if (CONSP (spec)
4730 && EQ (XCAR (spec), Qheight)
4731 && CONSP (XCDR (spec)))
4733 if (it)
4735 if (!FRAME_WINDOW_P (it->f))
4736 return 0;
4738 it->font_height = XCAR (XCDR (spec));
4739 if (!NILP (it->font_height))
4741 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4742 int new_height = -1;
4744 if (CONSP (it->font_height)
4745 && (EQ (XCAR (it->font_height), Qplus)
4746 || EQ (XCAR (it->font_height), Qminus))
4747 && CONSP (XCDR (it->font_height))
4748 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4750 /* `(+ N)' or `(- N)' where N is an integer. */
4751 int steps = XINT (XCAR (XCDR (it->font_height)));
4752 if (EQ (XCAR (it->font_height), Qplus))
4753 steps = - steps;
4754 it->face_id = smaller_face (it->f, it->face_id, steps);
4756 else if (FUNCTIONP (it->font_height))
4758 /* Call function with current height as argument.
4759 Value is the new height. */
4760 Lisp_Object height;
4761 height = safe_call1 (it->font_height,
4762 face->lface[LFACE_HEIGHT_INDEX]);
4763 if (NUMBERP (height))
4764 new_height = XFLOATINT (height);
4766 else if (NUMBERP (it->font_height))
4768 /* Value is a multiple of the canonical char height. */
4769 struct face *f;
4771 f = FACE_FROM_ID (it->f,
4772 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4773 new_height = (XFLOATINT (it->font_height)
4774 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4776 else
4778 /* Evaluate IT->font_height with `height' bound to the
4779 current specified height to get the new height. */
4780 ptrdiff_t count = SPECPDL_INDEX ();
4782 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4783 value = safe_eval (it->font_height);
4784 unbind_to (count, Qnil);
4786 if (NUMBERP (value))
4787 new_height = XFLOATINT (value);
4790 if (new_height > 0)
4791 it->face_id = face_with_height (it->f, it->face_id, new_height);
4795 return 0;
4798 /* Handle `(space-width WIDTH)'. */
4799 if (CONSP (spec)
4800 && EQ (XCAR (spec), Qspace_width)
4801 && CONSP (XCDR (spec)))
4803 if (it)
4805 if (!FRAME_WINDOW_P (it->f))
4806 return 0;
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value) && XFLOATINT (value) > 0)
4810 it->space_width = value;
4813 return 0;
4816 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qslice))
4820 Lisp_Object tem;
4822 if (it)
4824 if (!FRAME_WINDOW_P (it->f))
4825 return 0;
4827 if (tem = XCDR (spec), CONSP (tem))
4829 it->slice.x = XCAR (tem);
4830 if (tem = XCDR (tem), CONSP (tem))
4832 it->slice.y = XCAR (tem);
4833 if (tem = XCDR (tem), CONSP (tem))
4835 it->slice.width = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4837 it->slice.height = XCAR (tem);
4843 return 0;
4846 /* Handle `(raise FACTOR)'. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qraise)
4849 && CONSP (XCDR (spec)))
4851 if (it)
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4856 #ifdef HAVE_WINDOW_SYSTEM
4857 value = XCAR (XCDR (spec));
4858 if (NUMBERP (value))
4860 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4861 it->voffset = - (XFLOATINT (value)
4862 * (FONT_HEIGHT (face->font)));
4864 #endif /* HAVE_WINDOW_SYSTEM */
4867 return 0;
4870 /* Don't handle the other kinds of display specifications
4871 inside a string that we got from a `display' property. */
4872 if (it && it->string_from_display_prop_p)
4873 return 0;
4875 /* Characters having this form of property are not displayed, so
4876 we have to find the end of the property. */
4877 if (it)
4879 start_pos = *position;
4880 *position = display_prop_end (it, object, start_pos);
4882 value = Qnil;
4884 /* Stop the scan at that end position--we assume that all
4885 text properties change there. */
4886 if (it)
4887 it->stop_charpos = position->charpos;
4889 /* Handle `(left-fringe BITMAP [FACE])'
4890 and `(right-fringe BITMAP [FACE])'. */
4891 if (CONSP (spec)
4892 && (EQ (XCAR (spec), Qleft_fringe)
4893 || EQ (XCAR (spec), Qright_fringe))
4894 && CONSP (XCDR (spec)))
4896 int fringe_bitmap;
4898 if (it)
4900 if (!FRAME_WINDOW_P (it->f))
4901 /* If we return here, POSITION has been advanced
4902 across the text with this property. */
4904 /* Synchronize the bidi iterator with POSITION. This is
4905 needed because we are not going to push the iterator
4906 on behalf of this display property, so there will be
4907 no pop_it call to do this synchronization for us. */
4908 if (it->bidi_p)
4910 it->position = *position;
4911 iterate_out_of_display_property (it);
4912 *position = it->position;
4914 return 1;
4917 else if (!frame_window_p)
4918 return 1;
4920 #ifdef HAVE_WINDOW_SYSTEM
4921 value = XCAR (XCDR (spec));
4922 if (!SYMBOLP (value)
4923 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4924 /* If we return here, POSITION has been advanced
4925 across the text with this property. */
4927 if (it && it->bidi_p)
4929 it->position = *position;
4930 iterate_out_of_display_property (it);
4931 *position = it->position;
4933 return 1;
4936 if (it)
4938 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4940 if (CONSP (XCDR (XCDR (spec))))
4942 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4943 int face_id2 = lookup_derived_face (it->f, face_name,
4944 FRINGE_FACE_ID, 0);
4945 if (face_id2 >= 0)
4946 face_id = face_id2;
4949 /* Save current settings of IT so that we can restore them
4950 when we are finished with the glyph property value. */
4951 push_it (it, position);
4953 it->area = TEXT_AREA;
4954 it->what = IT_IMAGE;
4955 it->image_id = -1; /* no image */
4956 it->position = start_pos;
4957 it->object = NILP (object) ? it->w->contents : object;
4958 it->method = GET_FROM_IMAGE;
4959 it->from_overlay = Qnil;
4960 it->face_id = face_id;
4961 it->from_disp_prop_p = 1;
4963 /* Say that we haven't consumed the characters with
4964 `display' property yet. The call to pop_it in
4965 set_iterator_to_next will clean this up. */
4966 *position = start_pos;
4968 if (EQ (XCAR (spec), Qleft_fringe))
4970 it->left_user_fringe_bitmap = fringe_bitmap;
4971 it->left_user_fringe_face_id = face_id;
4973 else
4975 it->right_user_fringe_bitmap = fringe_bitmap;
4976 it->right_user_fringe_face_id = face_id;
4979 #endif /* HAVE_WINDOW_SYSTEM */
4980 return 1;
4983 /* Prepare to handle `((margin left-margin) ...)',
4984 `((margin right-margin) ...)' and `((margin nil) ...)'
4985 prefixes for display specifications. */
4986 location = Qunbound;
4987 if (CONSP (spec) && CONSP (XCAR (spec)))
4989 Lisp_Object tem;
4991 value = XCDR (spec);
4992 if (CONSP (value))
4993 value = XCAR (value);
4995 tem = XCAR (spec);
4996 if (EQ (XCAR (tem), Qmargin)
4997 && (tem = XCDR (tem),
4998 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4999 (NILP (tem)
5000 || EQ (tem, Qleft_margin)
5001 || EQ (tem, Qright_margin))))
5002 location = tem;
5005 if (EQ (location, Qunbound))
5007 location = Qnil;
5008 value = spec;
5011 /* After this point, VALUE is the property after any
5012 margin prefix has been stripped. It must be a string,
5013 an image specification, or `(space ...)'.
5015 LOCATION specifies where to display: `left-margin',
5016 `right-margin' or nil. */
5018 valid_p = (STRINGP (value)
5019 #ifdef HAVE_WINDOW_SYSTEM
5020 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5021 && valid_image_p (value))
5022 #endif /* not HAVE_WINDOW_SYSTEM */
5023 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5025 if (valid_p && !display_replaced_p)
5027 int retval = 1;
5029 if (!it)
5031 /* Callers need to know whether the display spec is any kind
5032 of `(space ...)' spec that is about to affect text-area
5033 display. */
5034 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5035 retval = 2;
5036 return retval;
5039 /* Save current settings of IT so that we can restore them
5040 when we are finished with the glyph property value. */
5041 push_it (it, position);
5042 it->from_overlay = overlay;
5043 it->from_disp_prop_p = 1;
5045 if (NILP (location))
5046 it->area = TEXT_AREA;
5047 else if (EQ (location, Qleft_margin))
5048 it->area = LEFT_MARGIN_AREA;
5049 else
5050 it->area = RIGHT_MARGIN_AREA;
5052 if (STRINGP (value))
5054 it->string = value;
5055 it->multibyte_p = STRING_MULTIBYTE (it->string);
5056 it->current.overlay_string_index = -1;
5057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5058 it->end_charpos = it->string_nchars = SCHARS (it->string);
5059 it->method = GET_FROM_STRING;
5060 it->stop_charpos = 0;
5061 it->prev_stop = 0;
5062 it->base_level_stop = 0;
5063 it->string_from_display_prop_p = 1;
5064 /* Say that we haven't consumed the characters with
5065 `display' property yet. The call to pop_it in
5066 set_iterator_to_next will clean this up. */
5067 if (BUFFERP (object))
5068 *position = start_pos;
5070 /* Force paragraph direction to be that of the parent
5071 object. If the parent object's paragraph direction is
5072 not yet determined, default to L2R. */
5073 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5074 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5075 else
5076 it->paragraph_embedding = L2R;
5078 /* Set up the bidi iterator for this display string. */
5079 if (it->bidi_p)
5081 it->bidi_it.string.lstring = it->string;
5082 it->bidi_it.string.s = NULL;
5083 it->bidi_it.string.schars = it->end_charpos;
5084 it->bidi_it.string.bufpos = bufpos;
5085 it->bidi_it.string.from_disp_str = 1;
5086 it->bidi_it.string.unibyte = !it->multibyte_p;
5087 it->bidi_it.w = it->w;
5088 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5091 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5093 it->method = GET_FROM_STRETCH;
5094 it->object = value;
5095 *position = it->position = start_pos;
5096 retval = 1 + (it->area == TEXT_AREA);
5098 #ifdef HAVE_WINDOW_SYSTEM
5099 else
5101 it->what = IT_IMAGE;
5102 it->image_id = lookup_image (it->f, value);
5103 it->position = start_pos;
5104 it->object = NILP (object) ? it->w->contents : object;
5105 it->method = GET_FROM_IMAGE;
5107 /* Say that we haven't consumed the characters with
5108 `display' property yet. The call to pop_it in
5109 set_iterator_to_next will clean this up. */
5110 *position = start_pos;
5112 #endif /* HAVE_WINDOW_SYSTEM */
5114 return retval;
5117 /* Invalid property or property not supported. Restore
5118 POSITION to what it was before. */
5119 *position = start_pos;
5120 return 0;
5123 /* Check if PROP is a display property value whose text should be
5124 treated as intangible. OVERLAY is the overlay from which PROP
5125 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5126 specify the buffer position covered by PROP. */
5129 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5130 ptrdiff_t charpos, ptrdiff_t bytepos)
5132 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5133 struct text_pos position;
5135 SET_TEXT_POS (position, charpos, bytepos);
5136 return handle_display_spec (NULL, prop, Qnil, overlay,
5137 &position, charpos, frame_window_p);
5141 /* Return 1 if PROP is a display sub-property value containing STRING.
5143 Implementation note: this and the following function are really
5144 special cases of handle_display_spec and
5145 handle_single_display_spec, and should ideally use the same code.
5146 Until they do, these two pairs must be consistent and must be
5147 modified in sync. */
5149 static int
5150 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5152 if (EQ (string, prop))
5153 return 1;
5155 /* Skip over `when FORM'. */
5156 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5158 prop = XCDR (prop);
5159 if (!CONSP (prop))
5160 return 0;
5161 /* Actually, the condition following `when' should be eval'ed,
5162 like handle_single_display_spec does, and we should return
5163 zero if it evaluates to nil. However, this function is
5164 called only when the buffer was already displayed and some
5165 glyph in the glyph matrix was found to come from a display
5166 string. Therefore, the condition was already evaluated, and
5167 the result was non-nil, otherwise the display string wouldn't
5168 have been displayed and we would have never been called for
5169 this property. Thus, we can skip the evaluation and assume
5170 its result is non-nil. */
5171 prop = XCDR (prop);
5174 if (CONSP (prop))
5175 /* Skip over `margin LOCATION'. */
5176 if (EQ (XCAR (prop), Qmargin))
5178 prop = XCDR (prop);
5179 if (!CONSP (prop))
5180 return 0;
5182 prop = XCDR (prop);
5183 if (!CONSP (prop))
5184 return 0;
5187 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5191 /* Return 1 if STRING appears in the `display' property PROP. */
5193 static int
5194 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5196 if (CONSP (prop)
5197 && !EQ (XCAR (prop), Qwhen)
5198 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5200 /* A list of sub-properties. */
5201 while (CONSP (prop))
5203 if (single_display_spec_string_p (XCAR (prop), string))
5204 return 1;
5205 prop = XCDR (prop);
5208 else if (VECTORP (prop))
5210 /* A vector of sub-properties. */
5211 ptrdiff_t i;
5212 for (i = 0; i < ASIZE (prop); ++i)
5213 if (single_display_spec_string_p (AREF (prop, i), string))
5214 return 1;
5216 else
5217 return single_display_spec_string_p (prop, string);
5219 return 0;
5222 /* Look for STRING in overlays and text properties in the current
5223 buffer, between character positions FROM and TO (excluding TO).
5224 BACK_P non-zero means look back (in this case, TO is supposed to be
5225 less than FROM).
5226 Value is the first character position where STRING was found, or
5227 zero if it wasn't found before hitting TO.
5229 This function may only use code that doesn't eval because it is
5230 called asynchronously from note_mouse_highlight. */
5232 static ptrdiff_t
5233 string_buffer_position_lim (Lisp_Object string,
5234 ptrdiff_t from, ptrdiff_t to, int back_p)
5236 Lisp_Object limit, prop, pos;
5237 int found = 0;
5239 pos = make_number (max (from, BEGV));
5241 if (!back_p) /* looking forward */
5243 limit = make_number (min (to, ZV));
5244 while (!found && !EQ (pos, limit))
5246 prop = Fget_char_property (pos, Qdisplay, Qnil);
5247 if (!NILP (prop) && display_prop_string_p (prop, string))
5248 found = 1;
5249 else
5250 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5251 limit);
5254 else /* looking back */
5256 limit = make_number (max (to, BEGV));
5257 while (!found && !EQ (pos, limit))
5259 prop = Fget_char_property (pos, Qdisplay, Qnil);
5260 if (!NILP (prop) && display_prop_string_p (prop, string))
5261 found = 1;
5262 else
5263 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5264 limit);
5268 return found ? XINT (pos) : 0;
5271 /* Determine which buffer position in current buffer STRING comes from.
5272 AROUND_CHARPOS is an approximate position where it could come from.
5273 Value is the buffer position or 0 if it couldn't be determined.
5275 This function is necessary because we don't record buffer positions
5276 in glyphs generated from strings (to keep struct glyph small).
5277 This function may only use code that doesn't eval because it is
5278 called asynchronously from note_mouse_highlight. */
5280 static ptrdiff_t
5281 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5283 const int MAX_DISTANCE = 1000;
5284 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5285 around_charpos + MAX_DISTANCE,
5288 if (!found)
5289 found = string_buffer_position_lim (string, around_charpos,
5290 around_charpos - MAX_DISTANCE, 1);
5291 return found;
5296 /***********************************************************************
5297 `composition' property
5298 ***********************************************************************/
5300 /* Set up iterator IT from `composition' property at its current
5301 position. Called from handle_stop. */
5303 static enum prop_handled
5304 handle_composition_prop (struct it *it)
5306 Lisp_Object prop, string;
5307 ptrdiff_t pos, pos_byte, start, end;
5309 if (STRINGP (it->string))
5311 unsigned char *s;
5313 pos = IT_STRING_CHARPOS (*it);
5314 pos_byte = IT_STRING_BYTEPOS (*it);
5315 string = it->string;
5316 s = SDATA (string) + pos_byte;
5317 it->c = STRING_CHAR (s);
5319 else
5321 pos = IT_CHARPOS (*it);
5322 pos_byte = IT_BYTEPOS (*it);
5323 string = Qnil;
5324 it->c = FETCH_CHAR (pos_byte);
5327 /* If there's a valid composition and point is not inside of the
5328 composition (in the case that the composition is from the current
5329 buffer), draw a glyph composed from the composition components. */
5330 if (find_composition (pos, -1, &start, &end, &prop, string)
5331 && composition_valid_p (start, end, prop)
5332 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5334 if (start < pos)
5335 /* As we can't handle this situation (perhaps font-lock added
5336 a new composition), we just return here hoping that next
5337 redisplay will detect this composition much earlier. */
5338 return HANDLED_NORMALLY;
5339 if (start != pos)
5341 if (STRINGP (it->string))
5342 pos_byte = string_char_to_byte (it->string, start);
5343 else
5344 pos_byte = CHAR_TO_BYTE (start);
5346 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5347 prop, string);
5349 if (it->cmp_it.id >= 0)
5351 it->cmp_it.ch = -1;
5352 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5353 it->cmp_it.nglyphs = -1;
5357 return HANDLED_NORMALLY;
5362 /***********************************************************************
5363 Overlay strings
5364 ***********************************************************************/
5366 /* The following structure is used to record overlay strings for
5367 later sorting in load_overlay_strings. */
5369 struct overlay_entry
5371 Lisp_Object overlay;
5372 Lisp_Object string;
5373 EMACS_INT priority;
5374 int after_string_p;
5378 /* Set up iterator IT from overlay strings at its current position.
5379 Called from handle_stop. */
5381 static enum prop_handled
5382 handle_overlay_change (struct it *it)
5384 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5385 return HANDLED_RECOMPUTE_PROPS;
5386 else
5387 return HANDLED_NORMALLY;
5391 /* Set up the next overlay string for delivery by IT, if there is an
5392 overlay string to deliver. Called by set_iterator_to_next when the
5393 end of the current overlay string is reached. If there are more
5394 overlay strings to display, IT->string and
5395 IT->current.overlay_string_index are set appropriately here.
5396 Otherwise IT->string is set to nil. */
5398 static void
5399 next_overlay_string (struct it *it)
5401 ++it->current.overlay_string_index;
5402 if (it->current.overlay_string_index == it->n_overlay_strings)
5404 /* No more overlay strings. Restore IT's settings to what
5405 they were before overlay strings were processed, and
5406 continue to deliver from current_buffer. */
5408 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5409 pop_it (it);
5410 eassert (it->sp > 0
5411 || (NILP (it->string)
5412 && it->method == GET_FROM_BUFFER
5413 && it->stop_charpos >= BEGV
5414 && it->stop_charpos <= it->end_charpos));
5415 it->current.overlay_string_index = -1;
5416 it->n_overlay_strings = 0;
5417 it->overlay_strings_charpos = -1;
5418 /* If there's an empty display string on the stack, pop the
5419 stack, to resync the bidi iterator with IT's position. Such
5420 empty strings are pushed onto the stack in
5421 get_overlay_strings_1. */
5422 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5423 pop_it (it);
5425 /* If we're at the end of the buffer, record that we have
5426 processed the overlay strings there already, so that
5427 next_element_from_buffer doesn't try it again. */
5428 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5429 it->overlay_strings_at_end_processed_p = 1;
5431 else
5433 /* There are more overlay strings to process. If
5434 IT->current.overlay_string_index has advanced to a position
5435 where we must load IT->overlay_strings with more strings, do
5436 it. We must load at the IT->overlay_strings_charpos where
5437 IT->n_overlay_strings was originally computed; when invisible
5438 text is present, this might not be IT_CHARPOS (Bug#7016). */
5439 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5441 if (it->current.overlay_string_index && i == 0)
5442 load_overlay_strings (it, it->overlay_strings_charpos);
5444 /* Initialize IT to deliver display elements from the overlay
5445 string. */
5446 it->string = it->overlay_strings[i];
5447 it->multibyte_p = STRING_MULTIBYTE (it->string);
5448 SET_TEXT_POS (it->current.string_pos, 0, 0);
5449 it->method = GET_FROM_STRING;
5450 it->stop_charpos = 0;
5451 it->end_charpos = SCHARS (it->string);
5452 if (it->cmp_it.stop_pos >= 0)
5453 it->cmp_it.stop_pos = 0;
5454 it->prev_stop = 0;
5455 it->base_level_stop = 0;
5457 /* Set up the bidi iterator for this overlay string. */
5458 if (it->bidi_p)
5460 it->bidi_it.string.lstring = it->string;
5461 it->bidi_it.string.s = NULL;
5462 it->bidi_it.string.schars = SCHARS (it->string);
5463 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5464 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5465 it->bidi_it.string.unibyte = !it->multibyte_p;
5466 it->bidi_it.w = it->w;
5467 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5471 CHECK_IT (it);
5475 /* Compare two overlay_entry structures E1 and E2. Used as a
5476 comparison function for qsort in load_overlay_strings. Overlay
5477 strings for the same position are sorted so that
5479 1. All after-strings come in front of before-strings, except
5480 when they come from the same overlay.
5482 2. Within after-strings, strings are sorted so that overlay strings
5483 from overlays with higher priorities come first.
5485 2. Within before-strings, strings are sorted so that overlay
5486 strings from overlays with higher priorities come last.
5488 Value is analogous to strcmp. */
5491 static int
5492 compare_overlay_entries (const void *e1, const void *e2)
5494 struct overlay_entry const *entry1 = e1;
5495 struct overlay_entry const *entry2 = e2;
5496 int result;
5498 if (entry1->after_string_p != entry2->after_string_p)
5500 /* Let after-strings appear in front of before-strings if
5501 they come from different overlays. */
5502 if (EQ (entry1->overlay, entry2->overlay))
5503 result = entry1->after_string_p ? 1 : -1;
5504 else
5505 result = entry1->after_string_p ? -1 : 1;
5507 else if (entry1->priority != entry2->priority)
5509 if (entry1->after_string_p)
5510 /* After-strings sorted in order of decreasing priority. */
5511 result = entry2->priority < entry1->priority ? -1 : 1;
5512 else
5513 /* Before-strings sorted in order of increasing priority. */
5514 result = entry1->priority < entry2->priority ? -1 : 1;
5516 else
5517 result = 0;
5519 return result;
5523 /* Load the vector IT->overlay_strings with overlay strings from IT's
5524 current buffer position, or from CHARPOS if that is > 0. Set
5525 IT->n_overlays to the total number of overlay strings found.
5527 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5528 a time. On entry into load_overlay_strings,
5529 IT->current.overlay_string_index gives the number of overlay
5530 strings that have already been loaded by previous calls to this
5531 function.
5533 IT->add_overlay_start contains an additional overlay start
5534 position to consider for taking overlay strings from, if non-zero.
5535 This position comes into play when the overlay has an `invisible'
5536 property, and both before and after-strings. When we've skipped to
5537 the end of the overlay, because of its `invisible' property, we
5538 nevertheless want its before-string to appear.
5539 IT->add_overlay_start will contain the overlay start position
5540 in this case.
5542 Overlay strings are sorted so that after-string strings come in
5543 front of before-string strings. Within before and after-strings,
5544 strings are sorted by overlay priority. See also function
5545 compare_overlay_entries. */
5547 static void
5548 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5550 Lisp_Object overlay, window, str, invisible;
5551 struct Lisp_Overlay *ov;
5552 ptrdiff_t start, end;
5553 ptrdiff_t size = 20;
5554 ptrdiff_t n = 0, i, j;
5555 int invis_p;
5556 struct overlay_entry *entries = alloca (size * sizeof *entries);
5557 USE_SAFE_ALLOCA;
5559 if (charpos <= 0)
5560 charpos = IT_CHARPOS (*it);
5562 /* Append the overlay string STRING of overlay OVERLAY to vector
5563 `entries' which has size `size' and currently contains `n'
5564 elements. AFTER_P non-zero means STRING is an after-string of
5565 OVERLAY. */
5566 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5567 do \
5569 Lisp_Object priority; \
5571 if (n == size) \
5573 struct overlay_entry *old = entries; \
5574 SAFE_NALLOCA (entries, 2, size); \
5575 memcpy (entries, old, size * sizeof *entries); \
5576 size *= 2; \
5579 entries[n].string = (STRING); \
5580 entries[n].overlay = (OVERLAY); \
5581 priority = Foverlay_get ((OVERLAY), Qpriority); \
5582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5583 entries[n].after_string_p = (AFTER_P); \
5584 ++n; \
5586 while (0)
5588 /* Process overlay before the overlay center. */
5589 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5591 XSETMISC (overlay, ov);
5592 eassert (OVERLAYP (overlay));
5593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5596 if (end < charpos)
5597 break;
5599 /* Skip this overlay if it doesn't start or end at IT's current
5600 position. */
5601 if (end != charpos && start != charpos)
5602 continue;
5604 /* Skip this overlay if it doesn't apply to IT->w. */
5605 window = Foverlay_get (overlay, Qwindow);
5606 if (WINDOWP (window) && XWINDOW (window) != it->w)
5607 continue;
5609 /* If the text ``under'' the overlay is invisible, both before-
5610 and after-strings from this overlay are visible; start and
5611 end position are indistinguishable. */
5612 invisible = Foverlay_get (overlay, Qinvisible);
5613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5615 /* If overlay has a non-empty before-string, record it. */
5616 if ((start == charpos || (end == charpos && invis_p))
5617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5618 && SCHARS (str))
5619 RECORD_OVERLAY_STRING (overlay, str, 0);
5621 /* If overlay has a non-empty after-string, record it. */
5622 if ((end == charpos || (start == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 1);
5628 /* Process overlays after the overlay center. */
5629 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5631 XSETMISC (overlay, ov);
5632 eassert (OVERLAYP (overlay));
5633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5636 if (start > charpos)
5637 break;
5639 /* Skip this overlay if it doesn't start or end at IT's current
5640 position. */
5641 if (end != charpos && start != charpos)
5642 continue;
5644 /* Skip this overlay if it doesn't apply to IT->w. */
5645 window = Foverlay_get (overlay, Qwindow);
5646 if (WINDOWP (window) && XWINDOW (window) != it->w)
5647 continue;
5649 /* If the text ``under'' the overlay is invisible, it has a zero
5650 dimension, and both before- and after-strings apply. */
5651 invisible = Foverlay_get (overlay, Qinvisible);
5652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5654 /* If overlay has a non-empty before-string, record it. */
5655 if ((start == charpos || (end == charpos && invis_p))
5656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5657 && SCHARS (str))
5658 RECORD_OVERLAY_STRING (overlay, str, 0);
5660 /* If overlay has a non-empty after-string, record it. */
5661 if ((end == charpos || (start == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 1);
5667 #undef RECORD_OVERLAY_STRING
5669 /* Sort entries. */
5670 if (n > 1)
5671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5673 /* Record number of overlay strings, and where we computed it. */
5674 it->n_overlay_strings = n;
5675 it->overlay_strings_charpos = charpos;
5677 /* IT->current.overlay_string_index is the number of overlay strings
5678 that have already been consumed by IT. Copy some of the
5679 remaining overlay strings to IT->overlay_strings. */
5680 i = 0;
5681 j = it->current.overlay_string_index;
5682 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5684 it->overlay_strings[i] = entries[j].string;
5685 it->string_overlays[i++] = entries[j++].overlay;
5688 CHECK_IT (it);
5689 SAFE_FREE ();
5693 /* Get the first chunk of overlay strings at IT's current buffer
5694 position, or at CHARPOS if that is > 0. Value is non-zero if at
5695 least one overlay string was found. */
5697 static int
5698 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5700 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5701 process. This fills IT->overlay_strings with strings, and sets
5702 IT->n_overlay_strings to the total number of strings to process.
5703 IT->pos.overlay_string_index has to be set temporarily to zero
5704 because load_overlay_strings needs this; it must be set to -1
5705 when no overlay strings are found because a zero value would
5706 indicate a position in the first overlay string. */
5707 it->current.overlay_string_index = 0;
5708 load_overlay_strings (it, charpos);
5710 /* If we found overlay strings, set up IT to deliver display
5711 elements from the first one. Otherwise set up IT to deliver
5712 from current_buffer. */
5713 if (it->n_overlay_strings)
5715 /* Make sure we know settings in current_buffer, so that we can
5716 restore meaningful values when we're done with the overlay
5717 strings. */
5718 if (compute_stop_p)
5719 compute_stop_pos (it);
5720 eassert (it->face_id >= 0);
5722 /* Save IT's settings. They are restored after all overlay
5723 strings have been processed. */
5724 eassert (!compute_stop_p || it->sp == 0);
5726 /* When called from handle_stop, there might be an empty display
5727 string loaded. In that case, don't bother saving it. But
5728 don't use this optimization with the bidi iterator, since we
5729 need the corresponding pop_it call to resync the bidi
5730 iterator's position with IT's position, after we are done
5731 with the overlay strings. (The corresponding call to pop_it
5732 in case of an empty display string is in
5733 next_overlay_string.) */
5734 if (!(!it->bidi_p
5735 && STRINGP (it->string) && !SCHARS (it->string)))
5736 push_it (it, NULL);
5738 /* Set up IT to deliver display elements from the first overlay
5739 string. */
5740 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5741 it->string = it->overlay_strings[0];
5742 it->from_overlay = Qnil;
5743 it->stop_charpos = 0;
5744 eassert (STRINGP (it->string));
5745 it->end_charpos = SCHARS (it->string);
5746 it->prev_stop = 0;
5747 it->base_level_stop = 0;
5748 it->multibyte_p = STRING_MULTIBYTE (it->string);
5749 it->method = GET_FROM_STRING;
5750 it->from_disp_prop_p = 0;
5752 /* Force paragraph direction to be that of the parent
5753 buffer. */
5754 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5755 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5756 else
5757 it->paragraph_embedding = L2R;
5759 /* Set up the bidi iterator for this overlay string. */
5760 if (it->bidi_p)
5762 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5764 it->bidi_it.string.lstring = it->string;
5765 it->bidi_it.string.s = NULL;
5766 it->bidi_it.string.schars = SCHARS (it->string);
5767 it->bidi_it.string.bufpos = pos;
5768 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5769 it->bidi_it.string.unibyte = !it->multibyte_p;
5770 it->bidi_it.w = it->w;
5771 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5773 return 1;
5776 it->current.overlay_string_index = -1;
5777 return 0;
5780 static int
5781 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5783 it->string = Qnil;
5784 it->method = GET_FROM_BUFFER;
5786 (void) get_overlay_strings_1 (it, charpos, 1);
5788 CHECK_IT (it);
5790 /* Value is non-zero if we found at least one overlay string. */
5791 return STRINGP (it->string);
5796 /***********************************************************************
5797 Saving and restoring state
5798 ***********************************************************************/
5800 /* Save current settings of IT on IT->stack. Called, for example,
5801 before setting up IT for an overlay string, to be able to restore
5802 IT's settings to what they were after the overlay string has been
5803 processed. If POSITION is non-NULL, it is the position to save on
5804 the stack instead of IT->position. */
5806 static void
5807 push_it (struct it *it, struct text_pos *position)
5809 struct iterator_stack_entry *p;
5811 eassert (it->sp < IT_STACK_SIZE);
5812 p = it->stack + it->sp;
5814 p->stop_charpos = it->stop_charpos;
5815 p->prev_stop = it->prev_stop;
5816 p->base_level_stop = it->base_level_stop;
5817 p->cmp_it = it->cmp_it;
5818 eassert (it->face_id >= 0);
5819 p->face_id = it->face_id;
5820 p->string = it->string;
5821 p->method = it->method;
5822 p->from_overlay = it->from_overlay;
5823 switch (p->method)
5825 case GET_FROM_IMAGE:
5826 p->u.image.object = it->object;
5827 p->u.image.image_id = it->image_id;
5828 p->u.image.slice = it->slice;
5829 break;
5830 case GET_FROM_STRETCH:
5831 p->u.stretch.object = it->object;
5832 break;
5834 p->position = position ? *position : it->position;
5835 p->current = it->current;
5836 p->end_charpos = it->end_charpos;
5837 p->string_nchars = it->string_nchars;
5838 p->area = it->area;
5839 p->multibyte_p = it->multibyte_p;
5840 p->avoid_cursor_p = it->avoid_cursor_p;
5841 p->space_width = it->space_width;
5842 p->font_height = it->font_height;
5843 p->voffset = it->voffset;
5844 p->string_from_display_prop_p = it->string_from_display_prop_p;
5845 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5846 p->display_ellipsis_p = 0;
5847 p->line_wrap = it->line_wrap;
5848 p->bidi_p = it->bidi_p;
5849 p->paragraph_embedding = it->paragraph_embedding;
5850 p->from_disp_prop_p = it->from_disp_prop_p;
5851 ++it->sp;
5853 /* Save the state of the bidi iterator as well. */
5854 if (it->bidi_p)
5855 bidi_push_it (&it->bidi_it);
5858 static void
5859 iterate_out_of_display_property (struct it *it)
5861 int buffer_p = !STRINGP (it->string);
5862 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5863 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5865 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5867 /* Maybe initialize paragraph direction. If we are at the beginning
5868 of a new paragraph, next_element_from_buffer may not have a
5869 chance to do that. */
5870 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5871 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5872 /* prev_stop can be zero, so check against BEGV as well. */
5873 while (it->bidi_it.charpos >= bob
5874 && it->prev_stop <= it->bidi_it.charpos
5875 && it->bidi_it.charpos < CHARPOS (it->position)
5876 && it->bidi_it.charpos < eob)
5877 bidi_move_to_visually_next (&it->bidi_it);
5878 /* Record the stop_pos we just crossed, for when we cross it
5879 back, maybe. */
5880 if (it->bidi_it.charpos > CHARPOS (it->position))
5881 it->prev_stop = CHARPOS (it->position);
5882 /* If we ended up not where pop_it put us, resync IT's
5883 positional members with the bidi iterator. */
5884 if (it->bidi_it.charpos != CHARPOS (it->position))
5885 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5886 if (buffer_p)
5887 it->current.pos = it->position;
5888 else
5889 it->current.string_pos = it->position;
5892 /* Restore IT's settings from IT->stack. Called, for example, when no
5893 more overlay strings must be processed, and we return to delivering
5894 display elements from a buffer, or when the end of a string from a
5895 `display' property is reached and we return to delivering display
5896 elements from an overlay string, or from a buffer. */
5898 static void
5899 pop_it (struct it *it)
5901 struct iterator_stack_entry *p;
5902 int from_display_prop = it->from_disp_prop_p;
5904 eassert (it->sp > 0);
5905 --it->sp;
5906 p = it->stack + it->sp;
5907 it->stop_charpos = p->stop_charpos;
5908 it->prev_stop = p->prev_stop;
5909 it->base_level_stop = p->base_level_stop;
5910 it->cmp_it = p->cmp_it;
5911 it->face_id = p->face_id;
5912 it->current = p->current;
5913 it->position = p->position;
5914 it->string = p->string;
5915 it->from_overlay = p->from_overlay;
5916 if (NILP (it->string))
5917 SET_TEXT_POS (it->current.string_pos, -1, -1);
5918 it->method = p->method;
5919 switch (it->method)
5921 case GET_FROM_IMAGE:
5922 it->image_id = p->u.image.image_id;
5923 it->object = p->u.image.object;
5924 it->slice = p->u.image.slice;
5925 break;
5926 case GET_FROM_STRETCH:
5927 it->object = p->u.stretch.object;
5928 break;
5929 case GET_FROM_BUFFER:
5930 it->object = it->w->contents;
5931 break;
5932 case GET_FROM_STRING:
5933 it->object = it->string;
5934 break;
5935 case GET_FROM_DISPLAY_VECTOR:
5936 if (it->s)
5937 it->method = GET_FROM_C_STRING;
5938 else if (STRINGP (it->string))
5939 it->method = GET_FROM_STRING;
5940 else
5942 it->method = GET_FROM_BUFFER;
5943 it->object = it->w->contents;
5946 it->end_charpos = p->end_charpos;
5947 it->string_nchars = p->string_nchars;
5948 it->area = p->area;
5949 it->multibyte_p = p->multibyte_p;
5950 it->avoid_cursor_p = p->avoid_cursor_p;
5951 it->space_width = p->space_width;
5952 it->font_height = p->font_height;
5953 it->voffset = p->voffset;
5954 it->string_from_display_prop_p = p->string_from_display_prop_p;
5955 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5956 it->line_wrap = p->line_wrap;
5957 it->bidi_p = p->bidi_p;
5958 it->paragraph_embedding = p->paragraph_embedding;
5959 it->from_disp_prop_p = p->from_disp_prop_p;
5960 if (it->bidi_p)
5962 bidi_pop_it (&it->bidi_it);
5963 /* Bidi-iterate until we get out of the portion of text, if any,
5964 covered by a `display' text property or by an overlay with
5965 `display' property. (We cannot just jump there, because the
5966 internal coherency of the bidi iterator state can not be
5967 preserved across such jumps.) We also must determine the
5968 paragraph base direction if the overlay we just processed is
5969 at the beginning of a new paragraph. */
5970 if (from_display_prop
5971 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5972 iterate_out_of_display_property (it);
5974 eassert ((BUFFERP (it->object)
5975 && IT_CHARPOS (*it) == it->bidi_it.charpos
5976 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5977 || (STRINGP (it->object)
5978 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5979 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5980 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5986 /***********************************************************************
5987 Moving over lines
5988 ***********************************************************************/
5990 /* Set IT's current position to the previous line start. */
5992 static void
5993 back_to_previous_line_start (struct it *it)
5995 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5997 DEC_BOTH (cp, bp);
5998 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6002 /* Move IT to the next line start.
6004 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6005 we skipped over part of the text (as opposed to moving the iterator
6006 continuously over the text). Otherwise, don't change the value
6007 of *SKIPPED_P.
6009 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6010 iterator on the newline, if it was found.
6012 Newlines may come from buffer text, overlay strings, or strings
6013 displayed via the `display' property. That's the reason we can't
6014 simply use find_newline_no_quit.
6016 Note that this function may not skip over invisible text that is so
6017 because of text properties and immediately follows a newline. If
6018 it would, function reseat_at_next_visible_line_start, when called
6019 from set_iterator_to_next, would effectively make invisible
6020 characters following a newline part of the wrong glyph row, which
6021 leads to wrong cursor motion. */
6023 static int
6024 forward_to_next_line_start (struct it *it, int *skipped_p,
6025 struct bidi_it *bidi_it_prev)
6027 ptrdiff_t old_selective;
6028 int newline_found_p, n;
6029 const int MAX_NEWLINE_DISTANCE = 500;
6031 /* If already on a newline, just consume it to avoid unintended
6032 skipping over invisible text below. */
6033 if (it->what == IT_CHARACTER
6034 && it->c == '\n'
6035 && CHARPOS (it->position) == IT_CHARPOS (*it))
6037 if (it->bidi_p && bidi_it_prev)
6038 *bidi_it_prev = it->bidi_it;
6039 set_iterator_to_next (it, 0);
6040 it->c = 0;
6041 return 1;
6044 /* Don't handle selective display in the following. It's (a)
6045 unnecessary because it's done by the caller, and (b) leads to an
6046 infinite recursion because next_element_from_ellipsis indirectly
6047 calls this function. */
6048 old_selective = it->selective;
6049 it->selective = 0;
6051 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6052 from buffer text. */
6053 for (n = newline_found_p = 0;
6054 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6055 n += STRINGP (it->string) ? 0 : 1)
6057 if (!get_next_display_element (it))
6058 return 0;
6059 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6060 if (newline_found_p && it->bidi_p && bidi_it_prev)
6061 *bidi_it_prev = it->bidi_it;
6062 set_iterator_to_next (it, 0);
6065 /* If we didn't find a newline near enough, see if we can use a
6066 short-cut. */
6067 if (!newline_found_p)
6069 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6070 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6071 1, &bytepos);
6072 Lisp_Object pos;
6074 eassert (!STRINGP (it->string));
6076 /* If there isn't any `display' property in sight, and no
6077 overlays, we can just use the position of the newline in
6078 buffer text. */
6079 if (it->stop_charpos >= limit
6080 || ((pos = Fnext_single_property_change (make_number (start),
6081 Qdisplay, Qnil,
6082 make_number (limit)),
6083 NILP (pos))
6084 && next_overlay_change (start) == ZV))
6086 if (!it->bidi_p)
6088 IT_CHARPOS (*it) = limit;
6089 IT_BYTEPOS (*it) = bytepos;
6091 else
6093 struct bidi_it bprev;
6095 /* Help bidi.c avoid expensive searches for display
6096 properties and overlays, by telling it that there are
6097 none up to `limit'. */
6098 if (it->bidi_it.disp_pos < limit)
6100 it->bidi_it.disp_pos = limit;
6101 it->bidi_it.disp_prop = 0;
6103 do {
6104 bprev = it->bidi_it;
6105 bidi_move_to_visually_next (&it->bidi_it);
6106 } while (it->bidi_it.charpos != limit);
6107 IT_CHARPOS (*it) = limit;
6108 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6109 if (bidi_it_prev)
6110 *bidi_it_prev = bprev;
6112 *skipped_p = newline_found_p = 1;
6114 else
6116 while (get_next_display_element (it)
6117 && !newline_found_p)
6119 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6120 if (newline_found_p && it->bidi_p && bidi_it_prev)
6121 *bidi_it_prev = it->bidi_it;
6122 set_iterator_to_next (it, 0);
6127 it->selective = old_selective;
6128 return newline_found_p;
6132 /* Set IT's current position to the previous visible line start. Skip
6133 invisible text that is so either due to text properties or due to
6134 selective display. Caution: this does not change IT->current_x and
6135 IT->hpos. */
6137 static void
6138 back_to_previous_visible_line_start (struct it *it)
6140 while (IT_CHARPOS (*it) > BEGV)
6142 back_to_previous_line_start (it);
6144 if (IT_CHARPOS (*it) <= BEGV)
6145 break;
6147 /* If selective > 0, then lines indented more than its value are
6148 invisible. */
6149 if (it->selective > 0
6150 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6151 it->selective))
6152 continue;
6154 /* Check the newline before point for invisibility. */
6156 Lisp_Object prop;
6157 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6158 Qinvisible, it->window);
6159 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6160 continue;
6163 if (IT_CHARPOS (*it) <= BEGV)
6164 break;
6167 struct it it2;
6168 void *it2data = NULL;
6169 ptrdiff_t pos;
6170 ptrdiff_t beg, end;
6171 Lisp_Object val, overlay;
6173 SAVE_IT (it2, *it, it2data);
6175 /* If newline is part of a composition, continue from start of composition */
6176 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6177 && beg < IT_CHARPOS (*it))
6178 goto replaced;
6180 /* If newline is replaced by a display property, find start of overlay
6181 or interval and continue search from that point. */
6182 pos = --IT_CHARPOS (it2);
6183 --IT_BYTEPOS (it2);
6184 it2.sp = 0;
6185 bidi_unshelve_cache (NULL, 0);
6186 it2.string_from_display_prop_p = 0;
6187 it2.from_disp_prop_p = 0;
6188 if (handle_display_prop (&it2) == HANDLED_RETURN
6189 && !NILP (val = get_char_property_and_overlay
6190 (make_number (pos), Qdisplay, Qnil, &overlay))
6191 && (OVERLAYP (overlay)
6192 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6193 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6195 RESTORE_IT (it, it, it2data);
6196 goto replaced;
6199 /* Newline is not replaced by anything -- so we are done. */
6200 RESTORE_IT (it, it, it2data);
6201 break;
6203 replaced:
6204 if (beg < BEGV)
6205 beg = BEGV;
6206 IT_CHARPOS (*it) = beg;
6207 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6211 it->continuation_lines_width = 0;
6213 eassert (IT_CHARPOS (*it) >= BEGV);
6214 eassert (IT_CHARPOS (*it) == BEGV
6215 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6216 CHECK_IT (it);
6220 /* Reseat iterator IT at the previous visible line start. Skip
6221 invisible text that is so either due to text properties or due to
6222 selective display. At the end, update IT's overlay information,
6223 face information etc. */
6225 void
6226 reseat_at_previous_visible_line_start (struct it *it)
6228 back_to_previous_visible_line_start (it);
6229 reseat (it, it->current.pos, 1);
6230 CHECK_IT (it);
6234 /* Reseat iterator IT on the next visible line start in the current
6235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6236 preceding the line start. Skip over invisible text that is so
6237 because of selective display. Compute faces, overlays etc at the
6238 new position. Note that this function does not skip over text that
6239 is invisible because of text properties. */
6241 static void
6242 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6244 int newline_found_p, skipped_p = 0;
6245 struct bidi_it bidi_it_prev;
6247 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6249 /* Skip over lines that are invisible because they are indented
6250 more than the value of IT->selective. */
6251 if (it->selective > 0)
6252 while (IT_CHARPOS (*it) < ZV
6253 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6254 it->selective))
6256 eassert (IT_BYTEPOS (*it) == BEGV
6257 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6258 newline_found_p =
6259 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6262 /* Position on the newline if that's what's requested. */
6263 if (on_newline_p && newline_found_p)
6265 if (STRINGP (it->string))
6267 if (IT_STRING_CHARPOS (*it) > 0)
6269 if (!it->bidi_p)
6271 --IT_STRING_CHARPOS (*it);
6272 --IT_STRING_BYTEPOS (*it);
6274 else
6276 /* We need to restore the bidi iterator to the state
6277 it had on the newline, and resync the IT's
6278 position with that. */
6279 it->bidi_it = bidi_it_prev;
6280 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6285 else if (IT_CHARPOS (*it) > BEGV)
6287 if (!it->bidi_p)
6289 --IT_CHARPOS (*it);
6290 --IT_BYTEPOS (*it);
6292 else
6294 /* We need to restore the bidi iterator to the state it
6295 had on the newline and resync IT with that. */
6296 it->bidi_it = bidi_it_prev;
6297 IT_CHARPOS (*it) = it->bidi_it.charpos;
6298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 reseat (it, it->current.pos, 0);
6303 else if (skipped_p)
6304 reseat (it, it->current.pos, 0);
6306 CHECK_IT (it);
6311 /***********************************************************************
6312 Changing an iterator's position
6313 ***********************************************************************/
6315 /* Change IT's current position to POS in current_buffer. If FORCE_P
6316 is non-zero, always check for text properties at the new position.
6317 Otherwise, text properties are only looked up if POS >=
6318 IT->check_charpos of a property. */
6320 static void
6321 reseat (struct it *it, struct text_pos pos, int force_p)
6323 ptrdiff_t original_pos = IT_CHARPOS (*it);
6325 reseat_1 (it, pos, 0);
6327 /* Determine where to check text properties. Avoid doing it
6328 where possible because text property lookup is very expensive. */
6329 if (force_p
6330 || CHARPOS (pos) > it->stop_charpos
6331 || CHARPOS (pos) < original_pos)
6333 if (it->bidi_p)
6335 /* For bidi iteration, we need to prime prev_stop and
6336 base_level_stop with our best estimations. */
6337 /* Implementation note: Of course, POS is not necessarily a
6338 stop position, so assigning prev_pos to it is a lie; we
6339 should have called compute_stop_backwards. However, if
6340 the current buffer does not include any R2L characters,
6341 that call would be a waste of cycles, because the
6342 iterator will never move back, and thus never cross this
6343 "fake" stop position. So we delay that backward search
6344 until the time we really need it, in next_element_from_buffer. */
6345 if (CHARPOS (pos) != it->prev_stop)
6346 it->prev_stop = CHARPOS (pos);
6347 if (CHARPOS (pos) < it->base_level_stop)
6348 it->base_level_stop = 0; /* meaning it's unknown */
6349 handle_stop (it);
6351 else
6353 handle_stop (it);
6354 it->prev_stop = it->base_level_stop = 0;
6359 CHECK_IT (it);
6363 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6364 IT->stop_pos to POS, also. */
6366 static void
6367 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6369 /* Don't call this function when scanning a C string. */
6370 eassert (it->s == NULL);
6372 /* POS must be a reasonable value. */
6373 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6375 it->current.pos = it->position = pos;
6376 it->end_charpos = ZV;
6377 it->dpvec = NULL;
6378 it->current.dpvec_index = -1;
6379 it->current.overlay_string_index = -1;
6380 IT_STRING_CHARPOS (*it) = -1;
6381 IT_STRING_BYTEPOS (*it) = -1;
6382 it->string = Qnil;
6383 it->method = GET_FROM_BUFFER;
6384 it->object = it->w->contents;
6385 it->area = TEXT_AREA;
6386 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6387 it->sp = 0;
6388 it->string_from_display_prop_p = 0;
6389 it->string_from_prefix_prop_p = 0;
6391 it->from_disp_prop_p = 0;
6392 it->face_before_selective_p = 0;
6393 if (it->bidi_p)
6395 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6396 &it->bidi_it);
6397 bidi_unshelve_cache (NULL, 0);
6398 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6399 it->bidi_it.string.s = NULL;
6400 it->bidi_it.string.lstring = Qnil;
6401 it->bidi_it.string.bufpos = 0;
6402 it->bidi_it.string.unibyte = 0;
6403 it->bidi_it.w = it->w;
6406 if (set_stop_p)
6408 it->stop_charpos = CHARPOS (pos);
6409 it->base_level_stop = CHARPOS (pos);
6411 /* This make the information stored in it->cmp_it invalidate. */
6412 it->cmp_it.id = -1;
6416 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6417 If S is non-null, it is a C string to iterate over. Otherwise,
6418 STRING gives a Lisp string to iterate over.
6420 If PRECISION > 0, don't return more then PRECISION number of
6421 characters from the string.
6423 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6424 characters have been returned. FIELD_WIDTH < 0 means an infinite
6425 field width.
6427 MULTIBYTE = 0 means disable processing of multibyte characters,
6428 MULTIBYTE > 0 means enable it,
6429 MULTIBYTE < 0 means use IT->multibyte_p.
6431 IT must be initialized via a prior call to init_iterator before
6432 calling this function. */
6434 static void
6435 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6436 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6437 int multibyte)
6439 /* No text property checks performed by default, but see below. */
6440 it->stop_charpos = -1;
6442 /* Set iterator position and end position. */
6443 memset (&it->current, 0, sizeof it->current);
6444 it->current.overlay_string_index = -1;
6445 it->current.dpvec_index = -1;
6446 eassert (charpos >= 0);
6448 /* If STRING is specified, use its multibyteness, otherwise use the
6449 setting of MULTIBYTE, if specified. */
6450 if (multibyte >= 0)
6451 it->multibyte_p = multibyte > 0;
6453 /* Bidirectional reordering of strings is controlled by the default
6454 value of bidi-display-reordering. Don't try to reorder while
6455 loading loadup.el, as the necessary character property tables are
6456 not yet available. */
6457 it->bidi_p =
6458 NILP (Vpurify_flag)
6459 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6461 if (s == NULL)
6463 eassert (STRINGP (string));
6464 it->string = string;
6465 it->s = NULL;
6466 it->end_charpos = it->string_nchars = SCHARS (string);
6467 it->method = GET_FROM_STRING;
6468 it->current.string_pos = string_pos (charpos, string);
6470 if (it->bidi_p)
6472 it->bidi_it.string.lstring = string;
6473 it->bidi_it.string.s = NULL;
6474 it->bidi_it.string.schars = it->end_charpos;
6475 it->bidi_it.string.bufpos = 0;
6476 it->bidi_it.string.from_disp_str = 0;
6477 it->bidi_it.string.unibyte = !it->multibyte_p;
6478 it->bidi_it.w = it->w;
6479 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6480 FRAME_WINDOW_P (it->f), &it->bidi_it);
6483 else
6485 it->s = (const unsigned char *) s;
6486 it->string = Qnil;
6488 /* Note that we use IT->current.pos, not it->current.string_pos,
6489 for displaying C strings. */
6490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6491 if (it->multibyte_p)
6493 it->current.pos = c_string_pos (charpos, s, 1);
6494 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6496 else
6498 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6499 it->end_charpos = it->string_nchars = strlen (s);
6502 if (it->bidi_p)
6504 it->bidi_it.string.lstring = Qnil;
6505 it->bidi_it.string.s = (const unsigned char *) s;
6506 it->bidi_it.string.schars = it->end_charpos;
6507 it->bidi_it.string.bufpos = 0;
6508 it->bidi_it.string.from_disp_str = 0;
6509 it->bidi_it.string.unibyte = !it->multibyte_p;
6510 it->bidi_it.w = it->w;
6511 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6512 &it->bidi_it);
6514 it->method = GET_FROM_C_STRING;
6517 /* PRECISION > 0 means don't return more than PRECISION characters
6518 from the string. */
6519 if (precision > 0 && it->end_charpos - charpos > precision)
6521 it->end_charpos = it->string_nchars = charpos + precision;
6522 if (it->bidi_p)
6523 it->bidi_it.string.schars = it->end_charpos;
6526 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6527 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6528 FIELD_WIDTH < 0 means infinite field width. This is useful for
6529 padding with `-' at the end of a mode line. */
6530 if (field_width < 0)
6531 field_width = INFINITY;
6532 /* Implementation note: We deliberately don't enlarge
6533 it->bidi_it.string.schars here to fit it->end_charpos, because
6534 the bidi iterator cannot produce characters out of thin air. */
6535 if (field_width > it->end_charpos - charpos)
6536 it->end_charpos = charpos + field_width;
6538 /* Use the standard display table for displaying strings. */
6539 if (DISP_TABLE_P (Vstandard_display_table))
6540 it->dp = XCHAR_TABLE (Vstandard_display_table);
6542 it->stop_charpos = charpos;
6543 it->prev_stop = charpos;
6544 it->base_level_stop = 0;
6545 if (it->bidi_p)
6547 it->bidi_it.first_elt = 1;
6548 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6549 it->bidi_it.disp_pos = -1;
6551 if (s == NULL && it->multibyte_p)
6553 ptrdiff_t endpos = SCHARS (it->string);
6554 if (endpos > it->end_charpos)
6555 endpos = it->end_charpos;
6556 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6557 it->string);
6559 CHECK_IT (it);
6564 /***********************************************************************
6565 Iteration
6566 ***********************************************************************/
6568 /* Map enum it_method value to corresponding next_element_from_* function. */
6570 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6572 next_element_from_buffer,
6573 next_element_from_display_vector,
6574 next_element_from_string,
6575 next_element_from_c_string,
6576 next_element_from_image,
6577 next_element_from_stretch
6580 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6583 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6584 (possibly with the following characters). */
6586 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6587 ((IT)->cmp_it.id >= 0 \
6588 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6589 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6590 END_CHARPOS, (IT)->w, \
6591 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6592 (IT)->string)))
6595 /* Lookup the char-table Vglyphless_char_display for character C (-1
6596 if we want information for no-font case), and return the display
6597 method symbol. By side-effect, update it->what and
6598 it->glyphless_method. This function is called from
6599 get_next_display_element for each character element, and from
6600 x_produce_glyphs when no suitable font was found. */
6602 Lisp_Object
6603 lookup_glyphless_char_display (int c, struct it *it)
6605 Lisp_Object glyphless_method = Qnil;
6607 if (CHAR_TABLE_P (Vglyphless_char_display)
6608 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6610 if (c >= 0)
6612 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6613 if (CONSP (glyphless_method))
6614 glyphless_method = FRAME_WINDOW_P (it->f)
6615 ? XCAR (glyphless_method)
6616 : XCDR (glyphless_method);
6618 else
6619 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6622 retry:
6623 if (NILP (glyphless_method))
6625 if (c >= 0)
6626 /* The default is to display the character by a proper font. */
6627 return Qnil;
6628 /* The default for the no-font case is to display an empty box. */
6629 glyphless_method = Qempty_box;
6631 if (EQ (glyphless_method, Qzero_width))
6633 if (c >= 0)
6634 return glyphless_method;
6635 /* This method can't be used for the no-font case. */
6636 glyphless_method = Qempty_box;
6638 if (EQ (glyphless_method, Qthin_space))
6639 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6640 else if (EQ (glyphless_method, Qempty_box))
6641 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6642 else if (EQ (glyphless_method, Qhex_code))
6643 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6644 else if (STRINGP (glyphless_method))
6645 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6646 else
6648 /* Invalid value. We use the default method. */
6649 glyphless_method = Qnil;
6650 goto retry;
6652 it->what = IT_GLYPHLESS;
6653 return glyphless_method;
6656 /* Merge escape glyph face and cache the result. */
6658 static struct frame *last_escape_glyph_frame = NULL;
6659 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6660 static int last_escape_glyph_merged_face_id = 0;
6662 static int
6663 merge_escape_glyph_face (struct it *it)
6665 int face_id;
6667 if (it->f == last_escape_glyph_frame
6668 && it->face_id == last_escape_glyph_face_id)
6669 face_id = last_escape_glyph_merged_face_id;
6670 else
6672 /* Merge the `escape-glyph' face into the current face. */
6673 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6674 last_escape_glyph_frame = it->f;
6675 last_escape_glyph_face_id = it->face_id;
6676 last_escape_glyph_merged_face_id = face_id;
6678 return face_id;
6681 /* Likewise for glyphless glyph face. */
6683 static struct frame *last_glyphless_glyph_frame = NULL;
6684 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6685 static int last_glyphless_glyph_merged_face_id = 0;
6688 merge_glyphless_glyph_face (struct it *it)
6690 int face_id;
6692 if (it->f == last_glyphless_glyph_frame
6693 && it->face_id == last_glyphless_glyph_face_id)
6694 face_id = last_glyphless_glyph_merged_face_id;
6695 else
6697 /* Merge the `glyphless-char' face into the current face. */
6698 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6699 last_glyphless_glyph_frame = it->f;
6700 last_glyphless_glyph_face_id = it->face_id;
6701 last_glyphless_glyph_merged_face_id = face_id;
6703 return face_id;
6706 /* Load IT's display element fields with information about the next
6707 display element from the current position of IT. Value is zero if
6708 end of buffer (or C string) is reached. */
6710 static int
6711 get_next_display_element (struct it *it)
6713 /* Non-zero means that we found a display element. Zero means that
6714 we hit the end of what we iterate over. Performance note: the
6715 function pointer `method' used here turns out to be faster than
6716 using a sequence of if-statements. */
6717 int success_p;
6719 get_next:
6720 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6722 if (it->what == IT_CHARACTER)
6724 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6725 and only if (a) the resolved directionality of that character
6726 is R..." */
6727 /* FIXME: Do we need an exception for characters from display
6728 tables? */
6729 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6730 it->c = bidi_mirror_char (it->c);
6731 /* Map via display table or translate control characters.
6732 IT->c, IT->len etc. have been set to the next character by
6733 the function call above. If we have a display table, and it
6734 contains an entry for IT->c, translate it. Don't do this if
6735 IT->c itself comes from a display table, otherwise we could
6736 end up in an infinite recursion. (An alternative could be to
6737 count the recursion depth of this function and signal an
6738 error when a certain maximum depth is reached.) Is it worth
6739 it? */
6740 if (success_p && it->dpvec == NULL)
6742 Lisp_Object dv;
6743 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6744 int nonascii_space_p = 0;
6745 int nonascii_hyphen_p = 0;
6746 int c = it->c; /* This is the character to display. */
6748 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6750 eassert (SINGLE_BYTE_CHAR_P (c));
6751 if (unibyte_display_via_language_environment)
6753 c = DECODE_CHAR (unibyte, c);
6754 if (c < 0)
6755 c = BYTE8_TO_CHAR (it->c);
6757 else
6758 c = BYTE8_TO_CHAR (it->c);
6761 if (it->dp
6762 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6763 VECTORP (dv)))
6765 struct Lisp_Vector *v = XVECTOR (dv);
6767 /* Return the first character from the display table
6768 entry, if not empty. If empty, don't display the
6769 current character. */
6770 if (v->header.size)
6772 it->dpvec_char_len = it->len;
6773 it->dpvec = v->contents;
6774 it->dpend = v->contents + v->header.size;
6775 it->current.dpvec_index = 0;
6776 it->dpvec_face_id = -1;
6777 it->saved_face_id = it->face_id;
6778 it->method = GET_FROM_DISPLAY_VECTOR;
6779 it->ellipsis_p = 0;
6781 else
6783 set_iterator_to_next (it, 0);
6785 goto get_next;
6788 if (! NILP (lookup_glyphless_char_display (c, it)))
6790 if (it->what == IT_GLYPHLESS)
6791 goto done;
6792 /* Don't display this character. */
6793 set_iterator_to_next (it, 0);
6794 goto get_next;
6797 /* If `nobreak-char-display' is non-nil, we display
6798 non-ASCII spaces and hyphens specially. */
6799 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6801 if (c == 0xA0)
6802 nonascii_space_p = 1;
6803 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6804 nonascii_hyphen_p = 1;
6807 /* Translate control characters into `\003' or `^C' form.
6808 Control characters coming from a display table entry are
6809 currently not translated because we use IT->dpvec to hold
6810 the translation. This could easily be changed but I
6811 don't believe that it is worth doing.
6813 The characters handled by `nobreak-char-display' must be
6814 translated too.
6816 Non-printable characters and raw-byte characters are also
6817 translated to octal form. */
6818 if (((c < ' ' || c == 127) /* ASCII control chars. */
6819 ? (it->area != TEXT_AREA
6820 /* In mode line, treat \n, \t like other crl chars. */
6821 || (c != '\t'
6822 && it->glyph_row
6823 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6824 || (c != '\n' && c != '\t'))
6825 : (nonascii_space_p
6826 || nonascii_hyphen_p
6827 || CHAR_BYTE8_P (c)
6828 || ! CHAR_PRINTABLE_P (c))))
6830 /* C is a control character, non-ASCII space/hyphen,
6831 raw-byte, or a non-printable character which must be
6832 displayed either as '\003' or as `^C' where the '\\'
6833 and '^' can be defined in the display table. Fill
6834 IT->ctl_chars with glyphs for what we have to
6835 display. Then, set IT->dpvec to these glyphs. */
6836 Lisp_Object gc;
6837 int ctl_len;
6838 int face_id;
6839 int lface_id = 0;
6840 int escape_glyph;
6842 /* Handle control characters with ^. */
6844 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6846 int g;
6848 g = '^'; /* default glyph for Control */
6849 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6850 if (it->dp
6851 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6853 g = GLYPH_CODE_CHAR (gc);
6854 lface_id = GLYPH_CODE_FACE (gc);
6857 face_id = (lface_id
6858 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6859 : merge_escape_glyph_face (it));
6861 XSETINT (it->ctl_chars[0], g);
6862 XSETINT (it->ctl_chars[1], c ^ 0100);
6863 ctl_len = 2;
6864 goto display_control;
6867 /* Handle non-ascii space in the mode where it only gets
6868 highlighting. */
6870 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6872 /* Merge `nobreak-space' into the current face. */
6873 face_id = merge_faces (it->f, Qnobreak_space, 0,
6874 it->face_id);
6875 XSETINT (it->ctl_chars[0], ' ');
6876 ctl_len = 1;
6877 goto display_control;
6880 /* Handle sequences that start with the "escape glyph". */
6882 /* the default escape glyph is \. */
6883 escape_glyph = '\\';
6885 if (it->dp
6886 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6888 escape_glyph = GLYPH_CODE_CHAR (gc);
6889 lface_id = GLYPH_CODE_FACE (gc);
6892 face_id = (lface_id
6893 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6894 : merge_escape_glyph_face (it));
6896 /* Draw non-ASCII hyphen with just highlighting: */
6898 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6900 XSETINT (it->ctl_chars[0], '-');
6901 ctl_len = 1;
6902 goto display_control;
6905 /* Draw non-ASCII space/hyphen with escape glyph: */
6907 if (nonascii_space_p || nonascii_hyphen_p)
6909 XSETINT (it->ctl_chars[0], escape_glyph);
6910 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6911 ctl_len = 2;
6912 goto display_control;
6916 char str[10];
6917 int len, i;
6919 if (CHAR_BYTE8_P (c))
6920 /* Display \200 instead of \17777600. */
6921 c = CHAR_TO_BYTE8 (c);
6922 len = sprintf (str, "%03o", c);
6924 XSETINT (it->ctl_chars[0], escape_glyph);
6925 for (i = 0; i < len; i++)
6926 XSETINT (it->ctl_chars[i + 1], str[i]);
6927 ctl_len = len + 1;
6930 display_control:
6931 /* Set up IT->dpvec and return first character from it. */
6932 it->dpvec_char_len = it->len;
6933 it->dpvec = it->ctl_chars;
6934 it->dpend = it->dpvec + ctl_len;
6935 it->current.dpvec_index = 0;
6936 it->dpvec_face_id = face_id;
6937 it->saved_face_id = it->face_id;
6938 it->method = GET_FROM_DISPLAY_VECTOR;
6939 it->ellipsis_p = 0;
6940 goto get_next;
6942 it->char_to_display = c;
6944 else if (success_p)
6946 it->char_to_display = it->c;
6950 #ifdef HAVE_WINDOW_SYSTEM
6951 /* Adjust face id for a multibyte character. There are no multibyte
6952 character in unibyte text. */
6953 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6954 && it->multibyte_p
6955 && success_p
6956 && FRAME_WINDOW_P (it->f))
6958 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6960 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6962 /* Automatic composition with glyph-string. */
6963 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6965 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6967 else
6969 ptrdiff_t pos = (it->s ? -1
6970 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6971 : IT_CHARPOS (*it));
6972 int c;
6974 if (it->what == IT_CHARACTER)
6975 c = it->char_to_display;
6976 else
6978 struct composition *cmp = composition_table[it->cmp_it.id];
6979 int i;
6981 c = ' ';
6982 for (i = 0; i < cmp->glyph_len; i++)
6983 /* TAB in a composition means display glyphs with
6984 padding space on the left or right. */
6985 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6986 break;
6988 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6991 #endif /* HAVE_WINDOW_SYSTEM */
6993 done:
6994 /* Is this character the last one of a run of characters with
6995 box? If yes, set IT->end_of_box_run_p to 1. */
6996 if (it->face_box_p
6997 && it->s == NULL)
6999 if (it->method == GET_FROM_STRING && it->sp)
7001 int face_id = underlying_face_id (it);
7002 struct face *face = FACE_FROM_ID (it->f, face_id);
7004 if (face)
7006 if (face->box == FACE_NO_BOX)
7008 /* If the box comes from face properties in a
7009 display string, check faces in that string. */
7010 int string_face_id = face_after_it_pos (it);
7011 it->end_of_box_run_p
7012 = (FACE_FROM_ID (it->f, string_face_id)->box
7013 == FACE_NO_BOX);
7015 /* Otherwise, the box comes from the underlying face.
7016 If this is the last string character displayed, check
7017 the next buffer location. */
7018 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7019 && (it->current.overlay_string_index
7020 == it->n_overlay_strings - 1))
7022 ptrdiff_t ignore;
7023 int next_face_id;
7024 struct text_pos pos = it->current.pos;
7025 INC_TEXT_POS (pos, it->multibyte_p);
7027 next_face_id = face_at_buffer_position
7028 (it->w, CHARPOS (pos), &ignore,
7029 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7030 -1);
7031 it->end_of_box_run_p
7032 = (FACE_FROM_ID (it->f, next_face_id)->box
7033 == FACE_NO_BOX);
7037 /* next_element_from_display_vector sets this flag according to
7038 faces of the display vector glyphs, see there. */
7039 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7041 int face_id = face_after_it_pos (it);
7042 it->end_of_box_run_p
7043 = (face_id != it->face_id
7044 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7047 /* If we reached the end of the object we've been iterating (e.g., a
7048 display string or an overlay string), and there's something on
7049 IT->stack, proceed with what's on the stack. It doesn't make
7050 sense to return zero if there's unprocessed stuff on the stack,
7051 because otherwise that stuff will never be displayed. */
7052 if (!success_p && it->sp > 0)
7054 set_iterator_to_next (it, 0);
7055 success_p = get_next_display_element (it);
7058 /* Value is 0 if end of buffer or string reached. */
7059 return success_p;
7063 /* Move IT to the next display element.
7065 RESEAT_P non-zero means if called on a newline in buffer text,
7066 skip to the next visible line start.
7068 Functions get_next_display_element and set_iterator_to_next are
7069 separate because I find this arrangement easier to handle than a
7070 get_next_display_element function that also increments IT's
7071 position. The way it is we can first look at an iterator's current
7072 display element, decide whether it fits on a line, and if it does,
7073 increment the iterator position. The other way around we probably
7074 would either need a flag indicating whether the iterator has to be
7075 incremented the next time, or we would have to implement a
7076 decrement position function which would not be easy to write. */
7078 void
7079 set_iterator_to_next (struct it *it, int reseat_p)
7081 /* Reset flags indicating start and end of a sequence of characters
7082 with box. Reset them at the start of this function because
7083 moving the iterator to a new position might set them. */
7084 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7086 switch (it->method)
7088 case GET_FROM_BUFFER:
7089 /* The current display element of IT is a character from
7090 current_buffer. Advance in the buffer, and maybe skip over
7091 invisible lines that are so because of selective display. */
7092 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7093 reseat_at_next_visible_line_start (it, 0);
7094 else if (it->cmp_it.id >= 0)
7096 /* We are currently getting glyphs from a composition. */
7097 int i;
7099 if (! it->bidi_p)
7101 IT_CHARPOS (*it) += it->cmp_it.nchars;
7102 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7103 if (it->cmp_it.to < it->cmp_it.nglyphs)
7105 it->cmp_it.from = it->cmp_it.to;
7107 else
7109 it->cmp_it.id = -1;
7110 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7111 IT_BYTEPOS (*it),
7112 it->end_charpos, Qnil);
7115 else if (! it->cmp_it.reversed_p)
7117 /* Composition created while scanning forward. */
7118 /* Update IT's char/byte positions to point to the first
7119 character of the next grapheme cluster, or to the
7120 character visually after the current composition. */
7121 for (i = 0; i < it->cmp_it.nchars; i++)
7122 bidi_move_to_visually_next (&it->bidi_it);
7123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7124 IT_CHARPOS (*it) = it->bidi_it.charpos;
7126 if (it->cmp_it.to < it->cmp_it.nglyphs)
7128 /* Proceed to the next grapheme cluster. */
7129 it->cmp_it.from = it->cmp_it.to;
7131 else
7133 /* No more grapheme clusters in this composition.
7134 Find the next stop position. */
7135 ptrdiff_t stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 /* Now we are scanning backward and don't know
7138 where to stop. */
7139 stop = -1;
7140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7141 IT_BYTEPOS (*it), stop, Qnil);
7144 else
7146 /* Composition created while scanning backward. */
7147 /* Update IT's char/byte positions to point to the last
7148 character of the previous grapheme cluster, or the
7149 character visually after the current composition. */
7150 for (i = 0; i < it->cmp_it.nchars; i++)
7151 bidi_move_to_visually_next (&it->bidi_it);
7152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7153 IT_CHARPOS (*it) = it->bidi_it.charpos;
7154 if (it->cmp_it.from > 0)
7156 /* Proceed to the previous grapheme cluster. */
7157 it->cmp_it.to = it->cmp_it.from;
7159 else
7161 /* No more grapheme clusters in this composition.
7162 Find the next stop position. */
7163 ptrdiff_t stop = it->end_charpos;
7164 if (it->bidi_it.scan_dir < 0)
7165 /* Now we are scanning backward and don't know
7166 where to stop. */
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7169 IT_BYTEPOS (*it), stop, Qnil);
7173 else
7175 eassert (it->len != 0);
7177 if (!it->bidi_p)
7179 IT_BYTEPOS (*it) += it->len;
7180 IT_CHARPOS (*it) += 1;
7182 else
7184 int prev_scan_dir = it->bidi_it.scan_dir;
7185 /* If this is a new paragraph, determine its base
7186 direction (a.k.a. its base embedding level). */
7187 if (it->bidi_it.new_paragraph)
7188 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7189 bidi_move_to_visually_next (&it->bidi_it);
7190 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7191 IT_CHARPOS (*it) = it->bidi_it.charpos;
7192 if (prev_scan_dir != it->bidi_it.scan_dir)
7194 /* As the scan direction was changed, we must
7195 re-compute the stop position for composition. */
7196 ptrdiff_t stop = it->end_charpos;
7197 if (it->bidi_it.scan_dir < 0)
7198 stop = -1;
7199 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7200 IT_BYTEPOS (*it), stop, Qnil);
7203 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7205 break;
7207 case GET_FROM_C_STRING:
7208 /* Current display element of IT is from a C string. */
7209 if (!it->bidi_p
7210 /* If the string position is beyond string's end, it means
7211 next_element_from_c_string is padding the string with
7212 blanks, in which case we bypass the bidi iterator,
7213 because it cannot deal with such virtual characters. */
7214 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7216 IT_BYTEPOS (*it) += it->len;
7217 IT_CHARPOS (*it) += 1;
7219 else
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7223 IT_CHARPOS (*it) = it->bidi_it.charpos;
7225 break;
7227 case GET_FROM_DISPLAY_VECTOR:
7228 /* Current display element of IT is from a display table entry.
7229 Advance in the display table definition. Reset it to null if
7230 end reached, and continue with characters from buffers/
7231 strings. */
7232 ++it->current.dpvec_index;
7234 /* Restore face of the iterator to what they were before the
7235 display vector entry (these entries may contain faces). */
7236 it->face_id = it->saved_face_id;
7238 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7240 int recheck_faces = it->ellipsis_p;
7242 if (it->s)
7243 it->method = GET_FROM_C_STRING;
7244 else if (STRINGP (it->string))
7245 it->method = GET_FROM_STRING;
7246 else
7248 it->method = GET_FROM_BUFFER;
7249 it->object = it->w->contents;
7252 it->dpvec = NULL;
7253 it->current.dpvec_index = -1;
7255 /* Skip over characters which were displayed via IT->dpvec. */
7256 if (it->dpvec_char_len < 0)
7257 reseat_at_next_visible_line_start (it, 1);
7258 else if (it->dpvec_char_len > 0)
7260 if (it->method == GET_FROM_STRING
7261 && it->current.overlay_string_index >= 0
7262 && it->n_overlay_strings > 0)
7263 it->ignore_overlay_strings_at_pos_p = 1;
7264 it->len = it->dpvec_char_len;
7265 set_iterator_to_next (it, reseat_p);
7268 /* Maybe recheck faces after display vector */
7269 if (recheck_faces)
7270 it->stop_charpos = IT_CHARPOS (*it);
7272 break;
7274 case GET_FROM_STRING:
7275 /* Current display element is a character from a Lisp string. */
7276 eassert (it->s == NULL && STRINGP (it->string));
7277 /* Don't advance past string end. These conditions are true
7278 when set_iterator_to_next is called at the end of
7279 get_next_display_element, in which case the Lisp string is
7280 already exhausted, and all we want is pop the iterator
7281 stack. */
7282 if (it->current.overlay_string_index >= 0)
7284 /* This is an overlay string, so there's no padding with
7285 spaces, and the number of characters in the string is
7286 where the string ends. */
7287 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7288 goto consider_string_end;
7290 else
7292 /* Not an overlay string. There could be padding, so test
7293 against it->end_charpos . */
7294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7295 goto consider_string_end;
7297 if (it->cmp_it.id >= 0)
7299 int i;
7301 if (! it->bidi_p)
7303 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7304 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7305 if (it->cmp_it.to < it->cmp_it.nglyphs)
7306 it->cmp_it.from = it->cmp_it.to;
7307 else
7309 it->cmp_it.id = -1;
7310 composition_compute_stop_pos (&it->cmp_it,
7311 IT_STRING_CHARPOS (*it),
7312 IT_STRING_BYTEPOS (*it),
7313 it->end_charpos, it->string);
7316 else if (! it->cmp_it.reversed_p)
7318 for (i = 0; i < it->cmp_it.nchars; i++)
7319 bidi_move_to_visually_next (&it->bidi_it);
7320 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7321 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7323 if (it->cmp_it.to < it->cmp_it.nglyphs)
7324 it->cmp_it.from = it->cmp_it.to;
7325 else
7327 ptrdiff_t stop = it->end_charpos;
7328 if (it->bidi_it.scan_dir < 0)
7329 stop = -1;
7330 composition_compute_stop_pos (&it->cmp_it,
7331 IT_STRING_CHARPOS (*it),
7332 IT_STRING_BYTEPOS (*it), stop,
7333 it->string);
7336 else
7338 for (i = 0; i < it->cmp_it.nchars; i++)
7339 bidi_move_to_visually_next (&it->bidi_it);
7340 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7341 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7342 if (it->cmp_it.from > 0)
7343 it->cmp_it.to = it->cmp_it.from;
7344 else
7346 ptrdiff_t stop = it->end_charpos;
7347 if (it->bidi_it.scan_dir < 0)
7348 stop = -1;
7349 composition_compute_stop_pos (&it->cmp_it,
7350 IT_STRING_CHARPOS (*it),
7351 IT_STRING_BYTEPOS (*it), stop,
7352 it->string);
7356 else
7358 if (!it->bidi_p
7359 /* If the string position is beyond string's end, it
7360 means next_element_from_string is padding the string
7361 with blanks, in which case we bypass the bidi
7362 iterator, because it cannot deal with such virtual
7363 characters. */
7364 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7366 IT_STRING_BYTEPOS (*it) += it->len;
7367 IT_STRING_CHARPOS (*it) += 1;
7369 else
7371 int prev_scan_dir = it->bidi_it.scan_dir;
7373 bidi_move_to_visually_next (&it->bidi_it);
7374 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7375 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7376 if (prev_scan_dir != it->bidi_it.scan_dir)
7378 ptrdiff_t stop = it->end_charpos;
7380 if (it->bidi_it.scan_dir < 0)
7381 stop = -1;
7382 composition_compute_stop_pos (&it->cmp_it,
7383 IT_STRING_CHARPOS (*it),
7384 IT_STRING_BYTEPOS (*it), stop,
7385 it->string);
7390 consider_string_end:
7392 if (it->current.overlay_string_index >= 0)
7394 /* IT->string is an overlay string. Advance to the
7395 next, if there is one. */
7396 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7398 it->ellipsis_p = 0;
7399 next_overlay_string (it);
7400 if (it->ellipsis_p)
7401 setup_for_ellipsis (it, 0);
7404 else
7406 /* IT->string is not an overlay string. If we reached
7407 its end, and there is something on IT->stack, proceed
7408 with what is on the stack. This can be either another
7409 string, this time an overlay string, or a buffer. */
7410 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7411 && it->sp > 0)
7413 pop_it (it);
7414 if (it->method == GET_FROM_STRING)
7415 goto consider_string_end;
7418 break;
7420 case GET_FROM_IMAGE:
7421 case GET_FROM_STRETCH:
7422 /* The position etc with which we have to proceed are on
7423 the stack. The position may be at the end of a string,
7424 if the `display' property takes up the whole string. */
7425 eassert (it->sp > 0);
7426 pop_it (it);
7427 if (it->method == GET_FROM_STRING)
7428 goto consider_string_end;
7429 break;
7431 default:
7432 /* There are no other methods defined, so this should be a bug. */
7433 emacs_abort ();
7436 eassert (it->method != GET_FROM_STRING
7437 || (STRINGP (it->string)
7438 && IT_STRING_CHARPOS (*it) >= 0));
7441 /* Load IT's display element fields with information about the next
7442 display element which comes from a display table entry or from the
7443 result of translating a control character to one of the forms `^C'
7444 or `\003'.
7446 IT->dpvec holds the glyphs to return as characters.
7447 IT->saved_face_id holds the face id before the display vector--it
7448 is restored into IT->face_id in set_iterator_to_next. */
7450 static int
7451 next_element_from_display_vector (struct it *it)
7453 Lisp_Object gc;
7454 int prev_face_id = it->face_id;
7455 int next_face_id;
7457 /* Precondition. */
7458 eassert (it->dpvec && it->current.dpvec_index >= 0);
7460 it->face_id = it->saved_face_id;
7462 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7463 That seemed totally bogus - so I changed it... */
7464 gc = it->dpvec[it->current.dpvec_index];
7466 if (GLYPH_CODE_P (gc))
7468 struct face *this_face, *prev_face, *next_face;
7470 it->c = GLYPH_CODE_CHAR (gc);
7471 it->len = CHAR_BYTES (it->c);
7473 /* The entry may contain a face id to use. Such a face id is
7474 the id of a Lisp face, not a realized face. A face id of
7475 zero means no face is specified. */
7476 if (it->dpvec_face_id >= 0)
7477 it->face_id = it->dpvec_face_id;
7478 else
7480 int lface_id = GLYPH_CODE_FACE (gc);
7481 if (lface_id > 0)
7482 it->face_id = merge_faces (it->f, Qt, lface_id,
7483 it->saved_face_id);
7486 /* Glyphs in the display vector could have the box face, so we
7487 need to set the related flags in the iterator, as
7488 appropriate. */
7489 this_face = FACE_FROM_ID (it->f, it->face_id);
7490 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7492 /* Is this character the first character of a box-face run? */
7493 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7494 && (!prev_face
7495 || prev_face->box == FACE_NO_BOX));
7497 /* For the last character of the box-face run, we need to look
7498 either at the next glyph from the display vector, or at the
7499 face we saw before the display vector. */
7500 next_face_id = it->saved_face_id;
7501 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7503 if (it->dpvec_face_id >= 0)
7504 next_face_id = it->dpvec_face_id;
7505 else
7507 int lface_id =
7508 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7510 if (lface_id > 0)
7511 next_face_id = merge_faces (it->f, Qt, lface_id,
7512 it->saved_face_id);
7515 next_face = FACE_FROM_ID (it->f, next_face_id);
7516 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7517 && (!next_face
7518 || next_face->box == FACE_NO_BOX));
7519 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7521 else
7522 /* Display table entry is invalid. Return a space. */
7523 it->c = ' ', it->len = 1;
7525 /* Don't change position and object of the iterator here. They are
7526 still the values of the character that had this display table
7527 entry or was translated, and that's what we want. */
7528 it->what = IT_CHARACTER;
7529 return 1;
7532 /* Get the first element of string/buffer in the visual order, after
7533 being reseated to a new position in a string or a buffer. */
7534 static void
7535 get_visually_first_element (struct it *it)
7537 int string_p = STRINGP (it->string) || it->s;
7538 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7539 ptrdiff_t bob = (string_p ? 0 : BEGV);
7541 if (STRINGP (it->string))
7543 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7544 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7546 else
7548 it->bidi_it.charpos = IT_CHARPOS (*it);
7549 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7552 if (it->bidi_it.charpos == eob)
7554 /* Nothing to do, but reset the FIRST_ELT flag, like
7555 bidi_paragraph_init does, because we are not going to
7556 call it. */
7557 it->bidi_it.first_elt = 0;
7559 else if (it->bidi_it.charpos == bob
7560 || (!string_p
7561 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7562 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7564 /* If we are at the beginning of a line/string, we can produce
7565 the next element right away. */
7566 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7567 bidi_move_to_visually_next (&it->bidi_it);
7569 else
7571 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7573 /* We need to prime the bidi iterator starting at the line's or
7574 string's beginning, before we will be able to produce the
7575 next element. */
7576 if (string_p)
7577 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7578 else
7579 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7580 IT_BYTEPOS (*it), -1,
7581 &it->bidi_it.bytepos);
7582 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7585 /* Now return to buffer/string position where we were asked
7586 to get the next display element, and produce that. */
7587 bidi_move_to_visually_next (&it->bidi_it);
7589 while (it->bidi_it.bytepos != orig_bytepos
7590 && it->bidi_it.charpos < eob);
7593 /* Adjust IT's position information to where we ended up. */
7594 if (STRINGP (it->string))
7596 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7597 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7599 else
7601 IT_CHARPOS (*it) = it->bidi_it.charpos;
7602 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7605 if (STRINGP (it->string) || !it->s)
7607 ptrdiff_t stop, charpos, bytepos;
7609 if (STRINGP (it->string))
7611 eassert (!it->s);
7612 stop = SCHARS (it->string);
7613 if (stop > it->end_charpos)
7614 stop = it->end_charpos;
7615 charpos = IT_STRING_CHARPOS (*it);
7616 bytepos = IT_STRING_BYTEPOS (*it);
7618 else
7620 stop = it->end_charpos;
7621 charpos = IT_CHARPOS (*it);
7622 bytepos = IT_BYTEPOS (*it);
7624 if (it->bidi_it.scan_dir < 0)
7625 stop = -1;
7626 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7627 it->string);
7631 /* Load IT with the next display element from Lisp string IT->string.
7632 IT->current.string_pos is the current position within the string.
7633 If IT->current.overlay_string_index >= 0, the Lisp string is an
7634 overlay string. */
7636 static int
7637 next_element_from_string (struct it *it)
7639 struct text_pos position;
7641 eassert (STRINGP (it->string));
7642 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7643 eassert (IT_STRING_CHARPOS (*it) >= 0);
7644 position = it->current.string_pos;
7646 /* With bidi reordering, the character to display might not be the
7647 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7648 that we were reseat()ed to a new string, whose paragraph
7649 direction is not known. */
7650 if (it->bidi_p && it->bidi_it.first_elt)
7652 get_visually_first_element (it);
7653 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7656 /* Time to check for invisible text? */
7657 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7659 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7661 if (!(!it->bidi_p
7662 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7663 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7665 /* With bidi non-linear iteration, we could find
7666 ourselves far beyond the last computed stop_charpos,
7667 with several other stop positions in between that we
7668 missed. Scan them all now, in buffer's logical
7669 order, until we find and handle the last stop_charpos
7670 that precedes our current position. */
7671 handle_stop_backwards (it, it->stop_charpos);
7672 return GET_NEXT_DISPLAY_ELEMENT (it);
7674 else
7676 if (it->bidi_p)
7678 /* Take note of the stop position we just moved
7679 across, for when we will move back across it. */
7680 it->prev_stop = it->stop_charpos;
7681 /* If we are at base paragraph embedding level, take
7682 note of the last stop position seen at this
7683 level. */
7684 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7685 it->base_level_stop = it->stop_charpos;
7687 handle_stop (it);
7689 /* Since a handler may have changed IT->method, we must
7690 recurse here. */
7691 return GET_NEXT_DISPLAY_ELEMENT (it);
7694 else if (it->bidi_p
7695 /* If we are before prev_stop, we may have overstepped
7696 on our way backwards a stop_pos, and if so, we need
7697 to handle that stop_pos. */
7698 && IT_STRING_CHARPOS (*it) < it->prev_stop
7699 /* We can sometimes back up for reasons that have nothing
7700 to do with bidi reordering. E.g., compositions. The
7701 code below is only needed when we are above the base
7702 embedding level, so test for that explicitly. */
7703 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7705 /* If we lost track of base_level_stop, we have no better
7706 place for handle_stop_backwards to start from than string
7707 beginning. This happens, e.g., when we were reseated to
7708 the previous screenful of text by vertical-motion. */
7709 if (it->base_level_stop <= 0
7710 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7711 it->base_level_stop = 0;
7712 handle_stop_backwards (it, it->base_level_stop);
7713 return GET_NEXT_DISPLAY_ELEMENT (it);
7717 if (it->current.overlay_string_index >= 0)
7719 /* Get the next character from an overlay string. In overlay
7720 strings, there is no field width or padding with spaces to
7721 do. */
7722 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7724 it->what = IT_EOB;
7725 return 0;
7727 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it),
7729 it->bidi_it.scan_dir < 0
7730 ? -1
7731 : SCHARS (it->string))
7732 && next_element_from_composition (it))
7734 return 1;
7736 else if (STRING_MULTIBYTE (it->string))
7738 const unsigned char *s = (SDATA (it->string)
7739 + IT_STRING_BYTEPOS (*it));
7740 it->c = string_char_and_length (s, &it->len);
7742 else
7744 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7745 it->len = 1;
7748 else
7750 /* Get the next character from a Lisp string that is not an
7751 overlay string. Such strings come from the mode line, for
7752 example. We may have to pad with spaces, or truncate the
7753 string. See also next_element_from_c_string. */
7754 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7756 it->what = IT_EOB;
7757 return 0;
7759 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7761 /* Pad with spaces. */
7762 it->c = ' ', it->len = 1;
7763 CHARPOS (position) = BYTEPOS (position) = -1;
7765 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7766 IT_STRING_BYTEPOS (*it),
7767 it->bidi_it.scan_dir < 0
7768 ? -1
7769 : it->string_nchars)
7770 && next_element_from_composition (it))
7772 return 1;
7774 else if (STRING_MULTIBYTE (it->string))
7776 const unsigned char *s = (SDATA (it->string)
7777 + IT_STRING_BYTEPOS (*it));
7778 it->c = string_char_and_length (s, &it->len);
7780 else
7782 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7783 it->len = 1;
7787 /* Record what we have and where it came from. */
7788 it->what = IT_CHARACTER;
7789 it->object = it->string;
7790 it->position = position;
7791 return 1;
7795 /* Load IT with next display element from C string IT->s.
7796 IT->string_nchars is the maximum number of characters to return
7797 from the string. IT->end_charpos may be greater than
7798 IT->string_nchars when this function is called, in which case we
7799 may have to return padding spaces. Value is zero if end of string
7800 reached, including padding spaces. */
7802 static int
7803 next_element_from_c_string (struct it *it)
7805 int success_p = 1;
7807 eassert (it->s);
7808 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7809 it->what = IT_CHARACTER;
7810 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7811 it->object = Qnil;
7813 /* With bidi reordering, the character to display might not be the
7814 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7815 we were reseated to a new string, whose paragraph direction is
7816 not known. */
7817 if (it->bidi_p && it->bidi_it.first_elt)
7818 get_visually_first_element (it);
7820 /* IT's position can be greater than IT->string_nchars in case a
7821 field width or precision has been specified when the iterator was
7822 initialized. */
7823 if (IT_CHARPOS (*it) >= it->end_charpos)
7825 /* End of the game. */
7826 it->what = IT_EOB;
7827 success_p = 0;
7829 else if (IT_CHARPOS (*it) >= it->string_nchars)
7831 /* Pad with spaces. */
7832 it->c = ' ', it->len = 1;
7833 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7835 else if (it->multibyte_p)
7836 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7837 else
7838 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7840 return success_p;
7844 /* Set up IT to return characters from an ellipsis, if appropriate.
7845 The definition of the ellipsis glyphs may come from a display table
7846 entry. This function fills IT with the first glyph from the
7847 ellipsis if an ellipsis is to be displayed. */
7849 static int
7850 next_element_from_ellipsis (struct it *it)
7852 if (it->selective_display_ellipsis_p)
7853 setup_for_ellipsis (it, it->len);
7854 else
7856 /* The face at the current position may be different from the
7857 face we find after the invisible text. Remember what it
7858 was in IT->saved_face_id, and signal that it's there by
7859 setting face_before_selective_p. */
7860 it->saved_face_id = it->face_id;
7861 it->method = GET_FROM_BUFFER;
7862 it->object = it->w->contents;
7863 reseat_at_next_visible_line_start (it, 1);
7864 it->face_before_selective_p = 1;
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7871 /* Deliver an image display element. The iterator IT is already
7872 filled with image information (done in handle_display_prop). Value
7873 is always 1. */
7876 static int
7877 next_element_from_image (struct it *it)
7879 it->what = IT_IMAGE;
7880 it->ignore_overlay_strings_at_pos_p = 0;
7881 return 1;
7885 /* Fill iterator IT with next display element from a stretch glyph
7886 property. IT->object is the value of the text property. Value is
7887 always 1. */
7889 static int
7890 next_element_from_stretch (struct it *it)
7892 it->what = IT_STRETCH;
7893 return 1;
7896 /* Scan backwards from IT's current position until we find a stop
7897 position, or until BEGV. This is called when we find ourself
7898 before both the last known prev_stop and base_level_stop while
7899 reordering bidirectional text. */
7901 static void
7902 compute_stop_pos_backwards (struct it *it)
7904 const int SCAN_BACK_LIMIT = 1000;
7905 struct text_pos pos;
7906 struct display_pos save_current = it->current;
7907 struct text_pos save_position = it->position;
7908 ptrdiff_t charpos = IT_CHARPOS (*it);
7909 ptrdiff_t where_we_are = charpos;
7910 ptrdiff_t save_stop_pos = it->stop_charpos;
7911 ptrdiff_t save_end_pos = it->end_charpos;
7913 eassert (NILP (it->string) && !it->s);
7914 eassert (it->bidi_p);
7915 it->bidi_p = 0;
7918 it->end_charpos = min (charpos + 1, ZV);
7919 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7920 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7921 reseat_1 (it, pos, 0);
7922 compute_stop_pos (it);
7923 /* We must advance forward, right? */
7924 if (it->stop_charpos <= charpos)
7925 emacs_abort ();
7927 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7929 if (it->stop_charpos <= where_we_are)
7930 it->prev_stop = it->stop_charpos;
7931 else
7932 it->prev_stop = BEGV;
7933 it->bidi_p = 1;
7934 it->current = save_current;
7935 it->position = save_position;
7936 it->stop_charpos = save_stop_pos;
7937 it->end_charpos = save_end_pos;
7940 /* Scan forward from CHARPOS in the current buffer/string, until we
7941 find a stop position > current IT's position. Then handle the stop
7942 position before that. This is called when we bump into a stop
7943 position while reordering bidirectional text. CHARPOS should be
7944 the last previously processed stop_pos (or BEGV/0, if none were
7945 processed yet) whose position is less that IT's current
7946 position. */
7948 static void
7949 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7951 int bufp = !STRINGP (it->string);
7952 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7953 struct display_pos save_current = it->current;
7954 struct text_pos save_position = it->position;
7955 struct text_pos pos1;
7956 ptrdiff_t next_stop;
7958 /* Scan in strict logical order. */
7959 eassert (it->bidi_p);
7960 it->bidi_p = 0;
7963 it->prev_stop = charpos;
7964 if (bufp)
7966 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7967 reseat_1 (it, pos1, 0);
7969 else
7970 it->current.string_pos = string_pos (charpos, it->string);
7971 compute_stop_pos (it);
7972 /* We must advance forward, right? */
7973 if (it->stop_charpos <= it->prev_stop)
7974 emacs_abort ();
7975 charpos = it->stop_charpos;
7977 while (charpos <= where_we_are);
7979 it->bidi_p = 1;
7980 it->current = save_current;
7981 it->position = save_position;
7982 next_stop = it->stop_charpos;
7983 it->stop_charpos = it->prev_stop;
7984 handle_stop (it);
7985 it->stop_charpos = next_stop;
7988 /* Load IT with the next display element from current_buffer. Value
7989 is zero if end of buffer reached. IT->stop_charpos is the next
7990 position at which to stop and check for text properties or buffer
7991 end. */
7993 static int
7994 next_element_from_buffer (struct it *it)
7996 int success_p = 1;
7998 eassert (IT_CHARPOS (*it) >= BEGV);
7999 eassert (NILP (it->string) && !it->s);
8000 eassert (!it->bidi_p
8001 || (EQ (it->bidi_it.string.lstring, Qnil)
8002 && it->bidi_it.string.s == NULL));
8004 /* With bidi reordering, the character to display might not be the
8005 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8006 we were reseat()ed to a new buffer position, which is potentially
8007 a different paragraph. */
8008 if (it->bidi_p && it->bidi_it.first_elt)
8010 get_visually_first_element (it);
8011 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8014 if (IT_CHARPOS (*it) >= it->stop_charpos)
8016 if (IT_CHARPOS (*it) >= it->end_charpos)
8018 int overlay_strings_follow_p;
8020 /* End of the game, except when overlay strings follow that
8021 haven't been returned yet. */
8022 if (it->overlay_strings_at_end_processed_p)
8023 overlay_strings_follow_p = 0;
8024 else
8026 it->overlay_strings_at_end_processed_p = 1;
8027 overlay_strings_follow_p = get_overlay_strings (it, 0);
8030 if (overlay_strings_follow_p)
8031 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8032 else
8034 it->what = IT_EOB;
8035 it->position = it->current.pos;
8036 success_p = 0;
8039 else if (!(!it->bidi_p
8040 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8041 || IT_CHARPOS (*it) == it->stop_charpos))
8043 /* With bidi non-linear iteration, we could find ourselves
8044 far beyond the last computed stop_charpos, with several
8045 other stop positions in between that we missed. Scan
8046 them all now, in buffer's logical order, until we find
8047 and handle the last stop_charpos that precedes our
8048 current position. */
8049 handle_stop_backwards (it, it->stop_charpos);
8050 return GET_NEXT_DISPLAY_ELEMENT (it);
8052 else
8054 if (it->bidi_p)
8056 /* Take note of the stop position we just moved across,
8057 for when we will move back across it. */
8058 it->prev_stop = it->stop_charpos;
8059 /* If we are at base paragraph embedding level, take
8060 note of the last stop position seen at this
8061 level. */
8062 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8063 it->base_level_stop = it->stop_charpos;
8065 handle_stop (it);
8066 return GET_NEXT_DISPLAY_ELEMENT (it);
8069 else if (it->bidi_p
8070 /* If we are before prev_stop, we may have overstepped on
8071 our way backwards a stop_pos, and if so, we need to
8072 handle that stop_pos. */
8073 && IT_CHARPOS (*it) < it->prev_stop
8074 /* We can sometimes back up for reasons that have nothing
8075 to do with bidi reordering. E.g., compositions. The
8076 code below is only needed when we are above the base
8077 embedding level, so test for that explicitly. */
8078 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8080 if (it->base_level_stop <= 0
8081 || IT_CHARPOS (*it) < it->base_level_stop)
8083 /* If we lost track of base_level_stop, we need to find
8084 prev_stop by looking backwards. This happens, e.g., when
8085 we were reseated to the previous screenful of text by
8086 vertical-motion. */
8087 it->base_level_stop = BEGV;
8088 compute_stop_pos_backwards (it);
8089 handle_stop_backwards (it, it->prev_stop);
8091 else
8092 handle_stop_backwards (it, it->base_level_stop);
8093 return GET_NEXT_DISPLAY_ELEMENT (it);
8095 else
8097 /* No face changes, overlays etc. in sight, so just return a
8098 character from current_buffer. */
8099 unsigned char *p;
8100 ptrdiff_t stop;
8102 /* Maybe run the redisplay end trigger hook. Performance note:
8103 This doesn't seem to cost measurable time. */
8104 if (it->redisplay_end_trigger_charpos
8105 && it->glyph_row
8106 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8107 run_redisplay_end_trigger_hook (it);
8109 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8110 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8111 stop)
8112 && next_element_from_composition (it))
8114 return 1;
8117 /* Get the next character, maybe multibyte. */
8118 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8119 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8120 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8121 else
8122 it->c = *p, it->len = 1;
8124 /* Record what we have and where it came from. */
8125 it->what = IT_CHARACTER;
8126 it->object = it->w->contents;
8127 it->position = it->current.pos;
8129 /* Normally we return the character found above, except when we
8130 really want to return an ellipsis for selective display. */
8131 if (it->selective)
8133 if (it->c == '\n')
8135 /* A value of selective > 0 means hide lines indented more
8136 than that number of columns. */
8137 if (it->selective > 0
8138 && IT_CHARPOS (*it) + 1 < ZV
8139 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8140 IT_BYTEPOS (*it) + 1,
8141 it->selective))
8143 success_p = next_element_from_ellipsis (it);
8144 it->dpvec_char_len = -1;
8147 else if (it->c == '\r' && it->selective == -1)
8149 /* A value of selective == -1 means that everything from the
8150 CR to the end of the line is invisible, with maybe an
8151 ellipsis displayed for it. */
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8158 /* Value is zero if end of buffer reached. */
8159 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8160 return success_p;
8164 /* Run the redisplay end trigger hook for IT. */
8166 static void
8167 run_redisplay_end_trigger_hook (struct it *it)
8169 Lisp_Object args[3];
8171 /* IT->glyph_row should be non-null, i.e. we should be actually
8172 displaying something, or otherwise we should not run the hook. */
8173 eassert (it->glyph_row);
8175 /* Set up hook arguments. */
8176 args[0] = Qredisplay_end_trigger_functions;
8177 args[1] = it->window;
8178 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8179 it->redisplay_end_trigger_charpos = 0;
8181 /* Since we are *trying* to run these functions, don't try to run
8182 them again, even if they get an error. */
8183 wset_redisplay_end_trigger (it->w, Qnil);
8184 Frun_hook_with_args (3, args);
8186 /* Notice if it changed the face of the character we are on. */
8187 handle_face_prop (it);
8191 /* Deliver a composition display element. Unlike the other
8192 next_element_from_XXX, this function is not registered in the array
8193 get_next_element[]. It is called from next_element_from_buffer and
8194 next_element_from_string when necessary. */
8196 static int
8197 next_element_from_composition (struct it *it)
8199 it->what = IT_COMPOSITION;
8200 it->len = it->cmp_it.nbytes;
8201 if (STRINGP (it->string))
8203 if (it->c < 0)
8205 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8206 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8207 return 0;
8209 it->position = it->current.string_pos;
8210 it->object = it->string;
8211 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8212 IT_STRING_BYTEPOS (*it), it->string);
8214 else
8216 if (it->c < 0)
8218 IT_CHARPOS (*it) += it->cmp_it.nchars;
8219 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8220 if (it->bidi_p)
8222 if (it->bidi_it.new_paragraph)
8223 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8224 /* Resync the bidi iterator with IT's new position.
8225 FIXME: this doesn't support bidirectional text. */
8226 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8227 bidi_move_to_visually_next (&it->bidi_it);
8229 return 0;
8231 it->position = it->current.pos;
8232 it->object = it->w->contents;
8233 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8234 IT_BYTEPOS (*it), Qnil);
8236 return 1;
8241 /***********************************************************************
8242 Moving an iterator without producing glyphs
8243 ***********************************************************************/
8245 /* Check if iterator is at a position corresponding to a valid buffer
8246 position after some move_it_ call. */
8248 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8249 ((it)->method == GET_FROM_STRING \
8250 ? IT_STRING_CHARPOS (*it) == 0 \
8251 : 1)
8254 /* Move iterator IT to a specified buffer or X position within one
8255 line on the display without producing glyphs.
8257 OP should be a bit mask including some or all of these bits:
8258 MOVE_TO_X: Stop upon reaching x-position TO_X.
8259 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8260 Regardless of OP's value, stop upon reaching the end of the display line.
8262 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8263 This means, in particular, that TO_X includes window's horizontal
8264 scroll amount.
8266 The return value has several possible values that
8267 say what condition caused the scan to stop:
8269 MOVE_POS_MATCH_OR_ZV
8270 - when TO_POS or ZV was reached.
8272 MOVE_X_REACHED
8273 -when TO_X was reached before TO_POS or ZV were reached.
8275 MOVE_LINE_CONTINUED
8276 - when we reached the end of the display area and the line must
8277 be continued.
8279 MOVE_LINE_TRUNCATED
8280 - when we reached the end of the display area and the line is
8281 truncated.
8283 MOVE_NEWLINE_OR_CR
8284 - when we stopped at a line end, i.e. a newline or a CR and selective
8285 display is on. */
8287 static enum move_it_result
8288 move_it_in_display_line_to (struct it *it,
8289 ptrdiff_t to_charpos, int to_x,
8290 enum move_operation_enum op)
8292 enum move_it_result result = MOVE_UNDEFINED;
8293 struct glyph_row *saved_glyph_row;
8294 struct it wrap_it, atpos_it, atx_it, ppos_it;
8295 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8296 void *ppos_data = NULL;
8297 int may_wrap = 0;
8298 enum it_method prev_method = it->method;
8299 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8300 int saw_smaller_pos = prev_pos < to_charpos;
8302 /* Don't produce glyphs in produce_glyphs. */
8303 saved_glyph_row = it->glyph_row;
8304 it->glyph_row = NULL;
8306 /* Use wrap_it to save a copy of IT wherever a word wrap could
8307 occur. Use atpos_it to save a copy of IT at the desired buffer
8308 position, if found, so that we can scan ahead and check if the
8309 word later overshoots the window edge. Use atx_it similarly, for
8310 pixel positions. */
8311 wrap_it.sp = -1;
8312 atpos_it.sp = -1;
8313 atx_it.sp = -1;
8315 /* Use ppos_it under bidi reordering to save a copy of IT for the
8316 position > CHARPOS that is the closest to CHARPOS. We restore
8317 that position in IT when we have scanned the entire display line
8318 without finding a match for CHARPOS and all the character
8319 positions are greater than CHARPOS. */
8320 if (it->bidi_p)
8322 SAVE_IT (ppos_it, *it, ppos_data);
8323 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8324 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8325 SAVE_IT (ppos_it, *it, ppos_data);
8328 #define BUFFER_POS_REACHED_P() \
8329 ((op & MOVE_TO_POS) != 0 \
8330 && BUFFERP (it->object) \
8331 && (IT_CHARPOS (*it) == to_charpos \
8332 || ((!it->bidi_p \
8333 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8334 && IT_CHARPOS (*it) > to_charpos) \
8335 || (it->what == IT_COMPOSITION \
8336 && ((IT_CHARPOS (*it) > to_charpos \
8337 && to_charpos >= it->cmp_it.charpos) \
8338 || (IT_CHARPOS (*it) < to_charpos \
8339 && to_charpos <= it->cmp_it.charpos)))) \
8340 && (it->method == GET_FROM_BUFFER \
8341 || (it->method == GET_FROM_DISPLAY_VECTOR \
8342 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8344 /* If there's a line-/wrap-prefix, handle it. */
8345 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8346 && it->current_y < it->last_visible_y)
8347 handle_line_prefix (it);
8349 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8350 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8352 while (1)
8354 int x, i, ascent = 0, descent = 0;
8356 /* Utility macro to reset an iterator with x, ascent, and descent. */
8357 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8358 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8359 (IT)->max_descent = descent)
8361 /* Stop if we move beyond TO_CHARPOS (after an image or a
8362 display string or stretch glyph). */
8363 if ((op & MOVE_TO_POS) != 0
8364 && BUFFERP (it->object)
8365 && it->method == GET_FROM_BUFFER
8366 && (((!it->bidi_p
8367 /* When the iterator is at base embedding level, we
8368 are guaranteed that characters are delivered for
8369 display in strictly increasing order of their
8370 buffer positions. */
8371 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8372 && IT_CHARPOS (*it) > to_charpos)
8373 || (it->bidi_p
8374 && (prev_method == GET_FROM_IMAGE
8375 || prev_method == GET_FROM_STRETCH
8376 || prev_method == GET_FROM_STRING)
8377 /* Passed TO_CHARPOS from left to right. */
8378 && ((prev_pos < to_charpos
8379 && IT_CHARPOS (*it) > to_charpos)
8380 /* Passed TO_CHARPOS from right to left. */
8381 || (prev_pos > to_charpos
8382 && IT_CHARPOS (*it) < to_charpos)))))
8384 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8386 result = MOVE_POS_MATCH_OR_ZV;
8387 break;
8389 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8390 /* If wrap_it is valid, the current position might be in a
8391 word that is wrapped. So, save the iterator in
8392 atpos_it and continue to see if wrapping happens. */
8393 SAVE_IT (atpos_it, *it, atpos_data);
8396 /* Stop when ZV reached.
8397 We used to stop here when TO_CHARPOS reached as well, but that is
8398 too soon if this glyph does not fit on this line. So we handle it
8399 explicitly below. */
8400 if (!get_next_display_element (it))
8402 result = MOVE_POS_MATCH_OR_ZV;
8403 break;
8406 if (it->line_wrap == TRUNCATE)
8408 if (BUFFER_POS_REACHED_P ())
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8414 else
8416 if (it->line_wrap == WORD_WRAP)
8418 if (IT_DISPLAYING_WHITESPACE (it))
8419 may_wrap = 1;
8420 else if (may_wrap)
8422 /* We have reached a glyph that follows one or more
8423 whitespace characters. If the position is
8424 already found, we are done. */
8425 if (atpos_it.sp >= 0)
8427 RESTORE_IT (it, &atpos_it, atpos_data);
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 goto done;
8431 if (atx_it.sp >= 0)
8433 RESTORE_IT (it, &atx_it, atx_data);
8434 result = MOVE_X_REACHED;
8435 goto done;
8437 /* Otherwise, we can wrap here. */
8438 SAVE_IT (wrap_it, *it, wrap_data);
8439 may_wrap = 0;
8444 /* Remember the line height for the current line, in case
8445 the next element doesn't fit on the line. */
8446 ascent = it->max_ascent;
8447 descent = it->max_descent;
8449 /* The call to produce_glyphs will get the metrics of the
8450 display element IT is loaded with. Record the x-position
8451 before this display element, in case it doesn't fit on the
8452 line. */
8453 x = it->current_x;
8455 PRODUCE_GLYPHS (it);
8457 if (it->area != TEXT_AREA)
8459 prev_method = it->method;
8460 if (it->method == GET_FROM_BUFFER)
8461 prev_pos = IT_CHARPOS (*it);
8462 set_iterator_to_next (it, 1);
8463 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8464 SET_TEXT_POS (this_line_min_pos,
8465 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8466 if (it->bidi_p
8467 && (op & MOVE_TO_POS)
8468 && IT_CHARPOS (*it) > to_charpos
8469 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8470 SAVE_IT (ppos_it, *it, ppos_data);
8471 continue;
8474 /* The number of glyphs we get back in IT->nglyphs will normally
8475 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8476 character on a terminal frame, or (iii) a line end. For the
8477 second case, IT->nglyphs - 1 padding glyphs will be present.
8478 (On X frames, there is only one glyph produced for a
8479 composite character.)
8481 The behavior implemented below means, for continuation lines,
8482 that as many spaces of a TAB as fit on the current line are
8483 displayed there. For terminal frames, as many glyphs of a
8484 multi-glyph character are displayed in the current line, too.
8485 This is what the old redisplay code did, and we keep it that
8486 way. Under X, the whole shape of a complex character must
8487 fit on the line or it will be completely displayed in the
8488 next line.
8490 Note that both for tabs and padding glyphs, all glyphs have
8491 the same width. */
8492 if (it->nglyphs)
8494 /* More than one glyph or glyph doesn't fit on line. All
8495 glyphs have the same width. */
8496 int single_glyph_width = it->pixel_width / it->nglyphs;
8497 int new_x;
8498 int x_before_this_char = x;
8499 int hpos_before_this_char = it->hpos;
8501 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8503 new_x = x + single_glyph_width;
8505 /* We want to leave anything reaching TO_X to the caller. */
8506 if ((op & MOVE_TO_X) && new_x > to_x)
8508 if (BUFFER_POS_REACHED_P ())
8510 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8511 goto buffer_pos_reached;
8512 if (atpos_it.sp < 0)
8514 SAVE_IT (atpos_it, *it, atpos_data);
8515 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8518 else
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8522 it->current_x = x;
8523 result = MOVE_X_REACHED;
8524 break;
8526 if (atx_it.sp < 0)
8528 SAVE_IT (atx_it, *it, atx_data);
8529 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8534 if (/* Lines are continued. */
8535 it->line_wrap != TRUNCATE
8536 && (/* And glyph doesn't fit on the line. */
8537 new_x > it->last_visible_x
8538 /* Or it fits exactly and we're on a window
8539 system frame. */
8540 || (new_x == it->last_visible_x
8541 && FRAME_WINDOW_P (it->f)
8542 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8543 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8544 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8546 if (/* IT->hpos == 0 means the very first glyph
8547 doesn't fit on the line, e.g. a wide image. */
8548 it->hpos == 0
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)))
8552 ++it->hpos;
8553 it->current_x = new_x;
8555 /* The character's last glyph just barely fits
8556 in this row. */
8557 if (i == it->nglyphs - 1)
8559 /* If this is the destination position,
8560 return a position *before* it in this row,
8561 now that we know it fits in this row. */
8562 if (BUFFER_POS_REACHED_P ())
8564 if (it->line_wrap != WORD_WRAP
8565 || wrap_it.sp < 0)
8567 it->hpos = hpos_before_this_char;
8568 it->current_x = x_before_this_char;
8569 result = MOVE_POS_MATCH_OR_ZV;
8570 break;
8572 if (it->line_wrap == WORD_WRAP
8573 && atpos_it.sp < 0)
8575 SAVE_IT (atpos_it, *it, atpos_data);
8576 atpos_it.current_x = x_before_this_char;
8577 atpos_it.hpos = hpos_before_this_char;
8581 prev_method = it->method;
8582 if (it->method == GET_FROM_BUFFER)
8583 prev_pos = IT_CHARPOS (*it);
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos,
8587 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8588 /* On graphical terminals, newlines may
8589 "overflow" into the fringe if
8590 overflow-newline-into-fringe is non-nil.
8591 On text terminals, and on graphical
8592 terminals with no right margin, newlines
8593 may overflow into the last glyph on the
8594 display line.*/
8595 if (!FRAME_WINDOW_P (it->f)
8596 || ((it->bidi_p
8597 && it->bidi_it.paragraph_dir == R2L)
8598 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8599 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8600 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8602 if (!get_next_display_element (it))
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 break;
8607 if (BUFFER_POS_REACHED_P ())
8609 if (ITERATOR_AT_END_OF_LINE_P (it))
8610 result = MOVE_POS_MATCH_OR_ZV;
8611 else
8612 result = MOVE_LINE_CONTINUED;
8613 break;
8615 if (ITERATOR_AT_END_OF_LINE_P (it)
8616 && (it->line_wrap != WORD_WRAP
8617 || wrap_it.sp < 0))
8619 result = MOVE_NEWLINE_OR_CR;
8620 break;
8625 else
8626 IT_RESET_X_ASCENT_DESCENT (it);
8628 if (wrap_it.sp >= 0)
8630 RESTORE_IT (it, &wrap_it, wrap_data);
8631 atpos_it.sp = -1;
8632 atx_it.sp = -1;
8635 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8636 IT_CHARPOS (*it)));
8637 result = MOVE_LINE_CONTINUED;
8638 break;
8641 if (BUFFER_POS_REACHED_P ())
8643 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8644 goto buffer_pos_reached;
8645 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8647 SAVE_IT (atpos_it, *it, atpos_data);
8648 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8652 if (new_x > it->first_visible_x)
8654 /* Glyph is visible. Increment number of glyphs that
8655 would be displayed. */
8656 ++it->hpos;
8660 if (result != MOVE_UNDEFINED)
8661 break;
8663 else if (BUFFER_POS_REACHED_P ())
8665 buffer_pos_reached:
8666 IT_RESET_X_ASCENT_DESCENT (it);
8667 result = MOVE_POS_MATCH_OR_ZV;
8668 break;
8670 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8672 /* Stop when TO_X specified and reached. This check is
8673 necessary here because of lines consisting of a line end,
8674 only. The line end will not produce any glyphs and we
8675 would never get MOVE_X_REACHED. */
8676 eassert (it->nglyphs == 0);
8677 result = MOVE_X_REACHED;
8678 break;
8681 /* Is this a line end? If yes, we're done. */
8682 if (ITERATOR_AT_END_OF_LINE_P (it))
8684 /* If we are past TO_CHARPOS, but never saw any character
8685 positions smaller than TO_CHARPOS, return
8686 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8687 did. */
8688 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8690 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8692 if (IT_CHARPOS (ppos_it) < ZV)
8694 RESTORE_IT (it, &ppos_it, ppos_data);
8695 result = MOVE_POS_MATCH_OR_ZV;
8697 else
8698 goto buffer_pos_reached;
8700 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8701 && IT_CHARPOS (*it) > to_charpos)
8702 goto buffer_pos_reached;
8703 else
8704 result = MOVE_NEWLINE_OR_CR;
8706 else
8707 result = MOVE_NEWLINE_OR_CR;
8708 break;
8711 prev_method = it->method;
8712 if (it->method == GET_FROM_BUFFER)
8713 prev_pos = IT_CHARPOS (*it);
8714 /* The current display element has been consumed. Advance
8715 to the next. */
8716 set_iterator_to_next (it, 1);
8717 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8718 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8719 if (IT_CHARPOS (*it) < to_charpos)
8720 saw_smaller_pos = 1;
8721 if (it->bidi_p
8722 && (op & MOVE_TO_POS)
8723 && IT_CHARPOS (*it) >= to_charpos
8724 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8725 SAVE_IT (ppos_it, *it, ppos_data);
8727 /* Stop if lines are truncated and IT's current x-position is
8728 past the right edge of the window now. */
8729 if (it->line_wrap == TRUNCATE
8730 && it->current_x >= it->last_visible_x)
8732 if (!FRAME_WINDOW_P (it->f)
8733 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8734 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8735 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8736 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8738 int at_eob_p = 0;
8740 if ((at_eob_p = !get_next_display_element (it))
8741 || BUFFER_POS_REACHED_P ()
8742 /* If we are past TO_CHARPOS, but never saw any
8743 character positions smaller than TO_CHARPOS,
8744 return MOVE_POS_MATCH_OR_ZV, like the
8745 unidirectional display did. */
8746 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8747 && !saw_smaller_pos
8748 && IT_CHARPOS (*it) > to_charpos))
8750 if (it->bidi_p
8751 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8752 RESTORE_IT (it, &ppos_it, ppos_data);
8753 result = MOVE_POS_MATCH_OR_ZV;
8754 break;
8756 if (ITERATOR_AT_END_OF_LINE_P (it))
8758 result = MOVE_NEWLINE_OR_CR;
8759 break;
8762 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8763 && !saw_smaller_pos
8764 && IT_CHARPOS (*it) > to_charpos)
8766 if (IT_CHARPOS (ppos_it) < ZV)
8767 RESTORE_IT (it, &ppos_it, ppos_data);
8768 result = MOVE_POS_MATCH_OR_ZV;
8769 break;
8771 result = MOVE_LINE_TRUNCATED;
8772 break;
8774 #undef IT_RESET_X_ASCENT_DESCENT
8777 #undef BUFFER_POS_REACHED_P
8779 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8780 restore the saved iterator. */
8781 if (atpos_it.sp >= 0)
8782 RESTORE_IT (it, &atpos_it, atpos_data);
8783 else if (atx_it.sp >= 0)
8784 RESTORE_IT (it, &atx_it, atx_data);
8786 done:
8788 if (atpos_data)
8789 bidi_unshelve_cache (atpos_data, 1);
8790 if (atx_data)
8791 bidi_unshelve_cache (atx_data, 1);
8792 if (wrap_data)
8793 bidi_unshelve_cache (wrap_data, 1);
8794 if (ppos_data)
8795 bidi_unshelve_cache (ppos_data, 1);
8797 /* Restore the iterator settings altered at the beginning of this
8798 function. */
8799 it->glyph_row = saved_glyph_row;
8800 return result;
8803 /* For external use. */
8804 void
8805 move_it_in_display_line (struct it *it,
8806 ptrdiff_t to_charpos, int to_x,
8807 enum move_operation_enum op)
8809 if (it->line_wrap == WORD_WRAP
8810 && (op & MOVE_TO_X))
8812 struct it save_it;
8813 void *save_data = NULL;
8814 int skip;
8816 SAVE_IT (save_it, *it, save_data);
8817 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8818 /* When word-wrap is on, TO_X may lie past the end
8819 of a wrapped line. Then it->current is the
8820 character on the next line, so backtrack to the
8821 space before the wrap point. */
8822 if (skip == MOVE_LINE_CONTINUED)
8824 int prev_x = max (it->current_x - 1, 0);
8825 RESTORE_IT (it, &save_it, save_data);
8826 move_it_in_display_line_to
8827 (it, -1, prev_x, MOVE_TO_X);
8829 else
8830 bidi_unshelve_cache (save_data, 1);
8832 else
8833 move_it_in_display_line_to (it, to_charpos, to_x, op);
8837 /* Move IT forward until it satisfies one or more of the criteria in
8838 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8840 OP is a bit-mask that specifies where to stop, and in particular,
8841 which of those four position arguments makes a difference. See the
8842 description of enum move_operation_enum.
8844 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8845 screen line, this function will set IT to the next position that is
8846 displayed to the right of TO_CHARPOS on the screen. */
8848 void
8849 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8851 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8852 int line_height, line_start_x = 0, reached = 0;
8853 void *backup_data = NULL;
8855 for (;;)
8857 if (op & MOVE_TO_VPOS)
8859 /* If no TO_CHARPOS and no TO_X specified, stop at the
8860 start of the line TO_VPOS. */
8861 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8863 if (it->vpos == to_vpos)
8865 reached = 1;
8866 break;
8868 else
8869 skip = move_it_in_display_line_to (it, -1, -1, 0);
8871 else
8873 /* TO_VPOS >= 0 means stop at TO_X in the line at
8874 TO_VPOS, or at TO_POS, whichever comes first. */
8875 if (it->vpos == to_vpos)
8877 reached = 2;
8878 break;
8881 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8883 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8885 reached = 3;
8886 break;
8888 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8890 /* We have reached TO_X but not in the line we want. */
8891 skip = move_it_in_display_line_to (it, to_charpos,
8892 -1, MOVE_TO_POS);
8893 if (skip == MOVE_POS_MATCH_OR_ZV)
8895 reached = 4;
8896 break;
8901 else if (op & MOVE_TO_Y)
8903 struct it it_backup;
8905 if (it->line_wrap == WORD_WRAP)
8906 SAVE_IT (it_backup, *it, backup_data);
8908 /* TO_Y specified means stop at TO_X in the line containing
8909 TO_Y---or at TO_CHARPOS if this is reached first. The
8910 problem is that we can't really tell whether the line
8911 contains TO_Y before we have completely scanned it, and
8912 this may skip past TO_X. What we do is to first scan to
8913 TO_X.
8915 If TO_X is not specified, use a TO_X of zero. The reason
8916 is to make the outcome of this function more predictable.
8917 If we didn't use TO_X == 0, we would stop at the end of
8918 the line which is probably not what a caller would expect
8919 to happen. */
8920 skip = move_it_in_display_line_to
8921 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8922 (MOVE_TO_X | (op & MOVE_TO_POS)));
8924 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8925 if (skip == MOVE_POS_MATCH_OR_ZV)
8926 reached = 5;
8927 else if (skip == MOVE_X_REACHED)
8929 /* If TO_X was reached, we want to know whether TO_Y is
8930 in the line. We know this is the case if the already
8931 scanned glyphs make the line tall enough. Otherwise,
8932 we must check by scanning the rest of the line. */
8933 line_height = it->max_ascent + it->max_descent;
8934 if (to_y >= it->current_y
8935 && to_y < it->current_y + line_height)
8937 reached = 6;
8938 break;
8940 SAVE_IT (it_backup, *it, backup_data);
8941 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8942 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8943 op & MOVE_TO_POS);
8944 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8945 line_height = it->max_ascent + it->max_descent;
8946 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8951 /* If TO_Y is in this line and TO_X was reached
8952 above, we scanned too far. We have to restore
8953 IT's settings to the ones before skipping. But
8954 keep the more accurate values of max_ascent and
8955 max_descent we've found while skipping the rest
8956 of the line, for the sake of callers, such as
8957 pos_visible_p, that need to know the line
8958 height. */
8959 int max_ascent = it->max_ascent;
8960 int max_descent = it->max_descent;
8962 RESTORE_IT (it, &it_backup, backup_data);
8963 it->max_ascent = max_ascent;
8964 it->max_descent = max_descent;
8965 reached = 6;
8967 else
8969 skip = skip2;
8970 if (skip == MOVE_POS_MATCH_OR_ZV)
8971 reached = 7;
8974 else
8976 /* Check whether TO_Y is in this line. */
8977 line_height = it->max_ascent + it->max_descent;
8978 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8980 if (to_y >= it->current_y
8981 && to_y < it->current_y + line_height)
8983 /* When word-wrap is on, TO_X may lie past the end
8984 of a wrapped line. Then it->current is the
8985 character on the next line, so backtrack to the
8986 space before the wrap point. */
8987 if (skip == MOVE_LINE_CONTINUED
8988 && it->line_wrap == WORD_WRAP)
8990 int prev_x = max (it->current_x - 1, 0);
8991 RESTORE_IT (it, &it_backup, backup_data);
8992 skip = move_it_in_display_line_to
8993 (it, -1, prev_x, MOVE_TO_X);
8995 reached = 6;
8999 if (reached)
9000 break;
9002 else if (BUFFERP (it->object)
9003 && (it->method == GET_FROM_BUFFER
9004 || it->method == GET_FROM_STRETCH)
9005 && IT_CHARPOS (*it) >= to_charpos
9006 /* Under bidi iteration, a call to set_iterator_to_next
9007 can scan far beyond to_charpos if the initial
9008 portion of the next line needs to be reordered. In
9009 that case, give move_it_in_display_line_to another
9010 chance below. */
9011 && !(it->bidi_p
9012 && it->bidi_it.scan_dir == -1))
9013 skip = MOVE_POS_MATCH_OR_ZV;
9014 else
9015 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9017 switch (skip)
9019 case MOVE_POS_MATCH_OR_ZV:
9020 reached = 8;
9021 goto out;
9023 case MOVE_NEWLINE_OR_CR:
9024 set_iterator_to_next (it, 1);
9025 it->continuation_lines_width = 0;
9026 break;
9028 case MOVE_LINE_TRUNCATED:
9029 it->continuation_lines_width = 0;
9030 reseat_at_next_visible_line_start (it, 0);
9031 if ((op & MOVE_TO_POS) != 0
9032 && IT_CHARPOS (*it) > to_charpos)
9034 reached = 9;
9035 goto out;
9037 break;
9039 case MOVE_LINE_CONTINUED:
9040 /* For continued lines ending in a tab, some of the glyphs
9041 associated with the tab are displayed on the current
9042 line. Since it->current_x does not include these glyphs,
9043 we use it->last_visible_x instead. */
9044 if (it->c == '\t')
9046 it->continuation_lines_width += it->last_visible_x;
9047 /* When moving by vpos, ensure that the iterator really
9048 advances to the next line (bug#847, bug#969). Fixme:
9049 do we need to do this in other circumstances? */
9050 if (it->current_x != it->last_visible_x
9051 && (op & MOVE_TO_VPOS)
9052 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9054 line_start_x = it->current_x + it->pixel_width
9055 - it->last_visible_x;
9056 set_iterator_to_next (it, 0);
9059 else
9060 it->continuation_lines_width += it->current_x;
9061 break;
9063 default:
9064 emacs_abort ();
9067 /* Reset/increment for the next run. */
9068 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9069 it->current_x = line_start_x;
9070 line_start_x = 0;
9071 it->hpos = 0;
9072 it->current_y += it->max_ascent + it->max_descent;
9073 ++it->vpos;
9074 last_height = it->max_ascent + it->max_descent;
9075 it->max_ascent = it->max_descent = 0;
9078 out:
9080 /* On text terminals, we may stop at the end of a line in the middle
9081 of a multi-character glyph. If the glyph itself is continued,
9082 i.e. it is actually displayed on the next line, don't treat this
9083 stopping point as valid; move to the next line instead (unless
9084 that brings us offscreen). */
9085 if (!FRAME_WINDOW_P (it->f)
9086 && op & MOVE_TO_POS
9087 && IT_CHARPOS (*it) == to_charpos
9088 && it->what == IT_CHARACTER
9089 && it->nglyphs > 1
9090 && it->line_wrap == WINDOW_WRAP
9091 && it->current_x == it->last_visible_x - 1
9092 && it->c != '\n'
9093 && it->c != '\t'
9094 && it->vpos < it->w->window_end_vpos)
9096 it->continuation_lines_width += it->current_x;
9097 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9098 it->current_y += it->max_ascent + it->max_descent;
9099 ++it->vpos;
9100 last_height = it->max_ascent + it->max_descent;
9103 if (backup_data)
9104 bidi_unshelve_cache (backup_data, 1);
9106 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9110 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9112 If DY > 0, move IT backward at least that many pixels. DY = 0
9113 means move IT backward to the preceding line start or BEGV. This
9114 function may move over more than DY pixels if IT->current_y - DY
9115 ends up in the middle of a line; in this case IT->current_y will be
9116 set to the top of the line moved to. */
9118 void
9119 move_it_vertically_backward (struct it *it, int dy)
9121 int nlines, h;
9122 struct it it2, it3;
9123 void *it2data = NULL, *it3data = NULL;
9124 ptrdiff_t start_pos;
9125 int nchars_per_row
9126 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9127 ptrdiff_t pos_limit;
9129 move_further_back:
9130 eassert (dy >= 0);
9132 start_pos = IT_CHARPOS (*it);
9134 /* Estimate how many newlines we must move back. */
9135 nlines = max (1, dy / default_line_pixel_height (it->w));
9136 if (it->line_wrap == TRUNCATE)
9137 pos_limit = BEGV;
9138 else
9139 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9141 /* Set the iterator's position that many lines back. But don't go
9142 back more than NLINES full screen lines -- this wins a day with
9143 buffers which have very long lines. */
9144 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9145 back_to_previous_visible_line_start (it);
9147 /* Reseat the iterator here. When moving backward, we don't want
9148 reseat to skip forward over invisible text, set up the iterator
9149 to deliver from overlay strings at the new position etc. So,
9150 use reseat_1 here. */
9151 reseat_1 (it, it->current.pos, 1);
9153 /* We are now surely at a line start. */
9154 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9155 reordering is in effect. */
9156 it->continuation_lines_width = 0;
9158 /* Move forward and see what y-distance we moved. First move to the
9159 start of the next line so that we get its height. We need this
9160 height to be able to tell whether we reached the specified
9161 y-distance. */
9162 SAVE_IT (it2, *it, it2data);
9163 it2.max_ascent = it2.max_descent = 0;
9166 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9167 MOVE_TO_POS | MOVE_TO_VPOS);
9169 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9170 /* If we are in a display string which starts at START_POS,
9171 and that display string includes a newline, and we are
9172 right after that newline (i.e. at the beginning of a
9173 display line), exit the loop, because otherwise we will
9174 infloop, since move_it_to will see that it is already at
9175 START_POS and will not move. */
9176 || (it2.method == GET_FROM_STRING
9177 && IT_CHARPOS (it2) == start_pos
9178 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9179 eassert (IT_CHARPOS (*it) >= BEGV);
9180 SAVE_IT (it3, it2, it3data);
9182 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9183 eassert (IT_CHARPOS (*it) >= BEGV);
9184 /* H is the actual vertical distance from the position in *IT
9185 and the starting position. */
9186 h = it2.current_y - it->current_y;
9187 /* NLINES is the distance in number of lines. */
9188 nlines = it2.vpos - it->vpos;
9190 /* Correct IT's y and vpos position
9191 so that they are relative to the starting point. */
9192 it->vpos -= nlines;
9193 it->current_y -= h;
9195 if (dy == 0)
9197 /* DY == 0 means move to the start of the screen line. The
9198 value of nlines is > 0 if continuation lines were involved,
9199 or if the original IT position was at start of a line. */
9200 RESTORE_IT (it, it, it2data);
9201 if (nlines > 0)
9202 move_it_by_lines (it, nlines);
9203 /* The above code moves us to some position NLINES down,
9204 usually to its first glyph (leftmost in an L2R line), but
9205 that's not necessarily the start of the line, under bidi
9206 reordering. We want to get to the character position
9207 that is immediately after the newline of the previous
9208 line. */
9209 if (it->bidi_p
9210 && !it->continuation_lines_width
9211 && !STRINGP (it->string)
9212 && IT_CHARPOS (*it) > BEGV
9213 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9215 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9217 DEC_BOTH (cp, bp);
9218 cp = find_newline_no_quit (cp, bp, -1, NULL);
9219 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9221 bidi_unshelve_cache (it3data, 1);
9223 else
9225 /* The y-position we try to reach, relative to *IT.
9226 Note that H has been subtracted in front of the if-statement. */
9227 int target_y = it->current_y + h - dy;
9228 int y0 = it3.current_y;
9229 int y1;
9230 int line_height;
9232 RESTORE_IT (&it3, &it3, it3data);
9233 y1 = line_bottom_y (&it3);
9234 line_height = y1 - y0;
9235 RESTORE_IT (it, it, it2data);
9236 /* If we did not reach target_y, try to move further backward if
9237 we can. If we moved too far backward, try to move forward. */
9238 if (target_y < it->current_y
9239 /* This is heuristic. In a window that's 3 lines high, with
9240 a line height of 13 pixels each, recentering with point
9241 on the bottom line will try to move -39/2 = 19 pixels
9242 backward. Try to avoid moving into the first line. */
9243 && (it->current_y - target_y
9244 > min (window_box_height (it->w), line_height * 2 / 3))
9245 && IT_CHARPOS (*it) > BEGV)
9247 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9248 target_y - it->current_y));
9249 dy = it->current_y - target_y;
9250 goto move_further_back;
9252 else if (target_y >= it->current_y + line_height
9253 && IT_CHARPOS (*it) < ZV)
9255 /* Should move forward by at least one line, maybe more.
9257 Note: Calling move_it_by_lines can be expensive on
9258 terminal frames, where compute_motion is used (via
9259 vmotion) to do the job, when there are very long lines
9260 and truncate-lines is nil. That's the reason for
9261 treating terminal frames specially here. */
9263 if (!FRAME_WINDOW_P (it->f))
9264 move_it_vertically (it, target_y - (it->current_y + line_height));
9265 else
9269 move_it_by_lines (it, 1);
9271 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9278 /* Move IT by a specified amount of pixel lines DY. DY negative means
9279 move backwards. DY = 0 means move to start of screen line. At the
9280 end, IT will be on the start of a screen line. */
9282 void
9283 move_it_vertically (struct it *it, int dy)
9285 if (dy <= 0)
9286 move_it_vertically_backward (it, -dy);
9287 else
9289 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9290 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9291 MOVE_TO_POS | MOVE_TO_Y);
9292 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9294 /* If buffer ends in ZV without a newline, move to the start of
9295 the line to satisfy the post-condition. */
9296 if (IT_CHARPOS (*it) == ZV
9297 && ZV > BEGV
9298 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9299 move_it_by_lines (it, 0);
9304 /* Move iterator IT past the end of the text line it is in. */
9306 void
9307 move_it_past_eol (struct it *it)
9309 enum move_it_result rc;
9311 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9312 if (rc == MOVE_NEWLINE_OR_CR)
9313 set_iterator_to_next (it, 0);
9317 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9318 negative means move up. DVPOS == 0 means move to the start of the
9319 screen line.
9321 Optimization idea: If we would know that IT->f doesn't use
9322 a face with proportional font, we could be faster for
9323 truncate-lines nil. */
9325 void
9326 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9329 /* The commented-out optimization uses vmotion on terminals. This
9330 gives bad results, because elements like it->what, on which
9331 callers such as pos_visible_p rely, aren't updated. */
9332 /* struct position pos;
9333 if (!FRAME_WINDOW_P (it->f))
9335 struct text_pos textpos;
9337 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9338 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9339 reseat (it, textpos, 1);
9340 it->vpos += pos.vpos;
9341 it->current_y += pos.vpos;
9343 else */
9345 if (dvpos == 0)
9347 /* DVPOS == 0 means move to the start of the screen line. */
9348 move_it_vertically_backward (it, 0);
9349 /* Let next call to line_bottom_y calculate real line height. */
9350 last_height = 0;
9352 else if (dvpos > 0)
9354 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9355 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9357 /* Only move to the next buffer position if we ended up in a
9358 string from display property, not in an overlay string
9359 (before-string or after-string). That is because the
9360 latter don't conceal the underlying buffer position, so
9361 we can ask to move the iterator to the exact position we
9362 are interested in. Note that, even if we are already at
9363 IT_CHARPOS (*it), the call below is not a no-op, as it
9364 will detect that we are at the end of the string, pop the
9365 iterator, and compute it->current_x and it->hpos
9366 correctly. */
9367 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9368 -1, -1, -1, MOVE_TO_POS);
9371 else
9373 struct it it2;
9374 void *it2data = NULL;
9375 ptrdiff_t start_charpos, i;
9376 int nchars_per_row
9377 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9378 ptrdiff_t pos_limit;
9380 /* Start at the beginning of the screen line containing IT's
9381 position. This may actually move vertically backwards,
9382 in case of overlays, so adjust dvpos accordingly. */
9383 dvpos += it->vpos;
9384 move_it_vertically_backward (it, 0);
9385 dvpos -= it->vpos;
9387 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9388 screen lines, and reseat the iterator there. */
9389 start_charpos = IT_CHARPOS (*it);
9390 if (it->line_wrap == TRUNCATE)
9391 pos_limit = BEGV;
9392 else
9393 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9394 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9395 back_to_previous_visible_line_start (it);
9396 reseat (it, it->current.pos, 1);
9398 /* Move further back if we end up in a string or an image. */
9399 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9401 /* First try to move to start of display line. */
9402 dvpos += it->vpos;
9403 move_it_vertically_backward (it, 0);
9404 dvpos -= it->vpos;
9405 if (IT_POS_VALID_AFTER_MOVE_P (it))
9406 break;
9407 /* If start of line is still in string or image,
9408 move further back. */
9409 back_to_previous_visible_line_start (it);
9410 reseat (it, it->current.pos, 1);
9411 dvpos--;
9414 it->current_x = it->hpos = 0;
9416 /* Above call may have moved too far if continuation lines
9417 are involved. Scan forward and see if it did. */
9418 SAVE_IT (it2, *it, it2data);
9419 it2.vpos = it2.current_y = 0;
9420 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9421 it->vpos -= it2.vpos;
9422 it->current_y -= it2.current_y;
9423 it->current_x = it->hpos = 0;
9425 /* If we moved too far back, move IT some lines forward. */
9426 if (it2.vpos > -dvpos)
9428 int delta = it2.vpos + dvpos;
9430 RESTORE_IT (&it2, &it2, it2data);
9431 SAVE_IT (it2, *it, it2data);
9432 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9433 /* Move back again if we got too far ahead. */
9434 if (IT_CHARPOS (*it) >= start_charpos)
9435 RESTORE_IT (it, &it2, it2data);
9436 else
9437 bidi_unshelve_cache (it2data, 1);
9439 else
9440 RESTORE_IT (it, it, it2data);
9444 /* Return 1 if IT points into the middle of a display vector. */
9447 in_display_vector_p (struct it *it)
9449 return (it->method == GET_FROM_DISPLAY_VECTOR
9450 && it->current.dpvec_index > 0
9451 && it->dpvec + it->current.dpvec_index != it->dpend);
9455 /***********************************************************************
9456 Messages
9457 ***********************************************************************/
9460 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9461 to *Messages*. */
9463 void
9464 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9466 Lisp_Object args[3];
9467 Lisp_Object msg, fmt;
9468 char *buffer;
9469 ptrdiff_t len;
9470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9471 USE_SAFE_ALLOCA;
9473 fmt = msg = Qnil;
9474 GCPRO4 (fmt, msg, arg1, arg2);
9476 args[0] = fmt = build_string (format);
9477 args[1] = arg1;
9478 args[2] = arg2;
9479 msg = Fformat (3, args);
9481 len = SBYTES (msg) + 1;
9482 buffer = SAFE_ALLOCA (len);
9483 memcpy (buffer, SDATA (msg), len);
9485 message_dolog (buffer, len - 1, 1, 0);
9486 SAFE_FREE ();
9488 UNGCPRO;
9492 /* Output a newline in the *Messages* buffer if "needs" one. */
9494 void
9495 message_log_maybe_newline (void)
9497 if (message_log_need_newline)
9498 message_dolog ("", 0, 1, 0);
9502 /* Add a string M of length NBYTES to the message log, optionally
9503 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9504 true, means interpret the contents of M as multibyte. This
9505 function calls low-level routines in order to bypass text property
9506 hooks, etc. which might not be safe to run.
9508 This may GC (insert may run before/after change hooks),
9509 so the buffer M must NOT point to a Lisp string. */
9511 void
9512 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9514 const unsigned char *msg = (const unsigned char *) m;
9516 if (!NILP (Vmemory_full))
9517 return;
9519 if (!NILP (Vmessage_log_max))
9521 struct buffer *oldbuf;
9522 Lisp_Object oldpoint, oldbegv, oldzv;
9523 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9524 ptrdiff_t point_at_end = 0;
9525 ptrdiff_t zv_at_end = 0;
9526 Lisp_Object old_deactivate_mark;
9527 struct gcpro gcpro1;
9529 old_deactivate_mark = Vdeactivate_mark;
9530 oldbuf = current_buffer;
9532 /* Ensure the Messages buffer exists, and switch to it.
9533 If we created it, set the major-mode. */
9535 int newbuffer = 0;
9536 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9538 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9540 if (newbuffer
9541 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9542 call0 (intern ("messages-buffer-mode"));
9545 bset_undo_list (current_buffer, Qt);
9546 bset_cache_long_scans (current_buffer, Qnil);
9548 oldpoint = message_dolog_marker1;
9549 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9550 oldbegv = message_dolog_marker2;
9551 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9552 oldzv = message_dolog_marker3;
9553 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9554 GCPRO1 (old_deactivate_mark);
9556 if (PT == Z)
9557 point_at_end = 1;
9558 if (ZV == Z)
9559 zv_at_end = 1;
9561 BEGV = BEG;
9562 BEGV_BYTE = BEG_BYTE;
9563 ZV = Z;
9564 ZV_BYTE = Z_BYTE;
9565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9567 /* Insert the string--maybe converting multibyte to single byte
9568 or vice versa, so that all the text fits the buffer. */
9569 if (multibyte
9570 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9572 ptrdiff_t i;
9573 int c, char_bytes;
9574 char work[1];
9576 /* Convert a multibyte string to single-byte
9577 for the *Message* buffer. */
9578 for (i = 0; i < nbytes; i += char_bytes)
9580 c = string_char_and_length (msg + i, &char_bytes);
9581 work[0] = (ASCII_CHAR_P (c)
9583 : multibyte_char_to_unibyte (c));
9584 insert_1_both (work, 1, 1, 1, 0, 0);
9587 else if (! multibyte
9588 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9590 ptrdiff_t i;
9591 int c, char_bytes;
9592 unsigned char str[MAX_MULTIBYTE_LENGTH];
9593 /* Convert a single-byte string to multibyte
9594 for the *Message* buffer. */
9595 for (i = 0; i < nbytes; i++)
9597 c = msg[i];
9598 MAKE_CHAR_MULTIBYTE (c);
9599 char_bytes = CHAR_STRING (c, str);
9600 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9603 else if (nbytes)
9604 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9606 if (nlflag)
9608 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9609 printmax_t dups;
9611 insert_1_both ("\n", 1, 1, 1, 0, 0);
9613 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9614 this_bol = PT;
9615 this_bol_byte = PT_BYTE;
9617 /* See if this line duplicates the previous one.
9618 If so, combine duplicates. */
9619 if (this_bol > BEG)
9621 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9622 prev_bol = PT;
9623 prev_bol_byte = PT_BYTE;
9625 dups = message_log_check_duplicate (prev_bol_byte,
9626 this_bol_byte);
9627 if (dups)
9629 del_range_both (prev_bol, prev_bol_byte,
9630 this_bol, this_bol_byte, 0);
9631 if (dups > 1)
9633 char dupstr[sizeof " [ times]"
9634 + INT_STRLEN_BOUND (printmax_t)];
9636 /* If you change this format, don't forget to also
9637 change message_log_check_duplicate. */
9638 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9639 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9640 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9645 /* If we have more than the desired maximum number of lines
9646 in the *Messages* buffer now, delete the oldest ones.
9647 This is safe because we don't have undo in this buffer. */
9649 if (NATNUMP (Vmessage_log_max))
9651 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9652 -XFASTINT (Vmessage_log_max) - 1, 0);
9653 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9656 BEGV = marker_position (oldbegv);
9657 BEGV_BYTE = marker_byte_position (oldbegv);
9659 if (zv_at_end)
9661 ZV = Z;
9662 ZV_BYTE = Z_BYTE;
9664 else
9666 ZV = marker_position (oldzv);
9667 ZV_BYTE = marker_byte_position (oldzv);
9670 if (point_at_end)
9671 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9672 else
9673 /* We can't do Fgoto_char (oldpoint) because it will run some
9674 Lisp code. */
9675 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9676 marker_byte_position (oldpoint));
9678 UNGCPRO;
9679 unchain_marker (XMARKER (oldpoint));
9680 unchain_marker (XMARKER (oldbegv));
9681 unchain_marker (XMARKER (oldzv));
9683 /* We called insert_1_both above with its 5th argument (PREPARE)
9684 zero, which prevents insert_1_both from calling
9685 prepare_to_modify_buffer, which in turns prevents us from
9686 incrementing windows_or_buffers_changed even if *Messages* is
9687 shown in some window. So we must manually set
9688 windows_or_buffers_changed here to make up for that. */
9689 windows_or_buffers_changed = old_windows_or_buffers_changed;
9690 bset_redisplay (current_buffer);
9692 set_buffer_internal (oldbuf);
9694 message_log_need_newline = !nlflag;
9695 Vdeactivate_mark = old_deactivate_mark;
9700 /* We are at the end of the buffer after just having inserted a newline.
9701 (Note: We depend on the fact we won't be crossing the gap.)
9702 Check to see if the most recent message looks a lot like the previous one.
9703 Return 0 if different, 1 if the new one should just replace it, or a
9704 value N > 1 if we should also append " [N times]". */
9706 static intmax_t
9707 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9709 ptrdiff_t i;
9710 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9711 int seen_dots = 0;
9712 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9713 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9715 for (i = 0; i < len; i++)
9717 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9718 seen_dots = 1;
9719 if (p1[i] != p2[i])
9720 return seen_dots;
9722 p1 += len;
9723 if (*p1 == '\n')
9724 return 2;
9725 if (*p1++ == ' ' && *p1++ == '[')
9727 char *pend;
9728 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9729 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9730 return n + 1;
9732 return 0;
9736 /* Display an echo area message M with a specified length of NBYTES
9737 bytes. The string may include null characters. If M is not a
9738 string, clear out any existing message, and let the mini-buffer
9739 text show through.
9741 This function cancels echoing. */
9743 void
9744 message3 (Lisp_Object m)
9746 struct gcpro gcpro1;
9748 GCPRO1 (m);
9749 clear_message (1,1);
9750 cancel_echoing ();
9752 /* First flush out any partial line written with print. */
9753 message_log_maybe_newline ();
9754 if (STRINGP (m))
9756 ptrdiff_t nbytes = SBYTES (m);
9757 bool multibyte = STRING_MULTIBYTE (m);
9758 USE_SAFE_ALLOCA;
9759 char *buffer = SAFE_ALLOCA (nbytes);
9760 memcpy (buffer, SDATA (m), nbytes);
9761 message_dolog (buffer, nbytes, 1, multibyte);
9762 SAFE_FREE ();
9764 message3_nolog (m);
9766 UNGCPRO;
9770 /* The non-logging version of message3.
9771 This does not cancel echoing, because it is used for echoing.
9772 Perhaps we need to make a separate function for echoing
9773 and make this cancel echoing. */
9775 void
9776 message3_nolog (Lisp_Object m)
9778 struct frame *sf = SELECTED_FRAME ();
9780 if (FRAME_INITIAL_P (sf))
9782 if (noninteractive_need_newline)
9783 putc ('\n', stderr);
9784 noninteractive_need_newline = 0;
9785 if (STRINGP (m))
9787 Lisp_Object s = ENCODE_SYSTEM (m);
9789 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9791 if (cursor_in_echo_area == 0)
9792 fprintf (stderr, "\n");
9793 fflush (stderr);
9795 /* Error messages get reported properly by cmd_error, so this must be just an
9796 informative message; if the frame hasn't really been initialized yet, just
9797 toss it. */
9798 else if (INTERACTIVE && sf->glyphs_initialized_p)
9800 /* Get the frame containing the mini-buffer
9801 that the selected frame is using. */
9802 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9803 Lisp_Object frame = XWINDOW (mini_window)->frame;
9804 struct frame *f = XFRAME (frame);
9806 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9807 Fmake_frame_visible (frame);
9809 if (STRINGP (m) && SCHARS (m) > 0)
9811 set_message (m);
9812 if (minibuffer_auto_raise)
9813 Fraise_frame (frame);
9814 /* Assume we are not echoing.
9815 (If we are, echo_now will override this.) */
9816 echo_message_buffer = Qnil;
9818 else
9819 clear_message (1, 1);
9821 do_pending_window_change (0);
9822 echo_area_display (1);
9823 do_pending_window_change (0);
9824 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9825 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9830 /* Display a null-terminated echo area message M. If M is 0, clear
9831 out any existing message, and let the mini-buffer text show through.
9833 The buffer M must continue to exist until after the echo area gets
9834 cleared or some other message gets displayed there. Do not pass
9835 text that is stored in a Lisp string. Do not pass text in a buffer
9836 that was alloca'd. */
9838 void
9839 message1 (const char *m)
9841 message3 (m ? build_unibyte_string (m) : Qnil);
9845 /* The non-logging counterpart of message1. */
9847 void
9848 message1_nolog (const char *m)
9850 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9853 /* Display a message M which contains a single %s
9854 which gets replaced with STRING. */
9856 void
9857 message_with_string (const char *m, Lisp_Object string, int log)
9859 CHECK_STRING (string);
9861 if (noninteractive)
9863 if (m)
9865 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9866 String whose data pointer might be passed to us in M. So
9867 we use a local copy. */
9868 char *fmt = xstrdup (m);
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9877 xfree (fmt);
9880 else if (INTERACTIVE)
9882 /* The frame whose minibuffer we're going to display the message on.
9883 It may be larger than the selected frame, so we need
9884 to use its buffer, not the selected frame's buffer. */
9885 Lisp_Object mini_window;
9886 struct frame *f, *sf = SELECTED_FRAME ();
9888 /* Get the frame containing the minibuffer
9889 that the selected frame is using. */
9890 mini_window = FRAME_MINIBUF_WINDOW (sf);
9891 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9893 /* Error messages get reported properly by cmd_error, so this must be
9894 just an informative message; if the frame hasn't really been
9895 initialized yet, just toss it. */
9896 if (f->glyphs_initialized_p)
9898 Lisp_Object args[2], msg;
9899 struct gcpro gcpro1, gcpro2;
9901 args[0] = build_string (m);
9902 args[1] = msg = string;
9903 GCPRO2 (args[0], msg);
9904 gcpro1.nvars = 2;
9906 msg = Fformat (2, args);
9908 if (log)
9909 message3 (msg);
9910 else
9911 message3_nolog (msg);
9913 UNGCPRO;
9915 /* Print should start at the beginning of the message
9916 buffer next time. */
9917 message_buf_print = 0;
9923 /* Dump an informative message to the minibuf. If M is 0, clear out
9924 any existing message, and let the mini-buffer text show through. */
9926 static void
9927 vmessage (const char *m, va_list ap)
9929 if (noninteractive)
9931 if (m)
9933 if (noninteractive_need_newline)
9934 putc ('\n', stderr);
9935 noninteractive_need_newline = 0;
9936 vfprintf (stderr, m, ap);
9937 if (cursor_in_echo_area == 0)
9938 fprintf (stderr, "\n");
9939 fflush (stderr);
9942 else if (INTERACTIVE)
9944 /* The frame whose mini-buffer we're going to display the message
9945 on. It may be larger than the selected frame, so we need to
9946 use its buffer, not the selected frame's buffer. */
9947 Lisp_Object mini_window;
9948 struct frame *f, *sf = SELECTED_FRAME ();
9950 /* Get the frame containing the mini-buffer
9951 that the selected frame is using. */
9952 mini_window = FRAME_MINIBUF_WINDOW (sf);
9953 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9955 /* Error messages get reported properly by cmd_error, so this must be
9956 just an informative message; if the frame hasn't really been
9957 initialized yet, just toss it. */
9958 if (f->glyphs_initialized_p)
9960 if (m)
9962 ptrdiff_t len;
9963 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9964 char *message_buf = alloca (maxsize + 1);
9966 len = doprnt (message_buf, maxsize, m, 0, ap);
9968 message3 (make_string (message_buf, len));
9970 else
9971 message1 (0);
9973 /* Print should start at the beginning of the message
9974 buffer next time. */
9975 message_buf_print = 0;
9980 void
9981 message (const char *m, ...)
9983 va_list ap;
9984 va_start (ap, m);
9985 vmessage (m, ap);
9986 va_end (ap);
9990 #if 0
9991 /* The non-logging version of message. */
9993 void
9994 message_nolog (const char *m, ...)
9996 Lisp_Object old_log_max;
9997 va_list ap;
9998 va_start (ap, m);
9999 old_log_max = Vmessage_log_max;
10000 Vmessage_log_max = Qnil;
10001 vmessage (m, ap);
10002 Vmessage_log_max = old_log_max;
10003 va_end (ap);
10005 #endif
10008 /* Display the current message in the current mini-buffer. This is
10009 only called from error handlers in process.c, and is not time
10010 critical. */
10012 void
10013 update_echo_area (void)
10015 if (!NILP (echo_area_buffer[0]))
10017 Lisp_Object string;
10018 string = Fcurrent_message ();
10019 message3 (string);
10024 /* Make sure echo area buffers in `echo_buffers' are live.
10025 If they aren't, make new ones. */
10027 static void
10028 ensure_echo_area_buffers (void)
10030 int i;
10032 for (i = 0; i < 2; ++i)
10033 if (!BUFFERP (echo_buffer[i])
10034 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10036 char name[30];
10037 Lisp_Object old_buffer;
10038 int j;
10040 old_buffer = echo_buffer[i];
10041 echo_buffer[i] = Fget_buffer_create
10042 (make_formatted_string (name, " *Echo Area %d*", i));
10043 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10044 /* to force word wrap in echo area -
10045 it was decided to postpone this*/
10046 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10048 for (j = 0; j < 2; ++j)
10049 if (EQ (old_buffer, echo_area_buffer[j]))
10050 echo_area_buffer[j] = echo_buffer[i];
10055 /* Call FN with args A1..A2 with either the current or last displayed
10056 echo_area_buffer as current buffer.
10058 WHICH zero means use the current message buffer
10059 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10060 from echo_buffer[] and clear it.
10062 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10063 suitable buffer from echo_buffer[] and clear it.
10065 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10066 that the current message becomes the last displayed one, make
10067 choose a suitable buffer for echo_area_buffer[0], and clear it.
10069 Value is what FN returns. */
10071 static int
10072 with_echo_area_buffer (struct window *w, int which,
10073 int (*fn) (ptrdiff_t, Lisp_Object),
10074 ptrdiff_t a1, Lisp_Object a2)
10076 Lisp_Object buffer;
10077 int this_one, the_other, clear_buffer_p, rc;
10078 ptrdiff_t count = SPECPDL_INDEX ();
10080 /* If buffers aren't live, make new ones. */
10081 ensure_echo_area_buffers ();
10083 clear_buffer_p = 0;
10085 if (which == 0)
10086 this_one = 0, the_other = 1;
10087 else if (which > 0)
10088 this_one = 1, the_other = 0;
10089 else
10091 this_one = 0, the_other = 1;
10092 clear_buffer_p = 1;
10094 /* We need a fresh one in case the current echo buffer equals
10095 the one containing the last displayed echo area message. */
10096 if (!NILP (echo_area_buffer[this_one])
10097 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10098 echo_area_buffer[this_one] = Qnil;
10101 /* Choose a suitable buffer from echo_buffer[] is we don't
10102 have one. */
10103 if (NILP (echo_area_buffer[this_one]))
10105 echo_area_buffer[this_one]
10106 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10107 ? echo_buffer[the_other]
10108 : echo_buffer[this_one]);
10109 clear_buffer_p = 1;
10112 buffer = echo_area_buffer[this_one];
10114 /* Don't get confused by reusing the buffer used for echoing
10115 for a different purpose. */
10116 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10117 cancel_echoing ();
10119 record_unwind_protect (unwind_with_echo_area_buffer,
10120 with_echo_area_buffer_unwind_data (w));
10122 /* Make the echo area buffer current. Note that for display
10123 purposes, it is not necessary that the displayed window's buffer
10124 == current_buffer, except for text property lookup. So, let's
10125 only set that buffer temporarily here without doing a full
10126 Fset_window_buffer. We must also change w->pointm, though,
10127 because otherwise an assertions in unshow_buffer fails, and Emacs
10128 aborts. */
10129 set_buffer_internal_1 (XBUFFER (buffer));
10130 if (w)
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10136 bset_undo_list (current_buffer, Qt);
10137 bset_read_only (current_buffer, Qnil);
10138 specbind (Qinhibit_read_only, Qt);
10139 specbind (Qinhibit_modification_hooks, Qt);
10141 if (clear_buffer_p && Z > BEG)
10142 del_range (BEG, Z);
10144 eassert (BEGV >= BEG);
10145 eassert (ZV <= Z && ZV >= BEGV);
10147 rc = fn (a1, a2);
10149 eassert (BEGV >= BEG);
10150 eassert (ZV <= Z && ZV >= BEGV);
10152 unbind_to (count, Qnil);
10153 return rc;
10157 /* Save state that should be preserved around the call to the function
10158 FN called in with_echo_area_buffer. */
10160 static Lisp_Object
10161 with_echo_area_buffer_unwind_data (struct window *w)
10163 int i = 0;
10164 Lisp_Object vector, tmp;
10166 /* Reduce consing by keeping one vector in
10167 Vwith_echo_area_save_vector. */
10168 vector = Vwith_echo_area_save_vector;
10169 Vwith_echo_area_save_vector = Qnil;
10171 if (NILP (vector))
10172 vector = Fmake_vector (make_number (9), Qnil);
10174 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10175 ASET (vector, i, Vdeactivate_mark); ++i;
10176 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10178 if (w)
10180 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10181 ASET (vector, i, w->contents); ++i;
10182 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10184 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10185 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10187 else
10189 int end = i + 6;
10190 for (; i < end; ++i)
10191 ASET (vector, i, Qnil);
10194 eassert (i == ASIZE (vector));
10195 return vector;
10199 /* Restore global state from VECTOR which was created by
10200 with_echo_area_buffer_unwind_data. */
10202 static void
10203 unwind_with_echo_area_buffer (Lisp_Object vector)
10205 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10206 Vdeactivate_mark = AREF (vector, 1);
10207 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10209 if (WINDOWP (AREF (vector, 3)))
10211 struct window *w;
10212 Lisp_Object buffer;
10214 w = XWINDOW (AREF (vector, 3));
10215 buffer = AREF (vector, 4);
10217 wset_buffer (w, buffer);
10218 set_marker_both (w->pointm, buffer,
10219 XFASTINT (AREF (vector, 5)),
10220 XFASTINT (AREF (vector, 6)));
10221 set_marker_both (w->start, buffer,
10222 XFASTINT (AREF (vector, 7)),
10223 XFASTINT (AREF (vector, 8)));
10226 Vwith_echo_area_save_vector = vector;
10230 /* Set up the echo area for use by print functions. MULTIBYTE_P
10231 non-zero means we will print multibyte. */
10233 void
10234 setup_echo_area_for_printing (int multibyte_p)
10236 /* If we can't find an echo area any more, exit. */
10237 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10238 Fkill_emacs (Qnil);
10240 ensure_echo_area_buffers ();
10242 if (!message_buf_print)
10244 /* A message has been output since the last time we printed.
10245 Choose a fresh echo area buffer. */
10246 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10247 echo_area_buffer[0] = echo_buffer[1];
10248 else
10249 echo_area_buffer[0] = echo_buffer[0];
10251 /* Switch to that buffer and clear it. */
10252 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10253 bset_truncate_lines (current_buffer, Qnil);
10255 if (Z > BEG)
10257 ptrdiff_t count = SPECPDL_INDEX ();
10258 specbind (Qinhibit_read_only, Qt);
10259 /* Note that undo recording is always disabled. */
10260 del_range (BEG, Z);
10261 unbind_to (count, Qnil);
10263 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10265 /* Set up the buffer for the multibyteness we need. */
10266 if (multibyte_p
10267 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10268 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10270 /* Raise the frame containing the echo area. */
10271 if (minibuffer_auto_raise)
10273 struct frame *sf = SELECTED_FRAME ();
10274 Lisp_Object mini_window;
10275 mini_window = FRAME_MINIBUF_WINDOW (sf);
10276 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10279 message_log_maybe_newline ();
10280 message_buf_print = 1;
10282 else
10284 if (NILP (echo_area_buffer[0]))
10286 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10287 echo_area_buffer[0] = echo_buffer[1];
10288 else
10289 echo_area_buffer[0] = echo_buffer[0];
10292 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10294 /* Someone switched buffers between print requests. */
10295 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10296 bset_truncate_lines (current_buffer, Qnil);
10302 /* Display an echo area message in window W. Value is non-zero if W's
10303 height is changed. If display_last_displayed_message_p is
10304 non-zero, display the message that was last displayed, otherwise
10305 display the current message. */
10307 static int
10308 display_echo_area (struct window *w)
10310 int i, no_message_p, window_height_changed_p;
10312 /* Temporarily disable garbage collections while displaying the echo
10313 area. This is done because a GC can print a message itself.
10314 That message would modify the echo area buffer's contents while a
10315 redisplay of the buffer is going on, and seriously confuse
10316 redisplay. */
10317 ptrdiff_t count = inhibit_garbage_collection ();
10319 /* If there is no message, we must call display_echo_area_1
10320 nevertheless because it resizes the window. But we will have to
10321 reset the echo_area_buffer in question to nil at the end because
10322 with_echo_area_buffer will sets it to an empty buffer. */
10323 i = display_last_displayed_message_p ? 1 : 0;
10324 no_message_p = NILP (echo_area_buffer[i]);
10326 window_height_changed_p
10327 = with_echo_area_buffer (w, display_last_displayed_message_p,
10328 display_echo_area_1,
10329 (intptr_t) w, Qnil);
10331 if (no_message_p)
10332 echo_area_buffer[i] = Qnil;
10334 unbind_to (count, Qnil);
10335 return window_height_changed_p;
10339 /* Helper for display_echo_area. Display the current buffer which
10340 contains the current echo area message in window W, a mini-window,
10341 a pointer to which is passed in A1. A2..A4 are currently not used.
10342 Change the height of W so that all of the message is displayed.
10343 Value is non-zero if height of W was changed. */
10345 static int
10346 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10348 intptr_t i1 = a1;
10349 struct window *w = (struct window *) i1;
10350 Lisp_Object window;
10351 struct text_pos start;
10352 int window_height_changed_p = 0;
10354 /* Do this before displaying, so that we have a large enough glyph
10355 matrix for the display. If we can't get enough space for the
10356 whole text, display the last N lines. That works by setting w->start. */
10357 window_height_changed_p = resize_mini_window (w, 0);
10359 /* Use the starting position chosen by resize_mini_window. */
10360 SET_TEXT_POS_FROM_MARKER (start, w->start);
10362 /* Display. */
10363 clear_glyph_matrix (w->desired_matrix);
10364 XSETWINDOW (window, w);
10365 try_window (window, start, 0);
10367 return window_height_changed_p;
10371 /* Resize the echo area window to exactly the size needed for the
10372 currently displayed message, if there is one. If a mini-buffer
10373 is active, don't shrink it. */
10375 void
10376 resize_echo_area_exactly (void)
10378 if (BUFFERP (echo_area_buffer[0])
10379 && WINDOWP (echo_area_window))
10381 struct window *w = XWINDOW (echo_area_window);
10382 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10383 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10384 (intptr_t) w, resize_exactly);
10385 if (resized_p)
10387 windows_or_buffers_changed = 42;
10388 update_mode_lines = 30;
10389 redisplay_internal ();
10395 /* Callback function for with_echo_area_buffer, when used from
10396 resize_echo_area_exactly. A1 contains a pointer to the window to
10397 resize, EXACTLY non-nil means resize the mini-window exactly to the
10398 size of the text displayed. A3 and A4 are not used. Value is what
10399 resize_mini_window returns. */
10401 static int
10402 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10404 intptr_t i1 = a1;
10405 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10409 /* Resize mini-window W to fit the size of its contents. EXACT_P
10410 means size the window exactly to the size needed. Otherwise, it's
10411 only enlarged until W's buffer is empty.
10413 Set W->start to the right place to begin display. If the whole
10414 contents fit, start at the beginning. Otherwise, start so as
10415 to make the end of the contents appear. This is particularly
10416 important for y-or-n-p, but seems desirable generally.
10418 Value is non-zero if the window height has been changed. */
10421 resize_mini_window (struct window *w, int exact_p)
10423 struct frame *f = XFRAME (w->frame);
10424 int window_height_changed_p = 0;
10426 eassert (MINI_WINDOW_P (w));
10428 /* By default, start display at the beginning. */
10429 set_marker_both (w->start, w->contents,
10430 BUF_BEGV (XBUFFER (w->contents)),
10431 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10433 /* Don't resize windows while redisplaying a window; it would
10434 confuse redisplay functions when the size of the window they are
10435 displaying changes from under them. Such a resizing can happen,
10436 for instance, when which-func prints a long message while
10437 we are running fontification-functions. We're running these
10438 functions with safe_call which binds inhibit-redisplay to t. */
10439 if (!NILP (Vinhibit_redisplay))
10440 return 0;
10442 /* Nil means don't try to resize. */
10443 if (NILP (Vresize_mini_windows)
10444 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10445 return 0;
10447 if (!FRAME_MINIBUF_ONLY_P (f))
10449 struct it it;
10450 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10451 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10452 int height;
10453 EMACS_INT max_height;
10454 int unit = FRAME_LINE_HEIGHT (f);
10455 struct text_pos start;
10456 struct buffer *old_current_buffer = NULL;
10458 if (current_buffer != XBUFFER (w->contents))
10460 old_current_buffer = current_buffer;
10461 set_buffer_internal (XBUFFER (w->contents));
10464 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10466 /* Compute the max. number of lines specified by the user. */
10467 if (FLOATP (Vmax_mini_window_height))
10468 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10469 else if (INTEGERP (Vmax_mini_window_height))
10470 max_height = XINT (Vmax_mini_window_height);
10471 else
10472 max_height = total_height / 4;
10474 /* Correct that max. height if it's bogus. */
10475 max_height = clip_to_bounds (1, max_height, total_height);
10477 /* Find out the height of the text in the window. */
10478 if (it.line_wrap == TRUNCATE)
10479 height = 1;
10480 else
10482 last_height = 0;
10483 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10484 if (it.max_ascent == 0 && it.max_descent == 0)
10485 height = it.current_y + last_height;
10486 else
10487 height = it.current_y + it.max_ascent + it.max_descent;
10488 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10489 height = (height + unit - 1) / unit;
10492 /* Compute a suitable window start. */
10493 if (height > max_height)
10495 height = max_height;
10496 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10497 move_it_vertically_backward (&it, (height - 1) * unit);
10498 start = it.current.pos;
10500 else
10501 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10502 SET_MARKER_FROM_TEXT_POS (w->start, start);
10504 if (EQ (Vresize_mini_windows, Qgrow_only))
10506 /* Let it grow only, until we display an empty message, in which
10507 case the window shrinks again. */
10508 if (height > WINDOW_TOTAL_LINES (w))
10510 int old_height = WINDOW_TOTAL_LINES (w);
10512 FRAME_WINDOWS_FROZEN (f) = 1;
10513 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10514 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10516 else if (height < WINDOW_TOTAL_LINES (w)
10517 && (exact_p || BEGV == ZV))
10519 int old_height = WINDOW_TOTAL_LINES (w);
10521 FRAME_WINDOWS_FROZEN (f) = 0;
10522 shrink_mini_window (w);
10523 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10526 else
10528 /* Always resize to exact size needed. */
10529 if (height > WINDOW_TOTAL_LINES (w))
10531 int old_height = WINDOW_TOTAL_LINES (w);
10533 FRAME_WINDOWS_FROZEN (f) = 1;
10534 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10535 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10537 else if (height < WINDOW_TOTAL_LINES (w))
10539 int old_height = WINDOW_TOTAL_LINES (w);
10541 FRAME_WINDOWS_FROZEN (f) = 0;
10542 shrink_mini_window (w);
10544 if (height)
10546 FRAME_WINDOWS_FROZEN (f) = 1;
10547 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10550 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10554 if (old_current_buffer)
10555 set_buffer_internal (old_current_buffer);
10558 return window_height_changed_p;
10562 /* Value is the current message, a string, or nil if there is no
10563 current message. */
10565 Lisp_Object
10566 current_message (void)
10568 Lisp_Object msg;
10570 if (!BUFFERP (echo_area_buffer[0]))
10571 msg = Qnil;
10572 else
10574 with_echo_area_buffer (0, 0, current_message_1,
10575 (intptr_t) &msg, Qnil);
10576 if (NILP (msg))
10577 echo_area_buffer[0] = Qnil;
10580 return msg;
10584 static int
10585 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10587 intptr_t i1 = a1;
10588 Lisp_Object *msg = (Lisp_Object *) i1;
10590 if (Z > BEG)
10591 *msg = make_buffer_string (BEG, Z, 1);
10592 else
10593 *msg = Qnil;
10594 return 0;
10598 /* Push the current message on Vmessage_stack for later restoration
10599 by restore_message. Value is non-zero if the current message isn't
10600 empty. This is a relatively infrequent operation, so it's not
10601 worth optimizing. */
10603 bool
10604 push_message (void)
10606 Lisp_Object msg = current_message ();
10607 Vmessage_stack = Fcons (msg, Vmessage_stack);
10608 return STRINGP (msg);
10612 /* Restore message display from the top of Vmessage_stack. */
10614 void
10615 restore_message (void)
10617 eassert (CONSP (Vmessage_stack));
10618 message3_nolog (XCAR (Vmessage_stack));
10622 /* Handler for unwind-protect calling pop_message. */
10624 void
10625 pop_message_unwind (void)
10627 /* Pop the top-most entry off Vmessage_stack. */
10628 eassert (CONSP (Vmessage_stack));
10629 Vmessage_stack = XCDR (Vmessage_stack);
10633 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10634 exits. If the stack is not empty, we have a missing pop_message
10635 somewhere. */
10637 void
10638 check_message_stack (void)
10640 if (!NILP (Vmessage_stack))
10641 emacs_abort ();
10645 /* Truncate to NCHARS what will be displayed in the echo area the next
10646 time we display it---but don't redisplay it now. */
10648 void
10649 truncate_echo_area (ptrdiff_t nchars)
10651 if (nchars == 0)
10652 echo_area_buffer[0] = Qnil;
10653 else if (!noninteractive
10654 && INTERACTIVE
10655 && !NILP (echo_area_buffer[0]))
10657 struct frame *sf = SELECTED_FRAME ();
10658 /* Error messages get reported properly by cmd_error, so this must be
10659 just an informative message; if the frame hasn't really been
10660 initialized yet, just toss it. */
10661 if (sf->glyphs_initialized_p)
10662 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10667 /* Helper function for truncate_echo_area. Truncate the current
10668 message to at most NCHARS characters. */
10670 static int
10671 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10673 if (BEG + nchars < Z)
10674 del_range (BEG + nchars, Z);
10675 if (Z == BEG)
10676 echo_area_buffer[0] = Qnil;
10677 return 0;
10680 /* Set the current message to STRING. */
10682 static void
10683 set_message (Lisp_Object string)
10685 eassert (STRINGP (string));
10687 message_enable_multibyte = STRING_MULTIBYTE (string);
10689 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10690 message_buf_print = 0;
10691 help_echo_showing_p = 0;
10693 if (STRINGP (Vdebug_on_message)
10694 && STRINGP (string)
10695 && fast_string_match (Vdebug_on_message, string) >= 0)
10696 call_debugger (list2 (Qerror, string));
10700 /* Helper function for set_message. First argument is ignored and second
10701 argument has the same meaning as for set_message.
10702 This function is called with the echo area buffer being current. */
10704 static int
10705 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10707 eassert (STRINGP (string));
10709 /* Change multibyteness of the echo buffer appropriately. */
10710 if (message_enable_multibyte
10711 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10712 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10714 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10715 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10716 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10718 /* Insert new message at BEG. */
10719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10721 /* This function takes care of single/multibyte conversion.
10722 We just have to ensure that the echo area buffer has the right
10723 setting of enable_multibyte_characters. */
10724 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10726 return 0;
10730 /* Clear messages. CURRENT_P non-zero means clear the current
10731 message. LAST_DISPLAYED_P non-zero means clear the message
10732 last displayed. */
10734 void
10735 clear_message (int current_p, int last_displayed_p)
10737 if (current_p)
10739 echo_area_buffer[0] = Qnil;
10740 message_cleared_p = 1;
10743 if (last_displayed_p)
10744 echo_area_buffer[1] = Qnil;
10746 message_buf_print = 0;
10749 /* Clear garbaged frames.
10751 This function is used where the old redisplay called
10752 redraw_garbaged_frames which in turn called redraw_frame which in
10753 turn called clear_frame. The call to clear_frame was a source of
10754 flickering. I believe a clear_frame is not necessary. It should
10755 suffice in the new redisplay to invalidate all current matrices,
10756 and ensure a complete redisplay of all windows. */
10758 static void
10759 clear_garbaged_frames (void)
10761 if (frame_garbaged)
10763 Lisp_Object tail, frame;
10765 FOR_EACH_FRAME (tail, frame)
10767 struct frame *f = XFRAME (frame);
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10771 if (f->resized_p)
10772 redraw_frame (f);
10773 else
10774 clear_current_matrices (f);
10775 fset_redisplay (f);
10776 f->garbaged = false;
10777 f->resized_p = false;
10781 frame_garbaged = false;
10786 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10787 is non-zero update selected_frame. Value is non-zero if the
10788 mini-windows height has been changed. */
10790 static int
10791 echo_area_display (int update_frame_p)
10793 Lisp_Object mini_window;
10794 struct window *w;
10795 struct frame *f;
10796 int window_height_changed_p = 0;
10797 struct frame *sf = SELECTED_FRAME ();
10799 mini_window = FRAME_MINIBUF_WINDOW (sf);
10800 w = XWINDOW (mini_window);
10801 f = XFRAME (WINDOW_FRAME (w));
10803 /* Don't display if frame is invisible or not yet initialized. */
10804 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10805 return 0;
10807 #ifdef HAVE_WINDOW_SYSTEM
10808 /* When Emacs starts, selected_frame may be the initial terminal
10809 frame. If we let this through, a message would be displayed on
10810 the terminal. */
10811 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10812 return 0;
10813 #endif /* HAVE_WINDOW_SYSTEM */
10815 /* Redraw garbaged frames. */
10816 clear_garbaged_frames ();
10818 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10820 echo_area_window = mini_window;
10821 window_height_changed_p = display_echo_area (w);
10822 w->must_be_updated_p = 1;
10824 /* Update the display, unless called from redisplay_internal.
10825 Also don't update the screen during redisplay itself. The
10826 update will happen at the end of redisplay, and an update
10827 here could cause confusion. */
10828 if (update_frame_p && !redisplaying_p)
10830 int n = 0;
10832 /* If the display update has been interrupted by pending
10833 input, update mode lines in the frame. Due to the
10834 pending input, it might have been that redisplay hasn't
10835 been called, so that mode lines above the echo area are
10836 garbaged. This looks odd, so we prevent it here. */
10837 if (!display_completed)
10838 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10840 if (window_height_changed_p
10841 /* Don't do this if Emacs is shutting down. Redisplay
10842 needs to run hooks. */
10843 && !NILP (Vrun_hooks))
10845 /* Must update other windows. Likewise as in other
10846 cases, don't let this update be interrupted by
10847 pending input. */
10848 ptrdiff_t count = SPECPDL_INDEX ();
10849 specbind (Qredisplay_dont_pause, Qt);
10850 windows_or_buffers_changed = 44;
10851 redisplay_internal ();
10852 unbind_to (count, Qnil);
10854 else if (FRAME_WINDOW_P (f) && n == 0)
10856 /* Window configuration is the same as before.
10857 Can do with a display update of the echo area,
10858 unless we displayed some mode lines. */
10859 update_single_window (w, 1);
10860 flush_frame (f);
10862 else
10863 update_frame (f, 1, 1);
10865 /* If cursor is in the echo area, make sure that the next
10866 redisplay displays the minibuffer, so that the cursor will
10867 be replaced with what the minibuffer wants. */
10868 if (cursor_in_echo_area)
10869 wset_redisplay (XWINDOW (mini_window));
10872 else if (!EQ (mini_window, selected_window))
10873 wset_redisplay (XWINDOW (mini_window));
10875 /* Last displayed message is now the current message. */
10876 echo_area_buffer[1] = echo_area_buffer[0];
10877 /* Inform read_char that we're not echoing. */
10878 echo_message_buffer = Qnil;
10880 /* Prevent redisplay optimization in redisplay_internal by resetting
10881 this_line_start_pos. This is done because the mini-buffer now
10882 displays the message instead of its buffer text. */
10883 if (EQ (mini_window, selected_window))
10884 CHARPOS (this_line_start_pos) = 0;
10886 return window_height_changed_p;
10889 /* Nonzero if W's buffer was changed but not saved. */
10891 static int
10892 window_buffer_changed (struct window *w)
10894 struct buffer *b = XBUFFER (w->contents);
10896 eassert (BUFFER_LIVE_P (b));
10898 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10901 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10903 static int
10904 mode_line_update_needed (struct window *w)
10906 return (w->column_number_displayed != -1
10907 && !(PT == w->last_point && !window_outdated (w))
10908 && (w->column_number_displayed != current_column ()));
10911 /* Nonzero if window start of W is frozen and may not be changed during
10912 redisplay. */
10914 static bool
10915 window_frozen_p (struct window *w)
10917 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10919 Lisp_Object window;
10921 XSETWINDOW (window, w);
10922 if (MINI_WINDOW_P (w))
10923 return 0;
10924 else if (EQ (window, selected_window))
10925 return 0;
10926 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10927 && EQ (window, Vminibuf_scroll_window))
10928 /* This special window can't be frozen too. */
10929 return 0;
10930 else
10931 return 1;
10933 return 0;
10936 /***********************************************************************
10937 Mode Lines and Frame Titles
10938 ***********************************************************************/
10940 /* A buffer for constructing non-propertized mode-line strings and
10941 frame titles in it; allocated from the heap in init_xdisp and
10942 resized as needed in store_mode_line_noprop_char. */
10944 static char *mode_line_noprop_buf;
10946 /* The buffer's end, and a current output position in it. */
10948 static char *mode_line_noprop_buf_end;
10949 static char *mode_line_noprop_ptr;
10951 #define MODE_LINE_NOPROP_LEN(start) \
10952 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10954 static enum {
10955 MODE_LINE_DISPLAY = 0,
10956 MODE_LINE_TITLE,
10957 MODE_LINE_NOPROP,
10958 MODE_LINE_STRING
10959 } mode_line_target;
10961 /* Alist that caches the results of :propertize.
10962 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10963 static Lisp_Object mode_line_proptrans_alist;
10965 /* List of strings making up the mode-line. */
10966 static Lisp_Object mode_line_string_list;
10968 /* Base face property when building propertized mode line string. */
10969 static Lisp_Object mode_line_string_face;
10970 static Lisp_Object mode_line_string_face_prop;
10973 /* Unwind data for mode line strings */
10975 static Lisp_Object Vmode_line_unwind_vector;
10977 static Lisp_Object
10978 format_mode_line_unwind_data (struct frame *target_frame,
10979 struct buffer *obuf,
10980 Lisp_Object owin,
10981 int save_proptrans)
10983 Lisp_Object vector, tmp;
10985 /* Reduce consing by keeping one vector in
10986 Vwith_echo_area_save_vector. */
10987 vector = Vmode_line_unwind_vector;
10988 Vmode_line_unwind_vector = Qnil;
10990 if (NILP (vector))
10991 vector = Fmake_vector (make_number (10), Qnil);
10993 ASET (vector, 0, make_number (mode_line_target));
10994 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10995 ASET (vector, 2, mode_line_string_list);
10996 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10997 ASET (vector, 4, mode_line_string_face);
10998 ASET (vector, 5, mode_line_string_face_prop);
11000 if (obuf)
11001 XSETBUFFER (tmp, obuf);
11002 else
11003 tmp = Qnil;
11004 ASET (vector, 6, tmp);
11005 ASET (vector, 7, owin);
11006 if (target_frame)
11008 /* Similarly to `with-selected-window', if the operation selects
11009 a window on another frame, we must restore that frame's
11010 selected window, and (for a tty) the top-frame. */
11011 ASET (vector, 8, target_frame->selected_window);
11012 if (FRAME_TERMCAP_P (target_frame))
11013 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11016 return vector;
11019 static void
11020 unwind_format_mode_line (Lisp_Object vector)
11022 Lisp_Object old_window = AREF (vector, 7);
11023 Lisp_Object target_frame_window = AREF (vector, 8);
11024 Lisp_Object old_top_frame = AREF (vector, 9);
11026 mode_line_target = XINT (AREF (vector, 0));
11027 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11028 mode_line_string_list = AREF (vector, 2);
11029 if (! EQ (AREF (vector, 3), Qt))
11030 mode_line_proptrans_alist = AREF (vector, 3);
11031 mode_line_string_face = AREF (vector, 4);
11032 mode_line_string_face_prop = AREF (vector, 5);
11034 /* Select window before buffer, since it may change the buffer. */
11035 if (!NILP (old_window))
11037 /* If the operation that we are unwinding had selected a window
11038 on a different frame, reset its frame-selected-window. For a
11039 text terminal, reset its top-frame if necessary. */
11040 if (!NILP (target_frame_window))
11042 Lisp_Object frame
11043 = WINDOW_FRAME (XWINDOW (target_frame_window));
11045 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11046 Fselect_window (target_frame_window, Qt);
11048 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11049 Fselect_frame (old_top_frame, Qt);
11052 Fselect_window (old_window, Qt);
11055 if (!NILP (AREF (vector, 6)))
11057 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11058 ASET (vector, 6, Qnil);
11061 Vmode_line_unwind_vector = vector;
11065 /* Store a single character C for the frame title in mode_line_noprop_buf.
11066 Re-allocate mode_line_noprop_buf if necessary. */
11068 static void
11069 store_mode_line_noprop_char (char c)
11071 /* If output position has reached the end of the allocated buffer,
11072 increase the buffer's size. */
11073 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11075 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11076 ptrdiff_t size = len;
11077 mode_line_noprop_buf =
11078 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11079 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11080 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11083 *mode_line_noprop_ptr++ = c;
11087 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11088 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11089 characters that yield more columns than PRECISION; PRECISION <= 0
11090 means copy the whole string. Pad with spaces until FIELD_WIDTH
11091 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11092 pad. Called from display_mode_element when it is used to build a
11093 frame title. */
11095 static int
11096 store_mode_line_noprop (const char *string, int field_width, int precision)
11098 const unsigned char *str = (const unsigned char *) string;
11099 int n = 0;
11100 ptrdiff_t dummy, nbytes;
11102 /* Copy at most PRECISION chars from STR. */
11103 nbytes = strlen (string);
11104 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11105 while (nbytes--)
11106 store_mode_line_noprop_char (*str++);
11108 /* Fill up with spaces until FIELD_WIDTH reached. */
11109 while (field_width > 0
11110 && n < field_width)
11112 store_mode_line_noprop_char (' ');
11113 ++n;
11116 return n;
11119 /***********************************************************************
11120 Frame Titles
11121 ***********************************************************************/
11123 #ifdef HAVE_WINDOW_SYSTEM
11125 /* Set the title of FRAME, if it has changed. The title format is
11126 Vicon_title_format if FRAME is iconified, otherwise it is
11127 frame_title_format. */
11129 static void
11130 x_consider_frame_title (Lisp_Object frame)
11132 struct frame *f = XFRAME (frame);
11134 if (FRAME_WINDOW_P (f)
11135 || FRAME_MINIBUF_ONLY_P (f)
11136 || f->explicit_name)
11138 /* Do we have more than one visible frame on this X display? */
11139 Lisp_Object tail, other_frame, fmt;
11140 ptrdiff_t title_start;
11141 char *title;
11142 ptrdiff_t len;
11143 struct it it;
11144 ptrdiff_t count = SPECPDL_INDEX ();
11146 FOR_EACH_FRAME (tail, other_frame)
11148 struct frame *tf = XFRAME (other_frame);
11150 if (tf != f
11151 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11152 && !FRAME_MINIBUF_ONLY_P (tf)
11153 && !EQ (other_frame, tip_frame)
11154 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11155 break;
11158 /* Set global variable indicating that multiple frames exist. */
11159 multiple_frames = CONSP (tail);
11161 /* Switch to the buffer of selected window of the frame. Set up
11162 mode_line_target so that display_mode_element will output into
11163 mode_line_noprop_buf; then display the title. */
11164 record_unwind_protect (unwind_format_mode_line,
11165 format_mode_line_unwind_data
11166 (f, current_buffer, selected_window, 0));
11168 Fselect_window (f->selected_window, Qt);
11169 set_buffer_internal_1
11170 (XBUFFER (XWINDOW (f->selected_window)->contents));
11171 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11173 mode_line_target = MODE_LINE_TITLE;
11174 title_start = MODE_LINE_NOPROP_LEN (0);
11175 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11176 NULL, DEFAULT_FACE_ID);
11177 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11178 len = MODE_LINE_NOPROP_LEN (title_start);
11179 title = mode_line_noprop_buf + title_start;
11180 unbind_to (count, Qnil);
11182 /* Set the title only if it's changed. This avoids consing in
11183 the common case where it hasn't. (If it turns out that we've
11184 already wasted too much time by walking through the list with
11185 display_mode_element, then we might need to optimize at a
11186 higher level than this.) */
11187 if (! STRINGP (f->name)
11188 || SBYTES (f->name) != len
11189 || memcmp (title, SDATA (f->name), len) != 0)
11190 x_implicitly_set_name (f, make_string (title, len), Qnil);
11194 #endif /* not HAVE_WINDOW_SYSTEM */
11197 /***********************************************************************
11198 Menu Bars
11199 ***********************************************************************/
11202 /* Prepare for redisplay by updating menu-bar item lists when
11203 appropriate. This can call eval. */
11205 static void
11206 prepare_menu_bars (void)
11208 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11209 bool some_windows = ((windows_or_buffers_changed == 0
11210 || windows_or_buffers_changed == REDISPLAY_SOME)
11211 && (update_mode_lines == 0
11212 || update_mode_lines == REDISPLAY_SOME));
11213 struct gcpro gcpro1, gcpro2;
11214 Lisp_Object tooltip_frame;
11216 #ifdef HAVE_WINDOW_SYSTEM
11217 tooltip_frame = tip_frame;
11218 #else
11219 tooltip_frame = Qnil;
11220 #endif
11222 if (FUNCTIONP (Vpre_redisplay_function))
11224 Lisp_Object windows = all_windows ? Qt : Qnil;
11225 if (all_windows && some_windows)
11227 Lisp_Object ws = window_list ();
11228 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11230 Lisp_Object this = XCAR (ws);
11231 struct window *w = XWINDOW (this);
11232 if (w->redisplay
11233 || XFRAME (w->frame)->redisplay
11234 || XBUFFER (w->contents)->text->redisplay)
11236 windows = Fcons (this, windows);
11240 safe_call1 (Vpre_redisplay_function, windows);
11243 /* Update all frame titles based on their buffer names, etc. We do
11244 this before the menu bars so that the buffer-menu will show the
11245 up-to-date frame titles. */
11246 #ifdef HAVE_WINDOW_SYSTEM
11247 if (all_windows)
11249 Lisp_Object tail, frame;
11251 FOR_EACH_FRAME (tail, frame)
11253 struct frame *f = XFRAME (frame);
11254 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11255 if (some_windows
11256 && !f->redisplay
11257 && !w->redisplay
11258 && !XBUFFER (w->contents)->text->redisplay)
11259 continue;
11261 if (!EQ (frame, tooltip_frame)
11262 && (FRAME_ICONIFIED_P (f)
11263 || FRAME_VISIBLE_P (f) == 1
11264 /* Exclude TTY frames that are obscured because they
11265 are not the top frame on their console. This is
11266 because x_consider_frame_title actually switches
11267 to the frame, which for TTY frames means it is
11268 marked as garbaged, and will be completely
11269 redrawn on the next redisplay cycle. This causes
11270 TTY frames to be completely redrawn, when there
11271 are more than one of them, even though nothing
11272 should be changed on display. */
11273 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11274 x_consider_frame_title (frame);
11277 #endif /* HAVE_WINDOW_SYSTEM */
11279 /* Update the menu bar item lists, if appropriate. This has to be
11280 done before any actual redisplay or generation of display lines. */
11282 if (all_windows)
11284 Lisp_Object tail, frame;
11285 ptrdiff_t count = SPECPDL_INDEX ();
11286 /* 1 means that update_menu_bar has run its hooks
11287 so any further calls to update_menu_bar shouldn't do so again. */
11288 int menu_bar_hooks_run = 0;
11290 record_unwind_save_match_data ();
11292 FOR_EACH_FRAME (tail, frame)
11294 struct frame *f = XFRAME (frame);
11295 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11297 /* Ignore tooltip frame. */
11298 if (EQ (frame, tooltip_frame))
11299 continue;
11301 if (some_windows
11302 && !f->redisplay
11303 && !w->redisplay
11304 && !XBUFFER (w->contents)->text->redisplay)
11305 continue;
11307 /* If a window on this frame changed size, report that to
11308 the user and clear the size-change flag. */
11309 if (FRAME_WINDOW_SIZES_CHANGED (f))
11311 Lisp_Object functions;
11313 /* Clear flag first in case we get an error below. */
11314 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11315 functions = Vwindow_size_change_functions;
11316 GCPRO2 (tail, functions);
11318 while (CONSP (functions))
11320 if (!EQ (XCAR (functions), Qt))
11321 call1 (XCAR (functions), frame);
11322 functions = XCDR (functions);
11324 UNGCPRO;
11327 GCPRO1 (tail);
11328 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11329 #ifdef HAVE_WINDOW_SYSTEM
11330 update_tool_bar (f, 0);
11331 #endif
11332 #ifdef HAVE_NS
11333 if (windows_or_buffers_changed
11334 && FRAME_NS_P (f))
11335 ns_set_doc_edited
11336 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11337 #endif
11338 UNGCPRO;
11341 unbind_to (count, Qnil);
11343 else
11345 struct frame *sf = SELECTED_FRAME ();
11346 update_menu_bar (sf, 1, 0);
11347 #ifdef HAVE_WINDOW_SYSTEM
11348 update_tool_bar (sf, 1);
11349 #endif
11354 /* Update the menu bar item list for frame F. This has to be done
11355 before we start to fill in any display lines, because it can call
11356 eval.
11358 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11360 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11361 already ran the menu bar hooks for this redisplay, so there
11362 is no need to run them again. The return value is the
11363 updated value of this flag, to pass to the next call. */
11365 static int
11366 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11368 Lisp_Object window;
11369 register struct window *w;
11371 /* If called recursively during a menu update, do nothing. This can
11372 happen when, for instance, an activate-menubar-hook causes a
11373 redisplay. */
11374 if (inhibit_menubar_update)
11375 return hooks_run;
11377 window = FRAME_SELECTED_WINDOW (f);
11378 w = XWINDOW (window);
11380 if (FRAME_WINDOW_P (f)
11382 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11383 || defined (HAVE_NS) || defined (USE_GTK)
11384 FRAME_EXTERNAL_MENU_BAR (f)
11385 #else
11386 FRAME_MENU_BAR_LINES (f) > 0
11387 #endif
11388 : FRAME_MENU_BAR_LINES (f) > 0)
11390 /* If the user has switched buffers or windows, we need to
11391 recompute to reflect the new bindings. But we'll
11392 recompute when update_mode_lines is set too; that means
11393 that people can use force-mode-line-update to request
11394 that the menu bar be recomputed. The adverse effect on
11395 the rest of the redisplay algorithm is about the same as
11396 windows_or_buffers_changed anyway. */
11397 if (windows_or_buffers_changed
11398 /* This used to test w->update_mode_line, but we believe
11399 there is no need to recompute the menu in that case. */
11400 || update_mode_lines
11401 || window_buffer_changed (w))
11403 struct buffer *prev = current_buffer;
11404 ptrdiff_t count = SPECPDL_INDEX ();
11406 specbind (Qinhibit_menubar_update, Qt);
11408 set_buffer_internal_1 (XBUFFER (w->contents));
11409 if (save_match_data)
11410 record_unwind_save_match_data ();
11411 if (NILP (Voverriding_local_map_menu_flag))
11413 specbind (Qoverriding_terminal_local_map, Qnil);
11414 specbind (Qoverriding_local_map, Qnil);
11417 if (!hooks_run)
11419 /* Run the Lucid hook. */
11420 safe_run_hooks (Qactivate_menubar_hook);
11422 /* If it has changed current-menubar from previous value,
11423 really recompute the menu-bar from the value. */
11424 if (! NILP (Vlucid_menu_bar_dirty_flag))
11425 call0 (Qrecompute_lucid_menubar);
11427 safe_run_hooks (Qmenu_bar_update_hook);
11429 hooks_run = 1;
11432 XSETFRAME (Vmenu_updating_frame, f);
11433 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11435 /* Redisplay the menu bar in case we changed it. */
11436 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11437 || defined (HAVE_NS) || defined (USE_GTK)
11438 if (FRAME_WINDOW_P (f))
11440 #if defined (HAVE_NS)
11441 /* All frames on Mac OS share the same menubar. So only
11442 the selected frame should be allowed to set it. */
11443 if (f == SELECTED_FRAME ())
11444 #endif
11445 set_frame_menubar (f, 0, 0);
11447 else
11448 /* On a terminal screen, the menu bar is an ordinary screen
11449 line, and this makes it get updated. */
11450 w->update_mode_line = 1;
11451 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11452 /* In the non-toolkit version, the menu bar is an ordinary screen
11453 line, and this makes it get updated. */
11454 w->update_mode_line = 1;
11455 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11457 unbind_to (count, Qnil);
11458 set_buffer_internal_1 (prev);
11462 return hooks_run;
11465 /***********************************************************************
11466 Tool-bars
11467 ***********************************************************************/
11469 #ifdef HAVE_WINDOW_SYSTEM
11471 /* Tool-bar item index of the item on which a mouse button was pressed
11472 or -1. */
11474 int last_tool_bar_item;
11476 /* Select `frame' temporarily without running all the code in
11477 do_switch_frame.
11478 FIXME: Maybe do_switch_frame should be trimmed down similarly
11479 when `norecord' is set. */
11480 static void
11481 fast_set_selected_frame (Lisp_Object frame)
11483 if (!EQ (selected_frame, frame))
11485 selected_frame = frame;
11486 selected_window = XFRAME (frame)->selected_window;
11490 /* Update the tool-bar item list for frame F. This has to be done
11491 before we start to fill in any display lines. Called from
11492 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11493 and restore it here. */
11495 static void
11496 update_tool_bar (struct frame *f, int save_match_data)
11498 #if defined (USE_GTK) || defined (HAVE_NS)
11499 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11500 #else
11501 int do_update = WINDOWP (f->tool_bar_window)
11502 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11503 #endif
11505 if (do_update)
11507 Lisp_Object window;
11508 struct window *w;
11510 window = FRAME_SELECTED_WINDOW (f);
11511 w = XWINDOW (window);
11513 /* If the user has switched buffers or windows, we need to
11514 recompute to reflect the new bindings. But we'll
11515 recompute when update_mode_lines is set too; that means
11516 that people can use force-mode-line-update to request
11517 that the menu bar be recomputed. The adverse effect on
11518 the rest of the redisplay algorithm is about the same as
11519 windows_or_buffers_changed anyway. */
11520 if (windows_or_buffers_changed
11521 || w->update_mode_line
11522 || update_mode_lines
11523 || window_buffer_changed (w))
11525 struct buffer *prev = current_buffer;
11526 ptrdiff_t count = SPECPDL_INDEX ();
11527 Lisp_Object frame, new_tool_bar;
11528 int new_n_tool_bar;
11529 struct gcpro gcpro1;
11531 /* Set current_buffer to the buffer of the selected
11532 window of the frame, so that we get the right local
11533 keymaps. */
11534 set_buffer_internal_1 (XBUFFER (w->contents));
11536 /* Save match data, if we must. */
11537 if (save_match_data)
11538 record_unwind_save_match_data ();
11540 /* Make sure that we don't accidentally use bogus keymaps. */
11541 if (NILP (Voverriding_local_map_menu_flag))
11543 specbind (Qoverriding_terminal_local_map, Qnil);
11544 specbind (Qoverriding_local_map, Qnil);
11547 GCPRO1 (new_tool_bar);
11549 /* We must temporarily set the selected frame to this frame
11550 before calling tool_bar_items, because the calculation of
11551 the tool-bar keymap uses the selected frame (see
11552 `tool-bar-make-keymap' in tool-bar.el). */
11553 eassert (EQ (selected_window,
11554 /* Since we only explicitly preserve selected_frame,
11555 check that selected_window would be redundant. */
11556 XFRAME (selected_frame)->selected_window));
11557 record_unwind_protect (fast_set_selected_frame, selected_frame);
11558 XSETFRAME (frame, f);
11559 fast_set_selected_frame (frame);
11561 /* Build desired tool-bar items from keymaps. */
11562 new_tool_bar
11563 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11564 &new_n_tool_bar);
11566 /* Redisplay the tool-bar if we changed it. */
11567 if (new_n_tool_bar != f->n_tool_bar_items
11568 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11570 /* Redisplay that happens asynchronously due to an expose event
11571 may access f->tool_bar_items. Make sure we update both
11572 variables within BLOCK_INPUT so no such event interrupts. */
11573 block_input ();
11574 fset_tool_bar_items (f, new_tool_bar);
11575 f->n_tool_bar_items = new_n_tool_bar;
11576 w->update_mode_line = 1;
11577 unblock_input ();
11580 UNGCPRO;
11582 unbind_to (count, Qnil);
11583 set_buffer_internal_1 (prev);
11588 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11590 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11591 F's desired tool-bar contents. F->tool_bar_items must have
11592 been set up previously by calling prepare_menu_bars. */
11594 static void
11595 build_desired_tool_bar_string (struct frame *f)
11597 int i, size, size_needed;
11598 struct gcpro gcpro1, gcpro2, gcpro3;
11599 Lisp_Object image, plist, props;
11601 image = plist = props = Qnil;
11602 GCPRO3 (image, plist, props);
11604 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11605 Otherwise, make a new string. */
11607 /* The size of the string we might be able to reuse. */
11608 size = (STRINGP (f->desired_tool_bar_string)
11609 ? SCHARS (f->desired_tool_bar_string)
11610 : 0);
11612 /* We need one space in the string for each image. */
11613 size_needed = f->n_tool_bar_items;
11615 /* Reuse f->desired_tool_bar_string, if possible. */
11616 if (size < size_needed || NILP (f->desired_tool_bar_string))
11617 fset_desired_tool_bar_string
11618 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11619 else
11621 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11622 Fremove_text_properties (make_number (0), make_number (size),
11623 props, f->desired_tool_bar_string);
11626 /* Put a `display' property on the string for the images to display,
11627 put a `menu_item' property on tool-bar items with a value that
11628 is the index of the item in F's tool-bar item vector. */
11629 for (i = 0; i < f->n_tool_bar_items; ++i)
11631 #define PROP(IDX) \
11632 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11634 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11635 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11636 int hmargin, vmargin, relief, idx, end;
11638 /* If image is a vector, choose the image according to the
11639 button state. */
11640 image = PROP (TOOL_BAR_ITEM_IMAGES);
11641 if (VECTORP (image))
11643 if (enabled_p)
11644 idx = (selected_p
11645 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11646 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11647 else
11648 idx = (selected_p
11649 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11650 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11652 eassert (ASIZE (image) >= idx);
11653 image = AREF (image, idx);
11655 else
11656 idx = -1;
11658 /* Ignore invalid image specifications. */
11659 if (!valid_image_p (image))
11660 continue;
11662 /* Display the tool-bar button pressed, or depressed. */
11663 plist = Fcopy_sequence (XCDR (image));
11665 /* Compute margin and relief to draw. */
11666 relief = (tool_bar_button_relief >= 0
11667 ? tool_bar_button_relief
11668 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11669 hmargin = vmargin = relief;
11671 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11672 INT_MAX - max (hmargin, vmargin)))
11674 hmargin += XFASTINT (Vtool_bar_button_margin);
11675 vmargin += XFASTINT (Vtool_bar_button_margin);
11677 else if (CONSP (Vtool_bar_button_margin))
11679 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11680 INT_MAX - hmargin))
11681 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11683 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11684 INT_MAX - vmargin))
11685 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11688 if (auto_raise_tool_bar_buttons_p)
11690 /* Add a `:relief' property to the image spec if the item is
11691 selected. */
11692 if (selected_p)
11694 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11695 hmargin -= relief;
11696 vmargin -= relief;
11699 else
11701 /* If image is selected, display it pressed, i.e. with a
11702 negative relief. If it's not selected, display it with a
11703 raised relief. */
11704 plist = Fplist_put (plist, QCrelief,
11705 (selected_p
11706 ? make_number (-relief)
11707 : make_number (relief)));
11708 hmargin -= relief;
11709 vmargin -= relief;
11712 /* Put a margin around the image. */
11713 if (hmargin || vmargin)
11715 if (hmargin == vmargin)
11716 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11717 else
11718 plist = Fplist_put (plist, QCmargin,
11719 Fcons (make_number (hmargin),
11720 make_number (vmargin)));
11723 /* If button is not enabled, and we don't have special images
11724 for the disabled state, make the image appear disabled by
11725 applying an appropriate algorithm to it. */
11726 if (!enabled_p && idx < 0)
11727 plist = Fplist_put (plist, QCconversion, Qdisabled);
11729 /* Put a `display' text property on the string for the image to
11730 display. Put a `menu-item' property on the string that gives
11731 the start of this item's properties in the tool-bar items
11732 vector. */
11733 image = Fcons (Qimage, plist);
11734 props = list4 (Qdisplay, image,
11735 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11737 /* Let the last image hide all remaining spaces in the tool bar
11738 string. The string can be longer than needed when we reuse a
11739 previous string. */
11740 if (i + 1 == f->n_tool_bar_items)
11741 end = SCHARS (f->desired_tool_bar_string);
11742 else
11743 end = i + 1;
11744 Fadd_text_properties (make_number (i), make_number (end),
11745 props, f->desired_tool_bar_string);
11746 #undef PROP
11749 UNGCPRO;
11753 /* Display one line of the tool-bar of frame IT->f.
11755 HEIGHT specifies the desired height of the tool-bar line.
11756 If the actual height of the glyph row is less than HEIGHT, the
11757 row's height is increased to HEIGHT, and the icons are centered
11758 vertically in the new height.
11760 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11761 count a final empty row in case the tool-bar width exactly matches
11762 the window width.
11765 static void
11766 display_tool_bar_line (struct it *it, int height)
11768 struct glyph_row *row = it->glyph_row;
11769 int max_x = it->last_visible_x;
11770 struct glyph *last;
11772 prepare_desired_row (row);
11773 row->y = it->current_y;
11775 /* Note that this isn't made use of if the face hasn't a box,
11776 so there's no need to check the face here. */
11777 it->start_of_box_run_p = 1;
11779 while (it->current_x < max_x)
11781 int x, n_glyphs_before, i, nglyphs;
11782 struct it it_before;
11784 /* Get the next display element. */
11785 if (!get_next_display_element (it))
11787 /* Don't count empty row if we are counting needed tool-bar lines. */
11788 if (height < 0 && !it->hpos)
11789 return;
11790 break;
11793 /* Produce glyphs. */
11794 n_glyphs_before = row->used[TEXT_AREA];
11795 it_before = *it;
11797 PRODUCE_GLYPHS (it);
11799 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11800 i = 0;
11801 x = it_before.current_x;
11802 while (i < nglyphs)
11804 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11806 if (x + glyph->pixel_width > max_x)
11808 /* Glyph doesn't fit on line. Backtrack. */
11809 row->used[TEXT_AREA] = n_glyphs_before;
11810 *it = it_before;
11811 /* If this is the only glyph on this line, it will never fit on the
11812 tool-bar, so skip it. But ensure there is at least one glyph,
11813 so we don't accidentally disable the tool-bar. */
11814 if (n_glyphs_before == 0
11815 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11816 break;
11817 goto out;
11820 ++it->hpos;
11821 x += glyph->pixel_width;
11822 ++i;
11825 /* Stop at line end. */
11826 if (ITERATOR_AT_END_OF_LINE_P (it))
11827 break;
11829 set_iterator_to_next (it, 1);
11832 out:;
11834 row->displays_text_p = row->used[TEXT_AREA] != 0;
11836 /* Use default face for the border below the tool bar.
11838 FIXME: When auto-resize-tool-bars is grow-only, there is
11839 no additional border below the possibly empty tool-bar lines.
11840 So to make the extra empty lines look "normal", we have to
11841 use the tool-bar face for the border too. */
11842 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11843 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11844 it->face_id = DEFAULT_FACE_ID;
11846 extend_face_to_end_of_line (it);
11847 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11848 last->right_box_line_p = 1;
11849 if (last == row->glyphs[TEXT_AREA])
11850 last->left_box_line_p = 1;
11852 /* Make line the desired height and center it vertically. */
11853 if ((height -= it->max_ascent + it->max_descent) > 0)
11855 /* Don't add more than one line height. */
11856 height %= FRAME_LINE_HEIGHT (it->f);
11857 it->max_ascent += height / 2;
11858 it->max_descent += (height + 1) / 2;
11861 compute_line_metrics (it);
11863 /* If line is empty, make it occupy the rest of the tool-bar. */
11864 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11866 row->height = row->phys_height = it->last_visible_y - row->y;
11867 row->visible_height = row->height;
11868 row->ascent = row->phys_ascent = 0;
11869 row->extra_line_spacing = 0;
11872 row->full_width_p = 1;
11873 row->continued_p = 0;
11874 row->truncated_on_left_p = 0;
11875 row->truncated_on_right_p = 0;
11877 it->current_x = it->hpos = 0;
11878 it->current_y += row->height;
11879 ++it->vpos;
11880 ++it->glyph_row;
11884 /* Max tool-bar height. */
11886 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11887 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11889 /* Value is the number of screen lines needed to make all tool-bar
11890 items of frame F visible. The number of actual rows needed is
11891 returned in *N_ROWS if non-NULL. */
11893 static int
11894 tool_bar_lines_needed (struct frame *f, int *n_rows)
11896 struct window *w = XWINDOW (f->tool_bar_window);
11897 struct it it;
11898 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11899 the desired matrix, so use (unused) mode-line row as temporary row to
11900 avoid destroying the first tool-bar row. */
11901 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11903 /* Initialize an iterator for iteration over
11904 F->desired_tool_bar_string in the tool-bar window of frame F. */
11905 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11906 it.first_visible_x = 0;
11907 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11908 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11909 it.paragraph_embedding = L2R;
11911 while (!ITERATOR_AT_END_P (&it))
11913 clear_glyph_row (temp_row);
11914 it.glyph_row = temp_row;
11915 display_tool_bar_line (&it, -1);
11917 clear_glyph_row (temp_row);
11919 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11920 if (n_rows)
11921 *n_rows = it.vpos > 0 ? it.vpos : -1;
11923 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11926 #endif /* !USE_GTK && !HAVE_NS */
11928 #if defined USE_GTK || defined HAVE_NS
11929 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11930 #endif
11932 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11933 0, 1, 0,
11934 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11935 If FRAME is nil or omitted, use the selected frame. */)
11936 (Lisp_Object frame)
11938 int nlines = 0;
11939 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11940 struct frame *f = decode_any_frame (frame);
11941 struct window *w;
11943 if (WINDOWP (f->tool_bar_window)
11944 && (w = XWINDOW (f->tool_bar_window),
11945 WINDOW_TOTAL_LINES (w) > 0))
11947 update_tool_bar (f, 1);
11948 if (f->n_tool_bar_items)
11950 build_desired_tool_bar_string (f);
11951 nlines = tool_bar_lines_needed (f, NULL);
11954 #endif
11955 return make_number (nlines);
11959 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11960 height should be changed. */
11962 static int
11963 redisplay_tool_bar (struct frame *f)
11965 #if defined (USE_GTK) || defined (HAVE_NS)
11967 if (FRAME_EXTERNAL_TOOL_BAR (f))
11968 update_frame_tool_bar (f);
11969 return 0;
11971 #else /* !USE_GTK && !HAVE_NS */
11973 struct window *w;
11974 struct it it;
11975 struct glyph_row *row;
11977 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11978 do anything. This means you must start with tool-bar-lines
11979 non-zero to get the auto-sizing effect. Or in other words, you
11980 can turn off tool-bars by specifying tool-bar-lines zero. */
11981 if (!WINDOWP (f->tool_bar_window)
11982 || (w = XWINDOW (f->tool_bar_window),
11983 WINDOW_TOTAL_LINES (w) == 0))
11984 return 0;
11986 /* Set up an iterator for the tool-bar window. */
11987 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11988 it.first_visible_x = 0;
11989 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11990 row = it.glyph_row;
11992 /* Build a string that represents the contents of the tool-bar. */
11993 build_desired_tool_bar_string (f);
11994 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11995 /* FIXME: This should be controlled by a user option. But it
11996 doesn't make sense to have an R2L tool bar if the menu bar cannot
11997 be drawn also R2L, and making the menu bar R2L is tricky due
11998 toolkit-specific code that implements it. If an R2L tool bar is
11999 ever supported, display_tool_bar_line should also be augmented to
12000 call unproduce_glyphs like display_line and display_string
12001 do. */
12002 it.paragraph_embedding = L2R;
12004 if (f->n_tool_bar_rows == 0)
12006 int nlines;
12008 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12009 nlines != WINDOW_TOTAL_LINES (w)))
12011 Lisp_Object frame;
12012 int old_height = WINDOW_TOTAL_LINES (w);
12014 XSETFRAME (frame, f);
12015 Fmodify_frame_parameters (frame,
12016 list1 (Fcons (Qtool_bar_lines,
12017 make_number (nlines))));
12018 if (WINDOW_TOTAL_LINES (w) != old_height)
12020 clear_glyph_matrix (w->desired_matrix);
12021 f->fonts_changed = 1;
12022 return 1;
12027 /* Display as many lines as needed to display all tool-bar items. */
12029 if (f->n_tool_bar_rows > 0)
12031 int border, rows, height, extra;
12033 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12034 border = XINT (Vtool_bar_border);
12035 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12036 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12037 else if (EQ (Vtool_bar_border, Qborder_width))
12038 border = f->border_width;
12039 else
12040 border = 0;
12041 if (border < 0)
12042 border = 0;
12044 rows = f->n_tool_bar_rows;
12045 height = max (1, (it.last_visible_y - border) / rows);
12046 extra = it.last_visible_y - border - height * rows;
12048 while (it.current_y < it.last_visible_y)
12050 int h = 0;
12051 if (extra > 0 && rows-- > 0)
12053 h = (extra + rows - 1) / rows;
12054 extra -= h;
12056 display_tool_bar_line (&it, height + h);
12059 else
12061 while (it.current_y < it.last_visible_y)
12062 display_tool_bar_line (&it, 0);
12065 /* It doesn't make much sense to try scrolling in the tool-bar
12066 window, so don't do it. */
12067 w->desired_matrix->no_scrolling_p = 1;
12068 w->must_be_updated_p = 1;
12070 if (!NILP (Vauto_resize_tool_bars))
12072 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12073 int change_height_p = 0;
12075 /* If we couldn't display everything, change the tool-bar's
12076 height if there is room for more. */
12077 if (IT_STRING_CHARPOS (it) < it.end_charpos
12078 && it.current_y < max_tool_bar_height)
12079 change_height_p = 1;
12081 row = it.glyph_row - 1;
12083 /* If there are blank lines at the end, except for a partially
12084 visible blank line at the end that is smaller than
12085 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12086 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12087 && row->height >= FRAME_LINE_HEIGHT (f))
12088 change_height_p = 1;
12090 /* If row displays tool-bar items, but is partially visible,
12091 change the tool-bar's height. */
12092 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12093 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12094 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12095 change_height_p = 1;
12097 /* Resize windows as needed by changing the `tool-bar-lines'
12098 frame parameter. */
12099 if (change_height_p)
12101 Lisp_Object frame;
12102 int old_height = WINDOW_TOTAL_LINES (w);
12103 int nrows;
12104 int nlines = tool_bar_lines_needed (f, &nrows);
12106 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12107 && !f->minimize_tool_bar_window_p)
12108 ? (nlines > old_height)
12109 : (nlines != old_height));
12110 f->minimize_tool_bar_window_p = 0;
12112 if (change_height_p)
12114 XSETFRAME (frame, f);
12115 Fmodify_frame_parameters (frame,
12116 list1 (Fcons (Qtool_bar_lines,
12117 make_number (nlines))));
12118 if (WINDOW_TOTAL_LINES (w) != old_height)
12120 clear_glyph_matrix (w->desired_matrix);
12121 f->n_tool_bar_rows = nrows;
12122 f->fonts_changed = 1;
12123 return 1;
12129 f->minimize_tool_bar_window_p = 0;
12130 return 0;
12132 #endif /* USE_GTK || HAVE_NS */
12135 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12137 /* Get information about the tool-bar item which is displayed in GLYPH
12138 on frame F. Return in *PROP_IDX the index where tool-bar item
12139 properties start in F->tool_bar_items. Value is zero if
12140 GLYPH doesn't display a tool-bar item. */
12142 static int
12143 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12145 Lisp_Object prop;
12146 int success_p;
12147 int charpos;
12149 /* This function can be called asynchronously, which means we must
12150 exclude any possibility that Fget_text_property signals an
12151 error. */
12152 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12153 charpos = max (0, charpos);
12155 /* Get the text property `menu-item' at pos. The value of that
12156 property is the start index of this item's properties in
12157 F->tool_bar_items. */
12158 prop = Fget_text_property (make_number (charpos),
12159 Qmenu_item, f->current_tool_bar_string);
12160 if (INTEGERP (prop))
12162 *prop_idx = XINT (prop);
12163 success_p = 1;
12165 else
12166 success_p = 0;
12168 return success_p;
12172 /* Get information about the tool-bar item at position X/Y on frame F.
12173 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12174 the current matrix of the tool-bar window of F, or NULL if not
12175 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12176 item in F->tool_bar_items. Value is
12178 -1 if X/Y is not on a tool-bar item
12179 0 if X/Y is on the same item that was highlighted before.
12180 1 otherwise. */
12182 static int
12183 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12184 int *hpos, int *vpos, int *prop_idx)
12186 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12187 struct window *w = XWINDOW (f->tool_bar_window);
12188 int area;
12190 /* Find the glyph under X/Y. */
12191 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12192 if (*glyph == NULL)
12193 return -1;
12195 /* Get the start of this tool-bar item's properties in
12196 f->tool_bar_items. */
12197 if (!tool_bar_item_info (f, *glyph, prop_idx))
12198 return -1;
12200 /* Is mouse on the highlighted item? */
12201 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12202 && *vpos >= hlinfo->mouse_face_beg_row
12203 && *vpos <= hlinfo->mouse_face_end_row
12204 && (*vpos > hlinfo->mouse_face_beg_row
12205 || *hpos >= hlinfo->mouse_face_beg_col)
12206 && (*vpos < hlinfo->mouse_face_end_row
12207 || *hpos < hlinfo->mouse_face_end_col
12208 || hlinfo->mouse_face_past_end))
12209 return 0;
12211 return 1;
12215 /* EXPORT:
12216 Handle mouse button event on the tool-bar of frame F, at
12217 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12218 0 for button release. MODIFIERS is event modifiers for button
12219 release. */
12221 void
12222 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12223 int modifiers)
12225 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12226 struct window *w = XWINDOW (f->tool_bar_window);
12227 int hpos, vpos, prop_idx;
12228 struct glyph *glyph;
12229 Lisp_Object enabled_p;
12230 int ts;
12232 /* If not on the highlighted tool-bar item, and mouse-highlight is
12233 non-nil, return. This is so we generate the tool-bar button
12234 click only when the mouse button is released on the same item as
12235 where it was pressed. However, when mouse-highlight is disabled,
12236 generate the click when the button is released regardless of the
12237 highlight, since tool-bar items are not highlighted in that
12238 case. */
12239 frame_to_window_pixel_xy (w, &x, &y);
12240 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12241 if (ts == -1
12242 || (ts != 0 && !NILP (Vmouse_highlight)))
12243 return;
12245 /* When mouse-highlight is off, generate the click for the item
12246 where the button was pressed, disregarding where it was
12247 released. */
12248 if (NILP (Vmouse_highlight) && !down_p)
12249 prop_idx = last_tool_bar_item;
12251 /* If item is disabled, do nothing. */
12252 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12253 if (NILP (enabled_p))
12254 return;
12256 if (down_p)
12258 /* Show item in pressed state. */
12259 if (!NILP (Vmouse_highlight))
12260 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12261 last_tool_bar_item = prop_idx;
12263 else
12265 Lisp_Object key, frame;
12266 struct input_event event;
12267 EVENT_INIT (event);
12269 /* Show item in released state. */
12270 if (!NILP (Vmouse_highlight))
12271 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12273 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12275 XSETFRAME (frame, f);
12276 event.kind = TOOL_BAR_EVENT;
12277 event.frame_or_window = frame;
12278 event.arg = frame;
12279 kbd_buffer_store_event (&event);
12281 event.kind = TOOL_BAR_EVENT;
12282 event.frame_or_window = frame;
12283 event.arg = key;
12284 event.modifiers = modifiers;
12285 kbd_buffer_store_event (&event);
12286 last_tool_bar_item = -1;
12291 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12292 tool-bar window-relative coordinates X/Y. Called from
12293 note_mouse_highlight. */
12295 static void
12296 note_tool_bar_highlight (struct frame *f, int x, int y)
12298 Lisp_Object window = f->tool_bar_window;
12299 struct window *w = XWINDOW (window);
12300 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12301 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12302 int hpos, vpos;
12303 struct glyph *glyph;
12304 struct glyph_row *row;
12305 int i;
12306 Lisp_Object enabled_p;
12307 int prop_idx;
12308 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12309 int mouse_down_p, rc;
12311 /* Function note_mouse_highlight is called with negative X/Y
12312 values when mouse moves outside of the frame. */
12313 if (x <= 0 || y <= 0)
12315 clear_mouse_face (hlinfo);
12316 return;
12319 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12320 if (rc < 0)
12322 /* Not on tool-bar item. */
12323 clear_mouse_face (hlinfo);
12324 return;
12326 else if (rc == 0)
12327 /* On same tool-bar item as before. */
12328 goto set_help_echo;
12330 clear_mouse_face (hlinfo);
12332 /* Mouse is down, but on different tool-bar item? */
12333 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12334 && f == dpyinfo->last_mouse_frame);
12336 if (mouse_down_p
12337 && last_tool_bar_item != prop_idx)
12338 return;
12340 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12342 /* If tool-bar item is not enabled, don't highlight it. */
12343 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12344 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12346 /* Compute the x-position of the glyph. In front and past the
12347 image is a space. We include this in the highlighted area. */
12348 row = MATRIX_ROW (w->current_matrix, vpos);
12349 for (i = x = 0; i < hpos; ++i)
12350 x += row->glyphs[TEXT_AREA][i].pixel_width;
12352 /* Record this as the current active region. */
12353 hlinfo->mouse_face_beg_col = hpos;
12354 hlinfo->mouse_face_beg_row = vpos;
12355 hlinfo->mouse_face_beg_x = x;
12356 hlinfo->mouse_face_past_end = 0;
12358 hlinfo->mouse_face_end_col = hpos + 1;
12359 hlinfo->mouse_face_end_row = vpos;
12360 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12361 hlinfo->mouse_face_window = window;
12362 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12364 /* Display it as active. */
12365 show_mouse_face (hlinfo, draw);
12368 set_help_echo:
12370 /* Set help_echo_string to a help string to display for this tool-bar item.
12371 XTread_socket does the rest. */
12372 help_echo_object = help_echo_window = Qnil;
12373 help_echo_pos = -1;
12374 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12375 if (NILP (help_echo_string))
12376 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12379 #endif /* !USE_GTK && !HAVE_NS */
12381 #endif /* HAVE_WINDOW_SYSTEM */
12385 /************************************************************************
12386 Horizontal scrolling
12387 ************************************************************************/
12389 static int hscroll_window_tree (Lisp_Object);
12390 static int hscroll_windows (Lisp_Object);
12392 /* For all leaf windows in the window tree rooted at WINDOW, set their
12393 hscroll value so that PT is (i) visible in the window, and (ii) so
12394 that it is not within a certain margin at the window's left and
12395 right border. Value is non-zero if any window's hscroll has been
12396 changed. */
12398 static int
12399 hscroll_window_tree (Lisp_Object window)
12401 int hscrolled_p = 0;
12402 int hscroll_relative_p = FLOATP (Vhscroll_step);
12403 int hscroll_step_abs = 0;
12404 double hscroll_step_rel = 0;
12406 if (hscroll_relative_p)
12408 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12409 if (hscroll_step_rel < 0)
12411 hscroll_relative_p = 0;
12412 hscroll_step_abs = 0;
12415 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12417 hscroll_step_abs = XINT (Vhscroll_step);
12418 if (hscroll_step_abs < 0)
12419 hscroll_step_abs = 0;
12421 else
12422 hscroll_step_abs = 0;
12424 while (WINDOWP (window))
12426 struct window *w = XWINDOW (window);
12428 if (WINDOWP (w->contents))
12429 hscrolled_p |= hscroll_window_tree (w->contents);
12430 else if (w->cursor.vpos >= 0)
12432 int h_margin;
12433 int text_area_width;
12434 struct glyph_row *current_cursor_row
12435 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12436 struct glyph_row *desired_cursor_row
12437 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12438 struct glyph_row *cursor_row
12439 = (desired_cursor_row->enabled_p
12440 ? desired_cursor_row
12441 : current_cursor_row);
12442 int row_r2l_p = cursor_row->reversed_p;
12444 text_area_width = window_box_width (w, TEXT_AREA);
12446 /* Scroll when cursor is inside this scroll margin. */
12447 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12449 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12450 /* For left-to-right rows, hscroll when cursor is either
12451 (i) inside the right hscroll margin, or (ii) if it is
12452 inside the left margin and the window is already
12453 hscrolled. */
12454 && ((!row_r2l_p
12455 && ((w->hscroll
12456 && w->cursor.x <= h_margin)
12457 || (cursor_row->enabled_p
12458 && cursor_row->truncated_on_right_p
12459 && (w->cursor.x >= text_area_width - h_margin))))
12460 /* For right-to-left rows, the logic is similar,
12461 except that rules for scrolling to left and right
12462 are reversed. E.g., if cursor.x <= h_margin, we
12463 need to hscroll "to the right" unconditionally,
12464 and that will scroll the screen to the left so as
12465 to reveal the next portion of the row. */
12466 || (row_r2l_p
12467 && ((cursor_row->enabled_p
12468 /* FIXME: It is confusing to set the
12469 truncated_on_right_p flag when R2L rows
12470 are actually truncated on the left. */
12471 && cursor_row->truncated_on_right_p
12472 && w->cursor.x <= h_margin)
12473 || (w->hscroll
12474 && (w->cursor.x >= text_area_width - h_margin))))))
12476 struct it it;
12477 ptrdiff_t hscroll;
12478 struct buffer *saved_current_buffer;
12479 ptrdiff_t pt;
12480 int wanted_x;
12482 /* Find point in a display of infinite width. */
12483 saved_current_buffer = current_buffer;
12484 current_buffer = XBUFFER (w->contents);
12486 if (w == XWINDOW (selected_window))
12487 pt = PT;
12488 else
12489 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12491 /* Move iterator to pt starting at cursor_row->start in
12492 a line with infinite width. */
12493 init_to_row_start (&it, w, cursor_row);
12494 it.last_visible_x = INFINITY;
12495 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12496 current_buffer = saved_current_buffer;
12498 /* Position cursor in window. */
12499 if (!hscroll_relative_p && hscroll_step_abs == 0)
12500 hscroll = max (0, (it.current_x
12501 - (ITERATOR_AT_END_OF_LINE_P (&it)
12502 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12503 : (text_area_width / 2))))
12504 / FRAME_COLUMN_WIDTH (it.f);
12505 else if ((!row_r2l_p
12506 && w->cursor.x >= text_area_width - h_margin)
12507 || (row_r2l_p && w->cursor.x <= h_margin))
12509 if (hscroll_relative_p)
12510 wanted_x = text_area_width * (1 - hscroll_step_rel)
12511 - h_margin;
12512 else
12513 wanted_x = text_area_width
12514 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12515 - h_margin;
12516 hscroll
12517 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12519 else
12521 if (hscroll_relative_p)
12522 wanted_x = text_area_width * hscroll_step_rel
12523 + h_margin;
12524 else
12525 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12526 + h_margin;
12527 hscroll
12528 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12530 hscroll = max (hscroll, w->min_hscroll);
12532 /* Don't prevent redisplay optimizations if hscroll
12533 hasn't changed, as it will unnecessarily slow down
12534 redisplay. */
12535 if (w->hscroll != hscroll)
12537 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12538 w->hscroll = hscroll;
12539 hscrolled_p = 1;
12544 window = w->next;
12547 /* Value is non-zero if hscroll of any leaf window has been changed. */
12548 return hscrolled_p;
12552 /* Set hscroll so that cursor is visible and not inside horizontal
12553 scroll margins for all windows in the tree rooted at WINDOW. See
12554 also hscroll_window_tree above. Value is non-zero if any window's
12555 hscroll has been changed. If it has, desired matrices on the frame
12556 of WINDOW are cleared. */
12558 static int
12559 hscroll_windows (Lisp_Object window)
12561 int hscrolled_p = hscroll_window_tree (window);
12562 if (hscrolled_p)
12563 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12564 return hscrolled_p;
12569 /************************************************************************
12570 Redisplay
12571 ************************************************************************/
12573 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12574 to a non-zero value. This is sometimes handy to have in a debugger
12575 session. */
12577 #ifdef GLYPH_DEBUG
12579 /* First and last unchanged row for try_window_id. */
12581 static int debug_first_unchanged_at_end_vpos;
12582 static int debug_last_unchanged_at_beg_vpos;
12584 /* Delta vpos and y. */
12586 static int debug_dvpos, debug_dy;
12588 /* Delta in characters and bytes for try_window_id. */
12590 static ptrdiff_t debug_delta, debug_delta_bytes;
12592 /* Values of window_end_pos and window_end_vpos at the end of
12593 try_window_id. */
12595 static ptrdiff_t debug_end_vpos;
12597 /* Append a string to W->desired_matrix->method. FMT is a printf
12598 format string. If trace_redisplay_p is non-zero also printf the
12599 resulting string to stderr. */
12601 static void debug_method_add (struct window *, char const *, ...)
12602 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12604 static void
12605 debug_method_add (struct window *w, char const *fmt, ...)
12607 void *ptr = w;
12608 char *method = w->desired_matrix->method;
12609 int len = strlen (method);
12610 int size = sizeof w->desired_matrix->method;
12611 int remaining = size - len - 1;
12612 va_list ap;
12614 if (len && remaining)
12616 method[len] = '|';
12617 --remaining, ++len;
12620 va_start (ap, fmt);
12621 vsnprintf (method + len, remaining + 1, fmt, ap);
12622 va_end (ap);
12624 if (trace_redisplay_p)
12625 fprintf (stderr, "%p (%s): %s\n",
12626 ptr,
12627 ((BUFFERP (w->contents)
12628 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12629 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12630 : "no buffer"),
12631 method + len);
12634 #endif /* GLYPH_DEBUG */
12637 /* Value is non-zero if all changes in window W, which displays
12638 current_buffer, are in the text between START and END. START is a
12639 buffer position, END is given as a distance from Z. Used in
12640 redisplay_internal for display optimization. */
12642 static int
12643 text_outside_line_unchanged_p (struct window *w,
12644 ptrdiff_t start, ptrdiff_t end)
12646 int unchanged_p = 1;
12648 /* If text or overlays have changed, see where. */
12649 if (window_outdated (w))
12651 /* Gap in the line? */
12652 if (GPT < start || Z - GPT < end)
12653 unchanged_p = 0;
12655 /* Changes start in front of the line, or end after it? */
12656 if (unchanged_p
12657 && (BEG_UNCHANGED < start - 1
12658 || END_UNCHANGED < end))
12659 unchanged_p = 0;
12661 /* If selective display, can't optimize if changes start at the
12662 beginning of the line. */
12663 if (unchanged_p
12664 && INTEGERP (BVAR (current_buffer, selective_display))
12665 && XINT (BVAR (current_buffer, selective_display)) > 0
12666 && (BEG_UNCHANGED < start || GPT <= start))
12667 unchanged_p = 0;
12669 /* If there are overlays at the start or end of the line, these
12670 may have overlay strings with newlines in them. A change at
12671 START, for instance, may actually concern the display of such
12672 overlay strings as well, and they are displayed on different
12673 lines. So, quickly rule out this case. (For the future, it
12674 might be desirable to implement something more telling than
12675 just BEG/END_UNCHANGED.) */
12676 if (unchanged_p)
12678 if (BEG + BEG_UNCHANGED == start
12679 && overlay_touches_p (start))
12680 unchanged_p = 0;
12681 if (END_UNCHANGED == end
12682 && overlay_touches_p (Z - end))
12683 unchanged_p = 0;
12686 /* Under bidi reordering, adding or deleting a character in the
12687 beginning of a paragraph, before the first strong directional
12688 character, can change the base direction of the paragraph (unless
12689 the buffer specifies a fixed paragraph direction), which will
12690 require to redisplay the whole paragraph. It might be worthwhile
12691 to find the paragraph limits and widen the range of redisplayed
12692 lines to that, but for now just give up this optimization. */
12693 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12694 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12695 unchanged_p = 0;
12698 return unchanged_p;
12702 /* Do a frame update, taking possible shortcuts into account. This is
12703 the main external entry point for redisplay.
12705 If the last redisplay displayed an echo area message and that message
12706 is no longer requested, we clear the echo area or bring back the
12707 mini-buffer if that is in use. */
12709 void
12710 redisplay (void)
12712 redisplay_internal ();
12716 static Lisp_Object
12717 overlay_arrow_string_or_property (Lisp_Object var)
12719 Lisp_Object val;
12721 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12722 return val;
12724 return Voverlay_arrow_string;
12727 /* Return 1 if there are any overlay-arrows in current_buffer. */
12728 static int
12729 overlay_arrow_in_current_buffer_p (void)
12731 Lisp_Object vlist;
12733 for (vlist = Voverlay_arrow_variable_list;
12734 CONSP (vlist);
12735 vlist = XCDR (vlist))
12737 Lisp_Object var = XCAR (vlist);
12738 Lisp_Object val;
12740 if (!SYMBOLP (var))
12741 continue;
12742 val = find_symbol_value (var);
12743 if (MARKERP (val)
12744 && current_buffer == XMARKER (val)->buffer)
12745 return 1;
12747 return 0;
12751 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12752 has changed. */
12754 static int
12755 overlay_arrows_changed_p (void)
12757 Lisp_Object vlist;
12759 for (vlist = Voverlay_arrow_variable_list;
12760 CONSP (vlist);
12761 vlist = XCDR (vlist))
12763 Lisp_Object var = XCAR (vlist);
12764 Lisp_Object val, pstr;
12766 if (!SYMBOLP (var))
12767 continue;
12768 val = find_symbol_value (var);
12769 if (!MARKERP (val))
12770 continue;
12771 if (! EQ (COERCE_MARKER (val),
12772 Fget (var, Qlast_arrow_position))
12773 || ! (pstr = overlay_arrow_string_or_property (var),
12774 EQ (pstr, Fget (var, Qlast_arrow_string))))
12775 return 1;
12777 return 0;
12780 /* Mark overlay arrows to be updated on next redisplay. */
12782 static void
12783 update_overlay_arrows (int up_to_date)
12785 Lisp_Object vlist;
12787 for (vlist = Voverlay_arrow_variable_list;
12788 CONSP (vlist);
12789 vlist = XCDR (vlist))
12791 Lisp_Object var = XCAR (vlist);
12793 if (!SYMBOLP (var))
12794 continue;
12796 if (up_to_date > 0)
12798 Lisp_Object val = find_symbol_value (var);
12799 Fput (var, Qlast_arrow_position,
12800 COERCE_MARKER (val));
12801 Fput (var, Qlast_arrow_string,
12802 overlay_arrow_string_or_property (var));
12804 else if (up_to_date < 0
12805 || !NILP (Fget (var, Qlast_arrow_position)))
12807 Fput (var, Qlast_arrow_position, Qt);
12808 Fput (var, Qlast_arrow_string, Qt);
12814 /* Return overlay arrow string to display at row.
12815 Return integer (bitmap number) for arrow bitmap in left fringe.
12816 Return nil if no overlay arrow. */
12818 static Lisp_Object
12819 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12821 Lisp_Object vlist;
12823 for (vlist = Voverlay_arrow_variable_list;
12824 CONSP (vlist);
12825 vlist = XCDR (vlist))
12827 Lisp_Object var = XCAR (vlist);
12828 Lisp_Object val;
12830 if (!SYMBOLP (var))
12831 continue;
12833 val = find_symbol_value (var);
12835 if (MARKERP (val)
12836 && current_buffer == XMARKER (val)->buffer
12837 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12839 if (FRAME_WINDOW_P (it->f)
12840 /* FIXME: if ROW->reversed_p is set, this should test
12841 the right fringe, not the left one. */
12842 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12844 #ifdef HAVE_WINDOW_SYSTEM
12845 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12847 int fringe_bitmap;
12848 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12849 return make_number (fringe_bitmap);
12851 #endif
12852 return make_number (-1); /* Use default arrow bitmap. */
12854 return overlay_arrow_string_or_property (var);
12858 return Qnil;
12861 /* Return 1 if point moved out of or into a composition. Otherwise
12862 return 0. PREV_BUF and PREV_PT are the last point buffer and
12863 position. BUF and PT are the current point buffer and position. */
12865 static int
12866 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12867 struct buffer *buf, ptrdiff_t pt)
12869 ptrdiff_t start, end;
12870 Lisp_Object prop;
12871 Lisp_Object buffer;
12873 XSETBUFFER (buffer, buf);
12874 /* Check a composition at the last point if point moved within the
12875 same buffer. */
12876 if (prev_buf == buf)
12878 if (prev_pt == pt)
12879 /* Point didn't move. */
12880 return 0;
12882 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12883 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12884 && composition_valid_p (start, end, prop)
12885 && start < prev_pt && end > prev_pt)
12886 /* The last point was within the composition. Return 1 iff
12887 point moved out of the composition. */
12888 return (pt <= start || pt >= end);
12891 /* Check a composition at the current point. */
12892 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12893 && find_composition (pt, -1, &start, &end, &prop, buffer)
12894 && composition_valid_p (start, end, prop)
12895 && start < pt && end > pt);
12898 /* Reconsider the clip changes of buffer which is displayed in W. */
12900 static void
12901 reconsider_clip_changes (struct window *w)
12903 struct buffer *b = XBUFFER (w->contents);
12905 if (b->clip_changed
12906 && w->window_end_valid
12907 && w->current_matrix->buffer == b
12908 && w->current_matrix->zv == BUF_ZV (b)
12909 && w->current_matrix->begv == BUF_BEGV (b))
12910 b->clip_changed = 0;
12912 /* If display wasn't paused, and W is not a tool bar window, see if
12913 point has been moved into or out of a composition. In that case,
12914 we set b->clip_changed to 1 to force updating the screen. If
12915 b->clip_changed has already been set to 1, we can skip this
12916 check. */
12917 if (!b->clip_changed && w->window_end_valid)
12919 ptrdiff_t pt = (w == XWINDOW (selected_window)
12920 ? PT : marker_position (w->pointm));
12922 if ((w->current_matrix->buffer != b || pt != w->last_point)
12923 && check_point_in_composition (w->current_matrix->buffer,
12924 w->last_point, b, pt))
12925 b->clip_changed = 1;
12929 static void
12930 propagate_buffer_redisplay (void)
12931 { /* Resetting b->text->redisplay is problematic!
12932 We can't just reset it in the case that some window that displays
12933 it has not been redisplayed; and such a window can stay
12934 unredisplayed for a long time if it's currently invisible.
12935 But we do want to reset it at the end of redisplay otherwise
12936 its displayed windows will keep being redisplayed over and over
12937 again.
12938 So we copy all b->text->redisplay flags up to their windows here,
12939 such that mark_window_display_accurate can safely reset
12940 b->text->redisplay. */
12941 Lisp_Object ws = window_list ();
12942 for (; CONSP (ws); ws = XCDR (ws))
12944 struct window *thisw = XWINDOW (XCAR (ws));
12945 struct buffer *thisb = XBUFFER (thisw->contents);
12946 if (thisb->text->redisplay)
12947 thisw->redisplay = true;
12951 #define STOP_POLLING \
12952 do { if (! polling_stopped_here) stop_polling (); \
12953 polling_stopped_here = 1; } while (0)
12955 #define RESUME_POLLING \
12956 do { if (polling_stopped_here) start_polling (); \
12957 polling_stopped_here = 0; } while (0)
12960 /* Perhaps in the future avoid recentering windows if it
12961 is not necessary; currently that causes some problems. */
12963 static void
12964 redisplay_internal (void)
12966 struct window *w = XWINDOW (selected_window);
12967 struct window *sw;
12968 struct frame *fr;
12969 int pending;
12970 bool must_finish = 0, match_p;
12971 struct text_pos tlbufpos, tlendpos;
12972 int number_of_visible_frames;
12973 ptrdiff_t count;
12974 struct frame *sf;
12975 int polling_stopped_here = 0;
12976 Lisp_Object tail, frame;
12978 /* True means redisplay has to consider all windows on all
12979 frames. False, only selected_window is considered. */
12980 bool consider_all_windows_p;
12982 /* True means redisplay has to redisplay the miniwindow. */
12983 bool update_miniwindow_p = false;
12985 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12987 /* No redisplay if running in batch mode or frame is not yet fully
12988 initialized, or redisplay is explicitly turned off by setting
12989 Vinhibit_redisplay. */
12990 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12991 || !NILP (Vinhibit_redisplay))
12992 return;
12994 /* Don't examine these until after testing Vinhibit_redisplay.
12995 When Emacs is shutting down, perhaps because its connection to
12996 X has dropped, we should not look at them at all. */
12997 fr = XFRAME (w->frame);
12998 sf = SELECTED_FRAME ();
13000 if (!fr->glyphs_initialized_p)
13001 return;
13003 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13004 if (popup_activated ())
13005 return;
13006 #endif
13008 /* I don't think this happens but let's be paranoid. */
13009 if (redisplaying_p)
13010 return;
13012 /* Record a function that clears redisplaying_p
13013 when we leave this function. */
13014 count = SPECPDL_INDEX ();
13015 record_unwind_protect_void (unwind_redisplay);
13016 redisplaying_p = 1;
13017 specbind (Qinhibit_free_realized_faces, Qnil);
13019 /* Record this function, so it appears on the profiler's backtraces. */
13020 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13022 FOR_EACH_FRAME (tail, frame)
13023 XFRAME (frame)->already_hscrolled_p = 0;
13025 retry:
13026 /* Remember the currently selected window. */
13027 sw = w;
13029 pending = 0;
13030 last_escape_glyph_frame = NULL;
13031 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13032 last_glyphless_glyph_frame = NULL;
13033 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13035 /* If face_change_count is non-zero, init_iterator will free all
13036 realized faces, which includes the faces referenced from current
13037 matrices. So, we can't reuse current matrices in this case. */
13038 if (face_change_count)
13039 windows_or_buffers_changed = 47;
13041 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13042 && FRAME_TTY (sf)->previous_frame != sf)
13044 /* Since frames on a single ASCII terminal share the same
13045 display area, displaying a different frame means redisplay
13046 the whole thing. */
13047 SET_FRAME_GARBAGED (sf);
13048 #ifndef DOS_NT
13049 set_tty_color_mode (FRAME_TTY (sf), sf);
13050 #endif
13051 FRAME_TTY (sf)->previous_frame = sf;
13054 /* Set the visible flags for all frames. Do this before checking for
13055 resized or garbaged frames; they want to know if their frames are
13056 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13057 number_of_visible_frames = 0;
13059 FOR_EACH_FRAME (tail, frame)
13061 struct frame *f = XFRAME (frame);
13063 if (FRAME_VISIBLE_P (f))
13065 ++number_of_visible_frames;
13066 /* Adjust matrices for visible frames only. */
13067 if (f->fonts_changed)
13069 adjust_frame_glyphs (f);
13070 f->fonts_changed = 0;
13072 /* If cursor type has been changed on the frame
13073 other than selected, consider all frames. */
13074 if (f != sf && f->cursor_type_changed)
13075 update_mode_lines = 31;
13077 clear_desired_matrices (f);
13080 /* Notice any pending interrupt request to change frame size. */
13081 do_pending_window_change (1);
13083 /* do_pending_window_change could change the selected_window due to
13084 frame resizing which makes the selected window too small. */
13085 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13086 sw = w;
13088 /* Clear frames marked as garbaged. */
13089 clear_garbaged_frames ();
13091 /* Build menubar and tool-bar items. */
13092 if (NILP (Vmemory_full))
13093 prepare_menu_bars ();
13095 reconsider_clip_changes (w);
13097 /* In most cases selected window displays current buffer. */
13098 match_p = XBUFFER (w->contents) == current_buffer;
13099 if (match_p)
13101 /* Detect case that we need to write or remove a star in the mode line. */
13102 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13103 w->update_mode_line = 1;
13105 if (mode_line_update_needed (w))
13106 w->update_mode_line = 1;
13109 /* Normally the message* functions will have already displayed and
13110 updated the echo area, but the frame may have been trashed, or
13111 the update may have been preempted, so display the echo area
13112 again here. Checking message_cleared_p captures the case that
13113 the echo area should be cleared. */
13114 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13115 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13116 || (message_cleared_p
13117 && minibuf_level == 0
13118 /* If the mini-window is currently selected, this means the
13119 echo-area doesn't show through. */
13120 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13122 int window_height_changed_p = echo_area_display (0);
13124 if (message_cleared_p)
13125 update_miniwindow_p = true;
13127 must_finish = 1;
13129 /* If we don't display the current message, don't clear the
13130 message_cleared_p flag, because, if we did, we wouldn't clear
13131 the echo area in the next redisplay which doesn't preserve
13132 the echo area. */
13133 if (!display_last_displayed_message_p)
13134 message_cleared_p = 0;
13136 if (window_height_changed_p)
13138 windows_or_buffers_changed = 50;
13140 /* If window configuration was changed, frames may have been
13141 marked garbaged. Clear them or we will experience
13142 surprises wrt scrolling. */
13143 clear_garbaged_frames ();
13146 else if (EQ (selected_window, minibuf_window)
13147 && (current_buffer->clip_changed || window_outdated (w))
13148 && resize_mini_window (w, 0))
13150 /* Resized active mini-window to fit the size of what it is
13151 showing if its contents might have changed. */
13152 must_finish = 1;
13154 /* If window configuration was changed, frames may have been
13155 marked garbaged. Clear them or we will experience
13156 surprises wrt scrolling. */
13157 clear_garbaged_frames ();
13160 if (windows_or_buffers_changed && !update_mode_lines)
13161 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13162 only the windows's contents needs to be refreshed, or whether the
13163 mode-lines also need a refresh. */
13164 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13165 ? REDISPLAY_SOME : 32);
13167 /* If specs for an arrow have changed, do thorough redisplay
13168 to ensure we remove any arrow that should no longer exist. */
13169 if (overlay_arrows_changed_p ())
13170 /* Apparently, this is the only case where we update other windows,
13171 without updating other mode-lines. */
13172 windows_or_buffers_changed = 49;
13174 consider_all_windows_p = (update_mode_lines
13175 || windows_or_buffers_changed);
13177 #define AINC(a,i) \
13178 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13179 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13181 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13182 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13184 /* Optimize the case that only the line containing the cursor in the
13185 selected window has changed. Variables starting with this_ are
13186 set in display_line and record information about the line
13187 containing the cursor. */
13188 tlbufpos = this_line_start_pos;
13189 tlendpos = this_line_end_pos;
13190 if (!consider_all_windows_p
13191 && CHARPOS (tlbufpos) > 0
13192 && !w->update_mode_line
13193 && !current_buffer->clip_changed
13194 && !current_buffer->prevent_redisplay_optimizations_p
13195 && FRAME_VISIBLE_P (XFRAME (w->frame))
13196 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13197 && !XFRAME (w->frame)->cursor_type_changed
13198 /* Make sure recorded data applies to current buffer, etc. */
13199 && this_line_buffer == current_buffer
13200 && match_p
13201 && !w->force_start
13202 && !w->optional_new_start
13203 /* Point must be on the line that we have info recorded about. */
13204 && PT >= CHARPOS (tlbufpos)
13205 && PT <= Z - CHARPOS (tlendpos)
13206 /* All text outside that line, including its final newline,
13207 must be unchanged. */
13208 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13209 CHARPOS (tlendpos)))
13211 if (CHARPOS (tlbufpos) > BEGV
13212 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13213 && (CHARPOS (tlbufpos) == ZV
13214 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13215 /* Former continuation line has disappeared by becoming empty. */
13216 goto cancel;
13217 else if (window_outdated (w) || MINI_WINDOW_P (w))
13219 /* We have to handle the case of continuation around a
13220 wide-column character (see the comment in indent.c around
13221 line 1340).
13223 For instance, in the following case:
13225 -------- Insert --------
13226 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13227 J_I_ ==> J_I_ `^^' are cursors.
13228 ^^ ^^
13229 -------- --------
13231 As we have to redraw the line above, we cannot use this
13232 optimization. */
13234 struct it it;
13235 int line_height_before = this_line_pixel_height;
13237 /* Note that start_display will handle the case that the
13238 line starting at tlbufpos is a continuation line. */
13239 start_display (&it, w, tlbufpos);
13241 /* Implementation note: It this still necessary? */
13242 if (it.current_x != this_line_start_x)
13243 goto cancel;
13245 TRACE ((stderr, "trying display optimization 1\n"));
13246 w->cursor.vpos = -1;
13247 overlay_arrow_seen = 0;
13248 it.vpos = this_line_vpos;
13249 it.current_y = this_line_y;
13250 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13251 display_line (&it);
13253 /* If line contains point, is not continued,
13254 and ends at same distance from eob as before, we win. */
13255 if (w->cursor.vpos >= 0
13256 /* Line is not continued, otherwise this_line_start_pos
13257 would have been set to 0 in display_line. */
13258 && CHARPOS (this_line_start_pos)
13259 /* Line ends as before. */
13260 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13261 /* Line has same height as before. Otherwise other lines
13262 would have to be shifted up or down. */
13263 && this_line_pixel_height == line_height_before)
13265 /* If this is not the window's last line, we must adjust
13266 the charstarts of the lines below. */
13267 if (it.current_y < it.last_visible_y)
13269 struct glyph_row *row
13270 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13271 ptrdiff_t delta, delta_bytes;
13273 /* We used to distinguish between two cases here,
13274 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13275 when the line ends in a newline or the end of the
13276 buffer's accessible portion. But both cases did
13277 the same, so they were collapsed. */
13278 delta = (Z
13279 - CHARPOS (tlendpos)
13280 - MATRIX_ROW_START_CHARPOS (row));
13281 delta_bytes = (Z_BYTE
13282 - BYTEPOS (tlendpos)
13283 - MATRIX_ROW_START_BYTEPOS (row));
13285 increment_matrix_positions (w->current_matrix,
13286 this_line_vpos + 1,
13287 w->current_matrix->nrows,
13288 delta, delta_bytes);
13291 /* If this row displays text now but previously didn't,
13292 or vice versa, w->window_end_vpos may have to be
13293 adjusted. */
13294 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13296 if (w->window_end_vpos < this_line_vpos)
13297 w->window_end_vpos = this_line_vpos;
13299 else if (w->window_end_vpos == this_line_vpos
13300 && this_line_vpos > 0)
13301 w->window_end_vpos = this_line_vpos - 1;
13302 w->window_end_valid = 0;
13304 /* Update hint: No need to try to scroll in update_window. */
13305 w->desired_matrix->no_scrolling_p = 1;
13307 #ifdef GLYPH_DEBUG
13308 *w->desired_matrix->method = 0;
13309 debug_method_add (w, "optimization 1");
13310 #endif
13311 #ifdef HAVE_WINDOW_SYSTEM
13312 update_window_fringes (w, 0);
13313 #endif
13314 goto update;
13316 else
13317 goto cancel;
13319 else if (/* Cursor position hasn't changed. */
13320 PT == w->last_point
13321 /* Make sure the cursor was last displayed
13322 in this window. Otherwise we have to reposition it. */
13323 && 0 <= w->cursor.vpos
13324 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13326 if (!must_finish)
13328 do_pending_window_change (1);
13329 /* If selected_window changed, redisplay again. */
13330 if (WINDOWP (selected_window)
13331 && (w = XWINDOW (selected_window)) != sw)
13332 goto retry;
13334 /* We used to always goto end_of_redisplay here, but this
13335 isn't enough if we have a blinking cursor. */
13336 if (w->cursor_off_p == w->last_cursor_off_p)
13337 goto end_of_redisplay;
13339 goto update;
13341 /* If highlighting the region, or if the cursor is in the echo area,
13342 then we can't just move the cursor. */
13343 else if (NILP (Vshow_trailing_whitespace)
13344 && !cursor_in_echo_area)
13346 struct it it;
13347 struct glyph_row *row;
13349 /* Skip from tlbufpos to PT and see where it is. Note that
13350 PT may be in invisible text. If so, we will end at the
13351 next visible position. */
13352 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13353 NULL, DEFAULT_FACE_ID);
13354 it.current_x = this_line_start_x;
13355 it.current_y = this_line_y;
13356 it.vpos = this_line_vpos;
13358 /* The call to move_it_to stops in front of PT, but
13359 moves over before-strings. */
13360 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13362 if (it.vpos == this_line_vpos
13363 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13364 row->enabled_p))
13366 eassert (this_line_vpos == it.vpos);
13367 eassert (this_line_y == it.current_y);
13368 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13369 #ifdef GLYPH_DEBUG
13370 *w->desired_matrix->method = 0;
13371 debug_method_add (w, "optimization 3");
13372 #endif
13373 goto update;
13375 else
13376 goto cancel;
13379 cancel:
13380 /* Text changed drastically or point moved off of line. */
13381 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13384 CHARPOS (this_line_start_pos) = 0;
13385 ++clear_face_cache_count;
13386 #ifdef HAVE_WINDOW_SYSTEM
13387 ++clear_image_cache_count;
13388 #endif
13390 /* Build desired matrices, and update the display. If
13391 consider_all_windows_p is non-zero, do it for all windows on all
13392 frames. Otherwise do it for selected_window, only. */
13394 if (consider_all_windows_p)
13396 FOR_EACH_FRAME (tail, frame)
13397 XFRAME (frame)->updated_p = 0;
13399 propagate_buffer_redisplay ();
13401 FOR_EACH_FRAME (tail, frame)
13403 struct frame *f = XFRAME (frame);
13405 /* We don't have to do anything for unselected terminal
13406 frames. */
13407 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13408 && !EQ (FRAME_TTY (f)->top_frame, frame))
13409 continue;
13411 retry_frame:
13413 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13415 bool gcscrollbars
13416 /* Only GC scollbars when we redisplay the whole frame. */
13417 = f->redisplay || windows_or_buffers_changed != REDISPLAY_SOME;
13418 /* Mark all the scroll bars to be removed; we'll redeem
13419 the ones we want when we redisplay their windows. */
13420 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13421 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13423 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13424 redisplay_windows (FRAME_ROOT_WINDOW (f));
13426 /* The X error handler may have deleted that frame. */
13427 if (!FRAME_LIVE_P (f))
13428 continue;
13430 /* Any scroll bars which redisplay_windows should have
13431 nuked should now go away. */
13432 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13433 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13435 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13437 /* If fonts changed on visible frame, display again. */
13438 if (f->fonts_changed)
13440 adjust_frame_glyphs (f);
13441 f->fonts_changed = 0;
13442 goto retry_frame;
13445 /* See if we have to hscroll. */
13446 if (!f->already_hscrolled_p)
13448 f->already_hscrolled_p = 1;
13449 if (hscroll_windows (f->root_window))
13450 goto retry_frame;
13453 /* Prevent various kinds of signals during display
13454 update. stdio is not robust about handling
13455 signals, which can cause an apparent I/O
13456 error. */
13457 if (interrupt_input)
13458 unrequest_sigio ();
13459 STOP_POLLING;
13461 /* Mark windows on frame F to update. If we decide to
13462 update all frames but windows_or_buffers_changed is
13463 zero, we assume that only the windows that shows
13464 current buffer should be really updated. */
13465 set_window_update_flags
13466 (XWINDOW (f->root_window),
13467 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13468 pending |= update_frame (f, 0, 0);
13469 f->cursor_type_changed = 0;
13470 f->updated_p = 1;
13475 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13477 if (!pending)
13479 /* Do the mark_window_display_accurate after all windows have
13480 been redisplayed because this call resets flags in buffers
13481 which are needed for proper redisplay. */
13482 FOR_EACH_FRAME (tail, frame)
13484 struct frame *f = XFRAME (frame);
13485 if (f->updated_p)
13487 f->redisplay = false;
13488 mark_window_display_accurate (f->root_window, 1);
13489 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13490 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13495 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13497 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13498 struct frame *mini_frame;
13500 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13501 /* Use list_of_error, not Qerror, so that
13502 we catch only errors and don't run the debugger. */
13503 internal_condition_case_1 (redisplay_window_1, selected_window,
13504 list_of_error,
13505 redisplay_window_error);
13506 if (update_miniwindow_p)
13507 internal_condition_case_1 (redisplay_window_1, mini_window,
13508 list_of_error,
13509 redisplay_window_error);
13511 /* Compare desired and current matrices, perform output. */
13513 update:
13514 /* If fonts changed, display again. */
13515 if (sf->fonts_changed)
13516 goto retry;
13518 /* Prevent various kinds of signals during display update.
13519 stdio is not robust about handling signals,
13520 which can cause an apparent I/O error. */
13521 if (interrupt_input)
13522 unrequest_sigio ();
13523 STOP_POLLING;
13525 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13527 if (hscroll_windows (selected_window))
13528 goto retry;
13530 XWINDOW (selected_window)->must_be_updated_p = 1;
13531 pending = update_frame (sf, 0, 0);
13532 sf->cursor_type_changed = 0;
13535 /* We may have called echo_area_display at the top of this
13536 function. If the echo area is on another frame, that may
13537 have put text on a frame other than the selected one, so the
13538 above call to update_frame would not have caught it. Catch
13539 it here. */
13540 mini_window = FRAME_MINIBUF_WINDOW (sf);
13541 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13543 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13545 XWINDOW (mini_window)->must_be_updated_p = 1;
13546 pending |= update_frame (mini_frame, 0, 0);
13547 mini_frame->cursor_type_changed = 0;
13548 if (!pending && hscroll_windows (mini_window))
13549 goto retry;
13553 /* If display was paused because of pending input, make sure we do a
13554 thorough update the next time. */
13555 if (pending)
13557 /* Prevent the optimization at the beginning of
13558 redisplay_internal that tries a single-line update of the
13559 line containing the cursor in the selected window. */
13560 CHARPOS (this_line_start_pos) = 0;
13562 /* Let the overlay arrow be updated the next time. */
13563 update_overlay_arrows (0);
13565 /* If we pause after scrolling, some rows in the current
13566 matrices of some windows are not valid. */
13567 if (!WINDOW_FULL_WIDTH_P (w)
13568 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13569 update_mode_lines = 36;
13571 else
13573 if (!consider_all_windows_p)
13575 /* This has already been done above if
13576 consider_all_windows_p is set. */
13577 if (XBUFFER (w->contents)->text->redisplay
13578 && buffer_window_count (XBUFFER (w->contents)) > 1)
13579 /* This can happen if b->text->redisplay was set during
13580 jit-lock. */
13581 propagate_buffer_redisplay ();
13582 mark_window_display_accurate_1 (w, 1);
13584 /* Say overlay arrows are up to date. */
13585 update_overlay_arrows (1);
13587 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13588 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13591 update_mode_lines = 0;
13592 windows_or_buffers_changed = 0;
13595 /* Start SIGIO interrupts coming again. Having them off during the
13596 code above makes it less likely one will discard output, but not
13597 impossible, since there might be stuff in the system buffer here.
13598 But it is much hairier to try to do anything about that. */
13599 if (interrupt_input)
13600 request_sigio ();
13601 RESUME_POLLING;
13603 /* If a frame has become visible which was not before, redisplay
13604 again, so that we display it. Expose events for such a frame
13605 (which it gets when becoming visible) don't call the parts of
13606 redisplay constructing glyphs, so simply exposing a frame won't
13607 display anything in this case. So, we have to display these
13608 frames here explicitly. */
13609 if (!pending)
13611 int new_count = 0;
13613 FOR_EACH_FRAME (tail, frame)
13615 if (XFRAME (frame)->visible)
13616 new_count++;
13619 if (new_count != number_of_visible_frames)
13620 windows_or_buffers_changed = 52;
13623 /* Change frame size now if a change is pending. */
13624 do_pending_window_change (1);
13626 /* If we just did a pending size change, or have additional
13627 visible frames, or selected_window changed, redisplay again. */
13628 if ((windows_or_buffers_changed && !pending)
13629 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13630 goto retry;
13632 /* Clear the face and image caches.
13634 We used to do this only if consider_all_windows_p. But the cache
13635 needs to be cleared if a timer creates images in the current
13636 buffer (e.g. the test case in Bug#6230). */
13638 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13640 clear_face_cache (0);
13641 clear_face_cache_count = 0;
13644 #ifdef HAVE_WINDOW_SYSTEM
13645 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13647 clear_image_caches (Qnil);
13648 clear_image_cache_count = 0;
13650 #endif /* HAVE_WINDOW_SYSTEM */
13652 end_of_redisplay:
13653 unbind_to (count, Qnil);
13654 RESUME_POLLING;
13658 /* Redisplay, but leave alone any recent echo area message unless
13659 another message has been requested in its place.
13661 This is useful in situations where you need to redisplay but no
13662 user action has occurred, making it inappropriate for the message
13663 area to be cleared. See tracking_off and
13664 wait_reading_process_output for examples of these situations.
13666 FROM_WHERE is an integer saying from where this function was
13667 called. This is useful for debugging. */
13669 void
13670 redisplay_preserve_echo_area (int from_where)
13672 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13674 if (!NILP (echo_area_buffer[1]))
13676 /* We have a previously displayed message, but no current
13677 message. Redisplay the previous message. */
13678 display_last_displayed_message_p = 1;
13679 redisplay_internal ();
13680 display_last_displayed_message_p = 0;
13682 else
13683 redisplay_internal ();
13685 flush_frame (SELECTED_FRAME ());
13689 /* Function registered with record_unwind_protect in redisplay_internal. */
13691 static void
13692 unwind_redisplay (void)
13694 redisplaying_p = 0;
13698 /* Mark the display of leaf window W as accurate or inaccurate.
13699 If ACCURATE_P is non-zero mark display of W as accurate. If
13700 ACCURATE_P is zero, arrange for W to be redisplayed the next
13701 time redisplay_internal is called. */
13703 static void
13704 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13706 struct buffer *b = XBUFFER (w->contents);
13708 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13709 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13710 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13712 if (accurate_p)
13714 b->clip_changed = false;
13715 b->prevent_redisplay_optimizations_p = false;
13716 eassert (buffer_window_count (b) > 0);
13717 /* Resetting b->text->redisplay is problematic!
13718 In order to make it safer to do it here, redisplay_internal must
13719 have copied all b->text->redisplay to their respective windows. */
13720 b->text->redisplay = false;
13722 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13723 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13724 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13725 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13727 w->current_matrix->buffer = b;
13728 w->current_matrix->begv = BUF_BEGV (b);
13729 w->current_matrix->zv = BUF_ZV (b);
13731 w->last_cursor_vpos = w->cursor.vpos;
13732 w->last_cursor_off_p = w->cursor_off_p;
13734 if (w == XWINDOW (selected_window))
13735 w->last_point = BUF_PT (b);
13736 else
13737 w->last_point = marker_position (w->pointm);
13739 w->window_end_valid = true;
13740 w->update_mode_line = false;
13743 w->redisplay = !accurate_p;
13747 /* Mark the display of windows in the window tree rooted at WINDOW as
13748 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13749 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13750 be redisplayed the next time redisplay_internal is called. */
13752 void
13753 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13755 struct window *w;
13757 for (; !NILP (window); window = w->next)
13759 w = XWINDOW (window);
13760 if (WINDOWP (w->contents))
13761 mark_window_display_accurate (w->contents, accurate_p);
13762 else
13763 mark_window_display_accurate_1 (w, accurate_p);
13766 if (accurate_p)
13767 update_overlay_arrows (1);
13768 else
13769 /* Force a thorough redisplay the next time by setting
13770 last_arrow_position and last_arrow_string to t, which is
13771 unequal to any useful value of Voverlay_arrow_... */
13772 update_overlay_arrows (-1);
13776 /* Return value in display table DP (Lisp_Char_Table *) for character
13777 C. Since a display table doesn't have any parent, we don't have to
13778 follow parent. Do not call this function directly but use the
13779 macro DISP_CHAR_VECTOR. */
13781 Lisp_Object
13782 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13784 Lisp_Object val;
13786 if (ASCII_CHAR_P (c))
13788 val = dp->ascii;
13789 if (SUB_CHAR_TABLE_P (val))
13790 val = XSUB_CHAR_TABLE (val)->contents[c];
13792 else
13794 Lisp_Object table;
13796 XSETCHAR_TABLE (table, dp);
13797 val = char_table_ref (table, c);
13799 if (NILP (val))
13800 val = dp->defalt;
13801 return val;
13806 /***********************************************************************
13807 Window Redisplay
13808 ***********************************************************************/
13810 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13812 static void
13813 redisplay_windows (Lisp_Object window)
13815 while (!NILP (window))
13817 struct window *w = XWINDOW (window);
13819 if (WINDOWP (w->contents))
13820 redisplay_windows (w->contents);
13821 else if (BUFFERP (w->contents))
13823 displayed_buffer = XBUFFER (w->contents);
13824 /* Use list_of_error, not Qerror, so that
13825 we catch only errors and don't run the debugger. */
13826 internal_condition_case_1 (redisplay_window_0, window,
13827 list_of_error,
13828 redisplay_window_error);
13831 window = w->next;
13835 static Lisp_Object
13836 redisplay_window_error (Lisp_Object ignore)
13838 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13839 return Qnil;
13842 static Lisp_Object
13843 redisplay_window_0 (Lisp_Object window)
13845 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13846 redisplay_window (window, 0);
13847 return Qnil;
13850 static Lisp_Object
13851 redisplay_window_1 (Lisp_Object window)
13853 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13854 redisplay_window (window, 1);
13855 return Qnil;
13859 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13860 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13861 which positions recorded in ROW differ from current buffer
13862 positions.
13864 Return 0 if cursor is not on this row, 1 otherwise. */
13866 static int
13867 set_cursor_from_row (struct window *w, struct glyph_row *row,
13868 struct glyph_matrix *matrix,
13869 ptrdiff_t delta, ptrdiff_t delta_bytes,
13870 int dy, int dvpos)
13872 struct glyph *glyph = row->glyphs[TEXT_AREA];
13873 struct glyph *end = glyph + row->used[TEXT_AREA];
13874 struct glyph *cursor = NULL;
13875 /* The last known character position in row. */
13876 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13877 int x = row->x;
13878 ptrdiff_t pt_old = PT - delta;
13879 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13880 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13881 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13882 /* A glyph beyond the edge of TEXT_AREA which we should never
13883 touch. */
13884 struct glyph *glyphs_end = end;
13885 /* Non-zero means we've found a match for cursor position, but that
13886 glyph has the avoid_cursor_p flag set. */
13887 int match_with_avoid_cursor = 0;
13888 /* Non-zero means we've seen at least one glyph that came from a
13889 display string. */
13890 int string_seen = 0;
13891 /* Largest and smallest buffer positions seen so far during scan of
13892 glyph row. */
13893 ptrdiff_t bpos_max = pos_before;
13894 ptrdiff_t bpos_min = pos_after;
13895 /* Last buffer position covered by an overlay string with an integer
13896 `cursor' property. */
13897 ptrdiff_t bpos_covered = 0;
13898 /* Non-zero means the display string on which to display the cursor
13899 comes from a text property, not from an overlay. */
13900 int string_from_text_prop = 0;
13902 /* Don't even try doing anything if called for a mode-line or
13903 header-line row, since the rest of the code isn't prepared to
13904 deal with such calamities. */
13905 eassert (!row->mode_line_p);
13906 if (row->mode_line_p)
13907 return 0;
13909 /* Skip over glyphs not having an object at the start and the end of
13910 the row. These are special glyphs like truncation marks on
13911 terminal frames. */
13912 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13914 if (!row->reversed_p)
13916 while (glyph < end
13917 && INTEGERP (glyph->object)
13918 && glyph->charpos < 0)
13920 x += glyph->pixel_width;
13921 ++glyph;
13923 while (end > glyph
13924 && INTEGERP ((end - 1)->object)
13925 /* CHARPOS is zero for blanks and stretch glyphs
13926 inserted by extend_face_to_end_of_line. */
13927 && (end - 1)->charpos <= 0)
13928 --end;
13929 glyph_before = glyph - 1;
13930 glyph_after = end;
13932 else
13934 struct glyph *g;
13936 /* If the glyph row is reversed, we need to process it from back
13937 to front, so swap the edge pointers. */
13938 glyphs_end = end = glyph - 1;
13939 glyph += row->used[TEXT_AREA] - 1;
13941 while (glyph > end + 1
13942 && INTEGERP (glyph->object)
13943 && glyph->charpos < 0)
13945 --glyph;
13946 x -= glyph->pixel_width;
13948 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13949 --glyph;
13950 /* By default, in reversed rows we put the cursor on the
13951 rightmost (first in the reading order) glyph. */
13952 for (g = end + 1; g < glyph; g++)
13953 x += g->pixel_width;
13954 while (end < glyph
13955 && INTEGERP ((end + 1)->object)
13956 && (end + 1)->charpos <= 0)
13957 ++end;
13958 glyph_before = glyph + 1;
13959 glyph_after = end;
13962 else if (row->reversed_p)
13964 /* In R2L rows that don't display text, put the cursor on the
13965 rightmost glyph. Case in point: an empty last line that is
13966 part of an R2L paragraph. */
13967 cursor = end - 1;
13968 /* Avoid placing the cursor on the last glyph of the row, where
13969 on terminal frames we hold the vertical border between
13970 adjacent windows. */
13971 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13972 && !WINDOW_RIGHTMOST_P (w)
13973 && cursor == row->glyphs[LAST_AREA] - 1)
13974 cursor--;
13975 x = -1; /* will be computed below, at label compute_x */
13978 /* Step 1: Try to find the glyph whose character position
13979 corresponds to point. If that's not possible, find 2 glyphs
13980 whose character positions are the closest to point, one before
13981 point, the other after it. */
13982 if (!row->reversed_p)
13983 while (/* not marched to end of glyph row */
13984 glyph < end
13985 /* glyph was not inserted by redisplay for internal purposes */
13986 && !INTEGERP (glyph->object))
13988 if (BUFFERP (glyph->object))
13990 ptrdiff_t dpos = glyph->charpos - pt_old;
13992 if (glyph->charpos > bpos_max)
13993 bpos_max = glyph->charpos;
13994 if (glyph->charpos < bpos_min)
13995 bpos_min = glyph->charpos;
13996 if (!glyph->avoid_cursor_p)
13998 /* If we hit point, we've found the glyph on which to
13999 display the cursor. */
14000 if (dpos == 0)
14002 match_with_avoid_cursor = 0;
14003 break;
14005 /* See if we've found a better approximation to
14006 POS_BEFORE or to POS_AFTER. */
14007 if (0 > dpos && dpos > pos_before - pt_old)
14009 pos_before = glyph->charpos;
14010 glyph_before = glyph;
14012 else if (0 < dpos && dpos < pos_after - pt_old)
14014 pos_after = glyph->charpos;
14015 glyph_after = glyph;
14018 else if (dpos == 0)
14019 match_with_avoid_cursor = 1;
14021 else if (STRINGP (glyph->object))
14023 Lisp_Object chprop;
14024 ptrdiff_t glyph_pos = glyph->charpos;
14026 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14027 glyph->object);
14028 if (!NILP (chprop))
14030 /* If the string came from a `display' text property,
14031 look up the buffer position of that property and
14032 use that position to update bpos_max, as if we
14033 actually saw such a position in one of the row's
14034 glyphs. This helps with supporting integer values
14035 of `cursor' property on the display string in
14036 situations where most or all of the row's buffer
14037 text is completely covered by display properties,
14038 so that no glyph with valid buffer positions is
14039 ever seen in the row. */
14040 ptrdiff_t prop_pos =
14041 string_buffer_position_lim (glyph->object, pos_before,
14042 pos_after, 0);
14044 if (prop_pos >= pos_before)
14045 bpos_max = prop_pos - 1;
14047 if (INTEGERP (chprop))
14049 bpos_covered = bpos_max + XINT (chprop);
14050 /* If the `cursor' property covers buffer positions up
14051 to and including point, we should display cursor on
14052 this glyph. Note that, if a `cursor' property on one
14053 of the string's characters has an integer value, we
14054 will break out of the loop below _before_ we get to
14055 the position match above. IOW, integer values of
14056 the `cursor' property override the "exact match for
14057 point" strategy of positioning the cursor. */
14058 /* Implementation note: bpos_max == pt_old when, e.g.,
14059 we are in an empty line, where bpos_max is set to
14060 MATRIX_ROW_START_CHARPOS, see above. */
14061 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14063 cursor = glyph;
14064 break;
14068 string_seen = 1;
14070 x += glyph->pixel_width;
14071 ++glyph;
14073 else if (glyph > end) /* row is reversed */
14074 while (!INTEGERP (glyph->object))
14076 if (BUFFERP (glyph->object))
14078 ptrdiff_t dpos = glyph->charpos - pt_old;
14080 if (glyph->charpos > bpos_max)
14081 bpos_max = glyph->charpos;
14082 if (glyph->charpos < bpos_min)
14083 bpos_min = glyph->charpos;
14084 if (!glyph->avoid_cursor_p)
14086 if (dpos == 0)
14088 match_with_avoid_cursor = 0;
14089 break;
14091 if (0 > dpos && dpos > pos_before - pt_old)
14093 pos_before = glyph->charpos;
14094 glyph_before = glyph;
14096 else if (0 < dpos && dpos < pos_after - pt_old)
14098 pos_after = glyph->charpos;
14099 glyph_after = glyph;
14102 else if (dpos == 0)
14103 match_with_avoid_cursor = 1;
14105 else if (STRINGP (glyph->object))
14107 Lisp_Object chprop;
14108 ptrdiff_t glyph_pos = glyph->charpos;
14110 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14111 glyph->object);
14112 if (!NILP (chprop))
14114 ptrdiff_t prop_pos =
14115 string_buffer_position_lim (glyph->object, pos_before,
14116 pos_after, 0);
14118 if (prop_pos >= pos_before)
14119 bpos_max = prop_pos - 1;
14121 if (INTEGERP (chprop))
14123 bpos_covered = bpos_max + XINT (chprop);
14124 /* If the `cursor' property covers buffer positions up
14125 to and including point, we should display cursor on
14126 this glyph. */
14127 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14129 cursor = glyph;
14130 break;
14133 string_seen = 1;
14135 --glyph;
14136 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14138 x--; /* can't use any pixel_width */
14139 break;
14141 x -= glyph->pixel_width;
14144 /* Step 2: If we didn't find an exact match for point, we need to
14145 look for a proper place to put the cursor among glyphs between
14146 GLYPH_BEFORE and GLYPH_AFTER. */
14147 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14148 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14149 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14151 /* An empty line has a single glyph whose OBJECT is zero and
14152 whose CHARPOS is the position of a newline on that line.
14153 Note that on a TTY, there are more glyphs after that, which
14154 were produced by extend_face_to_end_of_line, but their
14155 CHARPOS is zero or negative. */
14156 int empty_line_p =
14157 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14158 && INTEGERP (glyph->object) && glyph->charpos > 0
14159 /* On a TTY, continued and truncated rows also have a glyph at
14160 their end whose OBJECT is zero and whose CHARPOS is
14161 positive (the continuation and truncation glyphs), but such
14162 rows are obviously not "empty". */
14163 && !(row->continued_p || row->truncated_on_right_p);
14165 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14167 ptrdiff_t ellipsis_pos;
14169 /* Scan back over the ellipsis glyphs. */
14170 if (!row->reversed_p)
14172 ellipsis_pos = (glyph - 1)->charpos;
14173 while (glyph > row->glyphs[TEXT_AREA]
14174 && (glyph - 1)->charpos == ellipsis_pos)
14175 glyph--, x -= glyph->pixel_width;
14176 /* That loop always goes one position too far, including
14177 the glyph before the ellipsis. So scan forward over
14178 that one. */
14179 x += glyph->pixel_width;
14180 glyph++;
14182 else /* row is reversed */
14184 ellipsis_pos = (glyph + 1)->charpos;
14185 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14186 && (glyph + 1)->charpos == ellipsis_pos)
14187 glyph++, x += glyph->pixel_width;
14188 x -= glyph->pixel_width;
14189 glyph--;
14192 else if (match_with_avoid_cursor)
14194 cursor = glyph_after;
14195 x = -1;
14197 else if (string_seen)
14199 int incr = row->reversed_p ? -1 : +1;
14201 /* Need to find the glyph that came out of a string which is
14202 present at point. That glyph is somewhere between
14203 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14204 positioned between POS_BEFORE and POS_AFTER in the
14205 buffer. */
14206 struct glyph *start, *stop;
14207 ptrdiff_t pos = pos_before;
14209 x = -1;
14211 /* If the row ends in a newline from a display string,
14212 reordering could have moved the glyphs belonging to the
14213 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14214 in this case we extend the search to the last glyph in
14215 the row that was not inserted by redisplay. */
14216 if (row->ends_in_newline_from_string_p)
14218 glyph_after = end;
14219 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14222 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14223 correspond to POS_BEFORE and POS_AFTER, respectively. We
14224 need START and STOP in the order that corresponds to the
14225 row's direction as given by its reversed_p flag. If the
14226 directionality of characters between POS_BEFORE and
14227 POS_AFTER is the opposite of the row's base direction,
14228 these characters will have been reordered for display,
14229 and we need to reverse START and STOP. */
14230 if (!row->reversed_p)
14232 start = min (glyph_before, glyph_after);
14233 stop = max (glyph_before, glyph_after);
14235 else
14237 start = max (glyph_before, glyph_after);
14238 stop = min (glyph_before, glyph_after);
14240 for (glyph = start + incr;
14241 row->reversed_p ? glyph > stop : glyph < stop; )
14244 /* Any glyphs that come from the buffer are here because
14245 of bidi reordering. Skip them, and only pay
14246 attention to glyphs that came from some string. */
14247 if (STRINGP (glyph->object))
14249 Lisp_Object str;
14250 ptrdiff_t tem;
14251 /* If the display property covers the newline, we
14252 need to search for it one position farther. */
14253 ptrdiff_t lim = pos_after
14254 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14256 string_from_text_prop = 0;
14257 str = glyph->object;
14258 tem = string_buffer_position_lim (str, pos, lim, 0);
14259 if (tem == 0 /* from overlay */
14260 || pos <= tem)
14262 /* If the string from which this glyph came is
14263 found in the buffer at point, or at position
14264 that is closer to point than pos_after, then
14265 we've found the glyph we've been looking for.
14266 If it comes from an overlay (tem == 0), and
14267 it has the `cursor' property on one of its
14268 glyphs, record that glyph as a candidate for
14269 displaying the cursor. (As in the
14270 unidirectional version, we will display the
14271 cursor on the last candidate we find.) */
14272 if (tem == 0
14273 || tem == pt_old
14274 || (tem - pt_old > 0 && tem < pos_after))
14276 /* The glyphs from this string could have
14277 been reordered. Find the one with the
14278 smallest string position. Or there could
14279 be a character in the string with the
14280 `cursor' property, which means display
14281 cursor on that character's glyph. */
14282 ptrdiff_t strpos = glyph->charpos;
14284 if (tem)
14286 cursor = glyph;
14287 string_from_text_prop = 1;
14289 for ( ;
14290 (row->reversed_p ? glyph > stop : glyph < stop)
14291 && EQ (glyph->object, str);
14292 glyph += incr)
14294 Lisp_Object cprop;
14295 ptrdiff_t gpos = glyph->charpos;
14297 cprop = Fget_char_property (make_number (gpos),
14298 Qcursor,
14299 glyph->object);
14300 if (!NILP (cprop))
14302 cursor = glyph;
14303 break;
14305 if (tem && glyph->charpos < strpos)
14307 strpos = glyph->charpos;
14308 cursor = glyph;
14312 if (tem == pt_old
14313 || (tem - pt_old > 0 && tem < pos_after))
14314 goto compute_x;
14316 if (tem)
14317 pos = tem + 1; /* don't find previous instances */
14319 /* This string is not what we want; skip all of the
14320 glyphs that came from it. */
14321 while ((row->reversed_p ? glyph > stop : glyph < stop)
14322 && EQ (glyph->object, str))
14323 glyph += incr;
14325 else
14326 glyph += incr;
14329 /* If we reached the end of the line, and END was from a string,
14330 the cursor is not on this line. */
14331 if (cursor == NULL
14332 && (row->reversed_p ? glyph <= end : glyph >= end)
14333 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14334 && STRINGP (end->object)
14335 && row->continued_p)
14336 return 0;
14338 /* A truncated row may not include PT among its character positions.
14339 Setting the cursor inside the scroll margin will trigger
14340 recalculation of hscroll in hscroll_window_tree. But if a
14341 display string covers point, defer to the string-handling
14342 code below to figure this out. */
14343 else if (row->truncated_on_left_p && pt_old < bpos_min)
14345 cursor = glyph_before;
14346 x = -1;
14348 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14349 /* Zero-width characters produce no glyphs. */
14350 || (!empty_line_p
14351 && (row->reversed_p
14352 ? glyph_after > glyphs_end
14353 : glyph_after < glyphs_end)))
14355 cursor = glyph_after;
14356 x = -1;
14360 compute_x:
14361 if (cursor != NULL)
14362 glyph = cursor;
14363 else if (glyph == glyphs_end
14364 && pos_before == pos_after
14365 && STRINGP ((row->reversed_p
14366 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14367 : row->glyphs[TEXT_AREA])->object))
14369 /* If all the glyphs of this row came from strings, put the
14370 cursor on the first glyph of the row. This avoids having the
14371 cursor outside of the text area in this very rare and hard
14372 use case. */
14373 glyph =
14374 row->reversed_p
14375 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14376 : row->glyphs[TEXT_AREA];
14378 if (x < 0)
14380 struct glyph *g;
14382 /* Need to compute x that corresponds to GLYPH. */
14383 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14385 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14386 emacs_abort ();
14387 x += g->pixel_width;
14391 /* ROW could be part of a continued line, which, under bidi
14392 reordering, might have other rows whose start and end charpos
14393 occlude point. Only set w->cursor if we found a better
14394 approximation to the cursor position than we have from previously
14395 examined candidate rows belonging to the same continued line. */
14396 if (/* We already have a candidate row. */
14397 w->cursor.vpos >= 0
14398 /* That candidate is not the row we are processing. */
14399 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14400 /* Make sure cursor.vpos specifies a row whose start and end
14401 charpos occlude point, and it is valid candidate for being a
14402 cursor-row. This is because some callers of this function
14403 leave cursor.vpos at the row where the cursor was displayed
14404 during the last redisplay cycle. */
14405 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14406 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14407 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14409 struct glyph *g1
14410 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14412 /* Don't consider glyphs that are outside TEXT_AREA. */
14413 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14414 return 0;
14415 /* Keep the candidate whose buffer position is the closest to
14416 point or has the `cursor' property. */
14417 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14418 w->cursor.hpos >= 0
14419 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14420 && ((BUFFERP (g1->object)
14421 && (g1->charpos == pt_old /* An exact match always wins. */
14422 || (BUFFERP (glyph->object)
14423 && eabs (g1->charpos - pt_old)
14424 < eabs (glyph->charpos - pt_old))))
14425 /* Previous candidate is a glyph from a string that has
14426 a non-nil `cursor' property. */
14427 || (STRINGP (g1->object)
14428 && (!NILP (Fget_char_property (make_number (g1->charpos),
14429 Qcursor, g1->object))
14430 /* Previous candidate is from the same display
14431 string as this one, and the display string
14432 came from a text property. */
14433 || (EQ (g1->object, glyph->object)
14434 && string_from_text_prop)
14435 /* this candidate is from newline and its
14436 position is not an exact match */
14437 || (INTEGERP (glyph->object)
14438 && glyph->charpos != pt_old)))))
14439 return 0;
14440 /* If this candidate gives an exact match, use that. */
14441 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14442 /* If this candidate is a glyph created for the
14443 terminating newline of a line, and point is on that
14444 newline, it wins because it's an exact match. */
14445 || (!row->continued_p
14446 && INTEGERP (glyph->object)
14447 && glyph->charpos == 0
14448 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14449 /* Otherwise, keep the candidate that comes from a row
14450 spanning less buffer positions. This may win when one or
14451 both candidate positions are on glyphs that came from
14452 display strings, for which we cannot compare buffer
14453 positions. */
14454 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14455 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14456 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14457 return 0;
14459 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14460 w->cursor.x = x;
14461 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14462 w->cursor.y = row->y + dy;
14464 if (w == XWINDOW (selected_window))
14466 if (!row->continued_p
14467 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14468 && row->x == 0)
14470 this_line_buffer = XBUFFER (w->contents);
14472 CHARPOS (this_line_start_pos)
14473 = MATRIX_ROW_START_CHARPOS (row) + delta;
14474 BYTEPOS (this_line_start_pos)
14475 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14477 CHARPOS (this_line_end_pos)
14478 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14479 BYTEPOS (this_line_end_pos)
14480 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14482 this_line_y = w->cursor.y;
14483 this_line_pixel_height = row->height;
14484 this_line_vpos = w->cursor.vpos;
14485 this_line_start_x = row->x;
14487 else
14488 CHARPOS (this_line_start_pos) = 0;
14491 return 1;
14495 /* Run window scroll functions, if any, for WINDOW with new window
14496 start STARTP. Sets the window start of WINDOW to that position.
14498 We assume that the window's buffer is really current. */
14500 static struct text_pos
14501 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14503 struct window *w = XWINDOW (window);
14504 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14506 eassert (current_buffer == XBUFFER (w->contents));
14508 if (!NILP (Vwindow_scroll_functions))
14510 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14511 make_number (CHARPOS (startp)));
14512 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14513 /* In case the hook functions switch buffers. */
14514 set_buffer_internal (XBUFFER (w->contents));
14517 return startp;
14521 /* Make sure the line containing the cursor is fully visible.
14522 A value of 1 means there is nothing to be done.
14523 (Either the line is fully visible, or it cannot be made so,
14524 or we cannot tell.)
14526 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14527 is higher than window.
14529 A value of 0 means the caller should do scrolling
14530 as if point had gone off the screen. */
14532 static int
14533 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14535 struct glyph_matrix *matrix;
14536 struct glyph_row *row;
14537 int window_height;
14539 if (!make_cursor_line_fully_visible_p)
14540 return 1;
14542 /* It's not always possible to find the cursor, e.g, when a window
14543 is full of overlay strings. Don't do anything in that case. */
14544 if (w->cursor.vpos < 0)
14545 return 1;
14547 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14548 row = MATRIX_ROW (matrix, w->cursor.vpos);
14550 /* If the cursor row is not partially visible, there's nothing to do. */
14551 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14552 return 1;
14554 /* If the row the cursor is in is taller than the window's height,
14555 it's not clear what to do, so do nothing. */
14556 window_height = window_box_height (w);
14557 if (row->height >= window_height)
14559 if (!force_p || MINI_WINDOW_P (w)
14560 || w->vscroll || w->cursor.vpos == 0)
14561 return 1;
14563 return 0;
14567 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14568 non-zero means only WINDOW is redisplayed in redisplay_internal.
14569 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14570 in redisplay_window to bring a partially visible line into view in
14571 the case that only the cursor has moved.
14573 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14574 last screen line's vertical height extends past the end of the screen.
14576 Value is
14578 1 if scrolling succeeded
14580 0 if scrolling didn't find point.
14582 -1 if new fonts have been loaded so that we must interrupt
14583 redisplay, adjust glyph matrices, and try again. */
14585 enum
14587 SCROLLING_SUCCESS,
14588 SCROLLING_FAILED,
14589 SCROLLING_NEED_LARGER_MATRICES
14592 /* If scroll-conservatively is more than this, never recenter.
14594 If you change this, don't forget to update the doc string of
14595 `scroll-conservatively' and the Emacs manual. */
14596 #define SCROLL_LIMIT 100
14598 static int
14599 try_scrolling (Lisp_Object window, int just_this_one_p,
14600 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14601 int temp_scroll_step, int last_line_misfit)
14603 struct window *w = XWINDOW (window);
14604 struct frame *f = XFRAME (w->frame);
14605 struct text_pos pos, startp;
14606 struct it it;
14607 int this_scroll_margin, scroll_max, rc, height;
14608 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14609 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14610 Lisp_Object aggressive;
14611 /* We will never try scrolling more than this number of lines. */
14612 int scroll_limit = SCROLL_LIMIT;
14613 int frame_line_height = default_line_pixel_height (w);
14614 int window_total_lines
14615 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14617 #ifdef GLYPH_DEBUG
14618 debug_method_add (w, "try_scrolling");
14619 #endif
14621 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14623 /* Compute scroll margin height in pixels. We scroll when point is
14624 within this distance from the top or bottom of the window. */
14625 if (scroll_margin > 0)
14626 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14627 * frame_line_height;
14628 else
14629 this_scroll_margin = 0;
14631 /* Force arg_scroll_conservatively to have a reasonable value, to
14632 avoid scrolling too far away with slow move_it_* functions. Note
14633 that the user can supply scroll-conservatively equal to
14634 `most-positive-fixnum', which can be larger than INT_MAX. */
14635 if (arg_scroll_conservatively > scroll_limit)
14637 arg_scroll_conservatively = scroll_limit + 1;
14638 scroll_max = scroll_limit * frame_line_height;
14640 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14641 /* Compute how much we should try to scroll maximally to bring
14642 point into view. */
14643 scroll_max = (max (scroll_step,
14644 max (arg_scroll_conservatively, temp_scroll_step))
14645 * frame_line_height);
14646 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14647 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14648 /* We're trying to scroll because of aggressive scrolling but no
14649 scroll_step is set. Choose an arbitrary one. */
14650 scroll_max = 10 * frame_line_height;
14651 else
14652 scroll_max = 0;
14654 too_near_end:
14656 /* Decide whether to scroll down. */
14657 if (PT > CHARPOS (startp))
14659 int scroll_margin_y;
14661 /* Compute the pixel ypos of the scroll margin, then move IT to
14662 either that ypos or PT, whichever comes first. */
14663 start_display (&it, w, startp);
14664 scroll_margin_y = it.last_visible_y - this_scroll_margin
14665 - frame_line_height * extra_scroll_margin_lines;
14666 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14667 (MOVE_TO_POS | MOVE_TO_Y));
14669 if (PT > CHARPOS (it.current.pos))
14671 int y0 = line_bottom_y (&it);
14672 /* Compute how many pixels below window bottom to stop searching
14673 for PT. This avoids costly search for PT that is far away if
14674 the user limited scrolling by a small number of lines, but
14675 always finds PT if scroll_conservatively is set to a large
14676 number, such as most-positive-fixnum. */
14677 int slack = max (scroll_max, 10 * frame_line_height);
14678 int y_to_move = it.last_visible_y + slack;
14680 /* Compute the distance from the scroll margin to PT or to
14681 the scroll limit, whichever comes first. This should
14682 include the height of the cursor line, to make that line
14683 fully visible. */
14684 move_it_to (&it, PT, -1, y_to_move,
14685 -1, MOVE_TO_POS | MOVE_TO_Y);
14686 dy = line_bottom_y (&it) - y0;
14688 if (dy > scroll_max)
14689 return SCROLLING_FAILED;
14691 if (dy > 0)
14692 scroll_down_p = 1;
14696 if (scroll_down_p)
14698 /* Point is in or below the bottom scroll margin, so move the
14699 window start down. If scrolling conservatively, move it just
14700 enough down to make point visible. If scroll_step is set,
14701 move it down by scroll_step. */
14702 if (arg_scroll_conservatively)
14703 amount_to_scroll
14704 = min (max (dy, frame_line_height),
14705 frame_line_height * arg_scroll_conservatively);
14706 else if (scroll_step || temp_scroll_step)
14707 amount_to_scroll = scroll_max;
14708 else
14710 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14711 height = WINDOW_BOX_TEXT_HEIGHT (w);
14712 if (NUMBERP (aggressive))
14714 double float_amount = XFLOATINT (aggressive) * height;
14715 int aggressive_scroll = float_amount;
14716 if (aggressive_scroll == 0 && float_amount > 0)
14717 aggressive_scroll = 1;
14718 /* Don't let point enter the scroll margin near top of
14719 the window. This could happen if the value of
14720 scroll_up_aggressively is too large and there are
14721 non-zero margins, because scroll_up_aggressively
14722 means put point that fraction of window height
14723 _from_the_bottom_margin_. */
14724 if (aggressive_scroll + 2*this_scroll_margin > height)
14725 aggressive_scroll = height - 2*this_scroll_margin;
14726 amount_to_scroll = dy + aggressive_scroll;
14730 if (amount_to_scroll <= 0)
14731 return SCROLLING_FAILED;
14733 start_display (&it, w, startp);
14734 if (arg_scroll_conservatively <= scroll_limit)
14735 move_it_vertically (&it, amount_to_scroll);
14736 else
14738 /* Extra precision for users who set scroll-conservatively
14739 to a large number: make sure the amount we scroll
14740 the window start is never less than amount_to_scroll,
14741 which was computed as distance from window bottom to
14742 point. This matters when lines at window top and lines
14743 below window bottom have different height. */
14744 struct it it1;
14745 void *it1data = NULL;
14746 /* We use a temporary it1 because line_bottom_y can modify
14747 its argument, if it moves one line down; see there. */
14748 int start_y;
14750 SAVE_IT (it1, it, it1data);
14751 start_y = line_bottom_y (&it1);
14752 do {
14753 RESTORE_IT (&it, &it, it1data);
14754 move_it_by_lines (&it, 1);
14755 SAVE_IT (it1, it, it1data);
14756 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14759 /* If STARTP is unchanged, move it down another screen line. */
14760 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14761 move_it_by_lines (&it, 1);
14762 startp = it.current.pos;
14764 else
14766 struct text_pos scroll_margin_pos = startp;
14767 int y_offset = 0;
14769 /* See if point is inside the scroll margin at the top of the
14770 window. */
14771 if (this_scroll_margin)
14773 int y_start;
14775 start_display (&it, w, startp);
14776 y_start = it.current_y;
14777 move_it_vertically (&it, this_scroll_margin);
14778 scroll_margin_pos = it.current.pos;
14779 /* If we didn't move enough before hitting ZV, request
14780 additional amount of scroll, to move point out of the
14781 scroll margin. */
14782 if (IT_CHARPOS (it) == ZV
14783 && it.current_y - y_start < this_scroll_margin)
14784 y_offset = this_scroll_margin - (it.current_y - y_start);
14787 if (PT < CHARPOS (scroll_margin_pos))
14789 /* Point is in the scroll margin at the top of the window or
14790 above what is displayed in the window. */
14791 int y0, y_to_move;
14793 /* Compute the vertical distance from PT to the scroll
14794 margin position. Move as far as scroll_max allows, or
14795 one screenful, or 10 screen lines, whichever is largest.
14796 Give up if distance is greater than scroll_max or if we
14797 didn't reach the scroll margin position. */
14798 SET_TEXT_POS (pos, PT, PT_BYTE);
14799 start_display (&it, w, pos);
14800 y0 = it.current_y;
14801 y_to_move = max (it.last_visible_y,
14802 max (scroll_max, 10 * frame_line_height));
14803 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14804 y_to_move, -1,
14805 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14806 dy = it.current_y - y0;
14807 if (dy > scroll_max
14808 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14809 return SCROLLING_FAILED;
14811 /* Additional scroll for when ZV was too close to point. */
14812 dy += y_offset;
14814 /* Compute new window start. */
14815 start_display (&it, w, startp);
14817 if (arg_scroll_conservatively)
14818 amount_to_scroll = max (dy, frame_line_height *
14819 max (scroll_step, temp_scroll_step));
14820 else if (scroll_step || temp_scroll_step)
14821 amount_to_scroll = scroll_max;
14822 else
14824 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14825 height = WINDOW_BOX_TEXT_HEIGHT (w);
14826 if (NUMBERP (aggressive))
14828 double float_amount = XFLOATINT (aggressive) * height;
14829 int aggressive_scroll = float_amount;
14830 if (aggressive_scroll == 0 && float_amount > 0)
14831 aggressive_scroll = 1;
14832 /* Don't let point enter the scroll margin near
14833 bottom of the window, if the value of
14834 scroll_down_aggressively happens to be too
14835 large. */
14836 if (aggressive_scroll + 2*this_scroll_margin > height)
14837 aggressive_scroll = height - 2*this_scroll_margin;
14838 amount_to_scroll = dy + aggressive_scroll;
14842 if (amount_to_scroll <= 0)
14843 return SCROLLING_FAILED;
14845 move_it_vertically_backward (&it, amount_to_scroll);
14846 startp = it.current.pos;
14850 /* Run window scroll functions. */
14851 startp = run_window_scroll_functions (window, startp);
14853 /* Display the window. Give up if new fonts are loaded, or if point
14854 doesn't appear. */
14855 if (!try_window (window, startp, 0))
14856 rc = SCROLLING_NEED_LARGER_MATRICES;
14857 else if (w->cursor.vpos < 0)
14859 clear_glyph_matrix (w->desired_matrix);
14860 rc = SCROLLING_FAILED;
14862 else
14864 /* Maybe forget recorded base line for line number display. */
14865 if (!just_this_one_p
14866 || current_buffer->clip_changed
14867 || BEG_UNCHANGED < CHARPOS (startp))
14868 w->base_line_number = 0;
14870 /* If cursor ends up on a partially visible line,
14871 treat that as being off the bottom of the screen. */
14872 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14873 /* It's possible that the cursor is on the first line of the
14874 buffer, which is partially obscured due to a vscroll
14875 (Bug#7537). In that case, avoid looping forever . */
14876 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14878 clear_glyph_matrix (w->desired_matrix);
14879 ++extra_scroll_margin_lines;
14880 goto too_near_end;
14882 rc = SCROLLING_SUCCESS;
14885 return rc;
14889 /* Compute a suitable window start for window W if display of W starts
14890 on a continuation line. Value is non-zero if a new window start
14891 was computed.
14893 The new window start will be computed, based on W's width, starting
14894 from the start of the continued line. It is the start of the
14895 screen line with the minimum distance from the old start W->start. */
14897 static int
14898 compute_window_start_on_continuation_line (struct window *w)
14900 struct text_pos pos, start_pos;
14901 int window_start_changed_p = 0;
14903 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14905 /* If window start is on a continuation line... Window start may be
14906 < BEGV in case there's invisible text at the start of the
14907 buffer (M-x rmail, for example). */
14908 if (CHARPOS (start_pos) > BEGV
14909 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14911 struct it it;
14912 struct glyph_row *row;
14914 /* Handle the case that the window start is out of range. */
14915 if (CHARPOS (start_pos) < BEGV)
14916 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14917 else if (CHARPOS (start_pos) > ZV)
14918 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14920 /* Find the start of the continued line. This should be fast
14921 because find_newline is fast (newline cache). */
14922 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14923 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14924 row, DEFAULT_FACE_ID);
14925 reseat_at_previous_visible_line_start (&it);
14927 /* If the line start is "too far" away from the window start,
14928 say it takes too much time to compute a new window start. */
14929 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14930 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14932 int min_distance, distance;
14934 /* Move forward by display lines to find the new window
14935 start. If window width was enlarged, the new start can
14936 be expected to be > the old start. If window width was
14937 decreased, the new window start will be < the old start.
14938 So, we're looking for the display line start with the
14939 minimum distance from the old window start. */
14940 pos = it.current.pos;
14941 min_distance = INFINITY;
14942 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14943 distance < min_distance)
14945 min_distance = distance;
14946 pos = it.current.pos;
14947 if (it.line_wrap == WORD_WRAP)
14949 /* Under WORD_WRAP, move_it_by_lines is likely to
14950 overshoot and stop not at the first, but the
14951 second character from the left margin. So in
14952 that case, we need a more tight control on the X
14953 coordinate of the iterator than move_it_by_lines
14954 promises in its contract. The method is to first
14955 go to the last (rightmost) visible character of a
14956 line, then move to the leftmost character on the
14957 next line in a separate call. */
14958 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14959 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14960 move_it_to (&it, ZV, 0,
14961 it.current_y + it.max_ascent + it.max_descent, -1,
14962 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14964 else
14965 move_it_by_lines (&it, 1);
14968 /* Set the window start there. */
14969 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14970 window_start_changed_p = 1;
14974 return window_start_changed_p;
14978 /* Try cursor movement in case text has not changed in window WINDOW,
14979 with window start STARTP. Value is
14981 CURSOR_MOVEMENT_SUCCESS if successful
14983 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14985 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14986 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14987 we want to scroll as if scroll-step were set to 1. See the code.
14989 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14990 which case we have to abort this redisplay, and adjust matrices
14991 first. */
14993 enum
14995 CURSOR_MOVEMENT_SUCCESS,
14996 CURSOR_MOVEMENT_CANNOT_BE_USED,
14997 CURSOR_MOVEMENT_MUST_SCROLL,
14998 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15001 static int
15002 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15004 struct window *w = XWINDOW (window);
15005 struct frame *f = XFRAME (w->frame);
15006 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15008 #ifdef GLYPH_DEBUG
15009 if (inhibit_try_cursor_movement)
15010 return rc;
15011 #endif
15013 /* Previously, there was a check for Lisp integer in the
15014 if-statement below. Now, this field is converted to
15015 ptrdiff_t, thus zero means invalid position in a buffer. */
15016 eassert (w->last_point > 0);
15017 /* Likewise there was a check whether window_end_vpos is nil or larger
15018 than the window. Now window_end_vpos is int and so never nil, but
15019 let's leave eassert to check whether it fits in the window. */
15020 eassert (w->window_end_vpos < w->current_matrix->nrows);
15022 /* Handle case where text has not changed, only point, and it has
15023 not moved off the frame. */
15024 if (/* Point may be in this window. */
15025 PT >= CHARPOS (startp)
15026 /* Selective display hasn't changed. */
15027 && !current_buffer->clip_changed
15028 /* Function force-mode-line-update is used to force a thorough
15029 redisplay. It sets either windows_or_buffers_changed or
15030 update_mode_lines. So don't take a shortcut here for these
15031 cases. */
15032 && !update_mode_lines
15033 && !windows_or_buffers_changed
15034 && !f->cursor_type_changed
15035 && NILP (Vshow_trailing_whitespace)
15036 /* This code is not used for mini-buffer for the sake of the case
15037 of redisplaying to replace an echo area message; since in
15038 that case the mini-buffer contents per se are usually
15039 unchanged. This code is of no real use in the mini-buffer
15040 since the handling of this_line_start_pos, etc., in redisplay
15041 handles the same cases. */
15042 && !EQ (window, minibuf_window)
15043 && (FRAME_WINDOW_P (f)
15044 || !overlay_arrow_in_current_buffer_p ()))
15046 int this_scroll_margin, top_scroll_margin;
15047 struct glyph_row *row = NULL;
15048 int frame_line_height = default_line_pixel_height (w);
15049 int window_total_lines
15050 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15052 #ifdef GLYPH_DEBUG
15053 debug_method_add (w, "cursor movement");
15054 #endif
15056 /* Scroll if point within this distance from the top or bottom
15057 of the window. This is a pixel value. */
15058 if (scroll_margin > 0)
15060 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15061 this_scroll_margin *= frame_line_height;
15063 else
15064 this_scroll_margin = 0;
15066 top_scroll_margin = this_scroll_margin;
15067 if (WINDOW_WANTS_HEADER_LINE_P (w))
15068 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15070 /* Start with the row the cursor was displayed during the last
15071 not paused redisplay. Give up if that row is not valid. */
15072 if (w->last_cursor_vpos < 0
15073 || w->last_cursor_vpos >= w->current_matrix->nrows)
15074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15075 else
15077 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15078 if (row->mode_line_p)
15079 ++row;
15080 if (!row->enabled_p)
15081 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15084 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15086 int scroll_p = 0, must_scroll = 0;
15087 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15089 if (PT > w->last_point)
15091 /* Point has moved forward. */
15092 while (MATRIX_ROW_END_CHARPOS (row) < PT
15093 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15095 eassert (row->enabled_p);
15096 ++row;
15099 /* If the end position of a row equals the start
15100 position of the next row, and PT is at that position,
15101 we would rather display cursor in the next line. */
15102 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15103 && MATRIX_ROW_END_CHARPOS (row) == PT
15104 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15105 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15106 && !cursor_row_p (row))
15107 ++row;
15109 /* If within the scroll margin, scroll. Note that
15110 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15111 the next line would be drawn, and that
15112 this_scroll_margin can be zero. */
15113 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15114 || PT > MATRIX_ROW_END_CHARPOS (row)
15115 /* Line is completely visible last line in window
15116 and PT is to be set in the next line. */
15117 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15118 && PT == MATRIX_ROW_END_CHARPOS (row)
15119 && !row->ends_at_zv_p
15120 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15121 scroll_p = 1;
15123 else if (PT < w->last_point)
15125 /* Cursor has to be moved backward. Note that PT >=
15126 CHARPOS (startp) because of the outer if-statement. */
15127 while (!row->mode_line_p
15128 && (MATRIX_ROW_START_CHARPOS (row) > PT
15129 || (MATRIX_ROW_START_CHARPOS (row) == PT
15130 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15131 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15132 row > w->current_matrix->rows
15133 && (row-1)->ends_in_newline_from_string_p))))
15134 && (row->y > top_scroll_margin
15135 || CHARPOS (startp) == BEGV))
15137 eassert (row->enabled_p);
15138 --row;
15141 /* Consider the following case: Window starts at BEGV,
15142 there is invisible, intangible text at BEGV, so that
15143 display starts at some point START > BEGV. It can
15144 happen that we are called with PT somewhere between
15145 BEGV and START. Try to handle that case. */
15146 if (row < w->current_matrix->rows
15147 || row->mode_line_p)
15149 row = w->current_matrix->rows;
15150 if (row->mode_line_p)
15151 ++row;
15154 /* Due to newlines in overlay strings, we may have to
15155 skip forward over overlay strings. */
15156 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15157 && MATRIX_ROW_END_CHARPOS (row) == PT
15158 && !cursor_row_p (row))
15159 ++row;
15161 /* If within the scroll margin, scroll. */
15162 if (row->y < top_scroll_margin
15163 && CHARPOS (startp) != BEGV)
15164 scroll_p = 1;
15166 else
15168 /* Cursor did not move. So don't scroll even if cursor line
15169 is partially visible, as it was so before. */
15170 rc = CURSOR_MOVEMENT_SUCCESS;
15173 if (PT < MATRIX_ROW_START_CHARPOS (row)
15174 || PT > MATRIX_ROW_END_CHARPOS (row))
15176 /* if PT is not in the glyph row, give up. */
15177 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15178 must_scroll = 1;
15180 else if (rc != CURSOR_MOVEMENT_SUCCESS
15181 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15183 struct glyph_row *row1;
15185 /* If rows are bidi-reordered and point moved, back up
15186 until we find a row that does not belong to a
15187 continuation line. This is because we must consider
15188 all rows of a continued line as candidates for the
15189 new cursor positioning, since row start and end
15190 positions change non-linearly with vertical position
15191 in such rows. */
15192 /* FIXME: Revisit this when glyph ``spilling'' in
15193 continuation lines' rows is implemented for
15194 bidi-reordered rows. */
15195 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15196 MATRIX_ROW_CONTINUATION_LINE_P (row);
15197 --row)
15199 /* If we hit the beginning of the displayed portion
15200 without finding the first row of a continued
15201 line, give up. */
15202 if (row <= row1)
15204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15205 break;
15207 eassert (row->enabled_p);
15210 if (must_scroll)
15212 else if (rc != CURSOR_MOVEMENT_SUCCESS
15213 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15214 /* Make sure this isn't a header line by any chance, since
15215 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15216 && !row->mode_line_p
15217 && make_cursor_line_fully_visible_p)
15219 if (PT == MATRIX_ROW_END_CHARPOS (row)
15220 && !row->ends_at_zv_p
15221 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15223 else if (row->height > window_box_height (w))
15225 /* If we end up in a partially visible line, let's
15226 make it fully visible, except when it's taller
15227 than the window, in which case we can't do much
15228 about it. */
15229 *scroll_step = 1;
15230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15232 else
15234 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15235 if (!cursor_row_fully_visible_p (w, 0, 1))
15236 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15237 else
15238 rc = CURSOR_MOVEMENT_SUCCESS;
15241 else if (scroll_p)
15242 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15243 else if (rc != CURSOR_MOVEMENT_SUCCESS
15244 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15246 /* With bidi-reordered rows, there could be more than
15247 one candidate row whose start and end positions
15248 occlude point. We need to let set_cursor_from_row
15249 find the best candidate. */
15250 /* FIXME: Revisit this when glyph ``spilling'' in
15251 continuation lines' rows is implemented for
15252 bidi-reordered rows. */
15253 int rv = 0;
15257 int at_zv_p = 0, exact_match_p = 0;
15259 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15260 && PT <= MATRIX_ROW_END_CHARPOS (row)
15261 && cursor_row_p (row))
15262 rv |= set_cursor_from_row (w, row, w->current_matrix,
15263 0, 0, 0, 0);
15264 /* As soon as we've found the exact match for point,
15265 or the first suitable row whose ends_at_zv_p flag
15266 is set, we are done. */
15267 at_zv_p =
15268 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15269 if (rv && !at_zv_p
15270 && w->cursor.hpos >= 0
15271 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15272 w->cursor.vpos))
15274 struct glyph_row *candidate =
15275 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15276 struct glyph *g =
15277 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15278 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15280 exact_match_p =
15281 (BUFFERP (g->object) && g->charpos == PT)
15282 || (INTEGERP (g->object)
15283 && (g->charpos == PT
15284 || (g->charpos == 0 && endpos - 1 == PT)));
15286 if (rv && (at_zv_p || exact_match_p))
15288 rc = CURSOR_MOVEMENT_SUCCESS;
15289 break;
15291 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15292 break;
15293 ++row;
15295 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15296 || row->continued_p)
15297 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15298 || (MATRIX_ROW_START_CHARPOS (row) == PT
15299 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15300 /* If we didn't find any candidate rows, or exited the
15301 loop before all the candidates were examined, signal
15302 to the caller that this method failed. */
15303 if (rc != CURSOR_MOVEMENT_SUCCESS
15304 && !(rv
15305 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15306 && !row->continued_p))
15307 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15308 else if (rv)
15309 rc = CURSOR_MOVEMENT_SUCCESS;
15311 else
15315 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15317 rc = CURSOR_MOVEMENT_SUCCESS;
15318 break;
15320 ++row;
15322 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15323 && MATRIX_ROW_START_CHARPOS (row) == PT
15324 && cursor_row_p (row));
15329 return rc;
15332 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15333 static
15334 #endif
15335 void
15336 set_vertical_scroll_bar (struct window *w)
15338 ptrdiff_t start, end, whole;
15340 /* Calculate the start and end positions for the current window.
15341 At some point, it would be nice to choose between scrollbars
15342 which reflect the whole buffer size, with special markers
15343 indicating narrowing, and scrollbars which reflect only the
15344 visible region.
15346 Note that mini-buffers sometimes aren't displaying any text. */
15347 if (!MINI_WINDOW_P (w)
15348 || (w == XWINDOW (minibuf_window)
15349 && NILP (echo_area_buffer[0])))
15351 struct buffer *buf = XBUFFER (w->contents);
15352 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15353 start = marker_position (w->start) - BUF_BEGV (buf);
15354 /* I don't think this is guaranteed to be right. For the
15355 moment, we'll pretend it is. */
15356 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15358 if (end < start)
15359 end = start;
15360 if (whole < (end - start))
15361 whole = end - start;
15363 else
15364 start = end = whole = 0;
15366 /* Indicate what this scroll bar ought to be displaying now. */
15367 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15368 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15369 (w, end - start, whole, start);
15373 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15374 selected_window is redisplayed.
15376 We can return without actually redisplaying the window if fonts has been
15377 changed on window's frame. In that case, redisplay_internal will retry. */
15379 static void
15380 redisplay_window (Lisp_Object window, int just_this_one_p)
15382 struct window *w = XWINDOW (window);
15383 struct frame *f = XFRAME (w->frame);
15384 struct buffer *buffer = XBUFFER (w->contents);
15385 struct buffer *old = current_buffer;
15386 struct text_pos lpoint, opoint, startp;
15387 int update_mode_line;
15388 int tem;
15389 struct it it;
15390 /* Record it now because it's overwritten. */
15391 int current_matrix_up_to_date_p = 0;
15392 int used_current_matrix_p = 0;
15393 /* This is less strict than current_matrix_up_to_date_p.
15394 It indicates that the buffer contents and narrowing are unchanged. */
15395 int buffer_unchanged_p = 0;
15396 int temp_scroll_step = 0;
15397 ptrdiff_t count = SPECPDL_INDEX ();
15398 int rc;
15399 int centering_position = -1;
15400 int last_line_misfit = 0;
15401 ptrdiff_t beg_unchanged, end_unchanged;
15402 int frame_line_height;
15404 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15405 opoint = lpoint;
15407 #ifdef GLYPH_DEBUG
15408 *w->desired_matrix->method = 0;
15409 #endif
15411 if (!just_this_one_p
15412 && (update_mode_lines == REDISPLAY_SOME
15413 || update_mode_lines == 0)
15414 && (windows_or_buffers_changed == REDISPLAY_SOME
15415 || windows_or_buffers_changed == 0)
15416 && !w->redisplay
15417 && !f->redisplay
15418 && !buffer->text->redisplay)
15419 return;
15421 /* Make sure that both W's markers are valid. */
15422 eassert (XMARKER (w->start)->buffer == buffer);
15423 eassert (XMARKER (w->pointm)->buffer == buffer);
15425 restart:
15426 reconsider_clip_changes (w);
15427 frame_line_height = default_line_pixel_height (w);
15429 /* Has the mode line to be updated? */
15430 update_mode_line = (w->update_mode_line
15431 || update_mode_lines
15432 || buffer->clip_changed
15433 || buffer->prevent_redisplay_optimizations_p);
15435 if (MINI_WINDOW_P (w))
15437 if (w == XWINDOW (echo_area_window)
15438 && !NILP (echo_area_buffer[0]))
15440 if (update_mode_line)
15441 /* We may have to update a tty frame's menu bar or a
15442 tool-bar. Example `M-x C-h C-h C-g'. */
15443 goto finish_menu_bars;
15444 else
15445 /* We've already displayed the echo area glyphs in this window. */
15446 goto finish_scroll_bars;
15448 else if ((w != XWINDOW (minibuf_window)
15449 || minibuf_level == 0)
15450 /* When buffer is nonempty, redisplay window normally. */
15451 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15452 /* Quail displays non-mini buffers in minibuffer window.
15453 In that case, redisplay the window normally. */
15454 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15456 /* W is a mini-buffer window, but it's not active, so clear
15457 it. */
15458 int yb = window_text_bottom_y (w);
15459 struct glyph_row *row;
15460 int y;
15462 for (y = 0, row = w->desired_matrix->rows;
15463 y < yb;
15464 y += row->height, ++row)
15465 blank_row (w, row, y);
15466 goto finish_scroll_bars;
15469 clear_glyph_matrix (w->desired_matrix);
15472 /* Otherwise set up data on this window; select its buffer and point
15473 value. */
15474 /* Really select the buffer, for the sake of buffer-local
15475 variables. */
15476 set_buffer_internal_1 (XBUFFER (w->contents));
15478 current_matrix_up_to_date_p
15479 = (w->window_end_valid
15480 && !current_buffer->clip_changed
15481 && !current_buffer->prevent_redisplay_optimizations_p
15482 && !window_outdated (w));
15484 /* Run the window-bottom-change-functions
15485 if it is possible that the text on the screen has changed
15486 (either due to modification of the text, or any other reason). */
15487 if (!current_matrix_up_to_date_p
15488 && !NILP (Vwindow_text_change_functions))
15490 safe_run_hooks (Qwindow_text_change_functions);
15491 goto restart;
15494 beg_unchanged = BEG_UNCHANGED;
15495 end_unchanged = END_UNCHANGED;
15497 SET_TEXT_POS (opoint, PT, PT_BYTE);
15499 specbind (Qinhibit_point_motion_hooks, Qt);
15501 buffer_unchanged_p
15502 = (w->window_end_valid
15503 && !current_buffer->clip_changed
15504 && !window_outdated (w));
15506 /* When windows_or_buffers_changed is non-zero, we can't rely
15507 on the window end being valid, so set it to zero there. */
15508 if (windows_or_buffers_changed)
15510 /* If window starts on a continuation line, maybe adjust the
15511 window start in case the window's width changed. */
15512 if (XMARKER (w->start)->buffer == current_buffer)
15513 compute_window_start_on_continuation_line (w);
15515 w->window_end_valid = 0;
15516 /* If so, we also can't rely on current matrix
15517 and should not fool try_cursor_movement below. */
15518 current_matrix_up_to_date_p = 0;
15521 /* Some sanity checks. */
15522 CHECK_WINDOW_END (w);
15523 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15524 emacs_abort ();
15525 if (BYTEPOS (opoint) < CHARPOS (opoint))
15526 emacs_abort ();
15528 if (mode_line_update_needed (w))
15529 update_mode_line = 1;
15531 /* Point refers normally to the selected window. For any other
15532 window, set up appropriate value. */
15533 if (!EQ (window, selected_window))
15535 ptrdiff_t new_pt = marker_position (w->pointm);
15536 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15537 if (new_pt < BEGV)
15539 new_pt = BEGV;
15540 new_pt_byte = BEGV_BYTE;
15541 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15543 else if (new_pt > (ZV - 1))
15545 new_pt = ZV;
15546 new_pt_byte = ZV_BYTE;
15547 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15550 /* We don't use SET_PT so that the point-motion hooks don't run. */
15551 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15554 /* If any of the character widths specified in the display table
15555 have changed, invalidate the width run cache. It's true that
15556 this may be a bit late to catch such changes, but the rest of
15557 redisplay goes (non-fatally) haywire when the display table is
15558 changed, so why should we worry about doing any better? */
15559 if (current_buffer->width_run_cache)
15561 struct Lisp_Char_Table *disptab = buffer_display_table ();
15563 if (! disptab_matches_widthtab
15564 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15566 invalidate_region_cache (current_buffer,
15567 current_buffer->width_run_cache,
15568 BEG, Z);
15569 recompute_width_table (current_buffer, disptab);
15573 /* If window-start is screwed up, choose a new one. */
15574 if (XMARKER (w->start)->buffer != current_buffer)
15575 goto recenter;
15577 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15579 /* If someone specified a new starting point but did not insist,
15580 check whether it can be used. */
15581 if (w->optional_new_start
15582 && CHARPOS (startp) >= BEGV
15583 && CHARPOS (startp) <= ZV)
15585 w->optional_new_start = 0;
15586 start_display (&it, w, startp);
15587 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15588 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15589 if (IT_CHARPOS (it) == PT)
15590 w->force_start = 1;
15591 /* IT may overshoot PT if text at PT is invisible. */
15592 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15593 w->force_start = 1;
15596 force_start:
15598 /* Handle case where place to start displaying has been specified,
15599 unless the specified location is outside the accessible range. */
15600 if (w->force_start || window_frozen_p (w))
15602 /* We set this later on if we have to adjust point. */
15603 int new_vpos = -1;
15605 w->force_start = 0;
15606 w->vscroll = 0;
15607 w->window_end_valid = 0;
15609 /* Forget any recorded base line for line number display. */
15610 if (!buffer_unchanged_p)
15611 w->base_line_number = 0;
15613 /* Redisplay the mode line. Select the buffer properly for that.
15614 Also, run the hook window-scroll-functions
15615 because we have scrolled. */
15616 /* Note, we do this after clearing force_start because
15617 if there's an error, it is better to forget about force_start
15618 than to get into an infinite loop calling the hook functions
15619 and having them get more errors. */
15620 if (!update_mode_line
15621 || ! NILP (Vwindow_scroll_functions))
15623 update_mode_line = 1;
15624 w->update_mode_line = 1;
15625 startp = run_window_scroll_functions (window, startp);
15628 if (CHARPOS (startp) < BEGV)
15629 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15630 else if (CHARPOS (startp) > ZV)
15631 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15633 /* Redisplay, then check if cursor has been set during the
15634 redisplay. Give up if new fonts were loaded. */
15635 /* We used to issue a CHECK_MARGINS argument to try_window here,
15636 but this causes scrolling to fail when point begins inside
15637 the scroll margin (bug#148) -- cyd */
15638 if (!try_window (window, startp, 0))
15640 w->force_start = 1;
15641 clear_glyph_matrix (w->desired_matrix);
15642 goto need_larger_matrices;
15645 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15647 /* If point does not appear, try to move point so it does
15648 appear. The desired matrix has been built above, so we
15649 can use it here. */
15650 new_vpos = window_box_height (w) / 2;
15653 if (!cursor_row_fully_visible_p (w, 0, 0))
15655 /* Point does appear, but on a line partly visible at end of window.
15656 Move it back to a fully-visible line. */
15657 new_vpos = window_box_height (w);
15659 else if (w->cursor.vpos >= 0)
15661 /* Some people insist on not letting point enter the scroll
15662 margin, even though this part handles windows that didn't
15663 scroll at all. */
15664 int window_total_lines
15665 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15666 int margin = min (scroll_margin, window_total_lines / 4);
15667 int pixel_margin = margin * frame_line_height;
15668 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15670 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15671 below, which finds the row to move point to, advances by
15672 the Y coordinate of the _next_ row, see the definition of
15673 MATRIX_ROW_BOTTOM_Y. */
15674 if (w->cursor.vpos < margin + header_line)
15676 w->cursor.vpos = -1;
15677 clear_glyph_matrix (w->desired_matrix);
15678 goto try_to_scroll;
15680 else
15682 int window_height = window_box_height (w);
15684 if (header_line)
15685 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15686 if (w->cursor.y >= window_height - pixel_margin)
15688 w->cursor.vpos = -1;
15689 clear_glyph_matrix (w->desired_matrix);
15690 goto try_to_scroll;
15695 /* If we need to move point for either of the above reasons,
15696 now actually do it. */
15697 if (new_vpos >= 0)
15699 struct glyph_row *row;
15701 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15702 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15703 ++row;
15705 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15706 MATRIX_ROW_START_BYTEPOS (row));
15708 if (w != XWINDOW (selected_window))
15709 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15710 else if (current_buffer == old)
15711 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15713 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15715 /* If we are highlighting the region, then we just changed
15716 the region, so redisplay to show it. */
15717 /* FIXME: We need to (re)run pre-redisplay-function! */
15718 /* if (markpos_of_region () >= 0)
15720 clear_glyph_matrix (w->desired_matrix);
15721 if (!try_window (window, startp, 0))
15722 goto need_larger_matrices;
15727 #ifdef GLYPH_DEBUG
15728 debug_method_add (w, "forced window start");
15729 #endif
15730 goto done;
15733 /* Handle case where text has not changed, only point, and it has
15734 not moved off the frame, and we are not retrying after hscroll.
15735 (current_matrix_up_to_date_p is nonzero when retrying.) */
15736 if (current_matrix_up_to_date_p
15737 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15738 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15740 switch (rc)
15742 case CURSOR_MOVEMENT_SUCCESS:
15743 used_current_matrix_p = 1;
15744 goto done;
15746 case CURSOR_MOVEMENT_MUST_SCROLL:
15747 goto try_to_scroll;
15749 default:
15750 emacs_abort ();
15753 /* If current starting point was originally the beginning of a line
15754 but no longer is, find a new starting point. */
15755 else if (w->start_at_line_beg
15756 && !(CHARPOS (startp) <= BEGV
15757 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15759 #ifdef GLYPH_DEBUG
15760 debug_method_add (w, "recenter 1");
15761 #endif
15762 goto recenter;
15765 /* Try scrolling with try_window_id. Value is > 0 if update has
15766 been done, it is -1 if we know that the same window start will
15767 not work. It is 0 if unsuccessful for some other reason. */
15768 else if ((tem = try_window_id (w)) != 0)
15770 #ifdef GLYPH_DEBUG
15771 debug_method_add (w, "try_window_id %d", tem);
15772 #endif
15774 if (f->fonts_changed)
15775 goto need_larger_matrices;
15776 if (tem > 0)
15777 goto done;
15779 /* Otherwise try_window_id has returned -1 which means that we
15780 don't want the alternative below this comment to execute. */
15782 else if (CHARPOS (startp) >= BEGV
15783 && CHARPOS (startp) <= ZV
15784 && PT >= CHARPOS (startp)
15785 && (CHARPOS (startp) < ZV
15786 /* Avoid starting at end of buffer. */
15787 || CHARPOS (startp) == BEGV
15788 || !window_outdated (w)))
15790 int d1, d2, d3, d4, d5, d6;
15792 /* If first window line is a continuation line, and window start
15793 is inside the modified region, but the first change is before
15794 current window start, we must select a new window start.
15796 However, if this is the result of a down-mouse event (e.g. by
15797 extending the mouse-drag-overlay), we don't want to select a
15798 new window start, since that would change the position under
15799 the mouse, resulting in an unwanted mouse-movement rather
15800 than a simple mouse-click. */
15801 if (!w->start_at_line_beg
15802 && NILP (do_mouse_tracking)
15803 && CHARPOS (startp) > BEGV
15804 && CHARPOS (startp) > BEG + beg_unchanged
15805 && CHARPOS (startp) <= Z - end_unchanged
15806 /* Even if w->start_at_line_beg is nil, a new window may
15807 start at a line_beg, since that's how set_buffer_window
15808 sets it. So, we need to check the return value of
15809 compute_window_start_on_continuation_line. (See also
15810 bug#197). */
15811 && XMARKER (w->start)->buffer == current_buffer
15812 && compute_window_start_on_continuation_line (w)
15813 /* It doesn't make sense to force the window start like we
15814 do at label force_start if it is already known that point
15815 will not be visible in the resulting window, because
15816 doing so will move point from its correct position
15817 instead of scrolling the window to bring point into view.
15818 See bug#9324. */
15819 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15821 w->force_start = 1;
15822 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15823 goto force_start;
15826 #ifdef GLYPH_DEBUG
15827 debug_method_add (w, "same window start");
15828 #endif
15830 /* Try to redisplay starting at same place as before.
15831 If point has not moved off frame, accept the results. */
15832 if (!current_matrix_up_to_date_p
15833 /* Don't use try_window_reusing_current_matrix in this case
15834 because a window scroll function can have changed the
15835 buffer. */
15836 || !NILP (Vwindow_scroll_functions)
15837 || MINI_WINDOW_P (w)
15838 || !(used_current_matrix_p
15839 = try_window_reusing_current_matrix (w)))
15841 IF_DEBUG (debug_method_add (w, "1"));
15842 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15843 /* -1 means we need to scroll.
15844 0 means we need new matrices, but fonts_changed
15845 is set in that case, so we will detect it below. */
15846 goto try_to_scroll;
15849 if (f->fonts_changed)
15850 goto need_larger_matrices;
15852 if (w->cursor.vpos >= 0)
15854 if (!just_this_one_p
15855 || current_buffer->clip_changed
15856 || BEG_UNCHANGED < CHARPOS (startp))
15857 /* Forget any recorded base line for line number display. */
15858 w->base_line_number = 0;
15860 if (!cursor_row_fully_visible_p (w, 1, 0))
15862 clear_glyph_matrix (w->desired_matrix);
15863 last_line_misfit = 1;
15865 /* Drop through and scroll. */
15866 else
15867 goto done;
15869 else
15870 clear_glyph_matrix (w->desired_matrix);
15873 try_to_scroll:
15875 /* Redisplay the mode line. Select the buffer properly for that. */
15876 if (!update_mode_line)
15878 update_mode_line = 1;
15879 w->update_mode_line = 1;
15882 /* Try to scroll by specified few lines. */
15883 if ((scroll_conservatively
15884 || emacs_scroll_step
15885 || temp_scroll_step
15886 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15887 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15888 && CHARPOS (startp) >= BEGV
15889 && CHARPOS (startp) <= ZV)
15891 /* The function returns -1 if new fonts were loaded, 1 if
15892 successful, 0 if not successful. */
15893 int ss = try_scrolling (window, just_this_one_p,
15894 scroll_conservatively,
15895 emacs_scroll_step,
15896 temp_scroll_step, last_line_misfit);
15897 switch (ss)
15899 case SCROLLING_SUCCESS:
15900 goto done;
15902 case SCROLLING_NEED_LARGER_MATRICES:
15903 goto need_larger_matrices;
15905 case SCROLLING_FAILED:
15906 break;
15908 default:
15909 emacs_abort ();
15913 /* Finally, just choose a place to start which positions point
15914 according to user preferences. */
15916 recenter:
15918 #ifdef GLYPH_DEBUG
15919 debug_method_add (w, "recenter");
15920 #endif
15922 /* Forget any previously recorded base line for line number display. */
15923 if (!buffer_unchanged_p)
15924 w->base_line_number = 0;
15926 /* Determine the window start relative to point. */
15927 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15928 it.current_y = it.last_visible_y;
15929 if (centering_position < 0)
15931 int window_total_lines
15932 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15933 int margin =
15934 scroll_margin > 0
15935 ? min (scroll_margin, window_total_lines / 4)
15936 : 0;
15937 ptrdiff_t margin_pos = CHARPOS (startp);
15938 Lisp_Object aggressive;
15939 int scrolling_up;
15941 /* If there is a scroll margin at the top of the window, find
15942 its character position. */
15943 if (margin
15944 /* Cannot call start_display if startp is not in the
15945 accessible region of the buffer. This can happen when we
15946 have just switched to a different buffer and/or changed
15947 its restriction. In that case, startp is initialized to
15948 the character position 1 (BEGV) because we did not yet
15949 have chance to display the buffer even once. */
15950 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15952 struct it it1;
15953 void *it1data = NULL;
15955 SAVE_IT (it1, it, it1data);
15956 start_display (&it1, w, startp);
15957 move_it_vertically (&it1, margin * frame_line_height);
15958 margin_pos = IT_CHARPOS (it1);
15959 RESTORE_IT (&it, &it, it1data);
15961 scrolling_up = PT > margin_pos;
15962 aggressive =
15963 scrolling_up
15964 ? BVAR (current_buffer, scroll_up_aggressively)
15965 : BVAR (current_buffer, scroll_down_aggressively);
15967 if (!MINI_WINDOW_P (w)
15968 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15970 int pt_offset = 0;
15972 /* Setting scroll-conservatively overrides
15973 scroll-*-aggressively. */
15974 if (!scroll_conservatively && NUMBERP (aggressive))
15976 double float_amount = XFLOATINT (aggressive);
15978 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15979 if (pt_offset == 0 && float_amount > 0)
15980 pt_offset = 1;
15981 if (pt_offset && margin > 0)
15982 margin -= 1;
15984 /* Compute how much to move the window start backward from
15985 point so that point will be displayed where the user
15986 wants it. */
15987 if (scrolling_up)
15989 centering_position = it.last_visible_y;
15990 if (pt_offset)
15991 centering_position -= pt_offset;
15992 centering_position -=
15993 frame_line_height * (1 + margin + (last_line_misfit != 0))
15994 + WINDOW_HEADER_LINE_HEIGHT (w);
15995 /* Don't let point enter the scroll margin near top of
15996 the window. */
15997 if (centering_position < margin * frame_line_height)
15998 centering_position = margin * frame_line_height;
16000 else
16001 centering_position = margin * frame_line_height + pt_offset;
16003 else
16004 /* Set the window start half the height of the window backward
16005 from point. */
16006 centering_position = window_box_height (w) / 2;
16008 move_it_vertically_backward (&it, centering_position);
16010 eassert (IT_CHARPOS (it) >= BEGV);
16012 /* The function move_it_vertically_backward may move over more
16013 than the specified y-distance. If it->w is small, e.g. a
16014 mini-buffer window, we may end up in front of the window's
16015 display area. Start displaying at the start of the line
16016 containing PT in this case. */
16017 if (it.current_y <= 0)
16019 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16020 move_it_vertically_backward (&it, 0);
16021 it.current_y = 0;
16024 it.current_x = it.hpos = 0;
16026 /* Set the window start position here explicitly, to avoid an
16027 infinite loop in case the functions in window-scroll-functions
16028 get errors. */
16029 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16031 /* Run scroll hooks. */
16032 startp = run_window_scroll_functions (window, it.current.pos);
16034 /* Redisplay the window. */
16035 if (!current_matrix_up_to_date_p
16036 || windows_or_buffers_changed
16037 || f->cursor_type_changed
16038 /* Don't use try_window_reusing_current_matrix in this case
16039 because it can have changed the buffer. */
16040 || !NILP (Vwindow_scroll_functions)
16041 || !just_this_one_p
16042 || MINI_WINDOW_P (w)
16043 || !(used_current_matrix_p
16044 = try_window_reusing_current_matrix (w)))
16045 try_window (window, startp, 0);
16047 /* If new fonts have been loaded (due to fontsets), give up. We
16048 have to start a new redisplay since we need to re-adjust glyph
16049 matrices. */
16050 if (f->fonts_changed)
16051 goto need_larger_matrices;
16053 /* If cursor did not appear assume that the middle of the window is
16054 in the first line of the window. Do it again with the next line.
16055 (Imagine a window of height 100, displaying two lines of height
16056 60. Moving back 50 from it->last_visible_y will end in the first
16057 line.) */
16058 if (w->cursor.vpos < 0)
16060 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16062 clear_glyph_matrix (w->desired_matrix);
16063 move_it_by_lines (&it, 1);
16064 try_window (window, it.current.pos, 0);
16066 else if (PT < IT_CHARPOS (it))
16068 clear_glyph_matrix (w->desired_matrix);
16069 move_it_by_lines (&it, -1);
16070 try_window (window, it.current.pos, 0);
16072 else
16074 /* Not much we can do about it. */
16078 /* Consider the following case: Window starts at BEGV, there is
16079 invisible, intangible text at BEGV, so that display starts at
16080 some point START > BEGV. It can happen that we are called with
16081 PT somewhere between BEGV and START. Try to handle that case. */
16082 if (w->cursor.vpos < 0)
16084 struct glyph_row *row = w->current_matrix->rows;
16085 if (row->mode_line_p)
16086 ++row;
16087 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16090 if (!cursor_row_fully_visible_p (w, 0, 0))
16092 /* If vscroll is enabled, disable it and try again. */
16093 if (w->vscroll)
16095 w->vscroll = 0;
16096 clear_glyph_matrix (w->desired_matrix);
16097 goto recenter;
16100 /* Users who set scroll-conservatively to a large number want
16101 point just above/below the scroll margin. If we ended up
16102 with point's row partially visible, move the window start to
16103 make that row fully visible and out of the margin. */
16104 if (scroll_conservatively > SCROLL_LIMIT)
16106 int window_total_lines
16107 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16108 int margin =
16109 scroll_margin > 0
16110 ? min (scroll_margin, window_total_lines / 4)
16111 : 0;
16112 int move_down = w->cursor.vpos >= window_total_lines / 2;
16114 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16115 clear_glyph_matrix (w->desired_matrix);
16116 if (1 == try_window (window, it.current.pos,
16117 TRY_WINDOW_CHECK_MARGINS))
16118 goto done;
16121 /* If centering point failed to make the whole line visible,
16122 put point at the top instead. That has to make the whole line
16123 visible, if it can be done. */
16124 if (centering_position == 0)
16125 goto done;
16127 clear_glyph_matrix (w->desired_matrix);
16128 centering_position = 0;
16129 goto recenter;
16132 done:
16134 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16135 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16136 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16138 /* Display the mode line, if we must. */
16139 if ((update_mode_line
16140 /* If window not full width, must redo its mode line
16141 if (a) the window to its side is being redone and
16142 (b) we do a frame-based redisplay. This is a consequence
16143 of how inverted lines are drawn in frame-based redisplay. */
16144 || (!just_this_one_p
16145 && !FRAME_WINDOW_P (f)
16146 && !WINDOW_FULL_WIDTH_P (w))
16147 /* Line number to display. */
16148 || w->base_line_pos > 0
16149 /* Column number is displayed and different from the one displayed. */
16150 || (w->column_number_displayed != -1
16151 && (w->column_number_displayed != current_column ())))
16152 /* This means that the window has a mode line. */
16153 && (WINDOW_WANTS_MODELINE_P (w)
16154 || WINDOW_WANTS_HEADER_LINE_P (w)))
16156 display_mode_lines (w);
16158 /* If mode line height has changed, arrange for a thorough
16159 immediate redisplay using the correct mode line height. */
16160 if (WINDOW_WANTS_MODELINE_P (w)
16161 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16163 f->fonts_changed = 1;
16164 w->mode_line_height = -1;
16165 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16166 = DESIRED_MODE_LINE_HEIGHT (w);
16169 /* If header line height has changed, arrange for a thorough
16170 immediate redisplay using the correct header line height. */
16171 if (WINDOW_WANTS_HEADER_LINE_P (w)
16172 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16174 f->fonts_changed = 1;
16175 w->header_line_height = -1;
16176 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16177 = DESIRED_HEADER_LINE_HEIGHT (w);
16180 if (f->fonts_changed)
16181 goto need_larger_matrices;
16184 if (!line_number_displayed && w->base_line_pos != -1)
16186 w->base_line_pos = 0;
16187 w->base_line_number = 0;
16190 finish_menu_bars:
16192 /* When we reach a frame's selected window, redo the frame's menu bar. */
16193 if (update_mode_line
16194 && EQ (FRAME_SELECTED_WINDOW (f), window))
16196 int redisplay_menu_p = 0;
16198 if (FRAME_WINDOW_P (f))
16200 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16201 || defined (HAVE_NS) || defined (USE_GTK)
16202 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16203 #else
16204 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16205 #endif
16207 else
16208 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16210 if (redisplay_menu_p)
16211 display_menu_bar (w);
16213 #ifdef HAVE_WINDOW_SYSTEM
16214 if (FRAME_WINDOW_P (f))
16216 #if defined (USE_GTK) || defined (HAVE_NS)
16217 if (FRAME_EXTERNAL_TOOL_BAR (f))
16218 redisplay_tool_bar (f);
16219 #else
16220 if (WINDOWP (f->tool_bar_window)
16221 && (FRAME_TOOL_BAR_LINES (f) > 0
16222 || !NILP (Vauto_resize_tool_bars))
16223 && redisplay_tool_bar (f))
16224 ignore_mouse_drag_p = 1;
16225 #endif
16227 #endif
16230 #ifdef HAVE_WINDOW_SYSTEM
16231 if (FRAME_WINDOW_P (f)
16232 && update_window_fringes (w, (just_this_one_p
16233 || (!used_current_matrix_p && !overlay_arrow_seen)
16234 || w->pseudo_window_p)))
16236 update_begin (f);
16237 block_input ();
16238 if (draw_window_fringes (w, 1))
16239 x_draw_vertical_border (w);
16240 unblock_input ();
16241 update_end (f);
16243 #endif /* HAVE_WINDOW_SYSTEM */
16245 /* We go to this label, with fonts_changed set, if it is
16246 necessary to try again using larger glyph matrices.
16247 We have to redeem the scroll bar even in this case,
16248 because the loop in redisplay_internal expects that. */
16249 need_larger_matrices:
16251 finish_scroll_bars:
16253 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16255 /* Set the thumb's position and size. */
16256 set_vertical_scroll_bar (w);
16258 /* Note that we actually used the scroll bar attached to this
16259 window, so it shouldn't be deleted at the end of redisplay. */
16260 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16261 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16264 /* Restore current_buffer and value of point in it. The window
16265 update may have changed the buffer, so first make sure `opoint'
16266 is still valid (Bug#6177). */
16267 if (CHARPOS (opoint) < BEGV)
16268 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16269 else if (CHARPOS (opoint) > ZV)
16270 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16271 else
16272 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16274 set_buffer_internal_1 (old);
16275 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16276 shorter. This can be caused by log truncation in *Messages*. */
16277 if (CHARPOS (lpoint) <= ZV)
16278 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16280 unbind_to (count, Qnil);
16284 /* Build the complete desired matrix of WINDOW with a window start
16285 buffer position POS.
16287 Value is 1 if successful. It is zero if fonts were loaded during
16288 redisplay which makes re-adjusting glyph matrices necessary, and -1
16289 if point would appear in the scroll margins.
16290 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16291 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16292 set in FLAGS.) */
16295 try_window (Lisp_Object window, struct text_pos pos, int flags)
16297 struct window *w = XWINDOW (window);
16298 struct it it;
16299 struct glyph_row *last_text_row = NULL;
16300 struct frame *f = XFRAME (w->frame);
16301 int frame_line_height = default_line_pixel_height (w);
16303 /* Make POS the new window start. */
16304 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16306 /* Mark cursor position as unknown. No overlay arrow seen. */
16307 w->cursor.vpos = -1;
16308 overlay_arrow_seen = 0;
16310 /* Initialize iterator and info to start at POS. */
16311 start_display (&it, w, pos);
16313 /* Display all lines of W. */
16314 while (it.current_y < it.last_visible_y)
16316 if (display_line (&it))
16317 last_text_row = it.glyph_row - 1;
16318 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16319 return 0;
16322 /* Don't let the cursor end in the scroll margins. */
16323 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16324 && !MINI_WINDOW_P (w))
16326 int this_scroll_margin;
16327 int window_total_lines
16328 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16330 if (scroll_margin > 0)
16332 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16333 this_scroll_margin *= frame_line_height;
16335 else
16336 this_scroll_margin = 0;
16338 if ((w->cursor.y >= 0 /* not vscrolled */
16339 && w->cursor.y < this_scroll_margin
16340 && CHARPOS (pos) > BEGV
16341 && IT_CHARPOS (it) < ZV)
16342 /* rms: considering make_cursor_line_fully_visible_p here
16343 seems to give wrong results. We don't want to recenter
16344 when the last line is partly visible, we want to allow
16345 that case to be handled in the usual way. */
16346 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16348 w->cursor.vpos = -1;
16349 clear_glyph_matrix (w->desired_matrix);
16350 return -1;
16354 /* If bottom moved off end of frame, change mode line percentage. */
16355 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16356 w->update_mode_line = 1;
16358 /* Set window_end_pos to the offset of the last character displayed
16359 on the window from the end of current_buffer. Set
16360 window_end_vpos to its row number. */
16361 if (last_text_row)
16363 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16364 adjust_window_ends (w, last_text_row, 0);
16365 eassert
16366 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16367 w->window_end_vpos)));
16369 else
16371 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16372 w->window_end_pos = Z - ZV;
16373 w->window_end_vpos = 0;
16376 /* But that is not valid info until redisplay finishes. */
16377 w->window_end_valid = 0;
16378 return 1;
16383 /************************************************************************
16384 Window redisplay reusing current matrix when buffer has not changed
16385 ************************************************************************/
16387 /* Try redisplay of window W showing an unchanged buffer with a
16388 different window start than the last time it was displayed by
16389 reusing its current matrix. Value is non-zero if successful.
16390 W->start is the new window start. */
16392 static int
16393 try_window_reusing_current_matrix (struct window *w)
16395 struct frame *f = XFRAME (w->frame);
16396 struct glyph_row *bottom_row;
16397 struct it it;
16398 struct run run;
16399 struct text_pos start, new_start;
16400 int nrows_scrolled, i;
16401 struct glyph_row *last_text_row;
16402 struct glyph_row *last_reused_text_row;
16403 struct glyph_row *start_row;
16404 int start_vpos, min_y, max_y;
16406 #ifdef GLYPH_DEBUG
16407 if (inhibit_try_window_reusing)
16408 return 0;
16409 #endif
16411 if (/* This function doesn't handle terminal frames. */
16412 !FRAME_WINDOW_P (f)
16413 /* Don't try to reuse the display if windows have been split
16414 or such. */
16415 || windows_or_buffers_changed
16416 || f->cursor_type_changed)
16417 return 0;
16419 /* Can't do this if showing trailing whitespace. */
16420 if (!NILP (Vshow_trailing_whitespace))
16421 return 0;
16423 /* If top-line visibility has changed, give up. */
16424 if (WINDOW_WANTS_HEADER_LINE_P (w)
16425 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16426 return 0;
16428 /* Give up if old or new display is scrolled vertically. We could
16429 make this function handle this, but right now it doesn't. */
16430 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16431 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16432 return 0;
16434 /* The variable new_start now holds the new window start. The old
16435 start `start' can be determined from the current matrix. */
16436 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16437 start = start_row->minpos;
16438 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16440 /* Clear the desired matrix for the display below. */
16441 clear_glyph_matrix (w->desired_matrix);
16443 if (CHARPOS (new_start) <= CHARPOS (start))
16445 /* Don't use this method if the display starts with an ellipsis
16446 displayed for invisible text. It's not easy to handle that case
16447 below, and it's certainly not worth the effort since this is
16448 not a frequent case. */
16449 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16450 return 0;
16452 IF_DEBUG (debug_method_add (w, "twu1"));
16454 /* Display up to a row that can be reused. The variable
16455 last_text_row is set to the last row displayed that displays
16456 text. Note that it.vpos == 0 if or if not there is a
16457 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16458 start_display (&it, w, new_start);
16459 w->cursor.vpos = -1;
16460 last_text_row = last_reused_text_row = NULL;
16462 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16464 /* If we have reached into the characters in the START row,
16465 that means the line boundaries have changed. So we
16466 can't start copying with the row START. Maybe it will
16467 work to start copying with the following row. */
16468 while (IT_CHARPOS (it) > CHARPOS (start))
16470 /* Advance to the next row as the "start". */
16471 start_row++;
16472 start = start_row->minpos;
16473 /* If there are no more rows to try, or just one, give up. */
16474 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16475 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16476 || CHARPOS (start) == ZV)
16478 clear_glyph_matrix (w->desired_matrix);
16479 return 0;
16482 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16484 /* If we have reached alignment, we can copy the rest of the
16485 rows. */
16486 if (IT_CHARPOS (it) == CHARPOS (start)
16487 /* Don't accept "alignment" inside a display vector,
16488 since start_row could have started in the middle of
16489 that same display vector (thus their character
16490 positions match), and we have no way of telling if
16491 that is the case. */
16492 && it.current.dpvec_index < 0)
16493 break;
16495 if (display_line (&it))
16496 last_text_row = it.glyph_row - 1;
16500 /* A value of current_y < last_visible_y means that we stopped
16501 at the previous window start, which in turn means that we
16502 have at least one reusable row. */
16503 if (it.current_y < it.last_visible_y)
16505 struct glyph_row *row;
16507 /* IT.vpos always starts from 0; it counts text lines. */
16508 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16510 /* Find PT if not already found in the lines displayed. */
16511 if (w->cursor.vpos < 0)
16513 int dy = it.current_y - start_row->y;
16515 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16516 row = row_containing_pos (w, PT, row, NULL, dy);
16517 if (row)
16518 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16519 dy, nrows_scrolled);
16520 else
16522 clear_glyph_matrix (w->desired_matrix);
16523 return 0;
16527 /* Scroll the display. Do it before the current matrix is
16528 changed. The problem here is that update has not yet
16529 run, i.e. part of the current matrix is not up to date.
16530 scroll_run_hook will clear the cursor, and use the
16531 current matrix to get the height of the row the cursor is
16532 in. */
16533 run.current_y = start_row->y;
16534 run.desired_y = it.current_y;
16535 run.height = it.last_visible_y - it.current_y;
16537 if (run.height > 0 && run.current_y != run.desired_y)
16539 update_begin (f);
16540 FRAME_RIF (f)->update_window_begin_hook (w);
16541 FRAME_RIF (f)->clear_window_mouse_face (w);
16542 FRAME_RIF (f)->scroll_run_hook (w, &run);
16543 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16544 update_end (f);
16547 /* Shift current matrix down by nrows_scrolled lines. */
16548 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16549 rotate_matrix (w->current_matrix,
16550 start_vpos,
16551 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16552 nrows_scrolled);
16554 /* Disable lines that must be updated. */
16555 for (i = 0; i < nrows_scrolled; ++i)
16556 (start_row + i)->enabled_p = 0;
16558 /* Re-compute Y positions. */
16559 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16560 max_y = it.last_visible_y;
16561 for (row = start_row + nrows_scrolled;
16562 row < bottom_row;
16563 ++row)
16565 row->y = it.current_y;
16566 row->visible_height = row->height;
16568 if (row->y < min_y)
16569 row->visible_height -= min_y - row->y;
16570 if (row->y + row->height > max_y)
16571 row->visible_height -= row->y + row->height - max_y;
16572 if (row->fringe_bitmap_periodic_p)
16573 row->redraw_fringe_bitmaps_p = 1;
16575 it.current_y += row->height;
16577 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16578 last_reused_text_row = row;
16579 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16580 break;
16583 /* Disable lines in the current matrix which are now
16584 below the window. */
16585 for (++row; row < bottom_row; ++row)
16586 row->enabled_p = row->mode_line_p = 0;
16589 /* Update window_end_pos etc.; last_reused_text_row is the last
16590 reused row from the current matrix containing text, if any.
16591 The value of last_text_row is the last displayed line
16592 containing text. */
16593 if (last_reused_text_row)
16594 adjust_window_ends (w, last_reused_text_row, 1);
16595 else if (last_text_row)
16596 adjust_window_ends (w, last_text_row, 0);
16597 else
16599 /* This window must be completely empty. */
16600 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16601 w->window_end_pos = Z - ZV;
16602 w->window_end_vpos = 0;
16604 w->window_end_valid = 0;
16606 /* Update hint: don't try scrolling again in update_window. */
16607 w->desired_matrix->no_scrolling_p = 1;
16609 #ifdef GLYPH_DEBUG
16610 debug_method_add (w, "try_window_reusing_current_matrix 1");
16611 #endif
16612 return 1;
16614 else if (CHARPOS (new_start) > CHARPOS (start))
16616 struct glyph_row *pt_row, *row;
16617 struct glyph_row *first_reusable_row;
16618 struct glyph_row *first_row_to_display;
16619 int dy;
16620 int yb = window_text_bottom_y (w);
16622 /* Find the row starting at new_start, if there is one. Don't
16623 reuse a partially visible line at the end. */
16624 first_reusable_row = start_row;
16625 while (first_reusable_row->enabled_p
16626 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16627 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16628 < CHARPOS (new_start)))
16629 ++first_reusable_row;
16631 /* Give up if there is no row to reuse. */
16632 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16633 || !first_reusable_row->enabled_p
16634 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16635 != CHARPOS (new_start)))
16636 return 0;
16638 /* We can reuse fully visible rows beginning with
16639 first_reusable_row to the end of the window. Set
16640 first_row_to_display to the first row that cannot be reused.
16641 Set pt_row to the row containing point, if there is any. */
16642 pt_row = NULL;
16643 for (first_row_to_display = first_reusable_row;
16644 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16645 ++first_row_to_display)
16647 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16648 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16649 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16650 && first_row_to_display->ends_at_zv_p
16651 && pt_row == NULL)))
16652 pt_row = first_row_to_display;
16655 /* Start displaying at the start of first_row_to_display. */
16656 eassert (first_row_to_display->y < yb);
16657 init_to_row_start (&it, w, first_row_to_display);
16659 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16660 - start_vpos);
16661 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16662 - nrows_scrolled);
16663 it.current_y = (first_row_to_display->y - first_reusable_row->y
16664 + WINDOW_HEADER_LINE_HEIGHT (w));
16666 /* Display lines beginning with first_row_to_display in the
16667 desired matrix. Set last_text_row to the last row displayed
16668 that displays text. */
16669 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16670 if (pt_row == NULL)
16671 w->cursor.vpos = -1;
16672 last_text_row = NULL;
16673 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16674 if (display_line (&it))
16675 last_text_row = it.glyph_row - 1;
16677 /* If point is in a reused row, adjust y and vpos of the cursor
16678 position. */
16679 if (pt_row)
16681 w->cursor.vpos -= nrows_scrolled;
16682 w->cursor.y -= first_reusable_row->y - start_row->y;
16685 /* Give up if point isn't in a row displayed or reused. (This
16686 also handles the case where w->cursor.vpos < nrows_scrolled
16687 after the calls to display_line, which can happen with scroll
16688 margins. See bug#1295.) */
16689 if (w->cursor.vpos < 0)
16691 clear_glyph_matrix (w->desired_matrix);
16692 return 0;
16695 /* Scroll the display. */
16696 run.current_y = first_reusable_row->y;
16697 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16698 run.height = it.last_visible_y - run.current_y;
16699 dy = run.current_y - run.desired_y;
16701 if (run.height)
16703 update_begin (f);
16704 FRAME_RIF (f)->update_window_begin_hook (w);
16705 FRAME_RIF (f)->clear_window_mouse_face (w);
16706 FRAME_RIF (f)->scroll_run_hook (w, &run);
16707 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16708 update_end (f);
16711 /* Adjust Y positions of reused rows. */
16712 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16713 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16714 max_y = it.last_visible_y;
16715 for (row = first_reusable_row; row < first_row_to_display; ++row)
16717 row->y -= dy;
16718 row->visible_height = row->height;
16719 if (row->y < min_y)
16720 row->visible_height -= min_y - row->y;
16721 if (row->y + row->height > max_y)
16722 row->visible_height -= row->y + row->height - max_y;
16723 if (row->fringe_bitmap_periodic_p)
16724 row->redraw_fringe_bitmaps_p = 1;
16727 /* Scroll the current matrix. */
16728 eassert (nrows_scrolled > 0);
16729 rotate_matrix (w->current_matrix,
16730 start_vpos,
16731 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16732 -nrows_scrolled);
16734 /* Disable rows not reused. */
16735 for (row -= nrows_scrolled; row < bottom_row; ++row)
16736 row->enabled_p = 0;
16738 /* Point may have moved to a different line, so we cannot assume that
16739 the previous cursor position is valid; locate the correct row. */
16740 if (pt_row)
16742 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16743 row < bottom_row
16744 && PT >= MATRIX_ROW_END_CHARPOS (row)
16745 && !row->ends_at_zv_p;
16746 row++)
16748 w->cursor.vpos++;
16749 w->cursor.y = row->y;
16751 if (row < bottom_row)
16753 /* Can't simply scan the row for point with
16754 bidi-reordered glyph rows. Let set_cursor_from_row
16755 figure out where to put the cursor, and if it fails,
16756 give up. */
16757 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16759 if (!set_cursor_from_row (w, row, w->current_matrix,
16760 0, 0, 0, 0))
16762 clear_glyph_matrix (w->desired_matrix);
16763 return 0;
16766 else
16768 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16769 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16771 for (; glyph < end
16772 && (!BUFFERP (glyph->object)
16773 || glyph->charpos < PT);
16774 glyph++)
16776 w->cursor.hpos++;
16777 w->cursor.x += glyph->pixel_width;
16783 /* Adjust window end. A null value of last_text_row means that
16784 the window end is in reused rows which in turn means that
16785 only its vpos can have changed. */
16786 if (last_text_row)
16787 adjust_window_ends (w, last_text_row, 0);
16788 else
16789 w->window_end_vpos -= nrows_scrolled;
16791 w->window_end_valid = 0;
16792 w->desired_matrix->no_scrolling_p = 1;
16794 #ifdef GLYPH_DEBUG
16795 debug_method_add (w, "try_window_reusing_current_matrix 2");
16796 #endif
16797 return 1;
16800 return 0;
16805 /************************************************************************
16806 Window redisplay reusing current matrix when buffer has changed
16807 ************************************************************************/
16809 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16810 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16811 ptrdiff_t *, ptrdiff_t *);
16812 static struct glyph_row *
16813 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16814 struct glyph_row *);
16817 /* Return the last row in MATRIX displaying text. If row START is
16818 non-null, start searching with that row. IT gives the dimensions
16819 of the display. Value is null if matrix is empty; otherwise it is
16820 a pointer to the row found. */
16822 static struct glyph_row *
16823 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16824 struct glyph_row *start)
16826 struct glyph_row *row, *row_found;
16828 /* Set row_found to the last row in IT->w's current matrix
16829 displaying text. The loop looks funny but think of partially
16830 visible lines. */
16831 row_found = NULL;
16832 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16833 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16835 eassert (row->enabled_p);
16836 row_found = row;
16837 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16838 break;
16839 ++row;
16842 return row_found;
16846 /* Return the last row in the current matrix of W that is not affected
16847 by changes at the start of current_buffer that occurred since W's
16848 current matrix was built. Value is null if no such row exists.
16850 BEG_UNCHANGED us the number of characters unchanged at the start of
16851 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16852 first changed character in current_buffer. Characters at positions <
16853 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16854 when the current matrix was built. */
16856 static struct glyph_row *
16857 find_last_unchanged_at_beg_row (struct window *w)
16859 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16860 struct glyph_row *row;
16861 struct glyph_row *row_found = NULL;
16862 int yb = window_text_bottom_y (w);
16864 /* Find the last row displaying unchanged text. */
16865 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16866 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16867 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16868 ++row)
16870 if (/* If row ends before first_changed_pos, it is unchanged,
16871 except in some case. */
16872 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16873 /* When row ends in ZV and we write at ZV it is not
16874 unchanged. */
16875 && !row->ends_at_zv_p
16876 /* When first_changed_pos is the end of a continued line,
16877 row is not unchanged because it may be no longer
16878 continued. */
16879 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16880 && (row->continued_p
16881 || row->exact_window_width_line_p))
16882 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16883 needs to be recomputed, so don't consider this row as
16884 unchanged. This happens when the last line was
16885 bidi-reordered and was killed immediately before this
16886 redisplay cycle. In that case, ROW->end stores the
16887 buffer position of the first visual-order character of
16888 the killed text, which is now beyond ZV. */
16889 && CHARPOS (row->end.pos) <= ZV)
16890 row_found = row;
16892 /* Stop if last visible row. */
16893 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16894 break;
16897 return row_found;
16901 /* Find the first glyph row in the current matrix of W that is not
16902 affected by changes at the end of current_buffer since the
16903 time W's current matrix was built.
16905 Return in *DELTA the number of chars by which buffer positions in
16906 unchanged text at the end of current_buffer must be adjusted.
16908 Return in *DELTA_BYTES the corresponding number of bytes.
16910 Value is null if no such row exists, i.e. all rows are affected by
16911 changes. */
16913 static struct glyph_row *
16914 find_first_unchanged_at_end_row (struct window *w,
16915 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16917 struct glyph_row *row;
16918 struct glyph_row *row_found = NULL;
16920 *delta = *delta_bytes = 0;
16922 /* Display must not have been paused, otherwise the current matrix
16923 is not up to date. */
16924 eassert (w->window_end_valid);
16926 /* A value of window_end_pos >= END_UNCHANGED means that the window
16927 end is in the range of changed text. If so, there is no
16928 unchanged row at the end of W's current matrix. */
16929 if (w->window_end_pos >= END_UNCHANGED)
16930 return NULL;
16932 /* Set row to the last row in W's current matrix displaying text. */
16933 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16935 /* If matrix is entirely empty, no unchanged row exists. */
16936 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16938 /* The value of row is the last glyph row in the matrix having a
16939 meaningful buffer position in it. The end position of row
16940 corresponds to window_end_pos. This allows us to translate
16941 buffer positions in the current matrix to current buffer
16942 positions for characters not in changed text. */
16943 ptrdiff_t Z_old =
16944 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16945 ptrdiff_t Z_BYTE_old =
16946 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16947 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16948 struct glyph_row *first_text_row
16949 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16951 *delta = Z - Z_old;
16952 *delta_bytes = Z_BYTE - Z_BYTE_old;
16954 /* Set last_unchanged_pos to the buffer position of the last
16955 character in the buffer that has not been changed. Z is the
16956 index + 1 of the last character in current_buffer, i.e. by
16957 subtracting END_UNCHANGED we get the index of the last
16958 unchanged character, and we have to add BEG to get its buffer
16959 position. */
16960 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16961 last_unchanged_pos_old = last_unchanged_pos - *delta;
16963 /* Search backward from ROW for a row displaying a line that
16964 starts at a minimum position >= last_unchanged_pos_old. */
16965 for (; row > first_text_row; --row)
16967 /* This used to abort, but it can happen.
16968 It is ok to just stop the search instead here. KFS. */
16969 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16970 break;
16972 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16973 row_found = row;
16977 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16979 return row_found;
16983 /* Make sure that glyph rows in the current matrix of window W
16984 reference the same glyph memory as corresponding rows in the
16985 frame's frame matrix. This function is called after scrolling W's
16986 current matrix on a terminal frame in try_window_id and
16987 try_window_reusing_current_matrix. */
16989 static void
16990 sync_frame_with_window_matrix_rows (struct window *w)
16992 struct frame *f = XFRAME (w->frame);
16993 struct glyph_row *window_row, *window_row_end, *frame_row;
16995 /* Preconditions: W must be a leaf window and full-width. Its frame
16996 must have a frame matrix. */
16997 eassert (BUFFERP (w->contents));
16998 eassert (WINDOW_FULL_WIDTH_P (w));
16999 eassert (!FRAME_WINDOW_P (f));
17001 /* If W is a full-width window, glyph pointers in W's current matrix
17002 have, by definition, to be the same as glyph pointers in the
17003 corresponding frame matrix. Note that frame matrices have no
17004 marginal areas (see build_frame_matrix). */
17005 window_row = w->current_matrix->rows;
17006 window_row_end = window_row + w->current_matrix->nrows;
17007 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17008 while (window_row < window_row_end)
17010 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17011 struct glyph *end = window_row->glyphs[LAST_AREA];
17013 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17014 frame_row->glyphs[TEXT_AREA] = start;
17015 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17016 frame_row->glyphs[LAST_AREA] = end;
17018 /* Disable frame rows whose corresponding window rows have
17019 been disabled in try_window_id. */
17020 if (!window_row->enabled_p)
17021 frame_row->enabled_p = 0;
17023 ++window_row, ++frame_row;
17028 /* Find the glyph row in window W containing CHARPOS. Consider all
17029 rows between START and END (not inclusive). END null means search
17030 all rows to the end of the display area of W. Value is the row
17031 containing CHARPOS or null. */
17033 struct glyph_row *
17034 row_containing_pos (struct window *w, ptrdiff_t charpos,
17035 struct glyph_row *start, struct glyph_row *end, int dy)
17037 struct glyph_row *row = start;
17038 struct glyph_row *best_row = NULL;
17039 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17040 int last_y;
17042 /* If we happen to start on a header-line, skip that. */
17043 if (row->mode_line_p)
17044 ++row;
17046 if ((end && row >= end) || !row->enabled_p)
17047 return NULL;
17049 last_y = window_text_bottom_y (w) - dy;
17051 while (1)
17053 /* Give up if we have gone too far. */
17054 if (end && row >= end)
17055 return NULL;
17056 /* This formerly returned if they were equal.
17057 I think that both quantities are of a "last plus one" type;
17058 if so, when they are equal, the row is within the screen. -- rms. */
17059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17060 return NULL;
17062 /* If it is in this row, return this row. */
17063 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17064 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17065 /* The end position of a row equals the start
17066 position of the next row. If CHARPOS is there, we
17067 would rather consider it displayed in the next
17068 line, except when this line ends in ZV. */
17069 && !row_for_charpos_p (row, charpos)))
17070 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17072 struct glyph *g;
17074 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17075 || (!best_row && !row->continued_p))
17076 return row;
17077 /* In bidi-reordered rows, there could be several rows whose
17078 edges surround CHARPOS, all of these rows belonging to
17079 the same continued line. We need to find the row which
17080 fits CHARPOS the best. */
17081 for (g = row->glyphs[TEXT_AREA];
17082 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17083 g++)
17085 if (!STRINGP (g->object))
17087 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17089 mindif = eabs (g->charpos - charpos);
17090 best_row = row;
17091 /* Exact match always wins. */
17092 if (mindif == 0)
17093 return best_row;
17098 else if (best_row && !row->continued_p)
17099 return best_row;
17100 ++row;
17105 /* Try to redisplay window W by reusing its existing display. W's
17106 current matrix must be up to date when this function is called,
17107 i.e. window_end_valid must be nonzero.
17109 Value is
17111 1 if display has been updated
17112 0 if otherwise unsuccessful
17113 -1 if redisplay with same window start is known not to succeed
17115 The following steps are performed:
17117 1. Find the last row in the current matrix of W that is not
17118 affected by changes at the start of current_buffer. If no such row
17119 is found, give up.
17121 2. Find the first row in W's current matrix that is not affected by
17122 changes at the end of current_buffer. Maybe there is no such row.
17124 3. Display lines beginning with the row + 1 found in step 1 to the
17125 row found in step 2 or, if step 2 didn't find a row, to the end of
17126 the window.
17128 4. If cursor is not known to appear on the window, give up.
17130 5. If display stopped at the row found in step 2, scroll the
17131 display and current matrix as needed.
17133 6. Maybe display some lines at the end of W, if we must. This can
17134 happen under various circumstances, like a partially visible line
17135 becoming fully visible, or because newly displayed lines are displayed
17136 in smaller font sizes.
17138 7. Update W's window end information. */
17140 static int
17141 try_window_id (struct window *w)
17143 struct frame *f = XFRAME (w->frame);
17144 struct glyph_matrix *current_matrix = w->current_matrix;
17145 struct glyph_matrix *desired_matrix = w->desired_matrix;
17146 struct glyph_row *last_unchanged_at_beg_row;
17147 struct glyph_row *first_unchanged_at_end_row;
17148 struct glyph_row *row;
17149 struct glyph_row *bottom_row;
17150 int bottom_vpos;
17151 struct it it;
17152 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17153 int dvpos, dy;
17154 struct text_pos start_pos;
17155 struct run run;
17156 int first_unchanged_at_end_vpos = 0;
17157 struct glyph_row *last_text_row, *last_text_row_at_end;
17158 struct text_pos start;
17159 ptrdiff_t first_changed_charpos, last_changed_charpos;
17161 #ifdef GLYPH_DEBUG
17162 if (inhibit_try_window_id)
17163 return 0;
17164 #endif
17166 /* This is handy for debugging. */
17167 #if 0
17168 #define GIVE_UP(X) \
17169 do { \
17170 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17171 return 0; \
17172 } while (0)
17173 #else
17174 #define GIVE_UP(X) return 0
17175 #endif
17177 SET_TEXT_POS_FROM_MARKER (start, w->start);
17179 /* Don't use this for mini-windows because these can show
17180 messages and mini-buffers, and we don't handle that here. */
17181 if (MINI_WINDOW_P (w))
17182 GIVE_UP (1);
17184 /* This flag is used to prevent redisplay optimizations. */
17185 if (windows_or_buffers_changed || f->cursor_type_changed)
17186 GIVE_UP (2);
17188 /* Verify that narrowing has not changed.
17189 Also verify that we were not told to prevent redisplay optimizations.
17190 It would be nice to further
17191 reduce the number of cases where this prevents try_window_id. */
17192 if (current_buffer->clip_changed
17193 || current_buffer->prevent_redisplay_optimizations_p)
17194 GIVE_UP (3);
17196 /* Window must either use window-based redisplay or be full width. */
17197 if (!FRAME_WINDOW_P (f)
17198 && (!FRAME_LINE_INS_DEL_OK (f)
17199 || !WINDOW_FULL_WIDTH_P (w)))
17200 GIVE_UP (4);
17202 /* Give up if point is known NOT to appear in W. */
17203 if (PT < CHARPOS (start))
17204 GIVE_UP (5);
17206 /* Another way to prevent redisplay optimizations. */
17207 if (w->last_modified == 0)
17208 GIVE_UP (6);
17210 /* Verify that window is not hscrolled. */
17211 if (w->hscroll != 0)
17212 GIVE_UP (7);
17214 /* Verify that display wasn't paused. */
17215 if (!w->window_end_valid)
17216 GIVE_UP (8);
17218 /* Likewise if highlighting trailing whitespace. */
17219 if (!NILP (Vshow_trailing_whitespace))
17220 GIVE_UP (11);
17222 /* Can't use this if overlay arrow position and/or string have
17223 changed. */
17224 if (overlay_arrows_changed_p ())
17225 GIVE_UP (12);
17227 /* When word-wrap is on, adding a space to the first word of a
17228 wrapped line can change the wrap position, altering the line
17229 above it. It might be worthwhile to handle this more
17230 intelligently, but for now just redisplay from scratch. */
17231 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17232 GIVE_UP (21);
17234 /* Under bidi reordering, adding or deleting a character in the
17235 beginning of a paragraph, before the first strong directional
17236 character, can change the base direction of the paragraph (unless
17237 the buffer specifies a fixed paragraph direction), which will
17238 require to redisplay the whole paragraph. It might be worthwhile
17239 to find the paragraph limits and widen the range of redisplayed
17240 lines to that, but for now just give up this optimization and
17241 redisplay from scratch. */
17242 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17243 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17244 GIVE_UP (22);
17246 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17247 only if buffer has really changed. The reason is that the gap is
17248 initially at Z for freshly visited files. The code below would
17249 set end_unchanged to 0 in that case. */
17250 if (MODIFF > SAVE_MODIFF
17251 /* This seems to happen sometimes after saving a buffer. */
17252 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17254 if (GPT - BEG < BEG_UNCHANGED)
17255 BEG_UNCHANGED = GPT - BEG;
17256 if (Z - GPT < END_UNCHANGED)
17257 END_UNCHANGED = Z - GPT;
17260 /* The position of the first and last character that has been changed. */
17261 first_changed_charpos = BEG + BEG_UNCHANGED;
17262 last_changed_charpos = Z - END_UNCHANGED;
17264 /* If window starts after a line end, and the last change is in
17265 front of that newline, then changes don't affect the display.
17266 This case happens with stealth-fontification. Note that although
17267 the display is unchanged, glyph positions in the matrix have to
17268 be adjusted, of course. */
17269 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17270 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17271 && ((last_changed_charpos < CHARPOS (start)
17272 && CHARPOS (start) == BEGV)
17273 || (last_changed_charpos < CHARPOS (start) - 1
17274 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17276 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17277 struct glyph_row *r0;
17279 /* Compute how many chars/bytes have been added to or removed
17280 from the buffer. */
17281 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17282 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17283 Z_delta = Z - Z_old;
17284 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17286 /* Give up if PT is not in the window. Note that it already has
17287 been checked at the start of try_window_id that PT is not in
17288 front of the window start. */
17289 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17290 GIVE_UP (13);
17292 /* If window start is unchanged, we can reuse the whole matrix
17293 as is, after adjusting glyph positions. No need to compute
17294 the window end again, since its offset from Z hasn't changed. */
17295 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17296 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17297 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17298 /* PT must not be in a partially visible line. */
17299 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17300 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17302 /* Adjust positions in the glyph matrix. */
17303 if (Z_delta || Z_delta_bytes)
17305 struct glyph_row *r1
17306 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17307 increment_matrix_positions (w->current_matrix,
17308 MATRIX_ROW_VPOS (r0, current_matrix),
17309 MATRIX_ROW_VPOS (r1, current_matrix),
17310 Z_delta, Z_delta_bytes);
17313 /* Set the cursor. */
17314 row = row_containing_pos (w, PT, r0, NULL, 0);
17315 if (row)
17316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17317 return 1;
17321 /* Handle the case that changes are all below what is displayed in
17322 the window, and that PT is in the window. This shortcut cannot
17323 be taken if ZV is visible in the window, and text has been added
17324 there that is visible in the window. */
17325 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17326 /* ZV is not visible in the window, or there are no
17327 changes at ZV, actually. */
17328 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17329 || first_changed_charpos == last_changed_charpos))
17331 struct glyph_row *r0;
17333 /* Give up if PT is not in the window. Note that it already has
17334 been checked at the start of try_window_id that PT is not in
17335 front of the window start. */
17336 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17337 GIVE_UP (14);
17339 /* If window start is unchanged, we can reuse the whole matrix
17340 as is, without changing glyph positions since no text has
17341 been added/removed in front of the window end. */
17342 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17343 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17344 /* PT must not be in a partially visible line. */
17345 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17346 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17348 /* We have to compute the window end anew since text
17349 could have been added/removed after it. */
17350 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17351 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17353 /* Set the cursor. */
17354 row = row_containing_pos (w, PT, r0, NULL, 0);
17355 if (row)
17356 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17357 return 2;
17361 /* Give up if window start is in the changed area.
17363 The condition used to read
17365 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17367 but why that was tested escapes me at the moment. */
17368 if (CHARPOS (start) >= first_changed_charpos
17369 && CHARPOS (start) <= last_changed_charpos)
17370 GIVE_UP (15);
17372 /* Check that window start agrees with the start of the first glyph
17373 row in its current matrix. Check this after we know the window
17374 start is not in changed text, otherwise positions would not be
17375 comparable. */
17376 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17377 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17378 GIVE_UP (16);
17380 /* Give up if the window ends in strings. Overlay strings
17381 at the end are difficult to handle, so don't try. */
17382 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17383 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17384 GIVE_UP (20);
17386 /* Compute the position at which we have to start displaying new
17387 lines. Some of the lines at the top of the window might be
17388 reusable because they are not displaying changed text. Find the
17389 last row in W's current matrix not affected by changes at the
17390 start of current_buffer. Value is null if changes start in the
17391 first line of window. */
17392 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17393 if (last_unchanged_at_beg_row)
17395 /* Avoid starting to display in the middle of a character, a TAB
17396 for instance. This is easier than to set up the iterator
17397 exactly, and it's not a frequent case, so the additional
17398 effort wouldn't really pay off. */
17399 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17400 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17401 && last_unchanged_at_beg_row > w->current_matrix->rows)
17402 --last_unchanged_at_beg_row;
17404 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17405 GIVE_UP (17);
17407 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17408 GIVE_UP (18);
17409 start_pos = it.current.pos;
17411 /* Start displaying new lines in the desired matrix at the same
17412 vpos we would use in the current matrix, i.e. below
17413 last_unchanged_at_beg_row. */
17414 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17415 current_matrix);
17416 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17417 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17419 eassert (it.hpos == 0 && it.current_x == 0);
17421 else
17423 /* There are no reusable lines at the start of the window.
17424 Start displaying in the first text line. */
17425 start_display (&it, w, start);
17426 it.vpos = it.first_vpos;
17427 start_pos = it.current.pos;
17430 /* Find the first row that is not affected by changes at the end of
17431 the buffer. Value will be null if there is no unchanged row, in
17432 which case we must redisplay to the end of the window. delta
17433 will be set to the value by which buffer positions beginning with
17434 first_unchanged_at_end_row have to be adjusted due to text
17435 changes. */
17436 first_unchanged_at_end_row
17437 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17438 IF_DEBUG (debug_delta = delta);
17439 IF_DEBUG (debug_delta_bytes = delta_bytes);
17441 /* Set stop_pos to the buffer position up to which we will have to
17442 display new lines. If first_unchanged_at_end_row != NULL, this
17443 is the buffer position of the start of the line displayed in that
17444 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17445 that we don't stop at a buffer position. */
17446 stop_pos = 0;
17447 if (first_unchanged_at_end_row)
17449 eassert (last_unchanged_at_beg_row == NULL
17450 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17452 /* If this is a continuation line, move forward to the next one
17453 that isn't. Changes in lines above affect this line.
17454 Caution: this may move first_unchanged_at_end_row to a row
17455 not displaying text. */
17456 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17457 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17458 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17459 < it.last_visible_y))
17460 ++first_unchanged_at_end_row;
17462 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17463 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17464 >= it.last_visible_y))
17465 first_unchanged_at_end_row = NULL;
17466 else
17468 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17469 + delta);
17470 first_unchanged_at_end_vpos
17471 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17472 eassert (stop_pos >= Z - END_UNCHANGED);
17475 else if (last_unchanged_at_beg_row == NULL)
17476 GIVE_UP (19);
17479 #ifdef GLYPH_DEBUG
17481 /* Either there is no unchanged row at the end, or the one we have
17482 now displays text. This is a necessary condition for the window
17483 end pos calculation at the end of this function. */
17484 eassert (first_unchanged_at_end_row == NULL
17485 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17487 debug_last_unchanged_at_beg_vpos
17488 = (last_unchanged_at_beg_row
17489 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17490 : -1);
17491 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17493 #endif /* GLYPH_DEBUG */
17496 /* Display new lines. Set last_text_row to the last new line
17497 displayed which has text on it, i.e. might end up as being the
17498 line where the window_end_vpos is. */
17499 w->cursor.vpos = -1;
17500 last_text_row = NULL;
17501 overlay_arrow_seen = 0;
17502 while (it.current_y < it.last_visible_y
17503 && !f->fonts_changed
17504 && (first_unchanged_at_end_row == NULL
17505 || IT_CHARPOS (it) < stop_pos))
17507 if (display_line (&it))
17508 last_text_row = it.glyph_row - 1;
17511 if (f->fonts_changed)
17512 return -1;
17515 /* Compute differences in buffer positions, y-positions etc. for
17516 lines reused at the bottom of the window. Compute what we can
17517 scroll. */
17518 if (first_unchanged_at_end_row
17519 /* No lines reused because we displayed everything up to the
17520 bottom of the window. */
17521 && it.current_y < it.last_visible_y)
17523 dvpos = (it.vpos
17524 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17525 current_matrix));
17526 dy = it.current_y - first_unchanged_at_end_row->y;
17527 run.current_y = first_unchanged_at_end_row->y;
17528 run.desired_y = run.current_y + dy;
17529 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17531 else
17533 delta = delta_bytes = dvpos = dy
17534 = run.current_y = run.desired_y = run.height = 0;
17535 first_unchanged_at_end_row = NULL;
17537 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17540 /* Find the cursor if not already found. We have to decide whether
17541 PT will appear on this window (it sometimes doesn't, but this is
17542 not a very frequent case.) This decision has to be made before
17543 the current matrix is altered. A value of cursor.vpos < 0 means
17544 that PT is either in one of the lines beginning at
17545 first_unchanged_at_end_row or below the window. Don't care for
17546 lines that might be displayed later at the window end; as
17547 mentioned, this is not a frequent case. */
17548 if (w->cursor.vpos < 0)
17550 /* Cursor in unchanged rows at the top? */
17551 if (PT < CHARPOS (start_pos)
17552 && last_unchanged_at_beg_row)
17554 row = row_containing_pos (w, PT,
17555 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17556 last_unchanged_at_beg_row + 1, 0);
17557 if (row)
17558 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17561 /* Start from first_unchanged_at_end_row looking for PT. */
17562 else if (first_unchanged_at_end_row)
17564 row = row_containing_pos (w, PT - delta,
17565 first_unchanged_at_end_row, NULL, 0);
17566 if (row)
17567 set_cursor_from_row (w, row, w->current_matrix, delta,
17568 delta_bytes, dy, dvpos);
17571 /* Give up if cursor was not found. */
17572 if (w->cursor.vpos < 0)
17574 clear_glyph_matrix (w->desired_matrix);
17575 return -1;
17579 /* Don't let the cursor end in the scroll margins. */
17581 int this_scroll_margin, cursor_height;
17582 int frame_line_height = default_line_pixel_height (w);
17583 int window_total_lines
17584 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17586 this_scroll_margin =
17587 max (0, min (scroll_margin, window_total_lines / 4));
17588 this_scroll_margin *= frame_line_height;
17589 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17591 if ((w->cursor.y < this_scroll_margin
17592 && CHARPOS (start) > BEGV)
17593 /* Old redisplay didn't take scroll margin into account at the bottom,
17594 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17595 || (w->cursor.y + (make_cursor_line_fully_visible_p
17596 ? cursor_height + this_scroll_margin
17597 : 1)) > it.last_visible_y)
17599 w->cursor.vpos = -1;
17600 clear_glyph_matrix (w->desired_matrix);
17601 return -1;
17605 /* Scroll the display. Do it before changing the current matrix so
17606 that xterm.c doesn't get confused about where the cursor glyph is
17607 found. */
17608 if (dy && run.height)
17610 update_begin (f);
17612 if (FRAME_WINDOW_P (f))
17614 FRAME_RIF (f)->update_window_begin_hook (w);
17615 FRAME_RIF (f)->clear_window_mouse_face (w);
17616 FRAME_RIF (f)->scroll_run_hook (w, &run);
17617 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17619 else
17621 /* Terminal frame. In this case, dvpos gives the number of
17622 lines to scroll by; dvpos < 0 means scroll up. */
17623 int from_vpos
17624 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17625 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17626 int end = (WINDOW_TOP_EDGE_LINE (w)
17627 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17628 + window_internal_height (w));
17630 #if defined (HAVE_GPM) || defined (MSDOS)
17631 x_clear_window_mouse_face (w);
17632 #endif
17633 /* Perform the operation on the screen. */
17634 if (dvpos > 0)
17636 /* Scroll last_unchanged_at_beg_row to the end of the
17637 window down dvpos lines. */
17638 set_terminal_window (f, end);
17640 /* On dumb terminals delete dvpos lines at the end
17641 before inserting dvpos empty lines. */
17642 if (!FRAME_SCROLL_REGION_OK (f))
17643 ins_del_lines (f, end - dvpos, -dvpos);
17645 /* Insert dvpos empty lines in front of
17646 last_unchanged_at_beg_row. */
17647 ins_del_lines (f, from, dvpos);
17649 else if (dvpos < 0)
17651 /* Scroll up last_unchanged_at_beg_vpos to the end of
17652 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17653 set_terminal_window (f, end);
17655 /* Delete dvpos lines in front of
17656 last_unchanged_at_beg_vpos. ins_del_lines will set
17657 the cursor to the given vpos and emit |dvpos| delete
17658 line sequences. */
17659 ins_del_lines (f, from + dvpos, dvpos);
17661 /* On a dumb terminal insert dvpos empty lines at the
17662 end. */
17663 if (!FRAME_SCROLL_REGION_OK (f))
17664 ins_del_lines (f, end + dvpos, -dvpos);
17667 set_terminal_window (f, 0);
17670 update_end (f);
17673 /* Shift reused rows of the current matrix to the right position.
17674 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17675 text. */
17676 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17677 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17678 if (dvpos < 0)
17680 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17681 bottom_vpos, dvpos);
17682 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17683 bottom_vpos);
17685 else if (dvpos > 0)
17687 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17688 bottom_vpos, dvpos);
17689 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17690 first_unchanged_at_end_vpos + dvpos);
17693 /* For frame-based redisplay, make sure that current frame and window
17694 matrix are in sync with respect to glyph memory. */
17695 if (!FRAME_WINDOW_P (f))
17696 sync_frame_with_window_matrix_rows (w);
17698 /* Adjust buffer positions in reused rows. */
17699 if (delta || delta_bytes)
17700 increment_matrix_positions (current_matrix,
17701 first_unchanged_at_end_vpos + dvpos,
17702 bottom_vpos, delta, delta_bytes);
17704 /* Adjust Y positions. */
17705 if (dy)
17706 shift_glyph_matrix (w, current_matrix,
17707 first_unchanged_at_end_vpos + dvpos,
17708 bottom_vpos, dy);
17710 if (first_unchanged_at_end_row)
17712 first_unchanged_at_end_row += dvpos;
17713 if (first_unchanged_at_end_row->y >= it.last_visible_y
17714 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17715 first_unchanged_at_end_row = NULL;
17718 /* If scrolling up, there may be some lines to display at the end of
17719 the window. */
17720 last_text_row_at_end = NULL;
17721 if (dy < 0)
17723 /* Scrolling up can leave for example a partially visible line
17724 at the end of the window to be redisplayed. */
17725 /* Set last_row to the glyph row in the current matrix where the
17726 window end line is found. It has been moved up or down in
17727 the matrix by dvpos. */
17728 int last_vpos = w->window_end_vpos + dvpos;
17729 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17731 /* If last_row is the window end line, it should display text. */
17732 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17734 /* If window end line was partially visible before, begin
17735 displaying at that line. Otherwise begin displaying with the
17736 line following it. */
17737 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17739 init_to_row_start (&it, w, last_row);
17740 it.vpos = last_vpos;
17741 it.current_y = last_row->y;
17743 else
17745 init_to_row_end (&it, w, last_row);
17746 it.vpos = 1 + last_vpos;
17747 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17748 ++last_row;
17751 /* We may start in a continuation line. If so, we have to
17752 get the right continuation_lines_width and current_x. */
17753 it.continuation_lines_width = last_row->continuation_lines_width;
17754 it.hpos = it.current_x = 0;
17756 /* Display the rest of the lines at the window end. */
17757 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17758 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17760 /* Is it always sure that the display agrees with lines in
17761 the current matrix? I don't think so, so we mark rows
17762 displayed invalid in the current matrix by setting their
17763 enabled_p flag to zero. */
17764 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17765 if (display_line (&it))
17766 last_text_row_at_end = it.glyph_row - 1;
17770 /* Update window_end_pos and window_end_vpos. */
17771 if (first_unchanged_at_end_row && !last_text_row_at_end)
17773 /* Window end line if one of the preserved rows from the current
17774 matrix. Set row to the last row displaying text in current
17775 matrix starting at first_unchanged_at_end_row, after
17776 scrolling. */
17777 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17778 row = find_last_row_displaying_text (w->current_matrix, &it,
17779 first_unchanged_at_end_row);
17780 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17781 adjust_window_ends (w, row, 1);
17782 eassert (w->window_end_bytepos >= 0);
17783 IF_DEBUG (debug_method_add (w, "A"));
17785 else if (last_text_row_at_end)
17787 adjust_window_ends (w, last_text_row_at_end, 0);
17788 eassert (w->window_end_bytepos >= 0);
17789 IF_DEBUG (debug_method_add (w, "B"));
17791 else if (last_text_row)
17793 /* We have displayed either to the end of the window or at the
17794 end of the window, i.e. the last row with text is to be found
17795 in the desired matrix. */
17796 adjust_window_ends (w, last_text_row, 0);
17797 eassert (w->window_end_bytepos >= 0);
17799 else if (first_unchanged_at_end_row == NULL
17800 && last_text_row == NULL
17801 && last_text_row_at_end == NULL)
17803 /* Displayed to end of window, but no line containing text was
17804 displayed. Lines were deleted at the end of the window. */
17805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17806 int vpos = w->window_end_vpos;
17807 struct glyph_row *current_row = current_matrix->rows + vpos;
17808 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17810 for (row = NULL;
17811 row == NULL && vpos >= first_vpos;
17812 --vpos, --current_row, --desired_row)
17814 if (desired_row->enabled_p)
17816 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17817 row = desired_row;
17819 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17820 row = current_row;
17823 eassert (row != NULL);
17824 w->window_end_vpos = vpos + 1;
17825 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17826 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17827 eassert (w->window_end_bytepos >= 0);
17828 IF_DEBUG (debug_method_add (w, "C"));
17830 else
17831 emacs_abort ();
17833 IF_DEBUG (debug_end_pos = w->window_end_pos;
17834 debug_end_vpos = w->window_end_vpos);
17836 /* Record that display has not been completed. */
17837 w->window_end_valid = 0;
17838 w->desired_matrix->no_scrolling_p = 1;
17839 return 3;
17841 #undef GIVE_UP
17846 /***********************************************************************
17847 More debugging support
17848 ***********************************************************************/
17850 #ifdef GLYPH_DEBUG
17852 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17853 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17854 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17857 /* Dump the contents of glyph matrix MATRIX on stderr.
17859 GLYPHS 0 means don't show glyph contents.
17860 GLYPHS 1 means show glyphs in short form
17861 GLYPHS > 1 means show glyphs in long form. */
17863 void
17864 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17866 int i;
17867 for (i = 0; i < matrix->nrows; ++i)
17868 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17872 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17873 the glyph row and area where the glyph comes from. */
17875 void
17876 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17878 if (glyph->type == CHAR_GLYPH
17879 || glyph->type == GLYPHLESS_GLYPH)
17881 fprintf (stderr,
17882 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17883 glyph - row->glyphs[TEXT_AREA],
17884 (glyph->type == CHAR_GLYPH
17885 ? 'C'
17886 : 'G'),
17887 glyph->charpos,
17888 (BUFFERP (glyph->object)
17889 ? 'B'
17890 : (STRINGP (glyph->object)
17891 ? 'S'
17892 : (INTEGERP (glyph->object)
17893 ? '0'
17894 : '-'))),
17895 glyph->pixel_width,
17896 glyph->u.ch,
17897 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17898 ? glyph->u.ch
17899 : '.'),
17900 glyph->face_id,
17901 glyph->left_box_line_p,
17902 glyph->right_box_line_p);
17904 else if (glyph->type == STRETCH_GLYPH)
17906 fprintf (stderr,
17907 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17908 glyph - row->glyphs[TEXT_AREA],
17909 'S',
17910 glyph->charpos,
17911 (BUFFERP (glyph->object)
17912 ? 'B'
17913 : (STRINGP (glyph->object)
17914 ? 'S'
17915 : (INTEGERP (glyph->object)
17916 ? '0'
17917 : '-'))),
17918 glyph->pixel_width,
17920 ' ',
17921 glyph->face_id,
17922 glyph->left_box_line_p,
17923 glyph->right_box_line_p);
17925 else if (glyph->type == IMAGE_GLYPH)
17927 fprintf (stderr,
17928 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17929 glyph - row->glyphs[TEXT_AREA],
17930 'I',
17931 glyph->charpos,
17932 (BUFFERP (glyph->object)
17933 ? 'B'
17934 : (STRINGP (glyph->object)
17935 ? 'S'
17936 : (INTEGERP (glyph->object)
17937 ? '0'
17938 : '-'))),
17939 glyph->pixel_width,
17940 glyph->u.img_id,
17941 '.',
17942 glyph->face_id,
17943 glyph->left_box_line_p,
17944 glyph->right_box_line_p);
17946 else if (glyph->type == COMPOSITE_GLYPH)
17948 fprintf (stderr,
17949 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17950 glyph - row->glyphs[TEXT_AREA],
17951 '+',
17952 glyph->charpos,
17953 (BUFFERP (glyph->object)
17954 ? 'B'
17955 : (STRINGP (glyph->object)
17956 ? 'S'
17957 : (INTEGERP (glyph->object)
17958 ? '0'
17959 : '-'))),
17960 glyph->pixel_width,
17961 glyph->u.cmp.id);
17962 if (glyph->u.cmp.automatic)
17963 fprintf (stderr,
17964 "[%d-%d]",
17965 glyph->slice.cmp.from, glyph->slice.cmp.to);
17966 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17967 glyph->face_id,
17968 glyph->left_box_line_p,
17969 glyph->right_box_line_p);
17974 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17975 GLYPHS 0 means don't show glyph contents.
17976 GLYPHS 1 means show glyphs in short form
17977 GLYPHS > 1 means show glyphs in long form. */
17979 void
17980 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17982 if (glyphs != 1)
17984 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17985 fprintf (stderr, "==============================================================================\n");
17987 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17988 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17989 vpos,
17990 MATRIX_ROW_START_CHARPOS (row),
17991 MATRIX_ROW_END_CHARPOS (row),
17992 row->used[TEXT_AREA],
17993 row->contains_overlapping_glyphs_p,
17994 row->enabled_p,
17995 row->truncated_on_left_p,
17996 row->truncated_on_right_p,
17997 row->continued_p,
17998 MATRIX_ROW_CONTINUATION_LINE_P (row),
17999 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18000 row->ends_at_zv_p,
18001 row->fill_line_p,
18002 row->ends_in_middle_of_char_p,
18003 row->starts_in_middle_of_char_p,
18004 row->mouse_face_p,
18005 row->x,
18006 row->y,
18007 row->pixel_width,
18008 row->height,
18009 row->visible_height,
18010 row->ascent,
18011 row->phys_ascent);
18012 /* The next 3 lines should align to "Start" in the header. */
18013 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18014 row->end.overlay_string_index,
18015 row->continuation_lines_width);
18016 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18017 CHARPOS (row->start.string_pos),
18018 CHARPOS (row->end.string_pos));
18019 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18020 row->end.dpvec_index);
18023 if (glyphs > 1)
18025 int area;
18027 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18029 struct glyph *glyph = row->glyphs[area];
18030 struct glyph *glyph_end = glyph + row->used[area];
18032 /* Glyph for a line end in text. */
18033 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18034 ++glyph_end;
18036 if (glyph < glyph_end)
18037 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18039 for (; glyph < glyph_end; ++glyph)
18040 dump_glyph (row, glyph, area);
18043 else if (glyphs == 1)
18045 int area;
18047 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18049 char *s = alloca (row->used[area] + 4);
18050 int i;
18052 for (i = 0; i < row->used[area]; ++i)
18054 struct glyph *glyph = row->glyphs[area] + i;
18055 if (i == row->used[area] - 1
18056 && area == TEXT_AREA
18057 && INTEGERP (glyph->object)
18058 && glyph->type == CHAR_GLYPH
18059 && glyph->u.ch == ' ')
18061 strcpy (&s[i], "[\\n]");
18062 i += 4;
18064 else if (glyph->type == CHAR_GLYPH
18065 && glyph->u.ch < 0x80
18066 && glyph->u.ch >= ' ')
18067 s[i] = glyph->u.ch;
18068 else
18069 s[i] = '.';
18072 s[i] = '\0';
18073 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18079 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18080 Sdump_glyph_matrix, 0, 1, "p",
18081 doc: /* Dump the current matrix of the selected window to stderr.
18082 Shows contents of glyph row structures. With non-nil
18083 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18084 glyphs in short form, otherwise show glyphs in long form. */)
18085 (Lisp_Object glyphs)
18087 struct window *w = XWINDOW (selected_window);
18088 struct buffer *buffer = XBUFFER (w->contents);
18090 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18091 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18092 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18093 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18094 fprintf (stderr, "=============================================\n");
18095 dump_glyph_matrix (w->current_matrix,
18096 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18097 return Qnil;
18101 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18102 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18103 (void)
18105 struct frame *f = XFRAME (selected_frame);
18106 dump_glyph_matrix (f->current_matrix, 1);
18107 return Qnil;
18111 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18112 doc: /* Dump glyph row ROW to stderr.
18113 GLYPH 0 means don't dump glyphs.
18114 GLYPH 1 means dump glyphs in short form.
18115 GLYPH > 1 or omitted means dump glyphs in long form. */)
18116 (Lisp_Object row, Lisp_Object glyphs)
18118 struct glyph_matrix *matrix;
18119 EMACS_INT vpos;
18121 CHECK_NUMBER (row);
18122 matrix = XWINDOW (selected_window)->current_matrix;
18123 vpos = XINT (row);
18124 if (vpos >= 0 && vpos < matrix->nrows)
18125 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18126 vpos,
18127 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18128 return Qnil;
18132 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18133 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18134 GLYPH 0 means don't dump glyphs.
18135 GLYPH 1 means dump glyphs in short form.
18136 GLYPH > 1 or omitted means dump glyphs in long form.
18138 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18139 do nothing. */)
18140 (Lisp_Object row, Lisp_Object glyphs)
18142 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18143 struct frame *sf = SELECTED_FRAME ();
18144 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18145 EMACS_INT vpos;
18147 CHECK_NUMBER (row);
18148 vpos = XINT (row);
18149 if (vpos >= 0 && vpos < m->nrows)
18150 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18151 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18152 #endif
18153 return Qnil;
18157 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18158 doc: /* Toggle tracing of redisplay.
18159 With ARG, turn tracing on if and only if ARG is positive. */)
18160 (Lisp_Object arg)
18162 if (NILP (arg))
18163 trace_redisplay_p = !trace_redisplay_p;
18164 else
18166 arg = Fprefix_numeric_value (arg);
18167 trace_redisplay_p = XINT (arg) > 0;
18170 return Qnil;
18174 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18175 doc: /* Like `format', but print result to stderr.
18176 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18177 (ptrdiff_t nargs, Lisp_Object *args)
18179 Lisp_Object s = Fformat (nargs, args);
18180 fprintf (stderr, "%s", SDATA (s));
18181 return Qnil;
18184 #endif /* GLYPH_DEBUG */
18188 /***********************************************************************
18189 Building Desired Matrix Rows
18190 ***********************************************************************/
18192 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18193 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18195 static struct glyph_row *
18196 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18198 struct frame *f = XFRAME (WINDOW_FRAME (w));
18199 struct buffer *buffer = XBUFFER (w->contents);
18200 struct buffer *old = current_buffer;
18201 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18202 int arrow_len = SCHARS (overlay_arrow_string);
18203 const unsigned char *arrow_end = arrow_string + arrow_len;
18204 const unsigned char *p;
18205 struct it it;
18206 bool multibyte_p;
18207 int n_glyphs_before;
18209 set_buffer_temp (buffer);
18210 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18211 it.glyph_row->used[TEXT_AREA] = 0;
18212 SET_TEXT_POS (it.position, 0, 0);
18214 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18215 p = arrow_string;
18216 while (p < arrow_end)
18218 Lisp_Object face, ilisp;
18220 /* Get the next character. */
18221 if (multibyte_p)
18222 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18223 else
18225 it.c = it.char_to_display = *p, it.len = 1;
18226 if (! ASCII_CHAR_P (it.c))
18227 it.char_to_display = BYTE8_TO_CHAR (it.c);
18229 p += it.len;
18231 /* Get its face. */
18232 ilisp = make_number (p - arrow_string);
18233 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18234 it.face_id = compute_char_face (f, it.char_to_display, face);
18236 /* Compute its width, get its glyphs. */
18237 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18238 SET_TEXT_POS (it.position, -1, -1);
18239 PRODUCE_GLYPHS (&it);
18241 /* If this character doesn't fit any more in the line, we have
18242 to remove some glyphs. */
18243 if (it.current_x > it.last_visible_x)
18245 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18246 break;
18250 set_buffer_temp (old);
18251 return it.glyph_row;
18255 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18256 glyphs to insert is determined by produce_special_glyphs. */
18258 static void
18259 insert_left_trunc_glyphs (struct it *it)
18261 struct it truncate_it;
18262 struct glyph *from, *end, *to, *toend;
18264 eassert (!FRAME_WINDOW_P (it->f)
18265 || (!it->glyph_row->reversed_p
18266 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18267 || (it->glyph_row->reversed_p
18268 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18270 /* Get the truncation glyphs. */
18271 truncate_it = *it;
18272 truncate_it.current_x = 0;
18273 truncate_it.face_id = DEFAULT_FACE_ID;
18274 truncate_it.glyph_row = &scratch_glyph_row;
18275 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18276 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18277 truncate_it.object = make_number (0);
18278 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18280 /* Overwrite glyphs from IT with truncation glyphs. */
18281 if (!it->glyph_row->reversed_p)
18283 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18285 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18286 end = from + tused;
18287 to = it->glyph_row->glyphs[TEXT_AREA];
18288 toend = to + it->glyph_row->used[TEXT_AREA];
18289 if (FRAME_WINDOW_P (it->f))
18291 /* On GUI frames, when variable-size fonts are displayed,
18292 the truncation glyphs may need more pixels than the row's
18293 glyphs they overwrite. We overwrite more glyphs to free
18294 enough screen real estate, and enlarge the stretch glyph
18295 on the right (see display_line), if there is one, to
18296 preserve the screen position of the truncation glyphs on
18297 the right. */
18298 int w = 0;
18299 struct glyph *g = to;
18300 short used;
18302 /* The first glyph could be partially visible, in which case
18303 it->glyph_row->x will be negative. But we want the left
18304 truncation glyphs to be aligned at the left margin of the
18305 window, so we override the x coordinate at which the row
18306 will begin. */
18307 it->glyph_row->x = 0;
18308 while (g < toend && w < it->truncation_pixel_width)
18310 w += g->pixel_width;
18311 ++g;
18313 if (g - to - tused > 0)
18315 memmove (to + tused, g, (toend - g) * sizeof(*g));
18316 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18318 used = it->glyph_row->used[TEXT_AREA];
18319 if (it->glyph_row->truncated_on_right_p
18320 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18321 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18322 == STRETCH_GLYPH)
18324 int extra = w - it->truncation_pixel_width;
18326 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18330 while (from < end)
18331 *to++ = *from++;
18333 /* There may be padding glyphs left over. Overwrite them too. */
18334 if (!FRAME_WINDOW_P (it->f))
18336 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18338 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18339 while (from < end)
18340 *to++ = *from++;
18344 if (to > toend)
18345 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18347 else
18349 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18351 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18352 that back to front. */
18353 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18354 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18355 toend = it->glyph_row->glyphs[TEXT_AREA];
18356 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18357 if (FRAME_WINDOW_P (it->f))
18359 int w = 0;
18360 struct glyph *g = to;
18362 while (g >= toend && w < it->truncation_pixel_width)
18364 w += g->pixel_width;
18365 --g;
18367 if (to - g - tused > 0)
18368 to = g + tused;
18369 if (it->glyph_row->truncated_on_right_p
18370 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18371 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18373 int extra = w - it->truncation_pixel_width;
18375 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18379 while (from >= end && to >= toend)
18380 *to-- = *from--;
18381 if (!FRAME_WINDOW_P (it->f))
18383 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18385 from =
18386 truncate_it.glyph_row->glyphs[TEXT_AREA]
18387 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18388 while (from >= end && to >= toend)
18389 *to-- = *from--;
18392 if (from >= end)
18394 /* Need to free some room before prepending additional
18395 glyphs. */
18396 int move_by = from - end + 1;
18397 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18398 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18400 for ( ; g >= g0; g--)
18401 g[move_by] = *g;
18402 while (from >= end)
18403 *to-- = *from--;
18404 it->glyph_row->used[TEXT_AREA] += move_by;
18409 /* Compute the hash code for ROW. */
18410 unsigned
18411 row_hash (struct glyph_row *row)
18413 int area, k;
18414 unsigned hashval = 0;
18416 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18417 for (k = 0; k < row->used[area]; ++k)
18418 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18419 + row->glyphs[area][k].u.val
18420 + row->glyphs[area][k].face_id
18421 + row->glyphs[area][k].padding_p
18422 + (row->glyphs[area][k].type << 2));
18424 return hashval;
18427 /* Compute the pixel height and width of IT->glyph_row.
18429 Most of the time, ascent and height of a display line will be equal
18430 to the max_ascent and max_height values of the display iterator
18431 structure. This is not the case if
18433 1. We hit ZV without displaying anything. In this case, max_ascent
18434 and max_height will be zero.
18436 2. We have some glyphs that don't contribute to the line height.
18437 (The glyph row flag contributes_to_line_height_p is for future
18438 pixmap extensions).
18440 The first case is easily covered by using default values because in
18441 these cases, the line height does not really matter, except that it
18442 must not be zero. */
18444 static void
18445 compute_line_metrics (struct it *it)
18447 struct glyph_row *row = it->glyph_row;
18449 if (FRAME_WINDOW_P (it->f))
18451 int i, min_y, max_y;
18453 /* The line may consist of one space only, that was added to
18454 place the cursor on it. If so, the row's height hasn't been
18455 computed yet. */
18456 if (row->height == 0)
18458 if (it->max_ascent + it->max_descent == 0)
18459 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18460 row->ascent = it->max_ascent;
18461 row->height = it->max_ascent + it->max_descent;
18462 row->phys_ascent = it->max_phys_ascent;
18463 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18464 row->extra_line_spacing = it->max_extra_line_spacing;
18467 /* Compute the width of this line. */
18468 row->pixel_width = row->x;
18469 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18470 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18472 eassert (row->pixel_width >= 0);
18473 eassert (row->ascent >= 0 && row->height > 0);
18475 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18476 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18478 /* If first line's physical ascent is larger than its logical
18479 ascent, use the physical ascent, and make the row taller.
18480 This makes accented characters fully visible. */
18481 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18482 && row->phys_ascent > row->ascent)
18484 row->height += row->phys_ascent - row->ascent;
18485 row->ascent = row->phys_ascent;
18488 /* Compute how much of the line is visible. */
18489 row->visible_height = row->height;
18491 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18492 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18494 if (row->y < min_y)
18495 row->visible_height -= min_y - row->y;
18496 if (row->y + row->height > max_y)
18497 row->visible_height -= row->y + row->height - max_y;
18499 else
18501 row->pixel_width = row->used[TEXT_AREA];
18502 if (row->continued_p)
18503 row->pixel_width -= it->continuation_pixel_width;
18504 else if (row->truncated_on_right_p)
18505 row->pixel_width -= it->truncation_pixel_width;
18506 row->ascent = row->phys_ascent = 0;
18507 row->height = row->phys_height = row->visible_height = 1;
18508 row->extra_line_spacing = 0;
18511 /* Compute a hash code for this row. */
18512 row->hash = row_hash (row);
18514 it->max_ascent = it->max_descent = 0;
18515 it->max_phys_ascent = it->max_phys_descent = 0;
18519 /* Append one space to the glyph row of iterator IT if doing a
18520 window-based redisplay. The space has the same face as
18521 IT->face_id. Value is non-zero if a space was added.
18523 This function is called to make sure that there is always one glyph
18524 at the end of a glyph row that the cursor can be set on under
18525 window-systems. (If there weren't such a glyph we would not know
18526 how wide and tall a box cursor should be displayed).
18528 At the same time this space let's a nicely handle clearing to the
18529 end of the line if the row ends in italic text. */
18531 static int
18532 append_space_for_newline (struct it *it, int default_face_p)
18534 if (FRAME_WINDOW_P (it->f))
18536 int n = it->glyph_row->used[TEXT_AREA];
18538 if (it->glyph_row->glyphs[TEXT_AREA] + n
18539 < it->glyph_row->glyphs[1 + TEXT_AREA])
18541 /* Save some values that must not be changed.
18542 Must save IT->c and IT->len because otherwise
18543 ITERATOR_AT_END_P wouldn't work anymore after
18544 append_space_for_newline has been called. */
18545 enum display_element_type saved_what = it->what;
18546 int saved_c = it->c, saved_len = it->len;
18547 int saved_char_to_display = it->char_to_display;
18548 int saved_x = it->current_x;
18549 int saved_face_id = it->face_id;
18550 int saved_box_end = it->end_of_box_run_p;
18551 struct text_pos saved_pos;
18552 Lisp_Object saved_object;
18553 struct face *face;
18555 saved_object = it->object;
18556 saved_pos = it->position;
18558 it->what = IT_CHARACTER;
18559 memset (&it->position, 0, sizeof it->position);
18560 it->object = make_number (0);
18561 it->c = it->char_to_display = ' ';
18562 it->len = 1;
18564 /* If the default face was remapped, be sure to use the
18565 remapped face for the appended newline. */
18566 if (default_face_p)
18567 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18568 else if (it->face_before_selective_p)
18569 it->face_id = it->saved_face_id;
18570 face = FACE_FROM_ID (it->f, it->face_id);
18571 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18572 /* In R2L rows, we will prepend a stretch glyph that will
18573 have the end_of_box_run_p flag set for it, so there's no
18574 need for the appended newline glyph to have that flag
18575 set. */
18576 if (it->glyph_row->reversed_p
18577 /* But if the appended newline glyph goes all the way to
18578 the end of the row, there will be no stretch glyph,
18579 so leave the box flag set. */
18580 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18581 it->end_of_box_run_p = 0;
18583 PRODUCE_GLYPHS (it);
18585 it->override_ascent = -1;
18586 it->constrain_row_ascent_descent_p = 0;
18587 it->current_x = saved_x;
18588 it->object = saved_object;
18589 it->position = saved_pos;
18590 it->what = saved_what;
18591 it->face_id = saved_face_id;
18592 it->len = saved_len;
18593 it->c = saved_c;
18594 it->char_to_display = saved_char_to_display;
18595 it->end_of_box_run_p = saved_box_end;
18596 return 1;
18600 return 0;
18604 /* Extend the face of the last glyph in the text area of IT->glyph_row
18605 to the end of the display line. Called from display_line. If the
18606 glyph row is empty, add a space glyph to it so that we know the
18607 face to draw. Set the glyph row flag fill_line_p. If the glyph
18608 row is R2L, prepend a stretch glyph to cover the empty space to the
18609 left of the leftmost glyph. */
18611 static void
18612 extend_face_to_end_of_line (struct it *it)
18614 struct face *face, *default_face;
18615 struct frame *f = it->f;
18617 /* If line is already filled, do nothing. Non window-system frames
18618 get a grace of one more ``pixel'' because their characters are
18619 1-``pixel'' wide, so they hit the equality too early. This grace
18620 is needed only for R2L rows that are not continued, to produce
18621 one extra blank where we could display the cursor. */
18622 if (it->current_x >= it->last_visible_x
18623 + (!FRAME_WINDOW_P (f)
18624 && it->glyph_row->reversed_p
18625 && !it->glyph_row->continued_p))
18626 return;
18628 /* The default face, possibly remapped. */
18629 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18631 /* Face extension extends the background and box of IT->face_id
18632 to the end of the line. If the background equals the background
18633 of the frame, we don't have to do anything. */
18634 if (it->face_before_selective_p)
18635 face = FACE_FROM_ID (f, it->saved_face_id);
18636 else
18637 face = FACE_FROM_ID (f, it->face_id);
18639 if (FRAME_WINDOW_P (f)
18640 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18641 && face->box == FACE_NO_BOX
18642 && face->background == FRAME_BACKGROUND_PIXEL (f)
18643 #ifdef HAVE_WINDOW_SYSTEM
18644 && !face->stipple
18645 #endif
18646 && !it->glyph_row->reversed_p)
18647 return;
18649 /* Set the glyph row flag indicating that the face of the last glyph
18650 in the text area has to be drawn to the end of the text area. */
18651 it->glyph_row->fill_line_p = 1;
18653 /* If current character of IT is not ASCII, make sure we have the
18654 ASCII face. This will be automatically undone the next time
18655 get_next_display_element returns a multibyte character. Note
18656 that the character will always be single byte in unibyte
18657 text. */
18658 if (!ASCII_CHAR_P (it->c))
18660 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18663 if (FRAME_WINDOW_P (f))
18665 /* If the row is empty, add a space with the current face of IT,
18666 so that we know which face to draw. */
18667 if (it->glyph_row->used[TEXT_AREA] == 0)
18669 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18670 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18671 it->glyph_row->used[TEXT_AREA] = 1;
18673 #ifdef HAVE_WINDOW_SYSTEM
18674 if (it->glyph_row->reversed_p)
18676 /* Prepend a stretch glyph to the row, such that the
18677 rightmost glyph will be drawn flushed all the way to the
18678 right margin of the window. The stretch glyph that will
18679 occupy the empty space, if any, to the left of the
18680 glyphs. */
18681 struct font *font = face->font ? face->font : FRAME_FONT (f);
18682 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18683 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18684 struct glyph *g;
18685 int row_width, stretch_ascent, stretch_width;
18686 struct text_pos saved_pos;
18687 int saved_face_id, saved_avoid_cursor, saved_box_start;
18689 for (row_width = 0, g = row_start; g < row_end; g++)
18690 row_width += g->pixel_width;
18691 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18692 if (stretch_width > 0)
18694 stretch_ascent =
18695 (((it->ascent + it->descent)
18696 * FONT_BASE (font)) / FONT_HEIGHT (font));
18697 saved_pos = it->position;
18698 memset (&it->position, 0, sizeof it->position);
18699 saved_avoid_cursor = it->avoid_cursor_p;
18700 it->avoid_cursor_p = 1;
18701 saved_face_id = it->face_id;
18702 saved_box_start = it->start_of_box_run_p;
18703 /* The last row's stretch glyph should get the default
18704 face, to avoid painting the rest of the window with
18705 the region face, if the region ends at ZV. */
18706 if (it->glyph_row->ends_at_zv_p)
18707 it->face_id = default_face->id;
18708 else
18709 it->face_id = face->id;
18710 it->start_of_box_run_p = 0;
18711 append_stretch_glyph (it, make_number (0), stretch_width,
18712 it->ascent + it->descent, stretch_ascent);
18713 it->position = saved_pos;
18714 it->avoid_cursor_p = saved_avoid_cursor;
18715 it->face_id = saved_face_id;
18716 it->start_of_box_run_p = saved_box_start;
18719 #endif /* HAVE_WINDOW_SYSTEM */
18721 else
18723 /* Save some values that must not be changed. */
18724 int saved_x = it->current_x;
18725 struct text_pos saved_pos;
18726 Lisp_Object saved_object;
18727 enum display_element_type saved_what = it->what;
18728 int saved_face_id = it->face_id;
18730 saved_object = it->object;
18731 saved_pos = it->position;
18733 it->what = IT_CHARACTER;
18734 memset (&it->position, 0, sizeof it->position);
18735 it->object = make_number (0);
18736 it->c = it->char_to_display = ' ';
18737 it->len = 1;
18738 /* The last row's blank glyphs should get the default face, to
18739 avoid painting the rest of the window with the region face,
18740 if the region ends at ZV. */
18741 if (it->glyph_row->ends_at_zv_p)
18742 it->face_id = default_face->id;
18743 else
18744 it->face_id = face->id;
18746 PRODUCE_GLYPHS (it);
18748 while (it->current_x <= it->last_visible_x)
18749 PRODUCE_GLYPHS (it);
18751 /* Don't count these blanks really. It would let us insert a left
18752 truncation glyph below and make us set the cursor on them, maybe. */
18753 it->current_x = saved_x;
18754 it->object = saved_object;
18755 it->position = saved_pos;
18756 it->what = saved_what;
18757 it->face_id = saved_face_id;
18762 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18763 trailing whitespace. */
18765 static int
18766 trailing_whitespace_p (ptrdiff_t charpos)
18768 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18769 int c = 0;
18771 while (bytepos < ZV_BYTE
18772 && (c = FETCH_CHAR (bytepos),
18773 c == ' ' || c == '\t'))
18774 ++bytepos;
18776 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18778 if (bytepos != PT_BYTE)
18779 return 1;
18781 return 0;
18785 /* Highlight trailing whitespace, if any, in ROW. */
18787 static void
18788 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18790 int used = row->used[TEXT_AREA];
18792 if (used)
18794 struct glyph *start = row->glyphs[TEXT_AREA];
18795 struct glyph *glyph = start + used - 1;
18797 if (row->reversed_p)
18799 /* Right-to-left rows need to be processed in the opposite
18800 direction, so swap the edge pointers. */
18801 glyph = start;
18802 start = row->glyphs[TEXT_AREA] + used - 1;
18805 /* Skip over glyphs inserted to display the cursor at the
18806 end of a line, for extending the face of the last glyph
18807 to the end of the line on terminals, and for truncation
18808 and continuation glyphs. */
18809 if (!row->reversed_p)
18811 while (glyph >= start
18812 && glyph->type == CHAR_GLYPH
18813 && INTEGERP (glyph->object))
18814 --glyph;
18816 else
18818 while (glyph <= start
18819 && glyph->type == CHAR_GLYPH
18820 && INTEGERP (glyph->object))
18821 ++glyph;
18824 /* If last glyph is a space or stretch, and it's trailing
18825 whitespace, set the face of all trailing whitespace glyphs in
18826 IT->glyph_row to `trailing-whitespace'. */
18827 if ((row->reversed_p ? glyph <= start : glyph >= start)
18828 && BUFFERP (glyph->object)
18829 && (glyph->type == STRETCH_GLYPH
18830 || (glyph->type == CHAR_GLYPH
18831 && glyph->u.ch == ' '))
18832 && trailing_whitespace_p (glyph->charpos))
18834 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18835 if (face_id < 0)
18836 return;
18838 if (!row->reversed_p)
18840 while (glyph >= start
18841 && BUFFERP (glyph->object)
18842 && (glyph->type == STRETCH_GLYPH
18843 || (glyph->type == CHAR_GLYPH
18844 && glyph->u.ch == ' ')))
18845 (glyph--)->face_id = face_id;
18847 else
18849 while (glyph <= start
18850 && BUFFERP (glyph->object)
18851 && (glyph->type == STRETCH_GLYPH
18852 || (glyph->type == CHAR_GLYPH
18853 && glyph->u.ch == ' ')))
18854 (glyph++)->face_id = face_id;
18861 /* Value is non-zero if glyph row ROW should be
18862 considered to hold the buffer position CHARPOS. */
18864 static int
18865 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18867 int result = 1;
18869 if (charpos == CHARPOS (row->end.pos)
18870 || charpos == MATRIX_ROW_END_CHARPOS (row))
18872 /* Suppose the row ends on a string.
18873 Unless the row is continued, that means it ends on a newline
18874 in the string. If it's anything other than a display string
18875 (e.g., a before-string from an overlay), we don't want the
18876 cursor there. (This heuristic seems to give the optimal
18877 behavior for the various types of multi-line strings.)
18878 One exception: if the string has `cursor' property on one of
18879 its characters, we _do_ want the cursor there. */
18880 if (CHARPOS (row->end.string_pos) >= 0)
18882 if (row->continued_p)
18883 result = 1;
18884 else
18886 /* Check for `display' property. */
18887 struct glyph *beg = row->glyphs[TEXT_AREA];
18888 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18889 struct glyph *glyph;
18891 result = 0;
18892 for (glyph = end; glyph >= beg; --glyph)
18893 if (STRINGP (glyph->object))
18895 Lisp_Object prop
18896 = Fget_char_property (make_number (charpos),
18897 Qdisplay, Qnil);
18898 result =
18899 (!NILP (prop)
18900 && display_prop_string_p (prop, glyph->object));
18901 /* If there's a `cursor' property on one of the
18902 string's characters, this row is a cursor row,
18903 even though this is not a display string. */
18904 if (!result)
18906 Lisp_Object s = glyph->object;
18908 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18910 ptrdiff_t gpos = glyph->charpos;
18912 if (!NILP (Fget_char_property (make_number (gpos),
18913 Qcursor, s)))
18915 result = 1;
18916 break;
18920 break;
18924 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18926 /* If the row ends in middle of a real character,
18927 and the line is continued, we want the cursor here.
18928 That's because CHARPOS (ROW->end.pos) would equal
18929 PT if PT is before the character. */
18930 if (!row->ends_in_ellipsis_p)
18931 result = row->continued_p;
18932 else
18933 /* If the row ends in an ellipsis, then
18934 CHARPOS (ROW->end.pos) will equal point after the
18935 invisible text. We want that position to be displayed
18936 after the ellipsis. */
18937 result = 0;
18939 /* If the row ends at ZV, display the cursor at the end of that
18940 row instead of at the start of the row below. */
18941 else if (row->ends_at_zv_p)
18942 result = 1;
18943 else
18944 result = 0;
18947 return result;
18950 /* Value is non-zero if glyph row ROW should be
18951 used to hold the cursor. */
18953 static int
18954 cursor_row_p (struct glyph_row *row)
18956 return row_for_charpos_p (row, PT);
18961 /* Push the property PROP so that it will be rendered at the current
18962 position in IT. Return 1 if PROP was successfully pushed, 0
18963 otherwise. Called from handle_line_prefix to handle the
18964 `line-prefix' and `wrap-prefix' properties. */
18966 static int
18967 push_prefix_prop (struct it *it, Lisp_Object prop)
18969 struct text_pos pos =
18970 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18972 eassert (it->method == GET_FROM_BUFFER
18973 || it->method == GET_FROM_DISPLAY_VECTOR
18974 || it->method == GET_FROM_STRING);
18976 /* We need to save the current buffer/string position, so it will be
18977 restored by pop_it, because iterate_out_of_display_property
18978 depends on that being set correctly, but some situations leave
18979 it->position not yet set when this function is called. */
18980 push_it (it, &pos);
18982 if (STRINGP (prop))
18984 if (SCHARS (prop) == 0)
18986 pop_it (it);
18987 return 0;
18990 it->string = prop;
18991 it->string_from_prefix_prop_p = 1;
18992 it->multibyte_p = STRING_MULTIBYTE (it->string);
18993 it->current.overlay_string_index = -1;
18994 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18995 it->end_charpos = it->string_nchars = SCHARS (it->string);
18996 it->method = GET_FROM_STRING;
18997 it->stop_charpos = 0;
18998 it->prev_stop = 0;
18999 it->base_level_stop = 0;
19001 /* Force paragraph direction to be that of the parent
19002 buffer/string. */
19003 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19004 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19005 else
19006 it->paragraph_embedding = L2R;
19008 /* Set up the bidi iterator for this display string. */
19009 if (it->bidi_p)
19011 it->bidi_it.string.lstring = it->string;
19012 it->bidi_it.string.s = NULL;
19013 it->bidi_it.string.schars = it->end_charpos;
19014 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19015 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19016 it->bidi_it.string.unibyte = !it->multibyte_p;
19017 it->bidi_it.w = it->w;
19018 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19021 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19023 it->method = GET_FROM_STRETCH;
19024 it->object = prop;
19026 #ifdef HAVE_WINDOW_SYSTEM
19027 else if (IMAGEP (prop))
19029 it->what = IT_IMAGE;
19030 it->image_id = lookup_image (it->f, prop);
19031 it->method = GET_FROM_IMAGE;
19033 #endif /* HAVE_WINDOW_SYSTEM */
19034 else
19036 pop_it (it); /* bogus display property, give up */
19037 return 0;
19040 return 1;
19043 /* Return the character-property PROP at the current position in IT. */
19045 static Lisp_Object
19046 get_it_property (struct it *it, Lisp_Object prop)
19048 Lisp_Object position, object = it->object;
19050 if (STRINGP (object))
19051 position = make_number (IT_STRING_CHARPOS (*it));
19052 else if (BUFFERP (object))
19054 position = make_number (IT_CHARPOS (*it));
19055 object = it->window;
19057 else
19058 return Qnil;
19060 return Fget_char_property (position, prop, object);
19063 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19065 static void
19066 handle_line_prefix (struct it *it)
19068 Lisp_Object prefix;
19070 if (it->continuation_lines_width > 0)
19072 prefix = get_it_property (it, Qwrap_prefix);
19073 if (NILP (prefix))
19074 prefix = Vwrap_prefix;
19076 else
19078 prefix = get_it_property (it, Qline_prefix);
19079 if (NILP (prefix))
19080 prefix = Vline_prefix;
19082 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19084 /* If the prefix is wider than the window, and we try to wrap
19085 it, it would acquire its own wrap prefix, and so on till the
19086 iterator stack overflows. So, don't wrap the prefix. */
19087 it->line_wrap = TRUNCATE;
19088 it->avoid_cursor_p = 1;
19094 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19095 only for R2L lines from display_line and display_string, when they
19096 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19097 the line/string needs to be continued on the next glyph row. */
19098 static void
19099 unproduce_glyphs (struct it *it, int n)
19101 struct glyph *glyph, *end;
19103 eassert (it->glyph_row);
19104 eassert (it->glyph_row->reversed_p);
19105 eassert (it->area == TEXT_AREA);
19106 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19108 if (n > it->glyph_row->used[TEXT_AREA])
19109 n = it->glyph_row->used[TEXT_AREA];
19110 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19111 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19112 for ( ; glyph < end; glyph++)
19113 glyph[-n] = *glyph;
19116 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19117 and ROW->maxpos. */
19118 static void
19119 find_row_edges (struct it *it, struct glyph_row *row,
19120 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19121 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19123 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19124 lines' rows is implemented for bidi-reordered rows. */
19126 /* ROW->minpos is the value of min_pos, the minimal buffer position
19127 we have in ROW, or ROW->start.pos if that is smaller. */
19128 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19129 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19130 else
19131 /* We didn't find buffer positions smaller than ROW->start, or
19132 didn't find _any_ valid buffer positions in any of the glyphs,
19133 so we must trust the iterator's computed positions. */
19134 row->minpos = row->start.pos;
19135 if (max_pos <= 0)
19137 max_pos = CHARPOS (it->current.pos);
19138 max_bpos = BYTEPOS (it->current.pos);
19141 /* Here are the various use-cases for ending the row, and the
19142 corresponding values for ROW->maxpos:
19144 Line ends in a newline from buffer eol_pos + 1
19145 Line is continued from buffer max_pos + 1
19146 Line is truncated on right it->current.pos
19147 Line ends in a newline from string max_pos + 1(*)
19148 (*) + 1 only when line ends in a forward scan
19149 Line is continued from string max_pos
19150 Line is continued from display vector max_pos
19151 Line is entirely from a string min_pos == max_pos
19152 Line is entirely from a display vector min_pos == max_pos
19153 Line that ends at ZV ZV
19155 If you discover other use-cases, please add them here as
19156 appropriate. */
19157 if (row->ends_at_zv_p)
19158 row->maxpos = it->current.pos;
19159 else if (row->used[TEXT_AREA])
19161 int seen_this_string = 0;
19162 struct glyph_row *r1 = row - 1;
19164 /* Did we see the same display string on the previous row? */
19165 if (STRINGP (it->object)
19166 /* this is not the first row */
19167 && row > it->w->desired_matrix->rows
19168 /* previous row is not the header line */
19169 && !r1->mode_line_p
19170 /* previous row also ends in a newline from a string */
19171 && r1->ends_in_newline_from_string_p)
19173 struct glyph *start, *end;
19175 /* Search for the last glyph of the previous row that came
19176 from buffer or string. Depending on whether the row is
19177 L2R or R2L, we need to process it front to back or the
19178 other way round. */
19179 if (!r1->reversed_p)
19181 start = r1->glyphs[TEXT_AREA];
19182 end = start + r1->used[TEXT_AREA];
19183 /* Glyphs inserted by redisplay have an integer (zero)
19184 as their object. */
19185 while (end > start
19186 && INTEGERP ((end - 1)->object)
19187 && (end - 1)->charpos <= 0)
19188 --end;
19189 if (end > start)
19191 if (EQ ((end - 1)->object, it->object))
19192 seen_this_string = 1;
19194 else
19195 /* If all the glyphs of the previous row were inserted
19196 by redisplay, it means the previous row was
19197 produced from a single newline, which is only
19198 possible if that newline came from the same string
19199 as the one which produced this ROW. */
19200 seen_this_string = 1;
19202 else
19204 end = r1->glyphs[TEXT_AREA] - 1;
19205 start = end + r1->used[TEXT_AREA];
19206 while (end < start
19207 && INTEGERP ((end + 1)->object)
19208 && (end + 1)->charpos <= 0)
19209 ++end;
19210 if (end < start)
19212 if (EQ ((end + 1)->object, it->object))
19213 seen_this_string = 1;
19215 else
19216 seen_this_string = 1;
19219 /* Take note of each display string that covers a newline only
19220 once, the first time we see it. This is for when a display
19221 string includes more than one newline in it. */
19222 if (row->ends_in_newline_from_string_p && !seen_this_string)
19224 /* If we were scanning the buffer forward when we displayed
19225 the string, we want to account for at least one buffer
19226 position that belongs to this row (position covered by
19227 the display string), so that cursor positioning will
19228 consider this row as a candidate when point is at the end
19229 of the visual line represented by this row. This is not
19230 required when scanning back, because max_pos will already
19231 have a much larger value. */
19232 if (CHARPOS (row->end.pos) > max_pos)
19233 INC_BOTH (max_pos, max_bpos);
19234 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19236 else if (CHARPOS (it->eol_pos) > 0)
19237 SET_TEXT_POS (row->maxpos,
19238 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19239 else if (row->continued_p)
19241 /* If max_pos is different from IT's current position, it
19242 means IT->method does not belong to the display element
19243 at max_pos. However, it also means that the display
19244 element at max_pos was displayed in its entirety on this
19245 line, which is equivalent to saying that the next line
19246 starts at the next buffer position. */
19247 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19248 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19249 else
19251 INC_BOTH (max_pos, max_bpos);
19252 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19255 else if (row->truncated_on_right_p)
19256 /* display_line already called reseat_at_next_visible_line_start,
19257 which puts the iterator at the beginning of the next line, in
19258 the logical order. */
19259 row->maxpos = it->current.pos;
19260 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19261 /* A line that is entirely from a string/image/stretch... */
19262 row->maxpos = row->minpos;
19263 else
19264 emacs_abort ();
19266 else
19267 row->maxpos = it->current.pos;
19270 /* Construct the glyph row IT->glyph_row in the desired matrix of
19271 IT->w from text at the current position of IT. See dispextern.h
19272 for an overview of struct it. Value is non-zero if
19273 IT->glyph_row displays text, as opposed to a line displaying ZV
19274 only. */
19276 static int
19277 display_line (struct it *it)
19279 struct glyph_row *row = it->glyph_row;
19280 Lisp_Object overlay_arrow_string;
19281 struct it wrap_it;
19282 void *wrap_data = NULL;
19283 int may_wrap = 0, wrap_x IF_LINT (= 0);
19284 int wrap_row_used = -1;
19285 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19286 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19287 int wrap_row_extra_line_spacing IF_LINT (= 0);
19288 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19289 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19290 int cvpos;
19291 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19292 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19294 /* We always start displaying at hpos zero even if hscrolled. */
19295 eassert (it->hpos == 0 && it->current_x == 0);
19297 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19298 >= it->w->desired_matrix->nrows)
19300 it->w->nrows_scale_factor++;
19301 it->f->fonts_changed = 1;
19302 return 0;
19305 /* Clear the result glyph row and enable it. */
19306 prepare_desired_row (row);
19308 row->y = it->current_y;
19309 row->start = it->start;
19310 row->continuation_lines_width = it->continuation_lines_width;
19311 row->displays_text_p = 1;
19312 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19313 it->starts_in_middle_of_char_p = 0;
19315 /* Arrange the overlays nicely for our purposes. Usually, we call
19316 display_line on only one line at a time, in which case this
19317 can't really hurt too much, or we call it on lines which appear
19318 one after another in the buffer, in which case all calls to
19319 recenter_overlay_lists but the first will be pretty cheap. */
19320 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19322 /* Move over display elements that are not visible because we are
19323 hscrolled. This may stop at an x-position < IT->first_visible_x
19324 if the first glyph is partially visible or if we hit a line end. */
19325 if (it->current_x < it->first_visible_x)
19327 enum move_it_result move_result;
19329 this_line_min_pos = row->start.pos;
19330 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19331 MOVE_TO_POS | MOVE_TO_X);
19332 /* If we are under a large hscroll, move_it_in_display_line_to
19333 could hit the end of the line without reaching
19334 it->first_visible_x. Pretend that we did reach it. This is
19335 especially important on a TTY, where we will call
19336 extend_face_to_end_of_line, which needs to know how many
19337 blank glyphs to produce. */
19338 if (it->current_x < it->first_visible_x
19339 && (move_result == MOVE_NEWLINE_OR_CR
19340 || move_result == MOVE_POS_MATCH_OR_ZV))
19341 it->current_x = it->first_visible_x;
19343 /* Record the smallest positions seen while we moved over
19344 display elements that are not visible. This is needed by
19345 redisplay_internal for optimizing the case where the cursor
19346 stays inside the same line. The rest of this function only
19347 considers positions that are actually displayed, so
19348 RECORD_MAX_MIN_POS will not otherwise record positions that
19349 are hscrolled to the left of the left edge of the window. */
19350 min_pos = CHARPOS (this_line_min_pos);
19351 min_bpos = BYTEPOS (this_line_min_pos);
19353 else
19355 /* We only do this when not calling `move_it_in_display_line_to'
19356 above, because move_it_in_display_line_to calls
19357 handle_line_prefix itself. */
19358 handle_line_prefix (it);
19361 /* Get the initial row height. This is either the height of the
19362 text hscrolled, if there is any, or zero. */
19363 row->ascent = it->max_ascent;
19364 row->height = it->max_ascent + it->max_descent;
19365 row->phys_ascent = it->max_phys_ascent;
19366 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19367 row->extra_line_spacing = it->max_extra_line_spacing;
19369 /* Utility macro to record max and min buffer positions seen until now. */
19370 #define RECORD_MAX_MIN_POS(IT) \
19371 do \
19373 int composition_p = !STRINGP ((IT)->string) \
19374 && ((IT)->what == IT_COMPOSITION); \
19375 ptrdiff_t current_pos = \
19376 composition_p ? (IT)->cmp_it.charpos \
19377 : IT_CHARPOS (*(IT)); \
19378 ptrdiff_t current_bpos = \
19379 composition_p ? CHAR_TO_BYTE (current_pos) \
19380 : IT_BYTEPOS (*(IT)); \
19381 if (current_pos < min_pos) \
19383 min_pos = current_pos; \
19384 min_bpos = current_bpos; \
19386 if (IT_CHARPOS (*it) > max_pos) \
19388 max_pos = IT_CHARPOS (*it); \
19389 max_bpos = IT_BYTEPOS (*it); \
19392 while (0)
19394 /* Loop generating characters. The loop is left with IT on the next
19395 character to display. */
19396 while (1)
19398 int n_glyphs_before, hpos_before, x_before;
19399 int x, nglyphs;
19400 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19402 /* Retrieve the next thing to display. Value is zero if end of
19403 buffer reached. */
19404 if (!get_next_display_element (it))
19406 /* Maybe add a space at the end of this line that is used to
19407 display the cursor there under X. Set the charpos of the
19408 first glyph of blank lines not corresponding to any text
19409 to -1. */
19410 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19411 row->exact_window_width_line_p = 1;
19412 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19413 || row->used[TEXT_AREA] == 0)
19415 row->glyphs[TEXT_AREA]->charpos = -1;
19416 row->displays_text_p = 0;
19418 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19419 && (!MINI_WINDOW_P (it->w)
19420 || (minibuf_level && EQ (it->window, minibuf_window))))
19421 row->indicate_empty_line_p = 1;
19424 it->continuation_lines_width = 0;
19425 row->ends_at_zv_p = 1;
19426 /* A row that displays right-to-left text must always have
19427 its last face extended all the way to the end of line,
19428 even if this row ends in ZV, because we still write to
19429 the screen left to right. We also need to extend the
19430 last face if the default face is remapped to some
19431 different face, otherwise the functions that clear
19432 portions of the screen will clear with the default face's
19433 background color. */
19434 if (row->reversed_p
19435 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19436 extend_face_to_end_of_line (it);
19437 break;
19440 /* Now, get the metrics of what we want to display. This also
19441 generates glyphs in `row' (which is IT->glyph_row). */
19442 n_glyphs_before = row->used[TEXT_AREA];
19443 x = it->current_x;
19445 /* Remember the line height so far in case the next element doesn't
19446 fit on the line. */
19447 if (it->line_wrap != TRUNCATE)
19449 ascent = it->max_ascent;
19450 descent = it->max_descent;
19451 phys_ascent = it->max_phys_ascent;
19452 phys_descent = it->max_phys_descent;
19454 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19456 if (IT_DISPLAYING_WHITESPACE (it))
19457 may_wrap = 1;
19458 else if (may_wrap)
19460 SAVE_IT (wrap_it, *it, wrap_data);
19461 wrap_x = x;
19462 wrap_row_used = row->used[TEXT_AREA];
19463 wrap_row_ascent = row->ascent;
19464 wrap_row_height = row->height;
19465 wrap_row_phys_ascent = row->phys_ascent;
19466 wrap_row_phys_height = row->phys_height;
19467 wrap_row_extra_line_spacing = row->extra_line_spacing;
19468 wrap_row_min_pos = min_pos;
19469 wrap_row_min_bpos = min_bpos;
19470 wrap_row_max_pos = max_pos;
19471 wrap_row_max_bpos = max_bpos;
19472 may_wrap = 0;
19477 PRODUCE_GLYPHS (it);
19479 /* If this display element was in marginal areas, continue with
19480 the next one. */
19481 if (it->area != TEXT_AREA)
19483 row->ascent = max (row->ascent, it->max_ascent);
19484 row->height = max (row->height, it->max_ascent + it->max_descent);
19485 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19486 row->phys_height = max (row->phys_height,
19487 it->max_phys_ascent + it->max_phys_descent);
19488 row->extra_line_spacing = max (row->extra_line_spacing,
19489 it->max_extra_line_spacing);
19490 set_iterator_to_next (it, 1);
19491 continue;
19494 /* Does the display element fit on the line? If we truncate
19495 lines, we should draw past the right edge of the window. If
19496 we don't truncate, we want to stop so that we can display the
19497 continuation glyph before the right margin. If lines are
19498 continued, there are two possible strategies for characters
19499 resulting in more than 1 glyph (e.g. tabs): Display as many
19500 glyphs as possible in this line and leave the rest for the
19501 continuation line, or display the whole element in the next
19502 line. Original redisplay did the former, so we do it also. */
19503 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19504 hpos_before = it->hpos;
19505 x_before = x;
19507 if (/* Not a newline. */
19508 nglyphs > 0
19509 /* Glyphs produced fit entirely in the line. */
19510 && it->current_x < it->last_visible_x)
19512 it->hpos += nglyphs;
19513 row->ascent = max (row->ascent, it->max_ascent);
19514 row->height = max (row->height, it->max_ascent + it->max_descent);
19515 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19516 row->phys_height = max (row->phys_height,
19517 it->max_phys_ascent + it->max_phys_descent);
19518 row->extra_line_spacing = max (row->extra_line_spacing,
19519 it->max_extra_line_spacing);
19520 if (it->current_x - it->pixel_width < it->first_visible_x)
19521 row->x = x - it->first_visible_x;
19522 /* Record the maximum and minimum buffer positions seen so
19523 far in glyphs that will be displayed by this row. */
19524 if (it->bidi_p)
19525 RECORD_MAX_MIN_POS (it);
19527 else
19529 int i, new_x;
19530 struct glyph *glyph;
19532 for (i = 0; i < nglyphs; ++i, x = new_x)
19534 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19535 new_x = x + glyph->pixel_width;
19537 if (/* Lines are continued. */
19538 it->line_wrap != TRUNCATE
19539 && (/* Glyph doesn't fit on the line. */
19540 new_x > it->last_visible_x
19541 /* Or it fits exactly on a window system frame. */
19542 || (new_x == it->last_visible_x
19543 && FRAME_WINDOW_P (it->f)
19544 && (row->reversed_p
19545 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19546 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19548 /* End of a continued line. */
19550 if (it->hpos == 0
19551 || (new_x == it->last_visible_x
19552 && FRAME_WINDOW_P (it->f)
19553 && (row->reversed_p
19554 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19555 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19557 /* Current glyph is the only one on the line or
19558 fits exactly on the line. We must continue
19559 the line because we can't draw the cursor
19560 after the glyph. */
19561 row->continued_p = 1;
19562 it->current_x = new_x;
19563 it->continuation_lines_width += new_x;
19564 ++it->hpos;
19565 if (i == nglyphs - 1)
19567 /* If line-wrap is on, check if a previous
19568 wrap point was found. */
19569 if (wrap_row_used > 0
19570 /* Even if there is a previous wrap
19571 point, continue the line here as
19572 usual, if (i) the previous character
19573 was a space or tab AND (ii) the
19574 current character is not. */
19575 && (!may_wrap
19576 || IT_DISPLAYING_WHITESPACE (it)))
19577 goto back_to_wrap;
19579 /* Record the maximum and minimum buffer
19580 positions seen so far in glyphs that will be
19581 displayed by this row. */
19582 if (it->bidi_p)
19583 RECORD_MAX_MIN_POS (it);
19584 set_iterator_to_next (it, 1);
19585 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19587 if (!get_next_display_element (it))
19589 row->exact_window_width_line_p = 1;
19590 it->continuation_lines_width = 0;
19591 row->continued_p = 0;
19592 row->ends_at_zv_p = 1;
19594 else if (ITERATOR_AT_END_OF_LINE_P (it))
19596 row->continued_p = 0;
19597 row->exact_window_width_line_p = 1;
19601 else if (it->bidi_p)
19602 RECORD_MAX_MIN_POS (it);
19604 else if (CHAR_GLYPH_PADDING_P (*glyph)
19605 && !FRAME_WINDOW_P (it->f))
19607 /* A padding glyph that doesn't fit on this line.
19608 This means the whole character doesn't fit
19609 on the line. */
19610 if (row->reversed_p)
19611 unproduce_glyphs (it, row->used[TEXT_AREA]
19612 - n_glyphs_before);
19613 row->used[TEXT_AREA] = n_glyphs_before;
19615 /* Fill the rest of the row with continuation
19616 glyphs like in 20.x. */
19617 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19618 < row->glyphs[1 + TEXT_AREA])
19619 produce_special_glyphs (it, IT_CONTINUATION);
19621 row->continued_p = 1;
19622 it->current_x = x_before;
19623 it->continuation_lines_width += x_before;
19625 /* Restore the height to what it was before the
19626 element not fitting on the line. */
19627 it->max_ascent = ascent;
19628 it->max_descent = descent;
19629 it->max_phys_ascent = phys_ascent;
19630 it->max_phys_descent = phys_descent;
19632 else if (wrap_row_used > 0)
19634 back_to_wrap:
19635 if (row->reversed_p)
19636 unproduce_glyphs (it,
19637 row->used[TEXT_AREA] - wrap_row_used);
19638 RESTORE_IT (it, &wrap_it, wrap_data);
19639 it->continuation_lines_width += wrap_x;
19640 row->used[TEXT_AREA] = wrap_row_used;
19641 row->ascent = wrap_row_ascent;
19642 row->height = wrap_row_height;
19643 row->phys_ascent = wrap_row_phys_ascent;
19644 row->phys_height = wrap_row_phys_height;
19645 row->extra_line_spacing = wrap_row_extra_line_spacing;
19646 min_pos = wrap_row_min_pos;
19647 min_bpos = wrap_row_min_bpos;
19648 max_pos = wrap_row_max_pos;
19649 max_bpos = wrap_row_max_bpos;
19650 row->continued_p = 1;
19651 row->ends_at_zv_p = 0;
19652 row->exact_window_width_line_p = 0;
19653 it->continuation_lines_width += x;
19655 /* Make sure that a non-default face is extended
19656 up to the right margin of the window. */
19657 extend_face_to_end_of_line (it);
19659 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19661 /* A TAB that extends past the right edge of the
19662 window. This produces a single glyph on
19663 window system frames. We leave the glyph in
19664 this row and let it fill the row, but don't
19665 consume the TAB. */
19666 if ((row->reversed_p
19667 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19668 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19669 produce_special_glyphs (it, IT_CONTINUATION);
19670 it->continuation_lines_width += it->last_visible_x;
19671 row->ends_in_middle_of_char_p = 1;
19672 row->continued_p = 1;
19673 glyph->pixel_width = it->last_visible_x - x;
19674 it->starts_in_middle_of_char_p = 1;
19676 else
19678 /* Something other than a TAB that draws past
19679 the right edge of the window. Restore
19680 positions to values before the element. */
19681 if (row->reversed_p)
19682 unproduce_glyphs (it, row->used[TEXT_AREA]
19683 - (n_glyphs_before + i));
19684 row->used[TEXT_AREA] = n_glyphs_before + i;
19686 /* Display continuation glyphs. */
19687 it->current_x = x_before;
19688 it->continuation_lines_width += x;
19689 if (!FRAME_WINDOW_P (it->f)
19690 || (row->reversed_p
19691 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19692 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19693 produce_special_glyphs (it, IT_CONTINUATION);
19694 row->continued_p = 1;
19696 extend_face_to_end_of_line (it);
19698 if (nglyphs > 1 && i > 0)
19700 row->ends_in_middle_of_char_p = 1;
19701 it->starts_in_middle_of_char_p = 1;
19704 /* Restore the height to what it was before the
19705 element not fitting on the line. */
19706 it->max_ascent = ascent;
19707 it->max_descent = descent;
19708 it->max_phys_ascent = phys_ascent;
19709 it->max_phys_descent = phys_descent;
19712 break;
19714 else if (new_x > it->first_visible_x)
19716 /* Increment number of glyphs actually displayed. */
19717 ++it->hpos;
19719 /* Record the maximum and minimum buffer positions
19720 seen so far in glyphs that will be displayed by
19721 this row. */
19722 if (it->bidi_p)
19723 RECORD_MAX_MIN_POS (it);
19725 if (x < it->first_visible_x)
19726 /* Glyph is partially visible, i.e. row starts at
19727 negative X position. */
19728 row->x = x - it->first_visible_x;
19730 else
19732 /* Glyph is completely off the left margin of the
19733 window. This should not happen because of the
19734 move_it_in_display_line at the start of this
19735 function, unless the text display area of the
19736 window is empty. */
19737 eassert (it->first_visible_x <= it->last_visible_x);
19740 /* Even if this display element produced no glyphs at all,
19741 we want to record its position. */
19742 if (it->bidi_p && nglyphs == 0)
19743 RECORD_MAX_MIN_POS (it);
19745 row->ascent = max (row->ascent, it->max_ascent);
19746 row->height = max (row->height, it->max_ascent + it->max_descent);
19747 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19748 row->phys_height = max (row->phys_height,
19749 it->max_phys_ascent + it->max_phys_descent);
19750 row->extra_line_spacing = max (row->extra_line_spacing,
19751 it->max_extra_line_spacing);
19753 /* End of this display line if row is continued. */
19754 if (row->continued_p || row->ends_at_zv_p)
19755 break;
19758 at_end_of_line:
19759 /* Is this a line end? If yes, we're also done, after making
19760 sure that a non-default face is extended up to the right
19761 margin of the window. */
19762 if (ITERATOR_AT_END_OF_LINE_P (it))
19764 int used_before = row->used[TEXT_AREA];
19766 row->ends_in_newline_from_string_p = STRINGP (it->object);
19768 /* Add a space at the end of the line that is used to
19769 display the cursor there. */
19770 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19771 append_space_for_newline (it, 0);
19773 /* Extend the face to the end of the line. */
19774 extend_face_to_end_of_line (it);
19776 /* Make sure we have the position. */
19777 if (used_before == 0)
19778 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19780 /* Record the position of the newline, for use in
19781 find_row_edges. */
19782 it->eol_pos = it->current.pos;
19784 /* Consume the line end. This skips over invisible lines. */
19785 set_iterator_to_next (it, 1);
19786 it->continuation_lines_width = 0;
19787 break;
19790 /* Proceed with next display element. Note that this skips
19791 over lines invisible because of selective display. */
19792 set_iterator_to_next (it, 1);
19794 /* If we truncate lines, we are done when the last displayed
19795 glyphs reach past the right margin of the window. */
19796 if (it->line_wrap == TRUNCATE
19797 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19798 ? (it->current_x >= it->last_visible_x)
19799 : (it->current_x > it->last_visible_x)))
19801 /* Maybe add truncation glyphs. */
19802 if (!FRAME_WINDOW_P (it->f)
19803 || (row->reversed_p
19804 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19805 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19807 int i, n;
19809 if (!row->reversed_p)
19811 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19813 break;
19815 else
19817 for (i = 0; i < row->used[TEXT_AREA]; i++)
19818 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19819 break;
19820 /* Remove any padding glyphs at the front of ROW, to
19821 make room for the truncation glyphs we will be
19822 adding below. The loop below always inserts at
19823 least one truncation glyph, so also remove the
19824 last glyph added to ROW. */
19825 unproduce_glyphs (it, i + 1);
19826 /* Adjust i for the loop below. */
19827 i = row->used[TEXT_AREA] - (i + 1);
19830 it->current_x = x_before;
19831 if (!FRAME_WINDOW_P (it->f))
19833 for (n = row->used[TEXT_AREA]; i < n; ++i)
19835 row->used[TEXT_AREA] = i;
19836 produce_special_glyphs (it, IT_TRUNCATION);
19839 else
19841 row->used[TEXT_AREA] = i;
19842 produce_special_glyphs (it, IT_TRUNCATION);
19845 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19847 /* Don't truncate if we can overflow newline into fringe. */
19848 if (!get_next_display_element (it))
19850 it->continuation_lines_width = 0;
19851 row->ends_at_zv_p = 1;
19852 row->exact_window_width_line_p = 1;
19853 break;
19855 if (ITERATOR_AT_END_OF_LINE_P (it))
19857 row->exact_window_width_line_p = 1;
19858 goto at_end_of_line;
19860 it->current_x = x_before;
19863 row->truncated_on_right_p = 1;
19864 it->continuation_lines_width = 0;
19865 reseat_at_next_visible_line_start (it, 0);
19866 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19867 it->hpos = hpos_before;
19868 break;
19872 if (wrap_data)
19873 bidi_unshelve_cache (wrap_data, 1);
19875 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19876 at the left window margin. */
19877 if (it->first_visible_x
19878 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19880 if (!FRAME_WINDOW_P (it->f)
19881 || (row->reversed_p
19882 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19883 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19884 insert_left_trunc_glyphs (it);
19885 row->truncated_on_left_p = 1;
19888 /* Remember the position at which this line ends.
19890 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19891 cannot be before the call to find_row_edges below, since that is
19892 where these positions are determined. */
19893 row->end = it->current;
19894 if (!it->bidi_p)
19896 row->minpos = row->start.pos;
19897 row->maxpos = row->end.pos;
19899 else
19901 /* ROW->minpos and ROW->maxpos must be the smallest and
19902 `1 + the largest' buffer positions in ROW. But if ROW was
19903 bidi-reordered, these two positions can be anywhere in the
19904 row, so we must determine them now. */
19905 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19908 /* If the start of this line is the overlay arrow-position, then
19909 mark this glyph row as the one containing the overlay arrow.
19910 This is clearly a mess with variable size fonts. It would be
19911 better to let it be displayed like cursors under X. */
19912 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19913 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19914 !NILP (overlay_arrow_string)))
19916 /* Overlay arrow in window redisplay is a fringe bitmap. */
19917 if (STRINGP (overlay_arrow_string))
19919 struct glyph_row *arrow_row
19920 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19921 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19922 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19923 struct glyph *p = row->glyphs[TEXT_AREA];
19924 struct glyph *p2, *end;
19926 /* Copy the arrow glyphs. */
19927 while (glyph < arrow_end)
19928 *p++ = *glyph++;
19930 /* Throw away padding glyphs. */
19931 p2 = p;
19932 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19933 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19934 ++p2;
19935 if (p2 > p)
19937 while (p2 < end)
19938 *p++ = *p2++;
19939 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19942 else
19944 eassert (INTEGERP (overlay_arrow_string));
19945 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19947 overlay_arrow_seen = 1;
19950 /* Highlight trailing whitespace. */
19951 if (!NILP (Vshow_trailing_whitespace))
19952 highlight_trailing_whitespace (it->f, it->glyph_row);
19954 /* Compute pixel dimensions of this line. */
19955 compute_line_metrics (it);
19957 /* Implementation note: No changes in the glyphs of ROW or in their
19958 faces can be done past this point, because compute_line_metrics
19959 computes ROW's hash value and stores it within the glyph_row
19960 structure. */
19962 /* Record whether this row ends inside an ellipsis. */
19963 row->ends_in_ellipsis_p
19964 = (it->method == GET_FROM_DISPLAY_VECTOR
19965 && it->ellipsis_p);
19967 /* Save fringe bitmaps in this row. */
19968 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19969 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19970 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19971 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19973 it->left_user_fringe_bitmap = 0;
19974 it->left_user_fringe_face_id = 0;
19975 it->right_user_fringe_bitmap = 0;
19976 it->right_user_fringe_face_id = 0;
19978 /* Maybe set the cursor. */
19979 cvpos = it->w->cursor.vpos;
19980 if ((cvpos < 0
19981 /* In bidi-reordered rows, keep checking for proper cursor
19982 position even if one has been found already, because buffer
19983 positions in such rows change non-linearly with ROW->VPOS,
19984 when a line is continued. One exception: when we are at ZV,
19985 display cursor on the first suitable glyph row, since all
19986 the empty rows after that also have their position set to ZV. */
19987 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19988 lines' rows is implemented for bidi-reordered rows. */
19989 || (it->bidi_p
19990 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19991 && PT >= MATRIX_ROW_START_CHARPOS (row)
19992 && PT <= MATRIX_ROW_END_CHARPOS (row)
19993 && cursor_row_p (row))
19994 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19996 /* Prepare for the next line. This line starts horizontally at (X
19997 HPOS) = (0 0). Vertical positions are incremented. As a
19998 convenience for the caller, IT->glyph_row is set to the next
19999 row to be used. */
20000 it->current_x = it->hpos = 0;
20001 it->current_y += row->height;
20002 SET_TEXT_POS (it->eol_pos, 0, 0);
20003 ++it->vpos;
20004 ++it->glyph_row;
20005 /* The next row should by default use the same value of the
20006 reversed_p flag as this one. set_iterator_to_next decides when
20007 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20008 the flag accordingly. */
20009 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20010 it->glyph_row->reversed_p = row->reversed_p;
20011 it->start = row->end;
20012 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20014 #undef RECORD_MAX_MIN_POS
20017 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20018 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20019 doc: /* Return paragraph direction at point in BUFFER.
20020 Value is either `left-to-right' or `right-to-left'.
20021 If BUFFER is omitted or nil, it defaults to the current buffer.
20023 Paragraph direction determines how the text in the paragraph is displayed.
20024 In left-to-right paragraphs, text begins at the left margin of the window
20025 and the reading direction is generally left to right. In right-to-left
20026 paragraphs, text begins at the right margin and is read from right to left.
20028 See also `bidi-paragraph-direction'. */)
20029 (Lisp_Object buffer)
20031 struct buffer *buf = current_buffer;
20032 struct buffer *old = buf;
20034 if (! NILP (buffer))
20036 CHECK_BUFFER (buffer);
20037 buf = XBUFFER (buffer);
20040 if (NILP (BVAR (buf, bidi_display_reordering))
20041 || NILP (BVAR (buf, enable_multibyte_characters))
20042 /* When we are loading loadup.el, the character property tables
20043 needed for bidi iteration are not yet available. */
20044 || !NILP (Vpurify_flag))
20045 return Qleft_to_right;
20046 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20047 return BVAR (buf, bidi_paragraph_direction);
20048 else
20050 /* Determine the direction from buffer text. We could try to
20051 use current_matrix if it is up to date, but this seems fast
20052 enough as it is. */
20053 struct bidi_it itb;
20054 ptrdiff_t pos = BUF_PT (buf);
20055 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20056 int c;
20057 void *itb_data = bidi_shelve_cache ();
20059 set_buffer_temp (buf);
20060 /* bidi_paragraph_init finds the base direction of the paragraph
20061 by searching forward from paragraph start. We need the base
20062 direction of the current or _previous_ paragraph, so we need
20063 to make sure we are within that paragraph. To that end, find
20064 the previous non-empty line. */
20065 if (pos >= ZV && pos > BEGV)
20066 DEC_BOTH (pos, bytepos);
20067 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20068 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20070 while ((c = FETCH_BYTE (bytepos)) == '\n'
20071 || c == ' ' || c == '\t' || c == '\f')
20073 if (bytepos <= BEGV_BYTE)
20074 break;
20075 bytepos--;
20076 pos--;
20078 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20079 bytepos--;
20081 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20082 itb.paragraph_dir = NEUTRAL_DIR;
20083 itb.string.s = NULL;
20084 itb.string.lstring = Qnil;
20085 itb.string.bufpos = 0;
20086 itb.string.unibyte = 0;
20087 /* We have no window to use here for ignoring window-specific
20088 overlays. Using NULL for window pointer will cause
20089 compute_display_string_pos to use the current buffer. */
20090 itb.w = NULL;
20091 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20092 bidi_unshelve_cache (itb_data, 0);
20093 set_buffer_temp (old);
20094 switch (itb.paragraph_dir)
20096 case L2R:
20097 return Qleft_to_right;
20098 break;
20099 case R2L:
20100 return Qright_to_left;
20101 break;
20102 default:
20103 emacs_abort ();
20108 DEFUN ("move-point-visually", Fmove_point_visually,
20109 Smove_point_visually, 1, 1, 0,
20110 doc: /* Move point in the visual order in the specified DIRECTION.
20111 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20112 left.
20114 Value is the new character position of point. */)
20115 (Lisp_Object direction)
20117 struct window *w = XWINDOW (selected_window);
20118 struct buffer *b = XBUFFER (w->contents);
20119 struct glyph_row *row;
20120 int dir;
20121 Lisp_Object paragraph_dir;
20123 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20124 (!(ROW)->continued_p \
20125 && INTEGERP ((GLYPH)->object) \
20126 && (GLYPH)->type == CHAR_GLYPH \
20127 && (GLYPH)->u.ch == ' ' \
20128 && (GLYPH)->charpos >= 0 \
20129 && !(GLYPH)->avoid_cursor_p)
20131 CHECK_NUMBER (direction);
20132 dir = XINT (direction);
20133 if (dir > 0)
20134 dir = 1;
20135 else
20136 dir = -1;
20138 /* If current matrix is up-to-date, we can use the information
20139 recorded in the glyphs, at least as long as the goal is on the
20140 screen. */
20141 if (w->window_end_valid
20142 && !windows_or_buffers_changed
20143 && b
20144 && !b->clip_changed
20145 && !b->prevent_redisplay_optimizations_p
20146 && !window_outdated (w)
20147 && w->cursor.vpos >= 0
20148 && w->cursor.vpos < w->current_matrix->nrows
20149 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20151 struct glyph *g = row->glyphs[TEXT_AREA];
20152 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20153 struct glyph *gpt = g + w->cursor.hpos;
20155 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20157 if (BUFFERP (g->object) && g->charpos != PT)
20159 SET_PT (g->charpos);
20160 w->cursor.vpos = -1;
20161 return make_number (PT);
20163 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20165 ptrdiff_t new_pos;
20167 if (BUFFERP (gpt->object))
20169 new_pos = PT;
20170 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20171 new_pos += (row->reversed_p ? -dir : dir);
20172 else
20173 new_pos -= (row->reversed_p ? -dir : dir);;
20175 else if (BUFFERP (g->object))
20176 new_pos = g->charpos;
20177 else
20178 break;
20179 SET_PT (new_pos);
20180 w->cursor.vpos = -1;
20181 return make_number (PT);
20183 else if (ROW_GLYPH_NEWLINE_P (row, g))
20185 /* Glyphs inserted at the end of a non-empty line for
20186 positioning the cursor have zero charpos, so we must
20187 deduce the value of point by other means. */
20188 if (g->charpos > 0)
20189 SET_PT (g->charpos);
20190 else if (row->ends_at_zv_p && PT != ZV)
20191 SET_PT (ZV);
20192 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20193 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20194 else
20195 break;
20196 w->cursor.vpos = -1;
20197 return make_number (PT);
20200 if (g == e || INTEGERP (g->object))
20202 if (row->truncated_on_left_p || row->truncated_on_right_p)
20203 goto simulate_display;
20204 if (!row->reversed_p)
20205 row += dir;
20206 else
20207 row -= dir;
20208 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20209 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20210 goto simulate_display;
20212 if (dir > 0)
20214 if (row->reversed_p && !row->continued_p)
20216 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20217 w->cursor.vpos = -1;
20218 return make_number (PT);
20220 g = row->glyphs[TEXT_AREA];
20221 e = g + row->used[TEXT_AREA];
20222 for ( ; g < e; g++)
20224 if (BUFFERP (g->object)
20225 /* Empty lines have only one glyph, which stands
20226 for the newline, and whose charpos is the
20227 buffer position of the newline. */
20228 || ROW_GLYPH_NEWLINE_P (row, g)
20229 /* When the buffer ends in a newline, the line at
20230 EOB also has one glyph, but its charpos is -1. */
20231 || (row->ends_at_zv_p
20232 && !row->reversed_p
20233 && INTEGERP (g->object)
20234 && g->type == CHAR_GLYPH
20235 && g->u.ch == ' '))
20237 if (g->charpos > 0)
20238 SET_PT (g->charpos);
20239 else if (!row->reversed_p
20240 && row->ends_at_zv_p
20241 && PT != ZV)
20242 SET_PT (ZV);
20243 else
20244 continue;
20245 w->cursor.vpos = -1;
20246 return make_number (PT);
20250 else
20252 if (!row->reversed_p && !row->continued_p)
20254 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20255 w->cursor.vpos = -1;
20256 return make_number (PT);
20258 e = row->glyphs[TEXT_AREA];
20259 g = e + row->used[TEXT_AREA] - 1;
20260 for ( ; g >= e; g--)
20262 if (BUFFERP (g->object)
20263 || (ROW_GLYPH_NEWLINE_P (row, g)
20264 && g->charpos > 0)
20265 /* Empty R2L lines on GUI frames have the buffer
20266 position of the newline stored in the stretch
20267 glyph. */
20268 || g->type == STRETCH_GLYPH
20269 || (row->ends_at_zv_p
20270 && row->reversed_p
20271 && INTEGERP (g->object)
20272 && g->type == CHAR_GLYPH
20273 && g->u.ch == ' '))
20275 if (g->charpos > 0)
20276 SET_PT (g->charpos);
20277 else if (row->reversed_p
20278 && row->ends_at_zv_p
20279 && PT != ZV)
20280 SET_PT (ZV);
20281 else
20282 continue;
20283 w->cursor.vpos = -1;
20284 return make_number (PT);
20291 simulate_display:
20293 /* If we wind up here, we failed to move by using the glyphs, so we
20294 need to simulate display instead. */
20296 if (b)
20297 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20298 else
20299 paragraph_dir = Qleft_to_right;
20300 if (EQ (paragraph_dir, Qright_to_left))
20301 dir = -dir;
20302 if (PT <= BEGV && dir < 0)
20303 xsignal0 (Qbeginning_of_buffer);
20304 else if (PT >= ZV && dir > 0)
20305 xsignal0 (Qend_of_buffer);
20306 else
20308 struct text_pos pt;
20309 struct it it;
20310 int pt_x, target_x, pixel_width, pt_vpos;
20311 bool at_eol_p;
20312 bool overshoot_expected = false;
20313 bool target_is_eol_p = false;
20315 /* Setup the arena. */
20316 SET_TEXT_POS (pt, PT, PT_BYTE);
20317 start_display (&it, w, pt);
20319 if (it.cmp_it.id < 0
20320 && it.method == GET_FROM_STRING
20321 && it.area == TEXT_AREA
20322 && it.string_from_display_prop_p
20323 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20324 overshoot_expected = true;
20326 /* Find the X coordinate of point. We start from the beginning
20327 of this or previous line to make sure we are before point in
20328 the logical order (since the move_it_* functions can only
20329 move forward). */
20330 reseat_at_previous_visible_line_start (&it);
20331 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20332 if (IT_CHARPOS (it) != PT)
20333 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20334 -1, -1, -1, MOVE_TO_POS);
20335 pt_x = it.current_x;
20336 pt_vpos = it.vpos;
20337 if (dir > 0 || overshoot_expected)
20339 struct glyph_row *row = it.glyph_row;
20341 /* When point is at beginning of line, we don't have
20342 information about the glyph there loaded into struct
20343 it. Calling get_next_display_element fixes that. */
20344 if (pt_x == 0)
20345 get_next_display_element (&it);
20346 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20347 it.glyph_row = NULL;
20348 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20349 it.glyph_row = row;
20350 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20351 it, lest it will become out of sync with it's buffer
20352 position. */
20353 it.current_x = pt_x;
20355 else
20356 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20357 pixel_width = it.pixel_width;
20358 if (overshoot_expected && at_eol_p)
20359 pixel_width = 0;
20360 else if (pixel_width <= 0)
20361 pixel_width = 1;
20363 /* If there's a display string at point, we are actually at the
20364 glyph to the left of point, so we need to correct the X
20365 coordinate. */
20366 if (overshoot_expected)
20367 pt_x += pixel_width;
20369 /* Compute target X coordinate, either to the left or to the
20370 right of point. On TTY frames, all characters have the same
20371 pixel width of 1, so we can use that. On GUI frames we don't
20372 have an easy way of getting at the pixel width of the
20373 character to the left of point, so we use a different method
20374 of getting to that place. */
20375 if (dir > 0)
20376 target_x = pt_x + pixel_width;
20377 else
20378 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20380 /* Target X coordinate could be one line above or below the line
20381 of point, in which case we need to adjust the target X
20382 coordinate. Also, if moving to the left, we need to begin at
20383 the left edge of the point's screen line. */
20384 if (dir < 0)
20386 if (pt_x > 0)
20388 start_display (&it, w, pt);
20389 reseat_at_previous_visible_line_start (&it);
20390 it.current_x = it.current_y = it.hpos = 0;
20391 if (pt_vpos != 0)
20392 move_it_by_lines (&it, pt_vpos);
20394 else
20396 move_it_by_lines (&it, -1);
20397 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20398 target_is_eol_p = true;
20401 else
20403 if (at_eol_p
20404 || (target_x >= it.last_visible_x
20405 && it.line_wrap != TRUNCATE))
20407 if (pt_x > 0)
20408 move_it_by_lines (&it, 0);
20409 move_it_by_lines (&it, 1);
20410 target_x = 0;
20414 /* Move to the target X coordinate. */
20415 #ifdef HAVE_WINDOW_SYSTEM
20416 /* On GUI frames, as we don't know the X coordinate of the
20417 character to the left of point, moving point to the left
20418 requires walking, one grapheme cluster at a time, until we
20419 find ourself at a place immediately to the left of the
20420 character at point. */
20421 if (FRAME_WINDOW_P (it.f) && dir < 0)
20423 struct text_pos new_pos = it.current.pos;
20424 enum move_it_result rc = MOVE_X_REACHED;
20426 while (it.current_x + it.pixel_width <= target_x
20427 && rc == MOVE_X_REACHED)
20429 int new_x = it.current_x + it.pixel_width;
20431 new_pos = it.current.pos;
20432 if (new_x == it.current_x)
20433 new_x++;
20434 rc = move_it_in_display_line_to (&it, ZV, new_x,
20435 MOVE_TO_POS | MOVE_TO_X);
20436 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20437 break;
20439 /* If we ended up on a composed character inside
20440 bidi-reordered text (e.g., Hebrew text with diacritics),
20441 the iterator gives us the buffer position of the last (in
20442 logical order) character of the composed grapheme cluster,
20443 which is not what we want. So we cheat: we compute the
20444 character position of the character that follows (in the
20445 logical order) the one where the above loop stopped. That
20446 character will appear on display to the left of point. */
20447 if (it.bidi_p
20448 && it.bidi_it.scan_dir == -1
20449 && new_pos.charpos - IT_CHARPOS (it) > 1)
20451 new_pos.charpos = IT_CHARPOS (it) + 1;
20452 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20454 it.current.pos = new_pos;
20456 else
20457 #endif
20458 if (it.current_x != target_x)
20459 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20461 /* When lines are truncated, the above loop will stop at the
20462 window edge. But we want to get to the end of line, even if
20463 it is beyond the window edge; automatic hscroll will then
20464 scroll the window to show point as appropriate. */
20465 if (target_is_eol_p && it.line_wrap == TRUNCATE
20466 && get_next_display_element (&it))
20468 struct text_pos new_pos = it.current.pos;
20470 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20472 set_iterator_to_next (&it, 0);
20473 if (it.method == GET_FROM_BUFFER)
20474 new_pos = it.current.pos;
20475 if (!get_next_display_element (&it))
20476 break;
20479 it.current.pos = new_pos;
20482 /* If we ended up in a display string that covers point, move to
20483 buffer position to the right in the visual order. */
20484 if (dir > 0)
20486 while (IT_CHARPOS (it) == PT)
20488 set_iterator_to_next (&it, 0);
20489 if (!get_next_display_element (&it))
20490 break;
20494 /* Move point to that position. */
20495 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20498 return make_number (PT);
20500 #undef ROW_GLYPH_NEWLINE_P
20504 /***********************************************************************
20505 Menu Bar
20506 ***********************************************************************/
20508 /* Redisplay the menu bar in the frame for window W.
20510 The menu bar of X frames that don't have X toolkit support is
20511 displayed in a special window W->frame->menu_bar_window.
20513 The menu bar of terminal frames is treated specially as far as
20514 glyph matrices are concerned. Menu bar lines are not part of
20515 windows, so the update is done directly on the frame matrix rows
20516 for the menu bar. */
20518 static void
20519 display_menu_bar (struct window *w)
20521 struct frame *f = XFRAME (WINDOW_FRAME (w));
20522 struct it it;
20523 Lisp_Object items;
20524 int i;
20526 /* Don't do all this for graphical frames. */
20527 #ifdef HAVE_NTGUI
20528 if (FRAME_W32_P (f))
20529 return;
20530 #endif
20531 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20532 if (FRAME_X_P (f))
20533 return;
20534 #endif
20536 #ifdef HAVE_NS
20537 if (FRAME_NS_P (f))
20538 return;
20539 #endif /* HAVE_NS */
20541 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20542 eassert (!FRAME_WINDOW_P (f));
20543 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20544 it.first_visible_x = 0;
20545 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20546 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20547 if (FRAME_WINDOW_P (f))
20549 /* Menu bar lines are displayed in the desired matrix of the
20550 dummy window menu_bar_window. */
20551 struct window *menu_w;
20552 menu_w = XWINDOW (f->menu_bar_window);
20553 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20554 MENU_FACE_ID);
20555 it.first_visible_x = 0;
20556 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20558 else
20559 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20561 /* This is a TTY frame, i.e. character hpos/vpos are used as
20562 pixel x/y. */
20563 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20564 MENU_FACE_ID);
20565 it.first_visible_x = 0;
20566 it.last_visible_x = FRAME_COLS (f);
20569 /* FIXME: This should be controlled by a user option. See the
20570 comments in redisplay_tool_bar and display_mode_line about
20571 this. */
20572 it.paragraph_embedding = L2R;
20574 /* Clear all rows of the menu bar. */
20575 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20577 struct glyph_row *row = it.glyph_row + i;
20578 clear_glyph_row (row);
20579 row->enabled_p = 1;
20580 row->full_width_p = 1;
20583 /* Display all items of the menu bar. */
20584 items = FRAME_MENU_BAR_ITEMS (it.f);
20585 for (i = 0; i < ASIZE (items); i += 4)
20587 Lisp_Object string;
20589 /* Stop at nil string. */
20590 string = AREF (items, i + 1);
20591 if (NILP (string))
20592 break;
20594 /* Remember where item was displayed. */
20595 ASET (items, i + 3, make_number (it.hpos));
20597 /* Display the item, pad with one space. */
20598 if (it.current_x < it.last_visible_x)
20599 display_string (NULL, string, Qnil, 0, 0, &it,
20600 SCHARS (string) + 1, 0, 0, -1);
20603 /* Fill out the line with spaces. */
20604 if (it.current_x < it.last_visible_x)
20605 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20607 /* Compute the total height of the lines. */
20608 compute_line_metrics (&it);
20611 /* Deep copy of a glyph row, including the glyphs. */
20612 static void
20613 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20615 struct glyph *pointers[1 + LAST_AREA];
20616 int to_used = to->used[TEXT_AREA];
20618 /* Save glyph pointers of TO. */
20619 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20621 /* Do a structure assignment. */
20622 *to = *from;
20624 /* Restore original glyph pointers of TO. */
20625 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20627 /* Copy the glyphs. */
20628 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20629 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20631 /* If we filled only part of the TO row, fill the rest with
20632 space_glyph (which will display as empty space). */
20633 if (to_used > from->used[TEXT_AREA])
20634 fill_up_frame_row_with_spaces (to, to_used);
20637 /* Display one menu item on a TTY, by overwriting the glyphs in the
20638 frame F's desired glyph matrix with glyphs produced from the menu
20639 item text. Called from term.c to display TTY drop-down menus one
20640 item at a time.
20642 ITEM_TEXT is the menu item text as a C string.
20644 FACE_ID is the face ID to be used for this menu item. FACE_ID
20645 could specify one of 3 faces: a face for an enabled item, a face
20646 for a disabled item, or a face for a selected item.
20648 X and Y are coordinates of the first glyph in the frame's desired
20649 matrix to be overwritten by the menu item. Since this is a TTY, Y
20650 is the zero-based number of the glyph row and X is the zero-based
20651 glyph number in the row, starting from left, where to start
20652 displaying the item.
20654 SUBMENU non-zero means this menu item drops down a submenu, which
20655 should be indicated by displaying a proper visual cue after the
20656 item text. */
20658 void
20659 display_tty_menu_item (const char *item_text, int width, int face_id,
20660 int x, int y, int submenu)
20662 struct it it;
20663 struct frame *f = SELECTED_FRAME ();
20664 struct window *w = XWINDOW (f->selected_window);
20665 int saved_used, saved_truncated, saved_width, saved_reversed;
20666 struct glyph_row *row;
20667 size_t item_len = strlen (item_text);
20669 eassert (FRAME_TERMCAP_P (f));
20671 /* Don't write beyond the matrix's last row. This can happen for
20672 TTY screens that are not high enough to show the entire menu.
20673 (This is actually a bit of defensive programming, as
20674 tty_menu_display already limits the number of menu items to one
20675 less than the number of screen lines.) */
20676 if (y >= f->desired_matrix->nrows)
20677 return;
20679 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20680 it.first_visible_x = 0;
20681 it.last_visible_x = FRAME_COLS (f) - 1;
20682 row = it.glyph_row;
20683 /* Start with the row contents from the current matrix. */
20684 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20685 saved_width = row->full_width_p;
20686 row->full_width_p = 1;
20687 saved_reversed = row->reversed_p;
20688 row->reversed_p = 0;
20689 row->enabled_p = 1;
20691 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20692 desired face. */
20693 eassert (x < f->desired_matrix->matrix_w);
20694 it.current_x = it.hpos = x;
20695 it.current_y = it.vpos = y;
20696 saved_used = row->used[TEXT_AREA];
20697 saved_truncated = row->truncated_on_right_p;
20698 row->used[TEXT_AREA] = x;
20699 it.face_id = face_id;
20700 it.line_wrap = TRUNCATE;
20702 /* FIXME: This should be controlled by a user option. See the
20703 comments in redisplay_tool_bar and display_mode_line about this.
20704 Also, if paragraph_embedding could ever be R2L, changes will be
20705 needed to avoid shifting to the right the row characters in
20706 term.c:append_glyph. */
20707 it.paragraph_embedding = L2R;
20709 /* Pad with a space on the left. */
20710 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20711 width--;
20712 /* Display the menu item, pad with spaces to WIDTH. */
20713 if (submenu)
20715 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20716 item_len, 0, FRAME_COLS (f) - 1, -1);
20717 width -= item_len;
20718 /* Indicate with " >" that there's a submenu. */
20719 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20720 FRAME_COLS (f) - 1, -1);
20722 else
20723 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20724 width, 0, FRAME_COLS (f) - 1, -1);
20726 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20727 row->truncated_on_right_p = saved_truncated;
20728 row->hash = row_hash (row);
20729 row->full_width_p = saved_width;
20730 row->reversed_p = saved_reversed;
20733 /***********************************************************************
20734 Mode Line
20735 ***********************************************************************/
20737 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20738 FORCE is non-zero, redisplay mode lines unconditionally.
20739 Otherwise, redisplay only mode lines that are garbaged. Value is
20740 the number of windows whose mode lines were redisplayed. */
20742 static int
20743 redisplay_mode_lines (Lisp_Object window, int force)
20745 int nwindows = 0;
20747 while (!NILP (window))
20749 struct window *w = XWINDOW (window);
20751 if (WINDOWP (w->contents))
20752 nwindows += redisplay_mode_lines (w->contents, force);
20753 else if (force
20754 || FRAME_GARBAGED_P (XFRAME (w->frame))
20755 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20757 struct text_pos lpoint;
20758 struct buffer *old = current_buffer;
20760 /* Set the window's buffer for the mode line display. */
20761 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20762 set_buffer_internal_1 (XBUFFER (w->contents));
20764 /* Point refers normally to the selected window. For any
20765 other window, set up appropriate value. */
20766 if (!EQ (window, selected_window))
20768 struct text_pos pt;
20770 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20771 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20774 /* Display mode lines. */
20775 clear_glyph_matrix (w->desired_matrix);
20776 if (display_mode_lines (w))
20778 ++nwindows;
20779 w->must_be_updated_p = 1;
20782 /* Restore old settings. */
20783 set_buffer_internal_1 (old);
20784 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20787 window = w->next;
20790 return nwindows;
20794 /* Display the mode and/or header line of window W. Value is the
20795 sum number of mode lines and header lines displayed. */
20797 static int
20798 display_mode_lines (struct window *w)
20800 Lisp_Object old_selected_window = selected_window;
20801 Lisp_Object old_selected_frame = selected_frame;
20802 Lisp_Object new_frame = w->frame;
20803 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20804 int n = 0;
20806 selected_frame = new_frame;
20807 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20808 or window's point, then we'd need select_window_1 here as well. */
20809 XSETWINDOW (selected_window, w);
20810 XFRAME (new_frame)->selected_window = selected_window;
20812 /* These will be set while the mode line specs are processed. */
20813 line_number_displayed = 0;
20814 w->column_number_displayed = -1;
20816 if (WINDOW_WANTS_MODELINE_P (w))
20818 struct window *sel_w = XWINDOW (old_selected_window);
20820 /* Select mode line face based on the real selected window. */
20821 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20822 BVAR (current_buffer, mode_line_format));
20823 ++n;
20826 if (WINDOW_WANTS_HEADER_LINE_P (w))
20828 display_mode_line (w, HEADER_LINE_FACE_ID,
20829 BVAR (current_buffer, header_line_format));
20830 ++n;
20833 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20834 selected_frame = old_selected_frame;
20835 selected_window = old_selected_window;
20836 return n;
20840 /* Display mode or header line of window W. FACE_ID specifies which
20841 line to display; it is either MODE_LINE_FACE_ID or
20842 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20843 display. Value is the pixel height of the mode/header line
20844 displayed. */
20846 static int
20847 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20849 struct it it;
20850 struct face *face;
20851 ptrdiff_t count = SPECPDL_INDEX ();
20853 init_iterator (&it, w, -1, -1, NULL, face_id);
20854 /* Don't extend on a previously drawn mode-line.
20855 This may happen if called from pos_visible_p. */
20856 it.glyph_row->enabled_p = 0;
20857 prepare_desired_row (it.glyph_row);
20859 it.glyph_row->mode_line_p = 1;
20861 /* FIXME: This should be controlled by a user option. But
20862 supporting such an option is not trivial, since the mode line is
20863 made up of many separate strings. */
20864 it.paragraph_embedding = L2R;
20866 record_unwind_protect (unwind_format_mode_line,
20867 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20869 mode_line_target = MODE_LINE_DISPLAY;
20871 /* Temporarily make frame's keyboard the current kboard so that
20872 kboard-local variables in the mode_line_format will get the right
20873 values. */
20874 push_kboard (FRAME_KBOARD (it.f));
20875 record_unwind_save_match_data ();
20876 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20877 pop_kboard ();
20879 unbind_to (count, Qnil);
20881 /* Fill up with spaces. */
20882 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20884 compute_line_metrics (&it);
20885 it.glyph_row->full_width_p = 1;
20886 it.glyph_row->continued_p = 0;
20887 it.glyph_row->truncated_on_left_p = 0;
20888 it.glyph_row->truncated_on_right_p = 0;
20890 /* Make a 3D mode-line have a shadow at its right end. */
20891 face = FACE_FROM_ID (it.f, face_id);
20892 extend_face_to_end_of_line (&it);
20893 if (face->box != FACE_NO_BOX)
20895 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20896 + it.glyph_row->used[TEXT_AREA] - 1);
20897 last->right_box_line_p = 1;
20900 return it.glyph_row->height;
20903 /* Move element ELT in LIST to the front of LIST.
20904 Return the updated list. */
20906 static Lisp_Object
20907 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20909 register Lisp_Object tail, prev;
20910 register Lisp_Object tem;
20912 tail = list;
20913 prev = Qnil;
20914 while (CONSP (tail))
20916 tem = XCAR (tail);
20918 if (EQ (elt, tem))
20920 /* Splice out the link TAIL. */
20921 if (NILP (prev))
20922 list = XCDR (tail);
20923 else
20924 Fsetcdr (prev, XCDR (tail));
20926 /* Now make it the first. */
20927 Fsetcdr (tail, list);
20928 return tail;
20930 else
20931 prev = tail;
20932 tail = XCDR (tail);
20933 QUIT;
20936 /* Not found--return unchanged LIST. */
20937 return list;
20940 /* Contribute ELT to the mode line for window IT->w. How it
20941 translates into text depends on its data type.
20943 IT describes the display environment in which we display, as usual.
20945 DEPTH is the depth in recursion. It is used to prevent
20946 infinite recursion here.
20948 FIELD_WIDTH is the number of characters the display of ELT should
20949 occupy in the mode line, and PRECISION is the maximum number of
20950 characters to display from ELT's representation. See
20951 display_string for details.
20953 Returns the hpos of the end of the text generated by ELT.
20955 PROPS is a property list to add to any string we encounter.
20957 If RISKY is nonzero, remove (disregard) any properties in any string
20958 we encounter, and ignore :eval and :propertize.
20960 The global variable `mode_line_target' determines whether the
20961 output is passed to `store_mode_line_noprop',
20962 `store_mode_line_string', or `display_string'. */
20964 static int
20965 display_mode_element (struct it *it, int depth, int field_width, int precision,
20966 Lisp_Object elt, Lisp_Object props, int risky)
20968 int n = 0, field, prec;
20969 int literal = 0;
20971 tail_recurse:
20972 if (depth > 100)
20973 elt = build_string ("*too-deep*");
20975 depth++;
20977 switch (XTYPE (elt))
20979 case Lisp_String:
20981 /* A string: output it and check for %-constructs within it. */
20982 unsigned char c;
20983 ptrdiff_t offset = 0;
20985 if (SCHARS (elt) > 0
20986 && (!NILP (props) || risky))
20988 Lisp_Object oprops, aelt;
20989 oprops = Ftext_properties_at (make_number (0), elt);
20991 /* If the starting string's properties are not what
20992 we want, translate the string. Also, if the string
20993 is risky, do that anyway. */
20995 if (NILP (Fequal (props, oprops)) || risky)
20997 /* If the starting string has properties,
20998 merge the specified ones onto the existing ones. */
20999 if (! NILP (oprops) && !risky)
21001 Lisp_Object tem;
21003 oprops = Fcopy_sequence (oprops);
21004 tem = props;
21005 while (CONSP (tem))
21007 oprops = Fplist_put (oprops, XCAR (tem),
21008 XCAR (XCDR (tem)));
21009 tem = XCDR (XCDR (tem));
21011 props = oprops;
21014 aelt = Fassoc (elt, mode_line_proptrans_alist);
21015 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21017 /* AELT is what we want. Move it to the front
21018 without consing. */
21019 elt = XCAR (aelt);
21020 mode_line_proptrans_alist
21021 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21023 else
21025 Lisp_Object tem;
21027 /* If AELT has the wrong props, it is useless.
21028 so get rid of it. */
21029 if (! NILP (aelt))
21030 mode_line_proptrans_alist
21031 = Fdelq (aelt, mode_line_proptrans_alist);
21033 elt = Fcopy_sequence (elt);
21034 Fset_text_properties (make_number (0), Flength (elt),
21035 props, elt);
21036 /* Add this item to mode_line_proptrans_alist. */
21037 mode_line_proptrans_alist
21038 = Fcons (Fcons (elt, props),
21039 mode_line_proptrans_alist);
21040 /* Truncate mode_line_proptrans_alist
21041 to at most 50 elements. */
21042 tem = Fnthcdr (make_number (50),
21043 mode_line_proptrans_alist);
21044 if (! NILP (tem))
21045 XSETCDR (tem, Qnil);
21050 offset = 0;
21052 if (literal)
21054 prec = precision - n;
21055 switch (mode_line_target)
21057 case MODE_LINE_NOPROP:
21058 case MODE_LINE_TITLE:
21059 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21060 break;
21061 case MODE_LINE_STRING:
21062 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21063 break;
21064 case MODE_LINE_DISPLAY:
21065 n += display_string (NULL, elt, Qnil, 0, 0, it,
21066 0, prec, 0, STRING_MULTIBYTE (elt));
21067 break;
21070 break;
21073 /* Handle the non-literal case. */
21075 while ((precision <= 0 || n < precision)
21076 && SREF (elt, offset) != 0
21077 && (mode_line_target != MODE_LINE_DISPLAY
21078 || it->current_x < it->last_visible_x))
21080 ptrdiff_t last_offset = offset;
21082 /* Advance to end of string or next format specifier. */
21083 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21086 if (offset - 1 != last_offset)
21088 ptrdiff_t nchars, nbytes;
21090 /* Output to end of string or up to '%'. Field width
21091 is length of string. Don't output more than
21092 PRECISION allows us. */
21093 offset--;
21095 prec = c_string_width (SDATA (elt) + last_offset,
21096 offset - last_offset, precision - n,
21097 &nchars, &nbytes);
21099 switch (mode_line_target)
21101 case MODE_LINE_NOPROP:
21102 case MODE_LINE_TITLE:
21103 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21104 break;
21105 case MODE_LINE_STRING:
21107 ptrdiff_t bytepos = last_offset;
21108 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21109 ptrdiff_t endpos = (precision <= 0
21110 ? string_byte_to_char (elt, offset)
21111 : charpos + nchars);
21113 n += store_mode_line_string (NULL,
21114 Fsubstring (elt, make_number (charpos),
21115 make_number (endpos)),
21116 0, 0, 0, Qnil);
21118 break;
21119 case MODE_LINE_DISPLAY:
21121 ptrdiff_t bytepos = last_offset;
21122 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21124 if (precision <= 0)
21125 nchars = string_byte_to_char (elt, offset) - charpos;
21126 n += display_string (NULL, elt, Qnil, 0, charpos,
21127 it, 0, nchars, 0,
21128 STRING_MULTIBYTE (elt));
21130 break;
21133 else /* c == '%' */
21135 ptrdiff_t percent_position = offset;
21137 /* Get the specified minimum width. Zero means
21138 don't pad. */
21139 field = 0;
21140 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21141 field = field * 10 + c - '0';
21143 /* Don't pad beyond the total padding allowed. */
21144 if (field_width - n > 0 && field > field_width - n)
21145 field = field_width - n;
21147 /* Note that either PRECISION <= 0 or N < PRECISION. */
21148 prec = precision - n;
21150 if (c == 'M')
21151 n += display_mode_element (it, depth, field, prec,
21152 Vglobal_mode_string, props,
21153 risky);
21154 else if (c != 0)
21156 bool multibyte;
21157 ptrdiff_t bytepos, charpos;
21158 const char *spec;
21159 Lisp_Object string;
21161 bytepos = percent_position;
21162 charpos = (STRING_MULTIBYTE (elt)
21163 ? string_byte_to_char (elt, bytepos)
21164 : bytepos);
21165 spec = decode_mode_spec (it->w, c, field, &string);
21166 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21168 switch (mode_line_target)
21170 case MODE_LINE_NOPROP:
21171 case MODE_LINE_TITLE:
21172 n += store_mode_line_noprop (spec, field, prec);
21173 break;
21174 case MODE_LINE_STRING:
21176 Lisp_Object tem = build_string (spec);
21177 props = Ftext_properties_at (make_number (charpos), elt);
21178 /* Should only keep face property in props */
21179 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21181 break;
21182 case MODE_LINE_DISPLAY:
21184 int nglyphs_before, nwritten;
21186 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21187 nwritten = display_string (spec, string, elt,
21188 charpos, 0, it,
21189 field, prec, 0,
21190 multibyte);
21192 /* Assign to the glyphs written above the
21193 string where the `%x' came from, position
21194 of the `%'. */
21195 if (nwritten > 0)
21197 struct glyph *glyph
21198 = (it->glyph_row->glyphs[TEXT_AREA]
21199 + nglyphs_before);
21200 int i;
21202 for (i = 0; i < nwritten; ++i)
21204 glyph[i].object = elt;
21205 glyph[i].charpos = charpos;
21208 n += nwritten;
21211 break;
21214 else /* c == 0 */
21215 break;
21219 break;
21221 case Lisp_Symbol:
21222 /* A symbol: process the value of the symbol recursively
21223 as if it appeared here directly. Avoid error if symbol void.
21224 Special case: if value of symbol is a string, output the string
21225 literally. */
21227 register Lisp_Object tem;
21229 /* If the variable is not marked as risky to set
21230 then its contents are risky to use. */
21231 if (NILP (Fget (elt, Qrisky_local_variable)))
21232 risky = 1;
21234 tem = Fboundp (elt);
21235 if (!NILP (tem))
21237 tem = Fsymbol_value (elt);
21238 /* If value is a string, output that string literally:
21239 don't check for % within it. */
21240 if (STRINGP (tem))
21241 literal = 1;
21243 if (!EQ (tem, elt))
21245 /* Give up right away for nil or t. */
21246 elt = tem;
21247 goto tail_recurse;
21251 break;
21253 case Lisp_Cons:
21255 register Lisp_Object car, tem;
21257 /* A cons cell: five distinct cases.
21258 If first element is :eval or :propertize, do something special.
21259 If first element is a string or a cons, process all the elements
21260 and effectively concatenate them.
21261 If first element is a negative number, truncate displaying cdr to
21262 at most that many characters. If positive, pad (with spaces)
21263 to at least that many characters.
21264 If first element is a symbol, process the cadr or caddr recursively
21265 according to whether the symbol's value is non-nil or nil. */
21266 car = XCAR (elt);
21267 if (EQ (car, QCeval))
21269 /* An element of the form (:eval FORM) means evaluate FORM
21270 and use the result as mode line elements. */
21272 if (risky)
21273 break;
21275 if (CONSP (XCDR (elt)))
21277 Lisp_Object spec;
21278 spec = safe_eval (XCAR (XCDR (elt)));
21279 n += display_mode_element (it, depth, field_width - n,
21280 precision - n, spec, props,
21281 risky);
21284 else if (EQ (car, QCpropertize))
21286 /* An element of the form (:propertize ELT PROPS...)
21287 means display ELT but applying properties PROPS. */
21289 if (risky)
21290 break;
21292 if (CONSP (XCDR (elt)))
21293 n += display_mode_element (it, depth, field_width - n,
21294 precision - n, XCAR (XCDR (elt)),
21295 XCDR (XCDR (elt)), risky);
21297 else if (SYMBOLP (car))
21299 tem = Fboundp (car);
21300 elt = XCDR (elt);
21301 if (!CONSP (elt))
21302 goto invalid;
21303 /* elt is now the cdr, and we know it is a cons cell.
21304 Use its car if CAR has a non-nil value. */
21305 if (!NILP (tem))
21307 tem = Fsymbol_value (car);
21308 if (!NILP (tem))
21310 elt = XCAR (elt);
21311 goto tail_recurse;
21314 /* Symbol's value is nil (or symbol is unbound)
21315 Get the cddr of the original list
21316 and if possible find the caddr and use that. */
21317 elt = XCDR (elt);
21318 if (NILP (elt))
21319 break;
21320 else if (!CONSP (elt))
21321 goto invalid;
21322 elt = XCAR (elt);
21323 goto tail_recurse;
21325 else if (INTEGERP (car))
21327 register int lim = XINT (car);
21328 elt = XCDR (elt);
21329 if (lim < 0)
21331 /* Negative int means reduce maximum width. */
21332 if (precision <= 0)
21333 precision = -lim;
21334 else
21335 precision = min (precision, -lim);
21337 else if (lim > 0)
21339 /* Padding specified. Don't let it be more than
21340 current maximum. */
21341 if (precision > 0)
21342 lim = min (precision, lim);
21344 /* If that's more padding than already wanted, queue it.
21345 But don't reduce padding already specified even if
21346 that is beyond the current truncation point. */
21347 field_width = max (lim, field_width);
21349 goto tail_recurse;
21351 else if (STRINGP (car) || CONSP (car))
21353 Lisp_Object halftail = elt;
21354 int len = 0;
21356 while (CONSP (elt)
21357 && (precision <= 0 || n < precision))
21359 n += display_mode_element (it, depth,
21360 /* Do padding only after the last
21361 element in the list. */
21362 (! CONSP (XCDR (elt))
21363 ? field_width - n
21364 : 0),
21365 precision - n, XCAR (elt),
21366 props, risky);
21367 elt = XCDR (elt);
21368 len++;
21369 if ((len & 1) == 0)
21370 halftail = XCDR (halftail);
21371 /* Check for cycle. */
21372 if (EQ (halftail, elt))
21373 break;
21377 break;
21379 default:
21380 invalid:
21381 elt = build_string ("*invalid*");
21382 goto tail_recurse;
21385 /* Pad to FIELD_WIDTH. */
21386 if (field_width > 0 && n < field_width)
21388 switch (mode_line_target)
21390 case MODE_LINE_NOPROP:
21391 case MODE_LINE_TITLE:
21392 n += store_mode_line_noprop ("", field_width - n, 0);
21393 break;
21394 case MODE_LINE_STRING:
21395 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21396 break;
21397 case MODE_LINE_DISPLAY:
21398 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21399 0, 0, 0);
21400 break;
21404 return n;
21407 /* Store a mode-line string element in mode_line_string_list.
21409 If STRING is non-null, display that C string. Otherwise, the Lisp
21410 string LISP_STRING is displayed.
21412 FIELD_WIDTH is the minimum number of output glyphs to produce.
21413 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21414 with spaces. FIELD_WIDTH <= 0 means don't pad.
21416 PRECISION is the maximum number of characters to output from
21417 STRING. PRECISION <= 0 means don't truncate the string.
21419 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21420 properties to the string.
21422 PROPS are the properties to add to the string.
21423 The mode_line_string_face face property is always added to the string.
21426 static int
21427 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21428 int field_width, int precision, Lisp_Object props)
21430 ptrdiff_t len;
21431 int n = 0;
21433 if (string != NULL)
21435 len = strlen (string);
21436 if (precision > 0 && len > precision)
21437 len = precision;
21438 lisp_string = make_string (string, len);
21439 if (NILP (props))
21440 props = mode_line_string_face_prop;
21441 else if (!NILP (mode_line_string_face))
21443 Lisp_Object face = Fplist_get (props, Qface);
21444 props = Fcopy_sequence (props);
21445 if (NILP (face))
21446 face = mode_line_string_face;
21447 else
21448 face = list2 (face, mode_line_string_face);
21449 props = Fplist_put (props, Qface, face);
21451 Fadd_text_properties (make_number (0), make_number (len),
21452 props, lisp_string);
21454 else
21456 len = XFASTINT (Flength (lisp_string));
21457 if (precision > 0 && len > precision)
21459 len = precision;
21460 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21461 precision = -1;
21463 if (!NILP (mode_line_string_face))
21465 Lisp_Object face;
21466 if (NILP (props))
21467 props = Ftext_properties_at (make_number (0), lisp_string);
21468 face = Fplist_get (props, Qface);
21469 if (NILP (face))
21470 face = mode_line_string_face;
21471 else
21472 face = list2 (face, mode_line_string_face);
21473 props = list2 (Qface, face);
21474 if (copy_string)
21475 lisp_string = Fcopy_sequence (lisp_string);
21477 if (!NILP (props))
21478 Fadd_text_properties (make_number (0), make_number (len),
21479 props, lisp_string);
21482 if (len > 0)
21484 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21485 n += len;
21488 if (field_width > len)
21490 field_width -= len;
21491 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21492 if (!NILP (props))
21493 Fadd_text_properties (make_number (0), make_number (field_width),
21494 props, lisp_string);
21495 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21496 n += field_width;
21499 return n;
21503 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21504 1, 4, 0,
21505 doc: /* Format a string out of a mode line format specification.
21506 First arg FORMAT specifies the mode line format (see `mode-line-format'
21507 for details) to use.
21509 By default, the format is evaluated for the currently selected window.
21511 Optional second arg FACE specifies the face property to put on all
21512 characters for which no face is specified. The value nil means the
21513 default face. The value t means whatever face the window's mode line
21514 currently uses (either `mode-line' or `mode-line-inactive',
21515 depending on whether the window is the selected window or not).
21516 An integer value means the value string has no text
21517 properties.
21519 Optional third and fourth args WINDOW and BUFFER specify the window
21520 and buffer to use as the context for the formatting (defaults
21521 are the selected window and the WINDOW's buffer). */)
21522 (Lisp_Object format, Lisp_Object face,
21523 Lisp_Object window, Lisp_Object buffer)
21525 struct it it;
21526 int len;
21527 struct window *w;
21528 struct buffer *old_buffer = NULL;
21529 int face_id;
21530 int no_props = INTEGERP (face);
21531 ptrdiff_t count = SPECPDL_INDEX ();
21532 Lisp_Object str;
21533 int string_start = 0;
21535 w = decode_any_window (window);
21536 XSETWINDOW (window, w);
21538 if (NILP (buffer))
21539 buffer = w->contents;
21540 CHECK_BUFFER (buffer);
21542 /* Make formatting the modeline a non-op when noninteractive, otherwise
21543 there will be problems later caused by a partially initialized frame. */
21544 if (NILP (format) || noninteractive)
21545 return empty_unibyte_string;
21547 if (no_props)
21548 face = Qnil;
21550 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21551 : EQ (face, Qt) ? (EQ (window, selected_window)
21552 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21553 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21554 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21555 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21556 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21557 : DEFAULT_FACE_ID;
21559 old_buffer = current_buffer;
21561 /* Save things including mode_line_proptrans_alist,
21562 and set that to nil so that we don't alter the outer value. */
21563 record_unwind_protect (unwind_format_mode_line,
21564 format_mode_line_unwind_data
21565 (XFRAME (WINDOW_FRAME (w)),
21566 old_buffer, selected_window, 1));
21567 mode_line_proptrans_alist = Qnil;
21569 Fselect_window (window, Qt);
21570 set_buffer_internal_1 (XBUFFER (buffer));
21572 init_iterator (&it, w, -1, -1, NULL, face_id);
21574 if (no_props)
21576 mode_line_target = MODE_LINE_NOPROP;
21577 mode_line_string_face_prop = Qnil;
21578 mode_line_string_list = Qnil;
21579 string_start = MODE_LINE_NOPROP_LEN (0);
21581 else
21583 mode_line_target = MODE_LINE_STRING;
21584 mode_line_string_list = Qnil;
21585 mode_line_string_face = face;
21586 mode_line_string_face_prop
21587 = NILP (face) ? Qnil : list2 (Qface, face);
21590 push_kboard (FRAME_KBOARD (it.f));
21591 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21592 pop_kboard ();
21594 if (no_props)
21596 len = MODE_LINE_NOPROP_LEN (string_start);
21597 str = make_string (mode_line_noprop_buf + string_start, len);
21599 else
21601 mode_line_string_list = Fnreverse (mode_line_string_list);
21602 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21603 empty_unibyte_string);
21606 unbind_to (count, Qnil);
21607 return str;
21610 /* Write a null-terminated, right justified decimal representation of
21611 the positive integer D to BUF using a minimal field width WIDTH. */
21613 static void
21614 pint2str (register char *buf, register int width, register ptrdiff_t d)
21616 register char *p = buf;
21618 if (d <= 0)
21619 *p++ = '0';
21620 else
21622 while (d > 0)
21624 *p++ = d % 10 + '0';
21625 d /= 10;
21629 for (width -= (int) (p - buf); width > 0; --width)
21630 *p++ = ' ';
21631 *p-- = '\0';
21632 while (p > buf)
21634 d = *buf;
21635 *buf++ = *p;
21636 *p-- = d;
21640 /* Write a null-terminated, right justified decimal and "human
21641 readable" representation of the nonnegative integer D to BUF using
21642 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21644 static const char power_letter[] =
21646 0, /* no letter */
21647 'k', /* kilo */
21648 'M', /* mega */
21649 'G', /* giga */
21650 'T', /* tera */
21651 'P', /* peta */
21652 'E', /* exa */
21653 'Z', /* zetta */
21654 'Y' /* yotta */
21657 static void
21658 pint2hrstr (char *buf, int width, ptrdiff_t d)
21660 /* We aim to represent the nonnegative integer D as
21661 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21662 ptrdiff_t quotient = d;
21663 int remainder = 0;
21664 /* -1 means: do not use TENTHS. */
21665 int tenths = -1;
21666 int exponent = 0;
21668 /* Length of QUOTIENT.TENTHS as a string. */
21669 int length;
21671 char * psuffix;
21672 char * p;
21674 if (quotient >= 1000)
21676 /* Scale to the appropriate EXPONENT. */
21679 remainder = quotient % 1000;
21680 quotient /= 1000;
21681 exponent++;
21683 while (quotient >= 1000);
21685 /* Round to nearest and decide whether to use TENTHS or not. */
21686 if (quotient <= 9)
21688 tenths = remainder / 100;
21689 if (remainder % 100 >= 50)
21691 if (tenths < 9)
21692 tenths++;
21693 else
21695 quotient++;
21696 if (quotient == 10)
21697 tenths = -1;
21698 else
21699 tenths = 0;
21703 else
21704 if (remainder >= 500)
21706 if (quotient < 999)
21707 quotient++;
21708 else
21710 quotient = 1;
21711 exponent++;
21712 tenths = 0;
21717 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21718 if (tenths == -1 && quotient <= 99)
21719 if (quotient <= 9)
21720 length = 1;
21721 else
21722 length = 2;
21723 else
21724 length = 3;
21725 p = psuffix = buf + max (width, length);
21727 /* Print EXPONENT. */
21728 *psuffix++ = power_letter[exponent];
21729 *psuffix = '\0';
21731 /* Print TENTHS. */
21732 if (tenths >= 0)
21734 *--p = '0' + tenths;
21735 *--p = '.';
21738 /* Print QUOTIENT. */
21741 int digit = quotient % 10;
21742 *--p = '0' + digit;
21744 while ((quotient /= 10) != 0);
21746 /* Print leading spaces. */
21747 while (buf < p)
21748 *--p = ' ';
21751 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21752 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21753 type of CODING_SYSTEM. Return updated pointer into BUF. */
21755 static unsigned char invalid_eol_type[] = "(*invalid*)";
21757 static char *
21758 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21760 Lisp_Object val;
21761 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21762 const unsigned char *eol_str;
21763 int eol_str_len;
21764 /* The EOL conversion we are using. */
21765 Lisp_Object eoltype;
21767 val = CODING_SYSTEM_SPEC (coding_system);
21768 eoltype = Qnil;
21770 if (!VECTORP (val)) /* Not yet decided. */
21772 *buf++ = multibyte ? '-' : ' ';
21773 if (eol_flag)
21774 eoltype = eol_mnemonic_undecided;
21775 /* Don't mention EOL conversion if it isn't decided. */
21777 else
21779 Lisp_Object attrs;
21780 Lisp_Object eolvalue;
21782 attrs = AREF (val, 0);
21783 eolvalue = AREF (val, 2);
21785 *buf++ = multibyte
21786 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21787 : ' ';
21789 if (eol_flag)
21791 /* The EOL conversion that is normal on this system. */
21793 if (NILP (eolvalue)) /* Not yet decided. */
21794 eoltype = eol_mnemonic_undecided;
21795 else if (VECTORP (eolvalue)) /* Not yet decided. */
21796 eoltype = eol_mnemonic_undecided;
21797 else /* eolvalue is Qunix, Qdos, or Qmac. */
21798 eoltype = (EQ (eolvalue, Qunix)
21799 ? eol_mnemonic_unix
21800 : (EQ (eolvalue, Qdos) == 1
21801 ? eol_mnemonic_dos : eol_mnemonic_mac));
21805 if (eol_flag)
21807 /* Mention the EOL conversion if it is not the usual one. */
21808 if (STRINGP (eoltype))
21810 eol_str = SDATA (eoltype);
21811 eol_str_len = SBYTES (eoltype);
21813 else if (CHARACTERP (eoltype))
21815 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21816 int c = XFASTINT (eoltype);
21817 eol_str_len = CHAR_STRING (c, tmp);
21818 eol_str = tmp;
21820 else
21822 eol_str = invalid_eol_type;
21823 eol_str_len = sizeof (invalid_eol_type) - 1;
21825 memcpy (buf, eol_str, eol_str_len);
21826 buf += eol_str_len;
21829 return buf;
21832 /* Return a string for the output of a mode line %-spec for window W,
21833 generated by character C. FIELD_WIDTH > 0 means pad the string
21834 returned with spaces to that value. Return a Lisp string in
21835 *STRING if the resulting string is taken from that Lisp string.
21837 Note we operate on the current buffer for most purposes. */
21839 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21841 static const char *
21842 decode_mode_spec (struct window *w, register int c, int field_width,
21843 Lisp_Object *string)
21845 Lisp_Object obj;
21846 struct frame *f = XFRAME (WINDOW_FRAME (w));
21847 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21848 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21849 produce strings from numerical values, so limit preposterously
21850 large values of FIELD_WIDTH to avoid overrunning the buffer's
21851 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21852 bytes plus the terminating null. */
21853 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21854 struct buffer *b = current_buffer;
21856 obj = Qnil;
21857 *string = Qnil;
21859 switch (c)
21861 case '*':
21862 if (!NILP (BVAR (b, read_only)))
21863 return "%";
21864 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21865 return "*";
21866 return "-";
21868 case '+':
21869 /* This differs from %* only for a modified read-only buffer. */
21870 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21871 return "*";
21872 if (!NILP (BVAR (b, read_only)))
21873 return "%";
21874 return "-";
21876 case '&':
21877 /* This differs from %* in ignoring read-only-ness. */
21878 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21879 return "*";
21880 return "-";
21882 case '%':
21883 return "%";
21885 case '[':
21887 int i;
21888 char *p;
21890 if (command_loop_level > 5)
21891 return "[[[... ";
21892 p = decode_mode_spec_buf;
21893 for (i = 0; i < command_loop_level; i++)
21894 *p++ = '[';
21895 *p = 0;
21896 return decode_mode_spec_buf;
21899 case ']':
21901 int i;
21902 char *p;
21904 if (command_loop_level > 5)
21905 return " ...]]]";
21906 p = decode_mode_spec_buf;
21907 for (i = 0; i < command_loop_level; i++)
21908 *p++ = ']';
21909 *p = 0;
21910 return decode_mode_spec_buf;
21913 case '-':
21915 register int i;
21917 /* Let lots_of_dashes be a string of infinite length. */
21918 if (mode_line_target == MODE_LINE_NOPROP
21919 || mode_line_target == MODE_LINE_STRING)
21920 return "--";
21921 if (field_width <= 0
21922 || field_width > sizeof (lots_of_dashes))
21924 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21925 decode_mode_spec_buf[i] = '-';
21926 decode_mode_spec_buf[i] = '\0';
21927 return decode_mode_spec_buf;
21929 else
21930 return lots_of_dashes;
21933 case 'b':
21934 obj = BVAR (b, name);
21935 break;
21937 case 'c':
21938 /* %c and %l are ignored in `frame-title-format'.
21939 (In redisplay_internal, the frame title is drawn _before_ the
21940 windows are updated, so the stuff which depends on actual
21941 window contents (such as %l) may fail to render properly, or
21942 even crash emacs.) */
21943 if (mode_line_target == MODE_LINE_TITLE)
21944 return "";
21945 else
21947 ptrdiff_t col = current_column ();
21948 w->column_number_displayed = col;
21949 pint2str (decode_mode_spec_buf, width, col);
21950 return decode_mode_spec_buf;
21953 case 'e':
21954 #ifndef SYSTEM_MALLOC
21956 if (NILP (Vmemory_full))
21957 return "";
21958 else
21959 return "!MEM FULL! ";
21961 #else
21962 return "";
21963 #endif
21965 case 'F':
21966 /* %F displays the frame name. */
21967 if (!NILP (f->title))
21968 return SSDATA (f->title);
21969 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21970 return SSDATA (f->name);
21971 return "Emacs";
21973 case 'f':
21974 obj = BVAR (b, filename);
21975 break;
21977 case 'i':
21979 ptrdiff_t size = ZV - BEGV;
21980 pint2str (decode_mode_spec_buf, width, size);
21981 return decode_mode_spec_buf;
21984 case 'I':
21986 ptrdiff_t size = ZV - BEGV;
21987 pint2hrstr (decode_mode_spec_buf, width, size);
21988 return decode_mode_spec_buf;
21991 case 'l':
21993 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21994 ptrdiff_t topline, nlines, height;
21995 ptrdiff_t junk;
21997 /* %c and %l are ignored in `frame-title-format'. */
21998 if (mode_line_target == MODE_LINE_TITLE)
21999 return "";
22001 startpos = marker_position (w->start);
22002 startpos_byte = marker_byte_position (w->start);
22003 height = WINDOW_TOTAL_LINES (w);
22005 /* If we decided that this buffer isn't suitable for line numbers,
22006 don't forget that too fast. */
22007 if (w->base_line_pos == -1)
22008 goto no_value;
22010 /* If the buffer is very big, don't waste time. */
22011 if (INTEGERP (Vline_number_display_limit)
22012 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22014 w->base_line_pos = 0;
22015 w->base_line_number = 0;
22016 goto no_value;
22019 if (w->base_line_number > 0
22020 && w->base_line_pos > 0
22021 && w->base_line_pos <= startpos)
22023 line = w->base_line_number;
22024 linepos = w->base_line_pos;
22025 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22027 else
22029 line = 1;
22030 linepos = BUF_BEGV (b);
22031 linepos_byte = BUF_BEGV_BYTE (b);
22034 /* Count lines from base line to window start position. */
22035 nlines = display_count_lines (linepos_byte,
22036 startpos_byte,
22037 startpos, &junk);
22039 topline = nlines + line;
22041 /* Determine a new base line, if the old one is too close
22042 or too far away, or if we did not have one.
22043 "Too close" means it's plausible a scroll-down would
22044 go back past it. */
22045 if (startpos == BUF_BEGV (b))
22047 w->base_line_number = topline;
22048 w->base_line_pos = BUF_BEGV (b);
22050 else if (nlines < height + 25 || nlines > height * 3 + 50
22051 || linepos == BUF_BEGV (b))
22053 ptrdiff_t limit = BUF_BEGV (b);
22054 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22055 ptrdiff_t position;
22056 ptrdiff_t distance =
22057 (height * 2 + 30) * line_number_display_limit_width;
22059 if (startpos - distance > limit)
22061 limit = startpos - distance;
22062 limit_byte = CHAR_TO_BYTE (limit);
22065 nlines = display_count_lines (startpos_byte,
22066 limit_byte,
22067 - (height * 2 + 30),
22068 &position);
22069 /* If we couldn't find the lines we wanted within
22070 line_number_display_limit_width chars per line,
22071 give up on line numbers for this window. */
22072 if (position == limit_byte && limit == startpos - distance)
22074 w->base_line_pos = -1;
22075 w->base_line_number = 0;
22076 goto no_value;
22079 w->base_line_number = topline - nlines;
22080 w->base_line_pos = BYTE_TO_CHAR (position);
22083 /* Now count lines from the start pos to point. */
22084 nlines = display_count_lines (startpos_byte,
22085 PT_BYTE, PT, &junk);
22087 /* Record that we did display the line number. */
22088 line_number_displayed = 1;
22090 /* Make the string to show. */
22091 pint2str (decode_mode_spec_buf, width, topline + nlines);
22092 return decode_mode_spec_buf;
22093 no_value:
22095 char* p = decode_mode_spec_buf;
22096 int pad = width - 2;
22097 while (pad-- > 0)
22098 *p++ = ' ';
22099 *p++ = '?';
22100 *p++ = '?';
22101 *p = '\0';
22102 return decode_mode_spec_buf;
22105 break;
22107 case 'm':
22108 obj = BVAR (b, mode_name);
22109 break;
22111 case 'n':
22112 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22113 return " Narrow";
22114 break;
22116 case 'p':
22118 ptrdiff_t pos = marker_position (w->start);
22119 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22121 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22123 if (pos <= BUF_BEGV (b))
22124 return "All";
22125 else
22126 return "Bottom";
22128 else if (pos <= BUF_BEGV (b))
22129 return "Top";
22130 else
22132 if (total > 1000000)
22133 /* Do it differently for a large value, to avoid overflow. */
22134 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22135 else
22136 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22137 /* We can't normally display a 3-digit number,
22138 so get us a 2-digit number that is close. */
22139 if (total == 100)
22140 total = 99;
22141 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22142 return decode_mode_spec_buf;
22146 /* Display percentage of size above the bottom of the screen. */
22147 case 'P':
22149 ptrdiff_t toppos = marker_position (w->start);
22150 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22151 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22153 if (botpos >= BUF_ZV (b))
22155 if (toppos <= BUF_BEGV (b))
22156 return "All";
22157 else
22158 return "Bottom";
22160 else
22162 if (total > 1000000)
22163 /* Do it differently for a large value, to avoid overflow. */
22164 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22165 else
22166 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22167 /* We can't normally display a 3-digit number,
22168 so get us a 2-digit number that is close. */
22169 if (total == 100)
22170 total = 99;
22171 if (toppos <= BUF_BEGV (b))
22172 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22173 else
22174 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22175 return decode_mode_spec_buf;
22179 case 's':
22180 /* status of process */
22181 obj = Fget_buffer_process (Fcurrent_buffer ());
22182 if (NILP (obj))
22183 return "no process";
22184 #ifndef MSDOS
22185 obj = Fsymbol_name (Fprocess_status (obj));
22186 #endif
22187 break;
22189 case '@':
22191 ptrdiff_t count = inhibit_garbage_collection ();
22192 Lisp_Object val = call1 (intern ("file-remote-p"),
22193 BVAR (current_buffer, directory));
22194 unbind_to (count, Qnil);
22196 if (NILP (val))
22197 return "-";
22198 else
22199 return "@";
22202 case 'z':
22203 /* coding-system (not including end-of-line format) */
22204 case 'Z':
22205 /* coding-system (including end-of-line type) */
22207 int eol_flag = (c == 'Z');
22208 char *p = decode_mode_spec_buf;
22210 if (! FRAME_WINDOW_P (f))
22212 /* No need to mention EOL here--the terminal never needs
22213 to do EOL conversion. */
22214 p = decode_mode_spec_coding (CODING_ID_NAME
22215 (FRAME_KEYBOARD_CODING (f)->id),
22216 p, 0);
22217 p = decode_mode_spec_coding (CODING_ID_NAME
22218 (FRAME_TERMINAL_CODING (f)->id),
22219 p, 0);
22221 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22222 p, eol_flag);
22224 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22225 #ifdef subprocesses
22226 obj = Fget_buffer_process (Fcurrent_buffer ());
22227 if (PROCESSP (obj))
22229 p = decode_mode_spec_coding
22230 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22231 p = decode_mode_spec_coding
22232 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22234 #endif /* subprocesses */
22235 #endif /* 0 */
22236 *p = 0;
22237 return decode_mode_spec_buf;
22241 if (STRINGP (obj))
22243 *string = obj;
22244 return SSDATA (obj);
22246 else
22247 return "";
22251 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22252 means count lines back from START_BYTE. But don't go beyond
22253 LIMIT_BYTE. Return the number of lines thus found (always
22254 nonnegative).
22256 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22257 either the position COUNT lines after/before START_BYTE, if we
22258 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22259 COUNT lines. */
22261 static ptrdiff_t
22262 display_count_lines (ptrdiff_t start_byte,
22263 ptrdiff_t limit_byte, ptrdiff_t count,
22264 ptrdiff_t *byte_pos_ptr)
22266 register unsigned char *cursor;
22267 unsigned char *base;
22269 register ptrdiff_t ceiling;
22270 register unsigned char *ceiling_addr;
22271 ptrdiff_t orig_count = count;
22273 /* If we are not in selective display mode,
22274 check only for newlines. */
22275 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22276 && !INTEGERP (BVAR (current_buffer, selective_display)));
22278 if (count > 0)
22280 while (start_byte < limit_byte)
22282 ceiling = BUFFER_CEILING_OF (start_byte);
22283 ceiling = min (limit_byte - 1, ceiling);
22284 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22285 base = (cursor = BYTE_POS_ADDR (start_byte));
22289 if (selective_display)
22291 while (*cursor != '\n' && *cursor != 015
22292 && ++cursor != ceiling_addr)
22293 continue;
22294 if (cursor == ceiling_addr)
22295 break;
22297 else
22299 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22300 if (! cursor)
22301 break;
22304 cursor++;
22306 if (--count == 0)
22308 start_byte += cursor - base;
22309 *byte_pos_ptr = start_byte;
22310 return orig_count;
22313 while (cursor < ceiling_addr);
22315 start_byte += ceiling_addr - base;
22318 else
22320 while (start_byte > limit_byte)
22322 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22323 ceiling = max (limit_byte, ceiling);
22324 ceiling_addr = BYTE_POS_ADDR (ceiling);
22325 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22326 while (1)
22328 if (selective_display)
22330 while (--cursor >= ceiling_addr
22331 && *cursor != '\n' && *cursor != 015)
22332 continue;
22333 if (cursor < ceiling_addr)
22334 break;
22336 else
22338 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22339 if (! cursor)
22340 break;
22343 if (++count == 0)
22345 start_byte += cursor - base + 1;
22346 *byte_pos_ptr = start_byte;
22347 /* When scanning backwards, we should
22348 not count the newline posterior to which we stop. */
22349 return - orig_count - 1;
22352 start_byte += ceiling_addr - base;
22356 *byte_pos_ptr = limit_byte;
22358 if (count < 0)
22359 return - orig_count + count;
22360 return orig_count - count;
22366 /***********************************************************************
22367 Displaying strings
22368 ***********************************************************************/
22370 /* Display a NUL-terminated string, starting with index START.
22372 If STRING is non-null, display that C string. Otherwise, the Lisp
22373 string LISP_STRING is displayed. There's a case that STRING is
22374 non-null and LISP_STRING is not nil. It means STRING is a string
22375 data of LISP_STRING. In that case, we display LISP_STRING while
22376 ignoring its text properties.
22378 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22379 FACE_STRING. Display STRING or LISP_STRING with the face at
22380 FACE_STRING_POS in FACE_STRING:
22382 Display the string in the environment given by IT, but use the
22383 standard display table, temporarily.
22385 FIELD_WIDTH is the minimum number of output glyphs to produce.
22386 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22387 with spaces. If STRING has more characters, more than FIELD_WIDTH
22388 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22390 PRECISION is the maximum number of characters to output from
22391 STRING. PRECISION < 0 means don't truncate the string.
22393 This is roughly equivalent to printf format specifiers:
22395 FIELD_WIDTH PRECISION PRINTF
22396 ----------------------------------------
22397 -1 -1 %s
22398 -1 10 %.10s
22399 10 -1 %10s
22400 20 10 %20.10s
22402 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22403 display them, and < 0 means obey the current buffer's value of
22404 enable_multibyte_characters.
22406 Value is the number of columns displayed. */
22408 static int
22409 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22410 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22411 int field_width, int precision, int max_x, int multibyte)
22413 int hpos_at_start = it->hpos;
22414 int saved_face_id = it->face_id;
22415 struct glyph_row *row = it->glyph_row;
22416 ptrdiff_t it_charpos;
22418 /* Initialize the iterator IT for iteration over STRING beginning
22419 with index START. */
22420 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22421 precision, field_width, multibyte);
22422 if (string && STRINGP (lisp_string))
22423 /* LISP_STRING is the one returned by decode_mode_spec. We should
22424 ignore its text properties. */
22425 it->stop_charpos = it->end_charpos;
22427 /* If displaying STRING, set up the face of the iterator from
22428 FACE_STRING, if that's given. */
22429 if (STRINGP (face_string))
22431 ptrdiff_t endptr;
22432 struct face *face;
22434 it->face_id
22435 = face_at_string_position (it->w, face_string, face_string_pos,
22436 0, &endptr, it->base_face_id, 0);
22437 face = FACE_FROM_ID (it->f, it->face_id);
22438 it->face_box_p = face->box != FACE_NO_BOX;
22441 /* Set max_x to the maximum allowed X position. Don't let it go
22442 beyond the right edge of the window. */
22443 if (max_x <= 0)
22444 max_x = it->last_visible_x;
22445 else
22446 max_x = min (max_x, it->last_visible_x);
22448 /* Skip over display elements that are not visible. because IT->w is
22449 hscrolled. */
22450 if (it->current_x < it->first_visible_x)
22451 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22452 MOVE_TO_POS | MOVE_TO_X);
22454 row->ascent = it->max_ascent;
22455 row->height = it->max_ascent + it->max_descent;
22456 row->phys_ascent = it->max_phys_ascent;
22457 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22458 row->extra_line_spacing = it->max_extra_line_spacing;
22460 if (STRINGP (it->string))
22461 it_charpos = IT_STRING_CHARPOS (*it);
22462 else
22463 it_charpos = IT_CHARPOS (*it);
22465 /* This condition is for the case that we are called with current_x
22466 past last_visible_x. */
22467 while (it->current_x < max_x)
22469 int x_before, x, n_glyphs_before, i, nglyphs;
22471 /* Get the next display element. */
22472 if (!get_next_display_element (it))
22473 break;
22475 /* Produce glyphs. */
22476 x_before = it->current_x;
22477 n_glyphs_before = row->used[TEXT_AREA];
22478 PRODUCE_GLYPHS (it);
22480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22481 i = 0;
22482 x = x_before;
22483 while (i < nglyphs)
22485 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22487 if (it->line_wrap != TRUNCATE
22488 && x + glyph->pixel_width > max_x)
22490 /* End of continued line or max_x reached. */
22491 if (CHAR_GLYPH_PADDING_P (*glyph))
22493 /* A wide character is unbreakable. */
22494 if (row->reversed_p)
22495 unproduce_glyphs (it, row->used[TEXT_AREA]
22496 - n_glyphs_before);
22497 row->used[TEXT_AREA] = n_glyphs_before;
22498 it->current_x = x_before;
22500 else
22502 if (row->reversed_p)
22503 unproduce_glyphs (it, row->used[TEXT_AREA]
22504 - (n_glyphs_before + i));
22505 row->used[TEXT_AREA] = n_glyphs_before + i;
22506 it->current_x = x;
22508 break;
22510 else if (x + glyph->pixel_width >= it->first_visible_x)
22512 /* Glyph is at least partially visible. */
22513 ++it->hpos;
22514 if (x < it->first_visible_x)
22515 row->x = x - it->first_visible_x;
22517 else
22519 /* Glyph is off the left margin of the display area.
22520 Should not happen. */
22521 emacs_abort ();
22524 row->ascent = max (row->ascent, it->max_ascent);
22525 row->height = max (row->height, it->max_ascent + it->max_descent);
22526 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22527 row->phys_height = max (row->phys_height,
22528 it->max_phys_ascent + it->max_phys_descent);
22529 row->extra_line_spacing = max (row->extra_line_spacing,
22530 it->max_extra_line_spacing);
22531 x += glyph->pixel_width;
22532 ++i;
22535 /* Stop if max_x reached. */
22536 if (i < nglyphs)
22537 break;
22539 /* Stop at line ends. */
22540 if (ITERATOR_AT_END_OF_LINE_P (it))
22542 it->continuation_lines_width = 0;
22543 break;
22546 set_iterator_to_next (it, 1);
22547 if (STRINGP (it->string))
22548 it_charpos = IT_STRING_CHARPOS (*it);
22549 else
22550 it_charpos = IT_CHARPOS (*it);
22552 /* Stop if truncating at the right edge. */
22553 if (it->line_wrap == TRUNCATE
22554 && it->current_x >= it->last_visible_x)
22556 /* Add truncation mark, but don't do it if the line is
22557 truncated at a padding space. */
22558 if (it_charpos < it->string_nchars)
22560 if (!FRAME_WINDOW_P (it->f))
22562 int ii, n;
22564 if (it->current_x > it->last_visible_x)
22566 if (!row->reversed_p)
22568 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22569 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22570 break;
22572 else
22574 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22575 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22576 break;
22577 unproduce_glyphs (it, ii + 1);
22578 ii = row->used[TEXT_AREA] - (ii + 1);
22580 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22582 row->used[TEXT_AREA] = ii;
22583 produce_special_glyphs (it, IT_TRUNCATION);
22586 produce_special_glyphs (it, IT_TRUNCATION);
22588 row->truncated_on_right_p = 1;
22590 break;
22594 /* Maybe insert a truncation at the left. */
22595 if (it->first_visible_x
22596 && it_charpos > 0)
22598 if (!FRAME_WINDOW_P (it->f)
22599 || (row->reversed_p
22600 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22601 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22602 insert_left_trunc_glyphs (it);
22603 row->truncated_on_left_p = 1;
22606 it->face_id = saved_face_id;
22608 /* Value is number of columns displayed. */
22609 return it->hpos - hpos_at_start;
22614 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22615 appears as an element of LIST or as the car of an element of LIST.
22616 If PROPVAL is a list, compare each element against LIST in that
22617 way, and return 1/2 if any element of PROPVAL is found in LIST.
22618 Otherwise return 0. This function cannot quit.
22619 The return value is 2 if the text is invisible but with an ellipsis
22620 and 1 if it's invisible and without an ellipsis. */
22623 invisible_p (register Lisp_Object propval, Lisp_Object list)
22625 register Lisp_Object tail, proptail;
22627 for (tail = list; CONSP (tail); tail = XCDR (tail))
22629 register Lisp_Object tem;
22630 tem = XCAR (tail);
22631 if (EQ (propval, tem))
22632 return 1;
22633 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22634 return NILP (XCDR (tem)) ? 1 : 2;
22637 if (CONSP (propval))
22639 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22641 Lisp_Object propelt;
22642 propelt = XCAR (proptail);
22643 for (tail = list; CONSP (tail); tail = XCDR (tail))
22645 register Lisp_Object tem;
22646 tem = XCAR (tail);
22647 if (EQ (propelt, tem))
22648 return 1;
22649 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22650 return NILP (XCDR (tem)) ? 1 : 2;
22655 return 0;
22658 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22659 doc: /* Non-nil if the property makes the text invisible.
22660 POS-OR-PROP can be a marker or number, in which case it is taken to be
22661 a position in the current buffer and the value of the `invisible' property
22662 is checked; or it can be some other value, which is then presumed to be the
22663 value of the `invisible' property of the text of interest.
22664 The non-nil value returned can be t for truly invisible text or something
22665 else if the text is replaced by an ellipsis. */)
22666 (Lisp_Object pos_or_prop)
22668 Lisp_Object prop
22669 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22670 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22671 : pos_or_prop);
22672 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22673 return (invis == 0 ? Qnil
22674 : invis == 1 ? Qt
22675 : make_number (invis));
22678 /* Calculate a width or height in pixels from a specification using
22679 the following elements:
22681 SPEC ::=
22682 NUM - a (fractional) multiple of the default font width/height
22683 (NUM) - specifies exactly NUM pixels
22684 UNIT - a fixed number of pixels, see below.
22685 ELEMENT - size of a display element in pixels, see below.
22686 (NUM . SPEC) - equals NUM * SPEC
22687 (+ SPEC SPEC ...) - add pixel values
22688 (- SPEC SPEC ...) - subtract pixel values
22689 (- SPEC) - negate pixel value
22691 NUM ::=
22692 INT or FLOAT - a number constant
22693 SYMBOL - use symbol's (buffer local) variable binding.
22695 UNIT ::=
22696 in - pixels per inch *)
22697 mm - pixels per 1/1000 meter *)
22698 cm - pixels per 1/100 meter *)
22699 width - width of current font in pixels.
22700 height - height of current font in pixels.
22702 *) using the ratio(s) defined in display-pixels-per-inch.
22704 ELEMENT ::=
22706 left-fringe - left fringe width in pixels
22707 right-fringe - right fringe width in pixels
22709 left-margin - left margin width in pixels
22710 right-margin - right margin width in pixels
22712 scroll-bar - scroll-bar area width in pixels
22714 Examples:
22716 Pixels corresponding to 5 inches:
22717 (5 . in)
22719 Total width of non-text areas on left side of window (if scroll-bar is on left):
22720 '(space :width (+ left-fringe left-margin scroll-bar))
22722 Align to first text column (in header line):
22723 '(space :align-to 0)
22725 Align to middle of text area minus half the width of variable `my-image'
22726 containing a loaded image:
22727 '(space :align-to (0.5 . (- text my-image)))
22729 Width of left margin minus width of 1 character in the default font:
22730 '(space :width (- left-margin 1))
22732 Width of left margin minus width of 2 characters in the current font:
22733 '(space :width (- left-margin (2 . width)))
22735 Center 1 character over left-margin (in header line):
22736 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22738 Different ways to express width of left fringe plus left margin minus one pixel:
22739 '(space :width (- (+ left-fringe left-margin) (1)))
22740 '(space :width (+ left-fringe left-margin (- (1))))
22741 '(space :width (+ left-fringe left-margin (-1)))
22745 static int
22746 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22747 struct font *font, int width_p, int *align_to)
22749 double pixels;
22751 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22752 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22754 if (NILP (prop))
22755 return OK_PIXELS (0);
22757 eassert (FRAME_LIVE_P (it->f));
22759 if (SYMBOLP (prop))
22761 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22763 char *unit = SSDATA (SYMBOL_NAME (prop));
22765 if (unit[0] == 'i' && unit[1] == 'n')
22766 pixels = 1.0;
22767 else if (unit[0] == 'm' && unit[1] == 'm')
22768 pixels = 25.4;
22769 else if (unit[0] == 'c' && unit[1] == 'm')
22770 pixels = 2.54;
22771 else
22772 pixels = 0;
22773 if (pixels > 0)
22775 double ppi = (width_p ? FRAME_RES_X (it->f)
22776 : FRAME_RES_Y (it->f));
22778 if (ppi > 0)
22779 return OK_PIXELS (ppi / pixels);
22780 return 0;
22784 #ifdef HAVE_WINDOW_SYSTEM
22785 if (EQ (prop, Qheight))
22786 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22787 if (EQ (prop, Qwidth))
22788 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22789 #else
22790 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22791 return OK_PIXELS (1);
22792 #endif
22794 if (EQ (prop, Qtext))
22795 return OK_PIXELS (width_p
22796 ? window_box_width (it->w, TEXT_AREA)
22797 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22799 if (align_to && *align_to < 0)
22801 *res = 0;
22802 if (EQ (prop, Qleft))
22803 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22804 if (EQ (prop, Qright))
22805 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22806 if (EQ (prop, Qcenter))
22807 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22808 + window_box_width (it->w, TEXT_AREA) / 2);
22809 if (EQ (prop, Qleft_fringe))
22810 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22811 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22812 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22813 if (EQ (prop, Qright_fringe))
22814 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22815 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22816 : window_box_right_offset (it->w, TEXT_AREA));
22817 if (EQ (prop, Qleft_margin))
22818 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22819 if (EQ (prop, Qright_margin))
22820 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22821 if (EQ (prop, Qscroll_bar))
22822 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22824 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22825 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22826 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22827 : 0)));
22829 else
22831 if (EQ (prop, Qleft_fringe))
22832 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22833 if (EQ (prop, Qright_fringe))
22834 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22835 if (EQ (prop, Qleft_margin))
22836 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22837 if (EQ (prop, Qright_margin))
22838 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22839 if (EQ (prop, Qscroll_bar))
22840 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22843 prop = buffer_local_value_1 (prop, it->w->contents);
22844 if (EQ (prop, Qunbound))
22845 prop = Qnil;
22848 if (INTEGERP (prop) || FLOATP (prop))
22850 int base_unit = (width_p
22851 ? FRAME_COLUMN_WIDTH (it->f)
22852 : FRAME_LINE_HEIGHT (it->f));
22853 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22856 if (CONSP (prop))
22858 Lisp_Object car = XCAR (prop);
22859 Lisp_Object cdr = XCDR (prop);
22861 if (SYMBOLP (car))
22863 #ifdef HAVE_WINDOW_SYSTEM
22864 if (FRAME_WINDOW_P (it->f)
22865 && valid_image_p (prop))
22867 ptrdiff_t id = lookup_image (it->f, prop);
22868 struct image *img = IMAGE_FROM_ID (it->f, id);
22870 return OK_PIXELS (width_p ? img->width : img->height);
22872 #endif
22873 if (EQ (car, Qplus) || EQ (car, Qminus))
22875 int first = 1;
22876 double px;
22878 pixels = 0;
22879 while (CONSP (cdr))
22881 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22882 font, width_p, align_to))
22883 return 0;
22884 if (first)
22885 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22886 else
22887 pixels += px;
22888 cdr = XCDR (cdr);
22890 if (EQ (car, Qminus))
22891 pixels = -pixels;
22892 return OK_PIXELS (pixels);
22895 car = buffer_local_value_1 (car, it->w->contents);
22896 if (EQ (car, Qunbound))
22897 car = Qnil;
22900 if (INTEGERP (car) || FLOATP (car))
22902 double fact;
22903 pixels = XFLOATINT (car);
22904 if (NILP (cdr))
22905 return OK_PIXELS (pixels);
22906 if (calc_pixel_width_or_height (&fact, it, cdr,
22907 font, width_p, align_to))
22908 return OK_PIXELS (pixels * fact);
22909 return 0;
22912 return 0;
22915 return 0;
22919 /***********************************************************************
22920 Glyph Display
22921 ***********************************************************************/
22923 #ifdef HAVE_WINDOW_SYSTEM
22925 #ifdef GLYPH_DEBUG
22927 void
22928 dump_glyph_string (struct glyph_string *s)
22930 fprintf (stderr, "glyph string\n");
22931 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22932 s->x, s->y, s->width, s->height);
22933 fprintf (stderr, " ybase = %d\n", s->ybase);
22934 fprintf (stderr, " hl = %d\n", s->hl);
22935 fprintf (stderr, " left overhang = %d, right = %d\n",
22936 s->left_overhang, s->right_overhang);
22937 fprintf (stderr, " nchars = %d\n", s->nchars);
22938 fprintf (stderr, " extends to end of line = %d\n",
22939 s->extends_to_end_of_line_p);
22940 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22941 fprintf (stderr, " bg width = %d\n", s->background_width);
22944 #endif /* GLYPH_DEBUG */
22946 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22947 of XChar2b structures for S; it can't be allocated in
22948 init_glyph_string because it must be allocated via `alloca'. W
22949 is the window on which S is drawn. ROW and AREA are the glyph row
22950 and area within the row from which S is constructed. START is the
22951 index of the first glyph structure covered by S. HL is a
22952 face-override for drawing S. */
22954 #ifdef HAVE_NTGUI
22955 #define OPTIONAL_HDC(hdc) HDC hdc,
22956 #define DECLARE_HDC(hdc) HDC hdc;
22957 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22958 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22959 #endif
22961 #ifndef OPTIONAL_HDC
22962 #define OPTIONAL_HDC(hdc)
22963 #define DECLARE_HDC(hdc)
22964 #define ALLOCATE_HDC(hdc, f)
22965 #define RELEASE_HDC(hdc, f)
22966 #endif
22968 static void
22969 init_glyph_string (struct glyph_string *s,
22970 OPTIONAL_HDC (hdc)
22971 XChar2b *char2b, struct window *w, struct glyph_row *row,
22972 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22974 memset (s, 0, sizeof *s);
22975 s->w = w;
22976 s->f = XFRAME (w->frame);
22977 #ifdef HAVE_NTGUI
22978 s->hdc = hdc;
22979 #endif
22980 s->display = FRAME_X_DISPLAY (s->f);
22981 s->window = FRAME_X_WINDOW (s->f);
22982 s->char2b = char2b;
22983 s->hl = hl;
22984 s->row = row;
22985 s->area = area;
22986 s->first_glyph = row->glyphs[area] + start;
22987 s->height = row->height;
22988 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22989 s->ybase = s->y + row->ascent;
22993 /* Append the list of glyph strings with head H and tail T to the list
22994 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22996 static void
22997 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22998 struct glyph_string *h, struct glyph_string *t)
23000 if (h)
23002 if (*head)
23003 (*tail)->next = h;
23004 else
23005 *head = h;
23006 h->prev = *tail;
23007 *tail = t;
23012 /* Prepend the list of glyph strings with head H and tail T to the
23013 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23014 result. */
23016 static void
23017 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23018 struct glyph_string *h, struct glyph_string *t)
23020 if (h)
23022 if (*head)
23023 (*head)->prev = t;
23024 else
23025 *tail = t;
23026 t->next = *head;
23027 *head = h;
23032 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23033 Set *HEAD and *TAIL to the resulting list. */
23035 static void
23036 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23037 struct glyph_string *s)
23039 s->next = s->prev = NULL;
23040 append_glyph_string_lists (head, tail, s, s);
23044 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23045 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23046 make sure that X resources for the face returned are allocated.
23047 Value is a pointer to a realized face that is ready for display if
23048 DISPLAY_P is non-zero. */
23050 static struct face *
23051 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23052 XChar2b *char2b, int display_p)
23054 struct face *face = FACE_FROM_ID (f, face_id);
23055 unsigned code = 0;
23057 if (face->font)
23059 code = face->font->driver->encode_char (face->font, c);
23061 if (code == FONT_INVALID_CODE)
23062 code = 0;
23064 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23066 /* Make sure X resources of the face are allocated. */
23067 #ifdef HAVE_X_WINDOWS
23068 if (display_p)
23069 #endif
23071 eassert (face != NULL);
23072 PREPARE_FACE_FOR_DISPLAY (f, face);
23075 return face;
23079 /* Get face and two-byte form of character glyph GLYPH on frame F.
23080 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23081 a pointer to a realized face that is ready for display. */
23083 static struct face *
23084 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23085 XChar2b *char2b, int *two_byte_p)
23087 struct face *face;
23088 unsigned code = 0;
23090 eassert (glyph->type == CHAR_GLYPH);
23091 face = FACE_FROM_ID (f, glyph->face_id);
23093 /* Make sure X resources of the face are allocated. */
23094 eassert (face != NULL);
23095 PREPARE_FACE_FOR_DISPLAY (f, face);
23097 if (two_byte_p)
23098 *two_byte_p = 0;
23100 if (face->font)
23102 if (CHAR_BYTE8_P (glyph->u.ch))
23103 code = CHAR_TO_BYTE8 (glyph->u.ch);
23104 else
23105 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23107 if (code == FONT_INVALID_CODE)
23108 code = 0;
23111 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23112 return face;
23116 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23117 Return 1 if FONT has a glyph for C, otherwise return 0. */
23119 static int
23120 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23122 unsigned code;
23124 if (CHAR_BYTE8_P (c))
23125 code = CHAR_TO_BYTE8 (c);
23126 else
23127 code = font->driver->encode_char (font, c);
23129 if (code == FONT_INVALID_CODE)
23130 return 0;
23131 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23132 return 1;
23136 /* Fill glyph string S with composition components specified by S->cmp.
23138 BASE_FACE is the base face of the composition.
23139 S->cmp_from is the index of the first component for S.
23141 OVERLAPS non-zero means S should draw the foreground only, and use
23142 its physical height for clipping. See also draw_glyphs.
23144 Value is the index of a component not in S. */
23146 static int
23147 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23148 int overlaps)
23150 int i;
23151 /* For all glyphs of this composition, starting at the offset
23152 S->cmp_from, until we reach the end of the definition or encounter a
23153 glyph that requires the different face, add it to S. */
23154 struct face *face;
23156 eassert (s);
23158 s->for_overlaps = overlaps;
23159 s->face = NULL;
23160 s->font = NULL;
23161 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23163 int c = COMPOSITION_GLYPH (s->cmp, i);
23165 /* TAB in a composition means display glyphs with padding space
23166 on the left or right. */
23167 if (c != '\t')
23169 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23170 -1, Qnil);
23172 face = get_char_face_and_encoding (s->f, c, face_id,
23173 s->char2b + i, 1);
23174 if (face)
23176 if (! s->face)
23178 s->face = face;
23179 s->font = s->face->font;
23181 else if (s->face != face)
23182 break;
23185 ++s->nchars;
23187 s->cmp_to = i;
23189 if (s->face == NULL)
23191 s->face = base_face->ascii_face;
23192 s->font = s->face->font;
23195 /* All glyph strings for the same composition has the same width,
23196 i.e. the width set for the first component of the composition. */
23197 s->width = s->first_glyph->pixel_width;
23199 /* If the specified font could not be loaded, use the frame's
23200 default font, but record the fact that we couldn't load it in
23201 the glyph string so that we can draw rectangles for the
23202 characters of the glyph string. */
23203 if (s->font == NULL)
23205 s->font_not_found_p = 1;
23206 s->font = FRAME_FONT (s->f);
23209 /* Adjust base line for subscript/superscript text. */
23210 s->ybase += s->first_glyph->voffset;
23212 /* This glyph string must always be drawn with 16-bit functions. */
23213 s->two_byte_p = 1;
23215 return s->cmp_to;
23218 static int
23219 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23220 int start, int end, int overlaps)
23222 struct glyph *glyph, *last;
23223 Lisp_Object lgstring;
23224 int i;
23226 s->for_overlaps = overlaps;
23227 glyph = s->row->glyphs[s->area] + start;
23228 last = s->row->glyphs[s->area] + end;
23229 s->cmp_id = glyph->u.cmp.id;
23230 s->cmp_from = glyph->slice.cmp.from;
23231 s->cmp_to = glyph->slice.cmp.to + 1;
23232 s->face = FACE_FROM_ID (s->f, face_id);
23233 lgstring = composition_gstring_from_id (s->cmp_id);
23234 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23235 glyph++;
23236 while (glyph < last
23237 && glyph->u.cmp.automatic
23238 && glyph->u.cmp.id == s->cmp_id
23239 && s->cmp_to == glyph->slice.cmp.from)
23240 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23242 for (i = s->cmp_from; i < s->cmp_to; i++)
23244 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23245 unsigned code = LGLYPH_CODE (lglyph);
23247 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23249 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23250 return glyph - s->row->glyphs[s->area];
23254 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23255 See the comment of fill_glyph_string for arguments.
23256 Value is the index of the first glyph not in S. */
23259 static int
23260 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23261 int start, int end, int overlaps)
23263 struct glyph *glyph, *last;
23264 int voffset;
23266 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23267 s->for_overlaps = overlaps;
23268 glyph = s->row->glyphs[s->area] + start;
23269 last = s->row->glyphs[s->area] + end;
23270 voffset = glyph->voffset;
23271 s->face = FACE_FROM_ID (s->f, face_id);
23272 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23273 s->nchars = 1;
23274 s->width = glyph->pixel_width;
23275 glyph++;
23276 while (glyph < last
23277 && glyph->type == GLYPHLESS_GLYPH
23278 && glyph->voffset == voffset
23279 && glyph->face_id == face_id)
23281 s->nchars++;
23282 s->width += glyph->pixel_width;
23283 glyph++;
23285 s->ybase += voffset;
23286 return glyph - s->row->glyphs[s->area];
23290 /* Fill glyph string S from a sequence of character glyphs.
23292 FACE_ID is the face id of the string. START is the index of the
23293 first glyph to consider, END is the index of the last + 1.
23294 OVERLAPS non-zero means S should draw the foreground only, and use
23295 its physical height for clipping. See also draw_glyphs.
23297 Value is the index of the first glyph not in S. */
23299 static int
23300 fill_glyph_string (struct glyph_string *s, int face_id,
23301 int start, int end, int overlaps)
23303 struct glyph *glyph, *last;
23304 int voffset;
23305 int glyph_not_available_p;
23307 eassert (s->f == XFRAME (s->w->frame));
23308 eassert (s->nchars == 0);
23309 eassert (start >= 0 && end > start);
23311 s->for_overlaps = overlaps;
23312 glyph = s->row->glyphs[s->area] + start;
23313 last = s->row->glyphs[s->area] + end;
23314 voffset = glyph->voffset;
23315 s->padding_p = glyph->padding_p;
23316 glyph_not_available_p = glyph->glyph_not_available_p;
23318 while (glyph < last
23319 && glyph->type == CHAR_GLYPH
23320 && glyph->voffset == voffset
23321 /* Same face id implies same font, nowadays. */
23322 && glyph->face_id == face_id
23323 && glyph->glyph_not_available_p == glyph_not_available_p)
23325 int two_byte_p;
23327 s->face = get_glyph_face_and_encoding (s->f, glyph,
23328 s->char2b + s->nchars,
23329 &two_byte_p);
23330 s->two_byte_p = two_byte_p;
23331 ++s->nchars;
23332 eassert (s->nchars <= end - start);
23333 s->width += glyph->pixel_width;
23334 if (glyph++->padding_p != s->padding_p)
23335 break;
23338 s->font = s->face->font;
23340 /* If the specified font could not be loaded, use the frame's font,
23341 but record the fact that we couldn't load it in
23342 S->font_not_found_p so that we can draw rectangles for the
23343 characters of the glyph string. */
23344 if (s->font == NULL || glyph_not_available_p)
23346 s->font_not_found_p = 1;
23347 s->font = FRAME_FONT (s->f);
23350 /* Adjust base line for subscript/superscript text. */
23351 s->ybase += voffset;
23353 eassert (s->face && s->face->gc);
23354 return glyph - s->row->glyphs[s->area];
23358 /* Fill glyph string S from image glyph S->first_glyph. */
23360 static void
23361 fill_image_glyph_string (struct glyph_string *s)
23363 eassert (s->first_glyph->type == IMAGE_GLYPH);
23364 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23365 eassert (s->img);
23366 s->slice = s->first_glyph->slice.img;
23367 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23368 s->font = s->face->font;
23369 s->width = s->first_glyph->pixel_width;
23371 /* Adjust base line for subscript/superscript text. */
23372 s->ybase += s->first_glyph->voffset;
23376 /* Fill glyph string S from a sequence of stretch glyphs.
23378 START is the index of the first glyph to consider,
23379 END is the index of the last + 1.
23381 Value is the index of the first glyph not in S. */
23383 static int
23384 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23386 struct glyph *glyph, *last;
23387 int voffset, face_id;
23389 eassert (s->first_glyph->type == STRETCH_GLYPH);
23391 glyph = s->row->glyphs[s->area] + start;
23392 last = s->row->glyphs[s->area] + end;
23393 face_id = glyph->face_id;
23394 s->face = FACE_FROM_ID (s->f, face_id);
23395 s->font = s->face->font;
23396 s->width = glyph->pixel_width;
23397 s->nchars = 1;
23398 voffset = glyph->voffset;
23400 for (++glyph;
23401 (glyph < last
23402 && glyph->type == STRETCH_GLYPH
23403 && glyph->voffset == voffset
23404 && glyph->face_id == face_id);
23405 ++glyph)
23406 s->width += glyph->pixel_width;
23408 /* Adjust base line for subscript/superscript text. */
23409 s->ybase += voffset;
23411 /* The case that face->gc == 0 is handled when drawing the glyph
23412 string by calling PREPARE_FACE_FOR_DISPLAY. */
23413 eassert (s->face);
23414 return glyph - s->row->glyphs[s->area];
23417 static struct font_metrics *
23418 get_per_char_metric (struct font *font, XChar2b *char2b)
23420 static struct font_metrics metrics;
23421 unsigned code;
23423 if (! font)
23424 return NULL;
23425 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23426 if (code == FONT_INVALID_CODE)
23427 return NULL;
23428 font->driver->text_extents (font, &code, 1, &metrics);
23429 return &metrics;
23432 /* EXPORT for RIF:
23433 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23434 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23435 assumed to be zero. */
23437 void
23438 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23440 *left = *right = 0;
23442 if (glyph->type == CHAR_GLYPH)
23444 struct face *face;
23445 XChar2b char2b;
23446 struct font_metrics *pcm;
23448 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23449 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23451 if (pcm->rbearing > pcm->width)
23452 *right = pcm->rbearing - pcm->width;
23453 if (pcm->lbearing < 0)
23454 *left = -pcm->lbearing;
23457 else if (glyph->type == COMPOSITE_GLYPH)
23459 if (! glyph->u.cmp.automatic)
23461 struct composition *cmp = composition_table[glyph->u.cmp.id];
23463 if (cmp->rbearing > cmp->pixel_width)
23464 *right = cmp->rbearing - cmp->pixel_width;
23465 if (cmp->lbearing < 0)
23466 *left = - cmp->lbearing;
23468 else
23470 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23471 struct font_metrics metrics;
23473 composition_gstring_width (gstring, glyph->slice.cmp.from,
23474 glyph->slice.cmp.to + 1, &metrics);
23475 if (metrics.rbearing > metrics.width)
23476 *right = metrics.rbearing - metrics.width;
23477 if (metrics.lbearing < 0)
23478 *left = - metrics.lbearing;
23484 /* Return the index of the first glyph preceding glyph string S that
23485 is overwritten by S because of S's left overhang. Value is -1
23486 if no glyphs are overwritten. */
23488 static int
23489 left_overwritten (struct glyph_string *s)
23491 int k;
23493 if (s->left_overhang)
23495 int x = 0, i;
23496 struct glyph *glyphs = s->row->glyphs[s->area];
23497 int first = s->first_glyph - glyphs;
23499 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23500 x -= glyphs[i].pixel_width;
23502 k = i + 1;
23504 else
23505 k = -1;
23507 return k;
23511 /* Return the index of the first glyph preceding glyph string S that
23512 is overwriting S because of its right overhang. Value is -1 if no
23513 glyph in front of S overwrites S. */
23515 static int
23516 left_overwriting (struct glyph_string *s)
23518 int i, k, x;
23519 struct glyph *glyphs = s->row->glyphs[s->area];
23520 int first = s->first_glyph - glyphs;
23522 k = -1;
23523 x = 0;
23524 for (i = first - 1; i >= 0; --i)
23526 int left, right;
23527 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23528 if (x + right > 0)
23529 k = i;
23530 x -= glyphs[i].pixel_width;
23533 return k;
23537 /* Return the index of the last glyph following glyph string S that is
23538 overwritten by S because of S's right overhang. Value is -1 if
23539 no such glyph is found. */
23541 static int
23542 right_overwritten (struct glyph_string *s)
23544 int k = -1;
23546 if (s->right_overhang)
23548 int x = 0, i;
23549 struct glyph *glyphs = s->row->glyphs[s->area];
23550 int first = (s->first_glyph - glyphs
23551 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23552 int end = s->row->used[s->area];
23554 for (i = first; i < end && s->right_overhang > x; ++i)
23555 x += glyphs[i].pixel_width;
23557 k = i;
23560 return k;
23564 /* Return the index of the last glyph following glyph string S that
23565 overwrites S because of its left overhang. Value is negative
23566 if no such glyph is found. */
23568 static int
23569 right_overwriting (struct glyph_string *s)
23571 int i, k, x;
23572 int end = s->row->used[s->area];
23573 struct glyph *glyphs = s->row->glyphs[s->area];
23574 int first = (s->first_glyph - glyphs
23575 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23577 k = -1;
23578 x = 0;
23579 for (i = first; i < end; ++i)
23581 int left, right;
23582 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23583 if (x - left < 0)
23584 k = i;
23585 x += glyphs[i].pixel_width;
23588 return k;
23592 /* Set background width of glyph string S. START is the index of the
23593 first glyph following S. LAST_X is the right-most x-position + 1
23594 in the drawing area. */
23596 static void
23597 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23599 /* If the face of this glyph string has to be drawn to the end of
23600 the drawing area, set S->extends_to_end_of_line_p. */
23602 if (start == s->row->used[s->area]
23603 && s->area == TEXT_AREA
23604 && ((s->row->fill_line_p
23605 && (s->hl == DRAW_NORMAL_TEXT
23606 || s->hl == DRAW_IMAGE_RAISED
23607 || s->hl == DRAW_IMAGE_SUNKEN))
23608 || s->hl == DRAW_MOUSE_FACE))
23609 s->extends_to_end_of_line_p = 1;
23611 /* If S extends its face to the end of the line, set its
23612 background_width to the distance to the right edge of the drawing
23613 area. */
23614 if (s->extends_to_end_of_line_p)
23615 s->background_width = last_x - s->x + 1;
23616 else
23617 s->background_width = s->width;
23621 /* Compute overhangs and x-positions for glyph string S and its
23622 predecessors, or successors. X is the starting x-position for S.
23623 BACKWARD_P non-zero means process predecessors. */
23625 static void
23626 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23628 if (backward_p)
23630 while (s)
23632 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23633 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23634 x -= s->width;
23635 s->x = x;
23636 s = s->prev;
23639 else
23641 while (s)
23643 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23644 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23645 s->x = x;
23646 x += s->width;
23647 s = s->next;
23654 /* The following macros are only called from draw_glyphs below.
23655 They reference the following parameters of that function directly:
23656 `w', `row', `area', and `overlap_p'
23657 as well as the following local variables:
23658 `s', `f', and `hdc' (in W32) */
23660 #ifdef HAVE_NTGUI
23661 /* On W32, silently add local `hdc' variable to argument list of
23662 init_glyph_string. */
23663 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23664 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23665 #else
23666 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23667 init_glyph_string (s, char2b, w, row, area, start, hl)
23668 #endif
23670 /* Add a glyph string for a stretch glyph to the list of strings
23671 between HEAD and TAIL. START is the index of the stretch glyph in
23672 row area AREA of glyph row ROW. END is the index of the last glyph
23673 in that glyph row area. X is the current output position assigned
23674 to the new glyph string constructed. HL overrides that face of the
23675 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23676 is the right-most x-position of the drawing area. */
23678 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23679 and below -- keep them on one line. */
23680 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23681 do \
23683 s = alloca (sizeof *s); \
23684 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23685 START = fill_stretch_glyph_string (s, START, END); \
23686 append_glyph_string (&HEAD, &TAIL, s); \
23687 s->x = (X); \
23689 while (0)
23692 /* Add a glyph string for an image glyph to the list of strings
23693 between HEAD and TAIL. START is the index of the image glyph in
23694 row area AREA of glyph row ROW. END is the index of the last glyph
23695 in that glyph row area. X is the current output position assigned
23696 to the new glyph string constructed. HL overrides that face of the
23697 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23698 is the right-most x-position of the drawing area. */
23700 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23701 do \
23703 s = alloca (sizeof *s); \
23704 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23705 fill_image_glyph_string (s); \
23706 append_glyph_string (&HEAD, &TAIL, s); \
23707 ++START; \
23708 s->x = (X); \
23710 while (0)
23713 /* Add a glyph string for a sequence of character glyphs to the list
23714 of strings between HEAD and TAIL. START is the index of the first
23715 glyph in row area AREA of glyph row ROW that is part of the new
23716 glyph string. END is the index of the last glyph in that glyph row
23717 area. X is the current output position assigned to the new glyph
23718 string constructed. HL overrides that face of the glyph; e.g. it
23719 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23720 right-most x-position of the drawing area. */
23722 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23723 do \
23725 int face_id; \
23726 XChar2b *char2b; \
23728 face_id = (row)->glyphs[area][START].face_id; \
23730 s = alloca (sizeof *s); \
23731 char2b = alloca ((END - START) * sizeof *char2b); \
23732 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23733 append_glyph_string (&HEAD, &TAIL, s); \
23734 s->x = (X); \
23735 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23737 while (0)
23740 /* Add a glyph string for a composite sequence to the list of strings
23741 between HEAD and TAIL. START is the index of the first glyph in
23742 row area AREA of glyph row ROW that is part of the new glyph
23743 string. END is the index of the last glyph in that glyph row area.
23744 X is the current output position assigned to the new glyph string
23745 constructed. HL overrides that face of the glyph; e.g. it is
23746 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23747 x-position of the drawing area. */
23749 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23750 do { \
23751 int face_id = (row)->glyphs[area][START].face_id; \
23752 struct face *base_face = FACE_FROM_ID (f, face_id); \
23753 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23754 struct composition *cmp = composition_table[cmp_id]; \
23755 XChar2b *char2b; \
23756 struct glyph_string *first_s = NULL; \
23757 int n; \
23759 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23761 /* Make glyph_strings for each glyph sequence that is drawable by \
23762 the same face, and append them to HEAD/TAIL. */ \
23763 for (n = 0; n < cmp->glyph_len;) \
23765 s = alloca (sizeof *s); \
23766 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23767 append_glyph_string (&(HEAD), &(TAIL), s); \
23768 s->cmp = cmp; \
23769 s->cmp_from = n; \
23770 s->x = (X); \
23771 if (n == 0) \
23772 first_s = s; \
23773 n = fill_composite_glyph_string (s, base_face, overlaps); \
23776 ++START; \
23777 s = first_s; \
23778 } while (0)
23781 /* Add a glyph string for a glyph-string sequence to the list of strings
23782 between HEAD and TAIL. */
23784 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23785 do { \
23786 int face_id; \
23787 XChar2b *char2b; \
23788 Lisp_Object gstring; \
23790 face_id = (row)->glyphs[area][START].face_id; \
23791 gstring = (composition_gstring_from_id \
23792 ((row)->glyphs[area][START].u.cmp.id)); \
23793 s = alloca (sizeof *s); \
23794 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23795 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23796 append_glyph_string (&(HEAD), &(TAIL), s); \
23797 s->x = (X); \
23798 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23799 } while (0)
23802 /* Add a glyph string for a sequence of glyphless character's glyphs
23803 to the list of strings between HEAD and TAIL. The meanings of
23804 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23806 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23807 do \
23809 int face_id; \
23811 face_id = (row)->glyphs[area][START].face_id; \
23813 s = alloca (sizeof *s); \
23814 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23815 append_glyph_string (&HEAD, &TAIL, s); \
23816 s->x = (X); \
23817 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23818 overlaps); \
23820 while (0)
23823 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23824 of AREA of glyph row ROW on window W between indices START and END.
23825 HL overrides the face for drawing glyph strings, e.g. it is
23826 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23827 x-positions of the drawing area.
23829 This is an ugly monster macro construct because we must use alloca
23830 to allocate glyph strings (because draw_glyphs can be called
23831 asynchronously). */
23833 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23834 do \
23836 HEAD = TAIL = NULL; \
23837 while (START < END) \
23839 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23840 switch (first_glyph->type) \
23842 case CHAR_GLYPH: \
23843 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23844 HL, X, LAST_X); \
23845 break; \
23847 case COMPOSITE_GLYPH: \
23848 if (first_glyph->u.cmp.automatic) \
23849 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23850 HL, X, LAST_X); \
23851 else \
23852 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23853 HL, X, LAST_X); \
23854 break; \
23856 case STRETCH_GLYPH: \
23857 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23858 HL, X, LAST_X); \
23859 break; \
23861 case IMAGE_GLYPH: \
23862 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23863 HL, X, LAST_X); \
23864 break; \
23866 case GLYPHLESS_GLYPH: \
23867 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23868 HL, X, LAST_X); \
23869 break; \
23871 default: \
23872 emacs_abort (); \
23875 if (s) \
23877 set_glyph_string_background_width (s, START, LAST_X); \
23878 (X) += s->width; \
23881 } while (0)
23884 /* Draw glyphs between START and END in AREA of ROW on window W,
23885 starting at x-position X. X is relative to AREA in W. HL is a
23886 face-override with the following meaning:
23888 DRAW_NORMAL_TEXT draw normally
23889 DRAW_CURSOR draw in cursor face
23890 DRAW_MOUSE_FACE draw in mouse face.
23891 DRAW_INVERSE_VIDEO draw in mode line face
23892 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23893 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23895 If OVERLAPS is non-zero, draw only the foreground of characters and
23896 clip to the physical height of ROW. Non-zero value also defines
23897 the overlapping part to be drawn:
23899 OVERLAPS_PRED overlap with preceding rows
23900 OVERLAPS_SUCC overlap with succeeding rows
23901 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23902 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23904 Value is the x-position reached, relative to AREA of W. */
23906 static int
23907 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23908 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23909 enum draw_glyphs_face hl, int overlaps)
23911 struct glyph_string *head, *tail;
23912 struct glyph_string *s;
23913 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23914 int i, j, x_reached, last_x, area_left = 0;
23915 struct frame *f = XFRAME (WINDOW_FRAME (w));
23916 DECLARE_HDC (hdc);
23918 ALLOCATE_HDC (hdc, f);
23920 /* Let's rather be paranoid than getting a SEGV. */
23921 end = min (end, row->used[area]);
23922 start = clip_to_bounds (0, start, end);
23924 /* Translate X to frame coordinates. Set last_x to the right
23925 end of the drawing area. */
23926 if (row->full_width_p)
23928 /* X is relative to the left edge of W, without scroll bars
23929 or fringes. */
23930 area_left = WINDOW_LEFT_EDGE_X (w);
23931 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23933 else
23935 area_left = window_box_left (w, area);
23936 last_x = area_left + window_box_width (w, area);
23938 x += area_left;
23940 /* Build a doubly-linked list of glyph_string structures between
23941 head and tail from what we have to draw. Note that the macro
23942 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23943 the reason we use a separate variable `i'. */
23944 i = start;
23945 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23946 if (tail)
23947 x_reached = tail->x + tail->background_width;
23948 else
23949 x_reached = x;
23951 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23952 the row, redraw some glyphs in front or following the glyph
23953 strings built above. */
23954 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23956 struct glyph_string *h, *t;
23957 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23958 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23959 int check_mouse_face = 0;
23960 int dummy_x = 0;
23962 /* If mouse highlighting is on, we may need to draw adjacent
23963 glyphs using mouse-face highlighting. */
23964 if (area == TEXT_AREA && row->mouse_face_p
23965 && hlinfo->mouse_face_beg_row >= 0
23966 && hlinfo->mouse_face_end_row >= 0)
23968 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23970 if (row_vpos >= hlinfo->mouse_face_beg_row
23971 && row_vpos <= hlinfo->mouse_face_end_row)
23973 check_mouse_face = 1;
23974 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23975 ? hlinfo->mouse_face_beg_col : 0;
23976 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23977 ? hlinfo->mouse_face_end_col
23978 : row->used[TEXT_AREA];
23982 /* Compute overhangs for all glyph strings. */
23983 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23984 for (s = head; s; s = s->next)
23985 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23987 /* Prepend glyph strings for glyphs in front of the first glyph
23988 string that are overwritten because of the first glyph
23989 string's left overhang. The background of all strings
23990 prepended must be drawn because the first glyph string
23991 draws over it. */
23992 i = left_overwritten (head);
23993 if (i >= 0)
23995 enum draw_glyphs_face overlap_hl;
23997 /* If this row contains mouse highlighting, attempt to draw
23998 the overlapped glyphs with the correct highlight. This
23999 code fails if the overlap encompasses more than one glyph
24000 and mouse-highlight spans only some of these glyphs.
24001 However, making it work perfectly involves a lot more
24002 code, and I don't know if the pathological case occurs in
24003 practice, so we'll stick to this for now. --- cyd */
24004 if (check_mouse_face
24005 && mouse_beg_col < start && mouse_end_col > i)
24006 overlap_hl = DRAW_MOUSE_FACE;
24007 else
24008 overlap_hl = DRAW_NORMAL_TEXT;
24010 j = i;
24011 BUILD_GLYPH_STRINGS (j, start, h, t,
24012 overlap_hl, dummy_x, last_x);
24013 start = i;
24014 compute_overhangs_and_x (t, head->x, 1);
24015 prepend_glyph_string_lists (&head, &tail, h, t);
24016 clip_head = head;
24019 /* Prepend glyph strings for glyphs in front of the first glyph
24020 string that overwrite that glyph string because of their
24021 right overhang. For these strings, only the foreground must
24022 be drawn, because it draws over the glyph string at `head'.
24023 The background must not be drawn because this would overwrite
24024 right overhangs of preceding glyphs for which no glyph
24025 strings exist. */
24026 i = left_overwriting (head);
24027 if (i >= 0)
24029 enum draw_glyphs_face overlap_hl;
24031 if (check_mouse_face
24032 && mouse_beg_col < start && mouse_end_col > i)
24033 overlap_hl = DRAW_MOUSE_FACE;
24034 else
24035 overlap_hl = DRAW_NORMAL_TEXT;
24037 clip_head = head;
24038 BUILD_GLYPH_STRINGS (i, start, h, t,
24039 overlap_hl, dummy_x, last_x);
24040 for (s = h; s; s = s->next)
24041 s->background_filled_p = 1;
24042 compute_overhangs_and_x (t, head->x, 1);
24043 prepend_glyph_string_lists (&head, &tail, h, t);
24046 /* Append glyphs strings for glyphs following the last glyph
24047 string tail that are overwritten by tail. The background of
24048 these strings has to be drawn because tail's foreground draws
24049 over it. */
24050 i = right_overwritten (tail);
24051 if (i >= 0)
24053 enum draw_glyphs_face overlap_hl;
24055 if (check_mouse_face
24056 && mouse_beg_col < i && mouse_end_col > end)
24057 overlap_hl = DRAW_MOUSE_FACE;
24058 else
24059 overlap_hl = DRAW_NORMAL_TEXT;
24061 BUILD_GLYPH_STRINGS (end, i, h, t,
24062 overlap_hl, x, last_x);
24063 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24064 we don't have `end = i;' here. */
24065 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24066 append_glyph_string_lists (&head, &tail, h, t);
24067 clip_tail = tail;
24070 /* Append glyph strings for glyphs following the last glyph
24071 string tail that overwrite tail. The foreground of such
24072 glyphs has to be drawn because it writes into the background
24073 of tail. The background must not be drawn because it could
24074 paint over the foreground of following glyphs. */
24075 i = right_overwriting (tail);
24076 if (i >= 0)
24078 enum draw_glyphs_face overlap_hl;
24079 if (check_mouse_face
24080 && mouse_beg_col < i && mouse_end_col > end)
24081 overlap_hl = DRAW_MOUSE_FACE;
24082 else
24083 overlap_hl = DRAW_NORMAL_TEXT;
24085 clip_tail = tail;
24086 i++; /* We must include the Ith glyph. */
24087 BUILD_GLYPH_STRINGS (end, i, h, t,
24088 overlap_hl, x, last_x);
24089 for (s = h; s; s = s->next)
24090 s->background_filled_p = 1;
24091 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24092 append_glyph_string_lists (&head, &tail, h, t);
24094 if (clip_head || clip_tail)
24095 for (s = head; s; s = s->next)
24097 s->clip_head = clip_head;
24098 s->clip_tail = clip_tail;
24102 /* Draw all strings. */
24103 for (s = head; s; s = s->next)
24104 FRAME_RIF (f)->draw_glyph_string (s);
24106 #ifndef HAVE_NS
24107 /* When focus a sole frame and move horizontally, this sets on_p to 0
24108 causing a failure to erase prev cursor position. */
24109 if (area == TEXT_AREA
24110 && !row->full_width_p
24111 /* When drawing overlapping rows, only the glyph strings'
24112 foreground is drawn, which doesn't erase a cursor
24113 completely. */
24114 && !overlaps)
24116 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24117 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24118 : (tail ? tail->x + tail->background_width : x));
24119 x0 -= area_left;
24120 x1 -= area_left;
24122 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24123 row->y, MATRIX_ROW_BOTTOM_Y (row));
24125 #endif
24127 /* Value is the x-position up to which drawn, relative to AREA of W.
24128 This doesn't include parts drawn because of overhangs. */
24129 if (row->full_width_p)
24130 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24131 else
24132 x_reached -= area_left;
24134 RELEASE_HDC (hdc, f);
24136 return x_reached;
24139 /* Expand row matrix if too narrow. Don't expand if area
24140 is not present. */
24142 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24144 if (!it->f->fonts_changed \
24145 && (it->glyph_row->glyphs[area] \
24146 < it->glyph_row->glyphs[area + 1])) \
24148 it->w->ncols_scale_factor++; \
24149 it->f->fonts_changed = 1; \
24153 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24154 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24156 static void
24157 append_glyph (struct it *it)
24159 struct glyph *glyph;
24160 enum glyph_row_area area = it->area;
24162 eassert (it->glyph_row);
24163 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24165 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24166 if (glyph < it->glyph_row->glyphs[area + 1])
24168 /* If the glyph row is reversed, we need to prepend the glyph
24169 rather than append it. */
24170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24172 struct glyph *g;
24174 /* Make room for the additional glyph. */
24175 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24176 g[1] = *g;
24177 glyph = it->glyph_row->glyphs[area];
24179 glyph->charpos = CHARPOS (it->position);
24180 glyph->object = it->object;
24181 if (it->pixel_width > 0)
24183 glyph->pixel_width = it->pixel_width;
24184 glyph->padding_p = 0;
24186 else
24188 /* Assure at least 1-pixel width. Otherwise, cursor can't
24189 be displayed correctly. */
24190 glyph->pixel_width = 1;
24191 glyph->padding_p = 1;
24193 glyph->ascent = it->ascent;
24194 glyph->descent = it->descent;
24195 glyph->voffset = it->voffset;
24196 glyph->type = CHAR_GLYPH;
24197 glyph->avoid_cursor_p = it->avoid_cursor_p;
24198 glyph->multibyte_p = it->multibyte_p;
24199 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24201 /* In R2L rows, the left and the right box edges need to be
24202 drawn in reverse direction. */
24203 glyph->right_box_line_p = it->start_of_box_run_p;
24204 glyph->left_box_line_p = it->end_of_box_run_p;
24206 else
24208 glyph->left_box_line_p = it->start_of_box_run_p;
24209 glyph->right_box_line_p = it->end_of_box_run_p;
24211 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24212 || it->phys_descent > it->descent);
24213 glyph->glyph_not_available_p = it->glyph_not_available_p;
24214 glyph->face_id = it->face_id;
24215 glyph->u.ch = it->char_to_display;
24216 glyph->slice.img = null_glyph_slice;
24217 glyph->font_type = FONT_TYPE_UNKNOWN;
24218 if (it->bidi_p)
24220 glyph->resolved_level = it->bidi_it.resolved_level;
24221 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24222 emacs_abort ();
24223 glyph->bidi_type = it->bidi_it.type;
24225 else
24227 glyph->resolved_level = 0;
24228 glyph->bidi_type = UNKNOWN_BT;
24230 ++it->glyph_row->used[area];
24232 else
24233 IT_EXPAND_MATRIX_WIDTH (it, area);
24236 /* Store one glyph for the composition IT->cmp_it.id in
24237 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24238 non-null. */
24240 static void
24241 append_composite_glyph (struct it *it)
24243 struct glyph *glyph;
24244 enum glyph_row_area area = it->area;
24246 eassert (it->glyph_row);
24248 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24249 if (glyph < it->glyph_row->glyphs[area + 1])
24251 /* If the glyph row is reversed, we need to prepend the glyph
24252 rather than append it. */
24253 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24255 struct glyph *g;
24257 /* Make room for the new glyph. */
24258 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24259 g[1] = *g;
24260 glyph = it->glyph_row->glyphs[it->area];
24262 glyph->charpos = it->cmp_it.charpos;
24263 glyph->object = it->object;
24264 glyph->pixel_width = it->pixel_width;
24265 glyph->ascent = it->ascent;
24266 glyph->descent = it->descent;
24267 glyph->voffset = it->voffset;
24268 glyph->type = COMPOSITE_GLYPH;
24269 if (it->cmp_it.ch < 0)
24271 glyph->u.cmp.automatic = 0;
24272 glyph->u.cmp.id = it->cmp_it.id;
24273 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24275 else
24277 glyph->u.cmp.automatic = 1;
24278 glyph->u.cmp.id = it->cmp_it.id;
24279 glyph->slice.cmp.from = it->cmp_it.from;
24280 glyph->slice.cmp.to = it->cmp_it.to - 1;
24282 glyph->avoid_cursor_p = it->avoid_cursor_p;
24283 glyph->multibyte_p = it->multibyte_p;
24284 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24286 /* In R2L rows, the left and the right box edges need to be
24287 drawn in reverse direction. */
24288 glyph->right_box_line_p = it->start_of_box_run_p;
24289 glyph->left_box_line_p = it->end_of_box_run_p;
24291 else
24293 glyph->left_box_line_p = it->start_of_box_run_p;
24294 glyph->right_box_line_p = it->end_of_box_run_p;
24296 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24297 || it->phys_descent > it->descent);
24298 glyph->padding_p = 0;
24299 glyph->glyph_not_available_p = 0;
24300 glyph->face_id = it->face_id;
24301 glyph->font_type = FONT_TYPE_UNKNOWN;
24302 if (it->bidi_p)
24304 glyph->resolved_level = it->bidi_it.resolved_level;
24305 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24306 emacs_abort ();
24307 glyph->bidi_type = it->bidi_it.type;
24309 ++it->glyph_row->used[area];
24311 else
24312 IT_EXPAND_MATRIX_WIDTH (it, area);
24316 /* Change IT->ascent and IT->height according to the setting of
24317 IT->voffset. */
24319 static void
24320 take_vertical_position_into_account (struct it *it)
24322 if (it->voffset)
24324 if (it->voffset < 0)
24325 /* Increase the ascent so that we can display the text higher
24326 in the line. */
24327 it->ascent -= it->voffset;
24328 else
24329 /* Increase the descent so that we can display the text lower
24330 in the line. */
24331 it->descent += it->voffset;
24336 /* Produce glyphs/get display metrics for the image IT is loaded with.
24337 See the description of struct display_iterator in dispextern.h for
24338 an overview of struct display_iterator. */
24340 static void
24341 produce_image_glyph (struct it *it)
24343 struct image *img;
24344 struct face *face;
24345 int glyph_ascent, crop;
24346 struct glyph_slice slice;
24348 eassert (it->what == IT_IMAGE);
24350 face = FACE_FROM_ID (it->f, it->face_id);
24351 eassert (face);
24352 /* Make sure X resources of the face is loaded. */
24353 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24355 if (it->image_id < 0)
24357 /* Fringe bitmap. */
24358 it->ascent = it->phys_ascent = 0;
24359 it->descent = it->phys_descent = 0;
24360 it->pixel_width = 0;
24361 it->nglyphs = 0;
24362 return;
24365 img = IMAGE_FROM_ID (it->f, it->image_id);
24366 eassert (img);
24367 /* Make sure X resources of the image is loaded. */
24368 prepare_image_for_display (it->f, img);
24370 slice.x = slice.y = 0;
24371 slice.width = img->width;
24372 slice.height = img->height;
24374 if (INTEGERP (it->slice.x))
24375 slice.x = XINT (it->slice.x);
24376 else if (FLOATP (it->slice.x))
24377 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24379 if (INTEGERP (it->slice.y))
24380 slice.y = XINT (it->slice.y);
24381 else if (FLOATP (it->slice.y))
24382 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24384 if (INTEGERP (it->slice.width))
24385 slice.width = XINT (it->slice.width);
24386 else if (FLOATP (it->slice.width))
24387 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24389 if (INTEGERP (it->slice.height))
24390 slice.height = XINT (it->slice.height);
24391 else if (FLOATP (it->slice.height))
24392 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24394 if (slice.x >= img->width)
24395 slice.x = img->width;
24396 if (slice.y >= img->height)
24397 slice.y = img->height;
24398 if (slice.x + slice.width >= img->width)
24399 slice.width = img->width - slice.x;
24400 if (slice.y + slice.height > img->height)
24401 slice.height = img->height - slice.y;
24403 if (slice.width == 0 || slice.height == 0)
24404 return;
24406 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24408 it->descent = slice.height - glyph_ascent;
24409 if (slice.y == 0)
24410 it->descent += img->vmargin;
24411 if (slice.y + slice.height == img->height)
24412 it->descent += img->vmargin;
24413 it->phys_descent = it->descent;
24415 it->pixel_width = slice.width;
24416 if (slice.x == 0)
24417 it->pixel_width += img->hmargin;
24418 if (slice.x + slice.width == img->width)
24419 it->pixel_width += img->hmargin;
24421 /* It's quite possible for images to have an ascent greater than
24422 their height, so don't get confused in that case. */
24423 if (it->descent < 0)
24424 it->descent = 0;
24426 it->nglyphs = 1;
24428 if (face->box != FACE_NO_BOX)
24430 if (face->box_line_width > 0)
24432 if (slice.y == 0)
24433 it->ascent += face->box_line_width;
24434 if (slice.y + slice.height == img->height)
24435 it->descent += face->box_line_width;
24438 if (it->start_of_box_run_p && slice.x == 0)
24439 it->pixel_width += eabs (face->box_line_width);
24440 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24441 it->pixel_width += eabs (face->box_line_width);
24444 take_vertical_position_into_account (it);
24446 /* Automatically crop wide image glyphs at right edge so we can
24447 draw the cursor on same display row. */
24448 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24449 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24451 it->pixel_width -= crop;
24452 slice.width -= crop;
24455 if (it->glyph_row)
24457 struct glyph *glyph;
24458 enum glyph_row_area area = it->area;
24460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24461 if (glyph < it->glyph_row->glyphs[area + 1])
24463 glyph->charpos = CHARPOS (it->position);
24464 glyph->object = it->object;
24465 glyph->pixel_width = it->pixel_width;
24466 glyph->ascent = glyph_ascent;
24467 glyph->descent = it->descent;
24468 glyph->voffset = it->voffset;
24469 glyph->type = IMAGE_GLYPH;
24470 glyph->avoid_cursor_p = it->avoid_cursor_p;
24471 glyph->multibyte_p = it->multibyte_p;
24472 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24474 /* In R2L rows, the left and the right box edges need to be
24475 drawn in reverse direction. */
24476 glyph->right_box_line_p = it->start_of_box_run_p;
24477 glyph->left_box_line_p = it->end_of_box_run_p;
24479 else
24481 glyph->left_box_line_p = it->start_of_box_run_p;
24482 glyph->right_box_line_p = it->end_of_box_run_p;
24484 glyph->overlaps_vertically_p = 0;
24485 glyph->padding_p = 0;
24486 glyph->glyph_not_available_p = 0;
24487 glyph->face_id = it->face_id;
24488 glyph->u.img_id = img->id;
24489 glyph->slice.img = slice;
24490 glyph->font_type = FONT_TYPE_UNKNOWN;
24491 if (it->bidi_p)
24493 glyph->resolved_level = it->bidi_it.resolved_level;
24494 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24495 emacs_abort ();
24496 glyph->bidi_type = it->bidi_it.type;
24498 ++it->glyph_row->used[area];
24500 else
24501 IT_EXPAND_MATRIX_WIDTH (it, area);
24506 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24507 of the glyph, WIDTH and HEIGHT are the width and height of the
24508 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24510 static void
24511 append_stretch_glyph (struct it *it, Lisp_Object object,
24512 int width, int height, int ascent)
24514 struct glyph *glyph;
24515 enum glyph_row_area area = it->area;
24517 eassert (ascent >= 0 && ascent <= height);
24519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24520 if (glyph < it->glyph_row->glyphs[area + 1])
24522 /* If the glyph row is reversed, we need to prepend the glyph
24523 rather than append it. */
24524 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24526 struct glyph *g;
24528 /* Make room for the additional glyph. */
24529 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24530 g[1] = *g;
24531 glyph = it->glyph_row->glyphs[area];
24533 glyph->charpos = CHARPOS (it->position);
24534 glyph->object = object;
24535 glyph->pixel_width = width;
24536 glyph->ascent = ascent;
24537 glyph->descent = height - ascent;
24538 glyph->voffset = it->voffset;
24539 glyph->type = STRETCH_GLYPH;
24540 glyph->avoid_cursor_p = it->avoid_cursor_p;
24541 glyph->multibyte_p = it->multibyte_p;
24542 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24544 /* In R2L rows, the left and the right box edges need to be
24545 drawn in reverse direction. */
24546 glyph->right_box_line_p = it->start_of_box_run_p;
24547 glyph->left_box_line_p = it->end_of_box_run_p;
24549 else
24551 glyph->left_box_line_p = it->start_of_box_run_p;
24552 glyph->right_box_line_p = it->end_of_box_run_p;
24554 glyph->overlaps_vertically_p = 0;
24555 glyph->padding_p = 0;
24556 glyph->glyph_not_available_p = 0;
24557 glyph->face_id = it->face_id;
24558 glyph->u.stretch.ascent = ascent;
24559 glyph->u.stretch.height = height;
24560 glyph->slice.img = null_glyph_slice;
24561 glyph->font_type = FONT_TYPE_UNKNOWN;
24562 if (it->bidi_p)
24564 glyph->resolved_level = it->bidi_it.resolved_level;
24565 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24566 emacs_abort ();
24567 glyph->bidi_type = it->bidi_it.type;
24569 else
24571 glyph->resolved_level = 0;
24572 glyph->bidi_type = UNKNOWN_BT;
24574 ++it->glyph_row->used[area];
24576 else
24577 IT_EXPAND_MATRIX_WIDTH (it, area);
24580 #endif /* HAVE_WINDOW_SYSTEM */
24582 /* Produce a stretch glyph for iterator IT. IT->object is the value
24583 of the glyph property displayed. The value must be a list
24584 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24585 being recognized:
24587 1. `:width WIDTH' specifies that the space should be WIDTH *
24588 canonical char width wide. WIDTH may be an integer or floating
24589 point number.
24591 2. `:relative-width FACTOR' specifies that the width of the stretch
24592 should be computed from the width of the first character having the
24593 `glyph' property, and should be FACTOR times that width.
24595 3. `:align-to HPOS' specifies that the space should be wide enough
24596 to reach HPOS, a value in canonical character units.
24598 Exactly one of the above pairs must be present.
24600 4. `:height HEIGHT' specifies that the height of the stretch produced
24601 should be HEIGHT, measured in canonical character units.
24603 5. `:relative-height FACTOR' specifies that the height of the
24604 stretch should be FACTOR times the height of the characters having
24605 the glyph property.
24607 Either none or exactly one of 4 or 5 must be present.
24609 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24610 of the stretch should be used for the ascent of the stretch.
24611 ASCENT must be in the range 0 <= ASCENT <= 100. */
24613 void
24614 produce_stretch_glyph (struct it *it)
24616 /* (space :width WIDTH :height HEIGHT ...) */
24617 Lisp_Object prop, plist;
24618 int width = 0, height = 0, align_to = -1;
24619 int zero_width_ok_p = 0;
24620 double tem;
24621 struct font *font = NULL;
24623 #ifdef HAVE_WINDOW_SYSTEM
24624 int ascent = 0;
24625 int zero_height_ok_p = 0;
24627 if (FRAME_WINDOW_P (it->f))
24629 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24630 font = face->font ? face->font : FRAME_FONT (it->f);
24631 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24633 #endif
24635 /* List should start with `space'. */
24636 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24637 plist = XCDR (it->object);
24639 /* Compute the width of the stretch. */
24640 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24641 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24643 /* Absolute width `:width WIDTH' specified and valid. */
24644 zero_width_ok_p = 1;
24645 width = (int)tem;
24647 #ifdef HAVE_WINDOW_SYSTEM
24648 else if (FRAME_WINDOW_P (it->f)
24649 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24651 /* Relative width `:relative-width FACTOR' specified and valid.
24652 Compute the width of the characters having the `glyph'
24653 property. */
24654 struct it it2;
24655 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24657 it2 = *it;
24658 if (it->multibyte_p)
24659 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24660 else
24662 it2.c = it2.char_to_display = *p, it2.len = 1;
24663 if (! ASCII_CHAR_P (it2.c))
24664 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24667 it2.glyph_row = NULL;
24668 it2.what = IT_CHARACTER;
24669 x_produce_glyphs (&it2);
24670 width = NUMVAL (prop) * it2.pixel_width;
24672 #endif /* HAVE_WINDOW_SYSTEM */
24673 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24674 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24676 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24677 align_to = (align_to < 0
24679 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24680 else if (align_to < 0)
24681 align_to = window_box_left_offset (it->w, TEXT_AREA);
24682 width = max (0, (int)tem + align_to - it->current_x);
24683 zero_width_ok_p = 1;
24685 else
24686 /* Nothing specified -> width defaults to canonical char width. */
24687 width = FRAME_COLUMN_WIDTH (it->f);
24689 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24690 width = 1;
24692 #ifdef HAVE_WINDOW_SYSTEM
24693 /* Compute height. */
24694 if (FRAME_WINDOW_P (it->f))
24696 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24697 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24699 height = (int)tem;
24700 zero_height_ok_p = 1;
24702 else if (prop = Fplist_get (plist, QCrelative_height),
24703 NUMVAL (prop) > 0)
24704 height = FONT_HEIGHT (font) * NUMVAL (prop);
24705 else
24706 height = FONT_HEIGHT (font);
24708 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24709 height = 1;
24711 /* Compute percentage of height used for ascent. If
24712 `:ascent ASCENT' is present and valid, use that. Otherwise,
24713 derive the ascent from the font in use. */
24714 if (prop = Fplist_get (plist, QCascent),
24715 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24716 ascent = height * NUMVAL (prop) / 100.0;
24717 else if (!NILP (prop)
24718 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24719 ascent = min (max (0, (int)tem), height);
24720 else
24721 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24723 else
24724 #endif /* HAVE_WINDOW_SYSTEM */
24725 height = 1;
24727 if (width > 0 && it->line_wrap != TRUNCATE
24728 && it->current_x + width > it->last_visible_x)
24730 width = it->last_visible_x - it->current_x;
24731 #ifdef HAVE_WINDOW_SYSTEM
24732 /* Subtract one more pixel from the stretch width, but only on
24733 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24734 width -= FRAME_WINDOW_P (it->f);
24735 #endif
24738 if (width > 0 && height > 0 && it->glyph_row)
24740 Lisp_Object o_object = it->object;
24741 Lisp_Object object = it->stack[it->sp - 1].string;
24742 int n = width;
24744 if (!STRINGP (object))
24745 object = it->w->contents;
24746 #ifdef HAVE_WINDOW_SYSTEM
24747 if (FRAME_WINDOW_P (it->f))
24748 append_stretch_glyph (it, object, width, height, ascent);
24749 else
24750 #endif
24752 it->object = object;
24753 it->char_to_display = ' ';
24754 it->pixel_width = it->len = 1;
24755 while (n--)
24756 tty_append_glyph (it);
24757 it->object = o_object;
24761 it->pixel_width = width;
24762 #ifdef HAVE_WINDOW_SYSTEM
24763 if (FRAME_WINDOW_P (it->f))
24765 it->ascent = it->phys_ascent = ascent;
24766 it->descent = it->phys_descent = height - it->ascent;
24767 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24768 take_vertical_position_into_account (it);
24770 else
24771 #endif
24772 it->nglyphs = width;
24775 /* Get information about special display element WHAT in an
24776 environment described by IT. WHAT is one of IT_TRUNCATION or
24777 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24778 non-null glyph_row member. This function ensures that fields like
24779 face_id, c, len of IT are left untouched. */
24781 static void
24782 produce_special_glyphs (struct it *it, enum display_element_type what)
24784 struct it temp_it;
24785 Lisp_Object gc;
24786 GLYPH glyph;
24788 temp_it = *it;
24789 temp_it.object = make_number (0);
24790 memset (&temp_it.current, 0, sizeof temp_it.current);
24792 if (what == IT_CONTINUATION)
24794 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24795 if (it->bidi_it.paragraph_dir == R2L)
24796 SET_GLYPH_FROM_CHAR (glyph, '/');
24797 else
24798 SET_GLYPH_FROM_CHAR (glyph, '\\');
24799 if (it->dp
24800 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24802 /* FIXME: Should we mirror GC for R2L lines? */
24803 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24804 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24807 else if (what == IT_TRUNCATION)
24809 /* Truncation glyph. */
24810 SET_GLYPH_FROM_CHAR (glyph, '$');
24811 if (it->dp
24812 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24814 /* FIXME: Should we mirror GC for R2L lines? */
24815 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24816 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24819 else
24820 emacs_abort ();
24822 #ifdef HAVE_WINDOW_SYSTEM
24823 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24824 is turned off, we precede the truncation/continuation glyphs by a
24825 stretch glyph whose width is computed such that these special
24826 glyphs are aligned at the window margin, even when very different
24827 fonts are used in different glyph rows. */
24828 if (FRAME_WINDOW_P (temp_it.f)
24829 /* init_iterator calls this with it->glyph_row == NULL, and it
24830 wants only the pixel width of the truncation/continuation
24831 glyphs. */
24832 && temp_it.glyph_row
24833 /* insert_left_trunc_glyphs calls us at the beginning of the
24834 row, and it has its own calculation of the stretch glyph
24835 width. */
24836 && temp_it.glyph_row->used[TEXT_AREA] > 0
24837 && (temp_it.glyph_row->reversed_p
24838 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24839 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24841 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24843 if (stretch_width > 0)
24845 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24846 struct font *font =
24847 face->font ? face->font : FRAME_FONT (temp_it.f);
24848 int stretch_ascent =
24849 (((temp_it.ascent + temp_it.descent)
24850 * FONT_BASE (font)) / FONT_HEIGHT (font));
24852 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24853 temp_it.ascent + temp_it.descent,
24854 stretch_ascent);
24857 #endif
24859 temp_it.dp = NULL;
24860 temp_it.what = IT_CHARACTER;
24861 temp_it.len = 1;
24862 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24863 temp_it.face_id = GLYPH_FACE (glyph);
24864 temp_it.len = CHAR_BYTES (temp_it.c);
24866 PRODUCE_GLYPHS (&temp_it);
24867 it->pixel_width = temp_it.pixel_width;
24868 it->nglyphs = temp_it.pixel_width;
24871 #ifdef HAVE_WINDOW_SYSTEM
24873 /* Calculate line-height and line-spacing properties.
24874 An integer value specifies explicit pixel value.
24875 A float value specifies relative value to current face height.
24876 A cons (float . face-name) specifies relative value to
24877 height of specified face font.
24879 Returns height in pixels, or nil. */
24882 static Lisp_Object
24883 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24884 int boff, int override)
24886 Lisp_Object face_name = Qnil;
24887 int ascent, descent, height;
24889 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24890 return val;
24892 if (CONSP (val))
24894 face_name = XCAR (val);
24895 val = XCDR (val);
24896 if (!NUMBERP (val))
24897 val = make_number (1);
24898 if (NILP (face_name))
24900 height = it->ascent + it->descent;
24901 goto scale;
24905 if (NILP (face_name))
24907 font = FRAME_FONT (it->f);
24908 boff = FRAME_BASELINE_OFFSET (it->f);
24910 else if (EQ (face_name, Qt))
24912 override = 0;
24914 else
24916 int face_id;
24917 struct face *face;
24919 face_id = lookup_named_face (it->f, face_name, 0);
24920 if (face_id < 0)
24921 return make_number (-1);
24923 face = FACE_FROM_ID (it->f, face_id);
24924 font = face->font;
24925 if (font == NULL)
24926 return make_number (-1);
24927 boff = font->baseline_offset;
24928 if (font->vertical_centering)
24929 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24932 ascent = FONT_BASE (font) + boff;
24933 descent = FONT_DESCENT (font) - boff;
24935 if (override)
24937 it->override_ascent = ascent;
24938 it->override_descent = descent;
24939 it->override_boff = boff;
24942 height = ascent + descent;
24944 scale:
24945 if (FLOATP (val))
24946 height = (int)(XFLOAT_DATA (val) * height);
24947 else if (INTEGERP (val))
24948 height *= XINT (val);
24950 return make_number (height);
24954 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24955 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24956 and only if this is for a character for which no font was found.
24958 If the display method (it->glyphless_method) is
24959 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24960 length of the acronym or the hexadecimal string, UPPER_XOFF and
24961 UPPER_YOFF are pixel offsets for the upper part of the string,
24962 LOWER_XOFF and LOWER_YOFF are for the lower part.
24964 For the other display methods, LEN through LOWER_YOFF are zero. */
24966 static void
24967 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24968 short upper_xoff, short upper_yoff,
24969 short lower_xoff, short lower_yoff)
24971 struct glyph *glyph;
24972 enum glyph_row_area area = it->area;
24974 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24975 if (glyph < it->glyph_row->glyphs[area + 1])
24977 /* If the glyph row is reversed, we need to prepend the glyph
24978 rather than append it. */
24979 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24981 struct glyph *g;
24983 /* Make room for the additional glyph. */
24984 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24985 g[1] = *g;
24986 glyph = it->glyph_row->glyphs[area];
24988 glyph->charpos = CHARPOS (it->position);
24989 glyph->object = it->object;
24990 glyph->pixel_width = it->pixel_width;
24991 glyph->ascent = it->ascent;
24992 glyph->descent = it->descent;
24993 glyph->voffset = it->voffset;
24994 glyph->type = GLYPHLESS_GLYPH;
24995 glyph->u.glyphless.method = it->glyphless_method;
24996 glyph->u.glyphless.for_no_font = for_no_font;
24997 glyph->u.glyphless.len = len;
24998 glyph->u.glyphless.ch = it->c;
24999 glyph->slice.glyphless.upper_xoff = upper_xoff;
25000 glyph->slice.glyphless.upper_yoff = upper_yoff;
25001 glyph->slice.glyphless.lower_xoff = lower_xoff;
25002 glyph->slice.glyphless.lower_yoff = lower_yoff;
25003 glyph->avoid_cursor_p = it->avoid_cursor_p;
25004 glyph->multibyte_p = it->multibyte_p;
25005 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25007 /* In R2L rows, the left and the right box edges need to be
25008 drawn in reverse direction. */
25009 glyph->right_box_line_p = it->start_of_box_run_p;
25010 glyph->left_box_line_p = it->end_of_box_run_p;
25012 else
25014 glyph->left_box_line_p = it->start_of_box_run_p;
25015 glyph->right_box_line_p = it->end_of_box_run_p;
25017 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25018 || it->phys_descent > it->descent);
25019 glyph->padding_p = 0;
25020 glyph->glyph_not_available_p = 0;
25021 glyph->face_id = face_id;
25022 glyph->font_type = FONT_TYPE_UNKNOWN;
25023 if (it->bidi_p)
25025 glyph->resolved_level = it->bidi_it.resolved_level;
25026 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25027 emacs_abort ();
25028 glyph->bidi_type = it->bidi_it.type;
25030 ++it->glyph_row->used[area];
25032 else
25033 IT_EXPAND_MATRIX_WIDTH (it, area);
25037 /* Produce a glyph for a glyphless character for iterator IT.
25038 IT->glyphless_method specifies which method to use for displaying
25039 the character. See the description of enum
25040 glyphless_display_method in dispextern.h for the detail.
25042 FOR_NO_FONT is nonzero if and only if this is for a character for
25043 which no font was found. ACRONYM, if non-nil, is an acronym string
25044 for the character. */
25046 static void
25047 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25049 int face_id;
25050 struct face *face;
25051 struct font *font;
25052 int base_width, base_height, width, height;
25053 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25054 int len;
25056 /* Get the metrics of the base font. We always refer to the current
25057 ASCII face. */
25058 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25059 font = face->font ? face->font : FRAME_FONT (it->f);
25060 it->ascent = FONT_BASE (font) + font->baseline_offset;
25061 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25062 base_height = it->ascent + it->descent;
25063 base_width = font->average_width;
25065 face_id = merge_glyphless_glyph_face (it);
25067 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25069 it->pixel_width = THIN_SPACE_WIDTH;
25070 len = 0;
25071 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25073 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25075 width = CHAR_WIDTH (it->c);
25076 if (width == 0)
25077 width = 1;
25078 else if (width > 4)
25079 width = 4;
25080 it->pixel_width = base_width * width;
25081 len = 0;
25082 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25084 else
25086 char buf[7];
25087 const char *str;
25088 unsigned int code[6];
25089 int upper_len;
25090 int ascent, descent;
25091 struct font_metrics metrics_upper, metrics_lower;
25093 face = FACE_FROM_ID (it->f, face_id);
25094 font = face->font ? face->font : FRAME_FONT (it->f);
25095 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25097 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25099 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25100 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25101 if (CONSP (acronym))
25102 acronym = XCAR (acronym);
25103 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25105 else
25107 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25108 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25109 str = buf;
25111 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25112 code[len] = font->driver->encode_char (font, str[len]);
25113 upper_len = (len + 1) / 2;
25114 font->driver->text_extents (font, code, upper_len,
25115 &metrics_upper);
25116 font->driver->text_extents (font, code + upper_len, len - upper_len,
25117 &metrics_lower);
25121 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25122 width = max (metrics_upper.width, metrics_lower.width) + 4;
25123 upper_xoff = upper_yoff = 2; /* the typical case */
25124 if (base_width >= width)
25126 /* Align the upper to the left, the lower to the right. */
25127 it->pixel_width = base_width;
25128 lower_xoff = base_width - 2 - metrics_lower.width;
25130 else
25132 /* Center the shorter one. */
25133 it->pixel_width = width;
25134 if (metrics_upper.width >= metrics_lower.width)
25135 lower_xoff = (width - metrics_lower.width) / 2;
25136 else
25138 /* FIXME: This code doesn't look right. It formerly was
25139 missing the "lower_xoff = 0;", which couldn't have
25140 been right since it left lower_xoff uninitialized. */
25141 lower_xoff = 0;
25142 upper_xoff = (width - metrics_upper.width) / 2;
25146 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25147 top, bottom, and between upper and lower strings. */
25148 height = (metrics_upper.ascent + metrics_upper.descent
25149 + metrics_lower.ascent + metrics_lower.descent) + 5;
25150 /* Center vertically.
25151 H:base_height, D:base_descent
25152 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25154 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25155 descent = D - H/2 + h/2;
25156 lower_yoff = descent - 2 - ld;
25157 upper_yoff = lower_yoff - la - 1 - ud; */
25158 ascent = - (it->descent - (base_height + height + 1) / 2);
25159 descent = it->descent - (base_height - height) / 2;
25160 lower_yoff = descent - 2 - metrics_lower.descent;
25161 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25162 - metrics_upper.descent);
25163 /* Don't make the height shorter than the base height. */
25164 if (height > base_height)
25166 it->ascent = ascent;
25167 it->descent = descent;
25171 it->phys_ascent = it->ascent;
25172 it->phys_descent = it->descent;
25173 if (it->glyph_row)
25174 append_glyphless_glyph (it, face_id, for_no_font, len,
25175 upper_xoff, upper_yoff,
25176 lower_xoff, lower_yoff);
25177 it->nglyphs = 1;
25178 take_vertical_position_into_account (it);
25182 /* RIF:
25183 Produce glyphs/get display metrics for the display element IT is
25184 loaded with. See the description of struct it in dispextern.h
25185 for an overview of struct it. */
25187 void
25188 x_produce_glyphs (struct it *it)
25190 int extra_line_spacing = it->extra_line_spacing;
25192 it->glyph_not_available_p = 0;
25194 if (it->what == IT_CHARACTER)
25196 XChar2b char2b;
25197 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25198 struct font *font = face->font;
25199 struct font_metrics *pcm = NULL;
25200 int boff; /* Baseline offset. */
25202 if (font == NULL)
25204 /* When no suitable font is found, display this character by
25205 the method specified in the first extra slot of
25206 Vglyphless_char_display. */
25207 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25209 eassert (it->what == IT_GLYPHLESS);
25210 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25211 goto done;
25214 boff = font->baseline_offset;
25215 if (font->vertical_centering)
25216 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25218 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25220 int stretched_p;
25222 it->nglyphs = 1;
25224 if (it->override_ascent >= 0)
25226 it->ascent = it->override_ascent;
25227 it->descent = it->override_descent;
25228 boff = it->override_boff;
25230 else
25232 it->ascent = FONT_BASE (font) + boff;
25233 it->descent = FONT_DESCENT (font) - boff;
25236 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25238 pcm = get_per_char_metric (font, &char2b);
25239 if (pcm->width == 0
25240 && pcm->rbearing == 0 && pcm->lbearing == 0)
25241 pcm = NULL;
25244 if (pcm)
25246 it->phys_ascent = pcm->ascent + boff;
25247 it->phys_descent = pcm->descent - boff;
25248 it->pixel_width = pcm->width;
25250 else
25252 it->glyph_not_available_p = 1;
25253 it->phys_ascent = it->ascent;
25254 it->phys_descent = it->descent;
25255 it->pixel_width = font->space_width;
25258 if (it->constrain_row_ascent_descent_p)
25260 if (it->descent > it->max_descent)
25262 it->ascent += it->descent - it->max_descent;
25263 it->descent = it->max_descent;
25265 if (it->ascent > it->max_ascent)
25267 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25268 it->ascent = it->max_ascent;
25270 it->phys_ascent = min (it->phys_ascent, it->ascent);
25271 it->phys_descent = min (it->phys_descent, it->descent);
25272 extra_line_spacing = 0;
25275 /* If this is a space inside a region of text with
25276 `space-width' property, change its width. */
25277 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25278 if (stretched_p)
25279 it->pixel_width *= XFLOATINT (it->space_width);
25281 /* If face has a box, add the box thickness to the character
25282 height. If character has a box line to the left and/or
25283 right, add the box line width to the character's width. */
25284 if (face->box != FACE_NO_BOX)
25286 int thick = face->box_line_width;
25288 if (thick > 0)
25290 it->ascent += thick;
25291 it->descent += thick;
25293 else
25294 thick = -thick;
25296 if (it->start_of_box_run_p)
25297 it->pixel_width += thick;
25298 if (it->end_of_box_run_p)
25299 it->pixel_width += thick;
25302 /* If face has an overline, add the height of the overline
25303 (1 pixel) and a 1 pixel margin to the character height. */
25304 if (face->overline_p)
25305 it->ascent += overline_margin;
25307 if (it->constrain_row_ascent_descent_p)
25309 if (it->ascent > it->max_ascent)
25310 it->ascent = it->max_ascent;
25311 if (it->descent > it->max_descent)
25312 it->descent = it->max_descent;
25315 take_vertical_position_into_account (it);
25317 /* If we have to actually produce glyphs, do it. */
25318 if (it->glyph_row)
25320 if (stretched_p)
25322 /* Translate a space with a `space-width' property
25323 into a stretch glyph. */
25324 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25325 / FONT_HEIGHT (font));
25326 append_stretch_glyph (it, it->object, it->pixel_width,
25327 it->ascent + it->descent, ascent);
25329 else
25330 append_glyph (it);
25332 /* If characters with lbearing or rbearing are displayed
25333 in this line, record that fact in a flag of the
25334 glyph row. This is used to optimize X output code. */
25335 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25336 it->glyph_row->contains_overlapping_glyphs_p = 1;
25338 if (! stretched_p && it->pixel_width == 0)
25339 /* We assure that all visible glyphs have at least 1-pixel
25340 width. */
25341 it->pixel_width = 1;
25343 else if (it->char_to_display == '\n')
25345 /* A newline has no width, but we need the height of the
25346 line. But if previous part of the line sets a height,
25347 don't increase that height. */
25349 Lisp_Object height;
25350 Lisp_Object total_height = Qnil;
25352 it->override_ascent = -1;
25353 it->pixel_width = 0;
25354 it->nglyphs = 0;
25356 height = get_it_property (it, Qline_height);
25357 /* Split (line-height total-height) list. */
25358 if (CONSP (height)
25359 && CONSP (XCDR (height))
25360 && NILP (XCDR (XCDR (height))))
25362 total_height = XCAR (XCDR (height));
25363 height = XCAR (height);
25365 height = calc_line_height_property (it, height, font, boff, 1);
25367 if (it->override_ascent >= 0)
25369 it->ascent = it->override_ascent;
25370 it->descent = it->override_descent;
25371 boff = it->override_boff;
25373 else
25375 it->ascent = FONT_BASE (font) + boff;
25376 it->descent = FONT_DESCENT (font) - boff;
25379 if (EQ (height, Qt))
25381 if (it->descent > it->max_descent)
25383 it->ascent += it->descent - it->max_descent;
25384 it->descent = it->max_descent;
25386 if (it->ascent > it->max_ascent)
25388 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25389 it->ascent = it->max_ascent;
25391 it->phys_ascent = min (it->phys_ascent, it->ascent);
25392 it->phys_descent = min (it->phys_descent, it->descent);
25393 it->constrain_row_ascent_descent_p = 1;
25394 extra_line_spacing = 0;
25396 else
25398 Lisp_Object spacing;
25400 it->phys_ascent = it->ascent;
25401 it->phys_descent = it->descent;
25403 if ((it->max_ascent > 0 || it->max_descent > 0)
25404 && face->box != FACE_NO_BOX
25405 && face->box_line_width > 0)
25407 it->ascent += face->box_line_width;
25408 it->descent += face->box_line_width;
25410 if (!NILP (height)
25411 && XINT (height) > it->ascent + it->descent)
25412 it->ascent = XINT (height) - it->descent;
25414 if (!NILP (total_height))
25415 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25416 else
25418 spacing = get_it_property (it, Qline_spacing);
25419 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25421 if (INTEGERP (spacing))
25423 extra_line_spacing = XINT (spacing);
25424 if (!NILP (total_height))
25425 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25429 else /* i.e. (it->char_to_display == '\t') */
25431 if (font->space_width > 0)
25433 int tab_width = it->tab_width * font->space_width;
25434 int x = it->current_x + it->continuation_lines_width;
25435 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25437 /* If the distance from the current position to the next tab
25438 stop is less than a space character width, use the
25439 tab stop after that. */
25440 if (next_tab_x - x < font->space_width)
25441 next_tab_x += tab_width;
25443 it->pixel_width = next_tab_x - x;
25444 it->nglyphs = 1;
25445 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25446 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25448 if (it->glyph_row)
25450 append_stretch_glyph (it, it->object, it->pixel_width,
25451 it->ascent + it->descent, it->ascent);
25454 else
25456 it->pixel_width = 0;
25457 it->nglyphs = 1;
25461 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25463 /* A static composition.
25465 Note: A composition is represented as one glyph in the
25466 glyph matrix. There are no padding glyphs.
25468 Important note: pixel_width, ascent, and descent are the
25469 values of what is drawn by draw_glyphs (i.e. the values of
25470 the overall glyphs composed). */
25471 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25472 int boff; /* baseline offset */
25473 struct composition *cmp = composition_table[it->cmp_it.id];
25474 int glyph_len = cmp->glyph_len;
25475 struct font *font = face->font;
25477 it->nglyphs = 1;
25479 /* If we have not yet calculated pixel size data of glyphs of
25480 the composition for the current face font, calculate them
25481 now. Theoretically, we have to check all fonts for the
25482 glyphs, but that requires much time and memory space. So,
25483 here we check only the font of the first glyph. This may
25484 lead to incorrect display, but it's very rare, and C-l
25485 (recenter-top-bottom) can correct the display anyway. */
25486 if (! cmp->font || cmp->font != font)
25488 /* Ascent and descent of the font of the first character
25489 of this composition (adjusted by baseline offset).
25490 Ascent and descent of overall glyphs should not be less
25491 than these, respectively. */
25492 int font_ascent, font_descent, font_height;
25493 /* Bounding box of the overall glyphs. */
25494 int leftmost, rightmost, lowest, highest;
25495 int lbearing, rbearing;
25496 int i, width, ascent, descent;
25497 int left_padded = 0, right_padded = 0;
25498 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25499 XChar2b char2b;
25500 struct font_metrics *pcm;
25501 int font_not_found_p;
25502 ptrdiff_t pos;
25504 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25505 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25506 break;
25507 if (glyph_len < cmp->glyph_len)
25508 right_padded = 1;
25509 for (i = 0; i < glyph_len; i++)
25511 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25512 break;
25513 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25515 if (i > 0)
25516 left_padded = 1;
25518 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25519 : IT_CHARPOS (*it));
25520 /* If no suitable font is found, use the default font. */
25521 font_not_found_p = font == NULL;
25522 if (font_not_found_p)
25524 face = face->ascii_face;
25525 font = face->font;
25527 boff = font->baseline_offset;
25528 if (font->vertical_centering)
25529 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25530 font_ascent = FONT_BASE (font) + boff;
25531 font_descent = FONT_DESCENT (font) - boff;
25532 font_height = FONT_HEIGHT (font);
25534 cmp->font = font;
25536 pcm = NULL;
25537 if (! font_not_found_p)
25539 get_char_face_and_encoding (it->f, c, it->face_id,
25540 &char2b, 0);
25541 pcm = get_per_char_metric (font, &char2b);
25544 /* Initialize the bounding box. */
25545 if (pcm)
25547 width = cmp->glyph_len > 0 ? pcm->width : 0;
25548 ascent = pcm->ascent;
25549 descent = pcm->descent;
25550 lbearing = pcm->lbearing;
25551 rbearing = pcm->rbearing;
25553 else
25555 width = cmp->glyph_len > 0 ? font->space_width : 0;
25556 ascent = FONT_BASE (font);
25557 descent = FONT_DESCENT (font);
25558 lbearing = 0;
25559 rbearing = width;
25562 rightmost = width;
25563 leftmost = 0;
25564 lowest = - descent + boff;
25565 highest = ascent + boff;
25567 if (! font_not_found_p
25568 && font->default_ascent
25569 && CHAR_TABLE_P (Vuse_default_ascent)
25570 && !NILP (Faref (Vuse_default_ascent,
25571 make_number (it->char_to_display))))
25572 highest = font->default_ascent + boff;
25574 /* Draw the first glyph at the normal position. It may be
25575 shifted to right later if some other glyphs are drawn
25576 at the left. */
25577 cmp->offsets[i * 2] = 0;
25578 cmp->offsets[i * 2 + 1] = boff;
25579 cmp->lbearing = lbearing;
25580 cmp->rbearing = rbearing;
25582 /* Set cmp->offsets for the remaining glyphs. */
25583 for (i++; i < glyph_len; i++)
25585 int left, right, btm, top;
25586 int ch = COMPOSITION_GLYPH (cmp, i);
25587 int face_id;
25588 struct face *this_face;
25590 if (ch == '\t')
25591 ch = ' ';
25592 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25593 this_face = FACE_FROM_ID (it->f, face_id);
25594 font = this_face->font;
25596 if (font == NULL)
25597 pcm = NULL;
25598 else
25600 get_char_face_and_encoding (it->f, ch, face_id,
25601 &char2b, 0);
25602 pcm = get_per_char_metric (font, &char2b);
25604 if (! pcm)
25605 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25606 else
25608 width = pcm->width;
25609 ascent = pcm->ascent;
25610 descent = pcm->descent;
25611 lbearing = pcm->lbearing;
25612 rbearing = pcm->rbearing;
25613 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25615 /* Relative composition with or without
25616 alternate chars. */
25617 left = (leftmost + rightmost - width) / 2;
25618 btm = - descent + boff;
25619 if (font->relative_compose
25620 && (! CHAR_TABLE_P (Vignore_relative_composition)
25621 || NILP (Faref (Vignore_relative_composition,
25622 make_number (ch)))))
25625 if (- descent >= font->relative_compose)
25626 /* One extra pixel between two glyphs. */
25627 btm = highest + 1;
25628 else if (ascent <= 0)
25629 /* One extra pixel between two glyphs. */
25630 btm = lowest - 1 - ascent - descent;
25633 else
25635 /* A composition rule is specified by an integer
25636 value that encodes global and new reference
25637 points (GREF and NREF). GREF and NREF are
25638 specified by numbers as below:
25640 0---1---2 -- ascent
25644 9--10--11 -- center
25646 ---3---4---5--- baseline
25648 6---7---8 -- descent
25650 int rule = COMPOSITION_RULE (cmp, i);
25651 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25653 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25654 grefx = gref % 3, nrefx = nref % 3;
25655 grefy = gref / 3, nrefy = nref / 3;
25656 if (xoff)
25657 xoff = font_height * (xoff - 128) / 256;
25658 if (yoff)
25659 yoff = font_height * (yoff - 128) / 256;
25661 left = (leftmost
25662 + grefx * (rightmost - leftmost) / 2
25663 - nrefx * width / 2
25664 + xoff);
25666 btm = ((grefy == 0 ? highest
25667 : grefy == 1 ? 0
25668 : grefy == 2 ? lowest
25669 : (highest + lowest) / 2)
25670 - (nrefy == 0 ? ascent + descent
25671 : nrefy == 1 ? descent - boff
25672 : nrefy == 2 ? 0
25673 : (ascent + descent) / 2)
25674 + yoff);
25677 cmp->offsets[i * 2] = left;
25678 cmp->offsets[i * 2 + 1] = btm + descent;
25680 /* Update the bounding box of the overall glyphs. */
25681 if (width > 0)
25683 right = left + width;
25684 if (left < leftmost)
25685 leftmost = left;
25686 if (right > rightmost)
25687 rightmost = right;
25689 top = btm + descent + ascent;
25690 if (top > highest)
25691 highest = top;
25692 if (btm < lowest)
25693 lowest = btm;
25695 if (cmp->lbearing > left + lbearing)
25696 cmp->lbearing = left + lbearing;
25697 if (cmp->rbearing < left + rbearing)
25698 cmp->rbearing = left + rbearing;
25702 /* If there are glyphs whose x-offsets are negative,
25703 shift all glyphs to the right and make all x-offsets
25704 non-negative. */
25705 if (leftmost < 0)
25707 for (i = 0; i < cmp->glyph_len; i++)
25708 cmp->offsets[i * 2] -= leftmost;
25709 rightmost -= leftmost;
25710 cmp->lbearing -= leftmost;
25711 cmp->rbearing -= leftmost;
25714 if (left_padded && cmp->lbearing < 0)
25716 for (i = 0; i < cmp->glyph_len; i++)
25717 cmp->offsets[i * 2] -= cmp->lbearing;
25718 rightmost -= cmp->lbearing;
25719 cmp->rbearing -= cmp->lbearing;
25720 cmp->lbearing = 0;
25722 if (right_padded && rightmost < cmp->rbearing)
25724 rightmost = cmp->rbearing;
25727 cmp->pixel_width = rightmost;
25728 cmp->ascent = highest;
25729 cmp->descent = - lowest;
25730 if (cmp->ascent < font_ascent)
25731 cmp->ascent = font_ascent;
25732 if (cmp->descent < font_descent)
25733 cmp->descent = font_descent;
25736 if (it->glyph_row
25737 && (cmp->lbearing < 0
25738 || cmp->rbearing > cmp->pixel_width))
25739 it->glyph_row->contains_overlapping_glyphs_p = 1;
25741 it->pixel_width = cmp->pixel_width;
25742 it->ascent = it->phys_ascent = cmp->ascent;
25743 it->descent = it->phys_descent = cmp->descent;
25744 if (face->box != FACE_NO_BOX)
25746 int thick = face->box_line_width;
25748 if (thick > 0)
25750 it->ascent += thick;
25751 it->descent += thick;
25753 else
25754 thick = - thick;
25756 if (it->start_of_box_run_p)
25757 it->pixel_width += thick;
25758 if (it->end_of_box_run_p)
25759 it->pixel_width += thick;
25762 /* If face has an overline, add the height of the overline
25763 (1 pixel) and a 1 pixel margin to the character height. */
25764 if (face->overline_p)
25765 it->ascent += overline_margin;
25767 take_vertical_position_into_account (it);
25768 if (it->ascent < 0)
25769 it->ascent = 0;
25770 if (it->descent < 0)
25771 it->descent = 0;
25773 if (it->glyph_row && cmp->glyph_len > 0)
25774 append_composite_glyph (it);
25776 else if (it->what == IT_COMPOSITION)
25778 /* A dynamic (automatic) composition. */
25779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25780 Lisp_Object gstring;
25781 struct font_metrics metrics;
25783 it->nglyphs = 1;
25785 gstring = composition_gstring_from_id (it->cmp_it.id);
25786 it->pixel_width
25787 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25788 &metrics);
25789 if (it->glyph_row
25790 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25791 it->glyph_row->contains_overlapping_glyphs_p = 1;
25792 it->ascent = it->phys_ascent = metrics.ascent;
25793 it->descent = it->phys_descent = metrics.descent;
25794 if (face->box != FACE_NO_BOX)
25796 int thick = face->box_line_width;
25798 if (thick > 0)
25800 it->ascent += thick;
25801 it->descent += thick;
25803 else
25804 thick = - thick;
25806 if (it->start_of_box_run_p)
25807 it->pixel_width += thick;
25808 if (it->end_of_box_run_p)
25809 it->pixel_width += thick;
25811 /* If face has an overline, add the height of the overline
25812 (1 pixel) and a 1 pixel margin to the character height. */
25813 if (face->overline_p)
25814 it->ascent += overline_margin;
25815 take_vertical_position_into_account (it);
25816 if (it->ascent < 0)
25817 it->ascent = 0;
25818 if (it->descent < 0)
25819 it->descent = 0;
25821 if (it->glyph_row)
25822 append_composite_glyph (it);
25824 else if (it->what == IT_GLYPHLESS)
25825 produce_glyphless_glyph (it, 0, Qnil);
25826 else if (it->what == IT_IMAGE)
25827 produce_image_glyph (it);
25828 else if (it->what == IT_STRETCH)
25829 produce_stretch_glyph (it);
25831 done:
25832 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25833 because this isn't true for images with `:ascent 100'. */
25834 eassert (it->ascent >= 0 && it->descent >= 0);
25835 if (it->area == TEXT_AREA)
25836 it->current_x += it->pixel_width;
25838 if (extra_line_spacing > 0)
25840 it->descent += extra_line_spacing;
25841 if (extra_line_spacing > it->max_extra_line_spacing)
25842 it->max_extra_line_spacing = extra_line_spacing;
25845 it->max_ascent = max (it->max_ascent, it->ascent);
25846 it->max_descent = max (it->max_descent, it->descent);
25847 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25848 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25851 /* EXPORT for RIF:
25852 Output LEN glyphs starting at START at the nominal cursor position.
25853 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25854 being updated, and UPDATED_AREA is the area of that row being updated. */
25856 void
25857 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25858 struct glyph *start, enum glyph_row_area updated_area, int len)
25860 int x, hpos, chpos = w->phys_cursor.hpos;
25862 eassert (updated_row);
25863 /* When the window is hscrolled, cursor hpos can legitimately be out
25864 of bounds, but we draw the cursor at the corresponding window
25865 margin in that case. */
25866 if (!updated_row->reversed_p && chpos < 0)
25867 chpos = 0;
25868 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25869 chpos = updated_row->used[TEXT_AREA] - 1;
25871 block_input ();
25873 /* Write glyphs. */
25875 hpos = start - updated_row->glyphs[updated_area];
25876 x = draw_glyphs (w, w->output_cursor.x,
25877 updated_row, updated_area,
25878 hpos, hpos + len,
25879 DRAW_NORMAL_TEXT, 0);
25881 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25882 if (updated_area == TEXT_AREA
25883 && w->phys_cursor_on_p
25884 && w->phys_cursor.vpos == w->output_cursor.vpos
25885 && chpos >= hpos
25886 && chpos < hpos + len)
25887 w->phys_cursor_on_p = 0;
25889 unblock_input ();
25891 /* Advance the output cursor. */
25892 w->output_cursor.hpos += len;
25893 w->output_cursor.x = x;
25897 /* EXPORT for RIF:
25898 Insert LEN glyphs from START at the nominal cursor position. */
25900 void
25901 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25902 struct glyph *start, enum glyph_row_area updated_area, int len)
25904 struct frame *f;
25905 int line_height, shift_by_width, shifted_region_width;
25906 struct glyph_row *row;
25907 struct glyph *glyph;
25908 int frame_x, frame_y;
25909 ptrdiff_t hpos;
25911 eassert (updated_row);
25912 block_input ();
25913 f = XFRAME (WINDOW_FRAME (w));
25915 /* Get the height of the line we are in. */
25916 row = updated_row;
25917 line_height = row->height;
25919 /* Get the width of the glyphs to insert. */
25920 shift_by_width = 0;
25921 for (glyph = start; glyph < start + len; ++glyph)
25922 shift_by_width += glyph->pixel_width;
25924 /* Get the width of the region to shift right. */
25925 shifted_region_width = (window_box_width (w, updated_area)
25926 - w->output_cursor.x
25927 - shift_by_width);
25929 /* Shift right. */
25930 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25931 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25933 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25934 line_height, shift_by_width);
25936 /* Write the glyphs. */
25937 hpos = start - row->glyphs[updated_area];
25938 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25939 hpos, hpos + len,
25940 DRAW_NORMAL_TEXT, 0);
25942 /* Advance the output cursor. */
25943 w->output_cursor.hpos += len;
25944 w->output_cursor.x += shift_by_width;
25945 unblock_input ();
25949 /* EXPORT for RIF:
25950 Erase the current text line from the nominal cursor position
25951 (inclusive) to pixel column TO_X (exclusive). The idea is that
25952 everything from TO_X onward is already erased.
25954 TO_X is a pixel position relative to UPDATED_AREA of currently
25955 updated window W. TO_X == -1 means clear to the end of this area. */
25957 void
25958 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25959 enum glyph_row_area updated_area, int to_x)
25961 struct frame *f;
25962 int max_x, min_y, max_y;
25963 int from_x, from_y, to_y;
25965 eassert (updated_row);
25966 f = XFRAME (w->frame);
25968 if (updated_row->full_width_p)
25969 max_x = WINDOW_TOTAL_WIDTH (w);
25970 else
25971 max_x = window_box_width (w, updated_area);
25972 max_y = window_text_bottom_y (w);
25974 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25975 of window. For TO_X > 0, truncate to end of drawing area. */
25976 if (to_x == 0)
25977 return;
25978 else if (to_x < 0)
25979 to_x = max_x;
25980 else
25981 to_x = min (to_x, max_x);
25983 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25985 /* Notice if the cursor will be cleared by this operation. */
25986 if (!updated_row->full_width_p)
25987 notice_overwritten_cursor (w, updated_area,
25988 w->output_cursor.x, -1,
25989 updated_row->y,
25990 MATRIX_ROW_BOTTOM_Y (updated_row));
25992 from_x = w->output_cursor.x;
25994 /* Translate to frame coordinates. */
25995 if (updated_row->full_width_p)
25997 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25998 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26000 else
26002 int area_left = window_box_left (w, updated_area);
26003 from_x += area_left;
26004 to_x += area_left;
26007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26008 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26009 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26011 /* Prevent inadvertently clearing to end of the X window. */
26012 if (to_x > from_x && to_y > from_y)
26014 block_input ();
26015 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26016 to_x - from_x, to_y - from_y);
26017 unblock_input ();
26021 #endif /* HAVE_WINDOW_SYSTEM */
26025 /***********************************************************************
26026 Cursor types
26027 ***********************************************************************/
26029 /* Value is the internal representation of the specified cursor type
26030 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26031 of the bar cursor. */
26033 static enum text_cursor_kinds
26034 get_specified_cursor_type (Lisp_Object arg, int *width)
26036 enum text_cursor_kinds type;
26038 if (NILP (arg))
26039 return NO_CURSOR;
26041 if (EQ (arg, Qbox))
26042 return FILLED_BOX_CURSOR;
26044 if (EQ (arg, Qhollow))
26045 return HOLLOW_BOX_CURSOR;
26047 if (EQ (arg, Qbar))
26049 *width = 2;
26050 return BAR_CURSOR;
26053 if (CONSP (arg)
26054 && EQ (XCAR (arg), Qbar)
26055 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26057 *width = XINT (XCDR (arg));
26058 return BAR_CURSOR;
26061 if (EQ (arg, Qhbar))
26063 *width = 2;
26064 return HBAR_CURSOR;
26067 if (CONSP (arg)
26068 && EQ (XCAR (arg), Qhbar)
26069 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26071 *width = XINT (XCDR (arg));
26072 return HBAR_CURSOR;
26075 /* Treat anything unknown as "hollow box cursor".
26076 It was bad to signal an error; people have trouble fixing
26077 .Xdefaults with Emacs, when it has something bad in it. */
26078 type = HOLLOW_BOX_CURSOR;
26080 return type;
26083 /* Set the default cursor types for specified frame. */
26084 void
26085 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26087 int width = 1;
26088 Lisp_Object tem;
26090 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26091 FRAME_CURSOR_WIDTH (f) = width;
26093 /* By default, set up the blink-off state depending on the on-state. */
26095 tem = Fassoc (arg, Vblink_cursor_alist);
26096 if (!NILP (tem))
26098 FRAME_BLINK_OFF_CURSOR (f)
26099 = get_specified_cursor_type (XCDR (tem), &width);
26100 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26102 else
26103 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26105 /* Make sure the cursor gets redrawn. */
26106 f->cursor_type_changed = 1;
26110 #ifdef HAVE_WINDOW_SYSTEM
26112 /* Return the cursor we want to be displayed in window W. Return
26113 width of bar/hbar cursor through WIDTH arg. Return with
26114 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26115 (i.e. if the `system caret' should track this cursor).
26117 In a mini-buffer window, we want the cursor only to appear if we
26118 are reading input from this window. For the selected window, we
26119 want the cursor type given by the frame parameter or buffer local
26120 setting of cursor-type. If explicitly marked off, draw no cursor.
26121 In all other cases, we want a hollow box cursor. */
26123 static enum text_cursor_kinds
26124 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26125 int *active_cursor)
26127 struct frame *f = XFRAME (w->frame);
26128 struct buffer *b = XBUFFER (w->contents);
26129 int cursor_type = DEFAULT_CURSOR;
26130 Lisp_Object alt_cursor;
26131 int non_selected = 0;
26133 *active_cursor = 1;
26135 /* Echo area */
26136 if (cursor_in_echo_area
26137 && FRAME_HAS_MINIBUF_P (f)
26138 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26140 if (w == XWINDOW (echo_area_window))
26142 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26144 *width = FRAME_CURSOR_WIDTH (f);
26145 return FRAME_DESIRED_CURSOR (f);
26147 else
26148 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26151 *active_cursor = 0;
26152 non_selected = 1;
26155 /* Detect a nonselected window or nonselected frame. */
26156 else if (w != XWINDOW (f->selected_window)
26157 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26159 *active_cursor = 0;
26161 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26162 return NO_CURSOR;
26164 non_selected = 1;
26167 /* Never display a cursor in a window in which cursor-type is nil. */
26168 if (NILP (BVAR (b, cursor_type)))
26169 return NO_CURSOR;
26171 /* Get the normal cursor type for this window. */
26172 if (EQ (BVAR (b, cursor_type), Qt))
26174 cursor_type = FRAME_DESIRED_CURSOR (f);
26175 *width = FRAME_CURSOR_WIDTH (f);
26177 else
26178 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26180 /* Use cursor-in-non-selected-windows instead
26181 for non-selected window or frame. */
26182 if (non_selected)
26184 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26185 if (!EQ (Qt, alt_cursor))
26186 return get_specified_cursor_type (alt_cursor, width);
26187 /* t means modify the normal cursor type. */
26188 if (cursor_type == FILLED_BOX_CURSOR)
26189 cursor_type = HOLLOW_BOX_CURSOR;
26190 else if (cursor_type == BAR_CURSOR && *width > 1)
26191 --*width;
26192 return cursor_type;
26195 /* Use normal cursor if not blinked off. */
26196 if (!w->cursor_off_p)
26198 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26200 if (cursor_type == FILLED_BOX_CURSOR)
26202 /* Using a block cursor on large images can be very annoying.
26203 So use a hollow cursor for "large" images.
26204 If image is not transparent (no mask), also use hollow cursor. */
26205 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26206 if (img != NULL && IMAGEP (img->spec))
26208 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26209 where N = size of default frame font size.
26210 This should cover most of the "tiny" icons people may use. */
26211 if (!img->mask
26212 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26213 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26214 cursor_type = HOLLOW_BOX_CURSOR;
26217 else if (cursor_type != NO_CURSOR)
26219 /* Display current only supports BOX and HOLLOW cursors for images.
26220 So for now, unconditionally use a HOLLOW cursor when cursor is
26221 not a solid box cursor. */
26222 cursor_type = HOLLOW_BOX_CURSOR;
26225 return cursor_type;
26228 /* Cursor is blinked off, so determine how to "toggle" it. */
26230 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26231 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26232 return get_specified_cursor_type (XCDR (alt_cursor), width);
26234 /* Then see if frame has specified a specific blink off cursor type. */
26235 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26237 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26238 return FRAME_BLINK_OFF_CURSOR (f);
26241 #if 0
26242 /* Some people liked having a permanently visible blinking cursor,
26243 while others had very strong opinions against it. So it was
26244 decided to remove it. KFS 2003-09-03 */
26246 /* Finally perform built-in cursor blinking:
26247 filled box <-> hollow box
26248 wide [h]bar <-> narrow [h]bar
26249 narrow [h]bar <-> no cursor
26250 other type <-> no cursor */
26252 if (cursor_type == FILLED_BOX_CURSOR)
26253 return HOLLOW_BOX_CURSOR;
26255 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26257 *width = 1;
26258 return cursor_type;
26260 #endif
26262 return NO_CURSOR;
26266 /* Notice when the text cursor of window W has been completely
26267 overwritten by a drawing operation that outputs glyphs in AREA
26268 starting at X0 and ending at X1 in the line starting at Y0 and
26269 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26270 the rest of the line after X0 has been written. Y coordinates
26271 are window-relative. */
26273 static void
26274 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26275 int x0, int x1, int y0, int y1)
26277 int cx0, cx1, cy0, cy1;
26278 struct glyph_row *row;
26280 if (!w->phys_cursor_on_p)
26281 return;
26282 if (area != TEXT_AREA)
26283 return;
26285 if (w->phys_cursor.vpos < 0
26286 || w->phys_cursor.vpos >= w->current_matrix->nrows
26287 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26288 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26289 return;
26291 if (row->cursor_in_fringe_p)
26293 row->cursor_in_fringe_p = 0;
26294 draw_fringe_bitmap (w, row, row->reversed_p);
26295 w->phys_cursor_on_p = 0;
26296 return;
26299 cx0 = w->phys_cursor.x;
26300 cx1 = cx0 + w->phys_cursor_width;
26301 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26302 return;
26304 /* The cursor image will be completely removed from the
26305 screen if the output area intersects the cursor area in
26306 y-direction. When we draw in [y0 y1[, and some part of
26307 the cursor is at y < y0, that part must have been drawn
26308 before. When scrolling, the cursor is erased before
26309 actually scrolling, so we don't come here. When not
26310 scrolling, the rows above the old cursor row must have
26311 changed, and in this case these rows must have written
26312 over the cursor image.
26314 Likewise if part of the cursor is below y1, with the
26315 exception of the cursor being in the first blank row at
26316 the buffer and window end because update_text_area
26317 doesn't draw that row. (Except when it does, but
26318 that's handled in update_text_area.) */
26320 cy0 = w->phys_cursor.y;
26321 cy1 = cy0 + w->phys_cursor_height;
26322 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26323 return;
26325 w->phys_cursor_on_p = 0;
26328 #endif /* HAVE_WINDOW_SYSTEM */
26331 /************************************************************************
26332 Mouse Face
26333 ************************************************************************/
26335 #ifdef HAVE_WINDOW_SYSTEM
26337 /* EXPORT for RIF:
26338 Fix the display of area AREA of overlapping row ROW in window W
26339 with respect to the overlapping part OVERLAPS. */
26341 void
26342 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26343 enum glyph_row_area area, int overlaps)
26345 int i, x;
26347 block_input ();
26349 x = 0;
26350 for (i = 0; i < row->used[area];)
26352 if (row->glyphs[area][i].overlaps_vertically_p)
26354 int start = i, start_x = x;
26358 x += row->glyphs[area][i].pixel_width;
26359 ++i;
26361 while (i < row->used[area]
26362 && row->glyphs[area][i].overlaps_vertically_p);
26364 draw_glyphs (w, start_x, row, area,
26365 start, i,
26366 DRAW_NORMAL_TEXT, overlaps);
26368 else
26370 x += row->glyphs[area][i].pixel_width;
26371 ++i;
26375 unblock_input ();
26379 /* EXPORT:
26380 Draw the cursor glyph of window W in glyph row ROW. See the
26381 comment of draw_glyphs for the meaning of HL. */
26383 void
26384 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26385 enum draw_glyphs_face hl)
26387 /* If cursor hpos is out of bounds, don't draw garbage. This can
26388 happen in mini-buffer windows when switching between echo area
26389 glyphs and mini-buffer. */
26390 if ((row->reversed_p
26391 ? (w->phys_cursor.hpos >= 0)
26392 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26394 int on_p = w->phys_cursor_on_p;
26395 int x1;
26396 int hpos = w->phys_cursor.hpos;
26398 /* When the window is hscrolled, cursor hpos can legitimately be
26399 out of bounds, but we draw the cursor at the corresponding
26400 window margin in that case. */
26401 if (!row->reversed_p && hpos < 0)
26402 hpos = 0;
26403 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26404 hpos = row->used[TEXT_AREA] - 1;
26406 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26407 hl, 0);
26408 w->phys_cursor_on_p = on_p;
26410 if (hl == DRAW_CURSOR)
26411 w->phys_cursor_width = x1 - w->phys_cursor.x;
26412 /* When we erase the cursor, and ROW is overlapped by other
26413 rows, make sure that these overlapping parts of other rows
26414 are redrawn. */
26415 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26417 w->phys_cursor_width = x1 - w->phys_cursor.x;
26419 if (row > w->current_matrix->rows
26420 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26421 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26422 OVERLAPS_ERASED_CURSOR);
26424 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26425 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26426 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26427 OVERLAPS_ERASED_CURSOR);
26433 /* Erase the image of a cursor of window W from the screen. */
26435 #ifndef HAVE_NTGUI
26436 static
26437 #endif
26438 void
26439 erase_phys_cursor (struct window *w)
26441 struct frame *f = XFRAME (w->frame);
26442 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26443 int hpos = w->phys_cursor.hpos;
26444 int vpos = w->phys_cursor.vpos;
26445 int mouse_face_here_p = 0;
26446 struct glyph_matrix *active_glyphs = w->current_matrix;
26447 struct glyph_row *cursor_row;
26448 struct glyph *cursor_glyph;
26449 enum draw_glyphs_face hl;
26451 /* No cursor displayed or row invalidated => nothing to do on the
26452 screen. */
26453 if (w->phys_cursor_type == NO_CURSOR)
26454 goto mark_cursor_off;
26456 /* VPOS >= active_glyphs->nrows means that window has been resized.
26457 Don't bother to erase the cursor. */
26458 if (vpos >= active_glyphs->nrows)
26459 goto mark_cursor_off;
26461 /* If row containing cursor is marked invalid, there is nothing we
26462 can do. */
26463 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26464 if (!cursor_row->enabled_p)
26465 goto mark_cursor_off;
26467 /* If line spacing is > 0, old cursor may only be partially visible in
26468 window after split-window. So adjust visible height. */
26469 cursor_row->visible_height = min (cursor_row->visible_height,
26470 window_text_bottom_y (w) - cursor_row->y);
26472 /* If row is completely invisible, don't attempt to delete a cursor which
26473 isn't there. This can happen if cursor is at top of a window, and
26474 we switch to a buffer with a header line in that window. */
26475 if (cursor_row->visible_height <= 0)
26476 goto mark_cursor_off;
26478 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26479 if (cursor_row->cursor_in_fringe_p)
26481 cursor_row->cursor_in_fringe_p = 0;
26482 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26483 goto mark_cursor_off;
26486 /* This can happen when the new row is shorter than the old one.
26487 In this case, either draw_glyphs or clear_end_of_line
26488 should have cleared the cursor. Note that we wouldn't be
26489 able to erase the cursor in this case because we don't have a
26490 cursor glyph at hand. */
26491 if ((cursor_row->reversed_p
26492 ? (w->phys_cursor.hpos < 0)
26493 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26494 goto mark_cursor_off;
26496 /* When the window is hscrolled, cursor hpos can legitimately be out
26497 of bounds, but we draw the cursor at the corresponding window
26498 margin in that case. */
26499 if (!cursor_row->reversed_p && hpos < 0)
26500 hpos = 0;
26501 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26502 hpos = cursor_row->used[TEXT_AREA] - 1;
26504 /* If the cursor is in the mouse face area, redisplay that when
26505 we clear the cursor. */
26506 if (! NILP (hlinfo->mouse_face_window)
26507 && coords_in_mouse_face_p (w, hpos, vpos)
26508 /* Don't redraw the cursor's spot in mouse face if it is at the
26509 end of a line (on a newline). The cursor appears there, but
26510 mouse highlighting does not. */
26511 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26512 mouse_face_here_p = 1;
26514 /* Maybe clear the display under the cursor. */
26515 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26517 int x, y, left_x;
26518 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26519 int width;
26521 cursor_glyph = get_phys_cursor_glyph (w);
26522 if (cursor_glyph == NULL)
26523 goto mark_cursor_off;
26525 width = cursor_glyph->pixel_width;
26526 left_x = window_box_left_offset (w, TEXT_AREA);
26527 x = w->phys_cursor.x;
26528 if (x < left_x)
26529 width -= left_x - x;
26530 width = min (width, window_box_width (w, TEXT_AREA) - x);
26531 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26532 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26534 if (width > 0)
26535 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26538 /* Erase the cursor by redrawing the character underneath it. */
26539 if (mouse_face_here_p)
26540 hl = DRAW_MOUSE_FACE;
26541 else
26542 hl = DRAW_NORMAL_TEXT;
26543 draw_phys_cursor_glyph (w, cursor_row, hl);
26545 mark_cursor_off:
26546 w->phys_cursor_on_p = 0;
26547 w->phys_cursor_type = NO_CURSOR;
26551 /* EXPORT:
26552 Display or clear cursor of window W. If ON is zero, clear the
26553 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26554 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26556 void
26557 display_and_set_cursor (struct window *w, bool on,
26558 int hpos, int vpos, int x, int y)
26560 struct frame *f = XFRAME (w->frame);
26561 int new_cursor_type;
26562 int new_cursor_width;
26563 int active_cursor;
26564 struct glyph_row *glyph_row;
26565 struct glyph *glyph;
26567 /* This is pointless on invisible frames, and dangerous on garbaged
26568 windows and frames; in the latter case, the frame or window may
26569 be in the midst of changing its size, and x and y may be off the
26570 window. */
26571 if (! FRAME_VISIBLE_P (f)
26572 || FRAME_GARBAGED_P (f)
26573 || vpos >= w->current_matrix->nrows
26574 || hpos >= w->current_matrix->matrix_w)
26575 return;
26577 /* If cursor is off and we want it off, return quickly. */
26578 if (!on && !w->phys_cursor_on_p)
26579 return;
26581 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26582 /* If cursor row is not enabled, we don't really know where to
26583 display the cursor. */
26584 if (!glyph_row->enabled_p)
26586 w->phys_cursor_on_p = 0;
26587 return;
26590 glyph = NULL;
26591 if (!glyph_row->exact_window_width_line_p
26592 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26593 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26595 eassert (input_blocked_p ());
26597 /* Set new_cursor_type to the cursor we want to be displayed. */
26598 new_cursor_type = get_window_cursor_type (w, glyph,
26599 &new_cursor_width, &active_cursor);
26601 /* If cursor is currently being shown and we don't want it to be or
26602 it is in the wrong place, or the cursor type is not what we want,
26603 erase it. */
26604 if (w->phys_cursor_on_p
26605 && (!on
26606 || w->phys_cursor.x != x
26607 || w->phys_cursor.y != y
26608 || new_cursor_type != w->phys_cursor_type
26609 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26610 && new_cursor_width != w->phys_cursor_width)))
26611 erase_phys_cursor (w);
26613 /* Don't check phys_cursor_on_p here because that flag is only set
26614 to zero in some cases where we know that the cursor has been
26615 completely erased, to avoid the extra work of erasing the cursor
26616 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26617 still not be visible, or it has only been partly erased. */
26618 if (on)
26620 w->phys_cursor_ascent = glyph_row->ascent;
26621 w->phys_cursor_height = glyph_row->height;
26623 /* Set phys_cursor_.* before x_draw_.* is called because some
26624 of them may need the information. */
26625 w->phys_cursor.x = x;
26626 w->phys_cursor.y = glyph_row->y;
26627 w->phys_cursor.hpos = hpos;
26628 w->phys_cursor.vpos = vpos;
26631 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26632 new_cursor_type, new_cursor_width,
26633 on, active_cursor);
26637 /* Switch the display of W's cursor on or off, according to the value
26638 of ON. */
26640 static void
26641 update_window_cursor (struct window *w, bool on)
26643 /* Don't update cursor in windows whose frame is in the process
26644 of being deleted. */
26645 if (w->current_matrix)
26647 int hpos = w->phys_cursor.hpos;
26648 int vpos = w->phys_cursor.vpos;
26649 struct glyph_row *row;
26651 if (vpos >= w->current_matrix->nrows
26652 || hpos >= w->current_matrix->matrix_w)
26653 return;
26655 row = MATRIX_ROW (w->current_matrix, vpos);
26657 /* When the window is hscrolled, cursor hpos can legitimately be
26658 out of bounds, but we draw the cursor at the corresponding
26659 window margin in that case. */
26660 if (!row->reversed_p && hpos < 0)
26661 hpos = 0;
26662 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26663 hpos = row->used[TEXT_AREA] - 1;
26665 block_input ();
26666 display_and_set_cursor (w, on, hpos, vpos,
26667 w->phys_cursor.x, w->phys_cursor.y);
26668 unblock_input ();
26673 /* Call update_window_cursor with parameter ON_P on all leaf windows
26674 in the window tree rooted at W. */
26676 static void
26677 update_cursor_in_window_tree (struct window *w, bool on_p)
26679 while (w)
26681 if (WINDOWP (w->contents))
26682 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26683 else
26684 update_window_cursor (w, on_p);
26686 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26691 /* EXPORT:
26692 Display the cursor on window W, or clear it, according to ON_P.
26693 Don't change the cursor's position. */
26695 void
26696 x_update_cursor (struct frame *f, bool on_p)
26698 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26702 /* EXPORT:
26703 Clear the cursor of window W to background color, and mark the
26704 cursor as not shown. This is used when the text where the cursor
26705 is about to be rewritten. */
26707 void
26708 x_clear_cursor (struct window *w)
26710 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26711 update_window_cursor (w, 0);
26714 #endif /* HAVE_WINDOW_SYSTEM */
26716 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26717 and MSDOS. */
26718 static void
26719 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26720 int start_hpos, int end_hpos,
26721 enum draw_glyphs_face draw)
26723 #ifdef HAVE_WINDOW_SYSTEM
26724 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26726 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26727 return;
26729 #endif
26730 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26731 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26732 #endif
26735 /* Display the active region described by mouse_face_* according to DRAW. */
26737 static void
26738 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26740 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26741 struct frame *f = XFRAME (WINDOW_FRAME (w));
26743 if (/* If window is in the process of being destroyed, don't bother
26744 to do anything. */
26745 w->current_matrix != NULL
26746 /* Don't update mouse highlight if hidden */
26747 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26748 /* Recognize when we are called to operate on rows that don't exist
26749 anymore. This can happen when a window is split. */
26750 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26752 int phys_cursor_on_p = w->phys_cursor_on_p;
26753 struct glyph_row *row, *first, *last;
26755 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26756 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26758 for (row = first; row <= last && row->enabled_p; ++row)
26760 int start_hpos, end_hpos, start_x;
26762 /* For all but the first row, the highlight starts at column 0. */
26763 if (row == first)
26765 /* R2L rows have BEG and END in reversed order, but the
26766 screen drawing geometry is always left to right. So
26767 we need to mirror the beginning and end of the
26768 highlighted area in R2L rows. */
26769 if (!row->reversed_p)
26771 start_hpos = hlinfo->mouse_face_beg_col;
26772 start_x = hlinfo->mouse_face_beg_x;
26774 else if (row == last)
26776 start_hpos = hlinfo->mouse_face_end_col;
26777 start_x = hlinfo->mouse_face_end_x;
26779 else
26781 start_hpos = 0;
26782 start_x = 0;
26785 else if (row->reversed_p && row == last)
26787 start_hpos = hlinfo->mouse_face_end_col;
26788 start_x = hlinfo->mouse_face_end_x;
26790 else
26792 start_hpos = 0;
26793 start_x = 0;
26796 if (row == last)
26798 if (!row->reversed_p)
26799 end_hpos = hlinfo->mouse_face_end_col;
26800 else if (row == first)
26801 end_hpos = hlinfo->mouse_face_beg_col;
26802 else
26804 end_hpos = row->used[TEXT_AREA];
26805 if (draw == DRAW_NORMAL_TEXT)
26806 row->fill_line_p = 1; /* Clear to end of line */
26809 else if (row->reversed_p && row == first)
26810 end_hpos = hlinfo->mouse_face_beg_col;
26811 else
26813 end_hpos = row->used[TEXT_AREA];
26814 if (draw == DRAW_NORMAL_TEXT)
26815 row->fill_line_p = 1; /* Clear to end of line */
26818 if (end_hpos > start_hpos)
26820 draw_row_with_mouse_face (w, start_x, row,
26821 start_hpos, end_hpos, draw);
26823 row->mouse_face_p
26824 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26828 #ifdef HAVE_WINDOW_SYSTEM
26829 /* When we've written over the cursor, arrange for it to
26830 be displayed again. */
26831 if (FRAME_WINDOW_P (f)
26832 && phys_cursor_on_p && !w->phys_cursor_on_p)
26834 int hpos = w->phys_cursor.hpos;
26836 /* When the window is hscrolled, cursor hpos can legitimately be
26837 out of bounds, but we draw the cursor at the corresponding
26838 window margin in that case. */
26839 if (!row->reversed_p && hpos < 0)
26840 hpos = 0;
26841 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26842 hpos = row->used[TEXT_AREA] - 1;
26844 block_input ();
26845 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26846 w->phys_cursor.x, w->phys_cursor.y);
26847 unblock_input ();
26849 #endif /* HAVE_WINDOW_SYSTEM */
26852 #ifdef HAVE_WINDOW_SYSTEM
26853 /* Change the mouse cursor. */
26854 if (FRAME_WINDOW_P (f))
26856 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26857 if (draw == DRAW_NORMAL_TEXT
26858 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26859 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26860 else
26861 #endif
26862 if (draw == DRAW_MOUSE_FACE)
26863 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26864 else
26865 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26867 #endif /* HAVE_WINDOW_SYSTEM */
26870 /* EXPORT:
26871 Clear out the mouse-highlighted active region.
26872 Redraw it un-highlighted first. Value is non-zero if mouse
26873 face was actually drawn unhighlighted. */
26876 clear_mouse_face (Mouse_HLInfo *hlinfo)
26878 int cleared = 0;
26880 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26882 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26883 cleared = 1;
26886 reset_mouse_highlight (hlinfo);
26887 return cleared;
26890 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26891 within the mouse face on that window. */
26892 static int
26893 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26895 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26897 /* Quickly resolve the easy cases. */
26898 if (!(WINDOWP (hlinfo->mouse_face_window)
26899 && XWINDOW (hlinfo->mouse_face_window) == w))
26900 return 0;
26901 if (vpos < hlinfo->mouse_face_beg_row
26902 || vpos > hlinfo->mouse_face_end_row)
26903 return 0;
26904 if (vpos > hlinfo->mouse_face_beg_row
26905 && vpos < hlinfo->mouse_face_end_row)
26906 return 1;
26908 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26910 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26912 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26913 return 1;
26915 else if ((vpos == hlinfo->mouse_face_beg_row
26916 && hpos >= hlinfo->mouse_face_beg_col)
26917 || (vpos == hlinfo->mouse_face_end_row
26918 && hpos < hlinfo->mouse_face_end_col))
26919 return 1;
26921 else
26923 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26925 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26926 return 1;
26928 else if ((vpos == hlinfo->mouse_face_beg_row
26929 && hpos <= hlinfo->mouse_face_beg_col)
26930 || (vpos == hlinfo->mouse_face_end_row
26931 && hpos > hlinfo->mouse_face_end_col))
26932 return 1;
26934 return 0;
26938 /* EXPORT:
26939 Non-zero if physical cursor of window W is within mouse face. */
26942 cursor_in_mouse_face_p (struct window *w)
26944 int hpos = w->phys_cursor.hpos;
26945 int vpos = w->phys_cursor.vpos;
26946 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26948 /* When the window is hscrolled, cursor hpos can legitimately be out
26949 of bounds, but we draw the cursor at the corresponding window
26950 margin in that case. */
26951 if (!row->reversed_p && hpos < 0)
26952 hpos = 0;
26953 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26954 hpos = row->used[TEXT_AREA] - 1;
26956 return coords_in_mouse_face_p (w, hpos, vpos);
26961 /* Find the glyph rows START_ROW and END_ROW of window W that display
26962 characters between buffer positions START_CHARPOS and END_CHARPOS
26963 (excluding END_CHARPOS). DISP_STRING is a display string that
26964 covers these buffer positions. This is similar to
26965 row_containing_pos, but is more accurate when bidi reordering makes
26966 buffer positions change non-linearly with glyph rows. */
26967 static void
26968 rows_from_pos_range (struct window *w,
26969 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26970 Lisp_Object disp_string,
26971 struct glyph_row **start, struct glyph_row **end)
26973 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26974 int last_y = window_text_bottom_y (w);
26975 struct glyph_row *row;
26977 *start = NULL;
26978 *end = NULL;
26980 while (!first->enabled_p
26981 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26982 first++;
26984 /* Find the START row. */
26985 for (row = first;
26986 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26987 row++)
26989 /* A row can potentially be the START row if the range of the
26990 characters it displays intersects the range
26991 [START_CHARPOS..END_CHARPOS). */
26992 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26993 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26994 /* See the commentary in row_containing_pos, for the
26995 explanation of the complicated way to check whether
26996 some position is beyond the end of the characters
26997 displayed by a row. */
26998 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26999 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27000 && !row->ends_at_zv_p
27001 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27002 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27003 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27004 && !row->ends_at_zv_p
27005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27007 /* Found a candidate row. Now make sure at least one of the
27008 glyphs it displays has a charpos from the range
27009 [START_CHARPOS..END_CHARPOS).
27011 This is not obvious because bidi reordering could make
27012 buffer positions of a row be 1,2,3,102,101,100, and if we
27013 want to highlight characters in [50..60), we don't want
27014 this row, even though [50..60) does intersect [1..103),
27015 the range of character positions given by the row's start
27016 and end positions. */
27017 struct glyph *g = row->glyphs[TEXT_AREA];
27018 struct glyph *e = g + row->used[TEXT_AREA];
27020 while (g < e)
27022 if (((BUFFERP (g->object) || INTEGERP (g->object))
27023 && start_charpos <= g->charpos && g->charpos < end_charpos)
27024 /* A glyph that comes from DISP_STRING is by
27025 definition to be highlighted. */
27026 || EQ (g->object, disp_string))
27027 *start = row;
27028 g++;
27030 if (*start)
27031 break;
27035 /* Find the END row. */
27036 if (!*start
27037 /* If the last row is partially visible, start looking for END
27038 from that row, instead of starting from FIRST. */
27039 && !(row->enabled_p
27040 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27041 row = first;
27042 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27044 struct glyph_row *next = row + 1;
27045 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27047 if (!next->enabled_p
27048 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27049 /* The first row >= START whose range of displayed characters
27050 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27051 is the row END + 1. */
27052 || (start_charpos < next_start
27053 && end_charpos < next_start)
27054 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27055 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27056 && !next->ends_at_zv_p
27057 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27058 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27059 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27060 && !next->ends_at_zv_p
27061 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27063 *end = row;
27064 break;
27066 else
27068 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27069 but none of the characters it displays are in the range, it is
27070 also END + 1. */
27071 struct glyph *g = next->glyphs[TEXT_AREA];
27072 struct glyph *s = g;
27073 struct glyph *e = g + next->used[TEXT_AREA];
27075 while (g < e)
27077 if (((BUFFERP (g->object) || INTEGERP (g->object))
27078 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27079 /* If the buffer position of the first glyph in
27080 the row is equal to END_CHARPOS, it means
27081 the last character to be highlighted is the
27082 newline of ROW, and we must consider NEXT as
27083 END, not END+1. */
27084 || (((!next->reversed_p && g == s)
27085 || (next->reversed_p && g == e - 1))
27086 && (g->charpos == end_charpos
27087 /* Special case for when NEXT is an
27088 empty line at ZV. */
27089 || (g->charpos == -1
27090 && !row->ends_at_zv_p
27091 && next_start == end_charpos)))))
27092 /* A glyph that comes from DISP_STRING is by
27093 definition to be highlighted. */
27094 || EQ (g->object, disp_string))
27095 break;
27096 g++;
27098 if (g == e)
27100 *end = row;
27101 break;
27103 /* The first row that ends at ZV must be the last to be
27104 highlighted. */
27105 else if (next->ends_at_zv_p)
27107 *end = next;
27108 break;
27114 /* This function sets the mouse_face_* elements of HLINFO, assuming
27115 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27116 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27117 for the overlay or run of text properties specifying the mouse
27118 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27119 before-string and after-string that must also be highlighted.
27120 DISP_STRING, if non-nil, is a display string that may cover some
27121 or all of the highlighted text. */
27123 static void
27124 mouse_face_from_buffer_pos (Lisp_Object window,
27125 Mouse_HLInfo *hlinfo,
27126 ptrdiff_t mouse_charpos,
27127 ptrdiff_t start_charpos,
27128 ptrdiff_t end_charpos,
27129 Lisp_Object before_string,
27130 Lisp_Object after_string,
27131 Lisp_Object disp_string)
27133 struct window *w = XWINDOW (window);
27134 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27135 struct glyph_row *r1, *r2;
27136 struct glyph *glyph, *end;
27137 ptrdiff_t ignore, pos;
27138 int x;
27140 eassert (NILP (disp_string) || STRINGP (disp_string));
27141 eassert (NILP (before_string) || STRINGP (before_string));
27142 eassert (NILP (after_string) || STRINGP (after_string));
27144 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27145 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27146 if (r1 == NULL)
27147 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27148 /* If the before-string or display-string contains newlines,
27149 rows_from_pos_range skips to its last row. Move back. */
27150 if (!NILP (before_string) || !NILP (disp_string))
27152 struct glyph_row *prev;
27153 while ((prev = r1 - 1, prev >= first)
27154 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27155 && prev->used[TEXT_AREA] > 0)
27157 struct glyph *beg = prev->glyphs[TEXT_AREA];
27158 glyph = beg + prev->used[TEXT_AREA];
27159 while (--glyph >= beg && INTEGERP (glyph->object));
27160 if (glyph < beg
27161 || !(EQ (glyph->object, before_string)
27162 || EQ (glyph->object, disp_string)))
27163 break;
27164 r1 = prev;
27167 if (r2 == NULL)
27169 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27170 hlinfo->mouse_face_past_end = 1;
27172 else if (!NILP (after_string))
27174 /* If the after-string has newlines, advance to its last row. */
27175 struct glyph_row *next;
27176 struct glyph_row *last
27177 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27179 for (next = r2 + 1;
27180 next <= last
27181 && next->used[TEXT_AREA] > 0
27182 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27183 ++next)
27184 r2 = next;
27186 /* The rest of the display engine assumes that mouse_face_beg_row is
27187 either above mouse_face_end_row or identical to it. But with
27188 bidi-reordered continued lines, the row for START_CHARPOS could
27189 be below the row for END_CHARPOS. If so, swap the rows and store
27190 them in correct order. */
27191 if (r1->y > r2->y)
27193 struct glyph_row *tem = r2;
27195 r2 = r1;
27196 r1 = tem;
27199 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27200 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27202 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27203 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27204 could be anywhere in the row and in any order. The strategy
27205 below is to find the leftmost and the rightmost glyph that
27206 belongs to either of these 3 strings, or whose position is
27207 between START_CHARPOS and END_CHARPOS, and highlight all the
27208 glyphs between those two. This may cover more than just the text
27209 between START_CHARPOS and END_CHARPOS if the range of characters
27210 strides the bidi level boundary, e.g. if the beginning is in R2L
27211 text while the end is in L2R text or vice versa. */
27212 if (!r1->reversed_p)
27214 /* This row is in a left to right paragraph. Scan it left to
27215 right. */
27216 glyph = r1->glyphs[TEXT_AREA];
27217 end = glyph + r1->used[TEXT_AREA];
27218 x = r1->x;
27220 /* Skip truncation glyphs at the start of the glyph row. */
27221 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27222 for (; glyph < end
27223 && INTEGERP (glyph->object)
27224 && glyph->charpos < 0;
27225 ++glyph)
27226 x += glyph->pixel_width;
27228 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27229 or DISP_STRING, and the first glyph from buffer whose
27230 position is between START_CHARPOS and END_CHARPOS. */
27231 for (; glyph < end
27232 && !INTEGERP (glyph->object)
27233 && !EQ (glyph->object, disp_string)
27234 && !(BUFFERP (glyph->object)
27235 && (glyph->charpos >= start_charpos
27236 && glyph->charpos < end_charpos));
27237 ++glyph)
27239 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27240 are present at buffer positions between START_CHARPOS and
27241 END_CHARPOS, or if they come from an overlay. */
27242 if (EQ (glyph->object, before_string))
27244 pos = string_buffer_position (before_string,
27245 start_charpos);
27246 /* If pos == 0, it means before_string came from an
27247 overlay, not from a buffer position. */
27248 if (!pos || (pos >= start_charpos && pos < end_charpos))
27249 break;
27251 else if (EQ (glyph->object, after_string))
27253 pos = string_buffer_position (after_string, end_charpos);
27254 if (!pos || (pos >= start_charpos && pos < end_charpos))
27255 break;
27257 x += glyph->pixel_width;
27259 hlinfo->mouse_face_beg_x = x;
27260 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27262 else
27264 /* This row is in a right to left paragraph. Scan it right to
27265 left. */
27266 struct glyph *g;
27268 end = r1->glyphs[TEXT_AREA] - 1;
27269 glyph = end + r1->used[TEXT_AREA];
27271 /* Skip truncation glyphs at the start of the glyph row. */
27272 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27273 for (; glyph > end
27274 && INTEGERP (glyph->object)
27275 && glyph->charpos < 0;
27276 --glyph)
27279 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27280 or DISP_STRING, and the first glyph from buffer whose
27281 position is between START_CHARPOS and END_CHARPOS. */
27282 for (; glyph > end
27283 && !INTEGERP (glyph->object)
27284 && !EQ (glyph->object, disp_string)
27285 && !(BUFFERP (glyph->object)
27286 && (glyph->charpos >= start_charpos
27287 && glyph->charpos < end_charpos));
27288 --glyph)
27290 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27291 are present at buffer positions between START_CHARPOS and
27292 END_CHARPOS, or if they come from an overlay. */
27293 if (EQ (glyph->object, before_string))
27295 pos = string_buffer_position (before_string, start_charpos);
27296 /* If pos == 0, it means before_string came from an
27297 overlay, not from a buffer position. */
27298 if (!pos || (pos >= start_charpos && pos < end_charpos))
27299 break;
27301 else if (EQ (glyph->object, after_string))
27303 pos = string_buffer_position (after_string, end_charpos);
27304 if (!pos || (pos >= start_charpos && pos < end_charpos))
27305 break;
27309 glyph++; /* first glyph to the right of the highlighted area */
27310 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27311 x += g->pixel_width;
27312 hlinfo->mouse_face_beg_x = x;
27313 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27316 /* If the highlight ends in a different row, compute GLYPH and END
27317 for the end row. Otherwise, reuse the values computed above for
27318 the row where the highlight begins. */
27319 if (r2 != r1)
27321 if (!r2->reversed_p)
27323 glyph = r2->glyphs[TEXT_AREA];
27324 end = glyph + r2->used[TEXT_AREA];
27325 x = r2->x;
27327 else
27329 end = r2->glyphs[TEXT_AREA] - 1;
27330 glyph = end + r2->used[TEXT_AREA];
27334 if (!r2->reversed_p)
27336 /* Skip truncation and continuation glyphs near the end of the
27337 row, and also blanks and stretch glyphs inserted by
27338 extend_face_to_end_of_line. */
27339 while (end > glyph
27340 && INTEGERP ((end - 1)->object))
27341 --end;
27342 /* Scan the rest of the glyph row from the end, looking for the
27343 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27344 DISP_STRING, or whose position is between START_CHARPOS
27345 and END_CHARPOS */
27346 for (--end;
27347 end > glyph
27348 && !INTEGERP (end->object)
27349 && !EQ (end->object, disp_string)
27350 && !(BUFFERP (end->object)
27351 && (end->charpos >= start_charpos
27352 && end->charpos < end_charpos));
27353 --end)
27355 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27356 are present at buffer positions between START_CHARPOS and
27357 END_CHARPOS, or if they come from an overlay. */
27358 if (EQ (end->object, before_string))
27360 pos = string_buffer_position (before_string, start_charpos);
27361 if (!pos || (pos >= start_charpos && pos < end_charpos))
27362 break;
27364 else if (EQ (end->object, after_string))
27366 pos = string_buffer_position (after_string, end_charpos);
27367 if (!pos || (pos >= start_charpos && pos < end_charpos))
27368 break;
27371 /* Find the X coordinate of the last glyph to be highlighted. */
27372 for (; glyph <= end; ++glyph)
27373 x += glyph->pixel_width;
27375 hlinfo->mouse_face_end_x = x;
27376 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27378 else
27380 /* Skip truncation and continuation glyphs near the end of the
27381 row, and also blanks and stretch glyphs inserted by
27382 extend_face_to_end_of_line. */
27383 x = r2->x;
27384 end++;
27385 while (end < glyph
27386 && INTEGERP (end->object))
27388 x += end->pixel_width;
27389 ++end;
27391 /* Scan the rest of the glyph row from the end, looking for the
27392 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27393 DISP_STRING, or whose position is between START_CHARPOS
27394 and END_CHARPOS */
27395 for ( ;
27396 end < glyph
27397 && !INTEGERP (end->object)
27398 && !EQ (end->object, disp_string)
27399 && !(BUFFERP (end->object)
27400 && (end->charpos >= start_charpos
27401 && end->charpos < end_charpos));
27402 ++end)
27404 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27405 are present at buffer positions between START_CHARPOS and
27406 END_CHARPOS, or if they come from an overlay. */
27407 if (EQ (end->object, before_string))
27409 pos = string_buffer_position (before_string, start_charpos);
27410 if (!pos || (pos >= start_charpos && pos < end_charpos))
27411 break;
27413 else if (EQ (end->object, after_string))
27415 pos = string_buffer_position (after_string, end_charpos);
27416 if (!pos || (pos >= start_charpos && pos < end_charpos))
27417 break;
27419 x += end->pixel_width;
27421 /* If we exited the above loop because we arrived at the last
27422 glyph of the row, and its buffer position is still not in
27423 range, it means the last character in range is the preceding
27424 newline. Bump the end column and x values to get past the
27425 last glyph. */
27426 if (end == glyph
27427 && BUFFERP (end->object)
27428 && (end->charpos < start_charpos
27429 || end->charpos >= end_charpos))
27431 x += end->pixel_width;
27432 ++end;
27434 hlinfo->mouse_face_end_x = x;
27435 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27438 hlinfo->mouse_face_window = window;
27439 hlinfo->mouse_face_face_id
27440 = face_at_buffer_position (w, mouse_charpos, &ignore,
27441 mouse_charpos + 1,
27442 !hlinfo->mouse_face_hidden, -1);
27443 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27446 /* The following function is not used anymore (replaced with
27447 mouse_face_from_string_pos), but I leave it here for the time
27448 being, in case someone would. */
27450 #if 0 /* not used */
27452 /* Find the position of the glyph for position POS in OBJECT in
27453 window W's current matrix, and return in *X, *Y the pixel
27454 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27456 RIGHT_P non-zero means return the position of the right edge of the
27457 glyph, RIGHT_P zero means return the left edge position.
27459 If no glyph for POS exists in the matrix, return the position of
27460 the glyph with the next smaller position that is in the matrix, if
27461 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27462 exists in the matrix, return the position of the glyph with the
27463 next larger position in OBJECT.
27465 Value is non-zero if a glyph was found. */
27467 static int
27468 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27469 int *hpos, int *vpos, int *x, int *y, int right_p)
27471 int yb = window_text_bottom_y (w);
27472 struct glyph_row *r;
27473 struct glyph *best_glyph = NULL;
27474 struct glyph_row *best_row = NULL;
27475 int best_x = 0;
27477 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27478 r->enabled_p && r->y < yb;
27479 ++r)
27481 struct glyph *g = r->glyphs[TEXT_AREA];
27482 struct glyph *e = g + r->used[TEXT_AREA];
27483 int gx;
27485 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27486 if (EQ (g->object, object))
27488 if (g->charpos == pos)
27490 best_glyph = g;
27491 best_x = gx;
27492 best_row = r;
27493 goto found;
27495 else if (best_glyph == NULL
27496 || ((eabs (g->charpos - pos)
27497 < eabs (best_glyph->charpos - pos))
27498 && (right_p
27499 ? g->charpos < pos
27500 : g->charpos > pos)))
27502 best_glyph = g;
27503 best_x = gx;
27504 best_row = r;
27509 found:
27511 if (best_glyph)
27513 *x = best_x;
27514 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27516 if (right_p)
27518 *x += best_glyph->pixel_width;
27519 ++*hpos;
27522 *y = best_row->y;
27523 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27526 return best_glyph != NULL;
27528 #endif /* not used */
27530 /* Find the positions of the first and the last glyphs in window W's
27531 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27532 (assumed to be a string), and return in HLINFO's mouse_face_*
27533 members the pixel and column/row coordinates of those glyphs. */
27535 static void
27536 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27537 Lisp_Object object,
27538 ptrdiff_t startpos, ptrdiff_t endpos)
27540 int yb = window_text_bottom_y (w);
27541 struct glyph_row *r;
27542 struct glyph *g, *e;
27543 int gx;
27544 int found = 0;
27546 /* Find the glyph row with at least one position in the range
27547 [STARTPOS..ENDPOS), and the first glyph in that row whose
27548 position belongs to that range. */
27549 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27550 r->enabled_p && r->y < yb;
27551 ++r)
27553 if (!r->reversed_p)
27555 g = r->glyphs[TEXT_AREA];
27556 e = g + r->used[TEXT_AREA];
27557 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27558 if (EQ (g->object, object)
27559 && startpos <= g->charpos && g->charpos < endpos)
27561 hlinfo->mouse_face_beg_row
27562 = MATRIX_ROW_VPOS (r, w->current_matrix);
27563 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27564 hlinfo->mouse_face_beg_x = gx;
27565 found = 1;
27566 break;
27569 else
27571 struct glyph *g1;
27573 e = r->glyphs[TEXT_AREA];
27574 g = e + r->used[TEXT_AREA];
27575 for ( ; g > e; --g)
27576 if (EQ ((g-1)->object, object)
27577 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27579 hlinfo->mouse_face_beg_row
27580 = MATRIX_ROW_VPOS (r, w->current_matrix);
27581 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27582 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27583 gx += g1->pixel_width;
27584 hlinfo->mouse_face_beg_x = gx;
27585 found = 1;
27586 break;
27589 if (found)
27590 break;
27593 if (!found)
27594 return;
27596 /* Starting with the next row, look for the first row which does NOT
27597 include any glyphs whose positions are in the range. */
27598 for (++r; r->enabled_p && r->y < yb; ++r)
27600 g = r->glyphs[TEXT_AREA];
27601 e = g + r->used[TEXT_AREA];
27602 found = 0;
27603 for ( ; g < e; ++g)
27604 if (EQ (g->object, object)
27605 && startpos <= g->charpos && g->charpos < endpos)
27607 found = 1;
27608 break;
27610 if (!found)
27611 break;
27614 /* The highlighted region ends on the previous row. */
27615 r--;
27617 /* Set the end row. */
27618 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27620 /* Compute and set the end column and the end column's horizontal
27621 pixel coordinate. */
27622 if (!r->reversed_p)
27624 g = r->glyphs[TEXT_AREA];
27625 e = g + r->used[TEXT_AREA];
27626 for ( ; e > g; --e)
27627 if (EQ ((e-1)->object, object)
27628 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27629 break;
27630 hlinfo->mouse_face_end_col = e - g;
27632 for (gx = r->x; g < e; ++g)
27633 gx += g->pixel_width;
27634 hlinfo->mouse_face_end_x = gx;
27636 else
27638 e = r->glyphs[TEXT_AREA];
27639 g = e + r->used[TEXT_AREA];
27640 for (gx = r->x ; e < g; ++e)
27642 if (EQ (e->object, object)
27643 && startpos <= e->charpos && e->charpos < endpos)
27644 break;
27645 gx += e->pixel_width;
27647 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27648 hlinfo->mouse_face_end_x = gx;
27652 #ifdef HAVE_WINDOW_SYSTEM
27654 /* See if position X, Y is within a hot-spot of an image. */
27656 static int
27657 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27659 if (!CONSP (hot_spot))
27660 return 0;
27662 if (EQ (XCAR (hot_spot), Qrect))
27664 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27665 Lisp_Object rect = XCDR (hot_spot);
27666 Lisp_Object tem;
27667 if (!CONSP (rect))
27668 return 0;
27669 if (!CONSP (XCAR (rect)))
27670 return 0;
27671 if (!CONSP (XCDR (rect)))
27672 return 0;
27673 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27674 return 0;
27675 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27676 return 0;
27677 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27678 return 0;
27679 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27680 return 0;
27681 return 1;
27683 else if (EQ (XCAR (hot_spot), Qcircle))
27685 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27686 Lisp_Object circ = XCDR (hot_spot);
27687 Lisp_Object lr, lx0, ly0;
27688 if (CONSP (circ)
27689 && CONSP (XCAR (circ))
27690 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27691 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27692 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27694 double r = XFLOATINT (lr);
27695 double dx = XINT (lx0) - x;
27696 double dy = XINT (ly0) - y;
27697 return (dx * dx + dy * dy <= r * r);
27700 else if (EQ (XCAR (hot_spot), Qpoly))
27702 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27703 if (VECTORP (XCDR (hot_spot)))
27705 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27706 Lisp_Object *poly = v->contents;
27707 ptrdiff_t n = v->header.size;
27708 ptrdiff_t i;
27709 int inside = 0;
27710 Lisp_Object lx, ly;
27711 int x0, y0;
27713 /* Need an even number of coordinates, and at least 3 edges. */
27714 if (n < 6 || n & 1)
27715 return 0;
27717 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27718 If count is odd, we are inside polygon. Pixels on edges
27719 may or may not be included depending on actual geometry of the
27720 polygon. */
27721 if ((lx = poly[n-2], !INTEGERP (lx))
27722 || (ly = poly[n-1], !INTEGERP (lx)))
27723 return 0;
27724 x0 = XINT (lx), y0 = XINT (ly);
27725 for (i = 0; i < n; i += 2)
27727 int x1 = x0, y1 = y0;
27728 if ((lx = poly[i], !INTEGERP (lx))
27729 || (ly = poly[i+1], !INTEGERP (ly)))
27730 return 0;
27731 x0 = XINT (lx), y0 = XINT (ly);
27733 /* Does this segment cross the X line? */
27734 if (x0 >= x)
27736 if (x1 >= x)
27737 continue;
27739 else if (x1 < x)
27740 continue;
27741 if (y > y0 && y > y1)
27742 continue;
27743 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27744 inside = !inside;
27746 return inside;
27749 return 0;
27752 Lisp_Object
27753 find_hot_spot (Lisp_Object map, int x, int y)
27755 while (CONSP (map))
27757 if (CONSP (XCAR (map))
27758 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27759 return XCAR (map);
27760 map = XCDR (map);
27763 return Qnil;
27766 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27767 3, 3, 0,
27768 doc: /* Lookup in image map MAP coordinates X and Y.
27769 An image map is an alist where each element has the format (AREA ID PLIST).
27770 An AREA is specified as either a rectangle, a circle, or a polygon:
27771 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27772 pixel coordinates of the upper left and bottom right corners.
27773 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27774 and the radius of the circle; r may be a float or integer.
27775 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27776 vector describes one corner in the polygon.
27777 Returns the alist element for the first matching AREA in MAP. */)
27778 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27780 if (NILP (map))
27781 return Qnil;
27783 CHECK_NUMBER (x);
27784 CHECK_NUMBER (y);
27786 return find_hot_spot (map,
27787 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27788 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27792 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27793 static void
27794 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27796 /* Do not change cursor shape while dragging mouse. */
27797 if (!NILP (do_mouse_tracking))
27798 return;
27800 if (!NILP (pointer))
27802 if (EQ (pointer, Qarrow))
27803 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27804 else if (EQ (pointer, Qhand))
27805 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27806 else if (EQ (pointer, Qtext))
27807 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27808 else if (EQ (pointer, intern ("hdrag")))
27809 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27810 #ifdef HAVE_X_WINDOWS
27811 else if (EQ (pointer, intern ("vdrag")))
27812 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27813 #endif
27814 else if (EQ (pointer, intern ("hourglass")))
27815 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27816 else if (EQ (pointer, Qmodeline))
27817 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27818 else
27819 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27822 if (cursor != No_Cursor)
27823 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27826 #endif /* HAVE_WINDOW_SYSTEM */
27828 /* Take proper action when mouse has moved to the mode or header line
27829 or marginal area AREA of window W, x-position X and y-position Y.
27830 X is relative to the start of the text display area of W, so the
27831 width of bitmap areas and scroll bars must be subtracted to get a
27832 position relative to the start of the mode line. */
27834 static void
27835 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27836 enum window_part area)
27838 struct window *w = XWINDOW (window);
27839 struct frame *f = XFRAME (w->frame);
27840 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27841 #ifdef HAVE_WINDOW_SYSTEM
27842 Display_Info *dpyinfo;
27843 #endif
27844 Cursor cursor = No_Cursor;
27845 Lisp_Object pointer = Qnil;
27846 int dx, dy, width, height;
27847 ptrdiff_t charpos;
27848 Lisp_Object string, object = Qnil;
27849 Lisp_Object pos IF_LINT (= Qnil), help;
27851 Lisp_Object mouse_face;
27852 int original_x_pixel = x;
27853 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27854 struct glyph_row *row IF_LINT (= 0);
27856 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27858 int x0;
27859 struct glyph *end;
27861 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27862 returns them in row/column units! */
27863 string = mode_line_string (w, area, &x, &y, &charpos,
27864 &object, &dx, &dy, &width, &height);
27866 row = (area == ON_MODE_LINE
27867 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27868 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27870 /* Find the glyph under the mouse pointer. */
27871 if (row->mode_line_p && row->enabled_p)
27873 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27874 end = glyph + row->used[TEXT_AREA];
27876 for (x0 = original_x_pixel;
27877 glyph < end && x0 >= glyph->pixel_width;
27878 ++glyph)
27879 x0 -= glyph->pixel_width;
27881 if (glyph >= end)
27882 glyph = NULL;
27885 else
27887 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27888 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27889 returns them in row/column units! */
27890 string = marginal_area_string (w, area, &x, &y, &charpos,
27891 &object, &dx, &dy, &width, &height);
27894 help = Qnil;
27896 #ifdef HAVE_WINDOW_SYSTEM
27897 if (IMAGEP (object))
27899 Lisp_Object image_map, hotspot;
27900 if ((image_map = Fplist_get (XCDR (object), QCmap),
27901 !NILP (image_map))
27902 && (hotspot = find_hot_spot (image_map, dx, dy),
27903 CONSP (hotspot))
27904 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27906 Lisp_Object plist;
27908 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27909 If so, we could look for mouse-enter, mouse-leave
27910 properties in PLIST (and do something...). */
27911 hotspot = XCDR (hotspot);
27912 if (CONSP (hotspot)
27913 && (plist = XCAR (hotspot), CONSP (plist)))
27915 pointer = Fplist_get (plist, Qpointer);
27916 if (NILP (pointer))
27917 pointer = Qhand;
27918 help = Fplist_get (plist, Qhelp_echo);
27919 if (!NILP (help))
27921 help_echo_string = help;
27922 XSETWINDOW (help_echo_window, w);
27923 help_echo_object = w->contents;
27924 help_echo_pos = charpos;
27928 if (NILP (pointer))
27929 pointer = Fplist_get (XCDR (object), QCpointer);
27931 #endif /* HAVE_WINDOW_SYSTEM */
27933 if (STRINGP (string))
27934 pos = make_number (charpos);
27936 /* Set the help text and mouse pointer. If the mouse is on a part
27937 of the mode line without any text (e.g. past the right edge of
27938 the mode line text), use the default help text and pointer. */
27939 if (STRINGP (string) || area == ON_MODE_LINE)
27941 /* Arrange to display the help by setting the global variables
27942 help_echo_string, help_echo_object, and help_echo_pos. */
27943 if (NILP (help))
27945 if (STRINGP (string))
27946 help = Fget_text_property (pos, Qhelp_echo, string);
27948 if (!NILP (help))
27950 help_echo_string = help;
27951 XSETWINDOW (help_echo_window, w);
27952 help_echo_object = string;
27953 help_echo_pos = charpos;
27955 else if (area == ON_MODE_LINE)
27957 Lisp_Object default_help
27958 = buffer_local_value_1 (Qmode_line_default_help_echo,
27959 w->contents);
27961 if (STRINGP (default_help))
27963 help_echo_string = default_help;
27964 XSETWINDOW (help_echo_window, w);
27965 help_echo_object = Qnil;
27966 help_echo_pos = -1;
27971 #ifdef HAVE_WINDOW_SYSTEM
27972 /* Change the mouse pointer according to what is under it. */
27973 if (FRAME_WINDOW_P (f))
27975 dpyinfo = FRAME_DISPLAY_INFO (f);
27976 if (STRINGP (string))
27978 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27980 if (NILP (pointer))
27981 pointer = Fget_text_property (pos, Qpointer, string);
27983 /* Change the mouse pointer according to what is under X/Y. */
27984 if (NILP (pointer)
27985 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27987 Lisp_Object map;
27988 map = Fget_text_property (pos, Qlocal_map, string);
27989 if (!KEYMAPP (map))
27990 map = Fget_text_property (pos, Qkeymap, string);
27991 if (!KEYMAPP (map))
27992 cursor = dpyinfo->vertical_scroll_bar_cursor;
27995 else
27996 /* Default mode-line pointer. */
27997 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27999 #endif
28002 /* Change the mouse face according to what is under X/Y. */
28003 if (STRINGP (string))
28005 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28006 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28007 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28008 && glyph)
28010 Lisp_Object b, e;
28012 struct glyph * tmp_glyph;
28014 int gpos;
28015 int gseq_length;
28016 int total_pixel_width;
28017 ptrdiff_t begpos, endpos, ignore;
28019 int vpos, hpos;
28021 b = Fprevious_single_property_change (make_number (charpos + 1),
28022 Qmouse_face, string, Qnil);
28023 if (NILP (b))
28024 begpos = 0;
28025 else
28026 begpos = XINT (b);
28028 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28029 if (NILP (e))
28030 endpos = SCHARS (string);
28031 else
28032 endpos = XINT (e);
28034 /* Calculate the glyph position GPOS of GLYPH in the
28035 displayed string, relative to the beginning of the
28036 highlighted part of the string.
28038 Note: GPOS is different from CHARPOS. CHARPOS is the
28039 position of GLYPH in the internal string object. A mode
28040 line string format has structures which are converted to
28041 a flattened string by the Emacs Lisp interpreter. The
28042 internal string is an element of those structures. The
28043 displayed string is the flattened string. */
28044 tmp_glyph = row_start_glyph;
28045 while (tmp_glyph < glyph
28046 && (!(EQ (tmp_glyph->object, glyph->object)
28047 && begpos <= tmp_glyph->charpos
28048 && tmp_glyph->charpos < endpos)))
28049 tmp_glyph++;
28050 gpos = glyph - tmp_glyph;
28052 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28053 the highlighted part of the displayed string to which
28054 GLYPH belongs. Note: GSEQ_LENGTH is different from
28055 SCHARS (STRING), because the latter returns the length of
28056 the internal string. */
28057 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28058 tmp_glyph > glyph
28059 && (!(EQ (tmp_glyph->object, glyph->object)
28060 && begpos <= tmp_glyph->charpos
28061 && tmp_glyph->charpos < endpos));
28062 tmp_glyph--)
28064 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28066 /* Calculate the total pixel width of all the glyphs between
28067 the beginning of the highlighted area and GLYPH. */
28068 total_pixel_width = 0;
28069 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28070 total_pixel_width += tmp_glyph->pixel_width;
28072 /* Pre calculation of re-rendering position. Note: X is in
28073 column units here, after the call to mode_line_string or
28074 marginal_area_string. */
28075 hpos = x - gpos;
28076 vpos = (area == ON_MODE_LINE
28077 ? (w->current_matrix)->nrows - 1
28078 : 0);
28080 /* If GLYPH's position is included in the region that is
28081 already drawn in mouse face, we have nothing to do. */
28082 if ( EQ (window, hlinfo->mouse_face_window)
28083 && (!row->reversed_p
28084 ? (hlinfo->mouse_face_beg_col <= hpos
28085 && hpos < hlinfo->mouse_face_end_col)
28086 /* In R2L rows we swap BEG and END, see below. */
28087 : (hlinfo->mouse_face_end_col <= hpos
28088 && hpos < hlinfo->mouse_face_beg_col))
28089 && hlinfo->mouse_face_beg_row == vpos )
28090 return;
28092 if (clear_mouse_face (hlinfo))
28093 cursor = No_Cursor;
28095 if (!row->reversed_p)
28097 hlinfo->mouse_face_beg_col = hpos;
28098 hlinfo->mouse_face_beg_x = original_x_pixel
28099 - (total_pixel_width + dx);
28100 hlinfo->mouse_face_end_col = hpos + gseq_length;
28101 hlinfo->mouse_face_end_x = 0;
28103 else
28105 /* In R2L rows, show_mouse_face expects BEG and END
28106 coordinates to be swapped. */
28107 hlinfo->mouse_face_end_col = hpos;
28108 hlinfo->mouse_face_end_x = original_x_pixel
28109 - (total_pixel_width + dx);
28110 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28111 hlinfo->mouse_face_beg_x = 0;
28114 hlinfo->mouse_face_beg_row = vpos;
28115 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28116 hlinfo->mouse_face_past_end = 0;
28117 hlinfo->mouse_face_window = window;
28119 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28120 charpos,
28121 0, &ignore,
28122 glyph->face_id,
28124 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28126 if (NILP (pointer))
28127 pointer = Qhand;
28129 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28130 clear_mouse_face (hlinfo);
28132 #ifdef HAVE_WINDOW_SYSTEM
28133 if (FRAME_WINDOW_P (f))
28134 define_frame_cursor1 (f, cursor, pointer);
28135 #endif
28139 /* EXPORT:
28140 Take proper action when the mouse has moved to position X, Y on
28141 frame F with regards to highlighting portions of display that have
28142 mouse-face properties. Also de-highlight portions of display where
28143 the mouse was before, set the mouse pointer shape as appropriate
28144 for the mouse coordinates, and activate help echo (tooltips).
28145 X and Y can be negative or out of range. */
28147 void
28148 note_mouse_highlight (struct frame *f, int x, int y)
28150 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28151 enum window_part part = ON_NOTHING;
28152 Lisp_Object window;
28153 struct window *w;
28154 Cursor cursor = No_Cursor;
28155 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28156 struct buffer *b;
28158 /* When a menu is active, don't highlight because this looks odd. */
28159 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28160 if (popup_activated ())
28161 return;
28162 #endif
28164 if (!f->glyphs_initialized_p
28165 || f->pointer_invisible)
28166 return;
28168 hlinfo->mouse_face_mouse_x = x;
28169 hlinfo->mouse_face_mouse_y = y;
28170 hlinfo->mouse_face_mouse_frame = f;
28172 if (hlinfo->mouse_face_defer)
28173 return;
28175 /* Which window is that in? */
28176 window = window_from_coordinates (f, x, y, &part, 1);
28178 /* If displaying active text in another window, clear that. */
28179 if (! EQ (window, hlinfo->mouse_face_window)
28180 /* Also clear if we move out of text area in same window. */
28181 || (!NILP (hlinfo->mouse_face_window)
28182 && !NILP (window)
28183 && part != ON_TEXT
28184 && part != ON_MODE_LINE
28185 && part != ON_HEADER_LINE))
28186 clear_mouse_face (hlinfo);
28188 /* Not on a window -> return. */
28189 if (!WINDOWP (window))
28190 return;
28192 /* Reset help_echo_string. It will get recomputed below. */
28193 help_echo_string = Qnil;
28195 /* Convert to window-relative pixel coordinates. */
28196 w = XWINDOW (window);
28197 frame_to_window_pixel_xy (w, &x, &y);
28199 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28200 /* Handle tool-bar window differently since it doesn't display a
28201 buffer. */
28202 if (EQ (window, f->tool_bar_window))
28204 note_tool_bar_highlight (f, x, y);
28205 return;
28207 #endif
28209 /* Mouse is on the mode, header line or margin? */
28210 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28211 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28213 note_mode_line_or_margin_highlight (window, x, y, part);
28214 return;
28217 #ifdef HAVE_WINDOW_SYSTEM
28218 if (part == ON_VERTICAL_BORDER)
28220 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28221 help_echo_string = build_string ("drag-mouse-1: resize");
28223 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28224 || part == ON_SCROLL_BAR)
28225 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28226 else
28227 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28228 #endif
28230 /* Are we in a window whose display is up to date?
28231 And verify the buffer's text has not changed. */
28232 b = XBUFFER (w->contents);
28233 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28235 int hpos, vpos, dx, dy, area = LAST_AREA;
28236 ptrdiff_t pos;
28237 struct glyph *glyph;
28238 Lisp_Object object;
28239 Lisp_Object mouse_face = Qnil, position;
28240 Lisp_Object *overlay_vec = NULL;
28241 ptrdiff_t i, noverlays;
28242 struct buffer *obuf;
28243 ptrdiff_t obegv, ozv;
28244 int same_region;
28246 /* Find the glyph under X/Y. */
28247 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28249 #ifdef HAVE_WINDOW_SYSTEM
28250 /* Look for :pointer property on image. */
28251 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28253 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28254 if (img != NULL && IMAGEP (img->spec))
28256 Lisp_Object image_map, hotspot;
28257 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28258 !NILP (image_map))
28259 && (hotspot = find_hot_spot (image_map,
28260 glyph->slice.img.x + dx,
28261 glyph->slice.img.y + dy),
28262 CONSP (hotspot))
28263 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28265 Lisp_Object plist;
28267 /* Could check XCAR (hotspot) to see if we enter/leave
28268 this hot-spot.
28269 If so, we could look for mouse-enter, mouse-leave
28270 properties in PLIST (and do something...). */
28271 hotspot = XCDR (hotspot);
28272 if (CONSP (hotspot)
28273 && (plist = XCAR (hotspot), CONSP (plist)))
28275 pointer = Fplist_get (plist, Qpointer);
28276 if (NILP (pointer))
28277 pointer = Qhand;
28278 help_echo_string = Fplist_get (plist, Qhelp_echo);
28279 if (!NILP (help_echo_string))
28281 help_echo_window = window;
28282 help_echo_object = glyph->object;
28283 help_echo_pos = glyph->charpos;
28287 if (NILP (pointer))
28288 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28291 #endif /* HAVE_WINDOW_SYSTEM */
28293 /* Clear mouse face if X/Y not over text. */
28294 if (glyph == NULL
28295 || area != TEXT_AREA
28296 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28297 /* Glyph's OBJECT is an integer for glyphs inserted by the
28298 display engine for its internal purposes, like truncation
28299 and continuation glyphs and blanks beyond the end of
28300 line's text on text terminals. If we are over such a
28301 glyph, we are not over any text. */
28302 || INTEGERP (glyph->object)
28303 /* R2L rows have a stretch glyph at their front, which
28304 stands for no text, whereas L2R rows have no glyphs at
28305 all beyond the end of text. Treat such stretch glyphs
28306 like we do with NULL glyphs in L2R rows. */
28307 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28308 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28309 && glyph->type == STRETCH_GLYPH
28310 && glyph->avoid_cursor_p))
28312 if (clear_mouse_face (hlinfo))
28313 cursor = No_Cursor;
28314 #ifdef HAVE_WINDOW_SYSTEM
28315 if (FRAME_WINDOW_P (f) && NILP (pointer))
28317 if (area != TEXT_AREA)
28318 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28319 else
28320 pointer = Vvoid_text_area_pointer;
28322 #endif
28323 goto set_cursor;
28326 pos = glyph->charpos;
28327 object = glyph->object;
28328 if (!STRINGP (object) && !BUFFERP (object))
28329 goto set_cursor;
28331 /* If we get an out-of-range value, return now; avoid an error. */
28332 if (BUFFERP (object) && pos > BUF_Z (b))
28333 goto set_cursor;
28335 /* Make the window's buffer temporarily current for
28336 overlays_at and compute_char_face. */
28337 obuf = current_buffer;
28338 current_buffer = b;
28339 obegv = BEGV;
28340 ozv = ZV;
28341 BEGV = BEG;
28342 ZV = Z;
28344 /* Is this char mouse-active or does it have help-echo? */
28345 position = make_number (pos);
28347 if (BUFFERP (object))
28349 /* Put all the overlays we want in a vector in overlay_vec. */
28350 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28351 /* Sort overlays into increasing priority order. */
28352 noverlays = sort_overlays (overlay_vec, noverlays, w);
28354 else
28355 noverlays = 0;
28357 if (NILP (Vmouse_highlight))
28359 clear_mouse_face (hlinfo);
28360 goto check_help_echo;
28363 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28365 if (same_region)
28366 cursor = No_Cursor;
28368 /* Check mouse-face highlighting. */
28369 if (! same_region
28370 /* If there exists an overlay with mouse-face overlapping
28371 the one we are currently highlighting, we have to
28372 check if we enter the overlapping overlay, and then
28373 highlight only that. */
28374 || (OVERLAYP (hlinfo->mouse_face_overlay)
28375 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28377 /* Find the highest priority overlay with a mouse-face. */
28378 Lisp_Object overlay = Qnil;
28379 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28381 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28382 if (!NILP (mouse_face))
28383 overlay = overlay_vec[i];
28386 /* If we're highlighting the same overlay as before, there's
28387 no need to do that again. */
28388 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28389 goto check_help_echo;
28390 hlinfo->mouse_face_overlay = overlay;
28392 /* Clear the display of the old active region, if any. */
28393 if (clear_mouse_face (hlinfo))
28394 cursor = No_Cursor;
28396 /* If no overlay applies, get a text property. */
28397 if (NILP (overlay))
28398 mouse_face = Fget_text_property (position, Qmouse_face, object);
28400 /* Next, compute the bounds of the mouse highlighting and
28401 display it. */
28402 if (!NILP (mouse_face) && STRINGP (object))
28404 /* The mouse-highlighting comes from a display string
28405 with a mouse-face. */
28406 Lisp_Object s, e;
28407 ptrdiff_t ignore;
28409 s = Fprevious_single_property_change
28410 (make_number (pos + 1), Qmouse_face, object, Qnil);
28411 e = Fnext_single_property_change
28412 (position, Qmouse_face, object, Qnil);
28413 if (NILP (s))
28414 s = make_number (0);
28415 if (NILP (e))
28416 e = make_number (SCHARS (object));
28417 mouse_face_from_string_pos (w, hlinfo, object,
28418 XINT (s), XINT (e));
28419 hlinfo->mouse_face_past_end = 0;
28420 hlinfo->mouse_face_window = window;
28421 hlinfo->mouse_face_face_id
28422 = face_at_string_position (w, object, pos, 0, &ignore,
28423 glyph->face_id, 1);
28424 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28425 cursor = No_Cursor;
28427 else
28429 /* The mouse-highlighting, if any, comes from an overlay
28430 or text property in the buffer. */
28431 Lisp_Object buffer IF_LINT (= Qnil);
28432 Lisp_Object disp_string IF_LINT (= Qnil);
28434 if (STRINGP (object))
28436 /* If we are on a display string with no mouse-face,
28437 check if the text under it has one. */
28438 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28439 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28440 pos = string_buffer_position (object, start);
28441 if (pos > 0)
28443 mouse_face = get_char_property_and_overlay
28444 (make_number (pos), Qmouse_face, w->contents, &overlay);
28445 buffer = w->contents;
28446 disp_string = object;
28449 else
28451 buffer = object;
28452 disp_string = Qnil;
28455 if (!NILP (mouse_face))
28457 Lisp_Object before, after;
28458 Lisp_Object before_string, after_string;
28459 /* To correctly find the limits of mouse highlight
28460 in a bidi-reordered buffer, we must not use the
28461 optimization of limiting the search in
28462 previous-single-property-change and
28463 next-single-property-change, because
28464 rows_from_pos_range needs the real start and end
28465 positions to DTRT in this case. That's because
28466 the first row visible in a window does not
28467 necessarily display the character whose position
28468 is the smallest. */
28469 Lisp_Object lim1
28470 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28471 ? Fmarker_position (w->start)
28472 : Qnil;
28473 Lisp_Object lim2
28474 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28475 ? make_number (BUF_Z (XBUFFER (buffer))
28476 - w->window_end_pos)
28477 : Qnil;
28479 if (NILP (overlay))
28481 /* Handle the text property case. */
28482 before = Fprevious_single_property_change
28483 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28484 after = Fnext_single_property_change
28485 (make_number (pos), Qmouse_face, buffer, lim2);
28486 before_string = after_string = Qnil;
28488 else
28490 /* Handle the overlay case. */
28491 before = Foverlay_start (overlay);
28492 after = Foverlay_end (overlay);
28493 before_string = Foverlay_get (overlay, Qbefore_string);
28494 after_string = Foverlay_get (overlay, Qafter_string);
28496 if (!STRINGP (before_string)) before_string = Qnil;
28497 if (!STRINGP (after_string)) after_string = Qnil;
28500 mouse_face_from_buffer_pos (window, hlinfo, pos,
28501 NILP (before)
28503 : XFASTINT (before),
28504 NILP (after)
28505 ? BUF_Z (XBUFFER (buffer))
28506 : XFASTINT (after),
28507 before_string, after_string,
28508 disp_string);
28509 cursor = No_Cursor;
28514 check_help_echo:
28516 /* Look for a `help-echo' property. */
28517 if (NILP (help_echo_string)) {
28518 Lisp_Object help, overlay;
28520 /* Check overlays first. */
28521 help = overlay = Qnil;
28522 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28524 overlay = overlay_vec[i];
28525 help = Foverlay_get (overlay, Qhelp_echo);
28528 if (!NILP (help))
28530 help_echo_string = help;
28531 help_echo_window = window;
28532 help_echo_object = overlay;
28533 help_echo_pos = pos;
28535 else
28537 Lisp_Object obj = glyph->object;
28538 ptrdiff_t charpos = glyph->charpos;
28540 /* Try text properties. */
28541 if (STRINGP (obj)
28542 && charpos >= 0
28543 && charpos < SCHARS (obj))
28545 help = Fget_text_property (make_number (charpos),
28546 Qhelp_echo, obj);
28547 if (NILP (help))
28549 /* If the string itself doesn't specify a help-echo,
28550 see if the buffer text ``under'' it does. */
28551 struct glyph_row *r
28552 = MATRIX_ROW (w->current_matrix, vpos);
28553 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28554 ptrdiff_t p = string_buffer_position (obj, start);
28555 if (p > 0)
28557 help = Fget_char_property (make_number (p),
28558 Qhelp_echo, w->contents);
28559 if (!NILP (help))
28561 charpos = p;
28562 obj = w->contents;
28567 else if (BUFFERP (obj)
28568 && charpos >= BEGV
28569 && charpos < ZV)
28570 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28571 obj);
28573 if (!NILP (help))
28575 help_echo_string = help;
28576 help_echo_window = window;
28577 help_echo_object = obj;
28578 help_echo_pos = charpos;
28583 #ifdef HAVE_WINDOW_SYSTEM
28584 /* Look for a `pointer' property. */
28585 if (FRAME_WINDOW_P (f) && NILP (pointer))
28587 /* Check overlays first. */
28588 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28589 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28591 if (NILP (pointer))
28593 Lisp_Object obj = glyph->object;
28594 ptrdiff_t charpos = glyph->charpos;
28596 /* Try text properties. */
28597 if (STRINGP (obj)
28598 && charpos >= 0
28599 && charpos < SCHARS (obj))
28601 pointer = Fget_text_property (make_number (charpos),
28602 Qpointer, obj);
28603 if (NILP (pointer))
28605 /* If the string itself doesn't specify a pointer,
28606 see if the buffer text ``under'' it does. */
28607 struct glyph_row *r
28608 = MATRIX_ROW (w->current_matrix, vpos);
28609 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28610 ptrdiff_t p = string_buffer_position (obj, start);
28611 if (p > 0)
28612 pointer = Fget_char_property (make_number (p),
28613 Qpointer, w->contents);
28616 else if (BUFFERP (obj)
28617 && charpos >= BEGV
28618 && charpos < ZV)
28619 pointer = Fget_text_property (make_number (charpos),
28620 Qpointer, obj);
28623 #endif /* HAVE_WINDOW_SYSTEM */
28625 BEGV = obegv;
28626 ZV = ozv;
28627 current_buffer = obuf;
28630 set_cursor:
28632 #ifdef HAVE_WINDOW_SYSTEM
28633 if (FRAME_WINDOW_P (f))
28634 define_frame_cursor1 (f, cursor, pointer);
28635 #else
28636 /* This is here to prevent a compiler error, about "label at end of
28637 compound statement". */
28638 return;
28639 #endif
28643 /* EXPORT for RIF:
28644 Clear any mouse-face on window W. This function is part of the
28645 redisplay interface, and is called from try_window_id and similar
28646 functions to ensure the mouse-highlight is off. */
28648 void
28649 x_clear_window_mouse_face (struct window *w)
28651 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28652 Lisp_Object window;
28654 block_input ();
28655 XSETWINDOW (window, w);
28656 if (EQ (window, hlinfo->mouse_face_window))
28657 clear_mouse_face (hlinfo);
28658 unblock_input ();
28662 /* EXPORT:
28663 Just discard the mouse face information for frame F, if any.
28664 This is used when the size of F is changed. */
28666 void
28667 cancel_mouse_face (struct frame *f)
28669 Lisp_Object window;
28670 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28672 window = hlinfo->mouse_face_window;
28673 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28674 reset_mouse_highlight (hlinfo);
28679 /***********************************************************************
28680 Exposure Events
28681 ***********************************************************************/
28683 #ifdef HAVE_WINDOW_SYSTEM
28685 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28686 which intersects rectangle R. R is in window-relative coordinates. */
28688 static void
28689 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28690 enum glyph_row_area area)
28692 struct glyph *first = row->glyphs[area];
28693 struct glyph *end = row->glyphs[area] + row->used[area];
28694 struct glyph *last;
28695 int first_x, start_x, x;
28697 if (area == TEXT_AREA && row->fill_line_p)
28698 /* If row extends face to end of line write the whole line. */
28699 draw_glyphs (w, 0, row, area,
28700 0, row->used[area],
28701 DRAW_NORMAL_TEXT, 0);
28702 else
28704 /* Set START_X to the window-relative start position for drawing glyphs of
28705 AREA. The first glyph of the text area can be partially visible.
28706 The first glyphs of other areas cannot. */
28707 start_x = window_box_left_offset (w, area);
28708 x = start_x;
28709 if (area == TEXT_AREA)
28710 x += row->x;
28712 /* Find the first glyph that must be redrawn. */
28713 while (first < end
28714 && x + first->pixel_width < r->x)
28716 x += first->pixel_width;
28717 ++first;
28720 /* Find the last one. */
28721 last = first;
28722 first_x = x;
28723 while (last < end
28724 && x < r->x + r->width)
28726 x += last->pixel_width;
28727 ++last;
28730 /* Repaint. */
28731 if (last > first)
28732 draw_glyphs (w, first_x - start_x, row, area,
28733 first - row->glyphs[area], last - row->glyphs[area],
28734 DRAW_NORMAL_TEXT, 0);
28739 /* Redraw the parts of the glyph row ROW on window W intersecting
28740 rectangle R. R is in window-relative coordinates. Value is
28741 non-zero if mouse-face was overwritten. */
28743 static int
28744 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28746 eassert (row->enabled_p);
28748 if (row->mode_line_p || w->pseudo_window_p)
28749 draw_glyphs (w, 0, row, TEXT_AREA,
28750 0, row->used[TEXT_AREA],
28751 DRAW_NORMAL_TEXT, 0);
28752 else
28754 if (row->used[LEFT_MARGIN_AREA])
28755 expose_area (w, row, r, LEFT_MARGIN_AREA);
28756 if (row->used[TEXT_AREA])
28757 expose_area (w, row, r, TEXT_AREA);
28758 if (row->used[RIGHT_MARGIN_AREA])
28759 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28760 draw_row_fringe_bitmaps (w, row);
28763 return row->mouse_face_p;
28767 /* Redraw those parts of glyphs rows during expose event handling that
28768 overlap other rows. Redrawing of an exposed line writes over parts
28769 of lines overlapping that exposed line; this function fixes that.
28771 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28772 row in W's current matrix that is exposed and overlaps other rows.
28773 LAST_OVERLAPPING_ROW is the last such row. */
28775 static void
28776 expose_overlaps (struct window *w,
28777 struct glyph_row *first_overlapping_row,
28778 struct glyph_row *last_overlapping_row,
28779 XRectangle *r)
28781 struct glyph_row *row;
28783 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28784 if (row->overlapping_p)
28786 eassert (row->enabled_p && !row->mode_line_p);
28788 row->clip = r;
28789 if (row->used[LEFT_MARGIN_AREA])
28790 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28792 if (row->used[TEXT_AREA])
28793 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28795 if (row->used[RIGHT_MARGIN_AREA])
28796 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28797 row->clip = NULL;
28802 /* Return non-zero if W's cursor intersects rectangle R. */
28804 static int
28805 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28807 XRectangle cr, result;
28808 struct glyph *cursor_glyph;
28809 struct glyph_row *row;
28811 if (w->phys_cursor.vpos >= 0
28812 && w->phys_cursor.vpos < w->current_matrix->nrows
28813 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28814 row->enabled_p)
28815 && row->cursor_in_fringe_p)
28817 /* Cursor is in the fringe. */
28818 cr.x = window_box_right_offset (w,
28819 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28820 ? RIGHT_MARGIN_AREA
28821 : TEXT_AREA));
28822 cr.y = row->y;
28823 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28824 cr.height = row->height;
28825 return x_intersect_rectangles (&cr, r, &result);
28828 cursor_glyph = get_phys_cursor_glyph (w);
28829 if (cursor_glyph)
28831 /* r is relative to W's box, but w->phys_cursor.x is relative
28832 to left edge of W's TEXT area. Adjust it. */
28833 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28834 cr.y = w->phys_cursor.y;
28835 cr.width = cursor_glyph->pixel_width;
28836 cr.height = w->phys_cursor_height;
28837 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28838 I assume the effect is the same -- and this is portable. */
28839 return x_intersect_rectangles (&cr, r, &result);
28841 /* If we don't understand the format, pretend we're not in the hot-spot. */
28842 return 0;
28846 /* EXPORT:
28847 Draw a vertical window border to the right of window W if W doesn't
28848 have vertical scroll bars. */
28850 void
28851 x_draw_vertical_border (struct window *w)
28853 struct frame *f = XFRAME (WINDOW_FRAME (w));
28855 /* We could do better, if we knew what type of scroll-bar the adjacent
28856 windows (on either side) have... But we don't :-(
28857 However, I think this works ok. ++KFS 2003-04-25 */
28859 /* Redraw borders between horizontally adjacent windows. Don't
28860 do it for frames with vertical scroll bars because either the
28861 right scroll bar of a window, or the left scroll bar of its
28862 neighbor will suffice as a border. */
28863 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28864 return;
28866 /* Note: It is necessary to redraw both the left and the right
28867 borders, for when only this single window W is being
28868 redisplayed. */
28869 if (!WINDOW_RIGHTMOST_P (w)
28870 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28872 int x0, x1, y0, y1;
28874 window_box_edges (w, &x0, &y0, &x1, &y1);
28875 y1 -= 1;
28877 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28878 x1 -= 1;
28880 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28882 if (!WINDOW_LEFTMOST_P (w)
28883 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28885 int x0, x1, y0, y1;
28887 window_box_edges (w, &x0, &y0, &x1, &y1);
28888 y1 -= 1;
28890 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28891 x0 -= 1;
28893 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28898 /* Redraw the part of window W intersection rectangle FR. Pixel
28899 coordinates in FR are frame-relative. Call this function with
28900 input blocked. Value is non-zero if the exposure overwrites
28901 mouse-face. */
28903 static int
28904 expose_window (struct window *w, XRectangle *fr)
28906 struct frame *f = XFRAME (w->frame);
28907 XRectangle wr, r;
28908 int mouse_face_overwritten_p = 0;
28910 /* If window is not yet fully initialized, do nothing. This can
28911 happen when toolkit scroll bars are used and a window is split.
28912 Reconfiguring the scroll bar will generate an expose for a newly
28913 created window. */
28914 if (w->current_matrix == NULL)
28915 return 0;
28917 /* When we're currently updating the window, display and current
28918 matrix usually don't agree. Arrange for a thorough display
28919 later. */
28920 if (w->must_be_updated_p)
28922 SET_FRAME_GARBAGED (f);
28923 return 0;
28926 /* Frame-relative pixel rectangle of W. */
28927 wr.x = WINDOW_LEFT_EDGE_X (w);
28928 wr.y = WINDOW_TOP_EDGE_Y (w);
28929 wr.width = WINDOW_TOTAL_WIDTH (w);
28930 wr.height = WINDOW_TOTAL_HEIGHT (w);
28932 if (x_intersect_rectangles (fr, &wr, &r))
28934 int yb = window_text_bottom_y (w);
28935 struct glyph_row *row;
28936 int cursor_cleared_p, phys_cursor_on_p;
28937 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28939 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28940 r.x, r.y, r.width, r.height));
28942 /* Convert to window coordinates. */
28943 r.x -= WINDOW_LEFT_EDGE_X (w);
28944 r.y -= WINDOW_TOP_EDGE_Y (w);
28946 /* Turn off the cursor. */
28947 if (!w->pseudo_window_p
28948 && phys_cursor_in_rect_p (w, &r))
28950 x_clear_cursor (w);
28951 cursor_cleared_p = 1;
28953 else
28954 cursor_cleared_p = 0;
28956 /* If the row containing the cursor extends face to end of line,
28957 then expose_area might overwrite the cursor outside the
28958 rectangle and thus notice_overwritten_cursor might clear
28959 w->phys_cursor_on_p. We remember the original value and
28960 check later if it is changed. */
28961 phys_cursor_on_p = w->phys_cursor_on_p;
28963 /* Update lines intersecting rectangle R. */
28964 first_overlapping_row = last_overlapping_row = NULL;
28965 for (row = w->current_matrix->rows;
28966 row->enabled_p;
28967 ++row)
28969 int y0 = row->y;
28970 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28972 if ((y0 >= r.y && y0 < r.y + r.height)
28973 || (y1 > r.y && y1 < r.y + r.height)
28974 || (r.y >= y0 && r.y < y1)
28975 || (r.y + r.height > y0 && r.y + r.height < y1))
28977 /* A header line may be overlapping, but there is no need
28978 to fix overlapping areas for them. KFS 2005-02-12 */
28979 if (row->overlapping_p && !row->mode_line_p)
28981 if (first_overlapping_row == NULL)
28982 first_overlapping_row = row;
28983 last_overlapping_row = row;
28986 row->clip = fr;
28987 if (expose_line (w, row, &r))
28988 mouse_face_overwritten_p = 1;
28989 row->clip = NULL;
28991 else if (row->overlapping_p)
28993 /* We must redraw a row overlapping the exposed area. */
28994 if (y0 < r.y
28995 ? y0 + row->phys_height > r.y
28996 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28998 if (first_overlapping_row == NULL)
28999 first_overlapping_row = row;
29000 last_overlapping_row = row;
29004 if (y1 >= yb)
29005 break;
29008 /* Display the mode line if there is one. */
29009 if (WINDOW_WANTS_MODELINE_P (w)
29010 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29011 row->enabled_p)
29012 && row->y < r.y + r.height)
29014 if (expose_line (w, row, &r))
29015 mouse_face_overwritten_p = 1;
29018 if (!w->pseudo_window_p)
29020 /* Fix the display of overlapping rows. */
29021 if (first_overlapping_row)
29022 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29023 fr);
29025 /* Draw border between windows. */
29026 x_draw_vertical_border (w);
29028 /* Turn the cursor on again. */
29029 if (cursor_cleared_p
29030 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29031 update_window_cursor (w, 1);
29035 return mouse_face_overwritten_p;
29040 /* Redraw (parts) of all windows in the window tree rooted at W that
29041 intersect R. R contains frame pixel coordinates. Value is
29042 non-zero if the exposure overwrites mouse-face. */
29044 static int
29045 expose_window_tree (struct window *w, XRectangle *r)
29047 struct frame *f = XFRAME (w->frame);
29048 int mouse_face_overwritten_p = 0;
29050 while (w && !FRAME_GARBAGED_P (f))
29052 if (WINDOWP (w->contents))
29053 mouse_face_overwritten_p
29054 |= expose_window_tree (XWINDOW (w->contents), r);
29055 else
29056 mouse_face_overwritten_p |= expose_window (w, r);
29058 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29061 return mouse_face_overwritten_p;
29065 /* EXPORT:
29066 Redisplay an exposed area of frame F. X and Y are the upper-left
29067 corner of the exposed rectangle. W and H are width and height of
29068 the exposed area. All are pixel values. W or H zero means redraw
29069 the entire frame. */
29071 void
29072 expose_frame (struct frame *f, int x, int y, int w, int h)
29074 XRectangle r;
29075 int mouse_face_overwritten_p = 0;
29077 TRACE ((stderr, "expose_frame "));
29079 /* No need to redraw if frame will be redrawn soon. */
29080 if (FRAME_GARBAGED_P (f))
29082 TRACE ((stderr, " garbaged\n"));
29083 return;
29086 /* If basic faces haven't been realized yet, there is no point in
29087 trying to redraw anything. This can happen when we get an expose
29088 event while Emacs is starting, e.g. by moving another window. */
29089 if (FRAME_FACE_CACHE (f) == NULL
29090 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29092 TRACE ((stderr, " no faces\n"));
29093 return;
29096 if (w == 0 || h == 0)
29098 r.x = r.y = 0;
29099 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29100 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29102 else
29104 r.x = x;
29105 r.y = y;
29106 r.width = w;
29107 r.height = h;
29110 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29111 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29113 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29114 if (WINDOWP (f->tool_bar_window))
29115 mouse_face_overwritten_p
29116 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29117 #endif
29119 #ifdef HAVE_X_WINDOWS
29120 #ifndef MSDOS
29121 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29122 if (WINDOWP (f->menu_bar_window))
29123 mouse_face_overwritten_p
29124 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29125 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29126 #endif
29127 #endif
29129 /* Some window managers support a focus-follows-mouse style with
29130 delayed raising of frames. Imagine a partially obscured frame,
29131 and moving the mouse into partially obscured mouse-face on that
29132 frame. The visible part of the mouse-face will be highlighted,
29133 then the WM raises the obscured frame. With at least one WM, KDE
29134 2.1, Emacs is not getting any event for the raising of the frame
29135 (even tried with SubstructureRedirectMask), only Expose events.
29136 These expose events will draw text normally, i.e. not
29137 highlighted. Which means we must redo the highlight here.
29138 Subsume it under ``we love X''. --gerd 2001-08-15 */
29139 /* Included in Windows version because Windows most likely does not
29140 do the right thing if any third party tool offers
29141 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29142 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29144 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29145 if (f == hlinfo->mouse_face_mouse_frame)
29147 int mouse_x = hlinfo->mouse_face_mouse_x;
29148 int mouse_y = hlinfo->mouse_face_mouse_y;
29149 clear_mouse_face (hlinfo);
29150 note_mouse_highlight (f, mouse_x, mouse_y);
29156 /* EXPORT:
29157 Determine the intersection of two rectangles R1 and R2. Return
29158 the intersection in *RESULT. Value is non-zero if RESULT is not
29159 empty. */
29162 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29164 XRectangle *left, *right;
29165 XRectangle *upper, *lower;
29166 int intersection_p = 0;
29168 /* Rearrange so that R1 is the left-most rectangle. */
29169 if (r1->x < r2->x)
29170 left = r1, right = r2;
29171 else
29172 left = r2, right = r1;
29174 /* X0 of the intersection is right.x0, if this is inside R1,
29175 otherwise there is no intersection. */
29176 if (right->x <= left->x + left->width)
29178 result->x = right->x;
29180 /* The right end of the intersection is the minimum of
29181 the right ends of left and right. */
29182 result->width = (min (left->x + left->width, right->x + right->width)
29183 - result->x);
29185 /* Same game for Y. */
29186 if (r1->y < r2->y)
29187 upper = r1, lower = r2;
29188 else
29189 upper = r2, lower = r1;
29191 /* The upper end of the intersection is lower.y0, if this is inside
29192 of upper. Otherwise, there is no intersection. */
29193 if (lower->y <= upper->y + upper->height)
29195 result->y = lower->y;
29197 /* The lower end of the intersection is the minimum of the lower
29198 ends of upper and lower. */
29199 result->height = (min (lower->y + lower->height,
29200 upper->y + upper->height)
29201 - result->y);
29202 intersection_p = 1;
29206 return intersection_p;
29209 #endif /* HAVE_WINDOW_SYSTEM */
29212 /***********************************************************************
29213 Initialization
29214 ***********************************************************************/
29216 void
29217 syms_of_xdisp (void)
29219 Vwith_echo_area_save_vector = Qnil;
29220 staticpro (&Vwith_echo_area_save_vector);
29222 Vmessage_stack = Qnil;
29223 staticpro (&Vmessage_stack);
29225 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29226 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29228 message_dolog_marker1 = Fmake_marker ();
29229 staticpro (&message_dolog_marker1);
29230 message_dolog_marker2 = Fmake_marker ();
29231 staticpro (&message_dolog_marker2);
29232 message_dolog_marker3 = Fmake_marker ();
29233 staticpro (&message_dolog_marker3);
29235 #ifdef GLYPH_DEBUG
29236 defsubr (&Sdump_frame_glyph_matrix);
29237 defsubr (&Sdump_glyph_matrix);
29238 defsubr (&Sdump_glyph_row);
29239 defsubr (&Sdump_tool_bar_row);
29240 defsubr (&Strace_redisplay);
29241 defsubr (&Strace_to_stderr);
29242 #endif
29243 #ifdef HAVE_WINDOW_SYSTEM
29244 defsubr (&Stool_bar_lines_needed);
29245 defsubr (&Slookup_image_map);
29246 #endif
29247 defsubr (&Sline_pixel_height);
29248 defsubr (&Sformat_mode_line);
29249 defsubr (&Sinvisible_p);
29250 defsubr (&Scurrent_bidi_paragraph_direction);
29251 defsubr (&Smove_point_visually);
29253 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29254 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29255 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29256 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29257 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29258 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29259 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29260 DEFSYM (Qeval, "eval");
29261 DEFSYM (QCdata, ":data");
29262 DEFSYM (Qdisplay, "display");
29263 DEFSYM (Qspace_width, "space-width");
29264 DEFSYM (Qraise, "raise");
29265 DEFSYM (Qslice, "slice");
29266 DEFSYM (Qspace, "space");
29267 DEFSYM (Qmargin, "margin");
29268 DEFSYM (Qpointer, "pointer");
29269 DEFSYM (Qleft_margin, "left-margin");
29270 DEFSYM (Qright_margin, "right-margin");
29271 DEFSYM (Qcenter, "center");
29272 DEFSYM (Qline_height, "line-height");
29273 DEFSYM (QCalign_to, ":align-to");
29274 DEFSYM (QCrelative_width, ":relative-width");
29275 DEFSYM (QCrelative_height, ":relative-height");
29276 DEFSYM (QCeval, ":eval");
29277 DEFSYM (QCpropertize, ":propertize");
29278 DEFSYM (QCfile, ":file");
29279 DEFSYM (Qfontified, "fontified");
29280 DEFSYM (Qfontification_functions, "fontification-functions");
29281 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29282 DEFSYM (Qescape_glyph, "escape-glyph");
29283 DEFSYM (Qnobreak_space, "nobreak-space");
29284 DEFSYM (Qimage, "image");
29285 DEFSYM (Qtext, "text");
29286 DEFSYM (Qboth, "both");
29287 DEFSYM (Qboth_horiz, "both-horiz");
29288 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29289 DEFSYM (QCmap, ":map");
29290 DEFSYM (QCpointer, ":pointer");
29291 DEFSYM (Qrect, "rect");
29292 DEFSYM (Qcircle, "circle");
29293 DEFSYM (Qpoly, "poly");
29294 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29295 DEFSYM (Qgrow_only, "grow-only");
29296 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29297 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29298 DEFSYM (Qposition, "position");
29299 DEFSYM (Qbuffer_position, "buffer-position");
29300 DEFSYM (Qobject, "object");
29301 DEFSYM (Qbar, "bar");
29302 DEFSYM (Qhbar, "hbar");
29303 DEFSYM (Qbox, "box");
29304 DEFSYM (Qhollow, "hollow");
29305 DEFSYM (Qhand, "hand");
29306 DEFSYM (Qarrow, "arrow");
29307 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29309 list_of_error = list1 (list2 (intern_c_string ("error"),
29310 intern_c_string ("void-variable")));
29311 staticpro (&list_of_error);
29313 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29314 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29315 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29316 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29318 echo_buffer[0] = echo_buffer[1] = Qnil;
29319 staticpro (&echo_buffer[0]);
29320 staticpro (&echo_buffer[1]);
29322 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29323 staticpro (&echo_area_buffer[0]);
29324 staticpro (&echo_area_buffer[1]);
29326 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29327 staticpro (&Vmessages_buffer_name);
29329 mode_line_proptrans_alist = Qnil;
29330 staticpro (&mode_line_proptrans_alist);
29331 mode_line_string_list = Qnil;
29332 staticpro (&mode_line_string_list);
29333 mode_line_string_face = Qnil;
29334 staticpro (&mode_line_string_face);
29335 mode_line_string_face_prop = Qnil;
29336 staticpro (&mode_line_string_face_prop);
29337 Vmode_line_unwind_vector = Qnil;
29338 staticpro (&Vmode_line_unwind_vector);
29340 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29342 help_echo_string = Qnil;
29343 staticpro (&help_echo_string);
29344 help_echo_object = Qnil;
29345 staticpro (&help_echo_object);
29346 help_echo_window = Qnil;
29347 staticpro (&help_echo_window);
29348 previous_help_echo_string = Qnil;
29349 staticpro (&previous_help_echo_string);
29350 help_echo_pos = -1;
29352 DEFSYM (Qright_to_left, "right-to-left");
29353 DEFSYM (Qleft_to_right, "left-to-right");
29355 #ifdef HAVE_WINDOW_SYSTEM
29356 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29357 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29358 For example, if a block cursor is over a tab, it will be drawn as
29359 wide as that tab on the display. */);
29360 x_stretch_cursor_p = 0;
29361 #endif
29363 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29364 doc: /* Non-nil means highlight trailing whitespace.
29365 The face used for trailing whitespace is `trailing-whitespace'. */);
29366 Vshow_trailing_whitespace = Qnil;
29368 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29369 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29370 If the value is t, Emacs highlights non-ASCII chars which have the
29371 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29372 or `escape-glyph' face respectively.
29374 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29375 U+2011 (non-breaking hyphen) are affected.
29377 Any other non-nil value means to display these characters as a escape
29378 glyph followed by an ordinary space or hyphen.
29380 A value of nil means no special handling of these characters. */);
29381 Vnobreak_char_display = Qt;
29383 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29384 doc: /* The pointer shape to show in void text areas.
29385 A value of nil means to show the text pointer. Other options are `arrow',
29386 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29387 Vvoid_text_area_pointer = Qarrow;
29389 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29390 doc: /* Non-nil means don't actually do any redisplay.
29391 This is used for internal purposes. */);
29392 Vinhibit_redisplay = Qnil;
29394 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29395 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29396 Vglobal_mode_string = Qnil;
29398 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29399 doc: /* Marker for where to display an arrow on top of the buffer text.
29400 This must be the beginning of a line in order to work.
29401 See also `overlay-arrow-string'. */);
29402 Voverlay_arrow_position = Qnil;
29404 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29405 doc: /* String to display as an arrow in non-window frames.
29406 See also `overlay-arrow-position'. */);
29407 Voverlay_arrow_string = build_pure_c_string ("=>");
29409 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29410 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29411 The symbols on this list are examined during redisplay to determine
29412 where to display overlay arrows. */);
29413 Voverlay_arrow_variable_list
29414 = list1 (intern_c_string ("overlay-arrow-position"));
29416 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29417 doc: /* The number of lines to try scrolling a window by when point moves out.
29418 If that fails to bring point back on frame, point is centered instead.
29419 If this is zero, point is always centered after it moves off frame.
29420 If you want scrolling to always be a line at a time, you should set
29421 `scroll-conservatively' to a large value rather than set this to 1. */);
29423 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29424 doc: /* Scroll up to this many lines, to bring point back on screen.
29425 If point moves off-screen, redisplay will scroll by up to
29426 `scroll-conservatively' lines in order to bring point just barely
29427 onto the screen again. If that cannot be done, then redisplay
29428 recenters point as usual.
29430 If the value is greater than 100, redisplay will never recenter point,
29431 but will always scroll just enough text to bring point into view, even
29432 if you move far away.
29434 A value of zero means always recenter point if it moves off screen. */);
29435 scroll_conservatively = 0;
29437 DEFVAR_INT ("scroll-margin", scroll_margin,
29438 doc: /* Number of lines of margin at the top and bottom of a window.
29439 Recenter the window whenever point gets within this many lines
29440 of the top or bottom of the window. */);
29441 scroll_margin = 0;
29443 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29444 doc: /* Pixels per inch value for non-window system displays.
29445 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29446 Vdisplay_pixels_per_inch = make_float (72.0);
29448 #ifdef GLYPH_DEBUG
29449 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29450 #endif
29452 DEFVAR_LISP ("truncate-partial-width-windows",
29453 Vtruncate_partial_width_windows,
29454 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29455 For an integer value, truncate lines in each window narrower than the
29456 full frame width, provided the window width is less than that integer;
29457 otherwise, respect the value of `truncate-lines'.
29459 For any other non-nil value, truncate lines in all windows that do
29460 not span the full frame width.
29462 A value of nil means to respect the value of `truncate-lines'.
29464 If `word-wrap' is enabled, you might want to reduce this. */);
29465 Vtruncate_partial_width_windows = make_number (50);
29467 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29468 doc: /* Maximum buffer size for which line number should be displayed.
29469 If the buffer is bigger than this, the line number does not appear
29470 in the mode line. A value of nil means no limit. */);
29471 Vline_number_display_limit = Qnil;
29473 DEFVAR_INT ("line-number-display-limit-width",
29474 line_number_display_limit_width,
29475 doc: /* Maximum line width (in characters) for line number display.
29476 If the average length of the lines near point is bigger than this, then the
29477 line number may be omitted from the mode line. */);
29478 line_number_display_limit_width = 200;
29480 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29481 doc: /* Non-nil means highlight region even in nonselected windows. */);
29482 highlight_nonselected_windows = 0;
29484 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29485 doc: /* Non-nil if more than one frame is visible on this display.
29486 Minibuffer-only frames don't count, but iconified frames do.
29487 This variable is not guaranteed to be accurate except while processing
29488 `frame-title-format' and `icon-title-format'. */);
29490 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29491 doc: /* Template for displaying the title bar of visible frames.
29492 \(Assuming the window manager supports this feature.)
29494 This variable has the same structure as `mode-line-format', except that
29495 the %c and %l constructs are ignored. It is used only on frames for
29496 which no explicit name has been set \(see `modify-frame-parameters'). */);
29498 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29499 doc: /* Template for displaying the title bar of an iconified frame.
29500 \(Assuming the window manager supports this feature.)
29501 This variable has the same structure as `mode-line-format' (which see),
29502 and is used only on frames for which no explicit name has been set
29503 \(see `modify-frame-parameters'). */);
29504 Vicon_title_format
29505 = Vframe_title_format
29506 = listn (CONSTYPE_PURE, 3,
29507 intern_c_string ("multiple-frames"),
29508 build_pure_c_string ("%b"),
29509 listn (CONSTYPE_PURE, 4,
29510 empty_unibyte_string,
29511 intern_c_string ("invocation-name"),
29512 build_pure_c_string ("@"),
29513 intern_c_string ("system-name")));
29515 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29516 doc: /* Maximum number of lines to keep in the message log buffer.
29517 If nil, disable message logging. If t, log messages but don't truncate
29518 the buffer when it becomes large. */);
29519 Vmessage_log_max = make_number (1000);
29521 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29522 doc: /* Functions called before redisplay, if window sizes have changed.
29523 The value should be a list of functions that take one argument.
29524 Just before redisplay, for each frame, if any of its windows have changed
29525 size since the last redisplay, or have been split or deleted,
29526 all the functions in the list are called, with the frame as argument. */);
29527 Vwindow_size_change_functions = Qnil;
29529 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29530 doc: /* List of functions to call before redisplaying a window with scrolling.
29531 Each function is called with two arguments, the window and its new
29532 display-start position. Note that these functions are also called by
29533 `set-window-buffer'. Also note that the value of `window-end' is not
29534 valid when these functions are called.
29536 Warning: Do not use this feature to alter the way the window
29537 is scrolled. It is not designed for that, and such use probably won't
29538 work. */);
29539 Vwindow_scroll_functions = Qnil;
29541 DEFVAR_LISP ("window-text-change-functions",
29542 Vwindow_text_change_functions,
29543 doc: /* Functions to call in redisplay when text in the window might change. */);
29544 Vwindow_text_change_functions = Qnil;
29546 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29547 doc: /* Functions called when redisplay of a window reaches the end trigger.
29548 Each function is called with two arguments, the window and the end trigger value.
29549 See `set-window-redisplay-end-trigger'. */);
29550 Vredisplay_end_trigger_functions = Qnil;
29552 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29553 doc: /* Non-nil means autoselect window with mouse pointer.
29554 If nil, do not autoselect windows.
29555 A positive number means delay autoselection by that many seconds: a
29556 window is autoselected only after the mouse has remained in that
29557 window for the duration of the delay.
29558 A negative number has a similar effect, but causes windows to be
29559 autoselected only after the mouse has stopped moving. \(Because of
29560 the way Emacs compares mouse events, you will occasionally wait twice
29561 that time before the window gets selected.\)
29562 Any other value means to autoselect window instantaneously when the
29563 mouse pointer enters it.
29565 Autoselection selects the minibuffer only if it is active, and never
29566 unselects the minibuffer if it is active.
29568 When customizing this variable make sure that the actual value of
29569 `focus-follows-mouse' matches the behavior of your window manager. */);
29570 Vmouse_autoselect_window = Qnil;
29572 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29573 doc: /* Non-nil means automatically resize tool-bars.
29574 This dynamically changes the tool-bar's height to the minimum height
29575 that is needed to make all tool-bar items visible.
29576 If value is `grow-only', the tool-bar's height is only increased
29577 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29578 Vauto_resize_tool_bars = Qt;
29580 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29581 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29582 auto_raise_tool_bar_buttons_p = 1;
29584 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29585 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29586 make_cursor_line_fully_visible_p = 1;
29588 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29589 doc: /* Border below tool-bar in pixels.
29590 If an integer, use it as the height of the border.
29591 If it is one of `internal-border-width' or `border-width', use the
29592 value of the corresponding frame parameter.
29593 Otherwise, no border is added below the tool-bar. */);
29594 Vtool_bar_border = Qinternal_border_width;
29596 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29597 doc: /* Margin around tool-bar buttons in pixels.
29598 If an integer, use that for both horizontal and vertical margins.
29599 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29600 HORZ specifying the horizontal margin, and VERT specifying the
29601 vertical margin. */);
29602 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29604 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29605 doc: /* Relief thickness of tool-bar buttons. */);
29606 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29608 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29609 doc: /* Tool bar style to use.
29610 It can be one of
29611 image - show images only
29612 text - show text only
29613 both - show both, text below image
29614 both-horiz - show text to the right of the image
29615 text-image-horiz - show text to the left of the image
29616 any other - use system default or image if no system default.
29618 This variable only affects the GTK+ toolkit version of Emacs. */);
29619 Vtool_bar_style = Qnil;
29621 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29622 doc: /* Maximum number of characters a label can have to be shown.
29623 The tool bar style must also show labels for this to have any effect, see
29624 `tool-bar-style'. */);
29625 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29627 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29628 doc: /* List of functions to call to fontify regions of text.
29629 Each function is called with one argument POS. Functions must
29630 fontify a region starting at POS in the current buffer, and give
29631 fontified regions the property `fontified'. */);
29632 Vfontification_functions = Qnil;
29633 Fmake_variable_buffer_local (Qfontification_functions);
29635 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29636 unibyte_display_via_language_environment,
29637 doc: /* Non-nil means display unibyte text according to language environment.
29638 Specifically, this means that raw bytes in the range 160-255 decimal
29639 are displayed by converting them to the equivalent multibyte characters
29640 according to the current language environment. As a result, they are
29641 displayed according to the current fontset.
29643 Note that this variable affects only how these bytes are displayed,
29644 but does not change the fact they are interpreted as raw bytes. */);
29645 unibyte_display_via_language_environment = 0;
29647 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29648 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29649 If a float, it specifies a fraction of the mini-window frame's height.
29650 If an integer, it specifies a number of lines. */);
29651 Vmax_mini_window_height = make_float (0.25);
29653 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29654 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29655 A value of nil means don't automatically resize mini-windows.
29656 A value of t means resize them to fit the text displayed in them.
29657 A value of `grow-only', the default, means let mini-windows grow only;
29658 they return to their normal size when the minibuffer is closed, or the
29659 echo area becomes empty. */);
29660 Vresize_mini_windows = Qgrow_only;
29662 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29663 doc: /* Alist specifying how to blink the cursor off.
29664 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29665 `cursor-type' frame-parameter or variable equals ON-STATE,
29666 comparing using `equal', Emacs uses OFF-STATE to specify
29667 how to blink it off. ON-STATE and OFF-STATE are values for
29668 the `cursor-type' frame parameter.
29670 If a frame's ON-STATE has no entry in this list,
29671 the frame's other specifications determine how to blink the cursor off. */);
29672 Vblink_cursor_alist = Qnil;
29674 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29675 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29676 If non-nil, windows are automatically scrolled horizontally to make
29677 point visible. */);
29678 automatic_hscrolling_p = 1;
29679 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29681 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29682 doc: /* How many columns away from the window edge point is allowed to get
29683 before automatic hscrolling will horizontally scroll the window. */);
29684 hscroll_margin = 5;
29686 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29687 doc: /* How many columns to scroll the window when point gets too close to the edge.
29688 When point is less than `hscroll-margin' columns from the window
29689 edge, automatic hscrolling will scroll the window by the amount of columns
29690 determined by this variable. If its value is a positive integer, scroll that
29691 many columns. If it's a positive floating-point number, it specifies the
29692 fraction of the window's width to scroll. If it's nil or zero, point will be
29693 centered horizontally after the scroll. Any other value, including negative
29694 numbers, are treated as if the value were zero.
29696 Automatic hscrolling always moves point outside the scroll margin, so if
29697 point was more than scroll step columns inside the margin, the window will
29698 scroll more than the value given by the scroll step.
29700 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29701 and `scroll-right' overrides this variable's effect. */);
29702 Vhscroll_step = make_number (0);
29704 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29705 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29706 Bind this around calls to `message' to let it take effect. */);
29707 message_truncate_lines = 0;
29709 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29710 doc: /* Normal hook run to update the menu bar definitions.
29711 Redisplay runs this hook before it redisplays the menu bar.
29712 This is used to update submenus such as Buffers,
29713 whose contents depend on various data. */);
29714 Vmenu_bar_update_hook = Qnil;
29716 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29717 doc: /* Frame for which we are updating a menu.
29718 The enable predicate for a menu binding should check this variable. */);
29719 Vmenu_updating_frame = Qnil;
29721 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29722 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29723 inhibit_menubar_update = 0;
29725 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29726 doc: /* Prefix prepended to all continuation lines at display time.
29727 The value may be a string, an image, or a stretch-glyph; it is
29728 interpreted in the same way as the value of a `display' text property.
29730 This variable is overridden by any `wrap-prefix' text or overlay
29731 property.
29733 To add a prefix to non-continuation lines, use `line-prefix'. */);
29734 Vwrap_prefix = Qnil;
29735 DEFSYM (Qwrap_prefix, "wrap-prefix");
29736 Fmake_variable_buffer_local (Qwrap_prefix);
29738 DEFVAR_LISP ("line-prefix", Vline_prefix,
29739 doc: /* Prefix prepended to all non-continuation lines at display time.
29740 The value may be a string, an image, or a stretch-glyph; it is
29741 interpreted in the same way as the value of a `display' text property.
29743 This variable is overridden by any `line-prefix' text or overlay
29744 property.
29746 To add a prefix to continuation lines, use `wrap-prefix'. */);
29747 Vline_prefix = Qnil;
29748 DEFSYM (Qline_prefix, "line-prefix");
29749 Fmake_variable_buffer_local (Qline_prefix);
29751 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29752 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29753 inhibit_eval_during_redisplay = 0;
29755 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29756 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29757 inhibit_free_realized_faces = 0;
29759 #ifdef GLYPH_DEBUG
29760 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29761 doc: /* Inhibit try_window_id display optimization. */);
29762 inhibit_try_window_id = 0;
29764 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29765 doc: /* Inhibit try_window_reusing display optimization. */);
29766 inhibit_try_window_reusing = 0;
29768 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29769 doc: /* Inhibit try_cursor_movement display optimization. */);
29770 inhibit_try_cursor_movement = 0;
29771 #endif /* GLYPH_DEBUG */
29773 DEFVAR_INT ("overline-margin", overline_margin,
29774 doc: /* Space between overline and text, in pixels.
29775 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29776 margin to the character height. */);
29777 overline_margin = 2;
29779 DEFVAR_INT ("underline-minimum-offset",
29780 underline_minimum_offset,
29781 doc: /* Minimum distance between baseline and underline.
29782 This can improve legibility of underlined text at small font sizes,
29783 particularly when using variable `x-use-underline-position-properties'
29784 with fonts that specify an UNDERLINE_POSITION relatively close to the
29785 baseline. The default value is 1. */);
29786 underline_minimum_offset = 1;
29788 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29789 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29790 This feature only works when on a window system that can change
29791 cursor shapes. */);
29792 display_hourglass_p = 1;
29794 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29795 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29796 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29798 #ifdef HAVE_WINDOW_SYSTEM
29799 hourglass_atimer = NULL;
29800 hourglass_shown_p = 0;
29801 #endif /* HAVE_WINDOW_SYSTEM */
29803 DEFSYM (Qglyphless_char, "glyphless-char");
29804 DEFSYM (Qhex_code, "hex-code");
29805 DEFSYM (Qempty_box, "empty-box");
29806 DEFSYM (Qthin_space, "thin-space");
29807 DEFSYM (Qzero_width, "zero-width");
29809 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29810 doc: /* Function run just before redisplay.
29811 It is called with one argument, which is the set of windows that are to
29812 be redisplayed. This set can be nil (meaning, only the selected window),
29813 or t (meaning all windows). */);
29814 Vpre_redisplay_function = intern ("ignore");
29816 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29817 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29819 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29820 doc: /* Char-table defining glyphless characters.
29821 Each element, if non-nil, should be one of the following:
29822 an ASCII acronym string: display this string in a box
29823 `hex-code': display the hexadecimal code of a character in a box
29824 `empty-box': display as an empty box
29825 `thin-space': display as 1-pixel width space
29826 `zero-width': don't display
29827 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29828 display method for graphical terminals and text terminals respectively.
29829 GRAPHICAL and TEXT should each have one of the values listed above.
29831 The char-table has one extra slot to control the display of a character for
29832 which no font is found. This slot only takes effect on graphical terminals.
29833 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29834 `thin-space'. The default is `empty-box'.
29836 If a character has a non-nil entry in an active display table, the
29837 display table takes effect; in this case, Emacs does not consult
29838 `glyphless-char-display' at all. */);
29839 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29840 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29841 Qempty_box);
29843 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29844 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29845 Vdebug_on_message = Qnil;
29847 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
29848 doc: /* */);
29849 Vredisplay__all_windows_cause
29850 = Fmake_vector (make_number (100), make_number (0));
29852 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
29853 doc: /* */);
29854 Vredisplay__mode_lines_cause
29855 = Fmake_vector (make_number (100), make_number (0));
29859 /* Initialize this module when Emacs starts. */
29861 void
29862 init_xdisp (void)
29864 CHARPOS (this_line_start_pos) = 0;
29866 if (!noninteractive)
29868 struct window *m = XWINDOW (minibuf_window);
29869 Lisp_Object frame = m->frame;
29870 struct frame *f = XFRAME (frame);
29871 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29872 struct window *r = XWINDOW (root);
29873 int i;
29875 echo_area_window = minibuf_window;
29877 r->top_line = FRAME_TOP_MARGIN (f);
29878 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29879 r->total_cols = FRAME_COLS (f);
29881 m->top_line = FRAME_LINES (f) - 1;
29882 m->total_lines = 1;
29883 m->total_cols = FRAME_COLS (f);
29885 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29886 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29887 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29889 /* The default ellipsis glyphs `...'. */
29890 for (i = 0; i < 3; ++i)
29891 default_invis_vector[i] = make_number ('.');
29895 /* Allocate the buffer for frame titles.
29896 Also used for `format-mode-line'. */
29897 int size = 100;
29898 mode_line_noprop_buf = xmalloc (size);
29899 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29900 mode_line_noprop_ptr = mode_line_noprop_buf;
29901 mode_line_target = MODE_LINE_DISPLAY;
29904 help_echo_showing_p = 0;
29907 #ifdef HAVE_WINDOW_SYSTEM
29909 /* Platform-independent portion of hourglass implementation. */
29911 /* Cancel a currently active hourglass timer, and start a new one. */
29912 void
29913 start_hourglass (void)
29915 struct timespec delay;
29917 cancel_hourglass ();
29919 if (INTEGERP (Vhourglass_delay)
29920 && XINT (Vhourglass_delay) > 0)
29921 delay = make_timespec (min (XINT (Vhourglass_delay),
29922 TYPE_MAXIMUM (time_t)),
29924 else if (FLOATP (Vhourglass_delay)
29925 && XFLOAT_DATA (Vhourglass_delay) > 0)
29926 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29927 else
29928 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29930 #ifdef HAVE_NTGUI
29932 extern void w32_note_current_window (void);
29933 w32_note_current_window ();
29935 #endif /* HAVE_NTGUI */
29937 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29938 show_hourglass, NULL);
29942 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29943 shown. */
29944 void
29945 cancel_hourglass (void)
29947 if (hourglass_atimer)
29949 cancel_atimer (hourglass_atimer);
29950 hourglass_atimer = NULL;
29953 if (hourglass_shown_p)
29954 hide_hourglass ();
29957 #endif /* HAVE_WINDOW_SYSTEM */