Doc fix.
[emacs.git] / src / xdisp.c
bloba9dd387729e64cd42f4f5c15d778a7b1e4c81816
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. */
507 int update_mode_lines;
509 /* Nonzero if window sizes or contents have changed since last
510 redisplay that finished. */
512 int windows_or_buffers_changed;
514 /* Nonzero after display_mode_line if %l was used and it displayed a
515 line number. */
517 static bool line_number_displayed;
519 /* The name of the *Messages* buffer, a string. */
521 static Lisp_Object Vmessages_buffer_name;
523 /* Current, index 0, and last displayed echo area message. Either
524 buffers from echo_buffers, or nil to indicate no message. */
526 Lisp_Object echo_area_buffer[2];
528 /* The buffers referenced from echo_area_buffer. */
530 static Lisp_Object echo_buffer[2];
532 /* A vector saved used in with_area_buffer to reduce consing. */
534 static Lisp_Object Vwith_echo_area_save_vector;
536 /* Non-zero means display_echo_area should display the last echo area
537 message again. Set by redisplay_preserve_echo_area. */
539 static bool display_last_displayed_message_p;
541 /* Nonzero if echo area is being used by print; zero if being used by
542 message. */
544 static bool message_buf_print;
546 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
548 static Lisp_Object Qinhibit_menubar_update;
549 static Lisp_Object Qmessage_truncate_lines;
551 /* Set to 1 in clear_message to make redisplay_internal aware
552 of an emptied echo area. */
554 static bool message_cleared_p;
556 /* A scratch glyph row with contents used for generating truncation
557 glyphs. Also used in direct_output_for_insert. */
559 #define MAX_SCRATCH_GLYPHS 100
560 static struct glyph_row scratch_glyph_row;
561 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
563 /* Ascent and height of the last line processed by move_it_to. */
565 static int last_height;
567 /* Non-zero if there's a help-echo in the echo area. */
569 bool help_echo_showing_p;
571 /* The maximum distance to look ahead for text properties. Values
572 that are too small let us call compute_char_face and similar
573 functions too often which is expensive. Values that are too large
574 let us call compute_char_face and alike too often because we
575 might not be interested in text properties that far away. */
577 #define TEXT_PROP_DISTANCE_LIMIT 100
579 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
580 iterator state and later restore it. This is needed because the
581 bidi iterator on bidi.c keeps a stacked cache of its states, which
582 is really a singleton. When we use scratch iterator objects to
583 move around the buffer, we can cause the bidi cache to be pushed or
584 popped, and therefore we need to restore the cache state when we
585 return to the original iterator. */
586 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
587 do { \
588 if (CACHE) \
589 bidi_unshelve_cache (CACHE, 1); \
590 ITCOPY = ITORIG; \
591 CACHE = bidi_shelve_cache (); \
592 } while (0)
594 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
595 do { \
596 if (pITORIG != pITCOPY) \
597 *(pITORIG) = *(pITCOPY); \
598 bidi_unshelve_cache (CACHE, 0); \
599 CACHE = NULL; \
600 } while (0)
602 #ifdef GLYPH_DEBUG
604 /* Non-zero means print traces of redisplay if compiled with
605 GLYPH_DEBUG defined. */
607 int trace_redisplay_p;
609 #endif /* GLYPH_DEBUG */
611 #ifdef DEBUG_TRACE_MOVE
612 /* Non-zero means trace with TRACE_MOVE to stderr. */
613 int trace_move;
615 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
616 #else
617 #define TRACE_MOVE(x) (void) 0
618 #endif
620 static Lisp_Object Qauto_hscroll_mode;
622 /* Buffer being redisplayed -- for redisplay_window_error. */
624 static struct buffer *displayed_buffer;
626 /* Value returned from text property handlers (see below). */
628 enum prop_handled
630 HANDLED_NORMALLY,
631 HANDLED_RECOMPUTE_PROPS,
632 HANDLED_OVERLAY_STRING_CONSUMED,
633 HANDLED_RETURN
636 /* A description of text properties that redisplay is interested
637 in. */
639 struct props
641 /* The name of the property. */
642 Lisp_Object *name;
644 /* A unique index for the property. */
645 enum prop_idx idx;
647 /* A handler function called to set up iterator IT from the property
648 at IT's current position. Value is used to steer handle_stop. */
649 enum prop_handled (*handler) (struct it *it);
652 static enum prop_handled handle_face_prop (struct it *);
653 static enum prop_handled handle_invisible_prop (struct it *);
654 static enum prop_handled handle_display_prop (struct it *);
655 static enum prop_handled handle_composition_prop (struct it *);
656 static enum prop_handled handle_overlay_change (struct it *);
657 static enum prop_handled handle_fontified_prop (struct it *);
659 /* Properties handled by iterators. */
661 static struct props it_props[] =
663 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
664 /* Handle `face' before `display' because some sub-properties of
665 `display' need to know the face. */
666 {&Qface, FACE_PROP_IDX, handle_face_prop},
667 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
668 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
669 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
670 {NULL, 0, NULL}
673 /* Value is the position described by X. If X is a marker, value is
674 the marker_position of X. Otherwise, value is X. */
676 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
678 /* Enumeration returned by some move_it_.* functions internally. */
680 enum move_it_result
682 /* Not used. Undefined value. */
683 MOVE_UNDEFINED,
685 /* Move ended at the requested buffer position or ZV. */
686 MOVE_POS_MATCH_OR_ZV,
688 /* Move ended at the requested X pixel position. */
689 MOVE_X_REACHED,
691 /* Move within a line ended at the end of a line that must be
692 continued. */
693 MOVE_LINE_CONTINUED,
695 /* Move within a line ended at the end of a line that would
696 be displayed truncated. */
697 MOVE_LINE_TRUNCATED,
699 /* Move within a line ended at a line end. */
700 MOVE_NEWLINE_OR_CR
703 /* This counter is used to clear the face cache every once in a while
704 in redisplay_internal. It is incremented for each redisplay.
705 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
706 cleared. */
708 #define CLEAR_FACE_CACHE_COUNT 500
709 static int clear_face_cache_count;
711 /* Similarly for the image cache. */
713 #ifdef HAVE_WINDOW_SYSTEM
714 #define CLEAR_IMAGE_CACHE_COUNT 101
715 static int clear_image_cache_count;
717 /* Null glyph slice */
718 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
719 #endif
721 /* True while redisplay_internal is in progress. */
723 bool redisplaying_p;
725 static Lisp_Object Qinhibit_free_realized_faces;
726 static Lisp_Object Qmode_line_default_help_echo;
728 /* If a string, XTread_socket generates an event to display that string.
729 (The display is done in read_char.) */
731 Lisp_Object help_echo_string;
732 Lisp_Object help_echo_window;
733 Lisp_Object help_echo_object;
734 ptrdiff_t help_echo_pos;
736 /* Temporary variable for XTread_socket. */
738 Lisp_Object previous_help_echo_string;
740 /* Platform-independent portion of hourglass implementation. */
742 #ifdef HAVE_WINDOW_SYSTEM
744 /* Non-zero means an hourglass cursor is currently shown. */
745 bool hourglass_shown_p;
747 /* If non-null, an asynchronous timer that, when it expires, displays
748 an hourglass cursor on all frames. */
749 struct atimer *hourglass_atimer;
751 #endif /* HAVE_WINDOW_SYSTEM */
753 /* Name of the face used to display glyphless characters. */
754 static Lisp_Object Qglyphless_char;
756 /* Symbol for the purpose of Vglyphless_char_display. */
757 static Lisp_Object Qglyphless_char_display;
759 /* Method symbols for Vglyphless_char_display. */
760 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
762 /* Default number of seconds to wait before displaying an hourglass
763 cursor. */
764 #define DEFAULT_HOURGLASS_DELAY 1
766 #ifdef HAVE_WINDOW_SYSTEM
768 /* Default pixel width of `thin-space' display method. */
769 #define THIN_SPACE_WIDTH 1
771 #endif /* HAVE_WINDOW_SYSTEM */
773 /* Function prototypes. */
775 static void setup_for_ellipsis (struct it *, int);
776 static void set_iterator_to_next (struct it *, int);
777 static void mark_window_display_accurate_1 (struct window *, int);
778 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
779 static int display_prop_string_p (Lisp_Object, Lisp_Object);
780 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
781 static int cursor_row_p (struct glyph_row *);
782 static int redisplay_mode_lines (Lisp_Object, int);
783 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
785 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
787 static void handle_line_prefix (struct it *);
789 static void pint2str (char *, int, ptrdiff_t);
790 static void pint2hrstr (char *, int, ptrdiff_t);
791 static struct text_pos run_window_scroll_functions (Lisp_Object,
792 struct text_pos);
793 static int text_outside_line_unchanged_p (struct window *,
794 ptrdiff_t, ptrdiff_t);
795 static void store_mode_line_noprop_char (char);
796 static int store_mode_line_noprop (const char *, int, int);
797 static void handle_stop (struct it *);
798 static void handle_stop_backwards (struct it *, ptrdiff_t);
799 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
800 static void ensure_echo_area_buffers (void);
801 static void unwind_with_echo_area_buffer (Lisp_Object);
802 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
803 static int with_echo_area_buffer (struct window *, int,
804 int (*) (ptrdiff_t, Lisp_Object),
805 ptrdiff_t, Lisp_Object);
806 static void clear_garbaged_frames (void);
807 static int current_message_1 (ptrdiff_t, Lisp_Object);
808 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static int set_message_1 (ptrdiff_t, Lisp_Object);
811 static int display_echo_area (struct window *);
812 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static int string_char_and_length (const unsigned char *, int *);
816 static struct text_pos display_prop_end (struct it *, Lisp_Object,
817 struct text_pos);
818 static int compute_window_start_on_continuation_line (struct window *);
819 static void insert_left_trunc_glyphs (struct it *);
820 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
821 Lisp_Object);
822 static void extend_face_to_end_of_line (struct it *);
823 static int append_space_for_newline (struct it *, int);
824 static int cursor_row_fully_visible_p (struct window *, int, int);
825 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
826 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
827 static int trailing_whitespace_p (ptrdiff_t);
828 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
829 static void push_it (struct it *, struct text_pos *);
830 static void iterate_out_of_display_property (struct it *);
831 static void pop_it (struct it *);
832 static void sync_frame_with_window_matrix_rows (struct window *);
833 static void redisplay_internal (void);
834 static int echo_area_display (int);
835 static void redisplay_windows (Lisp_Object);
836 static void redisplay_window (Lisp_Object, int);
837 static Lisp_Object redisplay_window_error (Lisp_Object);
838 static Lisp_Object redisplay_window_0 (Lisp_Object);
839 static Lisp_Object redisplay_window_1 (Lisp_Object);
840 static int set_cursor_from_row (struct window *, struct glyph_row *,
841 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
842 int, int);
843 static int update_menu_bar (struct frame *, int, int);
844 static int try_window_reusing_current_matrix (struct window *);
845 static int try_window_id (struct window *);
846 static int display_line (struct it *);
847 static int display_mode_lines (struct window *);
848 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
849 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
850 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
851 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
852 static void display_menu_bar (struct window *);
853 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
854 ptrdiff_t *);
855 static int display_string (const char *, Lisp_Object, Lisp_Object,
856 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
857 static void compute_line_metrics (struct it *);
858 static void run_redisplay_end_trigger_hook (struct it *);
859 static int get_overlay_strings (struct it *, ptrdiff_t);
860 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
861 static void next_overlay_string (struct it *);
862 static void reseat (struct it *, struct text_pos, int);
863 static void reseat_1 (struct it *, struct text_pos, int);
864 static void back_to_previous_visible_line_start (struct it *);
865 static void reseat_at_next_visible_line_start (struct it *, int);
866 static int next_element_from_ellipsis (struct it *);
867 static int next_element_from_display_vector (struct it *);
868 static int next_element_from_string (struct it *);
869 static int next_element_from_c_string (struct it *);
870 static int next_element_from_buffer (struct it *);
871 static int next_element_from_composition (struct it *);
872 static int next_element_from_image (struct it *);
873 static int next_element_from_stretch (struct it *);
874 static void load_overlay_strings (struct it *, ptrdiff_t);
875 static int init_from_display_pos (struct it *, struct window *,
876 struct display_pos *);
877 static void reseat_to_string (struct it *, const char *,
878 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
879 static int get_next_display_element (struct it *);
880 static enum move_it_result
881 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
882 enum move_operation_enum);
883 static void get_visually_first_element (struct it *);
884 static void init_to_row_start (struct it *, struct window *,
885 struct glyph_row *);
886 static int init_to_row_end (struct it *, struct window *,
887 struct glyph_row *);
888 static void back_to_previous_line_start (struct it *);
889 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
890 static struct text_pos string_pos_nchars_ahead (struct text_pos,
891 Lisp_Object, ptrdiff_t);
892 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
893 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
894 static ptrdiff_t number_of_chars (const char *, bool);
895 static void compute_stop_pos (struct it *);
896 static void compute_string_pos (struct text_pos *, struct text_pos,
897 Lisp_Object);
898 static int face_before_or_after_it_pos (struct it *, int);
899 static ptrdiff_t next_overlay_change (ptrdiff_t);
900 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
901 Lisp_Object, struct text_pos *, ptrdiff_t, int);
902 static int handle_single_display_spec (struct it *, Lisp_Object,
903 Lisp_Object, Lisp_Object,
904 struct text_pos *, ptrdiff_t, int, int);
905 static int underlying_face_id (struct it *);
906 static int in_ellipses_for_invisible_text_p (struct display_pos *,
907 struct window *);
909 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
910 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
912 #ifdef HAVE_WINDOW_SYSTEM
914 static void x_consider_frame_title (Lisp_Object);
915 static void update_tool_bar (struct frame *, int);
916 static int redisplay_tool_bar (struct frame *);
917 static void notice_overwritten_cursor (struct window *,
918 enum glyph_row_area,
919 int, int, int, int);
920 static void append_stretch_glyph (struct it *, Lisp_Object,
921 int, int, int);
924 #endif /* HAVE_WINDOW_SYSTEM */
926 static void produce_special_glyphs (struct it *, enum display_element_type);
927 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
928 static int coords_in_mouse_face_p (struct window *, int, int);
932 /***********************************************************************
933 Window display dimensions
934 ***********************************************************************/
936 /* Return the bottom boundary y-position for text lines in window W.
937 This is the first y position at which a line cannot start.
938 It is relative to the top of the window.
940 This is the height of W minus the height of a mode line, if any. */
943 window_text_bottom_y (struct window *w)
945 int height = WINDOW_TOTAL_HEIGHT (w);
947 if (WINDOW_WANTS_MODELINE_P (w))
948 height -= CURRENT_MODE_LINE_HEIGHT (w);
949 return height;
952 /* Return the pixel width of display area AREA of window W.
953 ANY_AREA means return the total width of W, not including
954 fringes to the left and right of the window. */
957 window_box_width (struct window *w, enum glyph_row_area area)
959 int cols = w->total_cols;
960 int pixels = 0;
962 if (!w->pseudo_window_p)
964 cols -= WINDOW_SCROLL_BAR_COLS (w);
966 if (area == TEXT_AREA)
968 cols -= max (0, w->left_margin_cols);
969 cols -= max (0, w->right_margin_cols);
970 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
972 else if (area == LEFT_MARGIN_AREA)
974 cols = max (0, w->left_margin_cols);
975 pixels = 0;
977 else if (area == RIGHT_MARGIN_AREA)
979 cols = max (0, w->right_margin_cols);
980 pixels = 0;
984 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
988 /* Return the pixel height of the display area of window W, not
989 including mode lines of W, if any. */
992 window_box_height (struct window *w)
994 struct frame *f = XFRAME (w->frame);
995 int height = WINDOW_TOTAL_HEIGHT (w);
997 eassert (height >= 0);
999 /* Note: the code below that determines the mode-line/header-line
1000 height is essentially the same as that contained in the macro
1001 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1002 the appropriate glyph row has its `mode_line_p' flag set,
1003 and if it doesn't, uses estimate_mode_line_height instead. */
1005 if (WINDOW_WANTS_MODELINE_P (w))
1007 struct glyph_row *ml_row
1008 = (w->current_matrix && w->current_matrix->rows
1009 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1010 : 0);
1011 if (ml_row && ml_row->mode_line_p)
1012 height -= ml_row->height;
1013 else
1014 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1017 if (WINDOW_WANTS_HEADER_LINE_P (w))
1019 struct glyph_row *hl_row
1020 = (w->current_matrix && w->current_matrix->rows
1021 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1022 : 0);
1023 if (hl_row && hl_row->mode_line_p)
1024 height -= hl_row->height;
1025 else
1026 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1029 /* With a very small font and a mode-line that's taller than
1030 default, we might end up with a negative height. */
1031 return max (0, height);
1034 /* Return the window-relative coordinate of the left edge of display
1035 area AREA of window W. ANY_AREA means return the left edge of the
1036 whole window, to the right of the left fringe of W. */
1039 window_box_left_offset (struct window *w, enum glyph_row_area area)
1041 int x;
1043 if (w->pseudo_window_p)
1044 return 0;
1046 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1048 if (area == TEXT_AREA)
1049 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1050 + window_box_width (w, LEFT_MARGIN_AREA));
1051 else if (area == RIGHT_MARGIN_AREA)
1052 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1053 + window_box_width (w, LEFT_MARGIN_AREA)
1054 + window_box_width (w, TEXT_AREA)
1055 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1057 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1058 else if (area == LEFT_MARGIN_AREA
1059 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1060 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1062 return x;
1066 /* Return the window-relative coordinate of the right edge of display
1067 area AREA of window W. ANY_AREA means return the right edge of the
1068 whole window, to the left of the right fringe of W. */
1071 window_box_right_offset (struct window *w, enum glyph_row_area area)
1073 return window_box_left_offset (w, area) + window_box_width (w, area);
1076 /* Return the frame-relative coordinate of the left edge of display
1077 area AREA of window W. ANY_AREA means return the left edge of the
1078 whole window, to the right of the left fringe of W. */
1081 window_box_left (struct window *w, enum glyph_row_area area)
1083 struct frame *f = XFRAME (w->frame);
1084 int x;
1086 if (w->pseudo_window_p)
1087 return FRAME_INTERNAL_BORDER_WIDTH (f);
1089 x = (WINDOW_LEFT_EDGE_X (w)
1090 + window_box_left_offset (w, area));
1092 return x;
1096 /* Return the frame-relative coordinate of the right edge of display
1097 area AREA of window W. ANY_AREA means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1101 window_box_right (struct window *w, enum glyph_row_area area)
1103 return window_box_left (w, area) + window_box_width (w, area);
1106 /* Get the bounding box of the display area AREA of window W, without
1107 mode lines, in frame-relative coordinates. ANY_AREA means the
1108 whole window, not including the left and right fringes of
1109 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1110 coordinates of the upper-left corner of the box. Return in
1111 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1113 void
1114 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1115 int *box_y, int *box_width, int *box_height)
1117 if (box_width)
1118 *box_width = window_box_width (w, area);
1119 if (box_height)
1120 *box_height = window_box_height (w);
1121 if (box_x)
1122 *box_x = window_box_left (w, area);
1123 if (box_y)
1125 *box_y = WINDOW_TOP_EDGE_Y (w);
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1131 #ifdef HAVE_WINDOW_SYSTEM
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1135 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1136 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1137 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1138 box. */
1140 static void
1141 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1142 int *bottom_right_x, int *bottom_right_y)
1144 window_box (w, ANY_AREA, top_left_x, top_left_y,
1145 bottom_right_x, bottom_right_y);
1146 *bottom_right_x += *top_left_x;
1147 *bottom_right_y += *top_left_y;
1150 #endif /* HAVE_WINDOW_SYSTEM */
1152 /***********************************************************************
1153 Utilities
1154 ***********************************************************************/
1156 /* Return the bottom y-position of the line the iterator IT is in.
1157 This can modify IT's settings. */
1160 line_bottom_y (struct it *it)
1162 int line_height = it->max_ascent + it->max_descent;
1163 int line_top_y = it->current_y;
1165 if (line_height == 0)
1167 if (last_height)
1168 line_height = last_height;
1169 else if (IT_CHARPOS (*it) < ZV)
1171 move_it_by_lines (it, 1);
1172 line_height = (it->max_ascent || it->max_descent
1173 ? it->max_ascent + it->max_descent
1174 : last_height);
1176 else
1178 struct glyph_row *row = it->glyph_row;
1180 /* Use the default character height. */
1181 it->glyph_row = NULL;
1182 it->what = IT_CHARACTER;
1183 it->c = ' ';
1184 it->len = 1;
1185 PRODUCE_GLYPHS (it);
1186 line_height = it->ascent + it->descent;
1187 it->glyph_row = row;
1191 return line_top_y + line_height;
1194 DEFUN ("line-pixel-height", Fline_pixel_height,
1195 Sline_pixel_height, 0, 0, 0,
1196 doc: /* Return height in pixels of text line in the selected window.
1198 Value is the height in pixels of the line at point. */)
1199 (void)
1201 struct it it;
1202 struct text_pos pt;
1203 struct window *w = XWINDOW (selected_window);
1205 SET_TEXT_POS (pt, PT, PT_BYTE);
1206 start_display (&it, w, pt);
1207 it.vpos = it.current_y = 0;
1208 last_height = 0;
1209 return make_number (line_bottom_y (&it));
1212 /* Return the default pixel height of text lines in window W. The
1213 value is the canonical height of the W frame's default font, plus
1214 any extra space required by the line-spacing variable or frame
1215 parameter.
1217 Implementation note: this ignores any line-spacing text properties
1218 put on the newline characters. This is because those properties
1219 only affect the _screen_ line ending in the newline (i.e., in a
1220 continued line, only the last screen line will be affected), which
1221 means only a small number of lines in a buffer can ever use this
1222 feature. Since this function is used to compute the default pixel
1223 equivalent of text lines in a window, we can safely ignore those
1224 few lines. For the same reasons, we ignore the line-height
1225 properties. */
1227 default_line_pixel_height (struct window *w)
1229 struct frame *f = WINDOW_XFRAME (w);
1230 int height = FRAME_LINE_HEIGHT (f);
1232 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1234 struct buffer *b = XBUFFER (w->contents);
1235 Lisp_Object val = BVAR (b, extra_line_spacing);
1237 if (NILP (val))
1238 val = BVAR (&buffer_defaults, extra_line_spacing);
1239 if (!NILP (val))
1241 if (RANGED_INTEGERP (0, val, INT_MAX))
1242 height += XFASTINT (val);
1243 else if (FLOATP (val))
1245 int addon = XFLOAT_DATA (val) * height + 0.5;
1247 if (addon >= 0)
1248 height += addon;
1251 else
1252 height += f->extra_line_spacing;
1255 return height;
1258 /* Subroutine of pos_visible_p below. Extracts a display string, if
1259 any, from the display spec given as its argument. */
1260 static Lisp_Object
1261 string_from_display_spec (Lisp_Object spec)
1263 if (CONSP (spec))
1265 while (CONSP (spec))
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1269 spec = XCDR (spec);
1272 else if (VECTORP (spec))
1274 ptrdiff_t i;
1276 for (i = 0; i < ASIZE (spec); i++)
1278 if (STRINGP (AREF (spec, i)))
1279 return AREF (spec, i);
1281 return Qnil;
1284 return spec;
1288 /* Limit insanely large values of W->hscroll on frame F to the largest
1289 value that will still prevent first_visible_x and last_visible_x of
1290 'struct it' from overflowing an int. */
1291 static int
1292 window_hscroll_limited (struct window *w, struct frame *f)
1294 ptrdiff_t window_hscroll = w->hscroll;
1295 int window_text_width = window_box_width (w, TEXT_AREA);
1296 int colwidth = FRAME_COLUMN_WIDTH (f);
1298 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1299 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1301 return window_hscroll;
1304 /* Return 1 if position CHARPOS is visible in window W.
1305 CHARPOS < 0 means return info about WINDOW_END position.
1306 If visible, set *X and *Y to pixel coordinates of top left corner.
1307 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1308 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1311 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1312 int *rtop, int *rbot, int *rowh, int *vpos)
1314 struct it it;
1315 void *itdata = bidi_shelve_cache ();
1316 struct text_pos top;
1317 int visible_p = 0;
1318 struct buffer *old_buffer = NULL;
1320 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1321 return visible_p;
1323 if (XBUFFER (w->contents) != current_buffer)
1325 old_buffer = current_buffer;
1326 set_buffer_internal_1 (XBUFFER (w->contents));
1329 SET_TEXT_POS_FROM_MARKER (top, w->start);
1330 /* Scrolling a minibuffer window via scroll bar when the echo area
1331 shows long text sometimes resets the minibuffer contents behind
1332 our backs. */
1333 if (CHARPOS (top) > ZV)
1334 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1336 /* Compute exact mode line heights. */
1337 if (WINDOW_WANTS_MODELINE_P (w))
1338 w->mode_line_height
1339 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1340 BVAR (current_buffer, mode_line_format));
1342 if (WINDOW_WANTS_HEADER_LINE_P (w))
1343 w->header_line_height
1344 = display_mode_line (w, HEADER_LINE_FACE_ID,
1345 BVAR (current_buffer, header_line_format));
1347 start_display (&it, w, top);
1348 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1349 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1351 if (charpos >= 0
1352 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1353 && IT_CHARPOS (it) >= charpos)
1354 /* When scanning backwards under bidi iteration, move_it_to
1355 stops at or _before_ CHARPOS, because it stops at or to
1356 the _right_ of the character at CHARPOS. */
1357 || (it.bidi_p && it.bidi_it.scan_dir == -1
1358 && IT_CHARPOS (it) <= charpos)))
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1362 or covered by a display property, move_it_to stops at the end
1363 of the invisible text, to the right of CHARPOS. (ii) If
1364 CHARPOS is in a display vector, move_it_to stops on its last
1365 glyph. */
1366 int top_x = it.current_x;
1367 int top_y = it.current_y;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 enum it_method it_method = it.method;
1370 int bottom_y = (last_height = 0, line_bottom_y (&it));
1371 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1373 if (top_y < window_top_y)
1374 visible_p = bottom_y > window_top_y;
1375 else if (top_y < it.last_visible_y)
1376 visible_p = 1;
1377 if (bottom_y >= it.last_visible_y
1378 && it.bidi_p && it.bidi_it.scan_dir == -1
1379 && IT_CHARPOS (it) < charpos)
1381 /* When the last line of the window is scanned backwards
1382 under bidi iteration, we could be duped into thinking
1383 that we have passed CHARPOS, when in fact move_it_to
1384 simply stopped short of CHARPOS because it reached
1385 last_visible_y. To see if that's what happened, we call
1386 move_it_to again with a slightly larger vertical limit,
1387 and see if it actually moved vertically; if it did, we
1388 didn't really reach CHARPOS, which is beyond window end. */
1389 struct it save_it = it;
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = 0;
1399 it = save_it;
1401 if (visible_p)
1403 if (it_method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, 1);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1449 else if (IT_CHARPOS (it) != charpos)
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 int replacing_spec_p;
1456 bool newline_in_string
1457 = (STRINGP (string)
1458 && memchr (SDATA (string), '\n', SBYTES (string)));
1460 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1461 replacing_spec_p
1462 = (!NILP (spec)
1463 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1464 charpos, FRAME_WINDOW_P (it.f)));
1465 /* The tricky code below is needed because there's a
1466 discrepancy between move_it_to and how we set cursor
1467 when PT is at the beginning of a portion of text
1468 covered by a display property or an overlay with a
1469 display property, or the display line ends in a
1470 newline from a display string. move_it_to will stop
1471 _after_ such display strings, whereas
1472 set_cursor_from_row conspires with cursor_row_p to
1473 place the cursor on the first glyph produced from the
1474 display string. */
1476 /* We have overshoot PT because it is covered by a
1477 display property that replaces the text it covers.
1478 If the string includes embedded newlines, we are also
1479 in the wrong display line. Backtrack to the correct
1480 line, where the display property begins. */
1481 if (replacing_spec_p)
1483 Lisp_Object startpos, endpos;
1484 EMACS_INT start, end;
1485 struct it it3;
1486 int it3_moved;
1488 /* Find the first and the last buffer positions
1489 covered by the display string. */
1490 endpos =
1491 Fnext_single_char_property_change (cpos, Qdisplay,
1492 Qnil, Qnil);
1493 startpos =
1494 Fprevious_single_char_property_change (endpos, Qdisplay,
1495 Qnil, Qnil);
1496 start = XFASTINT (startpos);
1497 end = XFASTINT (endpos);
1498 /* Move to the last buffer position before the
1499 display property. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1502 /* Move forward one more line if the position before
1503 the display string is a newline or if it is the
1504 rightmost character on a line that is
1505 continued or word-wrapped. */
1506 if (it3.method == GET_FROM_BUFFER
1507 && (it3.c == '\n'
1508 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1509 move_it_by_lines (&it3, 1);
1510 else if (move_it_in_display_line_to (&it3, -1,
1511 it3.current_x
1512 + it3.pixel_width,
1513 MOVE_TO_X)
1514 == MOVE_LINE_CONTINUED)
1516 move_it_by_lines (&it3, 1);
1517 /* When we are under word-wrap, the #$@%!
1518 move_it_by_lines moves 2 lines, so we need to
1519 fix that up. */
1520 if (it3.line_wrap == WORD_WRAP)
1521 move_it_by_lines (&it3, -1);
1524 /* Record the vertical coordinate of the display
1525 line where we wound up. */
1526 top_y = it3.current_y;
1527 if (it3.bidi_p)
1529 /* When characters are reordered for display,
1530 the character displayed to the left of the
1531 display string could be _after_ the display
1532 property in the logical order. Use the
1533 smallest vertical position of these two. */
1534 start_display (&it3, w, top);
1535 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1536 if (it3.current_y < top_y)
1537 top_y = it3.current_y;
1539 /* Move from the top of the window to the beginning
1540 of the display line where the display string
1541 begins. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1544 /* If it3_moved stays zero after the 'while' loop
1545 below, that means we already were at a newline
1546 before the loop (e.g., the display string begins
1547 with a newline), so we don't need to (and cannot)
1548 inspect the glyphs of it3.glyph_row, because
1549 PRODUCE_GLYPHS will not produce anything for a
1550 newline, and thus it3.glyph_row stays at its
1551 stale content it got at top of the window. */
1552 it3_moved = 0;
1553 /* Finally, advance the iterator until we hit the
1554 first display element whose character position is
1555 CHARPOS, or until the first newline from the
1556 display string, which signals the end of the
1557 display line. */
1558 while (get_next_display_element (&it3))
1560 PRODUCE_GLYPHS (&it3);
1561 if (IT_CHARPOS (it3) == charpos
1562 || ITERATOR_AT_END_OF_LINE_P (&it3))
1563 break;
1564 it3_moved = 1;
1565 set_iterator_to_next (&it3, 0);
1567 top_x = it3.current_x - it3.pixel_width;
1568 /* Normally, we would exit the above loop because we
1569 found the display element whose character
1570 position is CHARPOS. For the contingency that we
1571 didn't, and stopped at the first newline from the
1572 display string, move back over the glyphs
1573 produced from the string, until we find the
1574 rightmost glyph not from the string. */
1575 if (it3_moved
1576 && newline_in_string
1577 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1579 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1580 + it3.glyph_row->used[TEXT_AREA];
1582 while (EQ ((g - 1)->object, string))
1584 --g;
1585 top_x -= g->pixel_width;
1587 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA]);
1593 *x = top_x;
1594 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1595 *rtop = max (0, window_top_y - top_y);
1596 *rbot = max (0, bottom_y - it.last_visible_y);
1597 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1598 - max (top_y, window_top_y)));
1599 *vpos = it.vpos;
1602 else
1604 /* We were asked to provide info about WINDOW_END. */
1605 struct it it2;
1606 void *it2data = NULL;
1608 SAVE_IT (it2, it, it2data);
1609 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1610 move_it_by_lines (&it, 1);
1611 if (charpos < IT_CHARPOS (it)
1612 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1614 visible_p = 1;
1615 RESTORE_IT (&it2, &it2, it2data);
1616 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1617 *x = it2.current_x;
1618 *y = it2.current_y + it2.max_ascent - it2.ascent;
1619 *rtop = max (0, -it2.current_y);
1620 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1621 - it.last_visible_y));
1622 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1623 it.last_visible_y)
1624 - max (it2.current_y,
1625 WINDOW_HEADER_LINE_HEIGHT (w))));
1626 *vpos = it2.vpos;
1628 else
1629 bidi_unshelve_cache (it2data, 1);
1631 bidi_unshelve_cache (itdata, 0);
1633 if (old_buffer)
1634 set_buffer_internal_1 (old_buffer);
1636 if (visible_p && w->hscroll > 0)
1637 *x -=
1638 window_hscroll_limited (w, WINDOW_XFRAME (w))
1639 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 #if 0
1642 /* Debugging code. */
1643 if (visible_p)
1644 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1645 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1646 else
1647 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1648 #endif
1650 return visible_p;
1654 /* Return the next character from STR. Return in *LEN the length of
1655 the character. This is like STRING_CHAR_AND_LENGTH but never
1656 returns an invalid character. If we find one, we return a `?', but
1657 with the length of the invalid character. */
1659 static int
1660 string_char_and_length (const unsigned char *str, int *len)
1662 int c;
1664 c = STRING_CHAR_AND_LENGTH (str, *len);
1665 if (!CHAR_VALID_P (c))
1666 /* We may not change the length here because other places in Emacs
1667 don't use this function, i.e. they silently accept invalid
1668 characters. */
1669 c = '?';
1671 return c;
1676 /* Given a position POS containing a valid character and byte position
1677 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1679 static struct text_pos
1680 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1682 eassert (STRINGP (string) && nchars >= 0);
1684 if (STRING_MULTIBYTE (string))
1686 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1687 int len;
1689 while (nchars--)
1691 string_char_and_length (p, &len);
1692 p += len;
1693 CHARPOS (pos) += 1;
1694 BYTEPOS (pos) += len;
1697 else
1698 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1700 return pos;
1704 /* Value is the text position, i.e. character and byte position,
1705 for character position CHARPOS in STRING. */
1707 static struct text_pos
1708 string_pos (ptrdiff_t charpos, Lisp_Object string)
1710 struct text_pos pos;
1711 eassert (STRINGP (string));
1712 eassert (charpos >= 0);
1713 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1714 return pos;
1718 /* Value is a text position, i.e. character and byte position, for
1719 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1720 means recognize multibyte characters. */
1722 static struct text_pos
1723 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1725 struct text_pos pos;
1727 eassert (s != NULL);
1728 eassert (charpos >= 0);
1730 if (multibyte_p)
1732 int len;
1734 SET_TEXT_POS (pos, 0, 0);
1735 while (charpos--)
1737 string_char_and_length ((const unsigned char *) s, &len);
1738 s += len;
1739 CHARPOS (pos) += 1;
1740 BYTEPOS (pos) += len;
1743 else
1744 SET_TEXT_POS (pos, charpos, charpos);
1746 return pos;
1750 /* Value is the number of characters in C string S. MULTIBYTE_P
1751 non-zero means recognize multibyte characters. */
1753 static ptrdiff_t
1754 number_of_chars (const char *s, bool multibyte_p)
1756 ptrdiff_t nchars;
1758 if (multibyte_p)
1760 ptrdiff_t rest = strlen (s);
1761 int len;
1762 const unsigned char *p = (const unsigned char *) s;
1764 for (nchars = 0; rest > 0; ++nchars)
1766 string_char_and_length (p, &len);
1767 rest -= len, p += len;
1770 else
1771 nchars = strlen (s);
1773 return nchars;
1777 /* Compute byte position NEWPOS->bytepos corresponding to
1778 NEWPOS->charpos. POS is a known position in string STRING.
1779 NEWPOS->charpos must be >= POS.charpos. */
1781 static void
1782 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1784 eassert (STRINGP (string));
1785 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1787 if (STRING_MULTIBYTE (string))
1788 *newpos = string_pos_nchars_ahead (pos, string,
1789 CHARPOS (*newpos) - CHARPOS (pos));
1790 else
1791 BYTEPOS (*newpos) = CHARPOS (*newpos);
1794 /* EXPORT:
1795 Return an estimation of the pixel height of mode or header lines on
1796 frame F. FACE_ID specifies what line's height to estimate. */
1799 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1804 int height = FONT_HEIGHT (FRAME_FONT (f));
1806 /* This function is called so early when Emacs starts that the face
1807 cache and mode line face are not yet initialized. */
1808 if (FRAME_FACE_CACHE (f))
1810 struct face *face = FACE_FROM_ID (f, face_id);
1811 if (face)
1813 if (face->font)
1814 height = FONT_HEIGHT (face->font);
1815 if (face->box_line_width > 0)
1816 height += 2 * face->box_line_width;
1820 return height;
1822 #endif
1824 return 1;
1827 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1828 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1829 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1830 not force the value into range. */
1832 void
1833 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1834 int *x, int *y, NativeRectangle *bounds, int noclip)
1837 #ifdef HAVE_WINDOW_SYSTEM
1838 if (FRAME_WINDOW_P (f))
1840 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1841 even for negative values. */
1842 if (pix_x < 0)
1843 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1844 if (pix_y < 0)
1845 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1847 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1848 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1850 if (bounds)
1851 STORE_NATIVE_RECT (*bounds,
1852 FRAME_COL_TO_PIXEL_X (f, pix_x),
1853 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1854 FRAME_COLUMN_WIDTH (f) - 1,
1855 FRAME_LINE_HEIGHT (f) - 1);
1857 if (!noclip)
1859 if (pix_x < 0)
1860 pix_x = 0;
1861 else if (pix_x > FRAME_TOTAL_COLS (f))
1862 pix_x = FRAME_TOTAL_COLS (f);
1864 if (pix_y < 0)
1865 pix_y = 0;
1866 else if (pix_y > FRAME_LINES (f))
1867 pix_y = FRAME_LINES (f);
1870 #endif
1872 *x = pix_x;
1873 *y = pix_y;
1877 /* Find the glyph under window-relative coordinates X/Y in window W.
1878 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1879 strings. Return in *HPOS and *VPOS the row and column number of
1880 the glyph found. Return in *AREA the glyph area containing X.
1881 Value is a pointer to the glyph found or null if X/Y is not on
1882 text, or we can't tell because W's current matrix is not up to
1883 date. */
1885 static struct glyph *
1886 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1887 int *dx, int *dy, int *area)
1889 struct glyph *glyph, *end;
1890 struct glyph_row *row = NULL;
1891 int x0, i;
1893 /* Find row containing Y. Give up if some row is not enabled. */
1894 for (i = 0; i < w->current_matrix->nrows; ++i)
1896 row = MATRIX_ROW (w->current_matrix, i);
1897 if (!row->enabled_p)
1898 return NULL;
1899 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1900 break;
1903 *vpos = i;
1904 *hpos = 0;
1906 /* Give up if Y is not in the window. */
1907 if (i == w->current_matrix->nrows)
1908 return NULL;
1910 /* Get the glyph area containing X. */
1911 if (w->pseudo_window_p)
1913 *area = TEXT_AREA;
1914 x0 = 0;
1916 else
1918 if (x < window_box_left_offset (w, TEXT_AREA))
1920 *area = LEFT_MARGIN_AREA;
1921 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1923 else if (x < window_box_right_offset (w, TEXT_AREA))
1925 *area = TEXT_AREA;
1926 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1928 else
1930 *area = RIGHT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1935 /* Find glyph containing X. */
1936 glyph = row->glyphs[*area];
1937 end = glyph + row->used[*area];
1938 x -= x0;
1939 while (glyph < end && x >= glyph->pixel_width)
1941 x -= glyph->pixel_width;
1942 ++glyph;
1945 if (glyph == end)
1946 return NULL;
1948 if (dx)
1950 *dx = x;
1951 *dy = y - (row->y + row->ascent - glyph->ascent);
1954 *hpos = glyph - row->glyphs[*area];
1955 return glyph;
1958 /* Convert frame-relative x/y to coordinates relative to window W.
1959 Takes pseudo-windows into account. */
1961 static void
1962 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1964 if (w->pseudo_window_p)
1966 /* A pseudo-window is always full-width, and starts at the
1967 left edge of the frame, plus a frame border. */
1968 struct frame *f = XFRAME (w->frame);
1969 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1970 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1972 else
1974 *x -= WINDOW_LEFT_EDGE_X (w);
1975 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1979 #ifdef HAVE_WINDOW_SYSTEM
1981 /* EXPORT:
1982 Return in RECTS[] at most N clipping rectangles for glyph string S.
1983 Return the number of stored rectangles. */
1986 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1988 XRectangle r;
1990 if (n <= 0)
1991 return 0;
1993 if (s->row->full_width_p)
1995 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1996 r.x = WINDOW_LEFT_EDGE_X (s->w);
1997 r.width = WINDOW_TOTAL_WIDTH (s->w);
1999 /* Unless displaying a mode or menu bar line, which are always
2000 fully visible, clip to the visible part of the row. */
2001 if (s->w->pseudo_window_p)
2002 r.height = s->row->visible_height;
2003 else
2004 r.height = s->height;
2006 else
2008 /* This is a text line that may be partially visible. */
2009 r.x = window_box_left (s->w, s->area);
2010 r.width = window_box_width (s->w, s->area);
2011 r.height = s->row->visible_height;
2014 if (s->clip_head)
2015 if (r.x < s->clip_head->x)
2017 if (r.width >= s->clip_head->x - r.x)
2018 r.width -= s->clip_head->x - r.x;
2019 else
2020 r.width = 0;
2021 r.x = s->clip_head->x;
2023 if (s->clip_tail)
2024 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2026 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2027 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2028 else
2029 r.width = 0;
2032 /* If S draws overlapping rows, it's sufficient to use the top and
2033 bottom of the window for clipping because this glyph string
2034 intentionally draws over other lines. */
2035 if (s->for_overlaps)
2037 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2038 r.height = window_text_bottom_y (s->w) - r.y;
2040 /* Alas, the above simple strategy does not work for the
2041 environments with anti-aliased text: if the same text is
2042 drawn onto the same place multiple times, it gets thicker.
2043 If the overlap we are processing is for the erased cursor, we
2044 take the intersection with the rectangle of the cursor. */
2045 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2047 XRectangle rc, r_save = r;
2049 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2050 rc.y = s->w->phys_cursor.y;
2051 rc.width = s->w->phys_cursor_width;
2052 rc.height = s->w->phys_cursor_height;
2054 x_intersect_rectangles (&r_save, &rc, &r);
2057 else
2059 /* Don't use S->y for clipping because it doesn't take partially
2060 visible lines into account. For example, it can be negative for
2061 partially visible lines at the top of a window. */
2062 if (!s->row->full_width_p
2063 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 else
2066 r.y = max (0, s->row->y);
2069 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2071 /* If drawing the cursor, don't let glyph draw outside its
2072 advertised boundaries. Cleartype does this under some circumstances. */
2073 if (s->hl == DRAW_CURSOR)
2075 struct glyph *glyph = s->first_glyph;
2076 int height, max_y;
2078 if (s->x > r.x)
2080 r.width -= s->x - r.x;
2081 r.x = s->x;
2083 r.width = min (r.width, glyph->pixel_width);
2085 /* If r.y is below window bottom, ensure that we still see a cursor. */
2086 height = min (glyph->ascent + glyph->descent,
2087 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2088 max_y = window_text_bottom_y (s->w) - height;
2089 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2090 if (s->ybase - glyph->ascent > max_y)
2092 r.y = max_y;
2093 r.height = height;
2095 else
2097 /* Don't draw cursor glyph taller than our actual glyph. */
2098 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2099 if (height < r.height)
2101 max_y = r.y + r.height;
2102 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2103 r.height = min (max_y - r.y, height);
2108 if (s->row->clip)
2110 XRectangle r_save = r;
2112 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2113 r.width = 0;
2116 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2117 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2119 #ifdef CONVERT_FROM_XRECT
2120 CONVERT_FROM_XRECT (r, *rects);
2121 #else
2122 *rects = r;
2123 #endif
2124 return 1;
2126 else
2128 /* If we are processing overlapping and allowed to return
2129 multiple clipping rectangles, we exclude the row of the glyph
2130 string from the clipping rectangle. This is to avoid drawing
2131 the same text on the environment with anti-aliasing. */
2132 #ifdef CONVERT_FROM_XRECT
2133 XRectangle rs[2];
2134 #else
2135 XRectangle *rs = rects;
2136 #endif
2137 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2139 if (s->for_overlaps & OVERLAPS_PRED)
2141 rs[i] = r;
2142 if (r.y + r.height > row_y)
2144 if (r.y < row_y)
2145 rs[i].height = row_y - r.y;
2146 else
2147 rs[i].height = 0;
2149 i++;
2151 if (s->for_overlaps & OVERLAPS_SUCC)
2153 rs[i] = r;
2154 if (r.y < row_y + s->row->visible_height)
2156 if (r.y + r.height > row_y + s->row->visible_height)
2158 rs[i].y = row_y + s->row->visible_height;
2159 rs[i].height = r.y + r.height - rs[i].y;
2161 else
2162 rs[i].height = 0;
2164 i++;
2167 n = i;
2168 #ifdef CONVERT_FROM_XRECT
2169 for (i = 0; i < n; i++)
2170 CONVERT_FROM_XRECT (rs[i], rects[i]);
2171 #endif
2172 return n;
2176 /* EXPORT:
2177 Return in *NR the clipping rectangle for glyph string S. */
2179 void
2180 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2182 get_glyph_string_clip_rects (s, nr, 1);
2186 /* EXPORT:
2187 Return the position and height of the phys cursor in window W.
2188 Set w->phys_cursor_width to width of phys cursor.
2191 void
2192 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2193 struct glyph *glyph, int *xp, int *yp, int *heightp)
2195 struct frame *f = XFRAME (WINDOW_FRAME (w));
2196 int x, y, wd, h, h0, y0;
2198 /* Compute the width of the rectangle to draw. If on a stretch
2199 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2200 rectangle as wide as the glyph, but use a canonical character
2201 width instead. */
2202 wd = glyph->pixel_width - 1;
2203 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2204 wd++; /* Why? */
2205 #endif
2207 x = w->phys_cursor.x;
2208 if (x < 0)
2210 wd += x;
2211 x = 0;
2214 if (glyph->type == STRETCH_GLYPH
2215 && !x_stretch_cursor_p)
2216 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2217 w->phys_cursor_width = wd;
2219 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2221 /* If y is below window bottom, ensure that we still see a cursor. */
2222 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2224 h = max (h0, glyph->ascent + glyph->descent);
2225 h0 = min (h0, glyph->ascent + glyph->descent);
2227 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2228 if (y < y0)
2230 h = max (h - (y0 - y) + 1, h0);
2231 y = y0 - 1;
2233 else
2235 y0 = window_text_bottom_y (w) - h0;
2236 if (y > y0)
2238 h += y - y0;
2239 y = y0;
2243 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2244 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2245 *heightp = h;
2249 * Remember which glyph the mouse is over.
2252 void
2253 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2255 Lisp_Object window;
2256 struct window *w;
2257 struct glyph_row *r, *gr, *end_row;
2258 enum window_part part;
2259 enum glyph_row_area area;
2260 int x, y, width, height;
2262 /* Try to determine frame pixel position and size of the glyph under
2263 frame pixel coordinates X/Y on frame F. */
2265 if (!f->glyphs_initialized_p
2266 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2267 NILP (window)))
2269 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2270 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2271 goto virtual_glyph;
2274 w = XWINDOW (window);
2275 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2276 height = WINDOW_FRAME_LINE_HEIGHT (w);
2278 x = window_relative_x_coord (w, part, gx);
2279 y = gy - WINDOW_TOP_EDGE_Y (w);
2281 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2282 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2284 if (w->pseudo_window_p)
2286 area = TEXT_AREA;
2287 part = ON_MODE_LINE; /* Don't adjust margin. */
2288 goto text_glyph;
2291 switch (part)
2293 case ON_LEFT_MARGIN:
2294 area = LEFT_MARGIN_AREA;
2295 goto text_glyph;
2297 case ON_RIGHT_MARGIN:
2298 area = RIGHT_MARGIN_AREA;
2299 goto text_glyph;
2301 case ON_HEADER_LINE:
2302 case ON_MODE_LINE:
2303 gr = (part == ON_HEADER_LINE
2304 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2305 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2306 gy = gr->y;
2307 area = TEXT_AREA;
2308 goto text_glyph_row_found;
2310 case ON_TEXT:
2311 area = TEXT_AREA;
2313 text_glyph:
2314 gr = 0; gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2318 gr = r; gy = r->y;
2319 break;
2322 text_glyph_row_found:
2323 if (gr && gy <= y)
2325 struct glyph *g = gr->glyphs[area];
2326 struct glyph *end = g + gr->used[area];
2328 height = gr->height;
2329 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2330 if (gx + g->pixel_width > x)
2331 break;
2333 if (g < end)
2335 if (g->type == IMAGE_GLYPH)
2337 /* Don't remember when mouse is over image, as
2338 image may have hot-spots. */
2339 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2340 return;
2342 width = g->pixel_width;
2344 else
2346 /* Use nominal char spacing at end of line. */
2347 x -= gx;
2348 gx += (x / width) * width;
2351 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2352 gx += window_box_left_offset (w, area);
2354 else
2356 /* Use nominal line height at end of window. */
2357 gx = (x / width) * width;
2358 y -= gy;
2359 gy += (y / height) * height;
2361 break;
2363 case ON_LEFT_FRINGE:
2364 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2365 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2366 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2367 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2368 goto row_glyph;
2370 case ON_RIGHT_FRINGE:
2371 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2372 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2373 : window_box_right_offset (w, TEXT_AREA));
2374 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2375 goto row_glyph;
2377 case ON_SCROLL_BAR:
2378 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2380 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2381 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2382 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2383 : 0)));
2384 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2386 row_glyph:
2387 gr = 0, gy = 0;
2388 for (; r <= end_row && r->enabled_p; ++r)
2389 if (r->y + r->height > y)
2391 gr = r; gy = r->y;
2392 break;
2395 if (gr && gy <= y)
2396 height = gr->height;
2397 else
2399 /* Use nominal line height at end of window. */
2400 y -= gy;
2401 gy += (y / height) * height;
2403 break;
2405 default:
2407 virtual_glyph:
2408 /* If there is no glyph under the mouse, then we divide the screen
2409 into a grid of the smallest glyph in the frame, and use that
2410 as our "glyph". */
2412 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2413 round down even for negative values. */
2414 if (gx < 0)
2415 gx -= width - 1;
2416 if (gy < 0)
2417 gy -= height - 1;
2419 gx = (gx / width) * width;
2420 gy = (gy / height) * height;
2422 goto store_rect;
2425 gx += WINDOW_LEFT_EDGE_X (w);
2426 gy += WINDOW_TOP_EDGE_Y (w);
2428 store_rect:
2429 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2431 /* Visible feedback for debugging. */
2432 #if 0
2433 #if HAVE_X_WINDOWS
2434 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2435 f->output_data.x->normal_gc,
2436 gx, gy, width, height);
2437 #endif
2438 #endif
2442 #endif /* HAVE_WINDOW_SYSTEM */
2444 static void
2445 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2447 eassert (w);
2448 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2449 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2450 w->window_end_vpos
2451 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2454 /***********************************************************************
2455 Lisp form evaluation
2456 ***********************************************************************/
2458 /* Error handler for safe_eval and safe_call. */
2460 static Lisp_Object
2461 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2463 add_to_log ("Error during redisplay: %S signaled %S",
2464 Flist (nargs, args), arg);
2465 return Qnil;
2468 /* Call function FUNC with the rest of NARGS - 1 arguments
2469 following. Return the result, or nil if something went
2470 wrong. Prevent redisplay during the evaluation. */
2472 Lisp_Object
2473 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2475 Lisp_Object val;
2477 if (inhibit_eval_during_redisplay)
2478 val = Qnil;
2479 else
2481 va_list ap;
2482 ptrdiff_t i;
2483 ptrdiff_t count = SPECPDL_INDEX ();
2484 struct gcpro gcpro1;
2485 Lisp_Object *args = alloca (nargs * word_size);
2487 args[0] = func;
2488 va_start (ap, func);
2489 for (i = 1; i < nargs; i++)
2490 args[i] = va_arg (ap, Lisp_Object);
2491 va_end (ap);
2493 GCPRO1 (args[0]);
2494 gcpro1.nvars = nargs;
2495 specbind (Qinhibit_redisplay, Qt);
2496 /* Use Qt to ensure debugger does not run,
2497 so there is no possibility of wanting to redisplay. */
2498 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2499 safe_eval_handler);
2500 UNGCPRO;
2501 val = unbind_to (count, val);
2504 return val;
2508 /* Call function FN with one argument ARG.
2509 Return the result, or nil if something went wrong. */
2511 Lisp_Object
2512 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2514 return safe_call (2, fn, arg);
2517 static Lisp_Object Qeval;
2519 Lisp_Object
2520 safe_eval (Lisp_Object sexpr)
2522 return safe_call1 (Qeval, sexpr);
2525 /* Call function FN with two arguments ARG1 and ARG2.
2526 Return the result, or nil if something went wrong. */
2528 Lisp_Object
2529 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2531 return safe_call (3, fn, arg1, arg2);
2536 /***********************************************************************
2537 Debugging
2538 ***********************************************************************/
2540 #if 0
2542 /* Define CHECK_IT to perform sanity checks on iterators.
2543 This is for debugging. It is too slow to do unconditionally. */
2545 static void
2546 check_it (struct it *it)
2548 if (it->method == GET_FROM_STRING)
2550 eassert (STRINGP (it->string));
2551 eassert (IT_STRING_CHARPOS (*it) >= 0);
2553 else
2555 eassert (IT_STRING_CHARPOS (*it) < 0);
2556 if (it->method == GET_FROM_BUFFER)
2558 /* Check that character and byte positions agree. */
2559 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2563 if (it->dpvec)
2564 eassert (it->current.dpvec_index >= 0);
2565 else
2566 eassert (it->current.dpvec_index < 0);
2569 #define CHECK_IT(IT) check_it ((IT))
2571 #else /* not 0 */
2573 #define CHECK_IT(IT) (void) 0
2575 #endif /* not 0 */
2578 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2580 /* Check that the window end of window W is what we expect it
2581 to be---the last row in the current matrix displaying text. */
2583 static void
2584 check_window_end (struct window *w)
2586 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2588 struct glyph_row *row;
2589 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2590 !row->enabled_p
2591 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2592 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2596 #define CHECK_WINDOW_END(W) check_window_end ((W))
2598 #else
2600 #define CHECK_WINDOW_END(W) (void) 0
2602 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2604 /***********************************************************************
2605 Iterator initialization
2606 ***********************************************************************/
2608 /* Initialize IT for displaying current_buffer in window W, starting
2609 at character position CHARPOS. CHARPOS < 0 means that no buffer
2610 position is specified which is useful when the iterator is assigned
2611 a position later. BYTEPOS is the byte position corresponding to
2612 CHARPOS.
2614 If ROW is not null, calls to produce_glyphs with IT as parameter
2615 will produce glyphs in that row.
2617 BASE_FACE_ID is the id of a base face to use. It must be one of
2618 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2619 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2620 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2622 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2623 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2624 will be initialized to use the corresponding mode line glyph row of
2625 the desired matrix of W. */
2627 void
2628 init_iterator (struct it *it, struct window *w,
2629 ptrdiff_t charpos, ptrdiff_t bytepos,
2630 struct glyph_row *row, enum face_id base_face_id)
2632 enum face_id remapped_base_face_id = base_face_id;
2634 /* Some precondition checks. */
2635 eassert (w != NULL && it != NULL);
2636 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2637 && charpos <= ZV));
2639 /* If face attributes have been changed since the last redisplay,
2640 free realized faces now because they depend on face definitions
2641 that might have changed. Don't free faces while there might be
2642 desired matrices pending which reference these faces. */
2643 if (face_change_count && !inhibit_free_realized_faces)
2645 face_change_count = 0;
2646 free_all_realized_faces (Qnil);
2649 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2650 if (! NILP (Vface_remapping_alist))
2651 remapped_base_face_id
2652 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2654 /* Use one of the mode line rows of W's desired matrix if
2655 appropriate. */
2656 if (row == NULL)
2658 if (base_face_id == MODE_LINE_FACE_ID
2659 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2660 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2661 else if (base_face_id == HEADER_LINE_FACE_ID)
2662 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2665 /* Clear IT. */
2666 memset (it, 0, sizeof *it);
2667 it->current.overlay_string_index = -1;
2668 it->current.dpvec_index = -1;
2669 it->base_face_id = remapped_base_face_id;
2670 it->string = Qnil;
2671 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2672 it->paragraph_embedding = L2R;
2673 it->bidi_it.string.lstring = Qnil;
2674 it->bidi_it.string.s = NULL;
2675 it->bidi_it.string.bufpos = 0;
2676 it->bidi_it.w = w;
2678 /* The window in which we iterate over current_buffer: */
2679 XSETWINDOW (it->window, w);
2680 it->w = w;
2681 it->f = XFRAME (w->frame);
2683 it->cmp_it.id = -1;
2685 /* Extra space between lines (on window systems only). */
2686 if (base_face_id == DEFAULT_FACE_ID
2687 && FRAME_WINDOW_P (it->f))
2689 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2690 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2691 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2692 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2693 * FRAME_LINE_HEIGHT (it->f));
2694 else if (it->f->extra_line_spacing > 0)
2695 it->extra_line_spacing = it->f->extra_line_spacing;
2696 it->max_extra_line_spacing = 0;
2699 /* If realized faces have been removed, e.g. because of face
2700 attribute changes of named faces, recompute them. When running
2701 in batch mode, the face cache of the initial frame is null. If
2702 we happen to get called, make a dummy face cache. */
2703 if (FRAME_FACE_CACHE (it->f) == NULL)
2704 init_frame_faces (it->f);
2705 if (FRAME_FACE_CACHE (it->f)->used == 0)
2706 recompute_basic_faces (it->f);
2708 /* Current value of the `slice', `space-width', and 'height' properties. */
2709 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2710 it->space_width = Qnil;
2711 it->font_height = Qnil;
2712 it->override_ascent = -1;
2714 /* Are control characters displayed as `^C'? */
2715 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2717 /* -1 means everything between a CR and the following line end
2718 is invisible. >0 means lines indented more than this value are
2719 invisible. */
2720 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2721 ? (clip_to_bounds
2722 (-1, XINT (BVAR (current_buffer, selective_display)),
2723 PTRDIFF_MAX))
2724 : (!NILP (BVAR (current_buffer, selective_display))
2725 ? -1 : 0));
2726 it->selective_display_ellipsis_p
2727 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2729 /* Display table to use. */
2730 it->dp = window_display_table (w);
2732 /* Are multibyte characters enabled in current_buffer? */
2733 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2735 /* Get the position at which the redisplay_end_trigger hook should
2736 be run, if it is to be run at all. */
2737 if (MARKERP (w->redisplay_end_trigger)
2738 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2739 it->redisplay_end_trigger_charpos
2740 = marker_position (w->redisplay_end_trigger);
2741 else if (INTEGERP (w->redisplay_end_trigger))
2742 it->redisplay_end_trigger_charpos =
2743 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2745 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2747 /* Are lines in the display truncated? */
2748 if (base_face_id != DEFAULT_FACE_ID
2749 || it->w->hscroll
2750 || (! WINDOW_FULL_WIDTH_P (it->w)
2751 && ((!NILP (Vtruncate_partial_width_windows)
2752 && !INTEGERP (Vtruncate_partial_width_windows))
2753 || (INTEGERP (Vtruncate_partial_width_windows)
2754 && (WINDOW_TOTAL_COLS (it->w)
2755 < XINT (Vtruncate_partial_width_windows))))))
2756 it->line_wrap = TRUNCATE;
2757 else if (NILP (BVAR (current_buffer, truncate_lines)))
2758 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2759 ? WINDOW_WRAP : WORD_WRAP;
2760 else
2761 it->line_wrap = TRUNCATE;
2763 /* Get dimensions of truncation and continuation glyphs. These are
2764 displayed as fringe bitmaps under X, but we need them for such
2765 frames when the fringes are turned off. But leave the dimensions
2766 zero for tooltip frames, as these glyphs look ugly there and also
2767 sabotage calculations of tooltip dimensions in x-show-tip. */
2768 #ifdef HAVE_WINDOW_SYSTEM
2769 if (!(FRAME_WINDOW_P (it->f)
2770 && FRAMEP (tip_frame)
2771 && it->f == XFRAME (tip_frame)))
2772 #endif
2774 if (it->line_wrap == TRUNCATE)
2776 /* We will need the truncation glyph. */
2777 eassert (it->glyph_row == NULL);
2778 produce_special_glyphs (it, IT_TRUNCATION);
2779 it->truncation_pixel_width = it->pixel_width;
2781 else
2783 /* We will need the continuation glyph. */
2784 eassert (it->glyph_row == NULL);
2785 produce_special_glyphs (it, IT_CONTINUATION);
2786 it->continuation_pixel_width = it->pixel_width;
2790 /* Reset these values to zero because the produce_special_glyphs
2791 above has changed them. */
2792 it->pixel_width = it->ascent = it->descent = 0;
2793 it->phys_ascent = it->phys_descent = 0;
2795 /* Set this after getting the dimensions of truncation and
2796 continuation glyphs, so that we don't produce glyphs when calling
2797 produce_special_glyphs, above. */
2798 it->glyph_row = row;
2799 it->area = TEXT_AREA;
2801 /* Forget any previous info about this row being reversed. */
2802 if (it->glyph_row)
2803 it->glyph_row->reversed_p = 0;
2805 /* Get the dimensions of the display area. The display area
2806 consists of the visible window area plus a horizontally scrolled
2807 part to the left of the window. All x-values are relative to the
2808 start of this total display area. */
2809 if (base_face_id != DEFAULT_FACE_ID)
2811 /* Mode lines, menu bar in terminal frames. */
2812 it->first_visible_x = 0;
2813 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2815 else
2817 it->first_visible_x =
2818 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2819 it->last_visible_x = (it->first_visible_x
2820 + window_box_width (w, TEXT_AREA));
2822 /* If we truncate lines, leave room for the truncation glyph(s) at
2823 the right margin. Otherwise, leave room for the continuation
2824 glyph(s). Done only if the window has no fringes. Since we
2825 don't know at this point whether there will be any R2L lines in
2826 the window, we reserve space for truncation/continuation glyphs
2827 even if only one of the fringes is absent. */
2828 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2829 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2831 if (it->line_wrap == TRUNCATE)
2832 it->last_visible_x -= it->truncation_pixel_width;
2833 else
2834 it->last_visible_x -= it->continuation_pixel_width;
2837 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2838 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2841 /* Leave room for a border glyph. */
2842 if (!FRAME_WINDOW_P (it->f)
2843 && !WINDOW_RIGHTMOST_P (it->w))
2844 it->last_visible_x -= 1;
2846 it->last_visible_y = window_text_bottom_y (w);
2848 /* For mode lines and alike, arrange for the first glyph having a
2849 left box line if the face specifies a box. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2852 struct face *face;
2854 it->face_id = remapped_base_face_id;
2856 /* If we have a boxed mode line, make the first character appear
2857 with a left box line. */
2858 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2859 if (face->box != FACE_NO_BOX)
2860 it->start_of_box_run_p = 1;
2863 /* If a buffer position was specified, set the iterator there,
2864 getting overlays and face properties from that position. */
2865 if (charpos >= BUF_BEG (current_buffer))
2867 it->end_charpos = ZV;
2868 eassert (charpos == BYTE_TO_CHAR (bytepos));
2869 IT_CHARPOS (*it) = charpos;
2870 IT_BYTEPOS (*it) = bytepos;
2872 /* We will rely on `reseat' to set this up properly, via
2873 handle_face_prop. */
2874 it->face_id = it->base_face_id;
2876 it->start = it->current;
2877 /* Do we need to reorder bidirectional text? Not if this is a
2878 unibyte buffer: by definition, none of the single-byte
2879 characters are strong R2L, so no reordering is needed. And
2880 bidi.c doesn't support unibyte buffers anyway. Also, don't
2881 reorder while we are loading loadup.el, since the tables of
2882 character properties needed for reordering are not yet
2883 available. */
2884 it->bidi_p =
2885 NILP (Vpurify_flag)
2886 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2887 && it->multibyte_p;
2889 /* If we are to reorder bidirectional text, init the bidi
2890 iterator. */
2891 if (it->bidi_p)
2893 /* Note the paragraph direction that this buffer wants to
2894 use. */
2895 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2896 Qleft_to_right))
2897 it->paragraph_embedding = L2R;
2898 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2899 Qright_to_left))
2900 it->paragraph_embedding = R2L;
2901 else
2902 it->paragraph_embedding = NEUTRAL_DIR;
2903 bidi_unshelve_cache (NULL, 0);
2904 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2905 &it->bidi_it);
2908 /* Compute faces etc. */
2909 reseat (it, it->current.pos, 1);
2912 CHECK_IT (it);
2916 /* Initialize IT for the display of window W with window start POS. */
2918 void
2919 start_display (struct it *it, struct window *w, struct text_pos pos)
2921 struct glyph_row *row;
2922 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2924 row = w->desired_matrix->rows + first_vpos;
2925 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2926 it->first_vpos = first_vpos;
2928 /* Don't reseat to previous visible line start if current start
2929 position is in a string or image. */
2930 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2932 int start_at_line_beg_p;
2933 int first_y = it->current_y;
2935 /* If window start is not at a line start, skip forward to POS to
2936 get the correct continuation lines width. */
2937 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2938 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2939 if (!start_at_line_beg_p)
2941 int new_x;
2943 reseat_at_previous_visible_line_start (it);
2944 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2946 new_x = it->current_x + it->pixel_width;
2948 /* If lines are continued, this line may end in the middle
2949 of a multi-glyph character (e.g. a control character
2950 displayed as \003, or in the middle of an overlay
2951 string). In this case move_it_to above will not have
2952 taken us to the start of the continuation line but to the
2953 end of the continued line. */
2954 if (it->current_x > 0
2955 && it->line_wrap != TRUNCATE /* Lines are continued. */
2956 && (/* And glyph doesn't fit on the line. */
2957 new_x > it->last_visible_x
2958 /* Or it fits exactly and we're on a window
2959 system frame. */
2960 || (new_x == it->last_visible_x
2961 && FRAME_WINDOW_P (it->f)
2962 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2963 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2964 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2966 if ((it->current.dpvec_index >= 0
2967 || it->current.overlay_string_index >= 0)
2968 /* If we are on a newline from a display vector or
2969 overlay string, then we are already at the end of
2970 a screen line; no need to go to the next line in
2971 that case, as this line is not really continued.
2972 (If we do go to the next line, C-e will not DTRT.) */
2973 && it->c != '\n')
2975 set_iterator_to_next (it, 1);
2976 move_it_in_display_line_to (it, -1, -1, 0);
2979 it->continuation_lines_width += it->current_x;
2981 /* If the character at POS is displayed via a display
2982 vector, move_it_to above stops at the final glyph of
2983 IT->dpvec. To make the caller redisplay that character
2984 again (a.k.a. start at POS), we need to reset the
2985 dpvec_index to the beginning of IT->dpvec. */
2986 else if (it->current.dpvec_index >= 0)
2987 it->current.dpvec_index = 0;
2989 /* We're starting a new display line, not affected by the
2990 height of the continued line, so clear the appropriate
2991 fields in the iterator structure. */
2992 it->max_ascent = it->max_descent = 0;
2993 it->max_phys_ascent = it->max_phys_descent = 0;
2995 it->current_y = first_y;
2996 it->vpos = 0;
2997 it->current_x = it->hpos = 0;
3003 /* Return 1 if POS is a position in ellipses displayed for invisible
3004 text. W is the window we display, for text property lookup. */
3006 static int
3007 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3009 Lisp_Object prop, window;
3010 int ellipses_p = 0;
3011 ptrdiff_t charpos = CHARPOS (pos->pos);
3013 /* If POS specifies a position in a display vector, this might
3014 be for an ellipsis displayed for invisible text. We won't
3015 get the iterator set up for delivering that ellipsis unless
3016 we make sure that it gets aware of the invisible text. */
3017 if (pos->dpvec_index >= 0
3018 && pos->overlay_string_index < 0
3019 && CHARPOS (pos->string_pos) < 0
3020 && charpos > BEGV
3021 && (XSETWINDOW (window, w),
3022 prop = Fget_char_property (make_number (charpos),
3023 Qinvisible, window),
3024 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3026 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3027 window);
3028 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3031 return ellipses_p;
3035 /* Initialize IT for stepping through current_buffer in window W,
3036 starting at position POS that includes overlay string and display
3037 vector/ control character translation position information. Value
3038 is zero if there are overlay strings with newlines at POS. */
3040 static int
3041 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3043 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3044 int i, overlay_strings_with_newlines = 0;
3046 /* If POS specifies a position in a display vector, this might
3047 be for an ellipsis displayed for invisible text. We won't
3048 get the iterator set up for delivering that ellipsis unless
3049 we make sure that it gets aware of the invisible text. */
3050 if (in_ellipses_for_invisible_text_p (pos, w))
3052 --charpos;
3053 bytepos = 0;
3056 /* Keep in mind: the call to reseat in init_iterator skips invisible
3057 text, so we might end up at a position different from POS. This
3058 is only a problem when POS is a row start after a newline and an
3059 overlay starts there with an after-string, and the overlay has an
3060 invisible property. Since we don't skip invisible text in
3061 display_line and elsewhere immediately after consuming the
3062 newline before the row start, such a POS will not be in a string,
3063 but the call to init_iterator below will move us to the
3064 after-string. */
3065 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3067 /* This only scans the current chunk -- it should scan all chunks.
3068 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3069 to 16 in 22.1 to make this a lesser problem. */
3070 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3072 const char *s = SSDATA (it->overlay_strings[i]);
3073 const char *e = s + SBYTES (it->overlay_strings[i]);
3075 while (s < e && *s != '\n')
3076 ++s;
3078 if (s < e)
3080 overlay_strings_with_newlines = 1;
3081 break;
3085 /* If position is within an overlay string, set up IT to the right
3086 overlay string. */
3087 if (pos->overlay_string_index >= 0)
3089 int relative_index;
3091 /* If the first overlay string happens to have a `display'
3092 property for an image, the iterator will be set up for that
3093 image, and we have to undo that setup first before we can
3094 correct the overlay string index. */
3095 if (it->method == GET_FROM_IMAGE)
3096 pop_it (it);
3098 /* We already have the first chunk of overlay strings in
3099 IT->overlay_strings. Load more until the one for
3100 pos->overlay_string_index is in IT->overlay_strings. */
3101 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3103 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3104 it->current.overlay_string_index = 0;
3105 while (n--)
3107 load_overlay_strings (it, 0);
3108 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3112 it->current.overlay_string_index = pos->overlay_string_index;
3113 relative_index = (it->current.overlay_string_index
3114 % OVERLAY_STRING_CHUNK_SIZE);
3115 it->string = it->overlay_strings[relative_index];
3116 eassert (STRINGP (it->string));
3117 it->current.string_pos = pos->string_pos;
3118 it->method = GET_FROM_STRING;
3119 it->end_charpos = SCHARS (it->string);
3120 /* Set up the bidi iterator for this overlay string. */
3121 if (it->bidi_p)
3123 it->bidi_it.string.lstring = it->string;
3124 it->bidi_it.string.s = NULL;
3125 it->bidi_it.string.schars = SCHARS (it->string);
3126 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3127 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3128 it->bidi_it.string.unibyte = !it->multibyte_p;
3129 it->bidi_it.w = it->w;
3130 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3131 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133 /* Synchronize the state of the bidi iterator with
3134 pos->string_pos. For any string position other than
3135 zero, this will be done automagically when we resume
3136 iteration over the string and get_visually_first_element
3137 is called. But if string_pos is zero, and the string is
3138 to be reordered for display, we need to resync manually,
3139 since it could be that the iteration state recorded in
3140 pos ended at string_pos of 0 moving backwards in string. */
3141 if (CHARPOS (pos->string_pos) == 0)
3143 get_visually_first_element (it);
3144 if (IT_STRING_CHARPOS (*it) != 0)
3145 do {
3146 /* Paranoia. */
3147 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3148 bidi_move_to_visually_next (&it->bidi_it);
3149 } while (it->bidi_it.charpos != 0);
3151 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3152 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3156 if (CHARPOS (pos->string_pos) >= 0)
3158 /* Recorded position is not in an overlay string, but in another
3159 string. This can only be a string from a `display' property.
3160 IT should already be filled with that string. */
3161 it->current.string_pos = pos->string_pos;
3162 eassert (STRINGP (it->string));
3163 if (it->bidi_p)
3164 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3165 FRAME_WINDOW_P (it->f), &it->bidi_it);
3168 /* Restore position in display vector translations, control
3169 character translations or ellipses. */
3170 if (pos->dpvec_index >= 0)
3172 if (it->dpvec == NULL)
3173 get_next_display_element (it);
3174 eassert (it->dpvec && it->current.dpvec_index == 0);
3175 it->current.dpvec_index = pos->dpvec_index;
3178 CHECK_IT (it);
3179 return !overlay_strings_with_newlines;
3183 /* Initialize IT for stepping through current_buffer in window W
3184 starting at ROW->start. */
3186 static void
3187 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3189 init_from_display_pos (it, w, &row->start);
3190 it->start = row->start;
3191 it->continuation_lines_width = row->continuation_lines_width;
3192 CHECK_IT (it);
3196 /* Initialize IT for stepping through current_buffer in window W
3197 starting in the line following ROW, i.e. starting at ROW->end.
3198 Value is zero if there are overlay strings with newlines at ROW's
3199 end position. */
3201 static int
3202 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3204 int success = 0;
3206 if (init_from_display_pos (it, w, &row->end))
3208 if (row->continued_p)
3209 it->continuation_lines_width
3210 = row->continuation_lines_width + row->pixel_width;
3211 CHECK_IT (it);
3212 success = 1;
3215 return success;
3221 /***********************************************************************
3222 Text properties
3223 ***********************************************************************/
3225 /* Called when IT reaches IT->stop_charpos. Handle text property and
3226 overlay changes. Set IT->stop_charpos to the next position where
3227 to stop. */
3229 static void
3230 handle_stop (struct it *it)
3232 enum prop_handled handled;
3233 int handle_overlay_change_p;
3234 struct props *p;
3236 it->dpvec = NULL;
3237 it->current.dpvec_index = -1;
3238 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3239 it->ignore_overlay_strings_at_pos_p = 0;
3240 it->ellipsis_p = 0;
3242 /* Use face of preceding text for ellipsis (if invisible) */
3243 if (it->selective_display_ellipsis_p)
3244 it->saved_face_id = it->face_id;
3248 handled = HANDLED_NORMALLY;
3250 /* Call text property handlers. */
3251 for (p = it_props; p->handler; ++p)
3253 handled = p->handler (it);
3255 if (handled == HANDLED_RECOMPUTE_PROPS)
3256 break;
3257 else if (handled == HANDLED_RETURN)
3259 /* We still want to show before and after strings from
3260 overlays even if the actual buffer text is replaced. */
3261 if (!handle_overlay_change_p
3262 || it->sp > 1
3263 /* Don't call get_overlay_strings_1 if we already
3264 have overlay strings loaded, because doing so
3265 will load them again and push the iterator state
3266 onto the stack one more time, which is not
3267 expected by the rest of the code that processes
3268 overlay strings. */
3269 || (it->current.overlay_string_index < 0
3270 ? !get_overlay_strings_1 (it, 0, 0)
3271 : 0))
3273 if (it->ellipsis_p)
3274 setup_for_ellipsis (it, 0);
3275 /* When handling a display spec, we might load an
3276 empty string. In that case, discard it here. We
3277 used to discard it in handle_single_display_spec,
3278 but that causes get_overlay_strings_1, above, to
3279 ignore overlay strings that we must check. */
3280 if (STRINGP (it->string) && !SCHARS (it->string))
3281 pop_it (it);
3282 return;
3284 else if (STRINGP (it->string) && !SCHARS (it->string))
3285 pop_it (it);
3286 else
3288 it->ignore_overlay_strings_at_pos_p = 1;
3289 it->string_from_display_prop_p = 0;
3290 it->from_disp_prop_p = 0;
3291 handle_overlay_change_p = 0;
3293 handled = HANDLED_RECOMPUTE_PROPS;
3294 break;
3296 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3297 handle_overlay_change_p = 0;
3300 if (handled != HANDLED_RECOMPUTE_PROPS)
3302 /* Don't check for overlay strings below when set to deliver
3303 characters from a display vector. */
3304 if (it->method == GET_FROM_DISPLAY_VECTOR)
3305 handle_overlay_change_p = 0;
3307 /* Handle overlay changes.
3308 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3309 if it finds overlays. */
3310 if (handle_overlay_change_p)
3311 handled = handle_overlay_change (it);
3314 if (it->ellipsis_p)
3316 setup_for_ellipsis (it, 0);
3317 break;
3320 while (handled == HANDLED_RECOMPUTE_PROPS);
3322 /* Determine where to stop next. */
3323 if (handled == HANDLED_NORMALLY)
3324 compute_stop_pos (it);
3328 /* Compute IT->stop_charpos from text property and overlay change
3329 information for IT's current position. */
3331 static void
3332 compute_stop_pos (struct it *it)
3334 register INTERVAL iv, next_iv;
3335 Lisp_Object object, limit, position;
3336 ptrdiff_t charpos, bytepos;
3338 if (STRINGP (it->string))
3340 /* Strings are usually short, so don't limit the search for
3341 properties. */
3342 it->stop_charpos = it->end_charpos;
3343 object = it->string;
3344 limit = Qnil;
3345 charpos = IT_STRING_CHARPOS (*it);
3346 bytepos = IT_STRING_BYTEPOS (*it);
3348 else
3350 ptrdiff_t pos;
3352 /* If end_charpos is out of range for some reason, such as a
3353 misbehaving display function, rationalize it (Bug#5984). */
3354 if (it->end_charpos > ZV)
3355 it->end_charpos = ZV;
3356 it->stop_charpos = it->end_charpos;
3358 /* If next overlay change is in front of the current stop pos
3359 (which is IT->end_charpos), stop there. Note: value of
3360 next_overlay_change is point-max if no overlay change
3361 follows. */
3362 charpos = IT_CHARPOS (*it);
3363 bytepos = IT_BYTEPOS (*it);
3364 pos = next_overlay_change (charpos);
3365 if (pos < it->stop_charpos)
3366 it->stop_charpos = pos;
3368 /* Set up variables for computing the stop position from text
3369 property changes. */
3370 XSETBUFFER (object, current_buffer);
3371 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3374 /* Get the interval containing IT's position. Value is a null
3375 interval if there isn't such an interval. */
3376 position = make_number (charpos);
3377 iv = validate_interval_range (object, &position, &position, 0);
3378 if (iv)
3380 Lisp_Object values_here[LAST_PROP_IDX];
3381 struct props *p;
3383 /* Get properties here. */
3384 for (p = it_props; p->handler; ++p)
3385 values_here[p->idx] = textget (iv->plist, *p->name);
3387 /* Look for an interval following iv that has different
3388 properties. */
3389 for (next_iv = next_interval (iv);
3390 (next_iv
3391 && (NILP (limit)
3392 || XFASTINT (limit) > next_iv->position));
3393 next_iv = next_interval (next_iv))
3395 for (p = it_props; p->handler; ++p)
3397 Lisp_Object new_value;
3399 new_value = textget (next_iv->plist, *p->name);
3400 if (!EQ (values_here[p->idx], new_value))
3401 break;
3404 if (p->handler)
3405 break;
3408 if (next_iv)
3410 if (INTEGERP (limit)
3411 && next_iv->position >= XFASTINT (limit))
3412 /* No text property change up to limit. */
3413 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3414 else
3415 /* Text properties change in next_iv. */
3416 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3420 if (it->cmp_it.id < 0)
3422 ptrdiff_t stoppos = it->end_charpos;
3424 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3425 stoppos = -1;
3426 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3427 stoppos, it->string);
3430 eassert (STRINGP (it->string)
3431 || (it->stop_charpos >= BEGV
3432 && it->stop_charpos >= IT_CHARPOS (*it)));
3436 /* Return the position of the next overlay change after POS in
3437 current_buffer. Value is point-max if no overlay change
3438 follows. This is like `next-overlay-change' but doesn't use
3439 xmalloc. */
3441 static ptrdiff_t
3442 next_overlay_change (ptrdiff_t pos)
3444 ptrdiff_t i, noverlays;
3445 ptrdiff_t endpos;
3446 Lisp_Object *overlays;
3448 /* Get all overlays at the given position. */
3449 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3451 /* If any of these overlays ends before endpos,
3452 use its ending point instead. */
3453 for (i = 0; i < noverlays; ++i)
3455 Lisp_Object oend;
3456 ptrdiff_t oendpos;
3458 oend = OVERLAY_END (overlays[i]);
3459 oendpos = OVERLAY_POSITION (oend);
3460 endpos = min (endpos, oendpos);
3463 return endpos;
3466 /* How many characters forward to search for a display property or
3467 display string. Searching too far forward makes the bidi display
3468 sluggish, especially in small windows. */
3469 #define MAX_DISP_SCAN 250
3471 /* Return the character position of a display string at or after
3472 position specified by POSITION. If no display string exists at or
3473 after POSITION, return ZV. A display string is either an overlay
3474 with `display' property whose value is a string, or a `display'
3475 text property whose value is a string. STRING is data about the
3476 string to iterate; if STRING->lstring is nil, we are iterating a
3477 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3478 on a GUI frame. DISP_PROP is set to zero if we searched
3479 MAX_DISP_SCAN characters forward without finding any display
3480 strings, non-zero otherwise. It is set to 2 if the display string
3481 uses any kind of `(space ...)' spec that will produce a stretch of
3482 white space in the text area. */
3483 ptrdiff_t
3484 compute_display_string_pos (struct text_pos *position,
3485 struct bidi_string_data *string,
3486 struct window *w,
3487 int frame_window_p, int *disp_prop)
3489 /* OBJECT = nil means current buffer. */
3490 Lisp_Object object, object1;
3491 Lisp_Object pos, spec, limpos;
3492 int string_p = (string && (STRINGP (string->lstring) || string->s));
3493 ptrdiff_t eob = string_p ? string->schars : ZV;
3494 ptrdiff_t begb = string_p ? 0 : BEGV;
3495 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3496 ptrdiff_t lim =
3497 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3498 struct text_pos tpos;
3499 int rv = 0;
3501 if (string && STRINGP (string->lstring))
3502 object1 = object = string->lstring;
3503 else if (w && !string_p)
3505 XSETWINDOW (object, w);
3506 object1 = Qnil;
3508 else
3509 object1 = object = Qnil;
3511 *disp_prop = 1;
3513 if (charpos >= eob
3514 /* We don't support display properties whose values are strings
3515 that have display string properties. */
3516 || string->from_disp_str
3517 /* C strings cannot have display properties. */
3518 || (string->s && !STRINGP (object)))
3520 *disp_prop = 0;
3521 return eob;
3524 /* If the character at CHARPOS is where the display string begins,
3525 return CHARPOS. */
3526 pos = make_number (charpos);
3527 if (STRINGP (object))
3528 bufpos = string->bufpos;
3529 else
3530 bufpos = charpos;
3531 tpos = *position;
3532 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3533 && (charpos <= begb
3534 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3535 object),
3536 spec))
3537 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3538 frame_window_p)))
3540 if (rv == 2)
3541 *disp_prop = 2;
3542 return charpos;
3545 /* Look forward for the first character with a `display' property
3546 that will replace the underlying text when displayed. */
3547 limpos = make_number (lim);
3548 do {
3549 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3550 CHARPOS (tpos) = XFASTINT (pos);
3551 if (CHARPOS (tpos) >= lim)
3553 *disp_prop = 0;
3554 break;
3556 if (STRINGP (object))
3557 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3558 else
3559 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3560 spec = Fget_char_property (pos, Qdisplay, object);
3561 if (!STRINGP (object))
3562 bufpos = CHARPOS (tpos);
3563 } while (NILP (spec)
3564 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3565 bufpos, frame_window_p)));
3566 if (rv == 2)
3567 *disp_prop = 2;
3569 return CHARPOS (tpos);
3572 /* Return the character position of the end of the display string that
3573 started at CHARPOS. If there's no display string at CHARPOS,
3574 return -1. A display string is either an overlay with `display'
3575 property whose value is a string or a `display' text property whose
3576 value is a string. */
3577 ptrdiff_t
3578 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3580 /* OBJECT = nil means current buffer. */
3581 Lisp_Object object =
3582 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3583 Lisp_Object pos = make_number (charpos);
3584 ptrdiff_t eob =
3585 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3587 if (charpos >= eob || (string->s && !STRINGP (object)))
3588 return eob;
3590 /* It could happen that the display property or overlay was removed
3591 since we found it in compute_display_string_pos above. One way
3592 this can happen is if JIT font-lock was called (through
3593 handle_fontified_prop), and jit-lock-functions remove text
3594 properties or overlays from the portion of buffer that includes
3595 CHARPOS. Muse mode is known to do that, for example. In this
3596 case, we return -1 to the caller, to signal that no display
3597 string is actually present at CHARPOS. See bidi_fetch_char for
3598 how this is handled.
3600 An alternative would be to never look for display properties past
3601 it->stop_charpos. But neither compute_display_string_pos nor
3602 bidi_fetch_char that calls it know or care where the next
3603 stop_charpos is. */
3604 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3605 return -1;
3607 /* Look forward for the first character where the `display' property
3608 changes. */
3609 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3611 return XFASTINT (pos);
3616 /***********************************************************************
3617 Fontification
3618 ***********************************************************************/
3620 /* Handle changes in the `fontified' property of the current buffer by
3621 calling hook functions from Qfontification_functions to fontify
3622 regions of text. */
3624 static enum prop_handled
3625 handle_fontified_prop (struct it *it)
3627 Lisp_Object prop, pos;
3628 enum prop_handled handled = HANDLED_NORMALLY;
3630 if (!NILP (Vmemory_full))
3631 return handled;
3633 /* Get the value of the `fontified' property at IT's current buffer
3634 position. (The `fontified' property doesn't have a special
3635 meaning in strings.) If the value is nil, call functions from
3636 Qfontification_functions. */
3637 if (!STRINGP (it->string)
3638 && it->s == NULL
3639 && !NILP (Vfontification_functions)
3640 && !NILP (Vrun_hooks)
3641 && (pos = make_number (IT_CHARPOS (*it)),
3642 prop = Fget_char_property (pos, Qfontified, Qnil),
3643 /* Ignore the special cased nil value always present at EOB since
3644 no amount of fontifying will be able to change it. */
3645 NILP (prop) && IT_CHARPOS (*it) < Z))
3647 ptrdiff_t count = SPECPDL_INDEX ();
3648 Lisp_Object val;
3649 struct buffer *obuf = current_buffer;
3650 ptrdiff_t begv = BEGV, zv = ZV;
3651 bool old_clip_changed = current_buffer->clip_changed;
3653 val = Vfontification_functions;
3654 specbind (Qfontification_functions, Qnil);
3656 eassert (it->end_charpos == ZV);
3658 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3659 safe_call1 (val, pos);
3660 else
3662 Lisp_Object fns, fn;
3663 struct gcpro gcpro1, gcpro2;
3665 fns = Qnil;
3666 GCPRO2 (val, fns);
3668 for (; CONSP (val); val = XCDR (val))
3670 fn = XCAR (val);
3672 if (EQ (fn, Qt))
3674 /* A value of t indicates this hook has a local
3675 binding; it means to run the global binding too.
3676 In a global value, t should not occur. If it
3677 does, we must ignore it to avoid an endless
3678 loop. */
3679 for (fns = Fdefault_value (Qfontification_functions);
3680 CONSP (fns);
3681 fns = XCDR (fns))
3683 fn = XCAR (fns);
3684 if (!EQ (fn, Qt))
3685 safe_call1 (fn, pos);
3688 else
3689 safe_call1 (fn, pos);
3692 UNGCPRO;
3695 unbind_to (count, Qnil);
3697 /* Fontification functions routinely call `save-restriction'.
3698 Normally, this tags clip_changed, which can confuse redisplay
3699 (see discussion in Bug#6671). Since we don't perform any
3700 special handling of fontification changes in the case where
3701 `save-restriction' isn't called, there's no point doing so in
3702 this case either. So, if the buffer's restrictions are
3703 actually left unchanged, reset clip_changed. */
3704 if (obuf == current_buffer)
3706 if (begv == BEGV && zv == ZV)
3707 current_buffer->clip_changed = old_clip_changed;
3709 /* There isn't much we can reasonably do to protect against
3710 misbehaving fontification, but here's a fig leaf. */
3711 else if (BUFFER_LIVE_P (obuf))
3712 set_buffer_internal_1 (obuf);
3714 /* The fontification code may have added/removed text.
3715 It could do even a lot worse, but let's at least protect against
3716 the most obvious case where only the text past `pos' gets changed',
3717 as is/was done in grep.el where some escapes sequences are turned
3718 into face properties (bug#7876). */
3719 it->end_charpos = ZV;
3721 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3722 something. This avoids an endless loop if they failed to
3723 fontify the text for which reason ever. */
3724 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3725 handled = HANDLED_RECOMPUTE_PROPS;
3728 return handled;
3733 /***********************************************************************
3734 Faces
3735 ***********************************************************************/
3737 /* Set up iterator IT from face properties at its current position.
3738 Called from handle_stop. */
3740 static enum prop_handled
3741 handle_face_prop (struct it *it)
3743 int new_face_id;
3744 ptrdiff_t next_stop;
3746 if (!STRINGP (it->string))
3748 new_face_id
3749 = face_at_buffer_position (it->w,
3750 IT_CHARPOS (*it),
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765 /* If it->face_id is -1, old_face below will be NULL, see
3766 the definition of FACE_FROM_ID. This will happen if this
3767 is the initial call that gets the face. */
3768 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3770 /* If the value of face_id of the iterator is -1, we have to
3771 look in front of IT's position and see whether there is a
3772 face there that's different from new_face_id. */
3773 if (!old_face && IT_CHARPOS (*it) > BEG)
3775 int prev_face_id = face_before_it_pos (it);
3777 old_face = FACE_FROM_ID (it->f, prev_face_id);
3780 /* If the new face has a box, but the old face does not,
3781 this is the start of a run of characters with box face,
3782 i.e. this character has a shadow on the left side. */
3783 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3784 && (old_face == NULL || !old_face->box));
3785 it->face_box_p = new_face->box != FACE_NO_BOX;
3788 else
3790 int base_face_id;
3791 ptrdiff_t bufpos;
3792 int i;
3793 Lisp_Object from_overlay
3794 = (it->current.overlay_string_index >= 0
3795 ? it->string_overlays[it->current.overlay_string_index
3796 % OVERLAY_STRING_CHUNK_SIZE]
3797 : Qnil);
3799 /* See if we got to this string directly or indirectly from
3800 an overlay property. That includes the before-string or
3801 after-string of an overlay, strings in display properties
3802 provided by an overlay, their text properties, etc.
3804 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3805 if (! NILP (from_overlay))
3806 for (i = it->sp - 1; i >= 0; i--)
3808 if (it->stack[i].current.overlay_string_index >= 0)
3809 from_overlay
3810 = it->string_overlays[it->stack[i].current.overlay_string_index
3811 % OVERLAY_STRING_CHUNK_SIZE];
3812 else if (! NILP (it->stack[i].from_overlay))
3813 from_overlay = it->stack[i].from_overlay;
3815 if (!NILP (from_overlay))
3816 break;
3819 if (! NILP (from_overlay))
3821 bufpos = IT_CHARPOS (*it);
3822 /* For a string from an overlay, the base face depends
3823 only on text properties and ignores overlays. */
3824 base_face_id
3825 = face_for_overlay_string (it->w,
3826 IT_CHARPOS (*it),
3827 &next_stop,
3828 (IT_CHARPOS (*it)
3829 + TEXT_PROP_DISTANCE_LIMIT),
3831 from_overlay);
3833 else
3835 bufpos = 0;
3837 /* For strings from a `display' property, use the face at
3838 IT's current buffer position as the base face to merge
3839 with, so that overlay strings appear in the same face as
3840 surrounding text, unless they specify their own faces.
3841 For strings from wrap-prefix and line-prefix properties,
3842 use the default face, possibly remapped via
3843 Vface_remapping_alist. */
3844 base_face_id = it->string_from_prefix_prop_p
3845 ? (!NILP (Vface_remapping_alist)
3846 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3847 : DEFAULT_FACE_ID)
3848 : underlying_face_id (it);
3851 new_face_id = face_at_string_position (it->w,
3852 it->string,
3853 IT_STRING_CHARPOS (*it),
3854 bufpos,
3855 &next_stop,
3856 base_face_id, 0);
3858 /* Is this a start of a run of characters with box? Caveat:
3859 this can be called for a freshly allocated iterator; face_id
3860 is -1 is this case. We know that the new face will not
3861 change until the next check pos, i.e. if the new face has a
3862 box, all characters up to that position will have a
3863 box. But, as usual, we don't know whether that position
3864 is really the end. */
3865 if (new_face_id != it->face_id)
3867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3870 /* If new face has a box but old face hasn't, this is the
3871 start of a run of characters with box, i.e. it has a
3872 shadow on the left side. */
3873 it->start_of_box_run_p
3874 = new_face->box && (old_face == NULL || !old_face->box);
3875 it->face_box_p = new_face->box != FACE_NO_BOX;
3879 it->face_id = new_face_id;
3880 return HANDLED_NORMALLY;
3884 /* Return the ID of the face ``underlying'' IT's current position,
3885 which is in a string. If the iterator is associated with a
3886 buffer, return the face at IT's current buffer position.
3887 Otherwise, use the iterator's base_face_id. */
3889 static int
3890 underlying_face_id (struct it *it)
3892 int face_id = it->base_face_id, i;
3894 eassert (STRINGP (it->string));
3896 for (i = it->sp - 1; i >= 0; --i)
3897 if (NILP (it->stack[i].string))
3898 face_id = it->stack[i].face_id;
3900 return face_id;
3904 /* Compute the face one character before or after the current position
3905 of IT, in the visual order. BEFORE_P non-zero means get the face
3906 in front (to the left in L2R paragraphs, to the right in R2L
3907 paragraphs) of IT's screen position. Value is the ID of the face. */
3909 static int
3910 face_before_or_after_it_pos (struct it *it, int before_p)
3912 int face_id, limit;
3913 ptrdiff_t next_check_charpos;
3914 struct it it_copy;
3915 void *it_copy_data = NULL;
3917 eassert (it->s == NULL);
3919 if (STRINGP (it->string))
3921 ptrdiff_t bufpos, charpos;
3922 int base_face_id;
3924 /* No face change past the end of the string (for the case
3925 we are padding with spaces). No face change before the
3926 string start. */
3927 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3928 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3929 return it->face_id;
3931 if (!it->bidi_p)
3933 /* Set charpos to the position before or after IT's current
3934 position, in the logical order, which in the non-bidi
3935 case is the same as the visual order. */
3936 if (before_p)
3937 charpos = IT_STRING_CHARPOS (*it) - 1;
3938 else if (it->what == IT_COMPOSITION)
3939 /* For composition, we must check the character after the
3940 composition. */
3941 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3942 else
3943 charpos = IT_STRING_CHARPOS (*it) + 1;
3945 else
3947 if (before_p)
3949 /* With bidi iteration, the character before the current
3950 in the visual order cannot be found by simple
3951 iteration, because "reverse" reordering is not
3952 supported. Instead, we need to use the move_it_*
3953 family of functions. */
3954 /* Ignore face changes before the first visible
3955 character on this display line. */
3956 if (it->current_x <= it->first_visible_x)
3957 return it->face_id;
3958 SAVE_IT (it_copy, *it, it_copy_data);
3959 /* Implementation note: Since move_it_in_display_line
3960 works in the iterator geometry, and thinks the first
3961 character is always the leftmost, even in R2L lines,
3962 we don't need to distinguish between the R2L and L2R
3963 cases here. */
3964 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3965 it_copy.current_x - 1, MOVE_TO_X);
3966 charpos = IT_STRING_CHARPOS (it_copy);
3967 RESTORE_IT (it, it, it_copy_data);
3969 else
3971 /* Set charpos to the string position of the character
3972 that comes after IT's current position in the visual
3973 order. */
3974 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3976 it_copy = *it;
3977 while (n--)
3978 bidi_move_to_visually_next (&it_copy.bidi_it);
3980 charpos = it_copy.bidi_it.charpos;
3983 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3985 if (it->current.overlay_string_index >= 0)
3986 bufpos = IT_CHARPOS (*it);
3987 else
3988 bufpos = 0;
3990 base_face_id = underlying_face_id (it);
3992 /* Get the face for ASCII, or unibyte. */
3993 face_id = face_at_string_position (it->w,
3994 it->string,
3995 charpos,
3996 bufpos,
3997 &next_check_charpos,
3998 base_face_id, 0);
4000 /* Correct the face for charsets different from ASCII. Do it
4001 for the multibyte case only. The face returned above is
4002 suitable for unibyte text if IT->string is unibyte. */
4003 if (STRING_MULTIBYTE (it->string))
4005 struct text_pos pos1 = string_pos (charpos, it->string);
4006 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4007 int c, len;
4008 struct face *face = FACE_FROM_ID (it->f, face_id);
4010 c = string_char_and_length (p, &len);
4011 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4014 else
4016 struct text_pos pos;
4018 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4019 || (IT_CHARPOS (*it) <= BEGV && before_p))
4020 return it->face_id;
4022 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4023 pos = it->current.pos;
4025 if (!it->bidi_p)
4027 if (before_p)
4028 DEC_TEXT_POS (pos, it->multibyte_p);
4029 else
4031 if (it->what == IT_COMPOSITION)
4033 /* For composition, we must check the position after
4034 the composition. */
4035 pos.charpos += it->cmp_it.nchars;
4036 pos.bytepos += it->len;
4038 else
4039 INC_TEXT_POS (pos, it->multibyte_p);
4042 else
4044 if (before_p)
4046 /* With bidi iteration, the character before the current
4047 in the visual order cannot be found by simple
4048 iteration, because "reverse" reordering is not
4049 supported. Instead, we need to use the move_it_*
4050 family of functions. */
4051 /* Ignore face changes before the first visible
4052 character on this display line. */
4053 if (it->current_x <= it->first_visible_x)
4054 return it->face_id;
4055 SAVE_IT (it_copy, *it, it_copy_data);
4056 /* Implementation note: Since move_it_in_display_line
4057 works in the iterator geometry, and thinks the first
4058 character is always the leftmost, even in R2L lines,
4059 we don't need to distinguish between the R2L and L2R
4060 cases here. */
4061 move_it_in_display_line (&it_copy, ZV,
4062 it_copy.current_x - 1, MOVE_TO_X);
4063 pos = it_copy.current.pos;
4064 RESTORE_IT (it, it, it_copy_data);
4066 else
4068 /* Set charpos to the buffer position of the character
4069 that comes after IT's current position in the visual
4070 order. */
4071 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4073 it_copy = *it;
4074 while (n--)
4075 bidi_move_to_visually_next (&it_copy.bidi_it);
4077 SET_TEXT_POS (pos,
4078 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4081 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4083 /* Determine face for CHARSET_ASCII, or unibyte. */
4084 face_id = face_at_buffer_position (it->w,
4085 CHARPOS (pos),
4086 &next_check_charpos,
4087 limit, 0, -1);
4089 /* Correct the face for charsets different from ASCII. Do it
4090 for the multibyte case only. The face returned above is
4091 suitable for unibyte text if current_buffer is unibyte. */
4092 if (it->multibyte_p)
4094 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4095 struct face *face = FACE_FROM_ID (it->f, face_id);
4096 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4100 return face_id;
4105 /***********************************************************************
4106 Invisible text
4107 ***********************************************************************/
4109 /* Set up iterator IT from invisible properties at its current
4110 position. Called from handle_stop. */
4112 static enum prop_handled
4113 handle_invisible_prop (struct it *it)
4115 enum prop_handled handled = HANDLED_NORMALLY;
4116 int invis_p;
4117 Lisp_Object prop;
4119 if (STRINGP (it->string))
4121 Lisp_Object end_charpos, limit, charpos;
4123 /* Get the value of the invisible text property at the
4124 current position. Value will be nil if there is no such
4125 property. */
4126 charpos = make_number (IT_STRING_CHARPOS (*it));
4127 prop = Fget_text_property (charpos, Qinvisible, it->string);
4128 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4130 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4132 /* Record whether we have to display an ellipsis for the
4133 invisible text. */
4134 int display_ellipsis_p = (invis_p == 2);
4135 ptrdiff_t len, endpos;
4137 handled = HANDLED_RECOMPUTE_PROPS;
4139 /* Get the position at which the next visible text can be
4140 found in IT->string, if any. */
4141 endpos = len = SCHARS (it->string);
4142 XSETINT (limit, len);
4145 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4146 it->string, limit);
4147 if (INTEGERP (end_charpos))
4149 endpos = XFASTINT (end_charpos);
4150 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4151 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4152 if (invis_p == 2)
4153 display_ellipsis_p = 1;
4156 while (invis_p && endpos < len);
4158 if (display_ellipsis_p)
4159 it->ellipsis_p = 1;
4161 if (endpos < len)
4163 /* Text at END_CHARPOS is visible. Move IT there. */
4164 struct text_pos old;
4165 ptrdiff_t oldpos;
4167 old = it->current.string_pos;
4168 oldpos = CHARPOS (old);
4169 if (it->bidi_p)
4171 if (it->bidi_it.first_elt
4172 && it->bidi_it.charpos < SCHARS (it->string))
4173 bidi_paragraph_init (it->paragraph_embedding,
4174 &it->bidi_it, 1);
4175 /* Bidi-iterate out of the invisible text. */
4178 bidi_move_to_visually_next (&it->bidi_it);
4180 while (oldpos <= it->bidi_it.charpos
4181 && it->bidi_it.charpos < endpos);
4183 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4184 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4185 if (IT_CHARPOS (*it) >= endpos)
4186 it->prev_stop = endpos;
4188 else
4190 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4191 compute_string_pos (&it->current.string_pos, old, it->string);
4194 else
4196 /* The rest of the string is invisible. If this is an
4197 overlay string, proceed with the next overlay string
4198 or whatever comes and return a character from there. */
4199 if (it->current.overlay_string_index >= 0
4200 && !display_ellipsis_p)
4202 next_overlay_string (it);
4203 /* Don't check for overlay strings when we just
4204 finished processing them. */
4205 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4207 else
4209 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4210 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4215 else
4217 ptrdiff_t newpos, next_stop, start_charpos, tem;
4218 Lisp_Object pos, overlay;
4220 /* First of all, is there invisible text at this position? */
4221 tem = start_charpos = IT_CHARPOS (*it);
4222 pos = make_number (tem);
4223 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4224 &overlay);
4225 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4227 /* If we are on invisible text, skip over it. */
4228 if (invis_p && start_charpos < it->end_charpos)
4230 /* Record whether we have to display an ellipsis for the
4231 invisible text. */
4232 int display_ellipsis_p = invis_p == 2;
4234 handled = HANDLED_RECOMPUTE_PROPS;
4236 /* Loop skipping over invisible text. The loop is left at
4237 ZV or with IT on the first char being visible again. */
4240 /* Try to skip some invisible text. Return value is the
4241 position reached which can be equal to where we start
4242 if there is nothing invisible there. This skips both
4243 over invisible text properties and overlays with
4244 invisible property. */
4245 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4247 /* If we skipped nothing at all we weren't at invisible
4248 text in the first place. If everything to the end of
4249 the buffer was skipped, end the loop. */
4250 if (newpos == tem || newpos >= ZV)
4251 invis_p = 0;
4252 else
4254 /* We skipped some characters but not necessarily
4255 all there are. Check if we ended up on visible
4256 text. Fget_char_property returns the property of
4257 the char before the given position, i.e. if we
4258 get invis_p = 0, this means that the char at
4259 newpos is visible. */
4260 pos = make_number (newpos);
4261 prop = Fget_char_property (pos, Qinvisible, it->window);
4262 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4265 /* If we ended up on invisible text, proceed to
4266 skip starting with next_stop. */
4267 if (invis_p)
4268 tem = next_stop;
4270 /* If there are adjacent invisible texts, don't lose the
4271 second one's ellipsis. */
4272 if (invis_p == 2)
4273 display_ellipsis_p = 1;
4275 while (invis_p);
4277 /* The position newpos is now either ZV or on visible text. */
4278 if (it->bidi_p)
4280 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4281 int on_newline =
4282 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4283 int after_newline =
4284 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4286 /* If the invisible text ends on a newline or on a
4287 character after a newline, we can avoid the costly,
4288 character by character, bidi iteration to NEWPOS, and
4289 instead simply reseat the iterator there. That's
4290 because all bidi reordering information is tossed at
4291 the newline. This is a big win for modes that hide
4292 complete lines, like Outline, Org, etc. */
4293 if (on_newline || after_newline)
4295 struct text_pos tpos;
4296 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4298 SET_TEXT_POS (tpos, newpos, bpos);
4299 reseat_1 (it, tpos, 0);
4300 /* If we reseat on a newline/ZV, we need to prep the
4301 bidi iterator for advancing to the next character
4302 after the newline/EOB, keeping the current paragraph
4303 direction (so that PRODUCE_GLYPHS does TRT wrt
4304 prepending/appending glyphs to a glyph row). */
4305 if (on_newline)
4307 it->bidi_it.first_elt = 0;
4308 it->bidi_it.paragraph_dir = pdir;
4309 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4310 it->bidi_it.nchars = 1;
4311 it->bidi_it.ch_len = 1;
4314 else /* Must use the slow method. */
4316 /* With bidi iteration, the region of invisible text
4317 could start and/or end in the middle of a
4318 non-base embedding level. Therefore, we need to
4319 skip invisible text using the bidi iterator,
4320 starting at IT's current position, until we find
4321 ourselves outside of the invisible text.
4322 Skipping invisible text _after_ bidi iteration
4323 avoids affecting the visual order of the
4324 displayed text when invisible properties are
4325 added or removed. */
4326 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4328 /* If we were `reseat'ed to a new paragraph,
4329 determine the paragraph base direction. We
4330 need to do it now because
4331 next_element_from_buffer may not have a
4332 chance to do it, if we are going to skip any
4333 text at the beginning, which resets the
4334 FIRST_ELT flag. */
4335 bidi_paragraph_init (it->paragraph_embedding,
4336 &it->bidi_it, 1);
4340 bidi_move_to_visually_next (&it->bidi_it);
4342 while (it->stop_charpos <= it->bidi_it.charpos
4343 && it->bidi_it.charpos < newpos);
4344 IT_CHARPOS (*it) = it->bidi_it.charpos;
4345 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4346 /* If we overstepped NEWPOS, record its position in
4347 the iterator, so that we skip invisible text if
4348 later the bidi iteration lands us in the
4349 invisible region again. */
4350 if (IT_CHARPOS (*it) >= newpos)
4351 it->prev_stop = newpos;
4354 else
4356 IT_CHARPOS (*it) = newpos;
4357 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4360 /* If there are before-strings at the start of invisible
4361 text, and the text is invisible because of a text
4362 property, arrange to show before-strings because 20.x did
4363 it that way. (If the text is invisible because of an
4364 overlay property instead of a text property, this is
4365 already handled in the overlay code.) */
4366 if (NILP (overlay)
4367 && get_overlay_strings (it, it->stop_charpos))
4369 handled = HANDLED_RECOMPUTE_PROPS;
4370 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4372 else if (display_ellipsis_p)
4374 /* Make sure that the glyphs of the ellipsis will get
4375 correct `charpos' values. If we would not update
4376 it->position here, the glyphs would belong to the
4377 last visible character _before_ the invisible
4378 text, which confuses `set_cursor_from_row'.
4380 We use the last invisible position instead of the
4381 first because this way the cursor is always drawn on
4382 the first "." of the ellipsis, whenever PT is inside
4383 the invisible text. Otherwise the cursor would be
4384 placed _after_ the ellipsis when the point is after the
4385 first invisible character. */
4386 if (!STRINGP (it->object))
4388 it->position.charpos = newpos - 1;
4389 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4391 it->ellipsis_p = 1;
4392 /* Let the ellipsis display before
4393 considering any properties of the following char.
4394 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4395 handled = HANDLED_RETURN;
4400 return handled;
4404 /* Make iterator IT return `...' next.
4405 Replaces LEN characters from buffer. */
4407 static void
4408 setup_for_ellipsis (struct it *it, int len)
4410 /* Use the display table definition for `...'. Invalid glyphs
4411 will be handled by the method returning elements from dpvec. */
4412 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4414 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4415 it->dpvec = v->contents;
4416 it->dpend = v->contents + v->header.size;
4418 else
4420 /* Default `...'. */
4421 it->dpvec = default_invis_vector;
4422 it->dpend = default_invis_vector + 3;
4425 it->dpvec_char_len = len;
4426 it->current.dpvec_index = 0;
4427 it->dpvec_face_id = -1;
4429 /* Remember the current face id in case glyphs specify faces.
4430 IT's face is restored in set_iterator_to_next.
4431 saved_face_id was set to preceding char's face in handle_stop. */
4432 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4433 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4435 it->method = GET_FROM_DISPLAY_VECTOR;
4436 it->ellipsis_p = 1;
4441 /***********************************************************************
4442 'display' property
4443 ***********************************************************************/
4445 /* Set up iterator IT from `display' property at its current position.
4446 Called from handle_stop.
4447 We return HANDLED_RETURN if some part of the display property
4448 overrides the display of the buffer text itself.
4449 Otherwise we return HANDLED_NORMALLY. */
4451 static enum prop_handled
4452 handle_display_prop (struct it *it)
4454 Lisp_Object propval, object, overlay;
4455 struct text_pos *position;
4456 ptrdiff_t bufpos;
4457 /* Nonzero if some property replaces the display of the text itself. */
4458 int display_replaced_p = 0;
4460 if (STRINGP (it->string))
4462 object = it->string;
4463 position = &it->current.string_pos;
4464 bufpos = CHARPOS (it->current.pos);
4466 else
4468 XSETWINDOW (object, it->w);
4469 position = &it->current.pos;
4470 bufpos = CHARPOS (*position);
4473 /* Reset those iterator values set from display property values. */
4474 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4475 it->space_width = Qnil;
4476 it->font_height = Qnil;
4477 it->voffset = 0;
4479 /* We don't support recursive `display' properties, i.e. string
4480 values that have a string `display' property, that have a string
4481 `display' property etc. */
4482 if (!it->string_from_display_prop_p)
4483 it->area = TEXT_AREA;
4485 propval = get_char_property_and_overlay (make_number (position->charpos),
4486 Qdisplay, object, &overlay);
4487 if (NILP (propval))
4488 return HANDLED_NORMALLY;
4489 /* Now OVERLAY is the overlay that gave us this property, or nil
4490 if it was a text property. */
4492 if (!STRINGP (it->string))
4493 object = it->w->contents;
4495 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4496 position, bufpos,
4497 FRAME_WINDOW_P (it->f));
4499 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4502 /* Subroutine of handle_display_prop. Returns non-zero if the display
4503 specification in SPEC is a replacing specification, i.e. it would
4504 replace the text covered by `display' property with something else,
4505 such as an image or a display string. If SPEC includes any kind or
4506 `(space ...) specification, the value is 2; this is used by
4507 compute_display_string_pos, which see.
4509 See handle_single_display_spec for documentation of arguments.
4510 frame_window_p is non-zero if the window being redisplayed is on a
4511 GUI frame; this argument is used only if IT is NULL, see below.
4513 IT can be NULL, if this is called by the bidi reordering code
4514 through compute_display_string_pos, which see. In that case, this
4515 function only examines SPEC, but does not otherwise "handle" it, in
4516 the sense that it doesn't set up members of IT from the display
4517 spec. */
4518 static int
4519 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4520 Lisp_Object overlay, struct text_pos *position,
4521 ptrdiff_t bufpos, int frame_window_p)
4523 int replacing_p = 0;
4524 int rv;
4526 if (CONSP (spec)
4527 /* Simple specifications. */
4528 && !EQ (XCAR (spec), Qimage)
4529 && !EQ (XCAR (spec), Qspace)
4530 && !EQ (XCAR (spec), Qwhen)
4531 && !EQ (XCAR (spec), Qslice)
4532 && !EQ (XCAR (spec), Qspace_width)
4533 && !EQ (XCAR (spec), Qheight)
4534 && !EQ (XCAR (spec), Qraise)
4535 /* Marginal area specifications. */
4536 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4537 && !EQ (XCAR (spec), Qleft_fringe)
4538 && !EQ (XCAR (spec), Qright_fringe)
4539 && !NILP (XCAR (spec)))
4541 for (; CONSP (spec); spec = XCDR (spec))
4543 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4555 else if (VECTORP (spec))
4557 ptrdiff_t i;
4558 for (i = 0; i < ASIZE (spec); ++i)
4559 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4560 overlay, position, bufpos,
4561 replacing_p, frame_window_p)))
4563 replacing_p = rv;
4564 /* If some text in a string is replaced, `position' no
4565 longer points to the position of `object'. */
4566 if (!it || STRINGP (object))
4567 break;
4570 else
4572 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4573 position, bufpos, 0,
4574 frame_window_p)))
4575 replacing_p = rv;
4578 return replacing_p;
4581 /* Value is the position of the end of the `display' property starting
4582 at START_POS in OBJECT. */
4584 static struct text_pos
4585 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4587 Lisp_Object end;
4588 struct text_pos end_pos;
4590 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4591 Qdisplay, object, Qnil);
4592 CHARPOS (end_pos) = XFASTINT (end);
4593 if (STRINGP (object))
4594 compute_string_pos (&end_pos, start_pos, it->string);
4595 else
4596 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4598 return end_pos;
4602 /* Set up IT from a single `display' property specification SPEC. OBJECT
4603 is the object in which the `display' property was found. *POSITION
4604 is the position in OBJECT at which the `display' property was found.
4605 BUFPOS is the buffer position of OBJECT (different from POSITION if
4606 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4607 previously saw a display specification which already replaced text
4608 display with something else, for example an image; we ignore such
4609 properties after the first one has been processed.
4611 OVERLAY is the overlay this `display' property came from,
4612 or nil if it was a text property.
4614 If SPEC is a `space' or `image' specification, and in some other
4615 cases too, set *POSITION to the position where the `display'
4616 property ends.
4618 If IT is NULL, only examine the property specification in SPEC, but
4619 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4620 is intended to be displayed in a window on a GUI frame.
4622 Value is non-zero if something was found which replaces the display
4623 of buffer or string text. */
4625 static int
4626 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4627 Lisp_Object overlay, struct text_pos *position,
4628 ptrdiff_t bufpos, int display_replaced_p,
4629 int frame_window_p)
4631 Lisp_Object form;
4632 Lisp_Object location, value;
4633 struct text_pos start_pos = *position;
4634 int valid_p;
4636 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4637 If the result is non-nil, use VALUE instead of SPEC. */
4638 form = Qt;
4639 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4641 spec = XCDR (spec);
4642 if (!CONSP (spec))
4643 return 0;
4644 form = XCAR (spec);
4645 spec = XCDR (spec);
4648 if (!NILP (form) && !EQ (form, Qt))
4650 ptrdiff_t count = SPECPDL_INDEX ();
4651 struct gcpro gcpro1;
4653 /* Bind `object' to the object having the `display' property, a
4654 buffer or string. Bind `position' to the position in the
4655 object where the property was found, and `buffer-position'
4656 to the current position in the buffer. */
4658 if (NILP (object))
4659 XSETBUFFER (object, current_buffer);
4660 specbind (Qobject, object);
4661 specbind (Qposition, make_number (CHARPOS (*position)));
4662 specbind (Qbuffer_position, make_number (bufpos));
4663 GCPRO1 (form);
4664 form = safe_eval (form);
4665 UNGCPRO;
4666 unbind_to (count, Qnil);
4669 if (NILP (form))
4670 return 0;
4672 /* Handle `(height HEIGHT)' specifications. */
4673 if (CONSP (spec)
4674 && EQ (XCAR (spec), Qheight)
4675 && CONSP (XCDR (spec)))
4677 if (it)
4679 if (!FRAME_WINDOW_P (it->f))
4680 return 0;
4682 it->font_height = XCAR (XCDR (spec));
4683 if (!NILP (it->font_height))
4685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4686 int new_height = -1;
4688 if (CONSP (it->font_height)
4689 && (EQ (XCAR (it->font_height), Qplus)
4690 || EQ (XCAR (it->font_height), Qminus))
4691 && CONSP (XCDR (it->font_height))
4692 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4694 /* `(+ N)' or `(- N)' where N is an integer. */
4695 int steps = XINT (XCAR (XCDR (it->font_height)));
4696 if (EQ (XCAR (it->font_height), Qplus))
4697 steps = - steps;
4698 it->face_id = smaller_face (it->f, it->face_id, steps);
4700 else if (FUNCTIONP (it->font_height))
4702 /* Call function with current height as argument.
4703 Value is the new height. */
4704 Lisp_Object height;
4705 height = safe_call1 (it->font_height,
4706 face->lface[LFACE_HEIGHT_INDEX]);
4707 if (NUMBERP (height))
4708 new_height = XFLOATINT (height);
4710 else if (NUMBERP (it->font_height))
4712 /* Value is a multiple of the canonical char height. */
4713 struct face *f;
4715 f = FACE_FROM_ID (it->f,
4716 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4717 new_height = (XFLOATINT (it->font_height)
4718 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4720 else
4722 /* Evaluate IT->font_height with `height' bound to the
4723 current specified height to get the new height. */
4724 ptrdiff_t count = SPECPDL_INDEX ();
4726 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4727 value = safe_eval (it->font_height);
4728 unbind_to (count, Qnil);
4730 if (NUMBERP (value))
4731 new_height = XFLOATINT (value);
4734 if (new_height > 0)
4735 it->face_id = face_with_height (it->f, it->face_id, new_height);
4739 return 0;
4742 /* Handle `(space-width WIDTH)'. */
4743 if (CONSP (spec)
4744 && EQ (XCAR (spec), Qspace_width)
4745 && CONSP (XCDR (spec)))
4747 if (it)
4749 if (!FRAME_WINDOW_P (it->f))
4750 return 0;
4752 value = XCAR (XCDR (spec));
4753 if (NUMBERP (value) && XFLOATINT (value) > 0)
4754 it->space_width = value;
4757 return 0;
4760 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4761 if (CONSP (spec)
4762 && EQ (XCAR (spec), Qslice))
4764 Lisp_Object tem;
4766 if (it)
4768 if (!FRAME_WINDOW_P (it->f))
4769 return 0;
4771 if (tem = XCDR (spec), CONSP (tem))
4773 it->slice.x = XCAR (tem);
4774 if (tem = XCDR (tem), CONSP (tem))
4776 it->slice.y = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4779 it->slice.width = XCAR (tem);
4780 if (tem = XCDR (tem), CONSP (tem))
4781 it->slice.height = XCAR (tem);
4787 return 0;
4790 /* Handle `(raise FACTOR)'. */
4791 if (CONSP (spec)
4792 && EQ (XCAR (spec), Qraise)
4793 && CONSP (XCDR (spec)))
4795 if (it)
4797 if (!FRAME_WINDOW_P (it->f))
4798 return 0;
4800 #ifdef HAVE_WINDOW_SYSTEM
4801 value = XCAR (XCDR (spec));
4802 if (NUMBERP (value))
4804 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4805 it->voffset = - (XFLOATINT (value)
4806 * (FONT_HEIGHT (face->font)));
4808 #endif /* HAVE_WINDOW_SYSTEM */
4811 return 0;
4814 /* Don't handle the other kinds of display specifications
4815 inside a string that we got from a `display' property. */
4816 if (it && it->string_from_display_prop_p)
4817 return 0;
4819 /* Characters having this form of property are not displayed, so
4820 we have to find the end of the property. */
4821 if (it)
4823 start_pos = *position;
4824 *position = display_prop_end (it, object, start_pos);
4826 value = Qnil;
4828 /* Stop the scan at that end position--we assume that all
4829 text properties change there. */
4830 if (it)
4831 it->stop_charpos = position->charpos;
4833 /* Handle `(left-fringe BITMAP [FACE])'
4834 and `(right-fringe BITMAP [FACE])'. */
4835 if (CONSP (spec)
4836 && (EQ (XCAR (spec), Qleft_fringe)
4837 || EQ (XCAR (spec), Qright_fringe))
4838 && CONSP (XCDR (spec)))
4840 int fringe_bitmap;
4842 if (it)
4844 if (!FRAME_WINDOW_P (it->f))
4845 /* If we return here, POSITION has been advanced
4846 across the text with this property. */
4848 /* Synchronize the bidi iterator with POSITION. This is
4849 needed because we are not going to push the iterator
4850 on behalf of this display property, so there will be
4851 no pop_it call to do this synchronization for us. */
4852 if (it->bidi_p)
4854 it->position = *position;
4855 iterate_out_of_display_property (it);
4856 *position = it->position;
4858 return 1;
4861 else if (!frame_window_p)
4862 return 1;
4864 #ifdef HAVE_WINDOW_SYSTEM
4865 value = XCAR (XCDR (spec));
4866 if (!SYMBOLP (value)
4867 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4868 /* If we return here, POSITION has been advanced
4869 across the text with this property. */
4871 if (it && it->bidi_p)
4873 it->position = *position;
4874 iterate_out_of_display_property (it);
4875 *position = it->position;
4877 return 1;
4880 if (it)
4882 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4884 if (CONSP (XCDR (XCDR (spec))))
4886 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4887 int face_id2 = lookup_derived_face (it->f, face_name,
4888 FRINGE_FACE_ID, 0);
4889 if (face_id2 >= 0)
4890 face_id = face_id2;
4893 /* Save current settings of IT so that we can restore them
4894 when we are finished with the glyph property value. */
4895 push_it (it, position);
4897 it->area = TEXT_AREA;
4898 it->what = IT_IMAGE;
4899 it->image_id = -1; /* no image */
4900 it->position = start_pos;
4901 it->object = NILP (object) ? it->w->contents : object;
4902 it->method = GET_FROM_IMAGE;
4903 it->from_overlay = Qnil;
4904 it->face_id = face_id;
4905 it->from_disp_prop_p = 1;
4907 /* Say that we haven't consumed the characters with
4908 `display' property yet. The call to pop_it in
4909 set_iterator_to_next will clean this up. */
4910 *position = start_pos;
4912 if (EQ (XCAR (spec), Qleft_fringe))
4914 it->left_user_fringe_bitmap = fringe_bitmap;
4915 it->left_user_fringe_face_id = face_id;
4917 else
4919 it->right_user_fringe_bitmap = fringe_bitmap;
4920 it->right_user_fringe_face_id = face_id;
4923 #endif /* HAVE_WINDOW_SYSTEM */
4924 return 1;
4927 /* Prepare to handle `((margin left-margin) ...)',
4928 `((margin right-margin) ...)' and `((margin nil) ...)'
4929 prefixes for display specifications. */
4930 location = Qunbound;
4931 if (CONSP (spec) && CONSP (XCAR (spec)))
4933 Lisp_Object tem;
4935 value = XCDR (spec);
4936 if (CONSP (value))
4937 value = XCAR (value);
4939 tem = XCAR (spec);
4940 if (EQ (XCAR (tem), Qmargin)
4941 && (tem = XCDR (tem),
4942 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4943 (NILP (tem)
4944 || EQ (tem, Qleft_margin)
4945 || EQ (tem, Qright_margin))))
4946 location = tem;
4949 if (EQ (location, Qunbound))
4951 location = Qnil;
4952 value = spec;
4955 /* After this point, VALUE is the property after any
4956 margin prefix has been stripped. It must be a string,
4957 an image specification, or `(space ...)'.
4959 LOCATION specifies where to display: `left-margin',
4960 `right-margin' or nil. */
4962 valid_p = (STRINGP (value)
4963 #ifdef HAVE_WINDOW_SYSTEM
4964 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4965 && valid_image_p (value))
4966 #endif /* not HAVE_WINDOW_SYSTEM */
4967 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4969 if (valid_p && !display_replaced_p)
4971 int retval = 1;
4973 if (!it)
4975 /* Callers need to know whether the display spec is any kind
4976 of `(space ...)' spec that is about to affect text-area
4977 display. */
4978 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4979 retval = 2;
4980 return retval;
4983 /* Save current settings of IT so that we can restore them
4984 when we are finished with the glyph property value. */
4985 push_it (it, position);
4986 it->from_overlay = overlay;
4987 it->from_disp_prop_p = 1;
4989 if (NILP (location))
4990 it->area = TEXT_AREA;
4991 else if (EQ (location, Qleft_margin))
4992 it->area = LEFT_MARGIN_AREA;
4993 else
4994 it->area = RIGHT_MARGIN_AREA;
4996 if (STRINGP (value))
4998 it->string = value;
4999 it->multibyte_p = STRING_MULTIBYTE (it->string);
5000 it->current.overlay_string_index = -1;
5001 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5002 it->end_charpos = it->string_nchars = SCHARS (it->string);
5003 it->method = GET_FROM_STRING;
5004 it->stop_charpos = 0;
5005 it->prev_stop = 0;
5006 it->base_level_stop = 0;
5007 it->string_from_display_prop_p = 1;
5008 /* Say that we haven't consumed the characters with
5009 `display' property yet. The call to pop_it in
5010 set_iterator_to_next will clean this up. */
5011 if (BUFFERP (object))
5012 *position = start_pos;
5014 /* Force paragraph direction to be that of the parent
5015 object. If the parent object's paragraph direction is
5016 not yet determined, default to L2R. */
5017 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5018 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5019 else
5020 it->paragraph_embedding = L2R;
5022 /* Set up the bidi iterator for this display string. */
5023 if (it->bidi_p)
5025 it->bidi_it.string.lstring = it->string;
5026 it->bidi_it.string.s = NULL;
5027 it->bidi_it.string.schars = it->end_charpos;
5028 it->bidi_it.string.bufpos = bufpos;
5029 it->bidi_it.string.from_disp_str = 1;
5030 it->bidi_it.string.unibyte = !it->multibyte_p;
5031 it->bidi_it.w = it->w;
5032 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5035 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5037 it->method = GET_FROM_STRETCH;
5038 it->object = value;
5039 *position = it->position = start_pos;
5040 retval = 1 + (it->area == TEXT_AREA);
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 else
5045 it->what = IT_IMAGE;
5046 it->image_id = lookup_image (it->f, value);
5047 it->position = start_pos;
5048 it->object = NILP (object) ? it->w->contents : object;
5049 it->method = GET_FROM_IMAGE;
5051 /* Say that we haven't consumed the characters with
5052 `display' property yet. The call to pop_it in
5053 set_iterator_to_next will clean this up. */
5054 *position = start_pos;
5056 #endif /* HAVE_WINDOW_SYSTEM */
5058 return retval;
5061 /* Invalid property or property not supported. Restore
5062 POSITION to what it was before. */
5063 *position = start_pos;
5064 return 0;
5067 /* Check if PROP is a display property value whose text should be
5068 treated as intangible. OVERLAY is the overlay from which PROP
5069 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5070 specify the buffer position covered by PROP. */
5073 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5074 ptrdiff_t charpos, ptrdiff_t bytepos)
5076 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5077 struct text_pos position;
5079 SET_TEXT_POS (position, charpos, bytepos);
5080 return handle_display_spec (NULL, prop, Qnil, overlay,
5081 &position, charpos, frame_window_p);
5085 /* Return 1 if PROP is a display sub-property value containing STRING.
5087 Implementation note: this and the following function are really
5088 special cases of handle_display_spec and
5089 handle_single_display_spec, and should ideally use the same code.
5090 Until they do, these two pairs must be consistent and must be
5091 modified in sync. */
5093 static int
5094 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5096 if (EQ (string, prop))
5097 return 1;
5099 /* Skip over `when FORM'. */
5100 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5102 prop = XCDR (prop);
5103 if (!CONSP (prop))
5104 return 0;
5105 /* Actually, the condition following `when' should be eval'ed,
5106 like handle_single_display_spec does, and we should return
5107 zero if it evaluates to nil. However, this function is
5108 called only when the buffer was already displayed and some
5109 glyph in the glyph matrix was found to come from a display
5110 string. Therefore, the condition was already evaluated, and
5111 the result was non-nil, otherwise the display string wouldn't
5112 have been displayed and we would have never been called for
5113 this property. Thus, we can skip the evaluation and assume
5114 its result is non-nil. */
5115 prop = XCDR (prop);
5118 if (CONSP (prop))
5119 /* Skip over `margin LOCATION'. */
5120 if (EQ (XCAR (prop), Qmargin))
5122 prop = XCDR (prop);
5123 if (!CONSP (prop))
5124 return 0;
5126 prop = XCDR (prop);
5127 if (!CONSP (prop))
5128 return 0;
5131 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5135 /* Return 1 if STRING appears in the `display' property PROP. */
5137 static int
5138 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5140 if (CONSP (prop)
5141 && !EQ (XCAR (prop), Qwhen)
5142 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5144 /* A list of sub-properties. */
5145 while (CONSP (prop))
5147 if (single_display_spec_string_p (XCAR (prop), string))
5148 return 1;
5149 prop = XCDR (prop);
5152 else if (VECTORP (prop))
5154 /* A vector of sub-properties. */
5155 ptrdiff_t i;
5156 for (i = 0; i < ASIZE (prop); ++i)
5157 if (single_display_spec_string_p (AREF (prop, i), string))
5158 return 1;
5160 else
5161 return single_display_spec_string_p (prop, string);
5163 return 0;
5166 /* Look for STRING in overlays and text properties in the current
5167 buffer, between character positions FROM and TO (excluding TO).
5168 BACK_P non-zero means look back (in this case, TO is supposed to be
5169 less than FROM).
5170 Value is the first character position where STRING was found, or
5171 zero if it wasn't found before hitting TO.
5173 This function may only use code that doesn't eval because it is
5174 called asynchronously from note_mouse_highlight. */
5176 static ptrdiff_t
5177 string_buffer_position_lim (Lisp_Object string,
5178 ptrdiff_t from, ptrdiff_t to, int back_p)
5180 Lisp_Object limit, prop, pos;
5181 int found = 0;
5183 pos = make_number (max (from, BEGV));
5185 if (!back_p) /* looking forward */
5187 limit = make_number (min (to, ZV));
5188 while (!found && !EQ (pos, limit))
5190 prop = Fget_char_property (pos, Qdisplay, Qnil);
5191 if (!NILP (prop) && display_prop_string_p (prop, string))
5192 found = 1;
5193 else
5194 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5195 limit);
5198 else /* looking back */
5200 limit = make_number (max (to, BEGV));
5201 while (!found && !EQ (pos, limit))
5203 prop = Fget_char_property (pos, Qdisplay, Qnil);
5204 if (!NILP (prop) && display_prop_string_p (prop, string))
5205 found = 1;
5206 else
5207 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5208 limit);
5212 return found ? XINT (pos) : 0;
5215 /* Determine which buffer position in current buffer STRING comes from.
5216 AROUND_CHARPOS is an approximate position where it could come from.
5217 Value is the buffer position or 0 if it couldn't be determined.
5219 This function is necessary because we don't record buffer positions
5220 in glyphs generated from strings (to keep struct glyph small).
5221 This function may only use code that doesn't eval because it is
5222 called asynchronously from note_mouse_highlight. */
5224 static ptrdiff_t
5225 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5227 const int MAX_DISTANCE = 1000;
5228 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos + MAX_DISTANCE,
5232 if (!found)
5233 found = string_buffer_position_lim (string, around_charpos,
5234 around_charpos - MAX_DISTANCE, 1);
5235 return found;
5240 /***********************************************************************
5241 `composition' property
5242 ***********************************************************************/
5244 /* Set up iterator IT from `composition' property at its current
5245 position. Called from handle_stop. */
5247 static enum prop_handled
5248 handle_composition_prop (struct it *it)
5250 Lisp_Object prop, string;
5251 ptrdiff_t pos, pos_byte, start, end;
5253 if (STRINGP (it->string))
5255 unsigned char *s;
5257 pos = IT_STRING_CHARPOS (*it);
5258 pos_byte = IT_STRING_BYTEPOS (*it);
5259 string = it->string;
5260 s = SDATA (string) + pos_byte;
5261 it->c = STRING_CHAR (s);
5263 else
5265 pos = IT_CHARPOS (*it);
5266 pos_byte = IT_BYTEPOS (*it);
5267 string = Qnil;
5268 it->c = FETCH_CHAR (pos_byte);
5271 /* If there's a valid composition and point is not inside of the
5272 composition (in the case that the composition is from the current
5273 buffer), draw a glyph composed from the composition components. */
5274 if (find_composition (pos, -1, &start, &end, &prop, string)
5275 && composition_valid_p (start, end, prop)
5276 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5278 if (start < pos)
5279 /* As we can't handle this situation (perhaps font-lock added
5280 a new composition), we just return here hoping that next
5281 redisplay will detect this composition much earlier. */
5282 return HANDLED_NORMALLY;
5283 if (start != pos)
5285 if (STRINGP (it->string))
5286 pos_byte = string_char_to_byte (it->string, start);
5287 else
5288 pos_byte = CHAR_TO_BYTE (start);
5290 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5291 prop, string);
5293 if (it->cmp_it.id >= 0)
5295 it->cmp_it.ch = -1;
5296 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5297 it->cmp_it.nglyphs = -1;
5301 return HANDLED_NORMALLY;
5306 /***********************************************************************
5307 Overlay strings
5308 ***********************************************************************/
5310 /* The following structure is used to record overlay strings for
5311 later sorting in load_overlay_strings. */
5313 struct overlay_entry
5315 Lisp_Object overlay;
5316 Lisp_Object string;
5317 EMACS_INT priority;
5318 int after_string_p;
5322 /* Set up iterator IT from overlay strings at its current position.
5323 Called from handle_stop. */
5325 static enum prop_handled
5326 handle_overlay_change (struct it *it)
5328 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5329 return HANDLED_RECOMPUTE_PROPS;
5330 else
5331 return HANDLED_NORMALLY;
5335 /* Set up the next overlay string for delivery by IT, if there is an
5336 overlay string to deliver. Called by set_iterator_to_next when the
5337 end of the current overlay string is reached. If there are more
5338 overlay strings to display, IT->string and
5339 IT->current.overlay_string_index are set appropriately here.
5340 Otherwise IT->string is set to nil. */
5342 static void
5343 next_overlay_string (struct it *it)
5345 ++it->current.overlay_string_index;
5346 if (it->current.overlay_string_index == it->n_overlay_strings)
5348 /* No more overlay strings. Restore IT's settings to what
5349 they were before overlay strings were processed, and
5350 continue to deliver from current_buffer. */
5352 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5353 pop_it (it);
5354 eassert (it->sp > 0
5355 || (NILP (it->string)
5356 && it->method == GET_FROM_BUFFER
5357 && it->stop_charpos >= BEGV
5358 && it->stop_charpos <= it->end_charpos));
5359 it->current.overlay_string_index = -1;
5360 it->n_overlay_strings = 0;
5361 it->overlay_strings_charpos = -1;
5362 /* If there's an empty display string on the stack, pop the
5363 stack, to resync the bidi iterator with IT's position. Such
5364 empty strings are pushed onto the stack in
5365 get_overlay_strings_1. */
5366 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5367 pop_it (it);
5369 /* If we're at the end of the buffer, record that we have
5370 processed the overlay strings there already, so that
5371 next_element_from_buffer doesn't try it again. */
5372 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5373 it->overlay_strings_at_end_processed_p = 1;
5375 else
5377 /* There are more overlay strings to process. If
5378 IT->current.overlay_string_index has advanced to a position
5379 where we must load IT->overlay_strings with more strings, do
5380 it. We must load at the IT->overlay_strings_charpos where
5381 IT->n_overlay_strings was originally computed; when invisible
5382 text is present, this might not be IT_CHARPOS (Bug#7016). */
5383 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5385 if (it->current.overlay_string_index && i == 0)
5386 load_overlay_strings (it, it->overlay_strings_charpos);
5388 /* Initialize IT to deliver display elements from the overlay
5389 string. */
5390 it->string = it->overlay_strings[i];
5391 it->multibyte_p = STRING_MULTIBYTE (it->string);
5392 SET_TEXT_POS (it->current.string_pos, 0, 0);
5393 it->method = GET_FROM_STRING;
5394 it->stop_charpos = 0;
5395 it->end_charpos = SCHARS (it->string);
5396 if (it->cmp_it.stop_pos >= 0)
5397 it->cmp_it.stop_pos = 0;
5398 it->prev_stop = 0;
5399 it->base_level_stop = 0;
5401 /* Set up the bidi iterator for this overlay string. */
5402 if (it->bidi_p)
5404 it->bidi_it.string.lstring = it->string;
5405 it->bidi_it.string.s = NULL;
5406 it->bidi_it.string.schars = SCHARS (it->string);
5407 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5408 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5409 it->bidi_it.string.unibyte = !it->multibyte_p;
5410 it->bidi_it.w = it->w;
5411 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5415 CHECK_IT (it);
5419 /* Compare two overlay_entry structures E1 and E2. Used as a
5420 comparison function for qsort in load_overlay_strings. Overlay
5421 strings for the same position are sorted so that
5423 1. All after-strings come in front of before-strings, except
5424 when they come from the same overlay.
5426 2. Within after-strings, strings are sorted so that overlay strings
5427 from overlays with higher priorities come first.
5429 2. Within before-strings, strings are sorted so that overlay
5430 strings from overlays with higher priorities come last.
5432 Value is analogous to strcmp. */
5435 static int
5436 compare_overlay_entries (const void *e1, const void *e2)
5438 struct overlay_entry const *entry1 = e1;
5439 struct overlay_entry const *entry2 = e2;
5440 int result;
5442 if (entry1->after_string_p != entry2->after_string_p)
5444 /* Let after-strings appear in front of before-strings if
5445 they come from different overlays. */
5446 if (EQ (entry1->overlay, entry2->overlay))
5447 result = entry1->after_string_p ? 1 : -1;
5448 else
5449 result = entry1->after_string_p ? -1 : 1;
5451 else if (entry1->priority != entry2->priority)
5453 if (entry1->after_string_p)
5454 /* After-strings sorted in order of decreasing priority. */
5455 result = entry2->priority < entry1->priority ? -1 : 1;
5456 else
5457 /* Before-strings sorted in order of increasing priority. */
5458 result = entry1->priority < entry2->priority ? -1 : 1;
5460 else
5461 result = 0;
5463 return result;
5467 /* Load the vector IT->overlay_strings with overlay strings from IT's
5468 current buffer position, or from CHARPOS if that is > 0. Set
5469 IT->n_overlays to the total number of overlay strings found.
5471 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5472 a time. On entry into load_overlay_strings,
5473 IT->current.overlay_string_index gives the number of overlay
5474 strings that have already been loaded by previous calls to this
5475 function.
5477 IT->add_overlay_start contains an additional overlay start
5478 position to consider for taking overlay strings from, if non-zero.
5479 This position comes into play when the overlay has an `invisible'
5480 property, and both before and after-strings. When we've skipped to
5481 the end of the overlay, because of its `invisible' property, we
5482 nevertheless want its before-string to appear.
5483 IT->add_overlay_start will contain the overlay start position
5484 in this case.
5486 Overlay strings are sorted so that after-string strings come in
5487 front of before-string strings. Within before and after-strings,
5488 strings are sorted by overlay priority. See also function
5489 compare_overlay_entries. */
5491 static void
5492 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5494 Lisp_Object overlay, window, str, invisible;
5495 struct Lisp_Overlay *ov;
5496 ptrdiff_t start, end;
5497 ptrdiff_t size = 20;
5498 ptrdiff_t n = 0, i, j;
5499 int invis_p;
5500 struct overlay_entry *entries = alloca (size * sizeof *entries);
5501 USE_SAFE_ALLOCA;
5503 if (charpos <= 0)
5504 charpos = IT_CHARPOS (*it);
5506 /* Append the overlay string STRING of overlay OVERLAY to vector
5507 `entries' which has size `size' and currently contains `n'
5508 elements. AFTER_P non-zero means STRING is an after-string of
5509 OVERLAY. */
5510 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5511 do \
5513 Lisp_Object priority; \
5515 if (n == size) \
5517 struct overlay_entry *old = entries; \
5518 SAFE_NALLOCA (entries, 2, size); \
5519 memcpy (entries, old, size * sizeof *entries); \
5520 size *= 2; \
5523 entries[n].string = (STRING); \
5524 entries[n].overlay = (OVERLAY); \
5525 priority = Foverlay_get ((OVERLAY), Qpriority); \
5526 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5527 entries[n].after_string_p = (AFTER_P); \
5528 ++n; \
5530 while (0)
5532 /* Process overlay before the overlay center. */
5533 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5535 XSETMISC (overlay, ov);
5536 eassert (OVERLAYP (overlay));
5537 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5538 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5540 if (end < charpos)
5541 break;
5543 /* Skip this overlay if it doesn't start or end at IT's current
5544 position. */
5545 if (end != charpos && start != charpos)
5546 continue;
5548 /* Skip this overlay if it doesn't apply to IT->w. */
5549 window = Foverlay_get (overlay, Qwindow);
5550 if (WINDOWP (window) && XWINDOW (window) != it->w)
5551 continue;
5553 /* If the text ``under'' the overlay is invisible, both before-
5554 and after-strings from this overlay are visible; start and
5555 end position are indistinguishable. */
5556 invisible = Foverlay_get (overlay, Qinvisible);
5557 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5559 /* If overlay has a non-empty before-string, record it. */
5560 if ((start == charpos || (end == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 0);
5565 /* If overlay has a non-empty after-string, record it. */
5566 if ((end == charpos || (start == charpos && invis_p))
5567 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5568 && SCHARS (str))
5569 RECORD_OVERLAY_STRING (overlay, str, 1);
5572 /* Process overlays after the overlay center. */
5573 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5575 XSETMISC (overlay, ov);
5576 eassert (OVERLAYP (overlay));
5577 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5578 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5580 if (start > charpos)
5581 break;
5583 /* Skip this overlay if it doesn't start or end at IT's current
5584 position. */
5585 if (end != charpos && start != charpos)
5586 continue;
5588 /* Skip this overlay if it doesn't apply to IT->w. */
5589 window = Foverlay_get (overlay, Qwindow);
5590 if (WINDOWP (window) && XWINDOW (window) != it->w)
5591 continue;
5593 /* If the text ``under'' the overlay is invisible, it has a zero
5594 dimension, and both before- and after-strings apply. */
5595 invisible = Foverlay_get (overlay, Qinvisible);
5596 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5598 /* If overlay has a non-empty before-string, record it. */
5599 if ((start == charpos || (end == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 0);
5604 /* If overlay has a non-empty after-string, record it. */
5605 if ((end == charpos || (start == charpos && invis_p))
5606 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5607 && SCHARS (str))
5608 RECORD_OVERLAY_STRING (overlay, str, 1);
5611 #undef RECORD_OVERLAY_STRING
5613 /* Sort entries. */
5614 if (n > 1)
5615 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5617 /* Record number of overlay strings, and where we computed it. */
5618 it->n_overlay_strings = n;
5619 it->overlay_strings_charpos = charpos;
5621 /* IT->current.overlay_string_index is the number of overlay strings
5622 that have already been consumed by IT. Copy some of the
5623 remaining overlay strings to IT->overlay_strings. */
5624 i = 0;
5625 j = it->current.overlay_string_index;
5626 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5628 it->overlay_strings[i] = entries[j].string;
5629 it->string_overlays[i++] = entries[j++].overlay;
5632 CHECK_IT (it);
5633 SAFE_FREE ();
5637 /* Get the first chunk of overlay strings at IT's current buffer
5638 position, or at CHARPOS if that is > 0. Value is non-zero if at
5639 least one overlay string was found. */
5641 static int
5642 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5644 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5645 process. This fills IT->overlay_strings with strings, and sets
5646 IT->n_overlay_strings to the total number of strings to process.
5647 IT->pos.overlay_string_index has to be set temporarily to zero
5648 because load_overlay_strings needs this; it must be set to -1
5649 when no overlay strings are found because a zero value would
5650 indicate a position in the first overlay string. */
5651 it->current.overlay_string_index = 0;
5652 load_overlay_strings (it, charpos);
5654 /* If we found overlay strings, set up IT to deliver display
5655 elements from the first one. Otherwise set up IT to deliver
5656 from current_buffer. */
5657 if (it->n_overlay_strings)
5659 /* Make sure we know settings in current_buffer, so that we can
5660 restore meaningful values when we're done with the overlay
5661 strings. */
5662 if (compute_stop_p)
5663 compute_stop_pos (it);
5664 eassert (it->face_id >= 0);
5666 /* Save IT's settings. They are restored after all overlay
5667 strings have been processed. */
5668 eassert (!compute_stop_p || it->sp == 0);
5670 /* When called from handle_stop, there might be an empty display
5671 string loaded. In that case, don't bother saving it. But
5672 don't use this optimization with the bidi iterator, since we
5673 need the corresponding pop_it call to resync the bidi
5674 iterator's position with IT's position, after we are done
5675 with the overlay strings. (The corresponding call to pop_it
5676 in case of an empty display string is in
5677 next_overlay_string.) */
5678 if (!(!it->bidi_p
5679 && STRINGP (it->string) && !SCHARS (it->string)))
5680 push_it (it, NULL);
5682 /* Set up IT to deliver display elements from the first overlay
5683 string. */
5684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5685 it->string = it->overlay_strings[0];
5686 it->from_overlay = Qnil;
5687 it->stop_charpos = 0;
5688 eassert (STRINGP (it->string));
5689 it->end_charpos = SCHARS (it->string);
5690 it->prev_stop = 0;
5691 it->base_level_stop = 0;
5692 it->multibyte_p = STRING_MULTIBYTE (it->string);
5693 it->method = GET_FROM_STRING;
5694 it->from_disp_prop_p = 0;
5696 /* Force paragraph direction to be that of the parent
5697 buffer. */
5698 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5699 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5700 else
5701 it->paragraph_embedding = L2R;
5703 /* Set up the bidi iterator for this overlay string. */
5704 if (it->bidi_p)
5706 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5708 it->bidi_it.string.lstring = it->string;
5709 it->bidi_it.string.s = NULL;
5710 it->bidi_it.string.schars = SCHARS (it->string);
5711 it->bidi_it.string.bufpos = pos;
5712 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5713 it->bidi_it.string.unibyte = !it->multibyte_p;
5714 it->bidi_it.w = it->w;
5715 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5717 return 1;
5720 it->current.overlay_string_index = -1;
5721 return 0;
5724 static int
5725 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5727 it->string = Qnil;
5728 it->method = GET_FROM_BUFFER;
5730 (void) get_overlay_strings_1 (it, charpos, 1);
5732 CHECK_IT (it);
5734 /* Value is non-zero if we found at least one overlay string. */
5735 return STRINGP (it->string);
5740 /***********************************************************************
5741 Saving and restoring state
5742 ***********************************************************************/
5744 /* Save current settings of IT on IT->stack. Called, for example,
5745 before setting up IT for an overlay string, to be able to restore
5746 IT's settings to what they were after the overlay string has been
5747 processed. If POSITION is non-NULL, it is the position to save on
5748 the stack instead of IT->position. */
5750 static void
5751 push_it (struct it *it, struct text_pos *position)
5753 struct iterator_stack_entry *p;
5755 eassert (it->sp < IT_STACK_SIZE);
5756 p = it->stack + it->sp;
5758 p->stop_charpos = it->stop_charpos;
5759 p->prev_stop = it->prev_stop;
5760 p->base_level_stop = it->base_level_stop;
5761 p->cmp_it = it->cmp_it;
5762 eassert (it->face_id >= 0);
5763 p->face_id = it->face_id;
5764 p->string = it->string;
5765 p->method = it->method;
5766 p->from_overlay = it->from_overlay;
5767 switch (p->method)
5769 case GET_FROM_IMAGE:
5770 p->u.image.object = it->object;
5771 p->u.image.image_id = it->image_id;
5772 p->u.image.slice = it->slice;
5773 break;
5774 case GET_FROM_STRETCH:
5775 p->u.stretch.object = it->object;
5776 break;
5778 p->position = position ? *position : it->position;
5779 p->current = it->current;
5780 p->end_charpos = it->end_charpos;
5781 p->string_nchars = it->string_nchars;
5782 p->area = it->area;
5783 p->multibyte_p = it->multibyte_p;
5784 p->avoid_cursor_p = it->avoid_cursor_p;
5785 p->space_width = it->space_width;
5786 p->font_height = it->font_height;
5787 p->voffset = it->voffset;
5788 p->string_from_display_prop_p = it->string_from_display_prop_p;
5789 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5790 p->display_ellipsis_p = 0;
5791 p->line_wrap = it->line_wrap;
5792 p->bidi_p = it->bidi_p;
5793 p->paragraph_embedding = it->paragraph_embedding;
5794 p->from_disp_prop_p = it->from_disp_prop_p;
5795 ++it->sp;
5797 /* Save the state of the bidi iterator as well. */
5798 if (it->bidi_p)
5799 bidi_push_it (&it->bidi_it);
5802 static void
5803 iterate_out_of_display_property (struct it *it)
5805 int buffer_p = !STRINGP (it->string);
5806 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5807 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5809 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5811 /* Maybe initialize paragraph direction. If we are at the beginning
5812 of a new paragraph, next_element_from_buffer may not have a
5813 chance to do that. */
5814 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5815 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5816 /* prev_stop can be zero, so check against BEGV as well. */
5817 while (it->bidi_it.charpos >= bob
5818 && it->prev_stop <= it->bidi_it.charpos
5819 && it->bidi_it.charpos < CHARPOS (it->position)
5820 && it->bidi_it.charpos < eob)
5821 bidi_move_to_visually_next (&it->bidi_it);
5822 /* Record the stop_pos we just crossed, for when we cross it
5823 back, maybe. */
5824 if (it->bidi_it.charpos > CHARPOS (it->position))
5825 it->prev_stop = CHARPOS (it->position);
5826 /* If we ended up not where pop_it put us, resync IT's
5827 positional members with the bidi iterator. */
5828 if (it->bidi_it.charpos != CHARPOS (it->position))
5829 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5830 if (buffer_p)
5831 it->current.pos = it->position;
5832 else
5833 it->current.string_pos = it->position;
5836 /* Restore IT's settings from IT->stack. Called, for example, when no
5837 more overlay strings must be processed, and we return to delivering
5838 display elements from a buffer, or when the end of a string from a
5839 `display' property is reached and we return to delivering display
5840 elements from an overlay string, or from a buffer. */
5842 static void
5843 pop_it (struct it *it)
5845 struct iterator_stack_entry *p;
5846 int from_display_prop = it->from_disp_prop_p;
5848 eassert (it->sp > 0);
5849 --it->sp;
5850 p = it->stack + it->sp;
5851 it->stop_charpos = p->stop_charpos;
5852 it->prev_stop = p->prev_stop;
5853 it->base_level_stop = p->base_level_stop;
5854 it->cmp_it = p->cmp_it;
5855 it->face_id = p->face_id;
5856 it->current = p->current;
5857 it->position = p->position;
5858 it->string = p->string;
5859 it->from_overlay = p->from_overlay;
5860 if (NILP (it->string))
5861 SET_TEXT_POS (it->current.string_pos, -1, -1);
5862 it->method = p->method;
5863 switch (it->method)
5865 case GET_FROM_IMAGE:
5866 it->image_id = p->u.image.image_id;
5867 it->object = p->u.image.object;
5868 it->slice = p->u.image.slice;
5869 break;
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5934 /* Set IT's current position to the previous line start. */
5936 static void
5937 back_to_previous_line_start (struct it *it)
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5946 /* Move IT to the next line start.
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6018 eassert (!STRINGP (it->string));
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6030 if (!it->bidi_p)
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6035 else
6037 struct bidi_it bprev;
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6056 *skipped_p = newline_found_p = 1;
6058 else
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6071 it->selective = old_selective;
6072 return newline_found_p;
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6084 while (IT_CHARPOS (*it) > BEGV)
6086 back_to_previous_line_start (it);
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6098 /* Check the newline before point for invisibility. */
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6117 SAVE_IT (it2, *it, it2data);
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6155 it->continuation_lines_width = 0;
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6209 if (STRINGP (it->string))
6211 if (IT_STRING_CHARPOS (*it) > 0)
6213 if (!it->bidi_p)
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6218 else
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6229 else if (IT_CHARPOS (*it) > BEGV)
6231 if (!it->bidi_p)
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6236 else
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 reseat (it, it->current.pos, 0);
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6250 CHECK_IT (it);
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269 reseat_1 (it, pos, 0);
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6277 if (it->bidi_p)
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6295 else
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6303 CHECK_IT (it);
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6347 it->bidi_it.w = it->w;
6350 if (set_stop_p)
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6383 /* No text property checks performed by default, but see below. */
6384 it->stop_charpos = -1;
6386 /* Set iterator position and end position. */
6387 memset (&it->current, 0, sizeof it->current);
6388 it->current.overlay_string_index = -1;
6389 it->current.dpvec_index = -1;
6390 eassert (charpos >= 0);
6392 /* If STRING is specified, use its multibyteness, otherwise use the
6393 setting of MULTIBYTE, if specified. */
6394 if (multibyte >= 0)
6395 it->multibyte_p = multibyte > 0;
6397 /* Bidirectional reordering of strings is controlled by the default
6398 value of bidi-display-reordering. Don't try to reorder while
6399 loading loadup.el, as the necessary character property tables are
6400 not yet available. */
6401 it->bidi_p =
6402 NILP (Vpurify_flag)
6403 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6405 if (s == NULL)
6407 eassert (STRINGP (string));
6408 it->string = string;
6409 it->s = NULL;
6410 it->end_charpos = it->string_nchars = SCHARS (string);
6411 it->method = GET_FROM_STRING;
6412 it->current.string_pos = string_pos (charpos, string);
6414 if (it->bidi_p)
6416 it->bidi_it.string.lstring = string;
6417 it->bidi_it.string.s = NULL;
6418 it->bidi_it.string.schars = it->end_charpos;
6419 it->bidi_it.string.bufpos = 0;
6420 it->bidi_it.string.from_disp_str = 0;
6421 it->bidi_it.string.unibyte = !it->multibyte_p;
6422 it->bidi_it.w = it->w;
6423 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6424 FRAME_WINDOW_P (it->f), &it->bidi_it);
6427 else
6429 it->s = (const unsigned char *) s;
6430 it->string = Qnil;
6432 /* Note that we use IT->current.pos, not it->current.string_pos,
6433 for displaying C strings. */
6434 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6435 if (it->multibyte_p)
6437 it->current.pos = c_string_pos (charpos, s, 1);
6438 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6440 else
6442 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6443 it->end_charpos = it->string_nchars = strlen (s);
6446 if (it->bidi_p)
6448 it->bidi_it.string.lstring = Qnil;
6449 it->bidi_it.string.s = (const unsigned char *) s;
6450 it->bidi_it.string.schars = it->end_charpos;
6451 it->bidi_it.string.bufpos = 0;
6452 it->bidi_it.string.from_disp_str = 0;
6453 it->bidi_it.string.unibyte = !it->multibyte_p;
6454 it->bidi_it.w = it->w;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6458 it->method = GET_FROM_C_STRING;
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6495 if (s == NULL && it->multibyte_p)
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6503 CHECK_IT (it);
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6524 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6527 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6528 (possibly with the following characters). */
6530 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6531 ((IT)->cmp_it.id >= 0 \
6532 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6533 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6534 END_CHARPOS, (IT)->w, \
6535 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6536 (IT)->string)))
6539 /* Lookup the char-table Vglyphless_char_display for character C (-1
6540 if we want information for no-font case), and return the display
6541 method symbol. By side-effect, update it->what and
6542 it->glyphless_method. This function is called from
6543 get_next_display_element for each character element, and from
6544 x_produce_glyphs when no suitable font was found. */
6546 Lisp_Object
6547 lookup_glyphless_char_display (int c, struct it *it)
6549 Lisp_Object glyphless_method = Qnil;
6551 if (CHAR_TABLE_P (Vglyphless_char_display)
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 if (c >= 0)
6556 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6557 if (CONSP (glyphless_method))
6558 glyphless_method = FRAME_WINDOW_P (it->f)
6559 ? XCAR (glyphless_method)
6560 : XCDR (glyphless_method);
6562 else
6563 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6566 retry:
6567 if (NILP (glyphless_method))
6569 if (c >= 0)
6570 /* The default is to display the character by a proper font. */
6571 return Qnil;
6572 /* The default for the no-font case is to display an empty box. */
6573 glyphless_method = Qempty_box;
6575 if (EQ (glyphless_method, Qzero_width))
6577 if (c >= 0)
6578 return glyphless_method;
6579 /* This method can't be used for the no-font case. */
6580 glyphless_method = Qempty_box;
6582 if (EQ (glyphless_method, Qthin_space))
6583 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6584 else if (EQ (glyphless_method, Qempty_box))
6585 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6586 else if (EQ (glyphless_method, Qhex_code))
6587 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6588 else if (STRINGP (glyphless_method))
6589 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6590 else
6592 /* Invalid value. We use the default method. */
6593 glyphless_method = Qnil;
6594 goto retry;
6596 it->what = IT_GLYPHLESS;
6597 return glyphless_method;
6600 /* Merge escape glyph face and cache the result. */
6602 static struct frame *last_escape_glyph_frame = NULL;
6603 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6604 static int last_escape_glyph_merged_face_id = 0;
6606 static int
6607 merge_escape_glyph_face (struct it *it)
6609 int face_id;
6611 if (it->f == last_escape_glyph_frame
6612 && it->face_id == last_escape_glyph_face_id)
6613 face_id = last_escape_glyph_merged_face_id;
6614 else
6616 /* Merge the `escape-glyph' face into the current face. */
6617 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6618 last_escape_glyph_frame = it->f;
6619 last_escape_glyph_face_id = it->face_id;
6620 last_escape_glyph_merged_face_id = face_id;
6622 return face_id;
6625 /* Likewise for glyphless glyph face. */
6627 static struct frame *last_glyphless_glyph_frame = NULL;
6628 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6629 static int last_glyphless_glyph_merged_face_id = 0;
6632 merge_glyphless_glyph_face (struct it *it)
6634 int face_id;
6636 if (it->f == last_glyphless_glyph_frame
6637 && it->face_id == last_glyphless_glyph_face_id)
6638 face_id = last_glyphless_glyph_merged_face_id;
6639 else
6641 /* Merge the `glyphless-char' face into the current face. */
6642 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6643 last_glyphless_glyph_frame = it->f;
6644 last_glyphless_glyph_face_id = it->face_id;
6645 last_glyphless_glyph_merged_face_id = face_id;
6647 return face_id;
6650 /* Load IT's display element fields with information about the next
6651 display element from the current position of IT. Value is zero if
6652 end of buffer (or C string) is reached. */
6654 static int
6655 get_next_display_element (struct it *it)
6657 /* Non-zero means that we found a display element. Zero means that
6658 we hit the end of what we iterate over. Performance note: the
6659 function pointer `method' used here turns out to be faster than
6660 using a sequence of if-statements. */
6661 int success_p;
6663 get_next:
6664 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6666 if (it->what == IT_CHARACTER)
6668 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6669 and only if (a) the resolved directionality of that character
6670 is R..." */
6671 /* FIXME: Do we need an exception for characters from display
6672 tables? */
6673 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6674 it->c = bidi_mirror_char (it->c);
6675 /* Map via display table or translate control characters.
6676 IT->c, IT->len etc. have been set to the next character by
6677 the function call above. If we have a display table, and it
6678 contains an entry for IT->c, translate it. Don't do this if
6679 IT->c itself comes from a display table, otherwise we could
6680 end up in an infinite recursion. (An alternative could be to
6681 count the recursion depth of this function and signal an
6682 error when a certain maximum depth is reached.) Is it worth
6683 it? */
6684 if (success_p && it->dpvec == NULL)
6686 Lisp_Object dv;
6687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6688 int nonascii_space_p = 0;
6689 int nonascii_hyphen_p = 0;
6690 int c = it->c; /* This is the character to display. */
6692 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6694 eassert (SINGLE_BYTE_CHAR_P (c));
6695 if (unibyte_display_via_language_environment)
6697 c = DECODE_CHAR (unibyte, c);
6698 if (c < 0)
6699 c = BYTE8_TO_CHAR (it->c);
6701 else
6702 c = BYTE8_TO_CHAR (it->c);
6705 if (it->dp
6706 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6707 VECTORP (dv)))
6709 struct Lisp_Vector *v = XVECTOR (dv);
6711 /* Return the first character from the display table
6712 entry, if not empty. If empty, don't display the
6713 current character. */
6714 if (v->header.size)
6716 it->dpvec_char_len = it->len;
6717 it->dpvec = v->contents;
6718 it->dpend = v->contents + v->header.size;
6719 it->current.dpvec_index = 0;
6720 it->dpvec_face_id = -1;
6721 it->saved_face_id = it->face_id;
6722 it->method = GET_FROM_DISPLAY_VECTOR;
6723 it->ellipsis_p = 0;
6725 else
6727 set_iterator_to_next (it, 0);
6729 goto get_next;
6732 if (! NILP (lookup_glyphless_char_display (c, it)))
6734 if (it->what == IT_GLYPHLESS)
6735 goto done;
6736 /* Don't display this character. */
6737 set_iterator_to_next (it, 0);
6738 goto get_next;
6741 /* If `nobreak-char-display' is non-nil, we display
6742 non-ASCII spaces and hyphens specially. */
6743 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6745 if (c == 0xA0)
6746 nonascii_space_p = 1;
6747 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6748 nonascii_hyphen_p = 1;
6751 /* Translate control characters into `\003' or `^C' form.
6752 Control characters coming from a display table entry are
6753 currently not translated because we use IT->dpvec to hold
6754 the translation. This could easily be changed but I
6755 don't believe that it is worth doing.
6757 The characters handled by `nobreak-char-display' must be
6758 translated too.
6760 Non-printable characters and raw-byte characters are also
6761 translated to octal form. */
6762 if (((c < ' ' || c == 127) /* ASCII control chars */
6763 ? (it->area != TEXT_AREA
6764 /* In mode line, treat \n, \t like other crl chars. */
6765 || (c != '\t'
6766 && it->glyph_row
6767 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6768 || (c != '\n' && c != '\t'))
6769 : (nonascii_space_p
6770 || nonascii_hyphen_p
6771 || CHAR_BYTE8_P (c)
6772 || ! CHAR_PRINTABLE_P (c))))
6774 /* C is a control character, non-ASCII space/hyphen,
6775 raw-byte, or a non-printable character which must be
6776 displayed either as '\003' or as `^C' where the '\\'
6777 and '^' can be defined in the display table. Fill
6778 IT->ctl_chars with glyphs for what we have to
6779 display. Then, set IT->dpvec to these glyphs. */
6780 Lisp_Object gc;
6781 int ctl_len;
6782 int face_id;
6783 int lface_id = 0;
6784 int escape_glyph;
6786 /* Handle control characters with ^. */
6788 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6790 int g;
6792 g = '^'; /* default glyph for Control */
6793 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6794 if (it->dp
6795 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6797 g = GLYPH_CODE_CHAR (gc);
6798 lface_id = GLYPH_CODE_FACE (gc);
6801 face_id = (lface_id
6802 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6803 : merge_escape_glyph_face (it));
6805 XSETINT (it->ctl_chars[0], g);
6806 XSETINT (it->ctl_chars[1], c ^ 0100);
6807 ctl_len = 2;
6808 goto display_control;
6811 /* Handle non-ascii space in the mode where it only gets
6812 highlighting. */
6814 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6816 /* Merge `nobreak-space' into the current face. */
6817 face_id = merge_faces (it->f, Qnobreak_space, 0,
6818 it->face_id);
6819 XSETINT (it->ctl_chars[0], ' ');
6820 ctl_len = 1;
6821 goto display_control;
6824 /* Handle sequences that start with the "escape glyph". */
6826 /* the default escape glyph is \. */
6827 escape_glyph = '\\';
6829 if (it->dp
6830 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6832 escape_glyph = GLYPH_CODE_CHAR (gc);
6833 lface_id = GLYPH_CODE_FACE (gc);
6836 face_id = (lface_id
6837 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6838 : merge_escape_glyph_face (it));
6840 /* Draw non-ASCII hyphen with just highlighting: */
6842 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6844 XSETINT (it->ctl_chars[0], '-');
6845 ctl_len = 1;
6846 goto display_control;
6849 /* Draw non-ASCII space/hyphen with escape glyph: */
6851 if (nonascii_space_p || nonascii_hyphen_p)
6853 XSETINT (it->ctl_chars[0], escape_glyph);
6854 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6855 ctl_len = 2;
6856 goto display_control;
6860 char str[10];
6861 int len, i;
6863 if (CHAR_BYTE8_P (c))
6864 /* Display \200 instead of \17777600. */
6865 c = CHAR_TO_BYTE8 (c);
6866 len = sprintf (str, "%03o", c);
6868 XSETINT (it->ctl_chars[0], escape_glyph);
6869 for (i = 0; i < len; i++)
6870 XSETINT (it->ctl_chars[i + 1], str[i]);
6871 ctl_len = len + 1;
6874 display_control:
6875 /* Set up IT->dpvec and return first character from it. */
6876 it->dpvec_char_len = it->len;
6877 it->dpvec = it->ctl_chars;
6878 it->dpend = it->dpvec + ctl_len;
6879 it->current.dpvec_index = 0;
6880 it->dpvec_face_id = face_id;
6881 it->saved_face_id = it->face_id;
6882 it->method = GET_FROM_DISPLAY_VECTOR;
6883 it->ellipsis_p = 0;
6884 goto get_next;
6886 it->char_to_display = c;
6888 else if (success_p)
6890 it->char_to_display = it->c;
6894 #ifdef HAVE_WINDOW_SYSTEM
6895 /* Adjust face id for a multibyte character. There are no multibyte
6896 character in unibyte text. */
6897 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6898 && it->multibyte_p
6899 && success_p
6900 && FRAME_WINDOW_P (it->f))
6902 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6904 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6906 /* Automatic composition with glyph-string. */
6907 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6909 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6911 else
6913 ptrdiff_t pos = (it->s ? -1
6914 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6915 : IT_CHARPOS (*it));
6916 int c;
6918 if (it->what == IT_CHARACTER)
6919 c = it->char_to_display;
6920 else
6922 struct composition *cmp = composition_table[it->cmp_it.id];
6923 int i;
6925 c = ' ';
6926 for (i = 0; i < cmp->glyph_len; i++)
6927 /* TAB in a composition means display glyphs with
6928 padding space on the left or right. */
6929 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6930 break;
6932 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6935 #endif /* HAVE_WINDOW_SYSTEM */
6937 done:
6938 /* Is this character the last one of a run of characters with
6939 box? If yes, set IT->end_of_box_run_p to 1. */
6940 if (it->face_box_p
6941 && it->s == NULL)
6943 if (it->method == GET_FROM_STRING && it->sp)
6945 int face_id = underlying_face_id (it);
6946 struct face *face = FACE_FROM_ID (it->f, face_id);
6948 if (face)
6950 if (face->box == FACE_NO_BOX)
6952 /* If the box comes from face properties in a
6953 display string, check faces in that string. */
6954 int string_face_id = face_after_it_pos (it);
6955 it->end_of_box_run_p
6956 = (FACE_FROM_ID (it->f, string_face_id)->box
6957 == FACE_NO_BOX);
6959 /* Otherwise, the box comes from the underlying face.
6960 If this is the last string character displayed, check
6961 the next buffer location. */
6962 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6963 && (it->current.overlay_string_index
6964 == it->n_overlay_strings - 1))
6966 ptrdiff_t ignore;
6967 int next_face_id;
6968 struct text_pos pos = it->current.pos;
6969 INC_TEXT_POS (pos, it->multibyte_p);
6971 next_face_id = face_at_buffer_position
6972 (it->w, CHARPOS (pos), &ignore,
6973 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6974 -1);
6975 it->end_of_box_run_p
6976 = (FACE_FROM_ID (it->f, next_face_id)->box
6977 == FACE_NO_BOX);
6981 /* next_element_from_display_vector sets this flag according to
6982 faces of the display vector glyphs, see there. */
6983 else if (it->method != GET_FROM_DISPLAY_VECTOR)
6985 int face_id = face_after_it_pos (it);
6986 it->end_of_box_run_p
6987 = (face_id != it->face_id
6988 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6991 /* If we reached the end of the object we've been iterating (e.g., a
6992 display string or an overlay string), and there's something on
6993 IT->stack, proceed with what's on the stack. It doesn't make
6994 sense to return zero if there's unprocessed stuff on the stack,
6995 because otherwise that stuff will never be displayed. */
6996 if (!success_p && it->sp > 0)
6998 set_iterator_to_next (it, 0);
6999 success_p = get_next_display_element (it);
7002 /* Value is 0 if end of buffer or string reached. */
7003 return success_p;
7007 /* Move IT to the next display element.
7009 RESEAT_P non-zero means if called on a newline in buffer text,
7010 skip to the next visible line start.
7012 Functions get_next_display_element and set_iterator_to_next are
7013 separate because I find this arrangement easier to handle than a
7014 get_next_display_element function that also increments IT's
7015 position. The way it is we can first look at an iterator's current
7016 display element, decide whether it fits on a line, and if it does,
7017 increment the iterator position. The other way around we probably
7018 would either need a flag indicating whether the iterator has to be
7019 incremented the next time, or we would have to implement a
7020 decrement position function which would not be easy to write. */
7022 void
7023 set_iterator_to_next (struct it *it, int reseat_p)
7025 /* Reset flags indicating start and end of a sequence of characters
7026 with box. Reset them at the start of this function because
7027 moving the iterator to a new position might set them. */
7028 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7030 switch (it->method)
7032 case GET_FROM_BUFFER:
7033 /* The current display element of IT is a character from
7034 current_buffer. Advance in the buffer, and maybe skip over
7035 invisible lines that are so because of selective display. */
7036 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7037 reseat_at_next_visible_line_start (it, 0);
7038 else if (it->cmp_it.id >= 0)
7040 /* We are currently getting glyphs from a composition. */
7041 int i;
7043 if (! it->bidi_p)
7045 IT_CHARPOS (*it) += it->cmp_it.nchars;
7046 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7047 if (it->cmp_it.to < it->cmp_it.nglyphs)
7049 it->cmp_it.from = it->cmp_it.to;
7051 else
7053 it->cmp_it.id = -1;
7054 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7055 IT_BYTEPOS (*it),
7056 it->end_charpos, Qnil);
7059 else if (! it->cmp_it.reversed_p)
7061 /* Composition created while scanning forward. */
7062 /* Update IT's char/byte positions to point to the first
7063 character of the next grapheme cluster, or to the
7064 character visually after the current composition. */
7065 for (i = 0; i < it->cmp_it.nchars; i++)
7066 bidi_move_to_visually_next (&it->bidi_it);
7067 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7068 IT_CHARPOS (*it) = it->bidi_it.charpos;
7070 if (it->cmp_it.to < it->cmp_it.nglyphs)
7072 /* Proceed to the next grapheme cluster. */
7073 it->cmp_it.from = it->cmp_it.to;
7075 else
7077 /* No more grapheme clusters in this composition.
7078 Find the next stop position. */
7079 ptrdiff_t stop = it->end_charpos;
7080 if (it->bidi_it.scan_dir < 0)
7081 /* Now we are scanning backward and don't know
7082 where to stop. */
7083 stop = -1;
7084 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7085 IT_BYTEPOS (*it), stop, Qnil);
7088 else
7090 /* Composition created while scanning backward. */
7091 /* Update IT's char/byte positions to point to the last
7092 character of the previous grapheme cluster, or the
7093 character visually after the current composition. */
7094 for (i = 0; i < it->cmp_it.nchars; i++)
7095 bidi_move_to_visually_next (&it->bidi_it);
7096 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7097 IT_CHARPOS (*it) = it->bidi_it.charpos;
7098 if (it->cmp_it.from > 0)
7100 /* Proceed to the previous grapheme cluster. */
7101 it->cmp_it.to = it->cmp_it.from;
7103 else
7105 /* No more grapheme clusters in this composition.
7106 Find the next stop position. */
7107 ptrdiff_t stop = it->end_charpos;
7108 if (it->bidi_it.scan_dir < 0)
7109 /* Now we are scanning backward and don't know
7110 where to stop. */
7111 stop = -1;
7112 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7113 IT_BYTEPOS (*it), stop, Qnil);
7117 else
7119 eassert (it->len != 0);
7121 if (!it->bidi_p)
7123 IT_BYTEPOS (*it) += it->len;
7124 IT_CHARPOS (*it) += 1;
7126 else
7128 int prev_scan_dir = it->bidi_it.scan_dir;
7129 /* If this is a new paragraph, determine its base
7130 direction (a.k.a. its base embedding level). */
7131 if (it->bidi_it.new_paragraph)
7132 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7133 bidi_move_to_visually_next (&it->bidi_it);
7134 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7135 IT_CHARPOS (*it) = it->bidi_it.charpos;
7136 if (prev_scan_dir != it->bidi_it.scan_dir)
7138 /* As the scan direction was changed, we must
7139 re-compute the stop position for composition. */
7140 ptrdiff_t stop = it->end_charpos;
7141 if (it->bidi_it.scan_dir < 0)
7142 stop = -1;
7143 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7144 IT_BYTEPOS (*it), stop, Qnil);
7147 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7149 break;
7151 case GET_FROM_C_STRING:
7152 /* Current display element of IT is from a C string. */
7153 if (!it->bidi_p
7154 /* If the string position is beyond string's end, it means
7155 next_element_from_c_string is padding the string with
7156 blanks, in which case we bypass the bidi iterator,
7157 because it cannot deal with such virtual characters. */
7158 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7160 IT_BYTEPOS (*it) += it->len;
7161 IT_CHARPOS (*it) += 1;
7163 else
7165 bidi_move_to_visually_next (&it->bidi_it);
7166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7167 IT_CHARPOS (*it) = it->bidi_it.charpos;
7169 break;
7171 case GET_FROM_DISPLAY_VECTOR:
7172 /* Current display element of IT is from a display table entry.
7173 Advance in the display table definition. Reset it to null if
7174 end reached, and continue with characters from buffers/
7175 strings. */
7176 ++it->current.dpvec_index;
7178 /* Restore face of the iterator to what they were before the
7179 display vector entry (these entries may contain faces). */
7180 it->face_id = it->saved_face_id;
7182 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7184 int recheck_faces = it->ellipsis_p;
7186 if (it->s)
7187 it->method = GET_FROM_C_STRING;
7188 else if (STRINGP (it->string))
7189 it->method = GET_FROM_STRING;
7190 else
7192 it->method = GET_FROM_BUFFER;
7193 it->object = it->w->contents;
7196 it->dpvec = NULL;
7197 it->current.dpvec_index = -1;
7199 /* Skip over characters which were displayed via IT->dpvec. */
7200 if (it->dpvec_char_len < 0)
7201 reseat_at_next_visible_line_start (it, 1);
7202 else if (it->dpvec_char_len > 0)
7204 if (it->method == GET_FROM_STRING
7205 && it->current.overlay_string_index >= 0
7206 && it->n_overlay_strings > 0)
7207 it->ignore_overlay_strings_at_pos_p = 1;
7208 it->len = it->dpvec_char_len;
7209 set_iterator_to_next (it, reseat_p);
7212 /* Maybe recheck faces after display vector */
7213 if (recheck_faces)
7214 it->stop_charpos = IT_CHARPOS (*it);
7216 break;
7218 case GET_FROM_STRING:
7219 /* Current display element is a character from a Lisp string. */
7220 eassert (it->s == NULL && STRINGP (it->string));
7221 /* Don't advance past string end. These conditions are true
7222 when set_iterator_to_next is called at the end of
7223 get_next_display_element, in which case the Lisp string is
7224 already exhausted, and all we want is pop the iterator
7225 stack. */
7226 if (it->current.overlay_string_index >= 0)
7228 /* This is an overlay string, so there's no padding with
7229 spaces, and the number of characters in the string is
7230 where the string ends. */
7231 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7232 goto consider_string_end;
7234 else
7236 /* Not an overlay string. There could be padding, so test
7237 against it->end_charpos . */
7238 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7239 goto consider_string_end;
7241 if (it->cmp_it.id >= 0)
7243 int i;
7245 if (! it->bidi_p)
7247 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7248 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7249 if (it->cmp_it.to < it->cmp_it.nglyphs)
7250 it->cmp_it.from = it->cmp_it.to;
7251 else
7253 it->cmp_it.id = -1;
7254 composition_compute_stop_pos (&it->cmp_it,
7255 IT_STRING_CHARPOS (*it),
7256 IT_STRING_BYTEPOS (*it),
7257 it->end_charpos, it->string);
7260 else if (! it->cmp_it.reversed_p)
7262 for (i = 0; i < it->cmp_it.nchars; i++)
7263 bidi_move_to_visually_next (&it->bidi_it);
7264 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7265 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7267 if (it->cmp_it.to < it->cmp_it.nglyphs)
7268 it->cmp_it.from = it->cmp_it.to;
7269 else
7271 ptrdiff_t stop = it->end_charpos;
7272 if (it->bidi_it.scan_dir < 0)
7273 stop = -1;
7274 composition_compute_stop_pos (&it->cmp_it,
7275 IT_STRING_CHARPOS (*it),
7276 IT_STRING_BYTEPOS (*it), stop,
7277 it->string);
7280 else
7282 for (i = 0; i < it->cmp_it.nchars; i++)
7283 bidi_move_to_visually_next (&it->bidi_it);
7284 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7285 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7286 if (it->cmp_it.from > 0)
7287 it->cmp_it.to = it->cmp_it.from;
7288 else
7290 ptrdiff_t stop = it->end_charpos;
7291 if (it->bidi_it.scan_dir < 0)
7292 stop = -1;
7293 composition_compute_stop_pos (&it->cmp_it,
7294 IT_STRING_CHARPOS (*it),
7295 IT_STRING_BYTEPOS (*it), stop,
7296 it->string);
7300 else
7302 if (!it->bidi_p
7303 /* If the string position is beyond string's end, it
7304 means next_element_from_string is padding the string
7305 with blanks, in which case we bypass the bidi
7306 iterator, because it cannot deal with such virtual
7307 characters. */
7308 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7310 IT_STRING_BYTEPOS (*it) += it->len;
7311 IT_STRING_CHARPOS (*it) += 1;
7313 else
7315 int prev_scan_dir = it->bidi_it.scan_dir;
7317 bidi_move_to_visually_next (&it->bidi_it);
7318 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7319 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7320 if (prev_scan_dir != it->bidi_it.scan_dir)
7322 ptrdiff_t stop = it->end_charpos;
7324 if (it->bidi_it.scan_dir < 0)
7325 stop = -1;
7326 composition_compute_stop_pos (&it->cmp_it,
7327 IT_STRING_CHARPOS (*it),
7328 IT_STRING_BYTEPOS (*it), stop,
7329 it->string);
7334 consider_string_end:
7336 if (it->current.overlay_string_index >= 0)
7338 /* IT->string is an overlay string. Advance to the
7339 next, if there is one. */
7340 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7342 it->ellipsis_p = 0;
7343 next_overlay_string (it);
7344 if (it->ellipsis_p)
7345 setup_for_ellipsis (it, 0);
7348 else
7350 /* IT->string is not an overlay string. If we reached
7351 its end, and there is something on IT->stack, proceed
7352 with what is on the stack. This can be either another
7353 string, this time an overlay string, or a buffer. */
7354 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7355 && it->sp > 0)
7357 pop_it (it);
7358 if (it->method == GET_FROM_STRING)
7359 goto consider_string_end;
7362 break;
7364 case GET_FROM_IMAGE:
7365 case GET_FROM_STRETCH:
7366 /* The position etc with which we have to proceed are on
7367 the stack. The position may be at the end of a string,
7368 if the `display' property takes up the whole string. */
7369 eassert (it->sp > 0);
7370 pop_it (it);
7371 if (it->method == GET_FROM_STRING)
7372 goto consider_string_end;
7373 break;
7375 default:
7376 /* There are no other methods defined, so this should be a bug. */
7377 emacs_abort ();
7380 eassert (it->method != GET_FROM_STRING
7381 || (STRINGP (it->string)
7382 && IT_STRING_CHARPOS (*it) >= 0));
7385 /* Load IT's display element fields with information about the next
7386 display element which comes from a display table entry or from the
7387 result of translating a control character to one of the forms `^C'
7388 or `\003'.
7390 IT->dpvec holds the glyphs to return as characters.
7391 IT->saved_face_id holds the face id before the display vector--it
7392 is restored into IT->face_id in set_iterator_to_next. */
7394 static int
7395 next_element_from_display_vector (struct it *it)
7397 Lisp_Object gc;
7398 int prev_face_id = it->face_id;
7399 int next_face_id;
7401 /* Precondition. */
7402 eassert (it->dpvec && it->current.dpvec_index >= 0);
7404 it->face_id = it->saved_face_id;
7406 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7407 That seemed totally bogus - so I changed it... */
7408 gc = it->dpvec[it->current.dpvec_index];
7410 if (GLYPH_CODE_P (gc))
7412 struct face *this_face, *prev_face, *next_face;
7414 it->c = GLYPH_CODE_CHAR (gc);
7415 it->len = CHAR_BYTES (it->c);
7417 /* The entry may contain a face id to use. Such a face id is
7418 the id of a Lisp face, not a realized face. A face id of
7419 zero means no face is specified. */
7420 if (it->dpvec_face_id >= 0)
7421 it->face_id = it->dpvec_face_id;
7422 else
7424 int lface_id = GLYPH_CODE_FACE (gc);
7425 if (lface_id > 0)
7426 it->face_id = merge_faces (it->f, Qt, lface_id,
7427 it->saved_face_id);
7430 /* Glyphs in the display vector could have the box face, so we
7431 need to set the related flags in the iterator, as
7432 appropriate. */
7433 this_face = FACE_FROM_ID (it->f, it->face_id);
7434 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7436 /* Is this character the first character of a box-face run? */
7437 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7438 && (!prev_face
7439 || prev_face->box == FACE_NO_BOX));
7441 /* For the last character of the box-face run, we need to look
7442 either at the next glyph from the display vector, or at the
7443 face we saw before the display vector. */
7444 next_face_id = it->saved_face_id;
7445 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7447 if (it->dpvec_face_id >= 0)
7448 next_face_id = it->dpvec_face_id;
7449 else
7451 int lface_id =
7452 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7454 if (lface_id > 0)
7455 next_face_id = merge_faces (it->f, Qt, lface_id,
7456 it->saved_face_id);
7459 next_face = FACE_FROM_ID (it->f, next_face_id);
7460 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7461 && (!next_face
7462 || next_face->box == FACE_NO_BOX));
7463 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7465 else
7466 /* Display table entry is invalid. Return a space. */
7467 it->c = ' ', it->len = 1;
7469 /* Don't change position and object of the iterator here. They are
7470 still the values of the character that had this display table
7471 entry or was translated, and that's what we want. */
7472 it->what = IT_CHARACTER;
7473 return 1;
7476 /* Get the first element of string/buffer in the visual order, after
7477 being reseated to a new position in a string or a buffer. */
7478 static void
7479 get_visually_first_element (struct it *it)
7481 int string_p = STRINGP (it->string) || it->s;
7482 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7483 ptrdiff_t bob = (string_p ? 0 : BEGV);
7485 if (STRINGP (it->string))
7487 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7488 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7490 else
7492 it->bidi_it.charpos = IT_CHARPOS (*it);
7493 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7496 if (it->bidi_it.charpos == eob)
7498 /* Nothing to do, but reset the FIRST_ELT flag, like
7499 bidi_paragraph_init does, because we are not going to
7500 call it. */
7501 it->bidi_it.first_elt = 0;
7503 else if (it->bidi_it.charpos == bob
7504 || (!string_p
7505 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7506 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7508 /* If we are at the beginning of a line/string, we can produce
7509 the next element right away. */
7510 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7511 bidi_move_to_visually_next (&it->bidi_it);
7513 else
7515 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7517 /* We need to prime the bidi iterator starting at the line's or
7518 string's beginning, before we will be able to produce the
7519 next element. */
7520 if (string_p)
7521 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7522 else
7523 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7524 IT_BYTEPOS (*it), -1,
7525 &it->bidi_it.bytepos);
7526 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7529 /* Now return to buffer/string position where we were asked
7530 to get the next display element, and produce that. */
7531 bidi_move_to_visually_next (&it->bidi_it);
7533 while (it->bidi_it.bytepos != orig_bytepos
7534 && it->bidi_it.charpos < eob);
7537 /* Adjust IT's position information to where we ended up. */
7538 if (STRINGP (it->string))
7540 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7541 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7543 else
7545 IT_CHARPOS (*it) = it->bidi_it.charpos;
7546 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7549 if (STRINGP (it->string) || !it->s)
7551 ptrdiff_t stop, charpos, bytepos;
7553 if (STRINGP (it->string))
7555 eassert (!it->s);
7556 stop = SCHARS (it->string);
7557 if (stop > it->end_charpos)
7558 stop = it->end_charpos;
7559 charpos = IT_STRING_CHARPOS (*it);
7560 bytepos = IT_STRING_BYTEPOS (*it);
7562 else
7564 stop = it->end_charpos;
7565 charpos = IT_CHARPOS (*it);
7566 bytepos = IT_BYTEPOS (*it);
7568 if (it->bidi_it.scan_dir < 0)
7569 stop = -1;
7570 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7571 it->string);
7575 /* Load IT with the next display element from Lisp string IT->string.
7576 IT->current.string_pos is the current position within the string.
7577 If IT->current.overlay_string_index >= 0, the Lisp string is an
7578 overlay string. */
7580 static int
7581 next_element_from_string (struct it *it)
7583 struct text_pos position;
7585 eassert (STRINGP (it->string));
7586 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7587 eassert (IT_STRING_CHARPOS (*it) >= 0);
7588 position = it->current.string_pos;
7590 /* With bidi reordering, the character to display might not be the
7591 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7592 that we were reseat()ed to a new string, whose paragraph
7593 direction is not known. */
7594 if (it->bidi_p && it->bidi_it.first_elt)
7596 get_visually_first_element (it);
7597 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7600 /* Time to check for invisible text? */
7601 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7603 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7605 if (!(!it->bidi_p
7606 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7607 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7609 /* With bidi non-linear iteration, we could find
7610 ourselves far beyond the last computed stop_charpos,
7611 with several other stop positions in between that we
7612 missed. Scan them all now, in buffer's logical
7613 order, until we find and handle the last stop_charpos
7614 that precedes our current position. */
7615 handle_stop_backwards (it, it->stop_charpos);
7616 return GET_NEXT_DISPLAY_ELEMENT (it);
7618 else
7620 if (it->bidi_p)
7622 /* Take note of the stop position we just moved
7623 across, for when we will move back across it. */
7624 it->prev_stop = it->stop_charpos;
7625 /* If we are at base paragraph embedding level, take
7626 note of the last stop position seen at this
7627 level. */
7628 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7629 it->base_level_stop = it->stop_charpos;
7631 handle_stop (it);
7633 /* Since a handler may have changed IT->method, we must
7634 recurse here. */
7635 return GET_NEXT_DISPLAY_ELEMENT (it);
7638 else if (it->bidi_p
7639 /* If we are before prev_stop, we may have overstepped
7640 on our way backwards a stop_pos, and if so, we need
7641 to handle that stop_pos. */
7642 && IT_STRING_CHARPOS (*it) < it->prev_stop
7643 /* We can sometimes back up for reasons that have nothing
7644 to do with bidi reordering. E.g., compositions. The
7645 code below is only needed when we are above the base
7646 embedding level, so test for that explicitly. */
7647 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7649 /* If we lost track of base_level_stop, we have no better
7650 place for handle_stop_backwards to start from than string
7651 beginning. This happens, e.g., when we were reseated to
7652 the previous screenful of text by vertical-motion. */
7653 if (it->base_level_stop <= 0
7654 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7655 it->base_level_stop = 0;
7656 handle_stop_backwards (it, it->base_level_stop);
7657 return GET_NEXT_DISPLAY_ELEMENT (it);
7661 if (it->current.overlay_string_index >= 0)
7663 /* Get the next character from an overlay string. In overlay
7664 strings, there is no field width or padding with spaces to
7665 do. */
7666 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7668 it->what = IT_EOB;
7669 return 0;
7671 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7672 IT_STRING_BYTEPOS (*it),
7673 it->bidi_it.scan_dir < 0
7674 ? -1
7675 : SCHARS (it->string))
7676 && next_element_from_composition (it))
7678 return 1;
7680 else if (STRING_MULTIBYTE (it->string))
7682 const unsigned char *s = (SDATA (it->string)
7683 + IT_STRING_BYTEPOS (*it));
7684 it->c = string_char_and_length (s, &it->len);
7686 else
7688 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7689 it->len = 1;
7692 else
7694 /* Get the next character from a Lisp string that is not an
7695 overlay string. Such strings come from the mode line, for
7696 example. We may have to pad with spaces, or truncate the
7697 string. See also next_element_from_c_string. */
7698 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7700 it->what = IT_EOB;
7701 return 0;
7703 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7705 /* Pad with spaces. */
7706 it->c = ' ', it->len = 1;
7707 CHARPOS (position) = BYTEPOS (position) = -1;
7709 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7710 IT_STRING_BYTEPOS (*it),
7711 it->bidi_it.scan_dir < 0
7712 ? -1
7713 : it->string_nchars)
7714 && next_element_from_composition (it))
7716 return 1;
7718 else if (STRING_MULTIBYTE (it->string))
7720 const unsigned char *s = (SDATA (it->string)
7721 + IT_STRING_BYTEPOS (*it));
7722 it->c = string_char_and_length (s, &it->len);
7724 else
7726 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7727 it->len = 1;
7731 /* Record what we have and where it came from. */
7732 it->what = IT_CHARACTER;
7733 it->object = it->string;
7734 it->position = position;
7735 return 1;
7739 /* Load IT with next display element from C string IT->s.
7740 IT->string_nchars is the maximum number of characters to return
7741 from the string. IT->end_charpos may be greater than
7742 IT->string_nchars when this function is called, in which case we
7743 may have to return padding spaces. Value is zero if end of string
7744 reached, including padding spaces. */
7746 static int
7747 next_element_from_c_string (struct it *it)
7749 int success_p = 1;
7751 eassert (it->s);
7752 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7753 it->what = IT_CHARACTER;
7754 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7755 it->object = Qnil;
7757 /* With bidi reordering, the character to display might not be the
7758 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7759 we were reseated to a new string, whose paragraph direction is
7760 not known. */
7761 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7764 /* IT's position can be greater than IT->string_nchars in case a
7765 field width or precision has been specified when the iterator was
7766 initialized. */
7767 if (IT_CHARPOS (*it) >= it->end_charpos)
7769 /* End of the game. */
7770 it->what = IT_EOB;
7771 success_p = 0;
7773 else if (IT_CHARPOS (*it) >= it->string_nchars)
7775 /* Pad with spaces. */
7776 it->c = ' ', it->len = 1;
7777 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7779 else if (it->multibyte_p)
7780 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7781 else
7782 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7784 return success_p;
7788 /* Set up IT to return characters from an ellipsis, if appropriate.
7789 The definition of the ellipsis glyphs may come from a display table
7790 entry. This function fills IT with the first glyph from the
7791 ellipsis if an ellipsis is to be displayed. */
7793 static int
7794 next_element_from_ellipsis (struct it *it)
7796 if (it->selective_display_ellipsis_p)
7797 setup_for_ellipsis (it, it->len);
7798 else
7800 /* The face at the current position may be different from the
7801 face we find after the invisible text. Remember what it
7802 was in IT->saved_face_id, and signal that it's there by
7803 setting face_before_selective_p. */
7804 it->saved_face_id = it->face_id;
7805 it->method = GET_FROM_BUFFER;
7806 it->object = it->w->contents;
7807 reseat_at_next_visible_line_start (it, 1);
7808 it->face_before_selective_p = 1;
7811 return GET_NEXT_DISPLAY_ELEMENT (it);
7815 /* Deliver an image display element. The iterator IT is already
7816 filled with image information (done in handle_display_prop). Value
7817 is always 1. */
7820 static int
7821 next_element_from_image (struct it *it)
7823 it->what = IT_IMAGE;
7824 it->ignore_overlay_strings_at_pos_p = 0;
7825 return 1;
7829 /* Fill iterator IT with next display element from a stretch glyph
7830 property. IT->object is the value of the text property. Value is
7831 always 1. */
7833 static int
7834 next_element_from_stretch (struct it *it)
7836 it->what = IT_STRETCH;
7837 return 1;
7840 /* Scan backwards from IT's current position until we find a stop
7841 position, or until BEGV. This is called when we find ourself
7842 before both the last known prev_stop and base_level_stop while
7843 reordering bidirectional text. */
7845 static void
7846 compute_stop_pos_backwards (struct it *it)
7848 const int SCAN_BACK_LIMIT = 1000;
7849 struct text_pos pos;
7850 struct display_pos save_current = it->current;
7851 struct text_pos save_position = it->position;
7852 ptrdiff_t charpos = IT_CHARPOS (*it);
7853 ptrdiff_t where_we_are = charpos;
7854 ptrdiff_t save_stop_pos = it->stop_charpos;
7855 ptrdiff_t save_end_pos = it->end_charpos;
7857 eassert (NILP (it->string) && !it->s);
7858 eassert (it->bidi_p);
7859 it->bidi_p = 0;
7862 it->end_charpos = min (charpos + 1, ZV);
7863 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7864 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7865 reseat_1 (it, pos, 0);
7866 compute_stop_pos (it);
7867 /* We must advance forward, right? */
7868 if (it->stop_charpos <= charpos)
7869 emacs_abort ();
7871 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7873 if (it->stop_charpos <= where_we_are)
7874 it->prev_stop = it->stop_charpos;
7875 else
7876 it->prev_stop = BEGV;
7877 it->bidi_p = 1;
7878 it->current = save_current;
7879 it->position = save_position;
7880 it->stop_charpos = save_stop_pos;
7881 it->end_charpos = save_end_pos;
7884 /* Scan forward from CHARPOS in the current buffer/string, until we
7885 find a stop position > current IT's position. Then handle the stop
7886 position before that. This is called when we bump into a stop
7887 position while reordering bidirectional text. CHARPOS should be
7888 the last previously processed stop_pos (or BEGV/0, if none were
7889 processed yet) whose position is less that IT's current
7890 position. */
7892 static void
7893 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7895 int bufp = !STRINGP (it->string);
7896 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7897 struct display_pos save_current = it->current;
7898 struct text_pos save_position = it->position;
7899 struct text_pos pos1;
7900 ptrdiff_t next_stop;
7902 /* Scan in strict logical order. */
7903 eassert (it->bidi_p);
7904 it->bidi_p = 0;
7907 it->prev_stop = charpos;
7908 if (bufp)
7910 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7911 reseat_1 (it, pos1, 0);
7913 else
7914 it->current.string_pos = string_pos (charpos, it->string);
7915 compute_stop_pos (it);
7916 /* We must advance forward, right? */
7917 if (it->stop_charpos <= it->prev_stop)
7918 emacs_abort ();
7919 charpos = it->stop_charpos;
7921 while (charpos <= where_we_are);
7923 it->bidi_p = 1;
7924 it->current = save_current;
7925 it->position = save_position;
7926 next_stop = it->stop_charpos;
7927 it->stop_charpos = it->prev_stop;
7928 handle_stop (it);
7929 it->stop_charpos = next_stop;
7932 /* Load IT with the next display element from current_buffer. Value
7933 is zero if end of buffer reached. IT->stop_charpos is the next
7934 position at which to stop and check for text properties or buffer
7935 end. */
7937 static int
7938 next_element_from_buffer (struct it *it)
7940 int success_p = 1;
7942 eassert (IT_CHARPOS (*it) >= BEGV);
7943 eassert (NILP (it->string) && !it->s);
7944 eassert (!it->bidi_p
7945 || (EQ (it->bidi_it.string.lstring, Qnil)
7946 && it->bidi_it.string.s == NULL));
7948 /* With bidi reordering, the character to display might not be the
7949 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7950 we were reseat()ed to a new buffer position, which is potentially
7951 a different paragraph. */
7952 if (it->bidi_p && it->bidi_it.first_elt)
7954 get_visually_first_element (it);
7955 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7958 if (IT_CHARPOS (*it) >= it->stop_charpos)
7960 if (IT_CHARPOS (*it) >= it->end_charpos)
7962 int overlay_strings_follow_p;
7964 /* End of the game, except when overlay strings follow that
7965 haven't been returned yet. */
7966 if (it->overlay_strings_at_end_processed_p)
7967 overlay_strings_follow_p = 0;
7968 else
7970 it->overlay_strings_at_end_processed_p = 1;
7971 overlay_strings_follow_p = get_overlay_strings (it, 0);
7974 if (overlay_strings_follow_p)
7975 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7976 else
7978 it->what = IT_EOB;
7979 it->position = it->current.pos;
7980 success_p = 0;
7983 else if (!(!it->bidi_p
7984 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7985 || IT_CHARPOS (*it) == it->stop_charpos))
7987 /* With bidi non-linear iteration, we could find ourselves
7988 far beyond the last computed stop_charpos, with several
7989 other stop positions in between that we missed. Scan
7990 them all now, in buffer's logical order, until we find
7991 and handle the last stop_charpos that precedes our
7992 current position. */
7993 handle_stop_backwards (it, it->stop_charpos);
7994 return GET_NEXT_DISPLAY_ELEMENT (it);
7996 else
7998 if (it->bidi_p)
8000 /* Take note of the stop position we just moved across,
8001 for when we will move back across it. */
8002 it->prev_stop = it->stop_charpos;
8003 /* If we are at base paragraph embedding level, take
8004 note of the last stop position seen at this
8005 level. */
8006 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8007 it->base_level_stop = it->stop_charpos;
8009 handle_stop (it);
8010 return GET_NEXT_DISPLAY_ELEMENT (it);
8013 else if (it->bidi_p
8014 /* If we are before prev_stop, we may have overstepped on
8015 our way backwards a stop_pos, and if so, we need to
8016 handle that stop_pos. */
8017 && IT_CHARPOS (*it) < it->prev_stop
8018 /* We can sometimes back up for reasons that have nothing
8019 to do with bidi reordering. E.g., compositions. The
8020 code below is only needed when we are above the base
8021 embedding level, so test for that explicitly. */
8022 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8024 if (it->base_level_stop <= 0
8025 || IT_CHARPOS (*it) < it->base_level_stop)
8027 /* If we lost track of base_level_stop, we need to find
8028 prev_stop by looking backwards. This happens, e.g., when
8029 we were reseated to the previous screenful of text by
8030 vertical-motion. */
8031 it->base_level_stop = BEGV;
8032 compute_stop_pos_backwards (it);
8033 handle_stop_backwards (it, it->prev_stop);
8035 else
8036 handle_stop_backwards (it, it->base_level_stop);
8037 return GET_NEXT_DISPLAY_ELEMENT (it);
8039 else
8041 /* No face changes, overlays etc. in sight, so just return a
8042 character from current_buffer. */
8043 unsigned char *p;
8044 ptrdiff_t stop;
8046 /* Maybe run the redisplay end trigger hook. Performance note:
8047 This doesn't seem to cost measurable time. */
8048 if (it->redisplay_end_trigger_charpos
8049 && it->glyph_row
8050 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8051 run_redisplay_end_trigger_hook (it);
8053 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8054 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8055 stop)
8056 && next_element_from_composition (it))
8058 return 1;
8061 /* Get the next character, maybe multibyte. */
8062 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8063 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8064 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8065 else
8066 it->c = *p, it->len = 1;
8068 /* Record what we have and where it came from. */
8069 it->what = IT_CHARACTER;
8070 it->object = it->w->contents;
8071 it->position = it->current.pos;
8073 /* Normally we return the character found above, except when we
8074 really want to return an ellipsis for selective display. */
8075 if (it->selective)
8077 if (it->c == '\n')
8079 /* A value of selective > 0 means hide lines indented more
8080 than that number of columns. */
8081 if (it->selective > 0
8082 && IT_CHARPOS (*it) + 1 < ZV
8083 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8084 IT_BYTEPOS (*it) + 1,
8085 it->selective))
8087 success_p = next_element_from_ellipsis (it);
8088 it->dpvec_char_len = -1;
8091 else if (it->c == '\r' && it->selective == -1)
8093 /* A value of selective == -1 means that everything from the
8094 CR to the end of the line is invisible, with maybe an
8095 ellipsis displayed for it. */
8096 success_p = next_element_from_ellipsis (it);
8097 it->dpvec_char_len = -1;
8102 /* Value is zero if end of buffer reached. */
8103 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8104 return success_p;
8108 /* Run the redisplay end trigger hook for IT. */
8110 static void
8111 run_redisplay_end_trigger_hook (struct it *it)
8113 Lisp_Object args[3];
8115 /* IT->glyph_row should be non-null, i.e. we should be actually
8116 displaying something, or otherwise we should not run the hook. */
8117 eassert (it->glyph_row);
8119 /* Set up hook arguments. */
8120 args[0] = Qredisplay_end_trigger_functions;
8121 args[1] = it->window;
8122 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8123 it->redisplay_end_trigger_charpos = 0;
8125 /* Since we are *trying* to run these functions, don't try to run
8126 them again, even if they get an error. */
8127 wset_redisplay_end_trigger (it->w, Qnil);
8128 Frun_hook_with_args (3, args);
8130 /* Notice if it changed the face of the character we are on. */
8131 handle_face_prop (it);
8135 /* Deliver a composition display element. Unlike the other
8136 next_element_from_XXX, this function is not registered in the array
8137 get_next_element[]. It is called from next_element_from_buffer and
8138 next_element_from_string when necessary. */
8140 static int
8141 next_element_from_composition (struct it *it)
8143 it->what = IT_COMPOSITION;
8144 it->len = it->cmp_it.nbytes;
8145 if (STRINGP (it->string))
8147 if (it->c < 0)
8149 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8150 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8151 return 0;
8153 it->position = it->current.string_pos;
8154 it->object = it->string;
8155 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8156 IT_STRING_BYTEPOS (*it), it->string);
8158 else
8160 if (it->c < 0)
8162 IT_CHARPOS (*it) += it->cmp_it.nchars;
8163 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8164 if (it->bidi_p)
8166 if (it->bidi_it.new_paragraph)
8167 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8168 /* Resync the bidi iterator with IT's new position.
8169 FIXME: this doesn't support bidirectional text. */
8170 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8171 bidi_move_to_visually_next (&it->bidi_it);
8173 return 0;
8175 it->position = it->current.pos;
8176 it->object = it->w->contents;
8177 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8178 IT_BYTEPOS (*it), Qnil);
8180 return 1;
8185 /***********************************************************************
8186 Moving an iterator without producing glyphs
8187 ***********************************************************************/
8189 /* Check if iterator is at a position corresponding to a valid buffer
8190 position after some move_it_ call. */
8192 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8193 ((it)->method == GET_FROM_STRING \
8194 ? IT_STRING_CHARPOS (*it) == 0 \
8195 : 1)
8198 /* Move iterator IT to a specified buffer or X position within one
8199 line on the display without producing glyphs.
8201 OP should be a bit mask including some or all of these bits:
8202 MOVE_TO_X: Stop upon reaching x-position TO_X.
8203 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8204 Regardless of OP's value, stop upon reaching the end of the display line.
8206 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8207 This means, in particular, that TO_X includes window's horizontal
8208 scroll amount.
8210 The return value has several possible values that
8211 say what condition caused the scan to stop:
8213 MOVE_POS_MATCH_OR_ZV
8214 - when TO_POS or ZV was reached.
8216 MOVE_X_REACHED
8217 -when TO_X was reached before TO_POS or ZV were reached.
8219 MOVE_LINE_CONTINUED
8220 - when we reached the end of the display area and the line must
8221 be continued.
8223 MOVE_LINE_TRUNCATED
8224 - when we reached the end of the display area and the line is
8225 truncated.
8227 MOVE_NEWLINE_OR_CR
8228 - when we stopped at a line end, i.e. a newline or a CR and selective
8229 display is on. */
8231 static enum move_it_result
8232 move_it_in_display_line_to (struct it *it,
8233 ptrdiff_t to_charpos, int to_x,
8234 enum move_operation_enum op)
8236 enum move_it_result result = MOVE_UNDEFINED;
8237 struct glyph_row *saved_glyph_row;
8238 struct it wrap_it, atpos_it, atx_it, ppos_it;
8239 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8240 void *ppos_data = NULL;
8241 int may_wrap = 0;
8242 enum it_method prev_method = it->method;
8243 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8244 int saw_smaller_pos = prev_pos < to_charpos;
8246 /* Don't produce glyphs in produce_glyphs. */
8247 saved_glyph_row = it->glyph_row;
8248 it->glyph_row = NULL;
8250 /* Use wrap_it to save a copy of IT wherever a word wrap could
8251 occur. Use atpos_it to save a copy of IT at the desired buffer
8252 position, if found, so that we can scan ahead and check if the
8253 word later overshoots the window edge. Use atx_it similarly, for
8254 pixel positions. */
8255 wrap_it.sp = -1;
8256 atpos_it.sp = -1;
8257 atx_it.sp = -1;
8259 /* Use ppos_it under bidi reordering to save a copy of IT for the
8260 position > CHARPOS that is the closest to CHARPOS. We restore
8261 that position in IT when we have scanned the entire display line
8262 without finding a match for CHARPOS and all the character
8263 positions are greater than CHARPOS. */
8264 if (it->bidi_p)
8266 SAVE_IT (ppos_it, *it, ppos_data);
8267 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8268 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8269 SAVE_IT (ppos_it, *it, ppos_data);
8272 #define BUFFER_POS_REACHED_P() \
8273 ((op & MOVE_TO_POS) != 0 \
8274 && BUFFERP (it->object) \
8275 && (IT_CHARPOS (*it) == to_charpos \
8276 || ((!it->bidi_p \
8277 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8278 && IT_CHARPOS (*it) > to_charpos) \
8279 || (it->what == IT_COMPOSITION \
8280 && ((IT_CHARPOS (*it) > to_charpos \
8281 && to_charpos >= it->cmp_it.charpos) \
8282 || (IT_CHARPOS (*it) < to_charpos \
8283 && to_charpos <= it->cmp_it.charpos)))) \
8284 && (it->method == GET_FROM_BUFFER \
8285 || (it->method == GET_FROM_DISPLAY_VECTOR \
8286 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8288 /* If there's a line-/wrap-prefix, handle it. */
8289 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8290 && it->current_y < it->last_visible_y)
8291 handle_line_prefix (it);
8293 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8294 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8296 while (1)
8298 int x, i, ascent = 0, descent = 0;
8300 /* Utility macro to reset an iterator with x, ascent, and descent. */
8301 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8302 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8303 (IT)->max_descent = descent)
8305 /* Stop if we move beyond TO_CHARPOS (after an image or a
8306 display string or stretch glyph). */
8307 if ((op & MOVE_TO_POS) != 0
8308 && BUFFERP (it->object)
8309 && it->method == GET_FROM_BUFFER
8310 && (((!it->bidi_p
8311 /* When the iterator is at base embedding level, we
8312 are guaranteed that characters are delivered for
8313 display in strictly increasing order of their
8314 buffer positions. */
8315 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8316 && IT_CHARPOS (*it) > to_charpos)
8317 || (it->bidi_p
8318 && (prev_method == GET_FROM_IMAGE
8319 || prev_method == GET_FROM_STRETCH
8320 || prev_method == GET_FROM_STRING)
8321 /* Passed TO_CHARPOS from left to right. */
8322 && ((prev_pos < to_charpos
8323 && IT_CHARPOS (*it) > to_charpos)
8324 /* Passed TO_CHARPOS from right to left. */
8325 || (prev_pos > to_charpos
8326 && IT_CHARPOS (*it) < to_charpos)))))
8328 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8330 result = MOVE_POS_MATCH_OR_ZV;
8331 break;
8333 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8334 /* If wrap_it is valid, the current position might be in a
8335 word that is wrapped. So, save the iterator in
8336 atpos_it and continue to see if wrapping happens. */
8337 SAVE_IT (atpos_it, *it, atpos_data);
8340 /* Stop when ZV reached.
8341 We used to stop here when TO_CHARPOS reached as well, but that is
8342 too soon if this glyph does not fit on this line. So we handle it
8343 explicitly below. */
8344 if (!get_next_display_element (it))
8346 result = MOVE_POS_MATCH_OR_ZV;
8347 break;
8350 if (it->line_wrap == TRUNCATE)
8352 if (BUFFER_POS_REACHED_P ())
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 break;
8358 else
8360 if (it->line_wrap == WORD_WRAP)
8362 if (IT_DISPLAYING_WHITESPACE (it))
8363 may_wrap = 1;
8364 else if (may_wrap)
8366 /* We have reached a glyph that follows one or more
8367 whitespace characters. If the position is
8368 already found, we are done. */
8369 if (atpos_it.sp >= 0)
8371 RESTORE_IT (it, &atpos_it, atpos_data);
8372 result = MOVE_POS_MATCH_OR_ZV;
8373 goto done;
8375 if (atx_it.sp >= 0)
8377 RESTORE_IT (it, &atx_it, atx_data);
8378 result = MOVE_X_REACHED;
8379 goto done;
8381 /* Otherwise, we can wrap here. */
8382 SAVE_IT (wrap_it, *it, wrap_data);
8383 may_wrap = 0;
8388 /* Remember the line height for the current line, in case
8389 the next element doesn't fit on the line. */
8390 ascent = it->max_ascent;
8391 descent = it->max_descent;
8393 /* The call to produce_glyphs will get the metrics of the
8394 display element IT is loaded with. Record the x-position
8395 before this display element, in case it doesn't fit on the
8396 line. */
8397 x = it->current_x;
8399 PRODUCE_GLYPHS (it);
8401 if (it->area != TEXT_AREA)
8403 prev_method = it->method;
8404 if (it->method == GET_FROM_BUFFER)
8405 prev_pos = IT_CHARPOS (*it);
8406 set_iterator_to_next (it, 1);
8407 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8408 SET_TEXT_POS (this_line_min_pos,
8409 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8410 if (it->bidi_p
8411 && (op & MOVE_TO_POS)
8412 && IT_CHARPOS (*it) > to_charpos
8413 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8414 SAVE_IT (ppos_it, *it, ppos_data);
8415 continue;
8418 /* The number of glyphs we get back in IT->nglyphs will normally
8419 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8420 character on a terminal frame, or (iii) a line end. For the
8421 second case, IT->nglyphs - 1 padding glyphs will be present.
8422 (On X frames, there is only one glyph produced for a
8423 composite character.)
8425 The behavior implemented below means, for continuation lines,
8426 that as many spaces of a TAB as fit on the current line are
8427 displayed there. For terminal frames, as many glyphs of a
8428 multi-glyph character are displayed in the current line, too.
8429 This is what the old redisplay code did, and we keep it that
8430 way. Under X, the whole shape of a complex character must
8431 fit on the line or it will be completely displayed in the
8432 next line.
8434 Note that both for tabs and padding glyphs, all glyphs have
8435 the same width. */
8436 if (it->nglyphs)
8438 /* More than one glyph or glyph doesn't fit on line. All
8439 glyphs have the same width. */
8440 int single_glyph_width = it->pixel_width / it->nglyphs;
8441 int new_x;
8442 int x_before_this_char = x;
8443 int hpos_before_this_char = it->hpos;
8445 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8447 new_x = x + single_glyph_width;
8449 /* We want to leave anything reaching TO_X to the caller. */
8450 if ((op & MOVE_TO_X) && new_x > to_x)
8452 if (BUFFER_POS_REACHED_P ())
8454 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8455 goto buffer_pos_reached;
8456 if (atpos_it.sp < 0)
8458 SAVE_IT (atpos_it, *it, atpos_data);
8459 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8462 else
8464 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8466 it->current_x = x;
8467 result = MOVE_X_REACHED;
8468 break;
8470 if (atx_it.sp < 0)
8472 SAVE_IT (atx_it, *it, atx_data);
8473 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8478 if (/* Lines are continued. */
8479 it->line_wrap != TRUNCATE
8480 && (/* And glyph doesn't fit on the line. */
8481 new_x > it->last_visible_x
8482 /* Or it fits exactly and we're on a window
8483 system frame. */
8484 || (new_x == it->last_visible_x
8485 && FRAME_WINDOW_P (it->f)
8486 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8487 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8488 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8490 if (/* IT->hpos == 0 means the very first glyph
8491 doesn't fit on the line, e.g. a wide image. */
8492 it->hpos == 0
8493 || (new_x == it->last_visible_x
8494 && FRAME_WINDOW_P (it->f)))
8496 ++it->hpos;
8497 it->current_x = new_x;
8499 /* The character's last glyph just barely fits
8500 in this row. */
8501 if (i == it->nglyphs - 1)
8503 /* If this is the destination position,
8504 return a position *before* it in this row,
8505 now that we know it fits in this row. */
8506 if (BUFFER_POS_REACHED_P ())
8508 if (it->line_wrap != WORD_WRAP
8509 || wrap_it.sp < 0)
8511 it->hpos = hpos_before_this_char;
8512 it->current_x = x_before_this_char;
8513 result = MOVE_POS_MATCH_OR_ZV;
8514 break;
8516 if (it->line_wrap == WORD_WRAP
8517 && atpos_it.sp < 0)
8519 SAVE_IT (atpos_it, *it, atpos_data);
8520 atpos_it.current_x = x_before_this_char;
8521 atpos_it.hpos = hpos_before_this_char;
8525 prev_method = it->method;
8526 if (it->method == GET_FROM_BUFFER)
8527 prev_pos = IT_CHARPOS (*it);
8528 set_iterator_to_next (it, 1);
8529 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8530 SET_TEXT_POS (this_line_min_pos,
8531 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8532 /* On graphical terminals, newlines may
8533 "overflow" into the fringe if
8534 overflow-newline-into-fringe is non-nil.
8535 On text terminals, and on graphical
8536 terminals with no right margin, newlines
8537 may overflow into the last glyph on the
8538 display line.*/
8539 if (!FRAME_WINDOW_P (it->f)
8540 || ((it->bidi_p
8541 && it->bidi_it.paragraph_dir == R2L)
8542 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8543 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8544 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8546 if (!get_next_display_element (it))
8548 result = MOVE_POS_MATCH_OR_ZV;
8549 break;
8551 if (BUFFER_POS_REACHED_P ())
8553 if (ITERATOR_AT_END_OF_LINE_P (it))
8554 result = MOVE_POS_MATCH_OR_ZV;
8555 else
8556 result = MOVE_LINE_CONTINUED;
8557 break;
8559 if (ITERATOR_AT_END_OF_LINE_P (it)
8560 && (it->line_wrap != WORD_WRAP
8561 || wrap_it.sp < 0))
8563 result = MOVE_NEWLINE_OR_CR;
8564 break;
8569 else
8570 IT_RESET_X_ASCENT_DESCENT (it);
8572 if (wrap_it.sp >= 0)
8574 RESTORE_IT (it, &wrap_it, wrap_data);
8575 atpos_it.sp = -1;
8576 atx_it.sp = -1;
8579 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8580 IT_CHARPOS (*it)));
8581 result = MOVE_LINE_CONTINUED;
8582 break;
8585 if (BUFFER_POS_REACHED_P ())
8587 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8588 goto buffer_pos_reached;
8589 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8591 SAVE_IT (atpos_it, *it, atpos_data);
8592 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8596 if (new_x > it->first_visible_x)
8598 /* Glyph is visible. Increment number of glyphs that
8599 would be displayed. */
8600 ++it->hpos;
8604 if (result != MOVE_UNDEFINED)
8605 break;
8607 else if (BUFFER_POS_REACHED_P ())
8609 buffer_pos_reached:
8610 IT_RESET_X_ASCENT_DESCENT (it);
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 break;
8614 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8616 /* Stop when TO_X specified and reached. This check is
8617 necessary here because of lines consisting of a line end,
8618 only. The line end will not produce any glyphs and we
8619 would never get MOVE_X_REACHED. */
8620 eassert (it->nglyphs == 0);
8621 result = MOVE_X_REACHED;
8622 break;
8625 /* Is this a line end? If yes, we're done. */
8626 if (ITERATOR_AT_END_OF_LINE_P (it))
8628 /* If we are past TO_CHARPOS, but never saw any character
8629 positions smaller than TO_CHARPOS, return
8630 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8631 did. */
8632 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8634 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8636 if (IT_CHARPOS (ppos_it) < ZV)
8638 RESTORE_IT (it, &ppos_it, ppos_data);
8639 result = MOVE_POS_MATCH_OR_ZV;
8641 else
8642 goto buffer_pos_reached;
8644 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8645 && IT_CHARPOS (*it) > to_charpos)
8646 goto buffer_pos_reached;
8647 else
8648 result = MOVE_NEWLINE_OR_CR;
8650 else
8651 result = MOVE_NEWLINE_OR_CR;
8652 break;
8655 prev_method = it->method;
8656 if (it->method == GET_FROM_BUFFER)
8657 prev_pos = IT_CHARPOS (*it);
8658 /* The current display element has been consumed. Advance
8659 to the next. */
8660 set_iterator_to_next (it, 1);
8661 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8662 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8663 if (IT_CHARPOS (*it) < to_charpos)
8664 saw_smaller_pos = 1;
8665 if (it->bidi_p
8666 && (op & MOVE_TO_POS)
8667 && IT_CHARPOS (*it) >= to_charpos
8668 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8669 SAVE_IT (ppos_it, *it, ppos_data);
8671 /* Stop if lines are truncated and IT's current x-position is
8672 past the right edge of the window now. */
8673 if (it->line_wrap == TRUNCATE
8674 && it->current_x >= it->last_visible_x)
8676 if (!FRAME_WINDOW_P (it->f)
8677 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8678 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8679 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8680 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8682 int at_eob_p = 0;
8684 if ((at_eob_p = !get_next_display_element (it))
8685 || BUFFER_POS_REACHED_P ()
8686 /* If we are past TO_CHARPOS, but never saw any
8687 character positions smaller than TO_CHARPOS,
8688 return MOVE_POS_MATCH_OR_ZV, like the
8689 unidirectional display did. */
8690 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8691 && !saw_smaller_pos
8692 && IT_CHARPOS (*it) > to_charpos))
8694 if (it->bidi_p
8695 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8696 RESTORE_IT (it, &ppos_it, ppos_data);
8697 result = MOVE_POS_MATCH_OR_ZV;
8698 break;
8700 if (ITERATOR_AT_END_OF_LINE_P (it))
8702 result = MOVE_NEWLINE_OR_CR;
8703 break;
8706 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8707 && !saw_smaller_pos
8708 && IT_CHARPOS (*it) > to_charpos)
8710 if (IT_CHARPOS (ppos_it) < ZV)
8711 RESTORE_IT (it, &ppos_it, ppos_data);
8712 result = MOVE_POS_MATCH_OR_ZV;
8713 break;
8715 result = MOVE_LINE_TRUNCATED;
8716 break;
8718 #undef IT_RESET_X_ASCENT_DESCENT
8721 #undef BUFFER_POS_REACHED_P
8723 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8724 restore the saved iterator. */
8725 if (atpos_it.sp >= 0)
8726 RESTORE_IT (it, &atpos_it, atpos_data);
8727 else if (atx_it.sp >= 0)
8728 RESTORE_IT (it, &atx_it, atx_data);
8730 done:
8732 if (atpos_data)
8733 bidi_unshelve_cache (atpos_data, 1);
8734 if (atx_data)
8735 bidi_unshelve_cache (atx_data, 1);
8736 if (wrap_data)
8737 bidi_unshelve_cache (wrap_data, 1);
8738 if (ppos_data)
8739 bidi_unshelve_cache (ppos_data, 1);
8741 /* Restore the iterator settings altered at the beginning of this
8742 function. */
8743 it->glyph_row = saved_glyph_row;
8744 return result;
8747 /* For external use. */
8748 void
8749 move_it_in_display_line (struct it *it,
8750 ptrdiff_t to_charpos, int to_x,
8751 enum move_operation_enum op)
8753 if (it->line_wrap == WORD_WRAP
8754 && (op & MOVE_TO_X))
8756 struct it save_it;
8757 void *save_data = NULL;
8758 int skip;
8760 SAVE_IT (save_it, *it, save_data);
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8762 /* When word-wrap is on, TO_X may lie past the end
8763 of a wrapped line. Then it->current is the
8764 character on the next line, so backtrack to the
8765 space before the wrap point. */
8766 if (skip == MOVE_LINE_CONTINUED)
8768 int prev_x = max (it->current_x - 1, 0);
8769 RESTORE_IT (it, &save_it, save_data);
8770 move_it_in_display_line_to
8771 (it, -1, prev_x, MOVE_TO_X);
8773 else
8774 bidi_unshelve_cache (save_data, 1);
8776 else
8777 move_it_in_display_line_to (it, to_charpos, to_x, op);
8781 /* Move IT forward until it satisfies one or more of the criteria in
8782 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8784 OP is a bit-mask that specifies where to stop, and in particular,
8785 which of those four position arguments makes a difference. See the
8786 description of enum move_operation_enum.
8788 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8789 screen line, this function will set IT to the next position that is
8790 displayed to the right of TO_CHARPOS on the screen. */
8792 void
8793 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8795 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8796 int line_height, line_start_x = 0, reached = 0;
8797 void *backup_data = NULL;
8799 for (;;)
8801 if (op & MOVE_TO_VPOS)
8803 /* If no TO_CHARPOS and no TO_X specified, stop at the
8804 start of the line TO_VPOS. */
8805 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8807 if (it->vpos == to_vpos)
8809 reached = 1;
8810 break;
8812 else
8813 skip = move_it_in_display_line_to (it, -1, -1, 0);
8815 else
8817 /* TO_VPOS >= 0 means stop at TO_X in the line at
8818 TO_VPOS, or at TO_POS, whichever comes first. */
8819 if (it->vpos == to_vpos)
8821 reached = 2;
8822 break;
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8827 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8829 reached = 3;
8830 break;
8832 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8834 /* We have reached TO_X but not in the line we want. */
8835 skip = move_it_in_display_line_to (it, to_charpos,
8836 -1, MOVE_TO_POS);
8837 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 4;
8840 break;
8845 else if (op & MOVE_TO_Y)
8847 struct it it_backup;
8849 if (it->line_wrap == WORD_WRAP)
8850 SAVE_IT (it_backup, *it, backup_data);
8852 /* TO_Y specified means stop at TO_X in the line containing
8853 TO_Y---or at TO_CHARPOS if this is reached first. The
8854 problem is that we can't really tell whether the line
8855 contains TO_Y before we have completely scanned it, and
8856 this may skip past TO_X. What we do is to first scan to
8857 TO_X.
8859 If TO_X is not specified, use a TO_X of zero. The reason
8860 is to make the outcome of this function more predictable.
8861 If we didn't use TO_X == 0, we would stop at the end of
8862 the line which is probably not what a caller would expect
8863 to happen. */
8864 skip = move_it_in_display_line_to
8865 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8866 (MOVE_TO_X | (op & MOVE_TO_POS)));
8868 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8869 if (skip == MOVE_POS_MATCH_OR_ZV)
8870 reached = 5;
8871 else if (skip == MOVE_X_REACHED)
8873 /* If TO_X was reached, we want to know whether TO_Y is
8874 in the line. We know this is the case if the already
8875 scanned glyphs make the line tall enough. Otherwise,
8876 we must check by scanning the rest of the line. */
8877 line_height = it->max_ascent + it->max_descent;
8878 if (to_y >= it->current_y
8879 && to_y < it->current_y + line_height)
8881 reached = 6;
8882 break;
8884 SAVE_IT (it_backup, *it, backup_data);
8885 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8886 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8887 op & MOVE_TO_POS);
8888 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8889 line_height = it->max_ascent + it->max_descent;
8890 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8892 if (to_y >= it->current_y
8893 && to_y < it->current_y + line_height)
8895 /* If TO_Y is in this line and TO_X was reached
8896 above, we scanned too far. We have to restore
8897 IT's settings to the ones before skipping. But
8898 keep the more accurate values of max_ascent and
8899 max_descent we've found while skipping the rest
8900 of the line, for the sake of callers, such as
8901 pos_visible_p, that need to know the line
8902 height. */
8903 int max_ascent = it->max_ascent;
8904 int max_descent = it->max_descent;
8906 RESTORE_IT (it, &it_backup, backup_data);
8907 it->max_ascent = max_ascent;
8908 it->max_descent = max_descent;
8909 reached = 6;
8911 else
8913 skip = skip2;
8914 if (skip == MOVE_POS_MATCH_OR_ZV)
8915 reached = 7;
8918 else
8920 /* Check whether TO_Y is in this line. */
8921 line_height = it->max_ascent + it->max_descent;
8922 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8924 if (to_y >= it->current_y
8925 && to_y < it->current_y + line_height)
8927 /* When word-wrap is on, TO_X may lie past the end
8928 of a wrapped line. Then it->current is the
8929 character on the next line, so backtrack to the
8930 space before the wrap point. */
8931 if (skip == MOVE_LINE_CONTINUED
8932 && it->line_wrap == WORD_WRAP)
8934 int prev_x = max (it->current_x - 1, 0);
8935 RESTORE_IT (it, &it_backup, backup_data);
8936 skip = move_it_in_display_line_to
8937 (it, -1, prev_x, MOVE_TO_X);
8939 reached = 6;
8943 if (reached)
8944 break;
8946 else if (BUFFERP (it->object)
8947 && (it->method == GET_FROM_BUFFER
8948 || it->method == GET_FROM_STRETCH)
8949 && IT_CHARPOS (*it) >= to_charpos
8950 /* Under bidi iteration, a call to set_iterator_to_next
8951 can scan far beyond to_charpos if the initial
8952 portion of the next line needs to be reordered. In
8953 that case, give move_it_in_display_line_to another
8954 chance below. */
8955 && !(it->bidi_p
8956 && it->bidi_it.scan_dir == -1))
8957 skip = MOVE_POS_MATCH_OR_ZV;
8958 else
8959 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8961 switch (skip)
8963 case MOVE_POS_MATCH_OR_ZV:
8964 reached = 8;
8965 goto out;
8967 case MOVE_NEWLINE_OR_CR:
8968 set_iterator_to_next (it, 1);
8969 it->continuation_lines_width = 0;
8970 break;
8972 case MOVE_LINE_TRUNCATED:
8973 it->continuation_lines_width = 0;
8974 reseat_at_next_visible_line_start (it, 0);
8975 if ((op & MOVE_TO_POS) != 0
8976 && IT_CHARPOS (*it) > to_charpos)
8978 reached = 9;
8979 goto out;
8981 break;
8983 case MOVE_LINE_CONTINUED:
8984 /* For continued lines ending in a tab, some of the glyphs
8985 associated with the tab are displayed on the current
8986 line. Since it->current_x does not include these glyphs,
8987 we use it->last_visible_x instead. */
8988 if (it->c == '\t')
8990 it->continuation_lines_width += it->last_visible_x;
8991 /* When moving by vpos, ensure that the iterator really
8992 advances to the next line (bug#847, bug#969). Fixme:
8993 do we need to do this in other circumstances? */
8994 if (it->current_x != it->last_visible_x
8995 && (op & MOVE_TO_VPOS)
8996 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8998 line_start_x = it->current_x + it->pixel_width
8999 - it->last_visible_x;
9000 set_iterator_to_next (it, 0);
9003 else
9004 it->continuation_lines_width += it->current_x;
9005 break;
9007 default:
9008 emacs_abort ();
9011 /* Reset/increment for the next run. */
9012 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9013 it->current_x = line_start_x;
9014 line_start_x = 0;
9015 it->hpos = 0;
9016 it->current_y += it->max_ascent + it->max_descent;
9017 ++it->vpos;
9018 last_height = it->max_ascent + it->max_descent;
9019 it->max_ascent = it->max_descent = 0;
9022 out:
9024 /* On text terminals, we may stop at the end of a line in the middle
9025 of a multi-character glyph. If the glyph itself is continued,
9026 i.e. it is actually displayed on the next line, don't treat this
9027 stopping point as valid; move to the next line instead (unless
9028 that brings us offscreen). */
9029 if (!FRAME_WINDOW_P (it->f)
9030 && op & MOVE_TO_POS
9031 && IT_CHARPOS (*it) == to_charpos
9032 && it->what == IT_CHARACTER
9033 && it->nglyphs > 1
9034 && it->line_wrap == WINDOW_WRAP
9035 && it->current_x == it->last_visible_x - 1
9036 && it->c != '\n'
9037 && it->c != '\t'
9038 && it->vpos < it->w->window_end_vpos)
9040 it->continuation_lines_width += it->current_x;
9041 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9042 it->current_y += it->max_ascent + it->max_descent;
9043 ++it->vpos;
9044 last_height = it->max_ascent + it->max_descent;
9047 if (backup_data)
9048 bidi_unshelve_cache (backup_data, 1);
9050 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9054 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9056 If DY > 0, move IT backward at least that many pixels. DY = 0
9057 means move IT backward to the preceding line start or BEGV. This
9058 function may move over more than DY pixels if IT->current_y - DY
9059 ends up in the middle of a line; in this case IT->current_y will be
9060 set to the top of the line moved to. */
9062 void
9063 move_it_vertically_backward (struct it *it, int dy)
9065 int nlines, h;
9066 struct it it2, it3;
9067 void *it2data = NULL, *it3data = NULL;
9068 ptrdiff_t start_pos;
9069 int nchars_per_row
9070 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9071 ptrdiff_t pos_limit;
9073 move_further_back:
9074 eassert (dy >= 0);
9076 start_pos = IT_CHARPOS (*it);
9078 /* Estimate how many newlines we must move back. */
9079 nlines = max (1, dy / default_line_pixel_height (it->w));
9080 if (it->line_wrap == TRUNCATE)
9081 pos_limit = BEGV;
9082 else
9083 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9085 /* Set the iterator's position that many lines back. But don't go
9086 back more than NLINES full screen lines -- this wins a day with
9087 buffers which have very long lines. */
9088 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9089 back_to_previous_visible_line_start (it);
9091 /* Reseat the iterator here. When moving backward, we don't want
9092 reseat to skip forward over invisible text, set up the iterator
9093 to deliver from overlay strings at the new position etc. So,
9094 use reseat_1 here. */
9095 reseat_1 (it, it->current.pos, 1);
9097 /* We are now surely at a line start. */
9098 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9099 reordering is in effect. */
9100 it->continuation_lines_width = 0;
9102 /* Move forward and see what y-distance we moved. First move to the
9103 start of the next line so that we get its height. We need this
9104 height to be able to tell whether we reached the specified
9105 y-distance. */
9106 SAVE_IT (it2, *it, it2data);
9107 it2.max_ascent = it2.max_descent = 0;
9110 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9111 MOVE_TO_POS | MOVE_TO_VPOS);
9113 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9114 /* If we are in a display string which starts at START_POS,
9115 and that display string includes a newline, and we are
9116 right after that newline (i.e. at the beginning of a
9117 display line), exit the loop, because otherwise we will
9118 infloop, since move_it_to will see that it is already at
9119 START_POS and will not move. */
9120 || (it2.method == GET_FROM_STRING
9121 && IT_CHARPOS (it2) == start_pos
9122 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9123 eassert (IT_CHARPOS (*it) >= BEGV);
9124 SAVE_IT (it3, it2, it3data);
9126 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9127 eassert (IT_CHARPOS (*it) >= BEGV);
9128 /* H is the actual vertical distance from the position in *IT
9129 and the starting position. */
9130 h = it2.current_y - it->current_y;
9131 /* NLINES is the distance in number of lines. */
9132 nlines = it2.vpos - it->vpos;
9134 /* Correct IT's y and vpos position
9135 so that they are relative to the starting point. */
9136 it->vpos -= nlines;
9137 it->current_y -= h;
9139 if (dy == 0)
9141 /* DY == 0 means move to the start of the screen line. The
9142 value of nlines is > 0 if continuation lines were involved,
9143 or if the original IT position was at start of a line. */
9144 RESTORE_IT (it, it, it2data);
9145 if (nlines > 0)
9146 move_it_by_lines (it, nlines);
9147 /* The above code moves us to some position NLINES down,
9148 usually to its first glyph (leftmost in an L2R line), but
9149 that's not necessarily the start of the line, under bidi
9150 reordering. We want to get to the character position
9151 that is immediately after the newline of the previous
9152 line. */
9153 if (it->bidi_p
9154 && !it->continuation_lines_width
9155 && !STRINGP (it->string)
9156 && IT_CHARPOS (*it) > BEGV
9157 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9159 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9161 DEC_BOTH (cp, bp);
9162 cp = find_newline_no_quit (cp, bp, -1, NULL);
9163 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9165 bidi_unshelve_cache (it3data, 1);
9167 else
9169 /* The y-position we try to reach, relative to *IT.
9170 Note that H has been subtracted in front of the if-statement. */
9171 int target_y = it->current_y + h - dy;
9172 int y0 = it3.current_y;
9173 int y1;
9174 int line_height;
9176 RESTORE_IT (&it3, &it3, it3data);
9177 y1 = line_bottom_y (&it3);
9178 line_height = y1 - y0;
9179 RESTORE_IT (it, it, it2data);
9180 /* If we did not reach target_y, try to move further backward if
9181 we can. If we moved too far backward, try to move forward. */
9182 if (target_y < it->current_y
9183 /* This is heuristic. In a window that's 3 lines high, with
9184 a line height of 13 pixels each, recentering with point
9185 on the bottom line will try to move -39/2 = 19 pixels
9186 backward. Try to avoid moving into the first line. */
9187 && (it->current_y - target_y
9188 > min (window_box_height (it->w), line_height * 2 / 3))
9189 && IT_CHARPOS (*it) > BEGV)
9191 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9192 target_y - it->current_y));
9193 dy = it->current_y - target_y;
9194 goto move_further_back;
9196 else if (target_y >= it->current_y + line_height
9197 && IT_CHARPOS (*it) < ZV)
9199 /* Should move forward by at least one line, maybe more.
9201 Note: Calling move_it_by_lines can be expensive on
9202 terminal frames, where compute_motion is used (via
9203 vmotion) to do the job, when there are very long lines
9204 and truncate-lines is nil. That's the reason for
9205 treating terminal frames specially here. */
9207 if (!FRAME_WINDOW_P (it->f))
9208 move_it_vertically (it, target_y - (it->current_y + line_height));
9209 else
9213 move_it_by_lines (it, 1);
9215 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9222 /* Move IT by a specified amount of pixel lines DY. DY negative means
9223 move backwards. DY = 0 means move to start of screen line. At the
9224 end, IT will be on the start of a screen line. */
9226 void
9227 move_it_vertically (struct it *it, int dy)
9229 if (dy <= 0)
9230 move_it_vertically_backward (it, -dy);
9231 else
9233 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9234 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9235 MOVE_TO_POS | MOVE_TO_Y);
9236 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9238 /* If buffer ends in ZV without a newline, move to the start of
9239 the line to satisfy the post-condition. */
9240 if (IT_CHARPOS (*it) == ZV
9241 && ZV > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 move_it_by_lines (it, 0);
9248 /* Move iterator IT past the end of the text line it is in. */
9250 void
9251 move_it_past_eol (struct it *it)
9253 enum move_it_result rc;
9255 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9256 if (rc == MOVE_NEWLINE_OR_CR)
9257 set_iterator_to_next (it, 0);
9261 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9262 negative means move up. DVPOS == 0 means move to the start of the
9263 screen line.
9265 Optimization idea: If we would know that IT->f doesn't use
9266 a face with proportional font, we could be faster for
9267 truncate-lines nil. */
9269 void
9270 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9273 /* The commented-out optimization uses vmotion on terminals. This
9274 gives bad results, because elements like it->what, on which
9275 callers such as pos_visible_p rely, aren't updated. */
9276 /* struct position pos;
9277 if (!FRAME_WINDOW_P (it->f))
9279 struct text_pos textpos;
9281 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9282 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9283 reseat (it, textpos, 1);
9284 it->vpos += pos.vpos;
9285 it->current_y += pos.vpos;
9287 else */
9289 if (dvpos == 0)
9291 /* DVPOS == 0 means move to the start of the screen line. */
9292 move_it_vertically_backward (it, 0);
9293 /* Let next call to line_bottom_y calculate real line height */
9294 last_height = 0;
9296 else if (dvpos > 0)
9298 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9299 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9301 /* Only move to the next buffer position if we ended up in a
9302 string from display property, not in an overlay string
9303 (before-string or after-string). That is because the
9304 latter don't conceal the underlying buffer position, so
9305 we can ask to move the iterator to the exact position we
9306 are interested in. Note that, even if we are already at
9307 IT_CHARPOS (*it), the call below is not a no-op, as it
9308 will detect that we are at the end of the string, pop the
9309 iterator, and compute it->current_x and it->hpos
9310 correctly. */
9311 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9312 -1, -1, -1, MOVE_TO_POS);
9315 else
9317 struct it it2;
9318 void *it2data = NULL;
9319 ptrdiff_t start_charpos, i;
9320 int nchars_per_row
9321 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9322 ptrdiff_t pos_limit;
9324 /* Start at the beginning of the screen line containing IT's
9325 position. This may actually move vertically backwards,
9326 in case of overlays, so adjust dvpos accordingly. */
9327 dvpos += it->vpos;
9328 move_it_vertically_backward (it, 0);
9329 dvpos -= it->vpos;
9331 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9332 screen lines, and reseat the iterator there. */
9333 start_charpos = IT_CHARPOS (*it);
9334 if (it->line_wrap == TRUNCATE)
9335 pos_limit = BEGV;
9336 else
9337 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9338 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9339 back_to_previous_visible_line_start (it);
9340 reseat (it, it->current.pos, 1);
9342 /* Move further back if we end up in a string or an image. */
9343 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9345 /* First try to move to start of display line. */
9346 dvpos += it->vpos;
9347 move_it_vertically_backward (it, 0);
9348 dvpos -= it->vpos;
9349 if (IT_POS_VALID_AFTER_MOVE_P (it))
9350 break;
9351 /* If start of line is still in string or image,
9352 move further back. */
9353 back_to_previous_visible_line_start (it);
9354 reseat (it, it->current.pos, 1);
9355 dvpos--;
9358 it->current_x = it->hpos = 0;
9360 /* Above call may have moved too far if continuation lines
9361 are involved. Scan forward and see if it did. */
9362 SAVE_IT (it2, *it, it2data);
9363 it2.vpos = it2.current_y = 0;
9364 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9365 it->vpos -= it2.vpos;
9366 it->current_y -= it2.current_y;
9367 it->current_x = it->hpos = 0;
9369 /* If we moved too far back, move IT some lines forward. */
9370 if (it2.vpos > -dvpos)
9372 int delta = it2.vpos + dvpos;
9374 RESTORE_IT (&it2, &it2, it2data);
9375 SAVE_IT (it2, *it, it2data);
9376 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9377 /* Move back again if we got too far ahead. */
9378 if (IT_CHARPOS (*it) >= start_charpos)
9379 RESTORE_IT (it, &it2, it2data);
9380 else
9381 bidi_unshelve_cache (it2data, 1);
9383 else
9384 RESTORE_IT (it, it, it2data);
9388 /* Return 1 if IT points into the middle of a display vector. */
9391 in_display_vector_p (struct it *it)
9393 return (it->method == GET_FROM_DISPLAY_VECTOR
9394 && it->current.dpvec_index > 0
9395 && it->dpvec + it->current.dpvec_index != it->dpend);
9399 /***********************************************************************
9400 Messages
9401 ***********************************************************************/
9404 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9405 to *Messages*. */
9407 void
9408 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9410 Lisp_Object args[3];
9411 Lisp_Object msg, fmt;
9412 char *buffer;
9413 ptrdiff_t len;
9414 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9415 USE_SAFE_ALLOCA;
9417 fmt = msg = Qnil;
9418 GCPRO4 (fmt, msg, arg1, arg2);
9420 args[0] = fmt = build_string (format);
9421 args[1] = arg1;
9422 args[2] = arg2;
9423 msg = Fformat (3, args);
9425 len = SBYTES (msg) + 1;
9426 buffer = SAFE_ALLOCA (len);
9427 memcpy (buffer, SDATA (msg), len);
9429 message_dolog (buffer, len - 1, 1, 0);
9430 SAFE_FREE ();
9432 UNGCPRO;
9436 /* Output a newline in the *Messages* buffer if "needs" one. */
9438 void
9439 message_log_maybe_newline (void)
9441 if (message_log_need_newline)
9442 message_dolog ("", 0, 1, 0);
9446 /* Add a string M of length NBYTES to the message log, optionally
9447 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9448 true, means interpret the contents of M as multibyte. This
9449 function calls low-level routines in order to bypass text property
9450 hooks, etc. which might not be safe to run.
9452 This may GC (insert may run before/after change hooks),
9453 so the buffer M must NOT point to a Lisp string. */
9455 void
9456 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9458 const unsigned char *msg = (const unsigned char *) m;
9460 if (!NILP (Vmemory_full))
9461 return;
9463 if (!NILP (Vmessage_log_max))
9465 struct buffer *oldbuf;
9466 Lisp_Object oldpoint, oldbegv, oldzv;
9467 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9468 ptrdiff_t point_at_end = 0;
9469 ptrdiff_t zv_at_end = 0;
9470 Lisp_Object old_deactivate_mark;
9471 bool shown;
9472 struct gcpro gcpro1;
9474 old_deactivate_mark = Vdeactivate_mark;
9475 oldbuf = current_buffer;
9477 /* Ensure the Messages buffer exists, and switch to it.
9478 If we created it, set the major-mode. */
9480 int newbuffer = 0;
9481 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9483 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9485 if (newbuffer &&
9486 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9487 call0 (intern ("messages-buffer-mode"));
9490 bset_undo_list (current_buffer, Qt);
9492 oldpoint = message_dolog_marker1;
9493 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9494 oldbegv = message_dolog_marker2;
9495 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9496 oldzv = message_dolog_marker3;
9497 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9498 GCPRO1 (old_deactivate_mark);
9500 if (PT == Z)
9501 point_at_end = 1;
9502 if (ZV == Z)
9503 zv_at_end = 1;
9505 BEGV = BEG;
9506 BEGV_BYTE = BEG_BYTE;
9507 ZV = Z;
9508 ZV_BYTE = Z_BYTE;
9509 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9511 /* Insert the string--maybe converting multibyte to single byte
9512 or vice versa, so that all the text fits the buffer. */
9513 if (multibyte
9514 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9516 ptrdiff_t i;
9517 int c, char_bytes;
9518 char work[1];
9520 /* Convert a multibyte string to single-byte
9521 for the *Message* buffer. */
9522 for (i = 0; i < nbytes; i += char_bytes)
9524 c = string_char_and_length (msg + i, &char_bytes);
9525 work[0] = (ASCII_CHAR_P (c)
9527 : multibyte_char_to_unibyte (c));
9528 insert_1_both (work, 1, 1, 1, 0, 0);
9531 else if (! multibyte
9532 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9534 ptrdiff_t i;
9535 int c, char_bytes;
9536 unsigned char str[MAX_MULTIBYTE_LENGTH];
9537 /* Convert a single-byte string to multibyte
9538 for the *Message* buffer. */
9539 for (i = 0; i < nbytes; i++)
9541 c = msg[i];
9542 MAKE_CHAR_MULTIBYTE (c);
9543 char_bytes = CHAR_STRING (c, str);
9544 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9547 else if (nbytes)
9548 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9550 if (nlflag)
9552 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9553 printmax_t dups;
9555 insert_1_both ("\n", 1, 1, 1, 0, 0);
9557 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9558 this_bol = PT;
9559 this_bol_byte = PT_BYTE;
9561 /* See if this line duplicates the previous one.
9562 If so, combine duplicates. */
9563 if (this_bol > BEG)
9565 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9566 prev_bol = PT;
9567 prev_bol_byte = PT_BYTE;
9569 dups = message_log_check_duplicate (prev_bol_byte,
9570 this_bol_byte);
9571 if (dups)
9573 del_range_both (prev_bol, prev_bol_byte,
9574 this_bol, this_bol_byte, 0);
9575 if (dups > 1)
9577 char dupstr[sizeof " [ times]"
9578 + INT_STRLEN_BOUND (printmax_t)];
9580 /* If you change this format, don't forget to also
9581 change message_log_check_duplicate. */
9582 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9583 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9584 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9589 /* If we have more than the desired maximum number of lines
9590 in the *Messages* buffer now, delete the oldest ones.
9591 This is safe because we don't have undo in this buffer. */
9593 if (NATNUMP (Vmessage_log_max))
9595 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9596 -XFASTINT (Vmessage_log_max) - 1, 0);
9597 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9600 BEGV = marker_position (oldbegv);
9601 BEGV_BYTE = marker_byte_position (oldbegv);
9603 if (zv_at_end)
9605 ZV = Z;
9606 ZV_BYTE = Z_BYTE;
9608 else
9610 ZV = marker_position (oldzv);
9611 ZV_BYTE = marker_byte_position (oldzv);
9614 if (point_at_end)
9615 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9616 else
9617 /* We can't do Fgoto_char (oldpoint) because it will run some
9618 Lisp code. */
9619 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9620 marker_byte_position (oldpoint));
9622 UNGCPRO;
9623 unchain_marker (XMARKER (oldpoint));
9624 unchain_marker (XMARKER (oldbegv));
9625 unchain_marker (XMARKER (oldzv));
9627 shown = buffer_window_count (current_buffer) > 0;
9628 set_buffer_internal (oldbuf);
9629 /* We called insert_1_both above with its 5th argument (PREPARE)
9630 zero, which prevents insert_1_both from calling
9631 prepare_to_modify_buffer, which in turns prevents us from
9632 incrementing windows_or_buffers_changed even if *Messages* is
9633 shown in some window. So we must manually incrementing
9634 windows_or_buffers_changed here to make up for that. */
9635 if (shown)
9636 windows_or_buffers_changed++;
9637 else
9638 windows_or_buffers_changed = old_windows_or_buffers_changed;
9639 message_log_need_newline = !nlflag;
9640 Vdeactivate_mark = old_deactivate_mark;
9645 /* We are at the end of the buffer after just having inserted a newline.
9646 (Note: We depend on the fact we won't be crossing the gap.)
9647 Check to see if the most recent message looks a lot like the previous one.
9648 Return 0 if different, 1 if the new one should just replace it, or a
9649 value N > 1 if we should also append " [N times]". */
9651 static intmax_t
9652 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9654 ptrdiff_t i;
9655 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9656 int seen_dots = 0;
9657 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9658 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9660 for (i = 0; i < len; i++)
9662 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9663 seen_dots = 1;
9664 if (p1[i] != p2[i])
9665 return seen_dots;
9667 p1 += len;
9668 if (*p1 == '\n')
9669 return 2;
9670 if (*p1++ == ' ' && *p1++ == '[')
9672 char *pend;
9673 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9674 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9675 return n + 1;
9677 return 0;
9681 /* Display an echo area message M with a specified length of NBYTES
9682 bytes. The string may include null characters. If M is not a
9683 string, clear out any existing message, and let the mini-buffer
9684 text show through.
9686 This function cancels echoing. */
9688 void
9689 message3 (Lisp_Object m)
9691 struct gcpro gcpro1;
9693 GCPRO1 (m);
9694 clear_message (1,1);
9695 cancel_echoing ();
9697 /* First flush out any partial line written with print. */
9698 message_log_maybe_newline ();
9699 if (STRINGP (m))
9701 ptrdiff_t nbytes = SBYTES (m);
9702 bool multibyte = STRING_MULTIBYTE (m);
9703 USE_SAFE_ALLOCA;
9704 char *buffer = SAFE_ALLOCA (nbytes);
9705 memcpy (buffer, SDATA (m), nbytes);
9706 message_dolog (buffer, nbytes, 1, multibyte);
9707 SAFE_FREE ();
9709 message3_nolog (m);
9711 UNGCPRO;
9715 /* The non-logging version of message3.
9716 This does not cancel echoing, because it is used for echoing.
9717 Perhaps we need to make a separate function for echoing
9718 and make this cancel echoing. */
9720 void
9721 message3_nolog (Lisp_Object m)
9723 struct frame *sf = SELECTED_FRAME ();
9725 if (FRAME_INITIAL_P (sf))
9727 if (noninteractive_need_newline)
9728 putc ('\n', stderr);
9729 noninteractive_need_newline = 0;
9730 if (STRINGP (m))
9732 Lisp_Object s = ENCODE_SYSTEM (m);
9734 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9736 if (cursor_in_echo_area == 0)
9737 fprintf (stderr, "\n");
9738 fflush (stderr);
9740 /* Error messages get reported properly by cmd_error, so this must be just an
9741 informative message; if the frame hasn't really been initialized yet, just
9742 toss it. */
9743 else if (INTERACTIVE && sf->glyphs_initialized_p)
9745 /* Get the frame containing the mini-buffer
9746 that the selected frame is using. */
9747 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9748 Lisp_Object frame = XWINDOW (mini_window)->frame;
9749 struct frame *f = XFRAME (frame);
9751 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9752 Fmake_frame_visible (frame);
9754 if (STRINGP (m) && SCHARS (m) > 0)
9756 set_message (m);
9757 if (minibuffer_auto_raise)
9758 Fraise_frame (frame);
9759 /* Assume we are not echoing.
9760 (If we are, echo_now will override this.) */
9761 echo_message_buffer = Qnil;
9763 else
9764 clear_message (1, 1);
9766 do_pending_window_change (0);
9767 echo_area_display (1);
9768 do_pending_window_change (0);
9769 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9770 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9775 /* Display a null-terminated echo area message M. If M is 0, clear
9776 out any existing message, and let the mini-buffer text show through.
9778 The buffer M must continue to exist until after the echo area gets
9779 cleared or some other message gets displayed there. Do not pass
9780 text that is stored in a Lisp string. Do not pass text in a buffer
9781 that was alloca'd. */
9783 void
9784 message1 (const char *m)
9786 message3 (m ? build_unibyte_string (m) : Qnil);
9790 /* The non-logging counterpart of message1. */
9792 void
9793 message1_nolog (const char *m)
9795 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9798 /* Display a message M which contains a single %s
9799 which gets replaced with STRING. */
9801 void
9802 message_with_string (const char *m, Lisp_Object string, int log)
9804 CHECK_STRING (string);
9806 if (noninteractive)
9808 if (m)
9810 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9811 String whose data pointer might be passed to us in M. So
9812 we use a local copy. */
9813 char *fmt = xstrdup (m);
9815 if (noninteractive_need_newline)
9816 putc ('\n', stderr);
9817 noninteractive_need_newline = 0;
9818 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9819 if (!cursor_in_echo_area)
9820 fprintf (stderr, "\n");
9821 fflush (stderr);
9822 xfree (fmt);
9825 else if (INTERACTIVE)
9827 /* The frame whose minibuffer we're going to display the message on.
9828 It may be larger than the selected frame, so we need
9829 to use its buffer, not the selected frame's buffer. */
9830 Lisp_Object mini_window;
9831 struct frame *f, *sf = SELECTED_FRAME ();
9833 /* Get the frame containing the minibuffer
9834 that the selected frame is using. */
9835 mini_window = FRAME_MINIBUF_WINDOW (sf);
9836 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9838 /* Error messages get reported properly by cmd_error, so this must be
9839 just an informative message; if the frame hasn't really been
9840 initialized yet, just toss it. */
9841 if (f->glyphs_initialized_p)
9843 Lisp_Object args[2], msg;
9844 struct gcpro gcpro1, gcpro2;
9846 args[0] = build_string (m);
9847 args[1] = msg = string;
9848 GCPRO2 (args[0], msg);
9849 gcpro1.nvars = 2;
9851 msg = Fformat (2, args);
9853 if (log)
9854 message3 (msg);
9855 else
9856 message3_nolog (msg);
9858 UNGCPRO;
9860 /* Print should start at the beginning of the message
9861 buffer next time. */
9862 message_buf_print = 0;
9868 /* Dump an informative message to the minibuf. If M is 0, clear out
9869 any existing message, and let the mini-buffer text show through. */
9871 static void
9872 vmessage (const char *m, va_list ap)
9874 if (noninteractive)
9876 if (m)
9878 if (noninteractive_need_newline)
9879 putc ('\n', stderr);
9880 noninteractive_need_newline = 0;
9881 vfprintf (stderr, m, ap);
9882 if (cursor_in_echo_area == 0)
9883 fprintf (stderr, "\n");
9884 fflush (stderr);
9887 else if (INTERACTIVE)
9889 /* The frame whose mini-buffer we're going to display the message
9890 on. It may be larger than the selected frame, so we need to
9891 use its buffer, not the selected frame's buffer. */
9892 Lisp_Object mini_window;
9893 struct frame *f, *sf = SELECTED_FRAME ();
9895 /* Get the frame containing the mini-buffer
9896 that the selected frame is using. */
9897 mini_window = FRAME_MINIBUF_WINDOW (sf);
9898 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9900 /* Error messages get reported properly by cmd_error, so this must be
9901 just an informative message; if the frame hasn't really been
9902 initialized yet, just toss it. */
9903 if (f->glyphs_initialized_p)
9905 if (m)
9907 ptrdiff_t len;
9908 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9909 char *message_buf = alloca (maxsize + 1);
9911 len = doprnt (message_buf, maxsize, m, 0, ap);
9913 message3 (make_string (message_buf, len));
9915 else
9916 message1 (0);
9918 /* Print should start at the beginning of the message
9919 buffer next time. */
9920 message_buf_print = 0;
9925 void
9926 message (const char *m, ...)
9928 va_list ap;
9929 va_start (ap, m);
9930 vmessage (m, ap);
9931 va_end (ap);
9935 #if 0
9936 /* The non-logging version of message. */
9938 void
9939 message_nolog (const char *m, ...)
9941 Lisp_Object old_log_max;
9942 va_list ap;
9943 va_start (ap, m);
9944 old_log_max = Vmessage_log_max;
9945 Vmessage_log_max = Qnil;
9946 vmessage (m, ap);
9947 Vmessage_log_max = old_log_max;
9948 va_end (ap);
9950 #endif
9953 /* Display the current message in the current mini-buffer. This is
9954 only called from error handlers in process.c, and is not time
9955 critical. */
9957 void
9958 update_echo_area (void)
9960 if (!NILP (echo_area_buffer[0]))
9962 Lisp_Object string;
9963 string = Fcurrent_message ();
9964 message3 (string);
9969 /* Make sure echo area buffers in `echo_buffers' are live.
9970 If they aren't, make new ones. */
9972 static void
9973 ensure_echo_area_buffers (void)
9975 int i;
9977 for (i = 0; i < 2; ++i)
9978 if (!BUFFERP (echo_buffer[i])
9979 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9981 char name[30];
9982 Lisp_Object old_buffer;
9983 int j;
9985 old_buffer = echo_buffer[i];
9986 echo_buffer[i] = Fget_buffer_create
9987 (make_formatted_string (name, " *Echo Area %d*", i));
9988 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9989 /* to force word wrap in echo area -
9990 it was decided to postpone this*/
9991 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9993 for (j = 0; j < 2; ++j)
9994 if (EQ (old_buffer, echo_area_buffer[j]))
9995 echo_area_buffer[j] = echo_buffer[i];
10000 /* Call FN with args A1..A2 with either the current or last displayed
10001 echo_area_buffer as current buffer.
10003 WHICH zero means use the current message buffer
10004 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10005 from echo_buffer[] and clear it.
10007 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10008 suitable buffer from echo_buffer[] and clear it.
10010 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10011 that the current message becomes the last displayed one, make
10012 choose a suitable buffer for echo_area_buffer[0], and clear it.
10014 Value is what FN returns. */
10016 static int
10017 with_echo_area_buffer (struct window *w, int which,
10018 int (*fn) (ptrdiff_t, Lisp_Object),
10019 ptrdiff_t a1, Lisp_Object a2)
10021 Lisp_Object buffer;
10022 int this_one, the_other, clear_buffer_p, rc;
10023 ptrdiff_t count = SPECPDL_INDEX ();
10025 /* If buffers aren't live, make new ones. */
10026 ensure_echo_area_buffers ();
10028 clear_buffer_p = 0;
10030 if (which == 0)
10031 this_one = 0, the_other = 1;
10032 else if (which > 0)
10033 this_one = 1, the_other = 0;
10034 else
10036 this_one = 0, the_other = 1;
10037 clear_buffer_p = 1;
10039 /* We need a fresh one in case the current echo buffer equals
10040 the one containing the last displayed echo area message. */
10041 if (!NILP (echo_area_buffer[this_one])
10042 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10043 echo_area_buffer[this_one] = Qnil;
10046 /* Choose a suitable buffer from echo_buffer[] is we don't
10047 have one. */
10048 if (NILP (echo_area_buffer[this_one]))
10050 echo_area_buffer[this_one]
10051 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10052 ? echo_buffer[the_other]
10053 : echo_buffer[this_one]);
10054 clear_buffer_p = 1;
10057 buffer = echo_area_buffer[this_one];
10059 /* Don't get confused by reusing the buffer used for echoing
10060 for a different purpose. */
10061 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10062 cancel_echoing ();
10064 record_unwind_protect (unwind_with_echo_area_buffer,
10065 with_echo_area_buffer_unwind_data (w));
10067 /* Make the echo area buffer current. Note that for display
10068 purposes, it is not necessary that the displayed window's buffer
10069 == current_buffer, except for text property lookup. So, let's
10070 only set that buffer temporarily here without doing a full
10071 Fset_window_buffer. We must also change w->pointm, though,
10072 because otherwise an assertions in unshow_buffer fails, and Emacs
10073 aborts. */
10074 set_buffer_internal_1 (XBUFFER (buffer));
10075 if (w)
10077 wset_buffer (w, buffer);
10078 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10081 bset_undo_list (current_buffer, Qt);
10082 bset_read_only (current_buffer, Qnil);
10083 specbind (Qinhibit_read_only, Qt);
10084 specbind (Qinhibit_modification_hooks, Qt);
10086 if (clear_buffer_p && Z > BEG)
10087 del_range (BEG, Z);
10089 eassert (BEGV >= BEG);
10090 eassert (ZV <= Z && ZV >= BEGV);
10092 rc = fn (a1, a2);
10094 eassert (BEGV >= BEG);
10095 eassert (ZV <= Z && ZV >= BEGV);
10097 unbind_to (count, Qnil);
10098 return rc;
10102 /* Save state that should be preserved around the call to the function
10103 FN called in with_echo_area_buffer. */
10105 static Lisp_Object
10106 with_echo_area_buffer_unwind_data (struct window *w)
10108 int i = 0;
10109 Lisp_Object vector, tmp;
10111 /* Reduce consing by keeping one vector in
10112 Vwith_echo_area_save_vector. */
10113 vector = Vwith_echo_area_save_vector;
10114 Vwith_echo_area_save_vector = Qnil;
10116 if (NILP (vector))
10117 vector = Fmake_vector (make_number (9), Qnil);
10119 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10120 ASET (vector, i, Vdeactivate_mark); ++i;
10121 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10123 if (w)
10125 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10126 ASET (vector, i, w->contents); ++i;
10127 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10128 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10129 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10130 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10132 else
10134 int end = i + 6;
10135 for (; i < end; ++i)
10136 ASET (vector, i, Qnil);
10139 eassert (i == ASIZE (vector));
10140 return vector;
10144 /* Restore global state from VECTOR which was created by
10145 with_echo_area_buffer_unwind_data. */
10147 static void
10148 unwind_with_echo_area_buffer (Lisp_Object vector)
10150 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10151 Vdeactivate_mark = AREF (vector, 1);
10152 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10154 if (WINDOWP (AREF (vector, 3)))
10156 struct window *w;
10157 Lisp_Object buffer;
10159 w = XWINDOW (AREF (vector, 3));
10160 buffer = AREF (vector, 4);
10162 wset_buffer (w, buffer);
10163 set_marker_both (w->pointm, buffer,
10164 XFASTINT (AREF (vector, 5)),
10165 XFASTINT (AREF (vector, 6)));
10166 set_marker_both (w->start, buffer,
10167 XFASTINT (AREF (vector, 7)),
10168 XFASTINT (AREF (vector, 8)));
10171 Vwith_echo_area_save_vector = vector;
10175 /* Set up the echo area for use by print functions. MULTIBYTE_P
10176 non-zero means we will print multibyte. */
10178 void
10179 setup_echo_area_for_printing (int multibyte_p)
10181 /* If we can't find an echo area any more, exit. */
10182 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10183 Fkill_emacs (Qnil);
10185 ensure_echo_area_buffers ();
10187 if (!message_buf_print)
10189 /* A message has been output since the last time we printed.
10190 Choose a fresh echo area buffer. */
10191 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10192 echo_area_buffer[0] = echo_buffer[1];
10193 else
10194 echo_area_buffer[0] = echo_buffer[0];
10196 /* Switch to that buffer and clear it. */
10197 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10198 bset_truncate_lines (current_buffer, Qnil);
10200 if (Z > BEG)
10202 ptrdiff_t count = SPECPDL_INDEX ();
10203 specbind (Qinhibit_read_only, Qt);
10204 /* Note that undo recording is always disabled. */
10205 del_range (BEG, Z);
10206 unbind_to (count, Qnil);
10208 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10210 /* Set up the buffer for the multibyteness we need. */
10211 if (multibyte_p
10212 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10213 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10215 /* Raise the frame containing the echo area. */
10216 if (minibuffer_auto_raise)
10218 struct frame *sf = SELECTED_FRAME ();
10219 Lisp_Object mini_window;
10220 mini_window = FRAME_MINIBUF_WINDOW (sf);
10221 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10224 message_log_maybe_newline ();
10225 message_buf_print = 1;
10227 else
10229 if (NILP (echo_area_buffer[0]))
10231 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10232 echo_area_buffer[0] = echo_buffer[1];
10233 else
10234 echo_area_buffer[0] = echo_buffer[0];
10237 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10239 /* Someone switched buffers between print requests. */
10240 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10241 bset_truncate_lines (current_buffer, Qnil);
10247 /* Display an echo area message in window W. Value is non-zero if W's
10248 height is changed. If display_last_displayed_message_p is
10249 non-zero, display the message that was last displayed, otherwise
10250 display the current message. */
10252 static int
10253 display_echo_area (struct window *w)
10255 int i, no_message_p, window_height_changed_p;
10257 /* Temporarily disable garbage collections while displaying the echo
10258 area. This is done because a GC can print a message itself.
10259 That message would modify the echo area buffer's contents while a
10260 redisplay of the buffer is going on, and seriously confuse
10261 redisplay. */
10262 ptrdiff_t count = inhibit_garbage_collection ();
10264 /* If there is no message, we must call display_echo_area_1
10265 nevertheless because it resizes the window. But we will have to
10266 reset the echo_area_buffer in question to nil at the end because
10267 with_echo_area_buffer will sets it to an empty buffer. */
10268 i = display_last_displayed_message_p ? 1 : 0;
10269 no_message_p = NILP (echo_area_buffer[i]);
10271 window_height_changed_p
10272 = with_echo_area_buffer (w, display_last_displayed_message_p,
10273 display_echo_area_1,
10274 (intptr_t) w, Qnil);
10276 if (no_message_p)
10277 echo_area_buffer[i] = Qnil;
10279 unbind_to (count, Qnil);
10280 return window_height_changed_p;
10284 /* Helper for display_echo_area. Display the current buffer which
10285 contains the current echo area message in window W, a mini-window,
10286 a pointer to which is passed in A1. A2..A4 are currently not used.
10287 Change the height of W so that all of the message is displayed.
10288 Value is non-zero if height of W was changed. */
10290 static int
10291 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10293 intptr_t i1 = a1;
10294 struct window *w = (struct window *) i1;
10295 Lisp_Object window;
10296 struct text_pos start;
10297 int window_height_changed_p = 0;
10299 /* Do this before displaying, so that we have a large enough glyph
10300 matrix for the display. If we can't get enough space for the
10301 whole text, display the last N lines. That works by setting w->start. */
10302 window_height_changed_p = resize_mini_window (w, 0);
10304 /* Use the starting position chosen by resize_mini_window. */
10305 SET_TEXT_POS_FROM_MARKER (start, w->start);
10307 /* Display. */
10308 clear_glyph_matrix (w->desired_matrix);
10309 XSETWINDOW (window, w);
10310 try_window (window, start, 0);
10312 return window_height_changed_p;
10316 /* Resize the echo area window to exactly the size needed for the
10317 currently displayed message, if there is one. If a mini-buffer
10318 is active, don't shrink it. */
10320 void
10321 resize_echo_area_exactly (void)
10323 if (BUFFERP (echo_area_buffer[0])
10324 && WINDOWP (echo_area_window))
10326 struct window *w = XWINDOW (echo_area_window);
10327 int resized_p;
10328 Lisp_Object resize_exactly;
10330 if (minibuf_level == 0)
10331 resize_exactly = Qt;
10332 else
10333 resize_exactly = Qnil;
10335 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10336 (intptr_t) w, resize_exactly);
10337 if (resized_p)
10339 ++windows_or_buffers_changed;
10340 ++update_mode_lines;
10341 redisplay_internal ();
10347 /* Callback function for with_echo_area_buffer, when used from
10348 resize_echo_area_exactly. A1 contains a pointer to the window to
10349 resize, EXACTLY non-nil means resize the mini-window exactly to the
10350 size of the text displayed. A3 and A4 are not used. Value is what
10351 resize_mini_window returns. */
10353 static int
10354 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10356 intptr_t i1 = a1;
10357 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10361 /* Resize mini-window W to fit the size of its contents. EXACT_P
10362 means size the window exactly to the size needed. Otherwise, it's
10363 only enlarged until W's buffer is empty.
10365 Set W->start to the right place to begin display. If the whole
10366 contents fit, start at the beginning. Otherwise, start so as
10367 to make the end of the contents appear. This is particularly
10368 important for y-or-n-p, but seems desirable generally.
10370 Value is non-zero if the window height has been changed. */
10373 resize_mini_window (struct window *w, int exact_p)
10375 struct frame *f = XFRAME (w->frame);
10376 int window_height_changed_p = 0;
10378 eassert (MINI_WINDOW_P (w));
10380 /* By default, start display at the beginning. */
10381 set_marker_both (w->start, w->contents,
10382 BUF_BEGV (XBUFFER (w->contents)),
10383 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10385 /* Don't resize windows while redisplaying a window; it would
10386 confuse redisplay functions when the size of the window they are
10387 displaying changes from under them. Such a resizing can happen,
10388 for instance, when which-func prints a long message while
10389 we are running fontification-functions. We're running these
10390 functions with safe_call which binds inhibit-redisplay to t. */
10391 if (!NILP (Vinhibit_redisplay))
10392 return 0;
10394 /* Nil means don't try to resize. */
10395 if (NILP (Vresize_mini_windows)
10396 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10397 return 0;
10399 if (!FRAME_MINIBUF_ONLY_P (f))
10401 struct it it;
10402 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10403 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10404 int height;
10405 EMACS_INT max_height;
10406 int unit = FRAME_LINE_HEIGHT (f);
10407 struct text_pos start;
10408 struct buffer *old_current_buffer = NULL;
10410 if (current_buffer != XBUFFER (w->contents))
10412 old_current_buffer = current_buffer;
10413 set_buffer_internal (XBUFFER (w->contents));
10416 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10418 /* Compute the max. number of lines specified by the user. */
10419 if (FLOATP (Vmax_mini_window_height))
10420 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10421 else if (INTEGERP (Vmax_mini_window_height))
10422 max_height = XINT (Vmax_mini_window_height);
10423 else
10424 max_height = total_height / 4;
10426 /* Correct that max. height if it's bogus. */
10427 max_height = clip_to_bounds (1, max_height, total_height);
10429 /* Find out the height of the text in the window. */
10430 if (it.line_wrap == TRUNCATE)
10431 height = 1;
10432 else
10434 last_height = 0;
10435 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10436 if (it.max_ascent == 0 && it.max_descent == 0)
10437 height = it.current_y + last_height;
10438 else
10439 height = it.current_y + it.max_ascent + it.max_descent;
10440 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10441 height = (height + unit - 1) / unit;
10444 /* Compute a suitable window start. */
10445 if (height > max_height)
10447 height = max_height;
10448 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10449 move_it_vertically_backward (&it, (height - 1) * unit);
10450 start = it.current.pos;
10452 else
10453 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10454 SET_MARKER_FROM_TEXT_POS (w->start, start);
10456 if (EQ (Vresize_mini_windows, Qgrow_only))
10458 /* Let it grow only, until we display an empty message, in which
10459 case the window shrinks again. */
10460 if (height > WINDOW_TOTAL_LINES (w))
10462 int old_height = WINDOW_TOTAL_LINES (w);
10464 FRAME_WINDOWS_FROZEN (f) = 1;
10465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10468 else if (height < WINDOW_TOTAL_LINES (w)
10469 && (exact_p || BEGV == ZV))
10471 int old_height = WINDOW_TOTAL_LINES (w);
10473 FRAME_WINDOWS_FROZEN (f) = 0;
10474 shrink_mini_window (w);
10475 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10478 else
10480 /* Always resize to exact size needed. */
10481 if (height > WINDOW_TOTAL_LINES (w))
10483 int old_height = WINDOW_TOTAL_LINES (w);
10485 FRAME_WINDOWS_FROZEN (f) = 1;
10486 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10487 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10489 else if (height < WINDOW_TOTAL_LINES (w))
10491 int old_height = WINDOW_TOTAL_LINES (w);
10493 FRAME_WINDOWS_FROZEN (f) = 0;
10494 shrink_mini_window (w);
10496 if (height)
10498 FRAME_WINDOWS_FROZEN (f) = 1;
10499 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10502 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10506 if (old_current_buffer)
10507 set_buffer_internal (old_current_buffer);
10510 return window_height_changed_p;
10514 /* Value is the current message, a string, or nil if there is no
10515 current message. */
10517 Lisp_Object
10518 current_message (void)
10520 Lisp_Object msg;
10522 if (!BUFFERP (echo_area_buffer[0]))
10523 msg = Qnil;
10524 else
10526 with_echo_area_buffer (0, 0, current_message_1,
10527 (intptr_t) &msg, Qnil);
10528 if (NILP (msg))
10529 echo_area_buffer[0] = Qnil;
10532 return msg;
10536 static int
10537 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10539 intptr_t i1 = a1;
10540 Lisp_Object *msg = (Lisp_Object *) i1;
10542 if (Z > BEG)
10543 *msg = make_buffer_string (BEG, Z, 1);
10544 else
10545 *msg = Qnil;
10546 return 0;
10550 /* Push the current message on Vmessage_stack for later restoration
10551 by restore_message. Value is non-zero if the current message isn't
10552 empty. This is a relatively infrequent operation, so it's not
10553 worth optimizing. */
10555 bool
10556 push_message (void)
10558 Lisp_Object msg = current_message ();
10559 Vmessage_stack = Fcons (msg, Vmessage_stack);
10560 return STRINGP (msg);
10564 /* Restore message display from the top of Vmessage_stack. */
10566 void
10567 restore_message (void)
10569 eassert (CONSP (Vmessage_stack));
10570 message3_nolog (XCAR (Vmessage_stack));
10574 /* Handler for unwind-protect calling pop_message. */
10576 void
10577 pop_message_unwind (void)
10579 /* Pop the top-most entry off Vmessage_stack. */
10580 eassert (CONSP (Vmessage_stack));
10581 Vmessage_stack = XCDR (Vmessage_stack);
10585 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10586 exits. If the stack is not empty, we have a missing pop_message
10587 somewhere. */
10589 void
10590 check_message_stack (void)
10592 if (!NILP (Vmessage_stack))
10593 emacs_abort ();
10597 /* Truncate to NCHARS what will be displayed in the echo area the next
10598 time we display it---but don't redisplay it now. */
10600 void
10601 truncate_echo_area (ptrdiff_t nchars)
10603 if (nchars == 0)
10604 echo_area_buffer[0] = Qnil;
10605 else if (!noninteractive
10606 && INTERACTIVE
10607 && !NILP (echo_area_buffer[0]))
10609 struct frame *sf = SELECTED_FRAME ();
10610 /* Error messages get reported properly by cmd_error, so this must be
10611 just an informative message; if the frame hasn't really been
10612 initialized yet, just toss it. */
10613 if (sf->glyphs_initialized_p)
10614 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10619 /* Helper function for truncate_echo_area. Truncate the current
10620 message to at most NCHARS characters. */
10622 static int
10623 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10625 if (BEG + nchars < Z)
10626 del_range (BEG + nchars, Z);
10627 if (Z == BEG)
10628 echo_area_buffer[0] = Qnil;
10629 return 0;
10632 /* Set the current message to STRING. */
10634 static void
10635 set_message (Lisp_Object string)
10637 eassert (STRINGP (string));
10639 message_enable_multibyte = STRING_MULTIBYTE (string);
10641 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10642 message_buf_print = 0;
10643 help_echo_showing_p = 0;
10645 if (STRINGP (Vdebug_on_message)
10646 && STRINGP (string)
10647 && fast_string_match (Vdebug_on_message, string) >= 0)
10648 call_debugger (list2 (Qerror, string));
10652 /* Helper function for set_message. First argument is ignored and second
10653 argument has the same meaning as for set_message.
10654 This function is called with the echo area buffer being current. */
10656 static int
10657 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10659 eassert (STRINGP (string));
10661 /* Change multibyteness of the echo buffer appropriately. */
10662 if (message_enable_multibyte
10663 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10664 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10666 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10667 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10668 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10670 /* Insert new message at BEG. */
10671 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10673 /* This function takes care of single/multibyte conversion.
10674 We just have to ensure that the echo area buffer has the right
10675 setting of enable_multibyte_characters. */
10676 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10678 return 0;
10682 /* Clear messages. CURRENT_P non-zero means clear the current
10683 message. LAST_DISPLAYED_P non-zero means clear the message
10684 last displayed. */
10686 void
10687 clear_message (int current_p, int last_displayed_p)
10689 if (current_p)
10691 echo_area_buffer[0] = Qnil;
10692 message_cleared_p = 1;
10695 if (last_displayed_p)
10696 echo_area_buffer[1] = Qnil;
10698 message_buf_print = 0;
10701 /* Clear garbaged frames.
10703 This function is used where the old redisplay called
10704 redraw_garbaged_frames which in turn called redraw_frame which in
10705 turn called clear_frame. The call to clear_frame was a source of
10706 flickering. I believe a clear_frame is not necessary. It should
10707 suffice in the new redisplay to invalidate all current matrices,
10708 and ensure a complete redisplay of all windows. */
10710 static void
10711 clear_garbaged_frames (void)
10713 if (frame_garbaged)
10715 Lisp_Object tail, frame;
10716 int changed_count = 0;
10718 FOR_EACH_FRAME (tail, frame)
10720 struct frame *f = XFRAME (frame);
10722 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10724 if (f->resized_p)
10725 redraw_frame (f);
10726 else
10727 clear_current_matrices (f);
10728 changed_count++;
10729 f->garbaged = 0;
10730 f->resized_p = 0;
10734 frame_garbaged = 0;
10735 if (changed_count)
10736 ++windows_or_buffers_changed;
10741 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10742 is non-zero update selected_frame. Value is non-zero if the
10743 mini-windows height has been changed. */
10745 static int
10746 echo_area_display (int update_frame_p)
10748 Lisp_Object mini_window;
10749 struct window *w;
10750 struct frame *f;
10751 int window_height_changed_p = 0;
10752 struct frame *sf = SELECTED_FRAME ();
10754 mini_window = FRAME_MINIBUF_WINDOW (sf);
10755 w = XWINDOW (mini_window);
10756 f = XFRAME (WINDOW_FRAME (w));
10758 /* Don't display if frame is invisible or not yet initialized. */
10759 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10760 return 0;
10762 #ifdef HAVE_WINDOW_SYSTEM
10763 /* When Emacs starts, selected_frame may be the initial terminal
10764 frame. If we let this through, a message would be displayed on
10765 the terminal. */
10766 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10767 return 0;
10768 #endif /* HAVE_WINDOW_SYSTEM */
10770 /* Redraw garbaged frames. */
10771 clear_garbaged_frames ();
10773 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10775 echo_area_window = mini_window;
10776 window_height_changed_p = display_echo_area (w);
10777 w->must_be_updated_p = 1;
10779 /* Update the display, unless called from redisplay_internal.
10780 Also don't update the screen during redisplay itself. The
10781 update will happen at the end of redisplay, and an update
10782 here could cause confusion. */
10783 if (update_frame_p && !redisplaying_p)
10785 int n = 0;
10787 /* If the display update has been interrupted by pending
10788 input, update mode lines in the frame. Due to the
10789 pending input, it might have been that redisplay hasn't
10790 been called, so that mode lines above the echo area are
10791 garbaged. This looks odd, so we prevent it here. */
10792 if (!display_completed)
10793 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10795 if (window_height_changed_p
10796 /* Don't do this if Emacs is shutting down. Redisplay
10797 needs to run hooks. */
10798 && !NILP (Vrun_hooks))
10800 /* Must update other windows. Likewise as in other
10801 cases, don't let this update be interrupted by
10802 pending input. */
10803 ptrdiff_t count = SPECPDL_INDEX ();
10804 specbind (Qredisplay_dont_pause, Qt);
10805 windows_or_buffers_changed = 1;
10806 redisplay_internal ();
10807 unbind_to (count, Qnil);
10809 else if (FRAME_WINDOW_P (f) && n == 0)
10811 /* Window configuration is the same as before.
10812 Can do with a display update of the echo area,
10813 unless we displayed some mode lines. */
10814 update_single_window (w, 1);
10815 flush_frame (f);
10817 else
10818 update_frame (f, 1, 1);
10820 /* If cursor is in the echo area, make sure that the next
10821 redisplay displays the minibuffer, so that the cursor will
10822 be replaced with what the minibuffer wants. */
10823 if (cursor_in_echo_area)
10824 ++windows_or_buffers_changed;
10827 else if (!EQ (mini_window, selected_window))
10828 windows_or_buffers_changed++;
10830 /* Last displayed message is now the current message. */
10831 echo_area_buffer[1] = echo_area_buffer[0];
10832 /* Inform read_char that we're not echoing. */
10833 echo_message_buffer = Qnil;
10835 /* Prevent redisplay optimization in redisplay_internal by resetting
10836 this_line_start_pos. This is done because the mini-buffer now
10837 displays the message instead of its buffer text. */
10838 if (EQ (mini_window, selected_window))
10839 CHARPOS (this_line_start_pos) = 0;
10841 return window_height_changed_p;
10844 /* Nonzero if the current window's buffer is shown in more than one
10845 window and was modified since last redisplay. */
10847 static int
10848 buffer_shared_and_changed (void)
10850 return (buffer_window_count (current_buffer) > 1
10851 && UNCHANGED_MODIFIED < MODIFF);
10854 /* Nonzero if W's buffer was changed but not saved. */
10856 static int
10857 window_buffer_changed (struct window *w)
10859 struct buffer *b = XBUFFER (w->contents);
10861 eassert (BUFFER_LIVE_P (b));
10863 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10866 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10868 static int
10869 mode_line_update_needed (struct window *w)
10871 return (w->column_number_displayed != -1
10872 && !(PT == w->last_point && !window_outdated (w))
10873 && (w->column_number_displayed != current_column ()));
10876 /* Nonzero if window start of W is frozen and may not be changed during
10877 redisplay. */
10879 static bool
10880 window_frozen_p (struct window *w)
10882 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10884 Lisp_Object window;
10886 XSETWINDOW (window, w);
10887 if (MINI_WINDOW_P (w))
10888 return 0;
10889 else if (EQ (window, selected_window))
10890 return 0;
10891 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10892 && EQ (window, Vminibuf_scroll_window))
10893 /* This special window can't be frozen too. */
10894 return 0;
10895 else
10896 return 1;
10898 return 0;
10901 /***********************************************************************
10902 Mode Lines and Frame Titles
10903 ***********************************************************************/
10905 /* A buffer for constructing non-propertized mode-line strings and
10906 frame titles in it; allocated from the heap in init_xdisp and
10907 resized as needed in store_mode_line_noprop_char. */
10909 static char *mode_line_noprop_buf;
10911 /* The buffer's end, and a current output position in it. */
10913 static char *mode_line_noprop_buf_end;
10914 static char *mode_line_noprop_ptr;
10916 #define MODE_LINE_NOPROP_LEN(start) \
10917 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10919 static enum {
10920 MODE_LINE_DISPLAY = 0,
10921 MODE_LINE_TITLE,
10922 MODE_LINE_NOPROP,
10923 MODE_LINE_STRING
10924 } mode_line_target;
10926 /* Alist that caches the results of :propertize.
10927 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10928 static Lisp_Object mode_line_proptrans_alist;
10930 /* List of strings making up the mode-line. */
10931 static Lisp_Object mode_line_string_list;
10933 /* Base face property when building propertized mode line string. */
10934 static Lisp_Object mode_line_string_face;
10935 static Lisp_Object mode_line_string_face_prop;
10938 /* Unwind data for mode line strings */
10940 static Lisp_Object Vmode_line_unwind_vector;
10942 static Lisp_Object
10943 format_mode_line_unwind_data (struct frame *target_frame,
10944 struct buffer *obuf,
10945 Lisp_Object owin,
10946 int save_proptrans)
10948 Lisp_Object vector, tmp;
10950 /* Reduce consing by keeping one vector in
10951 Vwith_echo_area_save_vector. */
10952 vector = Vmode_line_unwind_vector;
10953 Vmode_line_unwind_vector = Qnil;
10955 if (NILP (vector))
10956 vector = Fmake_vector (make_number (10), Qnil);
10958 ASET (vector, 0, make_number (mode_line_target));
10959 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10960 ASET (vector, 2, mode_line_string_list);
10961 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10962 ASET (vector, 4, mode_line_string_face);
10963 ASET (vector, 5, mode_line_string_face_prop);
10965 if (obuf)
10966 XSETBUFFER (tmp, obuf);
10967 else
10968 tmp = Qnil;
10969 ASET (vector, 6, tmp);
10970 ASET (vector, 7, owin);
10971 if (target_frame)
10973 /* Similarly to `with-selected-window', if the operation selects
10974 a window on another frame, we must restore that frame's
10975 selected window, and (for a tty) the top-frame. */
10976 ASET (vector, 8, target_frame->selected_window);
10977 if (FRAME_TERMCAP_P (target_frame))
10978 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10981 return vector;
10984 static void
10985 unwind_format_mode_line (Lisp_Object vector)
10987 Lisp_Object old_window = AREF (vector, 7);
10988 Lisp_Object target_frame_window = AREF (vector, 8);
10989 Lisp_Object old_top_frame = AREF (vector, 9);
10991 mode_line_target = XINT (AREF (vector, 0));
10992 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10993 mode_line_string_list = AREF (vector, 2);
10994 if (! EQ (AREF (vector, 3), Qt))
10995 mode_line_proptrans_alist = AREF (vector, 3);
10996 mode_line_string_face = AREF (vector, 4);
10997 mode_line_string_face_prop = AREF (vector, 5);
10999 /* Select window before buffer, since it may change the buffer. */
11000 if (!NILP (old_window))
11002 /* If the operation that we are unwinding had selected a window
11003 on a different frame, reset its frame-selected-window. For a
11004 text terminal, reset its top-frame if necessary. */
11005 if (!NILP (target_frame_window))
11007 Lisp_Object frame
11008 = WINDOW_FRAME (XWINDOW (target_frame_window));
11010 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11011 Fselect_window (target_frame_window, Qt);
11013 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11014 Fselect_frame (old_top_frame, Qt);
11017 Fselect_window (old_window, Qt);
11020 if (!NILP (AREF (vector, 6)))
11022 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11023 ASET (vector, 6, Qnil);
11026 Vmode_line_unwind_vector = vector;
11030 /* Store a single character C for the frame title in mode_line_noprop_buf.
11031 Re-allocate mode_line_noprop_buf if necessary. */
11033 static void
11034 store_mode_line_noprop_char (char c)
11036 /* If output position has reached the end of the allocated buffer,
11037 increase the buffer's size. */
11038 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11040 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11041 ptrdiff_t size = len;
11042 mode_line_noprop_buf =
11043 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11044 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11045 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11048 *mode_line_noprop_ptr++ = c;
11052 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11053 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11054 characters that yield more columns than PRECISION; PRECISION <= 0
11055 means copy the whole string. Pad with spaces until FIELD_WIDTH
11056 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11057 pad. Called from display_mode_element when it is used to build a
11058 frame title. */
11060 static int
11061 store_mode_line_noprop (const char *string, int field_width, int precision)
11063 const unsigned char *str = (const unsigned char *) string;
11064 int n = 0;
11065 ptrdiff_t dummy, nbytes;
11067 /* Copy at most PRECISION chars from STR. */
11068 nbytes = strlen (string);
11069 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11070 while (nbytes--)
11071 store_mode_line_noprop_char (*str++);
11073 /* Fill up with spaces until FIELD_WIDTH reached. */
11074 while (field_width > 0
11075 && n < field_width)
11077 store_mode_line_noprop_char (' ');
11078 ++n;
11081 return n;
11084 /***********************************************************************
11085 Frame Titles
11086 ***********************************************************************/
11088 #ifdef HAVE_WINDOW_SYSTEM
11090 /* Set the title of FRAME, if it has changed. The title format is
11091 Vicon_title_format if FRAME is iconified, otherwise it is
11092 frame_title_format. */
11094 static void
11095 x_consider_frame_title (Lisp_Object frame)
11097 struct frame *f = XFRAME (frame);
11099 if (FRAME_WINDOW_P (f)
11100 || FRAME_MINIBUF_ONLY_P (f)
11101 || f->explicit_name)
11103 /* Do we have more than one visible frame on this X display? */
11104 Lisp_Object tail, other_frame, fmt;
11105 ptrdiff_t title_start;
11106 char *title;
11107 ptrdiff_t len;
11108 struct it it;
11109 ptrdiff_t count = SPECPDL_INDEX ();
11111 FOR_EACH_FRAME (tail, other_frame)
11113 struct frame *tf = XFRAME (other_frame);
11115 if (tf != f
11116 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11117 && !FRAME_MINIBUF_ONLY_P (tf)
11118 && !EQ (other_frame, tip_frame)
11119 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11120 break;
11123 /* Set global variable indicating that multiple frames exist. */
11124 multiple_frames = CONSP (tail);
11126 /* Switch to the buffer of selected window of the frame. Set up
11127 mode_line_target so that display_mode_element will output into
11128 mode_line_noprop_buf; then display the title. */
11129 record_unwind_protect (unwind_format_mode_line,
11130 format_mode_line_unwind_data
11131 (f, current_buffer, selected_window, 0));
11133 Fselect_window (f->selected_window, Qt);
11134 set_buffer_internal_1
11135 (XBUFFER (XWINDOW (f->selected_window)->contents));
11136 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11138 mode_line_target = MODE_LINE_TITLE;
11139 title_start = MODE_LINE_NOPROP_LEN (0);
11140 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11141 NULL, DEFAULT_FACE_ID);
11142 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11143 len = MODE_LINE_NOPROP_LEN (title_start);
11144 title = mode_line_noprop_buf + title_start;
11145 unbind_to (count, Qnil);
11147 /* Set the title only if it's changed. This avoids consing in
11148 the common case where it hasn't. (If it turns out that we've
11149 already wasted too much time by walking through the list with
11150 display_mode_element, then we might need to optimize at a
11151 higher level than this.) */
11152 if (! STRINGP (f->name)
11153 || SBYTES (f->name) != len
11154 || memcmp (title, SDATA (f->name), len) != 0)
11155 x_implicitly_set_name (f, make_string (title, len), Qnil);
11159 #endif /* not HAVE_WINDOW_SYSTEM */
11162 /***********************************************************************
11163 Menu Bars
11164 ***********************************************************************/
11167 /* Prepare for redisplay by updating menu-bar item lists when
11168 appropriate. This can call eval. */
11170 void
11171 prepare_menu_bars (void)
11173 int all_windows;
11174 struct gcpro gcpro1, gcpro2;
11175 struct frame *f;
11176 Lisp_Object tooltip_frame;
11178 #ifdef HAVE_WINDOW_SYSTEM
11179 tooltip_frame = tip_frame;
11180 #else
11181 tooltip_frame = Qnil;
11182 #endif
11184 /* Update all frame titles based on their buffer names, etc. We do
11185 this before the menu bars so that the buffer-menu will show the
11186 up-to-date frame titles. */
11187 #ifdef HAVE_WINDOW_SYSTEM
11188 if (windows_or_buffers_changed || update_mode_lines)
11190 Lisp_Object tail, frame;
11192 FOR_EACH_FRAME (tail, frame)
11194 f = XFRAME (frame);
11195 if (!EQ (frame, tooltip_frame)
11196 && (FRAME_ICONIFIED_P (f)
11197 || FRAME_VISIBLE_P (f) == 1
11198 /* Exclude TTY frames that are obscured because they
11199 are not the top frame on their console. This is
11200 because x_consider_frame_title actually switches
11201 to the frame, which for TTY frames means it is
11202 marked as garbaged, and will be completely
11203 redrawn on the next redisplay cycle. This causes
11204 TTY frames to be completely redrawn, when there
11205 are more than one of them, even though nothing
11206 should be changed on display. */
11207 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11208 x_consider_frame_title (frame);
11211 #endif /* HAVE_WINDOW_SYSTEM */
11213 /* Update the menu bar item lists, if appropriate. This has to be
11214 done before any actual redisplay or generation of display lines. */
11215 all_windows = (update_mode_lines
11216 || buffer_shared_and_changed ()
11217 || windows_or_buffers_changed);
11219 if (FUNCTIONP (Vpre_redisplay_function))
11220 safe_call1 (Vpre_redisplay_function, all_windows ? Qt : Qnil);
11222 if (all_windows)
11224 Lisp_Object tail, frame;
11225 ptrdiff_t count = SPECPDL_INDEX ();
11226 /* 1 means that update_menu_bar has run its hooks
11227 so any further calls to update_menu_bar shouldn't do so again. */
11228 int menu_bar_hooks_run = 0;
11230 record_unwind_save_match_data ();
11232 FOR_EACH_FRAME (tail, frame)
11234 f = XFRAME (frame);
11236 /* Ignore tooltip frame. */
11237 if (EQ (frame, tooltip_frame))
11238 continue;
11240 /* If a window on this frame changed size, report that to
11241 the user and clear the size-change flag. */
11242 if (FRAME_WINDOW_SIZES_CHANGED (f))
11244 Lisp_Object functions;
11246 /* Clear flag first in case we get an error below. */
11247 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11248 functions = Vwindow_size_change_functions;
11249 GCPRO2 (tail, functions);
11251 while (CONSP (functions))
11253 if (!EQ (XCAR (functions), Qt))
11254 call1 (XCAR (functions), frame);
11255 functions = XCDR (functions);
11257 UNGCPRO;
11260 GCPRO1 (tail);
11261 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11262 #ifdef HAVE_WINDOW_SYSTEM
11263 update_tool_bar (f, 0);
11264 #endif
11265 #ifdef HAVE_NS
11266 if (windows_or_buffers_changed
11267 && FRAME_NS_P (f))
11268 ns_set_doc_edited
11269 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11270 #endif
11271 UNGCPRO;
11274 unbind_to (count, Qnil);
11276 else
11278 struct frame *sf = SELECTED_FRAME ();
11279 update_menu_bar (sf, 1, 0);
11280 #ifdef HAVE_WINDOW_SYSTEM
11281 update_tool_bar (sf, 1);
11282 #endif
11287 /* Update the menu bar item list for frame F. This has to be done
11288 before we start to fill in any display lines, because it can call
11289 eval.
11291 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11293 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11294 already ran the menu bar hooks for this redisplay, so there
11295 is no need to run them again. The return value is the
11296 updated value of this flag, to pass to the next call. */
11298 static int
11299 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11301 Lisp_Object window;
11302 register struct window *w;
11304 /* If called recursively during a menu update, do nothing. This can
11305 happen when, for instance, an activate-menubar-hook causes a
11306 redisplay. */
11307 if (inhibit_menubar_update)
11308 return hooks_run;
11310 window = FRAME_SELECTED_WINDOW (f);
11311 w = XWINDOW (window);
11313 if (FRAME_WINDOW_P (f)
11315 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11316 || defined (HAVE_NS) || defined (USE_GTK)
11317 FRAME_EXTERNAL_MENU_BAR (f)
11318 #else
11319 FRAME_MENU_BAR_LINES (f) > 0
11320 #endif
11321 : FRAME_MENU_BAR_LINES (f) > 0)
11323 /* If the user has switched buffers or windows, we need to
11324 recompute to reflect the new bindings. But we'll
11325 recompute when update_mode_lines is set too; that means
11326 that people can use force-mode-line-update to request
11327 that the menu bar be recomputed. The adverse effect on
11328 the rest of the redisplay algorithm is about the same as
11329 windows_or_buffers_changed anyway. */
11330 if (windows_or_buffers_changed
11331 /* This used to test w->update_mode_line, but we believe
11332 there is no need to recompute the menu in that case. */
11333 || update_mode_lines
11334 || window_buffer_changed (w))
11336 struct buffer *prev = current_buffer;
11337 ptrdiff_t count = SPECPDL_INDEX ();
11339 specbind (Qinhibit_menubar_update, Qt);
11341 set_buffer_internal_1 (XBUFFER (w->contents));
11342 if (save_match_data)
11343 record_unwind_save_match_data ();
11344 if (NILP (Voverriding_local_map_menu_flag))
11346 specbind (Qoverriding_terminal_local_map, Qnil);
11347 specbind (Qoverriding_local_map, Qnil);
11350 if (!hooks_run)
11352 /* Run the Lucid hook. */
11353 safe_run_hooks (Qactivate_menubar_hook);
11355 /* If it has changed current-menubar from previous value,
11356 really recompute the menu-bar from the value. */
11357 if (! NILP (Vlucid_menu_bar_dirty_flag))
11358 call0 (Qrecompute_lucid_menubar);
11360 safe_run_hooks (Qmenu_bar_update_hook);
11362 hooks_run = 1;
11365 XSETFRAME (Vmenu_updating_frame, f);
11366 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11368 /* Redisplay the menu bar in case we changed it. */
11369 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11370 || defined (HAVE_NS) || defined (USE_GTK)
11371 if (FRAME_WINDOW_P (f))
11373 #if defined (HAVE_NS)
11374 /* All frames on Mac OS share the same menubar. So only
11375 the selected frame should be allowed to set it. */
11376 if (f == SELECTED_FRAME ())
11377 #endif
11378 set_frame_menubar (f, 0, 0);
11380 else
11381 /* On a terminal screen, the menu bar is an ordinary screen
11382 line, and this makes it get updated. */
11383 w->update_mode_line = 1;
11384 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11385 /* In the non-toolkit version, the menu bar is an ordinary screen
11386 line, and this makes it get updated. */
11387 w->update_mode_line = 1;
11388 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11390 unbind_to (count, Qnil);
11391 set_buffer_internal_1 (prev);
11395 return hooks_run;
11398 /***********************************************************************
11399 Tool-bars
11400 ***********************************************************************/
11402 #ifdef HAVE_WINDOW_SYSTEM
11404 /* Tool-bar item index of the item on which a mouse button was pressed
11405 or -1. */
11407 int last_tool_bar_item;
11409 /* Select `frame' temporarily without running all the code in
11410 do_switch_frame.
11411 FIXME: Maybe do_switch_frame should be trimmed down similarly
11412 when `norecord' is set. */
11413 static void
11414 fast_set_selected_frame (Lisp_Object frame)
11416 if (!EQ (selected_frame, frame))
11418 selected_frame = frame;
11419 selected_window = XFRAME (frame)->selected_window;
11423 /* Update the tool-bar item list for frame F. This has to be done
11424 before we start to fill in any display lines. Called from
11425 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11426 and restore it here. */
11428 static void
11429 update_tool_bar (struct frame *f, int save_match_data)
11431 #if defined (USE_GTK) || defined (HAVE_NS)
11432 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11433 #else
11434 int do_update = WINDOWP (f->tool_bar_window)
11435 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11436 #endif
11438 if (do_update)
11440 Lisp_Object window;
11441 struct window *w;
11443 window = FRAME_SELECTED_WINDOW (f);
11444 w = XWINDOW (window);
11446 /* If the user has switched buffers or windows, we need to
11447 recompute to reflect the new bindings. But we'll
11448 recompute when update_mode_lines is set too; that means
11449 that people can use force-mode-line-update to request
11450 that the menu bar be recomputed. The adverse effect on
11451 the rest of the redisplay algorithm is about the same as
11452 windows_or_buffers_changed anyway. */
11453 if (windows_or_buffers_changed
11454 || w->update_mode_line
11455 || update_mode_lines
11456 || window_buffer_changed (w))
11458 struct buffer *prev = current_buffer;
11459 ptrdiff_t count = SPECPDL_INDEX ();
11460 Lisp_Object frame, new_tool_bar;
11461 int new_n_tool_bar;
11462 struct gcpro gcpro1;
11464 /* Set current_buffer to the buffer of the selected
11465 window of the frame, so that we get the right local
11466 keymaps. */
11467 set_buffer_internal_1 (XBUFFER (w->contents));
11469 /* Save match data, if we must. */
11470 if (save_match_data)
11471 record_unwind_save_match_data ();
11473 /* Make sure that we don't accidentally use bogus keymaps. */
11474 if (NILP (Voverriding_local_map_menu_flag))
11476 specbind (Qoverriding_terminal_local_map, Qnil);
11477 specbind (Qoverriding_local_map, Qnil);
11480 GCPRO1 (new_tool_bar);
11482 /* We must temporarily set the selected frame to this frame
11483 before calling tool_bar_items, because the calculation of
11484 the tool-bar keymap uses the selected frame (see
11485 `tool-bar-make-keymap' in tool-bar.el). */
11486 eassert (EQ (selected_window,
11487 /* Since we only explicitly preserve selected_frame,
11488 check that selected_window would be redundant. */
11489 XFRAME (selected_frame)->selected_window));
11490 record_unwind_protect (fast_set_selected_frame, selected_frame);
11491 XSETFRAME (frame, f);
11492 fast_set_selected_frame (frame);
11494 /* Build desired tool-bar items from keymaps. */
11495 new_tool_bar
11496 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11497 &new_n_tool_bar);
11499 /* Redisplay the tool-bar if we changed it. */
11500 if (new_n_tool_bar != f->n_tool_bar_items
11501 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11503 /* Redisplay that happens asynchronously due to an expose event
11504 may access f->tool_bar_items. Make sure we update both
11505 variables within BLOCK_INPUT so no such event interrupts. */
11506 block_input ();
11507 fset_tool_bar_items (f, new_tool_bar);
11508 f->n_tool_bar_items = new_n_tool_bar;
11509 w->update_mode_line = 1;
11510 unblock_input ();
11513 UNGCPRO;
11515 unbind_to (count, Qnil);
11516 set_buffer_internal_1 (prev);
11521 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11523 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11524 F's desired tool-bar contents. F->tool_bar_items must have
11525 been set up previously by calling prepare_menu_bars. */
11527 static void
11528 build_desired_tool_bar_string (struct frame *f)
11530 int i, size, size_needed;
11531 struct gcpro gcpro1, gcpro2, gcpro3;
11532 Lisp_Object image, plist, props;
11534 image = plist = props = Qnil;
11535 GCPRO3 (image, plist, props);
11537 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11538 Otherwise, make a new string. */
11540 /* The size of the string we might be able to reuse. */
11541 size = (STRINGP (f->desired_tool_bar_string)
11542 ? SCHARS (f->desired_tool_bar_string)
11543 : 0);
11545 /* We need one space in the string for each image. */
11546 size_needed = f->n_tool_bar_items;
11548 /* Reuse f->desired_tool_bar_string, if possible. */
11549 if (size < size_needed || NILP (f->desired_tool_bar_string))
11550 fset_desired_tool_bar_string
11551 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11552 else
11554 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11555 Fremove_text_properties (make_number (0), make_number (size),
11556 props, f->desired_tool_bar_string);
11559 /* Put a `display' property on the string for the images to display,
11560 put a `menu_item' property on tool-bar items with a value that
11561 is the index of the item in F's tool-bar item vector. */
11562 for (i = 0; i < f->n_tool_bar_items; ++i)
11564 #define PROP(IDX) \
11565 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11567 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11568 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11569 int hmargin, vmargin, relief, idx, end;
11571 /* If image is a vector, choose the image according to the
11572 button state. */
11573 image = PROP (TOOL_BAR_ITEM_IMAGES);
11574 if (VECTORP (image))
11576 if (enabled_p)
11577 idx = (selected_p
11578 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11579 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11580 else
11581 idx = (selected_p
11582 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11583 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11585 eassert (ASIZE (image) >= idx);
11586 image = AREF (image, idx);
11588 else
11589 idx = -1;
11591 /* Ignore invalid image specifications. */
11592 if (!valid_image_p (image))
11593 continue;
11595 /* Display the tool-bar button pressed, or depressed. */
11596 plist = Fcopy_sequence (XCDR (image));
11598 /* Compute margin and relief to draw. */
11599 relief = (tool_bar_button_relief >= 0
11600 ? tool_bar_button_relief
11601 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11602 hmargin = vmargin = relief;
11604 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11605 INT_MAX - max (hmargin, vmargin)))
11607 hmargin += XFASTINT (Vtool_bar_button_margin);
11608 vmargin += XFASTINT (Vtool_bar_button_margin);
11610 else if (CONSP (Vtool_bar_button_margin))
11612 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11613 INT_MAX - hmargin))
11614 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11616 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11617 INT_MAX - vmargin))
11618 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11621 if (auto_raise_tool_bar_buttons_p)
11623 /* Add a `:relief' property to the image spec if the item is
11624 selected. */
11625 if (selected_p)
11627 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11628 hmargin -= relief;
11629 vmargin -= relief;
11632 else
11634 /* If image is selected, display it pressed, i.e. with a
11635 negative relief. If it's not selected, display it with a
11636 raised relief. */
11637 plist = Fplist_put (plist, QCrelief,
11638 (selected_p
11639 ? make_number (-relief)
11640 : make_number (relief)));
11641 hmargin -= relief;
11642 vmargin -= relief;
11645 /* Put a margin around the image. */
11646 if (hmargin || vmargin)
11648 if (hmargin == vmargin)
11649 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11650 else
11651 plist = Fplist_put (plist, QCmargin,
11652 Fcons (make_number (hmargin),
11653 make_number (vmargin)));
11656 /* If button is not enabled, and we don't have special images
11657 for the disabled state, make the image appear disabled by
11658 applying an appropriate algorithm to it. */
11659 if (!enabled_p && idx < 0)
11660 plist = Fplist_put (plist, QCconversion, Qdisabled);
11662 /* Put a `display' text property on the string for the image to
11663 display. Put a `menu-item' property on the string that gives
11664 the start of this item's properties in the tool-bar items
11665 vector. */
11666 image = Fcons (Qimage, plist);
11667 props = list4 (Qdisplay, image,
11668 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11670 /* Let the last image hide all remaining spaces in the tool bar
11671 string. The string can be longer than needed when we reuse a
11672 previous string. */
11673 if (i + 1 == f->n_tool_bar_items)
11674 end = SCHARS (f->desired_tool_bar_string);
11675 else
11676 end = i + 1;
11677 Fadd_text_properties (make_number (i), make_number (end),
11678 props, f->desired_tool_bar_string);
11679 #undef PROP
11682 UNGCPRO;
11686 /* Display one line of the tool-bar of frame IT->f.
11688 HEIGHT specifies the desired height of the tool-bar line.
11689 If the actual height of the glyph row is less than HEIGHT, the
11690 row's height is increased to HEIGHT, and the icons are centered
11691 vertically in the new height.
11693 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11694 count a final empty row in case the tool-bar width exactly matches
11695 the window width.
11698 static void
11699 display_tool_bar_line (struct it *it, int height)
11701 struct glyph_row *row = it->glyph_row;
11702 int max_x = it->last_visible_x;
11703 struct glyph *last;
11705 prepare_desired_row (row);
11706 row->y = it->current_y;
11708 /* Note that this isn't made use of if the face hasn't a box,
11709 so there's no need to check the face here. */
11710 it->start_of_box_run_p = 1;
11712 while (it->current_x < max_x)
11714 int x, n_glyphs_before, i, nglyphs;
11715 struct it it_before;
11717 /* Get the next display element. */
11718 if (!get_next_display_element (it))
11720 /* Don't count empty row if we are counting needed tool-bar lines. */
11721 if (height < 0 && !it->hpos)
11722 return;
11723 break;
11726 /* Produce glyphs. */
11727 n_glyphs_before = row->used[TEXT_AREA];
11728 it_before = *it;
11730 PRODUCE_GLYPHS (it);
11732 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11733 i = 0;
11734 x = it_before.current_x;
11735 while (i < nglyphs)
11737 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11739 if (x + glyph->pixel_width > max_x)
11741 /* Glyph doesn't fit on line. Backtrack. */
11742 row->used[TEXT_AREA] = n_glyphs_before;
11743 *it = it_before;
11744 /* If this is the only glyph on this line, it will never fit on the
11745 tool-bar, so skip it. But ensure there is at least one glyph,
11746 so we don't accidentally disable the tool-bar. */
11747 if (n_glyphs_before == 0
11748 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11749 break;
11750 goto out;
11753 ++it->hpos;
11754 x += glyph->pixel_width;
11755 ++i;
11758 /* Stop at line end. */
11759 if (ITERATOR_AT_END_OF_LINE_P (it))
11760 break;
11762 set_iterator_to_next (it, 1);
11765 out:;
11767 row->displays_text_p = row->used[TEXT_AREA] != 0;
11769 /* Use default face for the border below the tool bar.
11771 FIXME: When auto-resize-tool-bars is grow-only, there is
11772 no additional border below the possibly empty tool-bar lines.
11773 So to make the extra empty lines look "normal", we have to
11774 use the tool-bar face for the border too. */
11775 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11776 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11777 it->face_id = DEFAULT_FACE_ID;
11779 extend_face_to_end_of_line (it);
11780 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11781 last->right_box_line_p = 1;
11782 if (last == row->glyphs[TEXT_AREA])
11783 last->left_box_line_p = 1;
11785 /* Make line the desired height and center it vertically. */
11786 if ((height -= it->max_ascent + it->max_descent) > 0)
11788 /* Don't add more than one line height. */
11789 height %= FRAME_LINE_HEIGHT (it->f);
11790 it->max_ascent += height / 2;
11791 it->max_descent += (height + 1) / 2;
11794 compute_line_metrics (it);
11796 /* If line is empty, make it occupy the rest of the tool-bar. */
11797 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11799 row->height = row->phys_height = it->last_visible_y - row->y;
11800 row->visible_height = row->height;
11801 row->ascent = row->phys_ascent = 0;
11802 row->extra_line_spacing = 0;
11805 row->full_width_p = 1;
11806 row->continued_p = 0;
11807 row->truncated_on_left_p = 0;
11808 row->truncated_on_right_p = 0;
11810 it->current_x = it->hpos = 0;
11811 it->current_y += row->height;
11812 ++it->vpos;
11813 ++it->glyph_row;
11817 /* Max tool-bar height. */
11819 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11820 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11822 /* Value is the number of screen lines needed to make all tool-bar
11823 items of frame F visible. The number of actual rows needed is
11824 returned in *N_ROWS if non-NULL. */
11826 static int
11827 tool_bar_lines_needed (struct frame *f, int *n_rows)
11829 struct window *w = XWINDOW (f->tool_bar_window);
11830 struct it it;
11831 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11832 the desired matrix, so use (unused) mode-line row as temporary row to
11833 avoid destroying the first tool-bar row. */
11834 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11836 /* Initialize an iterator for iteration over
11837 F->desired_tool_bar_string in the tool-bar window of frame F. */
11838 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11839 it.first_visible_x = 0;
11840 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11841 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11842 it.paragraph_embedding = L2R;
11844 while (!ITERATOR_AT_END_P (&it))
11846 clear_glyph_row (temp_row);
11847 it.glyph_row = temp_row;
11848 display_tool_bar_line (&it, -1);
11850 clear_glyph_row (temp_row);
11852 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11853 if (n_rows)
11854 *n_rows = it.vpos > 0 ? it.vpos : -1;
11856 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11859 #endif /* !USE_GTK && !HAVE_NS */
11861 #if defined USE_GTK || defined HAVE_NS
11862 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11863 #endif
11865 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11866 0, 1, 0,
11867 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11868 If FRAME is nil or omitted, use the selected frame. */)
11869 (Lisp_Object frame)
11871 int nlines = 0;
11872 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11873 struct frame *f = decode_any_frame (frame);
11874 struct window *w;
11876 if (WINDOWP (f->tool_bar_window)
11877 && (w = XWINDOW (f->tool_bar_window),
11878 WINDOW_TOTAL_LINES (w) > 0))
11880 update_tool_bar (f, 1);
11881 if (f->n_tool_bar_items)
11883 build_desired_tool_bar_string (f);
11884 nlines = tool_bar_lines_needed (f, NULL);
11887 #endif
11888 return make_number (nlines);
11892 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11893 height should be changed. */
11895 static int
11896 redisplay_tool_bar (struct frame *f)
11898 #if defined (USE_GTK) || defined (HAVE_NS)
11900 if (FRAME_EXTERNAL_TOOL_BAR (f))
11901 update_frame_tool_bar (f);
11902 return 0;
11904 #else /* !USE_GTK && !HAVE_NS */
11906 struct window *w;
11907 struct it it;
11908 struct glyph_row *row;
11910 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11911 do anything. This means you must start with tool-bar-lines
11912 non-zero to get the auto-sizing effect. Or in other words, you
11913 can turn off tool-bars by specifying tool-bar-lines zero. */
11914 if (!WINDOWP (f->tool_bar_window)
11915 || (w = XWINDOW (f->tool_bar_window),
11916 WINDOW_TOTAL_LINES (w) == 0))
11917 return 0;
11919 /* Set up an iterator for the tool-bar window. */
11920 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11921 it.first_visible_x = 0;
11922 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11923 row = it.glyph_row;
11925 /* Build a string that represents the contents of the tool-bar. */
11926 build_desired_tool_bar_string (f);
11927 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11928 /* FIXME: This should be controlled by a user option. But it
11929 doesn't make sense to have an R2L tool bar if the menu bar cannot
11930 be drawn also R2L, and making the menu bar R2L is tricky due
11931 toolkit-specific code that implements it. If an R2L tool bar is
11932 ever supported, display_tool_bar_line should also be augmented to
11933 call unproduce_glyphs like display_line and display_string
11934 do. */
11935 it.paragraph_embedding = L2R;
11937 if (f->n_tool_bar_rows == 0)
11939 int nlines;
11941 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11942 nlines != WINDOW_TOTAL_LINES (w)))
11944 Lisp_Object frame;
11945 int old_height = WINDOW_TOTAL_LINES (w);
11947 XSETFRAME (frame, f);
11948 Fmodify_frame_parameters (frame,
11949 list1 (Fcons (Qtool_bar_lines,
11950 make_number (nlines))));
11951 if (WINDOW_TOTAL_LINES (w) != old_height)
11953 clear_glyph_matrix (w->desired_matrix);
11954 f->fonts_changed = 1;
11955 return 1;
11960 /* Display as many lines as needed to display all tool-bar items. */
11962 if (f->n_tool_bar_rows > 0)
11964 int border, rows, height, extra;
11966 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11967 border = XINT (Vtool_bar_border);
11968 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11969 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11970 else if (EQ (Vtool_bar_border, Qborder_width))
11971 border = f->border_width;
11972 else
11973 border = 0;
11974 if (border < 0)
11975 border = 0;
11977 rows = f->n_tool_bar_rows;
11978 height = max (1, (it.last_visible_y - border) / rows);
11979 extra = it.last_visible_y - border - height * rows;
11981 while (it.current_y < it.last_visible_y)
11983 int h = 0;
11984 if (extra > 0 && rows-- > 0)
11986 h = (extra + rows - 1) / rows;
11987 extra -= h;
11989 display_tool_bar_line (&it, height + h);
11992 else
11994 while (it.current_y < it.last_visible_y)
11995 display_tool_bar_line (&it, 0);
11998 /* It doesn't make much sense to try scrolling in the tool-bar
11999 window, so don't do it. */
12000 w->desired_matrix->no_scrolling_p = 1;
12001 w->must_be_updated_p = 1;
12003 if (!NILP (Vauto_resize_tool_bars))
12005 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12006 int change_height_p = 0;
12008 /* If we couldn't display everything, change the tool-bar's
12009 height if there is room for more. */
12010 if (IT_STRING_CHARPOS (it) < it.end_charpos
12011 && it.current_y < max_tool_bar_height)
12012 change_height_p = 1;
12014 row = it.glyph_row - 1;
12016 /* If there are blank lines at the end, except for a partially
12017 visible blank line at the end that is smaller than
12018 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12019 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12020 && row->height >= FRAME_LINE_HEIGHT (f))
12021 change_height_p = 1;
12023 /* If row displays tool-bar items, but is partially visible,
12024 change the tool-bar's height. */
12025 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12026 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12027 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12028 change_height_p = 1;
12030 /* Resize windows as needed by changing the `tool-bar-lines'
12031 frame parameter. */
12032 if (change_height_p)
12034 Lisp_Object frame;
12035 int old_height = WINDOW_TOTAL_LINES (w);
12036 int nrows;
12037 int nlines = tool_bar_lines_needed (f, &nrows);
12039 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12040 && !f->minimize_tool_bar_window_p)
12041 ? (nlines > old_height)
12042 : (nlines != old_height));
12043 f->minimize_tool_bar_window_p = 0;
12045 if (change_height_p)
12047 XSETFRAME (frame, f);
12048 Fmodify_frame_parameters (frame,
12049 list1 (Fcons (Qtool_bar_lines,
12050 make_number (nlines))));
12051 if (WINDOW_TOTAL_LINES (w) != old_height)
12053 clear_glyph_matrix (w->desired_matrix);
12054 f->n_tool_bar_rows = nrows;
12055 f->fonts_changed = 1;
12056 return 1;
12062 f->minimize_tool_bar_window_p = 0;
12063 return 0;
12065 #endif /* USE_GTK || HAVE_NS */
12068 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12070 /* Get information about the tool-bar item which is displayed in GLYPH
12071 on frame F. Return in *PROP_IDX the index where tool-bar item
12072 properties start in F->tool_bar_items. Value is zero if
12073 GLYPH doesn't display a tool-bar item. */
12075 static int
12076 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12078 Lisp_Object prop;
12079 int success_p;
12080 int charpos;
12082 /* This function can be called asynchronously, which means we must
12083 exclude any possibility that Fget_text_property signals an
12084 error. */
12085 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12086 charpos = max (0, charpos);
12088 /* Get the text property `menu-item' at pos. The value of that
12089 property is the start index of this item's properties in
12090 F->tool_bar_items. */
12091 prop = Fget_text_property (make_number (charpos),
12092 Qmenu_item, f->current_tool_bar_string);
12093 if (INTEGERP (prop))
12095 *prop_idx = XINT (prop);
12096 success_p = 1;
12098 else
12099 success_p = 0;
12101 return success_p;
12105 /* Get information about the tool-bar item at position X/Y on frame F.
12106 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12107 the current matrix of the tool-bar window of F, or NULL if not
12108 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12109 item in F->tool_bar_items. Value is
12111 -1 if X/Y is not on a tool-bar item
12112 0 if X/Y is on the same item that was highlighted before.
12113 1 otherwise. */
12115 static int
12116 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12117 int *hpos, int *vpos, int *prop_idx)
12119 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12120 struct window *w = XWINDOW (f->tool_bar_window);
12121 int area;
12123 /* Find the glyph under X/Y. */
12124 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12125 if (*glyph == NULL)
12126 return -1;
12128 /* Get the start of this tool-bar item's properties in
12129 f->tool_bar_items. */
12130 if (!tool_bar_item_info (f, *glyph, prop_idx))
12131 return -1;
12133 /* Is mouse on the highlighted item? */
12134 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12135 && *vpos >= hlinfo->mouse_face_beg_row
12136 && *vpos <= hlinfo->mouse_face_end_row
12137 && (*vpos > hlinfo->mouse_face_beg_row
12138 || *hpos >= hlinfo->mouse_face_beg_col)
12139 && (*vpos < hlinfo->mouse_face_end_row
12140 || *hpos < hlinfo->mouse_face_end_col
12141 || hlinfo->mouse_face_past_end))
12142 return 0;
12144 return 1;
12148 /* EXPORT:
12149 Handle mouse button event on the tool-bar of frame F, at
12150 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12151 0 for button release. MODIFIERS is event modifiers for button
12152 release. */
12154 void
12155 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12156 int modifiers)
12158 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12159 struct window *w = XWINDOW (f->tool_bar_window);
12160 int hpos, vpos, prop_idx;
12161 struct glyph *glyph;
12162 Lisp_Object enabled_p;
12163 int ts;
12165 /* If not on the highlighted tool-bar item, and mouse-highlight is
12166 non-nil, return. This is so we generate the tool-bar button
12167 click only when the mouse button is released on the same item as
12168 where it was pressed. However, when mouse-highlight is disabled,
12169 generate the click when the button is released regardless of the
12170 highlight, since tool-bar items are not highlighted in that
12171 case. */
12172 frame_to_window_pixel_xy (w, &x, &y);
12173 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12174 if (ts == -1
12175 || (ts != 0 && !NILP (Vmouse_highlight)))
12176 return;
12178 /* When mouse-highlight is off, generate the click for the item
12179 where the button was pressed, disregarding where it was
12180 released. */
12181 if (NILP (Vmouse_highlight) && !down_p)
12182 prop_idx = last_tool_bar_item;
12184 /* If item is disabled, do nothing. */
12185 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12186 if (NILP (enabled_p))
12187 return;
12189 if (down_p)
12191 /* Show item in pressed state. */
12192 if (!NILP (Vmouse_highlight))
12193 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12194 last_tool_bar_item = prop_idx;
12196 else
12198 Lisp_Object key, frame;
12199 struct input_event event;
12200 EVENT_INIT (event);
12202 /* Show item in released state. */
12203 if (!NILP (Vmouse_highlight))
12204 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12206 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12208 XSETFRAME (frame, f);
12209 event.kind = TOOL_BAR_EVENT;
12210 event.frame_or_window = frame;
12211 event.arg = frame;
12212 kbd_buffer_store_event (&event);
12214 event.kind = TOOL_BAR_EVENT;
12215 event.frame_or_window = frame;
12216 event.arg = key;
12217 event.modifiers = modifiers;
12218 kbd_buffer_store_event (&event);
12219 last_tool_bar_item = -1;
12224 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12225 tool-bar window-relative coordinates X/Y. Called from
12226 note_mouse_highlight. */
12228 static void
12229 note_tool_bar_highlight (struct frame *f, int x, int y)
12231 Lisp_Object window = f->tool_bar_window;
12232 struct window *w = XWINDOW (window);
12233 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12234 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12235 int hpos, vpos;
12236 struct glyph *glyph;
12237 struct glyph_row *row;
12238 int i;
12239 Lisp_Object enabled_p;
12240 int prop_idx;
12241 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12242 int mouse_down_p, rc;
12244 /* Function note_mouse_highlight is called with negative X/Y
12245 values when mouse moves outside of the frame. */
12246 if (x <= 0 || y <= 0)
12248 clear_mouse_face (hlinfo);
12249 return;
12252 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12253 if (rc < 0)
12255 /* Not on tool-bar item. */
12256 clear_mouse_face (hlinfo);
12257 return;
12259 else if (rc == 0)
12260 /* On same tool-bar item as before. */
12261 goto set_help_echo;
12263 clear_mouse_face (hlinfo);
12265 /* Mouse is down, but on different tool-bar item? */
12266 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12267 && f == dpyinfo->last_mouse_frame);
12269 if (mouse_down_p
12270 && last_tool_bar_item != prop_idx)
12271 return;
12273 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12275 /* If tool-bar item is not enabled, don't highlight it. */
12276 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12277 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12279 /* Compute the x-position of the glyph. In front and past the
12280 image is a space. We include this in the highlighted area. */
12281 row = MATRIX_ROW (w->current_matrix, vpos);
12282 for (i = x = 0; i < hpos; ++i)
12283 x += row->glyphs[TEXT_AREA][i].pixel_width;
12285 /* Record this as the current active region. */
12286 hlinfo->mouse_face_beg_col = hpos;
12287 hlinfo->mouse_face_beg_row = vpos;
12288 hlinfo->mouse_face_beg_x = x;
12289 hlinfo->mouse_face_past_end = 0;
12291 hlinfo->mouse_face_end_col = hpos + 1;
12292 hlinfo->mouse_face_end_row = vpos;
12293 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12294 hlinfo->mouse_face_window = window;
12295 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12297 /* Display it as active. */
12298 show_mouse_face (hlinfo, draw);
12301 set_help_echo:
12303 /* Set help_echo_string to a help string to display for this tool-bar item.
12304 XTread_socket does the rest. */
12305 help_echo_object = help_echo_window = Qnil;
12306 help_echo_pos = -1;
12307 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12308 if (NILP (help_echo_string))
12309 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12312 #endif /* !USE_GTK && !HAVE_NS */
12314 #endif /* HAVE_WINDOW_SYSTEM */
12318 /************************************************************************
12319 Horizontal scrolling
12320 ************************************************************************/
12322 static int hscroll_window_tree (Lisp_Object);
12323 static int hscroll_windows (Lisp_Object);
12325 /* For all leaf windows in the window tree rooted at WINDOW, set their
12326 hscroll value so that PT is (i) visible in the window, and (ii) so
12327 that it is not within a certain margin at the window's left and
12328 right border. Value is non-zero if any window's hscroll has been
12329 changed. */
12331 static int
12332 hscroll_window_tree (Lisp_Object window)
12334 int hscrolled_p = 0;
12335 int hscroll_relative_p = FLOATP (Vhscroll_step);
12336 int hscroll_step_abs = 0;
12337 double hscroll_step_rel = 0;
12339 if (hscroll_relative_p)
12341 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12342 if (hscroll_step_rel < 0)
12344 hscroll_relative_p = 0;
12345 hscroll_step_abs = 0;
12348 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12350 hscroll_step_abs = XINT (Vhscroll_step);
12351 if (hscroll_step_abs < 0)
12352 hscroll_step_abs = 0;
12354 else
12355 hscroll_step_abs = 0;
12357 while (WINDOWP (window))
12359 struct window *w = XWINDOW (window);
12361 if (WINDOWP (w->contents))
12362 hscrolled_p |= hscroll_window_tree (w->contents);
12363 else if (w->cursor.vpos >= 0)
12365 int h_margin;
12366 int text_area_width;
12367 struct glyph_row *current_cursor_row
12368 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12369 struct glyph_row *desired_cursor_row
12370 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12371 struct glyph_row *cursor_row
12372 = (desired_cursor_row->enabled_p
12373 ? desired_cursor_row
12374 : current_cursor_row);
12375 int row_r2l_p = cursor_row->reversed_p;
12377 text_area_width = window_box_width (w, TEXT_AREA);
12379 /* Scroll when cursor is inside this scroll margin. */
12380 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12382 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12383 /* For left-to-right rows, hscroll when cursor is either
12384 (i) inside the right hscroll margin, or (ii) if it is
12385 inside the left margin and the window is already
12386 hscrolled. */
12387 && ((!row_r2l_p
12388 && ((w->hscroll
12389 && w->cursor.x <= h_margin)
12390 || (cursor_row->enabled_p
12391 && cursor_row->truncated_on_right_p
12392 && (w->cursor.x >= text_area_width - h_margin))))
12393 /* For right-to-left rows, the logic is similar,
12394 except that rules for scrolling to left and right
12395 are reversed. E.g., if cursor.x <= h_margin, we
12396 need to hscroll "to the right" unconditionally,
12397 and that will scroll the screen to the left so as
12398 to reveal the next portion of the row. */
12399 || (row_r2l_p
12400 && ((cursor_row->enabled_p
12401 /* FIXME: It is confusing to set the
12402 truncated_on_right_p flag when R2L rows
12403 are actually truncated on the left. */
12404 && cursor_row->truncated_on_right_p
12405 && w->cursor.x <= h_margin)
12406 || (w->hscroll
12407 && (w->cursor.x >= text_area_width - h_margin))))))
12409 struct it it;
12410 ptrdiff_t hscroll;
12411 struct buffer *saved_current_buffer;
12412 ptrdiff_t pt;
12413 int wanted_x;
12415 /* Find point in a display of infinite width. */
12416 saved_current_buffer = current_buffer;
12417 current_buffer = XBUFFER (w->contents);
12419 if (w == XWINDOW (selected_window))
12420 pt = PT;
12421 else
12422 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12424 /* Move iterator to pt starting at cursor_row->start in
12425 a line with infinite width. */
12426 init_to_row_start (&it, w, cursor_row);
12427 it.last_visible_x = INFINITY;
12428 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12429 current_buffer = saved_current_buffer;
12431 /* Position cursor in window. */
12432 if (!hscroll_relative_p && hscroll_step_abs == 0)
12433 hscroll = max (0, (it.current_x
12434 - (ITERATOR_AT_END_OF_LINE_P (&it)
12435 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12436 : (text_area_width / 2))))
12437 / FRAME_COLUMN_WIDTH (it.f);
12438 else if ((!row_r2l_p
12439 && w->cursor.x >= text_area_width - h_margin)
12440 || (row_r2l_p && w->cursor.x <= h_margin))
12442 if (hscroll_relative_p)
12443 wanted_x = text_area_width * (1 - hscroll_step_rel)
12444 - h_margin;
12445 else
12446 wanted_x = text_area_width
12447 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12448 - h_margin;
12449 hscroll
12450 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12452 else
12454 if (hscroll_relative_p)
12455 wanted_x = text_area_width * hscroll_step_rel
12456 + h_margin;
12457 else
12458 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12459 + h_margin;
12460 hscroll
12461 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12463 hscroll = max (hscroll, w->min_hscroll);
12465 /* Don't prevent redisplay optimizations if hscroll
12466 hasn't changed, as it will unnecessarily slow down
12467 redisplay. */
12468 if (w->hscroll != hscroll)
12470 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12471 w->hscroll = hscroll;
12472 hscrolled_p = 1;
12477 window = w->next;
12480 /* Value is non-zero if hscroll of any leaf window has been changed. */
12481 return hscrolled_p;
12485 /* Set hscroll so that cursor is visible and not inside horizontal
12486 scroll margins for all windows in the tree rooted at WINDOW. See
12487 also hscroll_window_tree above. Value is non-zero if any window's
12488 hscroll has been changed. If it has, desired matrices on the frame
12489 of WINDOW are cleared. */
12491 static int
12492 hscroll_windows (Lisp_Object window)
12494 int hscrolled_p = hscroll_window_tree (window);
12495 if (hscrolled_p)
12496 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12497 return hscrolled_p;
12502 /************************************************************************
12503 Redisplay
12504 ************************************************************************/
12506 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12507 to a non-zero value. This is sometimes handy to have in a debugger
12508 session. */
12510 #ifdef GLYPH_DEBUG
12512 /* First and last unchanged row for try_window_id. */
12514 static int debug_first_unchanged_at_end_vpos;
12515 static int debug_last_unchanged_at_beg_vpos;
12517 /* Delta vpos and y. */
12519 static int debug_dvpos, debug_dy;
12521 /* Delta in characters and bytes for try_window_id. */
12523 static ptrdiff_t debug_delta, debug_delta_bytes;
12525 /* Values of window_end_pos and window_end_vpos at the end of
12526 try_window_id. */
12528 static ptrdiff_t debug_end_vpos;
12530 /* Append a string to W->desired_matrix->method. FMT is a printf
12531 format string. If trace_redisplay_p is non-zero also printf the
12532 resulting string to stderr. */
12534 static void debug_method_add (struct window *, char const *, ...)
12535 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12537 static void
12538 debug_method_add (struct window *w, char const *fmt, ...)
12540 void *ptr = w;
12541 char *method = w->desired_matrix->method;
12542 int len = strlen (method);
12543 int size = sizeof w->desired_matrix->method;
12544 int remaining = size - len - 1;
12545 va_list ap;
12547 if (len && remaining)
12549 method[len] = '|';
12550 --remaining, ++len;
12553 va_start (ap, fmt);
12554 vsnprintf (method + len, remaining + 1, fmt, ap);
12555 va_end (ap);
12557 if (trace_redisplay_p)
12558 fprintf (stderr, "%p (%s): %s\n",
12559 ptr,
12560 ((BUFFERP (w->contents)
12561 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12562 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12563 : "no buffer"),
12564 method + len);
12567 #endif /* GLYPH_DEBUG */
12570 /* Value is non-zero if all changes in window W, which displays
12571 current_buffer, are in the text between START and END. START is a
12572 buffer position, END is given as a distance from Z. Used in
12573 redisplay_internal for display optimization. */
12575 static int
12576 text_outside_line_unchanged_p (struct window *w,
12577 ptrdiff_t start, ptrdiff_t end)
12579 int unchanged_p = 1;
12581 /* If text or overlays have changed, see where. */
12582 if (window_outdated (w))
12584 /* Gap in the line? */
12585 if (GPT < start || Z - GPT < end)
12586 unchanged_p = 0;
12588 /* Changes start in front of the line, or end after it? */
12589 if (unchanged_p
12590 && (BEG_UNCHANGED < start - 1
12591 || END_UNCHANGED < end))
12592 unchanged_p = 0;
12594 /* If selective display, can't optimize if changes start at the
12595 beginning of the line. */
12596 if (unchanged_p
12597 && INTEGERP (BVAR (current_buffer, selective_display))
12598 && XINT (BVAR (current_buffer, selective_display)) > 0
12599 && (BEG_UNCHANGED < start || GPT <= start))
12600 unchanged_p = 0;
12602 /* If there are overlays at the start or end of the line, these
12603 may have overlay strings with newlines in them. A change at
12604 START, for instance, may actually concern the display of such
12605 overlay strings as well, and they are displayed on different
12606 lines. So, quickly rule out this case. (For the future, it
12607 might be desirable to implement something more telling than
12608 just BEG/END_UNCHANGED.) */
12609 if (unchanged_p)
12611 if (BEG + BEG_UNCHANGED == start
12612 && overlay_touches_p (start))
12613 unchanged_p = 0;
12614 if (END_UNCHANGED == end
12615 && overlay_touches_p (Z - end))
12616 unchanged_p = 0;
12619 /* Under bidi reordering, adding or deleting a character in the
12620 beginning of a paragraph, before the first strong directional
12621 character, can change the base direction of the paragraph (unless
12622 the buffer specifies a fixed paragraph direction), which will
12623 require to redisplay the whole paragraph. It might be worthwhile
12624 to find the paragraph limits and widen the range of redisplayed
12625 lines to that, but for now just give up this optimization. */
12626 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12627 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12628 unchanged_p = 0;
12631 return unchanged_p;
12635 /* Do a frame update, taking possible shortcuts into account. This is
12636 the main external entry point for redisplay.
12638 If the last redisplay displayed an echo area message and that message
12639 is no longer requested, we clear the echo area or bring back the
12640 mini-buffer if that is in use. */
12642 void
12643 redisplay (void)
12645 redisplay_internal ();
12649 static Lisp_Object
12650 overlay_arrow_string_or_property (Lisp_Object var)
12652 Lisp_Object val;
12654 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12655 return val;
12657 return Voverlay_arrow_string;
12660 /* Return 1 if there are any overlay-arrows in current_buffer. */
12661 static int
12662 overlay_arrow_in_current_buffer_p (void)
12664 Lisp_Object vlist;
12666 for (vlist = Voverlay_arrow_variable_list;
12667 CONSP (vlist);
12668 vlist = XCDR (vlist))
12670 Lisp_Object var = XCAR (vlist);
12671 Lisp_Object val;
12673 if (!SYMBOLP (var))
12674 continue;
12675 val = find_symbol_value (var);
12676 if (MARKERP (val)
12677 && current_buffer == XMARKER (val)->buffer)
12678 return 1;
12680 return 0;
12684 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12685 has changed. */
12687 static int
12688 overlay_arrows_changed_p (void)
12690 Lisp_Object vlist;
12692 for (vlist = Voverlay_arrow_variable_list;
12693 CONSP (vlist);
12694 vlist = XCDR (vlist))
12696 Lisp_Object var = XCAR (vlist);
12697 Lisp_Object val, pstr;
12699 if (!SYMBOLP (var))
12700 continue;
12701 val = find_symbol_value (var);
12702 if (!MARKERP (val))
12703 continue;
12704 if (! EQ (COERCE_MARKER (val),
12705 Fget (var, Qlast_arrow_position))
12706 || ! (pstr = overlay_arrow_string_or_property (var),
12707 EQ (pstr, Fget (var, Qlast_arrow_string))))
12708 return 1;
12710 return 0;
12713 /* Mark overlay arrows to be updated on next redisplay. */
12715 static void
12716 update_overlay_arrows (int up_to_date)
12718 Lisp_Object vlist;
12720 for (vlist = Voverlay_arrow_variable_list;
12721 CONSP (vlist);
12722 vlist = XCDR (vlist))
12724 Lisp_Object var = XCAR (vlist);
12726 if (!SYMBOLP (var))
12727 continue;
12729 if (up_to_date > 0)
12731 Lisp_Object val = find_symbol_value (var);
12732 Fput (var, Qlast_arrow_position,
12733 COERCE_MARKER (val));
12734 Fput (var, Qlast_arrow_string,
12735 overlay_arrow_string_or_property (var));
12737 else if (up_to_date < 0
12738 || !NILP (Fget (var, Qlast_arrow_position)))
12740 Fput (var, Qlast_arrow_position, Qt);
12741 Fput (var, Qlast_arrow_string, Qt);
12747 /* Return overlay arrow string to display at row.
12748 Return integer (bitmap number) for arrow bitmap in left fringe.
12749 Return nil if no overlay arrow. */
12751 static Lisp_Object
12752 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12754 Lisp_Object vlist;
12756 for (vlist = Voverlay_arrow_variable_list;
12757 CONSP (vlist);
12758 vlist = XCDR (vlist))
12760 Lisp_Object var = XCAR (vlist);
12761 Lisp_Object val;
12763 if (!SYMBOLP (var))
12764 continue;
12766 val = find_symbol_value (var);
12768 if (MARKERP (val)
12769 && current_buffer == XMARKER (val)->buffer
12770 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12772 if (FRAME_WINDOW_P (it->f)
12773 /* FIXME: if ROW->reversed_p is set, this should test
12774 the right fringe, not the left one. */
12775 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12777 #ifdef HAVE_WINDOW_SYSTEM
12778 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12780 int fringe_bitmap;
12781 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12782 return make_number (fringe_bitmap);
12784 #endif
12785 return make_number (-1); /* Use default arrow bitmap. */
12787 return overlay_arrow_string_or_property (var);
12791 return Qnil;
12794 /* Return 1 if point moved out of or into a composition. Otherwise
12795 return 0. PREV_BUF and PREV_PT are the last point buffer and
12796 position. BUF and PT are the current point buffer and position. */
12798 static int
12799 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12800 struct buffer *buf, ptrdiff_t pt)
12802 ptrdiff_t start, end;
12803 Lisp_Object prop;
12804 Lisp_Object buffer;
12806 XSETBUFFER (buffer, buf);
12807 /* Check a composition at the last point if point moved within the
12808 same buffer. */
12809 if (prev_buf == buf)
12811 if (prev_pt == pt)
12812 /* Point didn't move. */
12813 return 0;
12815 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12816 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12817 && composition_valid_p (start, end, prop)
12818 && start < prev_pt && end > prev_pt)
12819 /* The last point was within the composition. Return 1 iff
12820 point moved out of the composition. */
12821 return (pt <= start || pt >= end);
12824 /* Check a composition at the current point. */
12825 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12826 && find_composition (pt, -1, &start, &end, &prop, buffer)
12827 && composition_valid_p (start, end, prop)
12828 && start < pt && end > pt);
12831 /* Reconsider the clip changes of buffer which is displayed in W. */
12833 static void
12834 reconsider_clip_changes (struct window *w)
12836 struct buffer *b = XBUFFER (w->contents);
12838 if (b->clip_changed
12839 && w->window_end_valid
12840 && w->current_matrix->buffer == b
12841 && w->current_matrix->zv == BUF_ZV (b)
12842 && w->current_matrix->begv == BUF_BEGV (b))
12843 b->clip_changed = 0;
12845 /* If display wasn't paused, and W is not a tool bar window, see if
12846 point has been moved into or out of a composition. In that case,
12847 we set b->clip_changed to 1 to force updating the screen. If
12848 b->clip_changed has already been set to 1, we can skip this
12849 check. */
12850 if (!b->clip_changed && w->window_end_valid)
12852 ptrdiff_t pt = (w == XWINDOW (selected_window)
12853 ? PT : marker_position (w->pointm));
12855 if ((w->current_matrix->buffer != b || pt != w->last_point)
12856 && check_point_in_composition (w->current_matrix->buffer,
12857 w->last_point, b, pt))
12858 b->clip_changed = 1;
12862 #define STOP_POLLING \
12863 do { if (! polling_stopped_here) stop_polling (); \
12864 polling_stopped_here = 1; } while (0)
12866 #define RESUME_POLLING \
12867 do { if (polling_stopped_here) start_polling (); \
12868 polling_stopped_here = 0; } while (0)
12871 /* Perhaps in the future avoid recentering windows if it
12872 is not necessary; currently that causes some problems. */
12874 static void
12875 redisplay_internal (void)
12877 struct window *w = XWINDOW (selected_window);
12878 struct window *sw;
12879 struct frame *fr;
12880 int pending;
12881 bool must_finish = 0, match_p;
12882 struct text_pos tlbufpos, tlendpos;
12883 int number_of_visible_frames;
12884 ptrdiff_t count;
12885 struct frame *sf;
12886 int polling_stopped_here = 0;
12887 Lisp_Object tail, frame;
12889 /* Non-zero means redisplay has to consider all windows on all
12890 frames. Zero means, only selected_window is considered. */
12891 int consider_all_windows_p;
12893 /* Non-zero means redisplay has to redisplay the miniwindow. */
12894 int update_miniwindow_p = 0;
12896 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12898 /* No redisplay if running in batch mode or frame is not yet fully
12899 initialized, or redisplay is explicitly turned off by setting
12900 Vinhibit_redisplay. */
12901 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12902 || !NILP (Vinhibit_redisplay))
12903 return;
12905 /* Don't examine these until after testing Vinhibit_redisplay.
12906 When Emacs is shutting down, perhaps because its connection to
12907 X has dropped, we should not look at them at all. */
12908 fr = XFRAME (w->frame);
12909 sf = SELECTED_FRAME ();
12911 if (!fr->glyphs_initialized_p)
12912 return;
12914 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12915 if (popup_activated ())
12916 return;
12917 #endif
12919 /* I don't think this happens but let's be paranoid. */
12920 if (redisplaying_p)
12921 return;
12923 /* Record a function that clears redisplaying_p
12924 when we leave this function. */
12925 count = SPECPDL_INDEX ();
12926 record_unwind_protect_void (unwind_redisplay);
12927 redisplaying_p = 1;
12928 specbind (Qinhibit_free_realized_faces, Qnil);
12930 /* Record this function, so it appears on the profiler's backtraces. */
12931 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12933 FOR_EACH_FRAME (tail, frame)
12934 XFRAME (frame)->already_hscrolled_p = 0;
12936 retry:
12937 /* Remember the currently selected window. */
12938 sw = w;
12940 pending = 0;
12941 last_escape_glyph_frame = NULL;
12942 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12943 last_glyphless_glyph_frame = NULL;
12944 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12946 /* If face_change_count is non-zero, init_iterator will free all
12947 realized faces, which includes the faces referenced from current
12948 matrices. So, we can't reuse current matrices in this case. */
12949 if (face_change_count)
12950 ++windows_or_buffers_changed;
12952 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12953 && FRAME_TTY (sf)->previous_frame != sf)
12955 /* Since frames on a single ASCII terminal share the same
12956 display area, displaying a different frame means redisplay
12957 the whole thing. */
12958 windows_or_buffers_changed++;
12959 SET_FRAME_GARBAGED (sf);
12960 #ifndef DOS_NT
12961 set_tty_color_mode (FRAME_TTY (sf), sf);
12962 #endif
12963 FRAME_TTY (sf)->previous_frame = sf;
12966 /* Set the visible flags for all frames. Do this before checking for
12967 resized or garbaged frames; they want to know if their frames are
12968 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12969 number_of_visible_frames = 0;
12971 FOR_EACH_FRAME (tail, frame)
12973 struct frame *f = XFRAME (frame);
12975 if (FRAME_VISIBLE_P (f))
12977 ++number_of_visible_frames;
12978 /* Adjust matrices for visible frames only. */
12979 if (f->fonts_changed)
12981 adjust_frame_glyphs (f);
12982 f->fonts_changed = 0;
12984 /* If cursor type has been changed on the frame
12985 other than selected, consider all frames. */
12986 if (f != sf && f->cursor_type_changed)
12987 update_mode_lines++;
12989 clear_desired_matrices (f);
12992 /* Notice any pending interrupt request to change frame size. */
12993 do_pending_window_change (1);
12995 /* do_pending_window_change could change the selected_window due to
12996 frame resizing which makes the selected window too small. */
12997 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12998 sw = w;
13000 /* Clear frames marked as garbaged. */
13001 clear_garbaged_frames ();
13003 /* Build menubar and tool-bar items. */
13004 if (NILP (Vmemory_full))
13005 prepare_menu_bars ();
13007 if (windows_or_buffers_changed)
13008 update_mode_lines++;
13010 reconsider_clip_changes (w);
13012 /* In most cases selected window displays current buffer. */
13013 match_p = XBUFFER (w->contents) == current_buffer;
13014 if (match_p)
13016 /* Detect case that we need to write or remove a star in the mode line. */
13017 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13019 w->update_mode_line = 1;
13020 if (buffer_shared_and_changed ())
13021 update_mode_lines++;
13024 if (mode_line_update_needed (w))
13025 w->update_mode_line = 1;
13028 consider_all_windows_p = (update_mode_lines
13029 || buffer_shared_and_changed ());
13031 /* If specs for an arrow have changed, do thorough redisplay
13032 to ensure we remove any arrow that should no longer exist. */
13033 if (overlay_arrows_changed_p ())
13034 consider_all_windows_p = windows_or_buffers_changed = 1;
13036 /* Normally the message* functions will have already displayed and
13037 updated the echo area, but the frame may have been trashed, or
13038 the update may have been preempted, so display the echo area
13039 again here. Checking message_cleared_p captures the case that
13040 the echo area should be cleared. */
13041 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13042 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13043 || (message_cleared_p
13044 && minibuf_level == 0
13045 /* If the mini-window is currently selected, this means the
13046 echo-area doesn't show through. */
13047 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13049 int window_height_changed_p = echo_area_display (0);
13051 if (message_cleared_p)
13052 update_miniwindow_p = 1;
13054 must_finish = 1;
13056 /* If we don't display the current message, don't clear the
13057 message_cleared_p flag, because, if we did, we wouldn't clear
13058 the echo area in the next redisplay which doesn't preserve
13059 the echo area. */
13060 if (!display_last_displayed_message_p)
13061 message_cleared_p = 0;
13063 if (window_height_changed_p)
13065 consider_all_windows_p = 1;
13066 ++update_mode_lines;
13067 ++windows_or_buffers_changed;
13069 /* If window configuration was changed, frames may have been
13070 marked garbaged. Clear them or we will experience
13071 surprises wrt scrolling. */
13072 clear_garbaged_frames ();
13075 else if (EQ (selected_window, minibuf_window)
13076 && (current_buffer->clip_changed || window_outdated (w))
13077 && resize_mini_window (w, 0))
13079 /* Resized active mini-window to fit the size of what it is
13080 showing if its contents might have changed. */
13081 must_finish = 1;
13082 /* FIXME: this causes all frames to be updated, which seems unnecessary
13083 since only the current frame needs to be considered. This function
13084 needs to be rewritten with two variables, consider_all_windows and
13085 consider_all_frames. */
13086 consider_all_windows_p = 1;
13087 ++windows_or_buffers_changed;
13088 ++update_mode_lines;
13090 /* If window configuration was changed, frames may have been
13091 marked garbaged. Clear them or we will experience
13092 surprises wrt scrolling. */
13093 clear_garbaged_frames ();
13096 /* Optimize the case that only the line containing the cursor in the
13097 selected window has changed. Variables starting with this_ are
13098 set in display_line and record information about the line
13099 containing the cursor. */
13100 tlbufpos = this_line_start_pos;
13101 tlendpos = this_line_end_pos;
13102 if (!consider_all_windows_p
13103 && CHARPOS (tlbufpos) > 0
13104 && !w->update_mode_line
13105 && !current_buffer->clip_changed
13106 && !current_buffer->prevent_redisplay_optimizations_p
13107 && FRAME_VISIBLE_P (XFRAME (w->frame))
13108 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13109 && !XFRAME (w->frame)->cursor_type_changed
13110 /* Make sure recorded data applies to current buffer, etc. */
13111 && this_line_buffer == current_buffer
13112 && match_p
13113 && !w->force_start
13114 && !w->optional_new_start
13115 /* Point must be on the line that we have info recorded about. */
13116 && PT >= CHARPOS (tlbufpos)
13117 && PT <= Z - CHARPOS (tlendpos)
13118 /* All text outside that line, including its final newline,
13119 must be unchanged. */
13120 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13121 CHARPOS (tlendpos)))
13123 if (CHARPOS (tlbufpos) > BEGV
13124 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13125 && (CHARPOS (tlbufpos) == ZV
13126 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13127 /* Former continuation line has disappeared by becoming empty. */
13128 goto cancel;
13129 else if (window_outdated (w) || MINI_WINDOW_P (w))
13131 /* We have to handle the case of continuation around a
13132 wide-column character (see the comment in indent.c around
13133 line 1340).
13135 For instance, in the following case:
13137 -------- Insert --------
13138 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13139 J_I_ ==> J_I_ `^^' are cursors.
13140 ^^ ^^
13141 -------- --------
13143 As we have to redraw the line above, we cannot use this
13144 optimization. */
13146 struct it it;
13147 int line_height_before = this_line_pixel_height;
13149 /* Note that start_display will handle the case that the
13150 line starting at tlbufpos is a continuation line. */
13151 start_display (&it, w, tlbufpos);
13153 /* Implementation note: It this still necessary? */
13154 if (it.current_x != this_line_start_x)
13155 goto cancel;
13157 TRACE ((stderr, "trying display optimization 1\n"));
13158 w->cursor.vpos = -1;
13159 overlay_arrow_seen = 0;
13160 it.vpos = this_line_vpos;
13161 it.current_y = this_line_y;
13162 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13163 display_line (&it);
13165 /* If line contains point, is not continued,
13166 and ends at same distance from eob as before, we win. */
13167 if (w->cursor.vpos >= 0
13168 /* Line is not continued, otherwise this_line_start_pos
13169 would have been set to 0 in display_line. */
13170 && CHARPOS (this_line_start_pos)
13171 /* Line ends as before. */
13172 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13173 /* Line has same height as before. Otherwise other lines
13174 would have to be shifted up or down. */
13175 && this_line_pixel_height == line_height_before)
13177 /* If this is not the window's last line, we must adjust
13178 the charstarts of the lines below. */
13179 if (it.current_y < it.last_visible_y)
13181 struct glyph_row *row
13182 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13183 ptrdiff_t delta, delta_bytes;
13185 /* We used to distinguish between two cases here,
13186 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13187 when the line ends in a newline or the end of the
13188 buffer's accessible portion. But both cases did
13189 the same, so they were collapsed. */
13190 delta = (Z
13191 - CHARPOS (tlendpos)
13192 - MATRIX_ROW_START_CHARPOS (row));
13193 delta_bytes = (Z_BYTE
13194 - BYTEPOS (tlendpos)
13195 - MATRIX_ROW_START_BYTEPOS (row));
13197 increment_matrix_positions (w->current_matrix,
13198 this_line_vpos + 1,
13199 w->current_matrix->nrows,
13200 delta, delta_bytes);
13203 /* If this row displays text now but previously didn't,
13204 or vice versa, w->window_end_vpos may have to be
13205 adjusted. */
13206 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13208 if (w->window_end_vpos < this_line_vpos)
13209 w->window_end_vpos = this_line_vpos;
13211 else if (w->window_end_vpos == this_line_vpos
13212 && this_line_vpos > 0)
13213 w->window_end_vpos = this_line_vpos - 1;
13214 w->window_end_valid = 0;
13216 /* Update hint: No need to try to scroll in update_window. */
13217 w->desired_matrix->no_scrolling_p = 1;
13219 #ifdef GLYPH_DEBUG
13220 *w->desired_matrix->method = 0;
13221 debug_method_add (w, "optimization 1");
13222 #endif
13223 #ifdef HAVE_WINDOW_SYSTEM
13224 update_window_fringes (w, 0);
13225 #endif
13226 goto update;
13228 else
13229 goto cancel;
13231 else if (/* Cursor position hasn't changed. */
13232 PT == w->last_point
13233 /* Make sure the cursor was last displayed
13234 in this window. Otherwise we have to reposition it. */
13235 && 0 <= w->cursor.vpos
13236 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13238 if (!must_finish)
13240 do_pending_window_change (1);
13241 /* If selected_window changed, redisplay again. */
13242 if (WINDOWP (selected_window)
13243 && (w = XWINDOW (selected_window)) != sw)
13244 goto retry;
13246 /* We used to always goto end_of_redisplay here, but this
13247 isn't enough if we have a blinking cursor. */
13248 if (w->cursor_off_p == w->last_cursor_off_p)
13249 goto end_of_redisplay;
13251 goto update;
13253 /* If highlighting the region, or if the cursor is in the echo area,
13254 then we can't just move the cursor. */
13255 else if (NILP (Vshow_trailing_whitespace)
13256 && !cursor_in_echo_area)
13258 struct it it;
13259 struct glyph_row *row;
13261 /* Skip from tlbufpos to PT and see where it is. Note that
13262 PT may be in invisible text. If so, we will end at the
13263 next visible position. */
13264 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13265 NULL, DEFAULT_FACE_ID);
13266 it.current_x = this_line_start_x;
13267 it.current_y = this_line_y;
13268 it.vpos = this_line_vpos;
13270 /* The call to move_it_to stops in front of PT, but
13271 moves over before-strings. */
13272 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13274 if (it.vpos == this_line_vpos
13275 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13276 row->enabled_p))
13278 eassert (this_line_vpos == it.vpos);
13279 eassert (this_line_y == it.current_y);
13280 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13281 #ifdef GLYPH_DEBUG
13282 *w->desired_matrix->method = 0;
13283 debug_method_add (w, "optimization 3");
13284 #endif
13285 goto update;
13287 else
13288 goto cancel;
13291 cancel:
13292 /* Text changed drastically or point moved off of line. */
13293 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13296 CHARPOS (this_line_start_pos) = 0;
13297 consider_all_windows_p |= buffer_shared_and_changed ();
13298 ++clear_face_cache_count;
13299 #ifdef HAVE_WINDOW_SYSTEM
13300 ++clear_image_cache_count;
13301 #endif
13303 /* Build desired matrices, and update the display. If
13304 consider_all_windows_p is non-zero, do it for all windows on all
13305 frames. Otherwise do it for selected_window, only. */
13307 if (consider_all_windows_p)
13309 FOR_EACH_FRAME (tail, frame)
13310 XFRAME (frame)->updated_p = 0;
13312 FOR_EACH_FRAME (tail, frame)
13314 struct frame *f = XFRAME (frame);
13316 /* We don't have to do anything for unselected terminal
13317 frames. */
13318 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13319 && !EQ (FRAME_TTY (f)->top_frame, frame))
13320 continue;
13322 retry_frame:
13324 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13326 /* Mark all the scroll bars to be removed; we'll redeem
13327 the ones we want when we redisplay their windows. */
13328 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13329 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13331 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13332 redisplay_windows (FRAME_ROOT_WINDOW (f));
13334 /* The X error handler may have deleted that frame. */
13335 if (!FRAME_LIVE_P (f))
13336 continue;
13338 /* Any scroll bars which redisplay_windows should have
13339 nuked should now go away. */
13340 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13341 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13343 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13345 /* If fonts changed on visible frame, display again. */
13346 if (f->fonts_changed)
13348 adjust_frame_glyphs (f);
13349 f->fonts_changed = 0;
13350 goto retry_frame;
13353 /* See if we have to hscroll. */
13354 if (!f->already_hscrolled_p)
13356 f->already_hscrolled_p = 1;
13357 if (hscroll_windows (f->root_window))
13358 goto retry_frame;
13361 /* Prevent various kinds of signals during display
13362 update. stdio is not robust about handling
13363 signals, which can cause an apparent I/O
13364 error. */
13365 if (interrupt_input)
13366 unrequest_sigio ();
13367 STOP_POLLING;
13369 /* Mark windows on frame F to update. If we decide to
13370 update all frames but windows_or_buffers_changed is
13371 zero, we assume that only the windows that shows
13372 current buffer should be really updated. */
13373 set_window_update_flags
13374 (XWINDOW (f->root_window),
13375 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13376 pending |= update_frame (f, 0, 0);
13377 f->cursor_type_changed = 0;
13378 f->updated_p = 1;
13383 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13385 if (!pending)
13387 /* Do the mark_window_display_accurate after all windows have
13388 been redisplayed because this call resets flags in buffers
13389 which are needed for proper redisplay. */
13390 FOR_EACH_FRAME (tail, frame)
13392 struct frame *f = XFRAME (frame);
13393 if (f->updated_p)
13395 mark_window_display_accurate (f->root_window, 1);
13396 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13397 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13402 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13404 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13405 struct frame *mini_frame;
13407 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13408 /* Use list_of_error, not Qerror, so that
13409 we catch only errors and don't run the debugger. */
13410 internal_condition_case_1 (redisplay_window_1, selected_window,
13411 list_of_error,
13412 redisplay_window_error);
13413 if (update_miniwindow_p)
13414 internal_condition_case_1 (redisplay_window_1, mini_window,
13415 list_of_error,
13416 redisplay_window_error);
13418 /* Compare desired and current matrices, perform output. */
13420 update:
13421 /* If fonts changed, display again. */
13422 if (sf->fonts_changed)
13423 goto retry;
13425 /* Prevent various kinds of signals during display update.
13426 stdio is not robust about handling signals,
13427 which can cause an apparent I/O error. */
13428 if (interrupt_input)
13429 unrequest_sigio ();
13430 STOP_POLLING;
13432 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13434 if (hscroll_windows (selected_window))
13435 goto retry;
13437 XWINDOW (selected_window)->must_be_updated_p = 1;
13438 pending = update_frame (sf, 0, 0);
13439 sf->cursor_type_changed = 0;
13442 /* We may have called echo_area_display at the top of this
13443 function. If the echo area is on another frame, that may
13444 have put text on a frame other than the selected one, so the
13445 above call to update_frame would not have caught it. Catch
13446 it here. */
13447 mini_window = FRAME_MINIBUF_WINDOW (sf);
13448 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13450 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13452 XWINDOW (mini_window)->must_be_updated_p = 1;
13453 pending |= update_frame (mini_frame, 0, 0);
13454 mini_frame->cursor_type_changed = 0;
13455 if (!pending && hscroll_windows (mini_window))
13456 goto retry;
13460 /* If display was paused because of pending input, make sure we do a
13461 thorough update the next time. */
13462 if (pending)
13464 /* Prevent the optimization at the beginning of
13465 redisplay_internal that tries a single-line update of the
13466 line containing the cursor in the selected window. */
13467 CHARPOS (this_line_start_pos) = 0;
13469 /* Let the overlay arrow be updated the next time. */
13470 update_overlay_arrows (0);
13472 /* If we pause after scrolling, some rows in the current
13473 matrices of some windows are not valid. */
13474 if (!WINDOW_FULL_WIDTH_P (w)
13475 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13476 update_mode_lines = 1;
13478 else
13480 if (!consider_all_windows_p)
13482 /* This has already been done above if
13483 consider_all_windows_p is set. */
13484 mark_window_display_accurate_1 (w, 1);
13486 /* Say overlay arrows are up to date. */
13487 update_overlay_arrows (1);
13489 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13490 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13493 update_mode_lines = 0;
13494 windows_or_buffers_changed = 0;
13497 /* Start SIGIO interrupts coming again. Having them off during the
13498 code above makes it less likely one will discard output, but not
13499 impossible, since there might be stuff in the system buffer here.
13500 But it is much hairier to try to do anything about that. */
13501 if (interrupt_input)
13502 request_sigio ();
13503 RESUME_POLLING;
13505 /* If a frame has become visible which was not before, redisplay
13506 again, so that we display it. Expose events for such a frame
13507 (which it gets when becoming visible) don't call the parts of
13508 redisplay constructing glyphs, so simply exposing a frame won't
13509 display anything in this case. So, we have to display these
13510 frames here explicitly. */
13511 if (!pending)
13513 int new_count = 0;
13515 FOR_EACH_FRAME (tail, frame)
13517 int this_is_visible = 0;
13519 if (XFRAME (frame)->visible)
13520 this_is_visible = 1;
13522 if (this_is_visible)
13523 new_count++;
13526 if (new_count != number_of_visible_frames)
13527 windows_or_buffers_changed++;
13530 /* Change frame size now if a change is pending. */
13531 do_pending_window_change (1);
13533 /* If we just did a pending size change, or have additional
13534 visible frames, or selected_window changed, redisplay again. */
13535 if ((windows_or_buffers_changed && !pending)
13536 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13537 goto retry;
13539 /* Clear the face and image caches.
13541 We used to do this only if consider_all_windows_p. But the cache
13542 needs to be cleared if a timer creates images in the current
13543 buffer (e.g. the test case in Bug#6230). */
13545 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13547 clear_face_cache (0);
13548 clear_face_cache_count = 0;
13551 #ifdef HAVE_WINDOW_SYSTEM
13552 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13554 clear_image_caches (Qnil);
13555 clear_image_cache_count = 0;
13557 #endif /* HAVE_WINDOW_SYSTEM */
13559 end_of_redisplay:
13560 unbind_to (count, Qnil);
13561 RESUME_POLLING;
13565 /* Redisplay, but leave alone any recent echo area message unless
13566 another message has been requested in its place.
13568 This is useful in situations where you need to redisplay but no
13569 user action has occurred, making it inappropriate for the message
13570 area to be cleared. See tracking_off and
13571 wait_reading_process_output for examples of these situations.
13573 FROM_WHERE is an integer saying from where this function was
13574 called. This is useful for debugging. */
13576 void
13577 redisplay_preserve_echo_area (int from_where)
13579 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13581 if (!NILP (echo_area_buffer[1]))
13583 /* We have a previously displayed message, but no current
13584 message. Redisplay the previous message. */
13585 display_last_displayed_message_p = 1;
13586 redisplay_internal ();
13587 display_last_displayed_message_p = 0;
13589 else
13590 redisplay_internal ();
13592 flush_frame (SELECTED_FRAME ());
13596 /* Function registered with record_unwind_protect in redisplay_internal. */
13598 static void
13599 unwind_redisplay (void)
13601 redisplaying_p = 0;
13605 /* Mark the display of leaf window W as accurate or inaccurate.
13606 If ACCURATE_P is non-zero mark display of W as accurate. If
13607 ACCURATE_P is zero, arrange for W to be redisplayed the next
13608 time redisplay_internal is called. */
13610 static void
13611 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13613 struct buffer *b = XBUFFER (w->contents);
13615 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13616 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13617 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13619 if (accurate_p)
13621 b->clip_changed = 0;
13622 b->prevent_redisplay_optimizations_p = 0;
13624 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13625 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13626 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13627 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13629 w->current_matrix->buffer = b;
13630 w->current_matrix->begv = BUF_BEGV (b);
13631 w->current_matrix->zv = BUF_ZV (b);
13633 w->last_cursor_vpos = w->cursor.vpos;
13634 w->last_cursor_off_p = w->cursor_off_p;
13636 if (w == XWINDOW (selected_window))
13637 w->last_point = BUF_PT (b);
13638 else
13639 w->last_point = marker_position (w->pointm);
13641 w->window_end_valid = 1;
13642 w->update_mode_line = 0;
13647 /* Mark the display of windows in the window tree rooted at WINDOW as
13648 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13649 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13650 be redisplayed the next time redisplay_internal is called. */
13652 void
13653 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13655 struct window *w;
13657 for (; !NILP (window); window = w->next)
13659 w = XWINDOW (window);
13660 if (WINDOWP (w->contents))
13661 mark_window_display_accurate (w->contents, accurate_p);
13662 else
13663 mark_window_display_accurate_1 (w, accurate_p);
13666 if (accurate_p)
13667 update_overlay_arrows (1);
13668 else
13669 /* Force a thorough redisplay the next time by setting
13670 last_arrow_position and last_arrow_string to t, which is
13671 unequal to any useful value of Voverlay_arrow_... */
13672 update_overlay_arrows (-1);
13676 /* Return value in display table DP (Lisp_Char_Table *) for character
13677 C. Since a display table doesn't have any parent, we don't have to
13678 follow parent. Do not call this function directly but use the
13679 macro DISP_CHAR_VECTOR. */
13681 Lisp_Object
13682 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13684 Lisp_Object val;
13686 if (ASCII_CHAR_P (c))
13688 val = dp->ascii;
13689 if (SUB_CHAR_TABLE_P (val))
13690 val = XSUB_CHAR_TABLE (val)->contents[c];
13692 else
13694 Lisp_Object table;
13696 XSETCHAR_TABLE (table, dp);
13697 val = char_table_ref (table, c);
13699 if (NILP (val))
13700 val = dp->defalt;
13701 return val;
13706 /***********************************************************************
13707 Window Redisplay
13708 ***********************************************************************/
13710 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13712 static void
13713 redisplay_windows (Lisp_Object window)
13715 while (!NILP (window))
13717 struct window *w = XWINDOW (window);
13719 if (WINDOWP (w->contents))
13720 redisplay_windows (w->contents);
13721 else if (BUFFERP (w->contents))
13723 displayed_buffer = XBUFFER (w->contents);
13724 /* Use list_of_error, not Qerror, so that
13725 we catch only errors and don't run the debugger. */
13726 internal_condition_case_1 (redisplay_window_0, window,
13727 list_of_error,
13728 redisplay_window_error);
13731 window = w->next;
13735 static Lisp_Object
13736 redisplay_window_error (Lisp_Object ignore)
13738 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13739 return Qnil;
13742 static Lisp_Object
13743 redisplay_window_0 (Lisp_Object window)
13745 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13746 redisplay_window (window, 0);
13747 return Qnil;
13750 static Lisp_Object
13751 redisplay_window_1 (Lisp_Object window)
13753 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13754 redisplay_window (window, 1);
13755 return Qnil;
13759 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13760 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13761 which positions recorded in ROW differ from current buffer
13762 positions.
13764 Return 0 if cursor is not on this row, 1 otherwise. */
13766 static int
13767 set_cursor_from_row (struct window *w, struct glyph_row *row,
13768 struct glyph_matrix *matrix,
13769 ptrdiff_t delta, ptrdiff_t delta_bytes,
13770 int dy, int dvpos)
13772 struct glyph *glyph = row->glyphs[TEXT_AREA];
13773 struct glyph *end = glyph + row->used[TEXT_AREA];
13774 struct glyph *cursor = NULL;
13775 /* The last known character position in row. */
13776 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13777 int x = row->x;
13778 ptrdiff_t pt_old = PT - delta;
13779 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13780 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13781 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13782 /* A glyph beyond the edge of TEXT_AREA which we should never
13783 touch. */
13784 struct glyph *glyphs_end = end;
13785 /* Non-zero means we've found a match for cursor position, but that
13786 glyph has the avoid_cursor_p flag set. */
13787 int match_with_avoid_cursor = 0;
13788 /* Non-zero means we've seen at least one glyph that came from a
13789 display string. */
13790 int string_seen = 0;
13791 /* Largest and smallest buffer positions seen so far during scan of
13792 glyph row. */
13793 ptrdiff_t bpos_max = pos_before;
13794 ptrdiff_t bpos_min = pos_after;
13795 /* Last buffer position covered by an overlay string with an integer
13796 `cursor' property. */
13797 ptrdiff_t bpos_covered = 0;
13798 /* Non-zero means the display string on which to display the cursor
13799 comes from a text property, not from an overlay. */
13800 int string_from_text_prop = 0;
13802 /* Don't even try doing anything if called for a mode-line or
13803 header-line row, since the rest of the code isn't prepared to
13804 deal with such calamities. */
13805 eassert (!row->mode_line_p);
13806 if (row->mode_line_p)
13807 return 0;
13809 /* Skip over glyphs not having an object at the start and the end of
13810 the row. These are special glyphs like truncation marks on
13811 terminal frames. */
13812 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13814 if (!row->reversed_p)
13816 while (glyph < end
13817 && INTEGERP (glyph->object)
13818 && glyph->charpos < 0)
13820 x += glyph->pixel_width;
13821 ++glyph;
13823 while (end > glyph
13824 && INTEGERP ((end - 1)->object)
13825 /* CHARPOS is zero for blanks and stretch glyphs
13826 inserted by extend_face_to_end_of_line. */
13827 && (end - 1)->charpos <= 0)
13828 --end;
13829 glyph_before = glyph - 1;
13830 glyph_after = end;
13832 else
13834 struct glyph *g;
13836 /* If the glyph row is reversed, we need to process it from back
13837 to front, so swap the edge pointers. */
13838 glyphs_end = end = glyph - 1;
13839 glyph += row->used[TEXT_AREA] - 1;
13841 while (glyph > end + 1
13842 && INTEGERP (glyph->object)
13843 && glyph->charpos < 0)
13845 --glyph;
13846 x -= glyph->pixel_width;
13848 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13849 --glyph;
13850 /* By default, in reversed rows we put the cursor on the
13851 rightmost (first in the reading order) glyph. */
13852 for (g = end + 1; g < glyph; g++)
13853 x += g->pixel_width;
13854 while (end < glyph
13855 && INTEGERP ((end + 1)->object)
13856 && (end + 1)->charpos <= 0)
13857 ++end;
13858 glyph_before = glyph + 1;
13859 glyph_after = end;
13862 else if (row->reversed_p)
13864 /* In R2L rows that don't display text, put the cursor on the
13865 rightmost glyph. Case in point: an empty last line that is
13866 part of an R2L paragraph. */
13867 cursor = end - 1;
13868 /* Avoid placing the cursor on the last glyph of the row, where
13869 on terminal frames we hold the vertical border between
13870 adjacent windows. */
13871 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13872 && !WINDOW_RIGHTMOST_P (w)
13873 && cursor == row->glyphs[LAST_AREA] - 1)
13874 cursor--;
13875 x = -1; /* will be computed below, at label compute_x */
13878 /* Step 1: Try to find the glyph whose character position
13879 corresponds to point. If that's not possible, find 2 glyphs
13880 whose character positions are the closest to point, one before
13881 point, the other after it. */
13882 if (!row->reversed_p)
13883 while (/* not marched to end of glyph row */
13884 glyph < end
13885 /* glyph was not inserted by redisplay for internal purposes */
13886 && !INTEGERP (glyph->object))
13888 if (BUFFERP (glyph->object))
13890 ptrdiff_t dpos = glyph->charpos - pt_old;
13892 if (glyph->charpos > bpos_max)
13893 bpos_max = glyph->charpos;
13894 if (glyph->charpos < bpos_min)
13895 bpos_min = glyph->charpos;
13896 if (!glyph->avoid_cursor_p)
13898 /* If we hit point, we've found the glyph on which to
13899 display the cursor. */
13900 if (dpos == 0)
13902 match_with_avoid_cursor = 0;
13903 break;
13905 /* See if we've found a better approximation to
13906 POS_BEFORE or to POS_AFTER. */
13907 if (0 > dpos && dpos > pos_before - pt_old)
13909 pos_before = glyph->charpos;
13910 glyph_before = glyph;
13912 else if (0 < dpos && dpos < pos_after - pt_old)
13914 pos_after = glyph->charpos;
13915 glyph_after = glyph;
13918 else if (dpos == 0)
13919 match_with_avoid_cursor = 1;
13921 else if (STRINGP (glyph->object))
13923 Lisp_Object chprop;
13924 ptrdiff_t glyph_pos = glyph->charpos;
13926 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13927 glyph->object);
13928 if (!NILP (chprop))
13930 /* If the string came from a `display' text property,
13931 look up the buffer position of that property and
13932 use that position to update bpos_max, as if we
13933 actually saw such a position in one of the row's
13934 glyphs. This helps with supporting integer values
13935 of `cursor' property on the display string in
13936 situations where most or all of the row's buffer
13937 text is completely covered by display properties,
13938 so that no glyph with valid buffer positions is
13939 ever seen in the row. */
13940 ptrdiff_t prop_pos =
13941 string_buffer_position_lim (glyph->object, pos_before,
13942 pos_after, 0);
13944 if (prop_pos >= pos_before)
13945 bpos_max = prop_pos - 1;
13947 if (INTEGERP (chprop))
13949 bpos_covered = bpos_max + XINT (chprop);
13950 /* If the `cursor' property covers buffer positions up
13951 to and including point, we should display cursor on
13952 this glyph. Note that, if a `cursor' property on one
13953 of the string's characters has an integer value, we
13954 will break out of the loop below _before_ we get to
13955 the position match above. IOW, integer values of
13956 the `cursor' property override the "exact match for
13957 point" strategy of positioning the cursor. */
13958 /* Implementation note: bpos_max == pt_old when, e.g.,
13959 we are in an empty line, where bpos_max is set to
13960 MATRIX_ROW_START_CHARPOS, see above. */
13961 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13963 cursor = glyph;
13964 break;
13968 string_seen = 1;
13970 x += glyph->pixel_width;
13971 ++glyph;
13973 else if (glyph > end) /* row is reversed */
13974 while (!INTEGERP (glyph->object))
13976 if (BUFFERP (glyph->object))
13978 ptrdiff_t dpos = glyph->charpos - pt_old;
13980 if (glyph->charpos > bpos_max)
13981 bpos_max = glyph->charpos;
13982 if (glyph->charpos < bpos_min)
13983 bpos_min = glyph->charpos;
13984 if (!glyph->avoid_cursor_p)
13986 if (dpos == 0)
13988 match_with_avoid_cursor = 0;
13989 break;
13991 if (0 > dpos && dpos > pos_before - pt_old)
13993 pos_before = glyph->charpos;
13994 glyph_before = glyph;
13996 else if (0 < dpos && dpos < pos_after - pt_old)
13998 pos_after = glyph->charpos;
13999 glyph_after = glyph;
14002 else if (dpos == 0)
14003 match_with_avoid_cursor = 1;
14005 else if (STRINGP (glyph->object))
14007 Lisp_Object chprop;
14008 ptrdiff_t glyph_pos = glyph->charpos;
14010 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14011 glyph->object);
14012 if (!NILP (chprop))
14014 ptrdiff_t prop_pos =
14015 string_buffer_position_lim (glyph->object, pos_before,
14016 pos_after, 0);
14018 if (prop_pos >= pos_before)
14019 bpos_max = prop_pos - 1;
14021 if (INTEGERP (chprop))
14023 bpos_covered = bpos_max + XINT (chprop);
14024 /* If the `cursor' property covers buffer positions up
14025 to and including point, we should display cursor on
14026 this glyph. */
14027 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14029 cursor = glyph;
14030 break;
14033 string_seen = 1;
14035 --glyph;
14036 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14038 x--; /* can't use any pixel_width */
14039 break;
14041 x -= glyph->pixel_width;
14044 /* Step 2: If we didn't find an exact match for point, we need to
14045 look for a proper place to put the cursor among glyphs between
14046 GLYPH_BEFORE and GLYPH_AFTER. */
14047 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14048 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14049 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14051 /* An empty line has a single glyph whose OBJECT is zero and
14052 whose CHARPOS is the position of a newline on that line.
14053 Note that on a TTY, there are more glyphs after that, which
14054 were produced by extend_face_to_end_of_line, but their
14055 CHARPOS is zero or negative. */
14056 int empty_line_p =
14057 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14058 && INTEGERP (glyph->object) && glyph->charpos > 0
14059 /* On a TTY, continued and truncated rows also have a glyph at
14060 their end whose OBJECT is zero and whose CHARPOS is
14061 positive (the continuation and truncation glyphs), but such
14062 rows are obviously not "empty". */
14063 && !(row->continued_p || row->truncated_on_right_p);
14065 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14067 ptrdiff_t ellipsis_pos;
14069 /* Scan back over the ellipsis glyphs. */
14070 if (!row->reversed_p)
14072 ellipsis_pos = (glyph - 1)->charpos;
14073 while (glyph > row->glyphs[TEXT_AREA]
14074 && (glyph - 1)->charpos == ellipsis_pos)
14075 glyph--, x -= glyph->pixel_width;
14076 /* That loop always goes one position too far, including
14077 the glyph before the ellipsis. So scan forward over
14078 that one. */
14079 x += glyph->pixel_width;
14080 glyph++;
14082 else /* row is reversed */
14084 ellipsis_pos = (glyph + 1)->charpos;
14085 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14086 && (glyph + 1)->charpos == ellipsis_pos)
14087 glyph++, x += glyph->pixel_width;
14088 x -= glyph->pixel_width;
14089 glyph--;
14092 else if (match_with_avoid_cursor)
14094 cursor = glyph_after;
14095 x = -1;
14097 else if (string_seen)
14099 int incr = row->reversed_p ? -1 : +1;
14101 /* Need to find the glyph that came out of a string which is
14102 present at point. That glyph is somewhere between
14103 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14104 positioned between POS_BEFORE and POS_AFTER in the
14105 buffer. */
14106 struct glyph *start, *stop;
14107 ptrdiff_t pos = pos_before;
14109 x = -1;
14111 /* If the row ends in a newline from a display string,
14112 reordering could have moved the glyphs belonging to the
14113 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14114 in this case we extend the search to the last glyph in
14115 the row that was not inserted by redisplay. */
14116 if (row->ends_in_newline_from_string_p)
14118 glyph_after = end;
14119 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14122 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14123 correspond to POS_BEFORE and POS_AFTER, respectively. We
14124 need START and STOP in the order that corresponds to the
14125 row's direction as given by its reversed_p flag. If the
14126 directionality of characters between POS_BEFORE and
14127 POS_AFTER is the opposite of the row's base direction,
14128 these characters will have been reordered for display,
14129 and we need to reverse START and STOP. */
14130 if (!row->reversed_p)
14132 start = min (glyph_before, glyph_after);
14133 stop = max (glyph_before, glyph_after);
14135 else
14137 start = max (glyph_before, glyph_after);
14138 stop = min (glyph_before, glyph_after);
14140 for (glyph = start + incr;
14141 row->reversed_p ? glyph > stop : glyph < stop; )
14144 /* Any glyphs that come from the buffer are here because
14145 of bidi reordering. Skip them, and only pay
14146 attention to glyphs that came from some string. */
14147 if (STRINGP (glyph->object))
14149 Lisp_Object str;
14150 ptrdiff_t tem;
14151 /* If the display property covers the newline, we
14152 need to search for it one position farther. */
14153 ptrdiff_t lim = pos_after
14154 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14156 string_from_text_prop = 0;
14157 str = glyph->object;
14158 tem = string_buffer_position_lim (str, pos, lim, 0);
14159 if (tem == 0 /* from overlay */
14160 || pos <= tem)
14162 /* If the string from which this glyph came is
14163 found in the buffer at point, or at position
14164 that is closer to point than pos_after, then
14165 we've found the glyph we've been looking for.
14166 If it comes from an overlay (tem == 0), and
14167 it has the `cursor' property on one of its
14168 glyphs, record that glyph as a candidate for
14169 displaying the cursor. (As in the
14170 unidirectional version, we will display the
14171 cursor on the last candidate we find.) */
14172 if (tem == 0
14173 || tem == pt_old
14174 || (tem - pt_old > 0 && tem < pos_after))
14176 /* The glyphs from this string could have
14177 been reordered. Find the one with the
14178 smallest string position. Or there could
14179 be a character in the string with the
14180 `cursor' property, which means display
14181 cursor on that character's glyph. */
14182 ptrdiff_t strpos = glyph->charpos;
14184 if (tem)
14186 cursor = glyph;
14187 string_from_text_prop = 1;
14189 for ( ;
14190 (row->reversed_p ? glyph > stop : glyph < stop)
14191 && EQ (glyph->object, str);
14192 glyph += incr)
14194 Lisp_Object cprop;
14195 ptrdiff_t gpos = glyph->charpos;
14197 cprop = Fget_char_property (make_number (gpos),
14198 Qcursor,
14199 glyph->object);
14200 if (!NILP (cprop))
14202 cursor = glyph;
14203 break;
14205 if (tem && glyph->charpos < strpos)
14207 strpos = glyph->charpos;
14208 cursor = glyph;
14212 if (tem == pt_old
14213 || (tem - pt_old > 0 && tem < pos_after))
14214 goto compute_x;
14216 if (tem)
14217 pos = tem + 1; /* don't find previous instances */
14219 /* This string is not what we want; skip all of the
14220 glyphs that came from it. */
14221 while ((row->reversed_p ? glyph > stop : glyph < stop)
14222 && EQ (glyph->object, str))
14223 glyph += incr;
14225 else
14226 glyph += incr;
14229 /* If we reached the end of the line, and END was from a string,
14230 the cursor is not on this line. */
14231 if (cursor == NULL
14232 && (row->reversed_p ? glyph <= end : glyph >= end)
14233 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14234 && STRINGP (end->object)
14235 && row->continued_p)
14236 return 0;
14238 /* A truncated row may not include PT among its character positions.
14239 Setting the cursor inside the scroll margin will trigger
14240 recalculation of hscroll in hscroll_window_tree. But if a
14241 display string covers point, defer to the string-handling
14242 code below to figure this out. */
14243 else if (row->truncated_on_left_p && pt_old < bpos_min)
14245 cursor = glyph_before;
14246 x = -1;
14248 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14249 /* Zero-width characters produce no glyphs. */
14250 || (!empty_line_p
14251 && (row->reversed_p
14252 ? glyph_after > glyphs_end
14253 : glyph_after < glyphs_end)))
14255 cursor = glyph_after;
14256 x = -1;
14260 compute_x:
14261 if (cursor != NULL)
14262 glyph = cursor;
14263 else if (glyph == glyphs_end
14264 && pos_before == pos_after
14265 && STRINGP ((row->reversed_p
14266 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14267 : row->glyphs[TEXT_AREA])->object))
14269 /* If all the glyphs of this row came from strings, put the
14270 cursor on the first glyph of the row. This avoids having the
14271 cursor outside of the text area in this very rare and hard
14272 use case. */
14273 glyph =
14274 row->reversed_p
14275 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14276 : row->glyphs[TEXT_AREA];
14278 if (x < 0)
14280 struct glyph *g;
14282 /* Need to compute x that corresponds to GLYPH. */
14283 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14285 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14286 emacs_abort ();
14287 x += g->pixel_width;
14291 /* ROW could be part of a continued line, which, under bidi
14292 reordering, might have other rows whose start and end charpos
14293 occlude point. Only set w->cursor if we found a better
14294 approximation to the cursor position than we have from previously
14295 examined candidate rows belonging to the same continued line. */
14296 if (/* we already have a candidate row */
14297 w->cursor.vpos >= 0
14298 /* that candidate is not the row we are processing */
14299 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14300 /* Make sure cursor.vpos specifies a row whose start and end
14301 charpos occlude point, and it is valid candidate for being a
14302 cursor-row. This is because some callers of this function
14303 leave cursor.vpos at the row where the cursor was displayed
14304 during the last redisplay cycle. */
14305 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14306 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14307 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14309 struct glyph *g1 =
14310 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14312 /* Don't consider glyphs that are outside TEXT_AREA. */
14313 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14314 return 0;
14315 /* Keep the candidate whose buffer position is the closest to
14316 point or has the `cursor' property. */
14317 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14318 w->cursor.hpos >= 0
14319 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14320 && ((BUFFERP (g1->object)
14321 && (g1->charpos == pt_old /* an exact match always wins */
14322 || (BUFFERP (glyph->object)
14323 && eabs (g1->charpos - pt_old)
14324 < eabs (glyph->charpos - pt_old))))
14325 /* previous candidate is a glyph from a string that has
14326 a non-nil `cursor' property */
14327 || (STRINGP (g1->object)
14328 && (!NILP (Fget_char_property (make_number (g1->charpos),
14329 Qcursor, g1->object))
14330 /* previous candidate is from the same display
14331 string as this one, and the display string
14332 came from a text property */
14333 || (EQ (g1->object, glyph->object)
14334 && string_from_text_prop)
14335 /* this candidate is from newline and its
14336 position is not an exact match */
14337 || (INTEGERP (glyph->object)
14338 && glyph->charpos != pt_old)))))
14339 return 0;
14340 /* If this candidate gives an exact match, use that. */
14341 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14342 /* If this candidate is a glyph created for the
14343 terminating newline of a line, and point is on that
14344 newline, it wins because it's an exact match. */
14345 || (!row->continued_p
14346 && INTEGERP (glyph->object)
14347 && glyph->charpos == 0
14348 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14349 /* Otherwise, keep the candidate that comes from a row
14350 spanning less buffer positions. This may win when one or
14351 both candidate positions are on glyphs that came from
14352 display strings, for which we cannot compare buffer
14353 positions. */
14354 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14355 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14356 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14357 return 0;
14359 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14360 w->cursor.x = x;
14361 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14362 w->cursor.y = row->y + dy;
14364 if (w == XWINDOW (selected_window))
14366 if (!row->continued_p
14367 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14368 && row->x == 0)
14370 this_line_buffer = XBUFFER (w->contents);
14372 CHARPOS (this_line_start_pos)
14373 = MATRIX_ROW_START_CHARPOS (row) + delta;
14374 BYTEPOS (this_line_start_pos)
14375 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14377 CHARPOS (this_line_end_pos)
14378 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14379 BYTEPOS (this_line_end_pos)
14380 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14382 this_line_y = w->cursor.y;
14383 this_line_pixel_height = row->height;
14384 this_line_vpos = w->cursor.vpos;
14385 this_line_start_x = row->x;
14387 else
14388 CHARPOS (this_line_start_pos) = 0;
14391 return 1;
14395 /* Run window scroll functions, if any, for WINDOW with new window
14396 start STARTP. Sets the window start of WINDOW to that position.
14398 We assume that the window's buffer is really current. */
14400 static struct text_pos
14401 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14403 struct window *w = XWINDOW (window);
14404 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14406 eassert (current_buffer == XBUFFER (w->contents));
14408 if (!NILP (Vwindow_scroll_functions))
14410 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14411 make_number (CHARPOS (startp)));
14412 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14413 /* In case the hook functions switch buffers. */
14414 set_buffer_internal (XBUFFER (w->contents));
14417 return startp;
14421 /* Make sure the line containing the cursor is fully visible.
14422 A value of 1 means there is nothing to be done.
14423 (Either the line is fully visible, or it cannot be made so,
14424 or we cannot tell.)
14426 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14427 is higher than window.
14429 A value of 0 means the caller should do scrolling
14430 as if point had gone off the screen. */
14432 static int
14433 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14435 struct glyph_matrix *matrix;
14436 struct glyph_row *row;
14437 int window_height;
14439 if (!make_cursor_line_fully_visible_p)
14440 return 1;
14442 /* It's not always possible to find the cursor, e.g, when a window
14443 is full of overlay strings. Don't do anything in that case. */
14444 if (w->cursor.vpos < 0)
14445 return 1;
14447 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14448 row = MATRIX_ROW (matrix, w->cursor.vpos);
14450 /* If the cursor row is not partially visible, there's nothing to do. */
14451 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14452 return 1;
14454 /* If the row the cursor is in is taller than the window's height,
14455 it's not clear what to do, so do nothing. */
14456 window_height = window_box_height (w);
14457 if (row->height >= window_height)
14459 if (!force_p || MINI_WINDOW_P (w)
14460 || w->vscroll || w->cursor.vpos == 0)
14461 return 1;
14463 return 0;
14467 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14468 non-zero means only WINDOW is redisplayed in redisplay_internal.
14469 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14470 in redisplay_window to bring a partially visible line into view in
14471 the case that only the cursor has moved.
14473 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14474 last screen line's vertical height extends past the end of the screen.
14476 Value is
14478 1 if scrolling succeeded
14480 0 if scrolling didn't find point.
14482 -1 if new fonts have been loaded so that we must interrupt
14483 redisplay, adjust glyph matrices, and try again. */
14485 enum
14487 SCROLLING_SUCCESS,
14488 SCROLLING_FAILED,
14489 SCROLLING_NEED_LARGER_MATRICES
14492 /* If scroll-conservatively is more than this, never recenter.
14494 If you change this, don't forget to update the doc string of
14495 `scroll-conservatively' and the Emacs manual. */
14496 #define SCROLL_LIMIT 100
14498 static int
14499 try_scrolling (Lisp_Object window, int just_this_one_p,
14500 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14501 int temp_scroll_step, int last_line_misfit)
14503 struct window *w = XWINDOW (window);
14504 struct frame *f = XFRAME (w->frame);
14505 struct text_pos pos, startp;
14506 struct it it;
14507 int this_scroll_margin, scroll_max, rc, height;
14508 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14509 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14510 Lisp_Object aggressive;
14511 /* We will never try scrolling more than this number of lines. */
14512 int scroll_limit = SCROLL_LIMIT;
14513 int frame_line_height = default_line_pixel_height (w);
14514 int window_total_lines
14515 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14517 #ifdef GLYPH_DEBUG
14518 debug_method_add (w, "try_scrolling");
14519 #endif
14521 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14523 /* Compute scroll margin height in pixels. We scroll when point is
14524 within this distance from the top or bottom of the window. */
14525 if (scroll_margin > 0)
14526 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14527 * frame_line_height;
14528 else
14529 this_scroll_margin = 0;
14531 /* Force arg_scroll_conservatively to have a reasonable value, to
14532 avoid scrolling too far away with slow move_it_* functions. Note
14533 that the user can supply scroll-conservatively equal to
14534 `most-positive-fixnum', which can be larger than INT_MAX. */
14535 if (arg_scroll_conservatively > scroll_limit)
14537 arg_scroll_conservatively = scroll_limit + 1;
14538 scroll_max = scroll_limit * frame_line_height;
14540 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14541 /* Compute how much we should try to scroll maximally to bring
14542 point into view. */
14543 scroll_max = (max (scroll_step,
14544 max (arg_scroll_conservatively, temp_scroll_step))
14545 * frame_line_height);
14546 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14547 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14548 /* We're trying to scroll because of aggressive scrolling but no
14549 scroll_step is set. Choose an arbitrary one. */
14550 scroll_max = 10 * frame_line_height;
14551 else
14552 scroll_max = 0;
14554 too_near_end:
14556 /* Decide whether to scroll down. */
14557 if (PT > CHARPOS (startp))
14559 int scroll_margin_y;
14561 /* Compute the pixel ypos of the scroll margin, then move IT to
14562 either that ypos or PT, whichever comes first. */
14563 start_display (&it, w, startp);
14564 scroll_margin_y = it.last_visible_y - this_scroll_margin
14565 - frame_line_height * extra_scroll_margin_lines;
14566 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14567 (MOVE_TO_POS | MOVE_TO_Y));
14569 if (PT > CHARPOS (it.current.pos))
14571 int y0 = line_bottom_y (&it);
14572 /* Compute how many pixels below window bottom to stop searching
14573 for PT. This avoids costly search for PT that is far away if
14574 the user limited scrolling by a small number of lines, but
14575 always finds PT if scroll_conservatively is set to a large
14576 number, such as most-positive-fixnum. */
14577 int slack = max (scroll_max, 10 * frame_line_height);
14578 int y_to_move = it.last_visible_y + slack;
14580 /* Compute the distance from the scroll margin to PT or to
14581 the scroll limit, whichever comes first. This should
14582 include the height of the cursor line, to make that line
14583 fully visible. */
14584 move_it_to (&it, PT, -1, y_to_move,
14585 -1, MOVE_TO_POS | MOVE_TO_Y);
14586 dy = line_bottom_y (&it) - y0;
14588 if (dy > scroll_max)
14589 return SCROLLING_FAILED;
14591 if (dy > 0)
14592 scroll_down_p = 1;
14596 if (scroll_down_p)
14598 /* Point is in or below the bottom scroll margin, so move the
14599 window start down. If scrolling conservatively, move it just
14600 enough down to make point visible. If scroll_step is set,
14601 move it down by scroll_step. */
14602 if (arg_scroll_conservatively)
14603 amount_to_scroll
14604 = min (max (dy, frame_line_height),
14605 frame_line_height * arg_scroll_conservatively);
14606 else if (scroll_step || temp_scroll_step)
14607 amount_to_scroll = scroll_max;
14608 else
14610 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14611 height = WINDOW_BOX_TEXT_HEIGHT (w);
14612 if (NUMBERP (aggressive))
14614 double float_amount = XFLOATINT (aggressive) * height;
14615 int aggressive_scroll = float_amount;
14616 if (aggressive_scroll == 0 && float_amount > 0)
14617 aggressive_scroll = 1;
14618 /* Don't let point enter the scroll margin near top of
14619 the window. This could happen if the value of
14620 scroll_up_aggressively is too large and there are
14621 non-zero margins, because scroll_up_aggressively
14622 means put point that fraction of window height
14623 _from_the_bottom_margin_. */
14624 if (aggressive_scroll + 2*this_scroll_margin > height)
14625 aggressive_scroll = height - 2*this_scroll_margin;
14626 amount_to_scroll = dy + aggressive_scroll;
14630 if (amount_to_scroll <= 0)
14631 return SCROLLING_FAILED;
14633 start_display (&it, w, startp);
14634 if (arg_scroll_conservatively <= scroll_limit)
14635 move_it_vertically (&it, amount_to_scroll);
14636 else
14638 /* Extra precision for users who set scroll-conservatively
14639 to a large number: make sure the amount we scroll
14640 the window start is never less than amount_to_scroll,
14641 which was computed as distance from window bottom to
14642 point. This matters when lines at window top and lines
14643 below window bottom have different height. */
14644 struct it it1;
14645 void *it1data = NULL;
14646 /* We use a temporary it1 because line_bottom_y can modify
14647 its argument, if it moves one line down; see there. */
14648 int start_y;
14650 SAVE_IT (it1, it, it1data);
14651 start_y = line_bottom_y (&it1);
14652 do {
14653 RESTORE_IT (&it, &it, it1data);
14654 move_it_by_lines (&it, 1);
14655 SAVE_IT (it1, it, it1data);
14656 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14659 /* If STARTP is unchanged, move it down another screen line. */
14660 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14661 move_it_by_lines (&it, 1);
14662 startp = it.current.pos;
14664 else
14666 struct text_pos scroll_margin_pos = startp;
14667 int y_offset = 0;
14669 /* See if point is inside the scroll margin at the top of the
14670 window. */
14671 if (this_scroll_margin)
14673 int y_start;
14675 start_display (&it, w, startp);
14676 y_start = it.current_y;
14677 move_it_vertically (&it, this_scroll_margin);
14678 scroll_margin_pos = it.current.pos;
14679 /* If we didn't move enough before hitting ZV, request
14680 additional amount of scroll, to move point out of the
14681 scroll margin. */
14682 if (IT_CHARPOS (it) == ZV
14683 && it.current_y - y_start < this_scroll_margin)
14684 y_offset = this_scroll_margin - (it.current_y - y_start);
14687 if (PT < CHARPOS (scroll_margin_pos))
14689 /* Point is in the scroll margin at the top of the window or
14690 above what is displayed in the window. */
14691 int y0, y_to_move;
14693 /* Compute the vertical distance from PT to the scroll
14694 margin position. Move as far as scroll_max allows, or
14695 one screenful, or 10 screen lines, whichever is largest.
14696 Give up if distance is greater than scroll_max or if we
14697 didn't reach the scroll margin position. */
14698 SET_TEXT_POS (pos, PT, PT_BYTE);
14699 start_display (&it, w, pos);
14700 y0 = it.current_y;
14701 y_to_move = max (it.last_visible_y,
14702 max (scroll_max, 10 * frame_line_height));
14703 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14704 y_to_move, -1,
14705 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14706 dy = it.current_y - y0;
14707 if (dy > scroll_max
14708 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14709 return SCROLLING_FAILED;
14711 /* Additional scroll for when ZV was too close to point. */
14712 dy += y_offset;
14714 /* Compute new window start. */
14715 start_display (&it, w, startp);
14717 if (arg_scroll_conservatively)
14718 amount_to_scroll = max (dy, frame_line_height *
14719 max (scroll_step, temp_scroll_step));
14720 else if (scroll_step || temp_scroll_step)
14721 amount_to_scroll = scroll_max;
14722 else
14724 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14725 height = WINDOW_BOX_TEXT_HEIGHT (w);
14726 if (NUMBERP (aggressive))
14728 double float_amount = XFLOATINT (aggressive) * height;
14729 int aggressive_scroll = float_amount;
14730 if (aggressive_scroll == 0 && float_amount > 0)
14731 aggressive_scroll = 1;
14732 /* Don't let point enter the scroll margin near
14733 bottom of the window, if the value of
14734 scroll_down_aggressively happens to be too
14735 large. */
14736 if (aggressive_scroll + 2*this_scroll_margin > height)
14737 aggressive_scroll = height - 2*this_scroll_margin;
14738 amount_to_scroll = dy + aggressive_scroll;
14742 if (amount_to_scroll <= 0)
14743 return SCROLLING_FAILED;
14745 move_it_vertically_backward (&it, amount_to_scroll);
14746 startp = it.current.pos;
14750 /* Run window scroll functions. */
14751 startp = run_window_scroll_functions (window, startp);
14753 /* Display the window. Give up if new fonts are loaded, or if point
14754 doesn't appear. */
14755 if (!try_window (window, startp, 0))
14756 rc = SCROLLING_NEED_LARGER_MATRICES;
14757 else if (w->cursor.vpos < 0)
14759 clear_glyph_matrix (w->desired_matrix);
14760 rc = SCROLLING_FAILED;
14762 else
14764 /* Maybe forget recorded base line for line number display. */
14765 if (!just_this_one_p
14766 || current_buffer->clip_changed
14767 || BEG_UNCHANGED < CHARPOS (startp))
14768 w->base_line_number = 0;
14770 /* If cursor ends up on a partially visible line,
14771 treat that as being off the bottom of the screen. */
14772 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14773 /* It's possible that the cursor is on the first line of the
14774 buffer, which is partially obscured due to a vscroll
14775 (Bug#7537). In that case, avoid looping forever . */
14776 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14778 clear_glyph_matrix (w->desired_matrix);
14779 ++extra_scroll_margin_lines;
14780 goto too_near_end;
14782 rc = SCROLLING_SUCCESS;
14785 return rc;
14789 /* Compute a suitable window start for window W if display of W starts
14790 on a continuation line. Value is non-zero if a new window start
14791 was computed.
14793 The new window start will be computed, based on W's width, starting
14794 from the start of the continued line. It is the start of the
14795 screen line with the minimum distance from the old start W->start. */
14797 static int
14798 compute_window_start_on_continuation_line (struct window *w)
14800 struct text_pos pos, start_pos;
14801 int window_start_changed_p = 0;
14803 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14805 /* If window start is on a continuation line... Window start may be
14806 < BEGV in case there's invisible text at the start of the
14807 buffer (M-x rmail, for example). */
14808 if (CHARPOS (start_pos) > BEGV
14809 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14811 struct it it;
14812 struct glyph_row *row;
14814 /* Handle the case that the window start is out of range. */
14815 if (CHARPOS (start_pos) < BEGV)
14816 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14817 else if (CHARPOS (start_pos) > ZV)
14818 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14820 /* Find the start of the continued line. This should be fast
14821 because find_newline is fast (newline cache). */
14822 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14823 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14824 row, DEFAULT_FACE_ID);
14825 reseat_at_previous_visible_line_start (&it);
14827 /* If the line start is "too far" away from the window start,
14828 say it takes too much time to compute a new window start. */
14829 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14830 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14832 int min_distance, distance;
14834 /* Move forward by display lines to find the new window
14835 start. If window width was enlarged, the new start can
14836 be expected to be > the old start. If window width was
14837 decreased, the new window start will be < the old start.
14838 So, we're looking for the display line start with the
14839 minimum distance from the old window start. */
14840 pos = it.current.pos;
14841 min_distance = INFINITY;
14842 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14843 distance < min_distance)
14845 min_distance = distance;
14846 pos = it.current.pos;
14847 if (it.line_wrap == WORD_WRAP)
14849 /* Under WORD_WRAP, move_it_by_lines is likely to
14850 overshoot and stop not at the first, but the
14851 second character from the left margin. So in
14852 that case, we need a more tight control on the X
14853 coordinate of the iterator than move_it_by_lines
14854 promises in its contract. The method is to first
14855 go to the last (rightmost) visible character of a
14856 line, then move to the leftmost character on the
14857 next line in a separate call. */
14858 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14859 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14860 move_it_to (&it, ZV, 0,
14861 it.current_y + it.max_ascent + it.max_descent, -1,
14862 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14864 else
14865 move_it_by_lines (&it, 1);
14868 /* Set the window start there. */
14869 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14870 window_start_changed_p = 1;
14874 return window_start_changed_p;
14878 /* Try cursor movement in case text has not changed in window WINDOW,
14879 with window start STARTP. Value is
14881 CURSOR_MOVEMENT_SUCCESS if successful
14883 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14885 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14886 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14887 we want to scroll as if scroll-step were set to 1. See the code.
14889 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14890 which case we have to abort this redisplay, and adjust matrices
14891 first. */
14893 enum
14895 CURSOR_MOVEMENT_SUCCESS,
14896 CURSOR_MOVEMENT_CANNOT_BE_USED,
14897 CURSOR_MOVEMENT_MUST_SCROLL,
14898 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14901 static int
14902 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14904 struct window *w = XWINDOW (window);
14905 struct frame *f = XFRAME (w->frame);
14906 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14908 #ifdef GLYPH_DEBUG
14909 if (inhibit_try_cursor_movement)
14910 return rc;
14911 #endif
14913 /* Previously, there was a check for Lisp integer in the
14914 if-statement below. Now, this field is converted to
14915 ptrdiff_t, thus zero means invalid position in a buffer. */
14916 eassert (w->last_point > 0);
14917 /* Likewise there was a check whether window_end_vpos is nil or larger
14918 than the window. Now window_end_vpos is int and so never nil, but
14919 let's leave eassert to check whether it fits in the window. */
14920 eassert (w->window_end_vpos < w->current_matrix->nrows);
14922 /* Handle case where text has not changed, only point, and it has
14923 not moved off the frame. */
14924 if (/* Point may be in this window. */
14925 PT >= CHARPOS (startp)
14926 /* Selective display hasn't changed. */
14927 && !current_buffer->clip_changed
14928 /* Function force-mode-line-update is used to force a thorough
14929 redisplay. It sets either windows_or_buffers_changed or
14930 update_mode_lines. So don't take a shortcut here for these
14931 cases. */
14932 && !update_mode_lines
14933 && !windows_or_buffers_changed
14934 && !f->cursor_type_changed
14935 && NILP (Vshow_trailing_whitespace)
14936 /* This code is not used for mini-buffer for the sake of the case
14937 of redisplaying to replace an echo area message; since in
14938 that case the mini-buffer contents per se are usually
14939 unchanged. This code is of no real use in the mini-buffer
14940 since the handling of this_line_start_pos, etc., in redisplay
14941 handles the same cases. */
14942 && !EQ (window, minibuf_window)
14943 && (FRAME_WINDOW_P (f)
14944 || !overlay_arrow_in_current_buffer_p ()))
14946 int this_scroll_margin, top_scroll_margin;
14947 struct glyph_row *row = NULL;
14948 int frame_line_height = default_line_pixel_height (w);
14949 int window_total_lines
14950 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14952 #ifdef GLYPH_DEBUG
14953 debug_method_add (w, "cursor movement");
14954 #endif
14956 /* Scroll if point within this distance from the top or bottom
14957 of the window. This is a pixel value. */
14958 if (scroll_margin > 0)
14960 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
14961 this_scroll_margin *= frame_line_height;
14963 else
14964 this_scroll_margin = 0;
14966 top_scroll_margin = this_scroll_margin;
14967 if (WINDOW_WANTS_HEADER_LINE_P (w))
14968 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14970 /* Start with the row the cursor was displayed during the last
14971 not paused redisplay. Give up if that row is not valid. */
14972 if (w->last_cursor_vpos < 0
14973 || w->last_cursor_vpos >= w->current_matrix->nrows)
14974 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14975 else
14977 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
14978 if (row->mode_line_p)
14979 ++row;
14980 if (!row->enabled_p)
14981 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14984 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14986 int scroll_p = 0, must_scroll = 0;
14987 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14989 if (PT > w->last_point)
14991 /* Point has moved forward. */
14992 while (MATRIX_ROW_END_CHARPOS (row) < PT
14993 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14995 eassert (row->enabled_p);
14996 ++row;
14999 /* If the end position of a row equals the start
15000 position of the next row, and PT is at that position,
15001 we would rather display cursor in the next line. */
15002 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15003 && MATRIX_ROW_END_CHARPOS (row) == PT
15004 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15005 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15006 && !cursor_row_p (row))
15007 ++row;
15009 /* If within the scroll margin, scroll. Note that
15010 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15011 the next line would be drawn, and that
15012 this_scroll_margin can be zero. */
15013 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15014 || PT > MATRIX_ROW_END_CHARPOS (row)
15015 /* Line is completely visible last line in window
15016 and PT is to be set in the next line. */
15017 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15018 && PT == MATRIX_ROW_END_CHARPOS (row)
15019 && !row->ends_at_zv_p
15020 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15021 scroll_p = 1;
15023 else if (PT < w->last_point)
15025 /* Cursor has to be moved backward. Note that PT >=
15026 CHARPOS (startp) because of the outer if-statement. */
15027 while (!row->mode_line_p
15028 && (MATRIX_ROW_START_CHARPOS (row) > PT
15029 || (MATRIX_ROW_START_CHARPOS (row) == PT
15030 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15031 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15032 row > w->current_matrix->rows
15033 && (row-1)->ends_in_newline_from_string_p))))
15034 && (row->y > top_scroll_margin
15035 || CHARPOS (startp) == BEGV))
15037 eassert (row->enabled_p);
15038 --row;
15041 /* Consider the following case: Window starts at BEGV,
15042 there is invisible, intangible text at BEGV, so that
15043 display starts at some point START > BEGV. It can
15044 happen that we are called with PT somewhere between
15045 BEGV and START. Try to handle that case. */
15046 if (row < w->current_matrix->rows
15047 || row->mode_line_p)
15049 row = w->current_matrix->rows;
15050 if (row->mode_line_p)
15051 ++row;
15054 /* Due to newlines in overlay strings, we may have to
15055 skip forward over overlay strings. */
15056 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15057 && MATRIX_ROW_END_CHARPOS (row) == PT
15058 && !cursor_row_p (row))
15059 ++row;
15061 /* If within the scroll margin, scroll. */
15062 if (row->y < top_scroll_margin
15063 && CHARPOS (startp) != BEGV)
15064 scroll_p = 1;
15066 else
15068 /* Cursor did not move. So don't scroll even if cursor line
15069 is partially visible, as it was so before. */
15070 rc = CURSOR_MOVEMENT_SUCCESS;
15073 if (PT < MATRIX_ROW_START_CHARPOS (row)
15074 || PT > MATRIX_ROW_END_CHARPOS (row))
15076 /* if PT is not in the glyph row, give up. */
15077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15078 must_scroll = 1;
15080 else if (rc != CURSOR_MOVEMENT_SUCCESS
15081 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15083 struct glyph_row *row1;
15085 /* If rows are bidi-reordered and point moved, back up
15086 until we find a row that does not belong to a
15087 continuation line. This is because we must consider
15088 all rows of a continued line as candidates for the
15089 new cursor positioning, since row start and end
15090 positions change non-linearly with vertical position
15091 in such rows. */
15092 /* FIXME: Revisit this when glyph ``spilling'' in
15093 continuation lines' rows is implemented for
15094 bidi-reordered rows. */
15095 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15096 MATRIX_ROW_CONTINUATION_LINE_P (row);
15097 --row)
15099 /* If we hit the beginning of the displayed portion
15100 without finding the first row of a continued
15101 line, give up. */
15102 if (row <= row1)
15104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15105 break;
15107 eassert (row->enabled_p);
15110 if (must_scroll)
15112 else if (rc != CURSOR_MOVEMENT_SUCCESS
15113 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15114 /* Make sure this isn't a header line by any chance, since
15115 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15116 && !row->mode_line_p
15117 && make_cursor_line_fully_visible_p)
15119 if (PT == MATRIX_ROW_END_CHARPOS (row)
15120 && !row->ends_at_zv_p
15121 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15123 else if (row->height > window_box_height (w))
15125 /* If we end up in a partially visible line, let's
15126 make it fully visible, except when it's taller
15127 than the window, in which case we can't do much
15128 about it. */
15129 *scroll_step = 1;
15130 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 else
15134 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15135 if (!cursor_row_fully_visible_p (w, 0, 1))
15136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15137 else
15138 rc = CURSOR_MOVEMENT_SUCCESS;
15141 else if (scroll_p)
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15143 else if (rc != CURSOR_MOVEMENT_SUCCESS
15144 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15146 /* With bidi-reordered rows, there could be more than
15147 one candidate row whose start and end positions
15148 occlude point. We need to let set_cursor_from_row
15149 find the best candidate. */
15150 /* FIXME: Revisit this when glyph ``spilling'' in
15151 continuation lines' rows is implemented for
15152 bidi-reordered rows. */
15153 int rv = 0;
15157 int at_zv_p = 0, exact_match_p = 0;
15159 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15160 && PT <= MATRIX_ROW_END_CHARPOS (row)
15161 && cursor_row_p (row))
15162 rv |= set_cursor_from_row (w, row, w->current_matrix,
15163 0, 0, 0, 0);
15164 /* As soon as we've found the exact match for point,
15165 or the first suitable row whose ends_at_zv_p flag
15166 is set, we are done. */
15167 at_zv_p =
15168 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15169 if (rv && !at_zv_p
15170 && w->cursor.hpos >= 0
15171 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15172 w->cursor.vpos))
15174 struct glyph_row *candidate =
15175 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15176 struct glyph *g =
15177 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15178 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15180 exact_match_p =
15181 (BUFFERP (g->object) && g->charpos == PT)
15182 || (INTEGERP (g->object)
15183 && (g->charpos == PT
15184 || (g->charpos == 0 && endpos - 1 == PT)));
15186 if (rv && (at_zv_p || exact_match_p))
15188 rc = CURSOR_MOVEMENT_SUCCESS;
15189 break;
15191 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15192 break;
15193 ++row;
15195 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15196 || row->continued_p)
15197 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15198 || (MATRIX_ROW_START_CHARPOS (row) == PT
15199 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15200 /* If we didn't find any candidate rows, or exited the
15201 loop before all the candidates were examined, signal
15202 to the caller that this method failed. */
15203 if (rc != CURSOR_MOVEMENT_SUCCESS
15204 && !(rv
15205 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15206 && !row->continued_p))
15207 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15208 else if (rv)
15209 rc = CURSOR_MOVEMENT_SUCCESS;
15211 else
15215 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15217 rc = CURSOR_MOVEMENT_SUCCESS;
15218 break;
15220 ++row;
15222 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15223 && MATRIX_ROW_START_CHARPOS (row) == PT
15224 && cursor_row_p (row));
15229 return rc;
15232 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15233 static
15234 #endif
15235 void
15236 set_vertical_scroll_bar (struct window *w)
15238 ptrdiff_t start, end, whole;
15240 /* Calculate the start and end positions for the current window.
15241 At some point, it would be nice to choose between scrollbars
15242 which reflect the whole buffer size, with special markers
15243 indicating narrowing, and scrollbars which reflect only the
15244 visible region.
15246 Note that mini-buffers sometimes aren't displaying any text. */
15247 if (!MINI_WINDOW_P (w)
15248 || (w == XWINDOW (minibuf_window)
15249 && NILP (echo_area_buffer[0])))
15251 struct buffer *buf = XBUFFER (w->contents);
15252 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15253 start = marker_position (w->start) - BUF_BEGV (buf);
15254 /* I don't think this is guaranteed to be right. For the
15255 moment, we'll pretend it is. */
15256 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15258 if (end < start)
15259 end = start;
15260 if (whole < (end - start))
15261 whole = end - start;
15263 else
15264 start = end = whole = 0;
15266 /* Indicate what this scroll bar ought to be displaying now. */
15267 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15268 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15269 (w, end - start, whole, start);
15273 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15274 selected_window is redisplayed.
15276 We can return without actually redisplaying the window if fonts has been
15277 changed on window's frame. In that case, redisplay_internal will retry. */
15279 static void
15280 redisplay_window (Lisp_Object window, int just_this_one_p)
15282 struct window *w = XWINDOW (window);
15283 struct frame *f = XFRAME (w->frame);
15284 struct buffer *buffer = XBUFFER (w->contents);
15285 struct buffer *old = current_buffer;
15286 struct text_pos lpoint, opoint, startp;
15287 int update_mode_line;
15288 int tem;
15289 struct it it;
15290 /* Record it now because it's overwritten. */
15291 int current_matrix_up_to_date_p = 0;
15292 int used_current_matrix_p = 0;
15293 /* This is less strict than current_matrix_up_to_date_p.
15294 It indicates that the buffer contents and narrowing are unchanged. */
15295 int buffer_unchanged_p = 0;
15296 int temp_scroll_step = 0;
15297 ptrdiff_t count = SPECPDL_INDEX ();
15298 int rc;
15299 int centering_position = -1;
15300 int last_line_misfit = 0;
15301 ptrdiff_t beg_unchanged, end_unchanged;
15302 int frame_line_height;
15304 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15305 opoint = lpoint;
15307 #ifdef GLYPH_DEBUG
15308 *w->desired_matrix->method = 0;
15309 #endif
15311 /* Make sure that both W's markers are valid. */
15312 eassert (XMARKER (w->start)->buffer == buffer);
15313 eassert (XMARKER (w->pointm)->buffer == buffer);
15315 restart:
15316 reconsider_clip_changes (w);
15317 frame_line_height = default_line_pixel_height (w);
15319 /* Has the mode line to be updated? */
15320 update_mode_line = (w->update_mode_line
15321 || update_mode_lines
15322 || buffer->clip_changed
15323 || buffer->prevent_redisplay_optimizations_p);
15325 if (MINI_WINDOW_P (w))
15327 if (w == XWINDOW (echo_area_window)
15328 && !NILP (echo_area_buffer[0]))
15330 if (update_mode_line)
15331 /* We may have to update a tty frame's menu bar or a
15332 tool-bar. Example `M-x C-h C-h C-g'. */
15333 goto finish_menu_bars;
15334 else
15335 /* We've already displayed the echo area glyphs in this window. */
15336 goto finish_scroll_bars;
15338 else if ((w != XWINDOW (minibuf_window)
15339 || minibuf_level == 0)
15340 /* When buffer is nonempty, redisplay window normally. */
15341 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15342 /* Quail displays non-mini buffers in minibuffer window.
15343 In that case, redisplay the window normally. */
15344 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15346 /* W is a mini-buffer window, but it's not active, so clear
15347 it. */
15348 int yb = window_text_bottom_y (w);
15349 struct glyph_row *row;
15350 int y;
15352 for (y = 0, row = w->desired_matrix->rows;
15353 y < yb;
15354 y += row->height, ++row)
15355 blank_row (w, row, y);
15356 goto finish_scroll_bars;
15359 clear_glyph_matrix (w->desired_matrix);
15362 /* Otherwise set up data on this window; select its buffer and point
15363 value. */
15364 /* Really select the buffer, for the sake of buffer-local
15365 variables. */
15366 set_buffer_internal_1 (XBUFFER (w->contents));
15368 current_matrix_up_to_date_p
15369 = (w->window_end_valid
15370 && !current_buffer->clip_changed
15371 && !current_buffer->prevent_redisplay_optimizations_p
15372 && !window_outdated (w));
15374 /* Run the window-bottom-change-functions
15375 if it is possible that the text on the screen has changed
15376 (either due to modification of the text, or any other reason). */
15377 if (!current_matrix_up_to_date_p
15378 && !NILP (Vwindow_text_change_functions))
15380 safe_run_hooks (Qwindow_text_change_functions);
15381 goto restart;
15384 beg_unchanged = BEG_UNCHANGED;
15385 end_unchanged = END_UNCHANGED;
15387 SET_TEXT_POS (opoint, PT, PT_BYTE);
15389 specbind (Qinhibit_point_motion_hooks, Qt);
15391 buffer_unchanged_p
15392 = (w->window_end_valid
15393 && !current_buffer->clip_changed
15394 && !window_outdated (w));
15396 /* When windows_or_buffers_changed is non-zero, we can't rely
15397 on the window end being valid, so set it to zero there. */
15398 if (windows_or_buffers_changed)
15400 /* If window starts on a continuation line, maybe adjust the
15401 window start in case the window's width changed. */
15402 if (XMARKER (w->start)->buffer == current_buffer)
15403 compute_window_start_on_continuation_line (w);
15405 w->window_end_valid = 0;
15406 /* If so, we also can't rely on current matrix
15407 and should not fool try_cursor_movement below. */
15408 current_matrix_up_to_date_p = 0;
15411 /* Some sanity checks. */
15412 CHECK_WINDOW_END (w);
15413 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15414 emacs_abort ();
15415 if (BYTEPOS (opoint) < CHARPOS (opoint))
15416 emacs_abort ();
15418 if (mode_line_update_needed (w))
15419 update_mode_line = 1;
15421 /* Point refers normally to the selected window. For any other
15422 window, set up appropriate value. */
15423 if (!EQ (window, selected_window))
15425 ptrdiff_t new_pt = marker_position (w->pointm);
15426 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15427 if (new_pt < BEGV)
15429 new_pt = BEGV;
15430 new_pt_byte = BEGV_BYTE;
15431 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15433 else if (new_pt > (ZV - 1))
15435 new_pt = ZV;
15436 new_pt_byte = ZV_BYTE;
15437 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15440 /* We don't use SET_PT so that the point-motion hooks don't run. */
15441 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15444 /* If any of the character widths specified in the display table
15445 have changed, invalidate the width run cache. It's true that
15446 this may be a bit late to catch such changes, but the rest of
15447 redisplay goes (non-fatally) haywire when the display table is
15448 changed, so why should we worry about doing any better? */
15449 if (current_buffer->width_run_cache)
15451 struct Lisp_Char_Table *disptab = buffer_display_table ();
15453 if (! disptab_matches_widthtab
15454 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15456 invalidate_region_cache (current_buffer,
15457 current_buffer->width_run_cache,
15458 BEG, Z);
15459 recompute_width_table (current_buffer, disptab);
15463 /* If window-start is screwed up, choose a new one. */
15464 if (XMARKER (w->start)->buffer != current_buffer)
15465 goto recenter;
15467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15469 /* If someone specified a new starting point but did not insist,
15470 check whether it can be used. */
15471 if (w->optional_new_start
15472 && CHARPOS (startp) >= BEGV
15473 && CHARPOS (startp) <= ZV)
15475 w->optional_new_start = 0;
15476 start_display (&it, w, startp);
15477 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15478 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15479 if (IT_CHARPOS (it) == PT)
15480 w->force_start = 1;
15481 /* IT may overshoot PT if text at PT is invisible. */
15482 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15483 w->force_start = 1;
15486 force_start:
15488 /* Handle case where place to start displaying has been specified,
15489 unless the specified location is outside the accessible range. */
15490 if (w->force_start || window_frozen_p (w))
15492 /* We set this later on if we have to adjust point. */
15493 int new_vpos = -1;
15495 w->force_start = 0;
15496 w->vscroll = 0;
15497 w->window_end_valid = 0;
15499 /* Forget any recorded base line for line number display. */
15500 if (!buffer_unchanged_p)
15501 w->base_line_number = 0;
15503 /* Redisplay the mode line. Select the buffer properly for that.
15504 Also, run the hook window-scroll-functions
15505 because we have scrolled. */
15506 /* Note, we do this after clearing force_start because
15507 if there's an error, it is better to forget about force_start
15508 than to get into an infinite loop calling the hook functions
15509 and having them get more errors. */
15510 if (!update_mode_line
15511 || ! NILP (Vwindow_scroll_functions))
15513 update_mode_line = 1;
15514 w->update_mode_line = 1;
15515 startp = run_window_scroll_functions (window, startp);
15518 if (CHARPOS (startp) < BEGV)
15519 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15520 else if (CHARPOS (startp) > ZV)
15521 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15523 /* Redisplay, then check if cursor has been set during the
15524 redisplay. Give up if new fonts were loaded. */
15525 /* We used to issue a CHECK_MARGINS argument to try_window here,
15526 but this causes scrolling to fail when point begins inside
15527 the scroll margin (bug#148) -- cyd */
15528 if (!try_window (window, startp, 0))
15530 w->force_start = 1;
15531 clear_glyph_matrix (w->desired_matrix);
15532 goto need_larger_matrices;
15535 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15537 /* If point does not appear, try to move point so it does
15538 appear. The desired matrix has been built above, so we
15539 can use it here. */
15540 new_vpos = window_box_height (w) / 2;
15543 if (!cursor_row_fully_visible_p (w, 0, 0))
15545 /* Point does appear, but on a line partly visible at end of window.
15546 Move it back to a fully-visible line. */
15547 new_vpos = window_box_height (w);
15549 else if (w->cursor.vpos >= 0)
15551 /* Some people insist on not letting point enter the scroll
15552 margin, even though this part handles windows that didn't
15553 scroll at all. */
15554 int window_total_lines
15555 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15556 int margin = min (scroll_margin, window_total_lines / 4);
15557 int pixel_margin = margin * frame_line_height;
15558 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15560 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15561 below, which finds the row to move point to, advances by
15562 the Y coordinate of the _next_ row, see the definition of
15563 MATRIX_ROW_BOTTOM_Y. */
15564 if (w->cursor.vpos < margin + header_line)
15566 w->cursor.vpos = -1;
15567 clear_glyph_matrix (w->desired_matrix);
15568 goto try_to_scroll;
15570 else
15572 int window_height = window_box_height (w);
15574 if (header_line)
15575 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15576 if (w->cursor.y >= window_height - pixel_margin)
15578 w->cursor.vpos = -1;
15579 clear_glyph_matrix (w->desired_matrix);
15580 goto try_to_scroll;
15585 /* If we need to move point for either of the above reasons,
15586 now actually do it. */
15587 if (new_vpos >= 0)
15589 struct glyph_row *row;
15591 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15592 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15593 ++row;
15595 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15596 MATRIX_ROW_START_BYTEPOS (row));
15598 if (w != XWINDOW (selected_window))
15599 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15600 else if (current_buffer == old)
15601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15603 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15605 /* If we are highlighting the region, then we just changed
15606 the region, so redisplay to show it. */
15607 /* FIXME: We need to (re)run pre-redisplay-function! */
15608 /* if (markpos_of_region () >= 0)
15610 clear_glyph_matrix (w->desired_matrix);
15611 if (!try_window (window, startp, 0))
15612 goto need_larger_matrices;
15617 #ifdef GLYPH_DEBUG
15618 debug_method_add (w, "forced window start");
15619 #endif
15620 goto done;
15623 /* Handle case where text has not changed, only point, and it has
15624 not moved off the frame, and we are not retrying after hscroll.
15625 (current_matrix_up_to_date_p is nonzero when retrying.) */
15626 if (current_matrix_up_to_date_p
15627 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15628 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15630 switch (rc)
15632 case CURSOR_MOVEMENT_SUCCESS:
15633 used_current_matrix_p = 1;
15634 goto done;
15636 case CURSOR_MOVEMENT_MUST_SCROLL:
15637 goto try_to_scroll;
15639 default:
15640 emacs_abort ();
15643 /* If current starting point was originally the beginning of a line
15644 but no longer is, find a new starting point. */
15645 else if (w->start_at_line_beg
15646 && !(CHARPOS (startp) <= BEGV
15647 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15649 #ifdef GLYPH_DEBUG
15650 debug_method_add (w, "recenter 1");
15651 #endif
15652 goto recenter;
15655 /* Try scrolling with try_window_id. Value is > 0 if update has
15656 been done, it is -1 if we know that the same window start will
15657 not work. It is 0 if unsuccessful for some other reason. */
15658 else if ((tem = try_window_id (w)) != 0)
15660 #ifdef GLYPH_DEBUG
15661 debug_method_add (w, "try_window_id %d", tem);
15662 #endif
15664 if (f->fonts_changed)
15665 goto need_larger_matrices;
15666 if (tem > 0)
15667 goto done;
15669 /* Otherwise try_window_id has returned -1 which means that we
15670 don't want the alternative below this comment to execute. */
15672 else if (CHARPOS (startp) >= BEGV
15673 && CHARPOS (startp) <= ZV
15674 && PT >= CHARPOS (startp)
15675 && (CHARPOS (startp) < ZV
15676 /* Avoid starting at end of buffer. */
15677 || CHARPOS (startp) == BEGV
15678 || !window_outdated (w)))
15680 int d1, d2, d3, d4, d5, d6;
15682 /* If first window line is a continuation line, and window start
15683 is inside the modified region, but the first change is before
15684 current window start, we must select a new window start.
15686 However, if this is the result of a down-mouse event (e.g. by
15687 extending the mouse-drag-overlay), we don't want to select a
15688 new window start, since that would change the position under
15689 the mouse, resulting in an unwanted mouse-movement rather
15690 than a simple mouse-click. */
15691 if (!w->start_at_line_beg
15692 && NILP (do_mouse_tracking)
15693 && CHARPOS (startp) > BEGV
15694 && CHARPOS (startp) > BEG + beg_unchanged
15695 && CHARPOS (startp) <= Z - end_unchanged
15696 /* Even if w->start_at_line_beg is nil, a new window may
15697 start at a line_beg, since that's how set_buffer_window
15698 sets it. So, we need to check the return value of
15699 compute_window_start_on_continuation_line. (See also
15700 bug#197). */
15701 && XMARKER (w->start)->buffer == current_buffer
15702 && compute_window_start_on_continuation_line (w)
15703 /* It doesn't make sense to force the window start like we
15704 do at label force_start if it is already known that point
15705 will not be visible in the resulting window, because
15706 doing so will move point from its correct position
15707 instead of scrolling the window to bring point into view.
15708 See bug#9324. */
15709 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15711 w->force_start = 1;
15712 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15713 goto force_start;
15716 #ifdef GLYPH_DEBUG
15717 debug_method_add (w, "same window start");
15718 #endif
15720 /* Try to redisplay starting at same place as before.
15721 If point has not moved off frame, accept the results. */
15722 if (!current_matrix_up_to_date_p
15723 /* Don't use try_window_reusing_current_matrix in this case
15724 because a window scroll function can have changed the
15725 buffer. */
15726 || !NILP (Vwindow_scroll_functions)
15727 || MINI_WINDOW_P (w)
15728 || !(used_current_matrix_p
15729 = try_window_reusing_current_matrix (w)))
15731 IF_DEBUG (debug_method_add (w, "1"));
15732 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15733 /* -1 means we need to scroll.
15734 0 means we need new matrices, but fonts_changed
15735 is set in that case, so we will detect it below. */
15736 goto try_to_scroll;
15739 if (f->fonts_changed)
15740 goto need_larger_matrices;
15742 if (w->cursor.vpos >= 0)
15744 if (!just_this_one_p
15745 || current_buffer->clip_changed
15746 || BEG_UNCHANGED < CHARPOS (startp))
15747 /* Forget any recorded base line for line number display. */
15748 w->base_line_number = 0;
15750 if (!cursor_row_fully_visible_p (w, 1, 0))
15752 clear_glyph_matrix (w->desired_matrix);
15753 last_line_misfit = 1;
15755 /* Drop through and scroll. */
15756 else
15757 goto done;
15759 else
15760 clear_glyph_matrix (w->desired_matrix);
15763 try_to_scroll:
15765 /* Redisplay the mode line. Select the buffer properly for that. */
15766 if (!update_mode_line)
15768 update_mode_line = 1;
15769 w->update_mode_line = 1;
15772 /* Try to scroll by specified few lines. */
15773 if ((scroll_conservatively
15774 || emacs_scroll_step
15775 || temp_scroll_step
15776 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15777 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15778 && CHARPOS (startp) >= BEGV
15779 && CHARPOS (startp) <= ZV)
15781 /* The function returns -1 if new fonts were loaded, 1 if
15782 successful, 0 if not successful. */
15783 int ss = try_scrolling (window, just_this_one_p,
15784 scroll_conservatively,
15785 emacs_scroll_step,
15786 temp_scroll_step, last_line_misfit);
15787 switch (ss)
15789 case SCROLLING_SUCCESS:
15790 goto done;
15792 case SCROLLING_NEED_LARGER_MATRICES:
15793 goto need_larger_matrices;
15795 case SCROLLING_FAILED:
15796 break;
15798 default:
15799 emacs_abort ();
15803 /* Finally, just choose a place to start which positions point
15804 according to user preferences. */
15806 recenter:
15808 #ifdef GLYPH_DEBUG
15809 debug_method_add (w, "recenter");
15810 #endif
15812 /* Forget any previously recorded base line for line number display. */
15813 if (!buffer_unchanged_p)
15814 w->base_line_number = 0;
15816 /* Determine the window start relative to point. */
15817 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15818 it.current_y = it.last_visible_y;
15819 if (centering_position < 0)
15821 int window_total_lines
15822 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15823 int margin =
15824 scroll_margin > 0
15825 ? min (scroll_margin, window_total_lines / 4)
15826 : 0;
15827 ptrdiff_t margin_pos = CHARPOS (startp);
15828 Lisp_Object aggressive;
15829 int scrolling_up;
15831 /* If there is a scroll margin at the top of the window, find
15832 its character position. */
15833 if (margin
15834 /* Cannot call start_display if startp is not in the
15835 accessible region of the buffer. This can happen when we
15836 have just switched to a different buffer and/or changed
15837 its restriction. In that case, startp is initialized to
15838 the character position 1 (BEGV) because we did not yet
15839 have chance to display the buffer even once. */
15840 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15842 struct it it1;
15843 void *it1data = NULL;
15845 SAVE_IT (it1, it, it1data);
15846 start_display (&it1, w, startp);
15847 move_it_vertically (&it1, margin * frame_line_height);
15848 margin_pos = IT_CHARPOS (it1);
15849 RESTORE_IT (&it, &it, it1data);
15851 scrolling_up = PT > margin_pos;
15852 aggressive =
15853 scrolling_up
15854 ? BVAR (current_buffer, scroll_up_aggressively)
15855 : BVAR (current_buffer, scroll_down_aggressively);
15857 if (!MINI_WINDOW_P (w)
15858 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15860 int pt_offset = 0;
15862 /* Setting scroll-conservatively overrides
15863 scroll-*-aggressively. */
15864 if (!scroll_conservatively && NUMBERP (aggressive))
15866 double float_amount = XFLOATINT (aggressive);
15868 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15869 if (pt_offset == 0 && float_amount > 0)
15870 pt_offset = 1;
15871 if (pt_offset && margin > 0)
15872 margin -= 1;
15874 /* Compute how much to move the window start backward from
15875 point so that point will be displayed where the user
15876 wants it. */
15877 if (scrolling_up)
15879 centering_position = it.last_visible_y;
15880 if (pt_offset)
15881 centering_position -= pt_offset;
15882 centering_position -=
15883 frame_line_height * (1 + margin + (last_line_misfit != 0))
15884 + WINDOW_HEADER_LINE_HEIGHT (w);
15885 /* Don't let point enter the scroll margin near top of
15886 the window. */
15887 if (centering_position < margin * frame_line_height)
15888 centering_position = margin * frame_line_height;
15890 else
15891 centering_position = margin * frame_line_height + pt_offset;
15893 else
15894 /* Set the window start half the height of the window backward
15895 from point. */
15896 centering_position = window_box_height (w) / 2;
15898 move_it_vertically_backward (&it, centering_position);
15900 eassert (IT_CHARPOS (it) >= BEGV);
15902 /* The function move_it_vertically_backward may move over more
15903 than the specified y-distance. If it->w is small, e.g. a
15904 mini-buffer window, we may end up in front of the window's
15905 display area. Start displaying at the start of the line
15906 containing PT in this case. */
15907 if (it.current_y <= 0)
15909 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15910 move_it_vertically_backward (&it, 0);
15911 it.current_y = 0;
15914 it.current_x = it.hpos = 0;
15916 /* Set the window start position here explicitly, to avoid an
15917 infinite loop in case the functions in window-scroll-functions
15918 get errors. */
15919 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15921 /* Run scroll hooks. */
15922 startp = run_window_scroll_functions (window, it.current.pos);
15924 /* Redisplay the window. */
15925 if (!current_matrix_up_to_date_p
15926 || windows_or_buffers_changed
15927 || f->cursor_type_changed
15928 /* Don't use try_window_reusing_current_matrix in this case
15929 because it can have changed the buffer. */
15930 || !NILP (Vwindow_scroll_functions)
15931 || !just_this_one_p
15932 || MINI_WINDOW_P (w)
15933 || !(used_current_matrix_p
15934 = try_window_reusing_current_matrix (w)))
15935 try_window (window, startp, 0);
15937 /* If new fonts have been loaded (due to fontsets), give up. We
15938 have to start a new redisplay since we need to re-adjust glyph
15939 matrices. */
15940 if (f->fonts_changed)
15941 goto need_larger_matrices;
15943 /* If cursor did not appear assume that the middle of the window is
15944 in the first line of the window. Do it again with the next line.
15945 (Imagine a window of height 100, displaying two lines of height
15946 60. Moving back 50 from it->last_visible_y will end in the first
15947 line.) */
15948 if (w->cursor.vpos < 0)
15950 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15952 clear_glyph_matrix (w->desired_matrix);
15953 move_it_by_lines (&it, 1);
15954 try_window (window, it.current.pos, 0);
15956 else if (PT < IT_CHARPOS (it))
15958 clear_glyph_matrix (w->desired_matrix);
15959 move_it_by_lines (&it, -1);
15960 try_window (window, it.current.pos, 0);
15962 else
15964 /* Not much we can do about it. */
15968 /* Consider the following case: Window starts at BEGV, there is
15969 invisible, intangible text at BEGV, so that display starts at
15970 some point START > BEGV. It can happen that we are called with
15971 PT somewhere between BEGV and START. Try to handle that case. */
15972 if (w->cursor.vpos < 0)
15974 struct glyph_row *row = w->current_matrix->rows;
15975 if (row->mode_line_p)
15976 ++row;
15977 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15980 if (!cursor_row_fully_visible_p (w, 0, 0))
15982 /* If vscroll is enabled, disable it and try again. */
15983 if (w->vscroll)
15985 w->vscroll = 0;
15986 clear_glyph_matrix (w->desired_matrix);
15987 goto recenter;
15990 /* Users who set scroll-conservatively to a large number want
15991 point just above/below the scroll margin. If we ended up
15992 with point's row partially visible, move the window start to
15993 make that row fully visible and out of the margin. */
15994 if (scroll_conservatively > SCROLL_LIMIT)
15996 int window_total_lines
15997 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
15998 int margin =
15999 scroll_margin > 0
16000 ? min (scroll_margin, window_total_lines / 4)
16001 : 0;
16002 int move_down = w->cursor.vpos >= window_total_lines / 2;
16004 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16005 clear_glyph_matrix (w->desired_matrix);
16006 if (1 == try_window (window, it.current.pos,
16007 TRY_WINDOW_CHECK_MARGINS))
16008 goto done;
16011 /* If centering point failed to make the whole line visible,
16012 put point at the top instead. That has to make the whole line
16013 visible, if it can be done. */
16014 if (centering_position == 0)
16015 goto done;
16017 clear_glyph_matrix (w->desired_matrix);
16018 centering_position = 0;
16019 goto recenter;
16022 done:
16024 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16025 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16026 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16028 /* Display the mode line, if we must. */
16029 if ((update_mode_line
16030 /* If window not full width, must redo its mode line
16031 if (a) the window to its side is being redone and
16032 (b) we do a frame-based redisplay. This is a consequence
16033 of how inverted lines are drawn in frame-based redisplay. */
16034 || (!just_this_one_p
16035 && !FRAME_WINDOW_P (f)
16036 && !WINDOW_FULL_WIDTH_P (w))
16037 /* Line number to display. */
16038 || w->base_line_pos > 0
16039 /* Column number is displayed and different from the one displayed. */
16040 || (w->column_number_displayed != -1
16041 && (w->column_number_displayed != current_column ())))
16042 /* This means that the window has a mode line. */
16043 && (WINDOW_WANTS_MODELINE_P (w)
16044 || WINDOW_WANTS_HEADER_LINE_P (w)))
16046 display_mode_lines (w);
16048 /* If mode line height has changed, arrange for a thorough
16049 immediate redisplay using the correct mode line height. */
16050 if (WINDOW_WANTS_MODELINE_P (w)
16051 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16053 f->fonts_changed = 1;
16054 w->mode_line_height = -1;
16055 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16056 = DESIRED_MODE_LINE_HEIGHT (w);
16059 /* If header line height has changed, arrange for a thorough
16060 immediate redisplay using the correct header line height. */
16061 if (WINDOW_WANTS_HEADER_LINE_P (w)
16062 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16064 f->fonts_changed = 1;
16065 w->header_line_height = -1;
16066 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16067 = DESIRED_HEADER_LINE_HEIGHT (w);
16070 if (f->fonts_changed)
16071 goto need_larger_matrices;
16074 if (!line_number_displayed && w->base_line_pos != -1)
16076 w->base_line_pos = 0;
16077 w->base_line_number = 0;
16080 finish_menu_bars:
16082 /* When we reach a frame's selected window, redo the frame's menu bar. */
16083 if (update_mode_line
16084 && EQ (FRAME_SELECTED_WINDOW (f), window))
16086 int redisplay_menu_p = 0;
16088 if (FRAME_WINDOW_P (f))
16090 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16091 || defined (HAVE_NS) || defined (USE_GTK)
16092 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16093 #else
16094 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16095 #endif
16097 else
16098 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16100 if (redisplay_menu_p)
16101 display_menu_bar (w);
16103 #ifdef HAVE_WINDOW_SYSTEM
16104 if (FRAME_WINDOW_P (f))
16106 #if defined (USE_GTK) || defined (HAVE_NS)
16107 if (FRAME_EXTERNAL_TOOL_BAR (f))
16108 redisplay_tool_bar (f);
16109 #else
16110 if (WINDOWP (f->tool_bar_window)
16111 && (FRAME_TOOL_BAR_LINES (f) > 0
16112 || !NILP (Vauto_resize_tool_bars))
16113 && redisplay_tool_bar (f))
16114 ignore_mouse_drag_p = 1;
16115 #endif
16117 #endif
16120 #ifdef HAVE_WINDOW_SYSTEM
16121 if (FRAME_WINDOW_P (f)
16122 && update_window_fringes (w, (just_this_one_p
16123 || (!used_current_matrix_p && !overlay_arrow_seen)
16124 || w->pseudo_window_p)))
16126 update_begin (f);
16127 block_input ();
16128 if (draw_window_fringes (w, 1))
16129 x_draw_vertical_border (w);
16130 unblock_input ();
16131 update_end (f);
16133 #endif /* HAVE_WINDOW_SYSTEM */
16135 /* We go to this label, with fonts_changed set, if it is
16136 necessary to try again using larger glyph matrices.
16137 We have to redeem the scroll bar even in this case,
16138 because the loop in redisplay_internal expects that. */
16139 need_larger_matrices:
16141 finish_scroll_bars:
16143 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16145 /* Set the thumb's position and size. */
16146 set_vertical_scroll_bar (w);
16148 /* Note that we actually used the scroll bar attached to this
16149 window, so it shouldn't be deleted at the end of redisplay. */
16150 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16151 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16154 /* Restore current_buffer and value of point in it. The window
16155 update may have changed the buffer, so first make sure `opoint'
16156 is still valid (Bug#6177). */
16157 if (CHARPOS (opoint) < BEGV)
16158 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16159 else if (CHARPOS (opoint) > ZV)
16160 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16161 else
16162 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16164 set_buffer_internal_1 (old);
16165 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16166 shorter. This can be caused by log truncation in *Messages*. */
16167 if (CHARPOS (lpoint) <= ZV)
16168 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16170 unbind_to (count, Qnil);
16174 /* Build the complete desired matrix of WINDOW with a window start
16175 buffer position POS.
16177 Value is 1 if successful. It is zero if fonts were loaded during
16178 redisplay which makes re-adjusting glyph matrices necessary, and -1
16179 if point would appear in the scroll margins.
16180 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16181 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16182 set in FLAGS.) */
16185 try_window (Lisp_Object window, struct text_pos pos, int flags)
16187 struct window *w = XWINDOW (window);
16188 struct it it;
16189 struct glyph_row *last_text_row = NULL;
16190 struct frame *f = XFRAME (w->frame);
16191 int frame_line_height = default_line_pixel_height (w);
16193 /* Make POS the new window start. */
16194 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16196 /* Mark cursor position as unknown. No overlay arrow seen. */
16197 w->cursor.vpos = -1;
16198 overlay_arrow_seen = 0;
16200 /* Initialize iterator and info to start at POS. */
16201 start_display (&it, w, pos);
16203 /* Display all lines of W. */
16204 while (it.current_y < it.last_visible_y)
16206 if (display_line (&it))
16207 last_text_row = it.glyph_row - 1;
16208 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16209 return 0;
16212 /* Don't let the cursor end in the scroll margins. */
16213 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16214 && !MINI_WINDOW_P (w))
16216 int this_scroll_margin;
16217 int window_total_lines
16218 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16220 if (scroll_margin > 0)
16222 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16223 this_scroll_margin *= frame_line_height;
16225 else
16226 this_scroll_margin = 0;
16228 if ((w->cursor.y >= 0 /* not vscrolled */
16229 && w->cursor.y < this_scroll_margin
16230 && CHARPOS (pos) > BEGV
16231 && IT_CHARPOS (it) < ZV)
16232 /* rms: considering make_cursor_line_fully_visible_p here
16233 seems to give wrong results. We don't want to recenter
16234 when the last line is partly visible, we want to allow
16235 that case to be handled in the usual way. */
16236 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16238 w->cursor.vpos = -1;
16239 clear_glyph_matrix (w->desired_matrix);
16240 return -1;
16244 /* If bottom moved off end of frame, change mode line percentage. */
16245 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16246 w->update_mode_line = 1;
16248 /* Set window_end_pos to the offset of the last character displayed
16249 on the window from the end of current_buffer. Set
16250 window_end_vpos to its row number. */
16251 if (last_text_row)
16253 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16254 adjust_window_ends (w, last_text_row, 0);
16255 eassert
16256 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16257 w->window_end_vpos)));
16259 else
16261 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16262 w->window_end_pos = Z - ZV;
16263 w->window_end_vpos = 0;
16266 /* But that is not valid info until redisplay finishes. */
16267 w->window_end_valid = 0;
16268 return 1;
16273 /************************************************************************
16274 Window redisplay reusing current matrix when buffer has not changed
16275 ************************************************************************/
16277 /* Try redisplay of window W showing an unchanged buffer with a
16278 different window start than the last time it was displayed by
16279 reusing its current matrix. Value is non-zero if successful.
16280 W->start is the new window start. */
16282 static int
16283 try_window_reusing_current_matrix (struct window *w)
16285 struct frame *f = XFRAME (w->frame);
16286 struct glyph_row *bottom_row;
16287 struct it it;
16288 struct run run;
16289 struct text_pos start, new_start;
16290 int nrows_scrolled, i;
16291 struct glyph_row *last_text_row;
16292 struct glyph_row *last_reused_text_row;
16293 struct glyph_row *start_row;
16294 int start_vpos, min_y, max_y;
16296 #ifdef GLYPH_DEBUG
16297 if (inhibit_try_window_reusing)
16298 return 0;
16299 #endif
16301 if (/* This function doesn't handle terminal frames. */
16302 !FRAME_WINDOW_P (f)
16303 /* Don't try to reuse the display if windows have been split
16304 or such. */
16305 || windows_or_buffers_changed
16306 || f->cursor_type_changed)
16307 return 0;
16309 /* Can't do this if showing trailing whitespace. */
16310 if (!NILP (Vshow_trailing_whitespace))
16311 return 0;
16313 /* If top-line visibility has changed, give up. */
16314 if (WINDOW_WANTS_HEADER_LINE_P (w)
16315 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16316 return 0;
16318 /* Give up if old or new display is scrolled vertically. We could
16319 make this function handle this, but right now it doesn't. */
16320 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16321 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16322 return 0;
16324 /* The variable new_start now holds the new window start. The old
16325 start `start' can be determined from the current matrix. */
16326 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16327 start = start_row->minpos;
16328 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16330 /* Clear the desired matrix for the display below. */
16331 clear_glyph_matrix (w->desired_matrix);
16333 if (CHARPOS (new_start) <= CHARPOS (start))
16335 /* Don't use this method if the display starts with an ellipsis
16336 displayed for invisible text. It's not easy to handle that case
16337 below, and it's certainly not worth the effort since this is
16338 not a frequent case. */
16339 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16340 return 0;
16342 IF_DEBUG (debug_method_add (w, "twu1"));
16344 /* Display up to a row that can be reused. The variable
16345 last_text_row is set to the last row displayed that displays
16346 text. Note that it.vpos == 0 if or if not there is a
16347 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16348 start_display (&it, w, new_start);
16349 w->cursor.vpos = -1;
16350 last_text_row = last_reused_text_row = NULL;
16352 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16354 /* If we have reached into the characters in the START row,
16355 that means the line boundaries have changed. So we
16356 can't start copying with the row START. Maybe it will
16357 work to start copying with the following row. */
16358 while (IT_CHARPOS (it) > CHARPOS (start))
16360 /* Advance to the next row as the "start". */
16361 start_row++;
16362 start = start_row->minpos;
16363 /* If there are no more rows to try, or just one, give up. */
16364 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16365 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16366 || CHARPOS (start) == ZV)
16368 clear_glyph_matrix (w->desired_matrix);
16369 return 0;
16372 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16374 /* If we have reached alignment, we can copy the rest of the
16375 rows. */
16376 if (IT_CHARPOS (it) == CHARPOS (start)
16377 /* Don't accept "alignment" inside a display vector,
16378 since start_row could have started in the middle of
16379 that same display vector (thus their character
16380 positions match), and we have no way of telling if
16381 that is the case. */
16382 && it.current.dpvec_index < 0)
16383 break;
16385 if (display_line (&it))
16386 last_text_row = it.glyph_row - 1;
16390 /* A value of current_y < last_visible_y means that we stopped
16391 at the previous window start, which in turn means that we
16392 have at least one reusable row. */
16393 if (it.current_y < it.last_visible_y)
16395 struct glyph_row *row;
16397 /* IT.vpos always starts from 0; it counts text lines. */
16398 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16400 /* Find PT if not already found in the lines displayed. */
16401 if (w->cursor.vpos < 0)
16403 int dy = it.current_y - start_row->y;
16405 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16406 row = row_containing_pos (w, PT, row, NULL, dy);
16407 if (row)
16408 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16409 dy, nrows_scrolled);
16410 else
16412 clear_glyph_matrix (w->desired_matrix);
16413 return 0;
16417 /* Scroll the display. Do it before the current matrix is
16418 changed. The problem here is that update has not yet
16419 run, i.e. part of the current matrix is not up to date.
16420 scroll_run_hook will clear the cursor, and use the
16421 current matrix to get the height of the row the cursor is
16422 in. */
16423 run.current_y = start_row->y;
16424 run.desired_y = it.current_y;
16425 run.height = it.last_visible_y - it.current_y;
16427 if (run.height > 0 && run.current_y != run.desired_y)
16429 update_begin (f);
16430 FRAME_RIF (f)->update_window_begin_hook (w);
16431 FRAME_RIF (f)->clear_window_mouse_face (w);
16432 FRAME_RIF (f)->scroll_run_hook (w, &run);
16433 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16434 update_end (f);
16437 /* Shift current matrix down by nrows_scrolled lines. */
16438 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16439 rotate_matrix (w->current_matrix,
16440 start_vpos,
16441 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16442 nrows_scrolled);
16444 /* Disable lines that must be updated. */
16445 for (i = 0; i < nrows_scrolled; ++i)
16446 (start_row + i)->enabled_p = 0;
16448 /* Re-compute Y positions. */
16449 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16450 max_y = it.last_visible_y;
16451 for (row = start_row + nrows_scrolled;
16452 row < bottom_row;
16453 ++row)
16455 row->y = it.current_y;
16456 row->visible_height = row->height;
16458 if (row->y < min_y)
16459 row->visible_height -= min_y - row->y;
16460 if (row->y + row->height > max_y)
16461 row->visible_height -= row->y + row->height - max_y;
16462 if (row->fringe_bitmap_periodic_p)
16463 row->redraw_fringe_bitmaps_p = 1;
16465 it.current_y += row->height;
16467 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16468 last_reused_text_row = row;
16469 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16470 break;
16473 /* Disable lines in the current matrix which are now
16474 below the window. */
16475 for (++row; row < bottom_row; ++row)
16476 row->enabled_p = row->mode_line_p = 0;
16479 /* Update window_end_pos etc.; last_reused_text_row is the last
16480 reused row from the current matrix containing text, if any.
16481 The value of last_text_row is the last displayed line
16482 containing text. */
16483 if (last_reused_text_row)
16484 adjust_window_ends (w, last_reused_text_row, 1);
16485 else if (last_text_row)
16486 adjust_window_ends (w, last_text_row, 0);
16487 else
16489 /* This window must be completely empty. */
16490 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16491 w->window_end_pos = Z - ZV;
16492 w->window_end_vpos = 0;
16494 w->window_end_valid = 0;
16496 /* Update hint: don't try scrolling again in update_window. */
16497 w->desired_matrix->no_scrolling_p = 1;
16499 #ifdef GLYPH_DEBUG
16500 debug_method_add (w, "try_window_reusing_current_matrix 1");
16501 #endif
16502 return 1;
16504 else if (CHARPOS (new_start) > CHARPOS (start))
16506 struct glyph_row *pt_row, *row;
16507 struct glyph_row *first_reusable_row;
16508 struct glyph_row *first_row_to_display;
16509 int dy;
16510 int yb = window_text_bottom_y (w);
16512 /* Find the row starting at new_start, if there is one. Don't
16513 reuse a partially visible line at the end. */
16514 first_reusable_row = start_row;
16515 while (first_reusable_row->enabled_p
16516 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16517 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16518 < CHARPOS (new_start)))
16519 ++first_reusable_row;
16521 /* Give up if there is no row to reuse. */
16522 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16523 || !first_reusable_row->enabled_p
16524 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16525 != CHARPOS (new_start)))
16526 return 0;
16528 /* We can reuse fully visible rows beginning with
16529 first_reusable_row to the end of the window. Set
16530 first_row_to_display to the first row that cannot be reused.
16531 Set pt_row to the row containing point, if there is any. */
16532 pt_row = NULL;
16533 for (first_row_to_display = first_reusable_row;
16534 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16535 ++first_row_to_display)
16537 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16538 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16539 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16540 && first_row_to_display->ends_at_zv_p
16541 && pt_row == NULL)))
16542 pt_row = first_row_to_display;
16545 /* Start displaying at the start of first_row_to_display. */
16546 eassert (first_row_to_display->y < yb);
16547 init_to_row_start (&it, w, first_row_to_display);
16549 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16550 - start_vpos);
16551 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16552 - nrows_scrolled);
16553 it.current_y = (first_row_to_display->y - first_reusable_row->y
16554 + WINDOW_HEADER_LINE_HEIGHT (w));
16556 /* Display lines beginning with first_row_to_display in the
16557 desired matrix. Set last_text_row to the last row displayed
16558 that displays text. */
16559 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16560 if (pt_row == NULL)
16561 w->cursor.vpos = -1;
16562 last_text_row = NULL;
16563 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16564 if (display_line (&it))
16565 last_text_row = it.glyph_row - 1;
16567 /* If point is in a reused row, adjust y and vpos of the cursor
16568 position. */
16569 if (pt_row)
16571 w->cursor.vpos -= nrows_scrolled;
16572 w->cursor.y -= first_reusable_row->y - start_row->y;
16575 /* Give up if point isn't in a row displayed or reused. (This
16576 also handles the case where w->cursor.vpos < nrows_scrolled
16577 after the calls to display_line, which can happen with scroll
16578 margins. See bug#1295.) */
16579 if (w->cursor.vpos < 0)
16581 clear_glyph_matrix (w->desired_matrix);
16582 return 0;
16585 /* Scroll the display. */
16586 run.current_y = first_reusable_row->y;
16587 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16588 run.height = it.last_visible_y - run.current_y;
16589 dy = run.current_y - run.desired_y;
16591 if (run.height)
16593 update_begin (f);
16594 FRAME_RIF (f)->update_window_begin_hook (w);
16595 FRAME_RIF (f)->clear_window_mouse_face (w);
16596 FRAME_RIF (f)->scroll_run_hook (w, &run);
16597 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16598 update_end (f);
16601 /* Adjust Y positions of reused rows. */
16602 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16604 max_y = it.last_visible_y;
16605 for (row = first_reusable_row; row < first_row_to_display; ++row)
16607 row->y -= dy;
16608 row->visible_height = row->height;
16609 if (row->y < min_y)
16610 row->visible_height -= min_y - row->y;
16611 if (row->y + row->height > max_y)
16612 row->visible_height -= row->y + row->height - max_y;
16613 if (row->fringe_bitmap_periodic_p)
16614 row->redraw_fringe_bitmaps_p = 1;
16617 /* Scroll the current matrix. */
16618 eassert (nrows_scrolled > 0);
16619 rotate_matrix (w->current_matrix,
16620 start_vpos,
16621 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16622 -nrows_scrolled);
16624 /* Disable rows not reused. */
16625 for (row -= nrows_scrolled; row < bottom_row; ++row)
16626 row->enabled_p = 0;
16628 /* Point may have moved to a different line, so we cannot assume that
16629 the previous cursor position is valid; locate the correct row. */
16630 if (pt_row)
16632 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16633 row < bottom_row
16634 && PT >= MATRIX_ROW_END_CHARPOS (row)
16635 && !row->ends_at_zv_p;
16636 row++)
16638 w->cursor.vpos++;
16639 w->cursor.y = row->y;
16641 if (row < bottom_row)
16643 /* Can't simply scan the row for point with
16644 bidi-reordered glyph rows. Let set_cursor_from_row
16645 figure out where to put the cursor, and if it fails,
16646 give up. */
16647 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16649 if (!set_cursor_from_row (w, row, w->current_matrix,
16650 0, 0, 0, 0))
16652 clear_glyph_matrix (w->desired_matrix);
16653 return 0;
16656 else
16658 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16659 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16661 for (; glyph < end
16662 && (!BUFFERP (glyph->object)
16663 || glyph->charpos < PT);
16664 glyph++)
16666 w->cursor.hpos++;
16667 w->cursor.x += glyph->pixel_width;
16673 /* Adjust window end. A null value of last_text_row means that
16674 the window end is in reused rows which in turn means that
16675 only its vpos can have changed. */
16676 if (last_text_row)
16677 adjust_window_ends (w, last_text_row, 0);
16678 else
16679 w->window_end_vpos -= nrows_scrolled;
16681 w->window_end_valid = 0;
16682 w->desired_matrix->no_scrolling_p = 1;
16684 #ifdef GLYPH_DEBUG
16685 debug_method_add (w, "try_window_reusing_current_matrix 2");
16686 #endif
16687 return 1;
16690 return 0;
16695 /************************************************************************
16696 Window redisplay reusing current matrix when buffer has changed
16697 ************************************************************************/
16699 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16700 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16701 ptrdiff_t *, ptrdiff_t *);
16702 static struct glyph_row *
16703 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16704 struct glyph_row *);
16707 /* Return the last row in MATRIX displaying text. If row START is
16708 non-null, start searching with that row. IT gives the dimensions
16709 of the display. Value is null if matrix is empty; otherwise it is
16710 a pointer to the row found. */
16712 static struct glyph_row *
16713 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16714 struct glyph_row *start)
16716 struct glyph_row *row, *row_found;
16718 /* Set row_found to the last row in IT->w's current matrix
16719 displaying text. The loop looks funny but think of partially
16720 visible lines. */
16721 row_found = NULL;
16722 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16723 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16725 eassert (row->enabled_p);
16726 row_found = row;
16727 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16728 break;
16729 ++row;
16732 return row_found;
16736 /* Return the last row in the current matrix of W that is not affected
16737 by changes at the start of current_buffer that occurred since W's
16738 current matrix was built. Value is null if no such row exists.
16740 BEG_UNCHANGED us the number of characters unchanged at the start of
16741 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16742 first changed character in current_buffer. Characters at positions <
16743 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16744 when the current matrix was built. */
16746 static struct glyph_row *
16747 find_last_unchanged_at_beg_row (struct window *w)
16749 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16750 struct glyph_row *row;
16751 struct glyph_row *row_found = NULL;
16752 int yb = window_text_bottom_y (w);
16754 /* Find the last row displaying unchanged text. */
16755 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16756 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16757 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16758 ++row)
16760 if (/* If row ends before first_changed_pos, it is unchanged,
16761 except in some case. */
16762 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16763 /* When row ends in ZV and we write at ZV it is not
16764 unchanged. */
16765 && !row->ends_at_zv_p
16766 /* When first_changed_pos is the end of a continued line,
16767 row is not unchanged because it may be no longer
16768 continued. */
16769 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16770 && (row->continued_p
16771 || row->exact_window_width_line_p))
16772 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16773 needs to be recomputed, so don't consider this row as
16774 unchanged. This happens when the last line was
16775 bidi-reordered and was killed immediately before this
16776 redisplay cycle. In that case, ROW->end stores the
16777 buffer position of the first visual-order character of
16778 the killed text, which is now beyond ZV. */
16779 && CHARPOS (row->end.pos) <= ZV)
16780 row_found = row;
16782 /* Stop if last visible row. */
16783 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16784 break;
16787 return row_found;
16791 /* Find the first glyph row in the current matrix of W that is not
16792 affected by changes at the end of current_buffer since the
16793 time W's current matrix was built.
16795 Return in *DELTA the number of chars by which buffer positions in
16796 unchanged text at the end of current_buffer must be adjusted.
16798 Return in *DELTA_BYTES the corresponding number of bytes.
16800 Value is null if no such row exists, i.e. all rows are affected by
16801 changes. */
16803 static struct glyph_row *
16804 find_first_unchanged_at_end_row (struct window *w,
16805 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16807 struct glyph_row *row;
16808 struct glyph_row *row_found = NULL;
16810 *delta = *delta_bytes = 0;
16812 /* Display must not have been paused, otherwise the current matrix
16813 is not up to date. */
16814 eassert (w->window_end_valid);
16816 /* A value of window_end_pos >= END_UNCHANGED means that the window
16817 end is in the range of changed text. If so, there is no
16818 unchanged row at the end of W's current matrix. */
16819 if (w->window_end_pos >= END_UNCHANGED)
16820 return NULL;
16822 /* Set row to the last row in W's current matrix displaying text. */
16823 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16825 /* If matrix is entirely empty, no unchanged row exists. */
16826 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16828 /* The value of row is the last glyph row in the matrix having a
16829 meaningful buffer position in it. The end position of row
16830 corresponds to window_end_pos. This allows us to translate
16831 buffer positions in the current matrix to current buffer
16832 positions for characters not in changed text. */
16833 ptrdiff_t Z_old =
16834 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16835 ptrdiff_t Z_BYTE_old =
16836 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16837 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16838 struct glyph_row *first_text_row
16839 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16841 *delta = Z - Z_old;
16842 *delta_bytes = Z_BYTE - Z_BYTE_old;
16844 /* Set last_unchanged_pos to the buffer position of the last
16845 character in the buffer that has not been changed. Z is the
16846 index + 1 of the last character in current_buffer, i.e. by
16847 subtracting END_UNCHANGED we get the index of the last
16848 unchanged character, and we have to add BEG to get its buffer
16849 position. */
16850 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16851 last_unchanged_pos_old = last_unchanged_pos - *delta;
16853 /* Search backward from ROW for a row displaying a line that
16854 starts at a minimum position >= last_unchanged_pos_old. */
16855 for (; row > first_text_row; --row)
16857 /* This used to abort, but it can happen.
16858 It is ok to just stop the search instead here. KFS. */
16859 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16860 break;
16862 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16863 row_found = row;
16867 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16869 return row_found;
16873 /* Make sure that glyph rows in the current matrix of window W
16874 reference the same glyph memory as corresponding rows in the
16875 frame's frame matrix. This function is called after scrolling W's
16876 current matrix on a terminal frame in try_window_id and
16877 try_window_reusing_current_matrix. */
16879 static void
16880 sync_frame_with_window_matrix_rows (struct window *w)
16882 struct frame *f = XFRAME (w->frame);
16883 struct glyph_row *window_row, *window_row_end, *frame_row;
16885 /* Preconditions: W must be a leaf window and full-width. Its frame
16886 must have a frame matrix. */
16887 eassert (BUFFERP (w->contents));
16888 eassert (WINDOW_FULL_WIDTH_P (w));
16889 eassert (!FRAME_WINDOW_P (f));
16891 /* If W is a full-width window, glyph pointers in W's current matrix
16892 have, by definition, to be the same as glyph pointers in the
16893 corresponding frame matrix. Note that frame matrices have no
16894 marginal areas (see build_frame_matrix). */
16895 window_row = w->current_matrix->rows;
16896 window_row_end = window_row + w->current_matrix->nrows;
16897 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16898 while (window_row < window_row_end)
16900 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16901 struct glyph *end = window_row->glyphs[LAST_AREA];
16903 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16904 frame_row->glyphs[TEXT_AREA] = start;
16905 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16906 frame_row->glyphs[LAST_AREA] = end;
16908 /* Disable frame rows whose corresponding window rows have
16909 been disabled in try_window_id. */
16910 if (!window_row->enabled_p)
16911 frame_row->enabled_p = 0;
16913 ++window_row, ++frame_row;
16918 /* Find the glyph row in window W containing CHARPOS. Consider all
16919 rows between START and END (not inclusive). END null means search
16920 all rows to the end of the display area of W. Value is the row
16921 containing CHARPOS or null. */
16923 struct glyph_row *
16924 row_containing_pos (struct window *w, ptrdiff_t charpos,
16925 struct glyph_row *start, struct glyph_row *end, int dy)
16927 struct glyph_row *row = start;
16928 struct glyph_row *best_row = NULL;
16929 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16930 int last_y;
16932 /* If we happen to start on a header-line, skip that. */
16933 if (row->mode_line_p)
16934 ++row;
16936 if ((end && row >= end) || !row->enabled_p)
16937 return NULL;
16939 last_y = window_text_bottom_y (w) - dy;
16941 while (1)
16943 /* Give up if we have gone too far. */
16944 if (end && row >= end)
16945 return NULL;
16946 /* This formerly returned if they were equal.
16947 I think that both quantities are of a "last plus one" type;
16948 if so, when they are equal, the row is within the screen. -- rms. */
16949 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16950 return NULL;
16952 /* If it is in this row, return this row. */
16953 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16954 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16955 /* The end position of a row equals the start
16956 position of the next row. If CHARPOS is there, we
16957 would rather consider it displayed in the next
16958 line, except when this line ends in ZV. */
16959 && !row_for_charpos_p (row, charpos)))
16960 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16962 struct glyph *g;
16964 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16965 || (!best_row && !row->continued_p))
16966 return row;
16967 /* In bidi-reordered rows, there could be several rows whose
16968 edges surround CHARPOS, all of these rows belonging to
16969 the same continued line. We need to find the row which
16970 fits CHARPOS the best. */
16971 for (g = row->glyphs[TEXT_AREA];
16972 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16973 g++)
16975 if (!STRINGP (g->object))
16977 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16979 mindif = eabs (g->charpos - charpos);
16980 best_row = row;
16981 /* Exact match always wins. */
16982 if (mindif == 0)
16983 return best_row;
16988 else if (best_row && !row->continued_p)
16989 return best_row;
16990 ++row;
16995 /* Try to redisplay window W by reusing its existing display. W's
16996 current matrix must be up to date when this function is called,
16997 i.e. window_end_valid must be nonzero.
16999 Value is
17001 1 if display has been updated
17002 0 if otherwise unsuccessful
17003 -1 if redisplay with same window start is known not to succeed
17005 The following steps are performed:
17007 1. Find the last row in the current matrix of W that is not
17008 affected by changes at the start of current_buffer. If no such row
17009 is found, give up.
17011 2. Find the first row in W's current matrix that is not affected by
17012 changes at the end of current_buffer. Maybe there is no such row.
17014 3. Display lines beginning with the row + 1 found in step 1 to the
17015 row found in step 2 or, if step 2 didn't find a row, to the end of
17016 the window.
17018 4. If cursor is not known to appear on the window, give up.
17020 5. If display stopped at the row found in step 2, scroll the
17021 display and current matrix as needed.
17023 6. Maybe display some lines at the end of W, if we must. This can
17024 happen under various circumstances, like a partially visible line
17025 becoming fully visible, or because newly displayed lines are displayed
17026 in smaller font sizes.
17028 7. Update W's window end information. */
17030 static int
17031 try_window_id (struct window *w)
17033 struct frame *f = XFRAME (w->frame);
17034 struct glyph_matrix *current_matrix = w->current_matrix;
17035 struct glyph_matrix *desired_matrix = w->desired_matrix;
17036 struct glyph_row *last_unchanged_at_beg_row;
17037 struct glyph_row *first_unchanged_at_end_row;
17038 struct glyph_row *row;
17039 struct glyph_row *bottom_row;
17040 int bottom_vpos;
17041 struct it it;
17042 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17043 int dvpos, dy;
17044 struct text_pos start_pos;
17045 struct run run;
17046 int first_unchanged_at_end_vpos = 0;
17047 struct glyph_row *last_text_row, *last_text_row_at_end;
17048 struct text_pos start;
17049 ptrdiff_t first_changed_charpos, last_changed_charpos;
17051 #ifdef GLYPH_DEBUG
17052 if (inhibit_try_window_id)
17053 return 0;
17054 #endif
17056 /* This is handy for debugging. */
17057 #if 0
17058 #define GIVE_UP(X) \
17059 do { \
17060 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17061 return 0; \
17062 } while (0)
17063 #else
17064 #define GIVE_UP(X) return 0
17065 #endif
17067 SET_TEXT_POS_FROM_MARKER (start, w->start);
17069 /* Don't use this for mini-windows because these can show
17070 messages and mini-buffers, and we don't handle that here. */
17071 if (MINI_WINDOW_P (w))
17072 GIVE_UP (1);
17074 /* This flag is used to prevent redisplay optimizations. */
17075 if (windows_or_buffers_changed || f->cursor_type_changed)
17076 GIVE_UP (2);
17078 /* Verify that narrowing has not changed.
17079 Also verify that we were not told to prevent redisplay optimizations.
17080 It would be nice to further
17081 reduce the number of cases where this prevents try_window_id. */
17082 if (current_buffer->clip_changed
17083 || current_buffer->prevent_redisplay_optimizations_p)
17084 GIVE_UP (3);
17086 /* Window must either use window-based redisplay or be full width. */
17087 if (!FRAME_WINDOW_P (f)
17088 && (!FRAME_LINE_INS_DEL_OK (f)
17089 || !WINDOW_FULL_WIDTH_P (w)))
17090 GIVE_UP (4);
17092 /* Give up if point is known NOT to appear in W. */
17093 if (PT < CHARPOS (start))
17094 GIVE_UP (5);
17096 /* Another way to prevent redisplay optimizations. */
17097 if (w->last_modified == 0)
17098 GIVE_UP (6);
17100 /* Verify that window is not hscrolled. */
17101 if (w->hscroll != 0)
17102 GIVE_UP (7);
17104 /* Verify that display wasn't paused. */
17105 if (!w->window_end_valid)
17106 GIVE_UP (8);
17108 /* Likewise if highlighting trailing whitespace. */
17109 if (!NILP (Vshow_trailing_whitespace))
17110 GIVE_UP (11);
17112 /* Can't use this if overlay arrow position and/or string have
17113 changed. */
17114 if (overlay_arrows_changed_p ())
17115 GIVE_UP (12);
17117 /* When word-wrap is on, adding a space to the first word of a
17118 wrapped line can change the wrap position, altering the line
17119 above it. It might be worthwhile to handle this more
17120 intelligently, but for now just redisplay from scratch. */
17121 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17122 GIVE_UP (21);
17124 /* Under bidi reordering, adding or deleting a character in the
17125 beginning of a paragraph, before the first strong directional
17126 character, can change the base direction of the paragraph (unless
17127 the buffer specifies a fixed paragraph direction), which will
17128 require to redisplay the whole paragraph. It might be worthwhile
17129 to find the paragraph limits and widen the range of redisplayed
17130 lines to that, but for now just give up this optimization and
17131 redisplay from scratch. */
17132 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17133 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17134 GIVE_UP (22);
17136 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17137 only if buffer has really changed. The reason is that the gap is
17138 initially at Z for freshly visited files. The code below would
17139 set end_unchanged to 0 in that case. */
17140 if (MODIFF > SAVE_MODIFF
17141 /* This seems to happen sometimes after saving a buffer. */
17142 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17144 if (GPT - BEG < BEG_UNCHANGED)
17145 BEG_UNCHANGED = GPT - BEG;
17146 if (Z - GPT < END_UNCHANGED)
17147 END_UNCHANGED = Z - GPT;
17150 /* The position of the first and last character that has been changed. */
17151 first_changed_charpos = BEG + BEG_UNCHANGED;
17152 last_changed_charpos = Z - END_UNCHANGED;
17154 /* If window starts after a line end, and the last change is in
17155 front of that newline, then changes don't affect the display.
17156 This case happens with stealth-fontification. Note that although
17157 the display is unchanged, glyph positions in the matrix have to
17158 be adjusted, of course. */
17159 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17160 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17161 && ((last_changed_charpos < CHARPOS (start)
17162 && CHARPOS (start) == BEGV)
17163 || (last_changed_charpos < CHARPOS (start) - 1
17164 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17166 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17167 struct glyph_row *r0;
17169 /* Compute how many chars/bytes have been added to or removed
17170 from the buffer. */
17171 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17172 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17173 Z_delta = Z - Z_old;
17174 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17176 /* Give up if PT is not in the window. Note that it already has
17177 been checked at the start of try_window_id that PT is not in
17178 front of the window start. */
17179 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17180 GIVE_UP (13);
17182 /* If window start is unchanged, we can reuse the whole matrix
17183 as is, after adjusting glyph positions. No need to compute
17184 the window end again, since its offset from Z hasn't changed. */
17185 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17186 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17187 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17188 /* PT must not be in a partially visible line. */
17189 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17190 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17192 /* Adjust positions in the glyph matrix. */
17193 if (Z_delta || Z_delta_bytes)
17195 struct glyph_row *r1
17196 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17197 increment_matrix_positions (w->current_matrix,
17198 MATRIX_ROW_VPOS (r0, current_matrix),
17199 MATRIX_ROW_VPOS (r1, current_matrix),
17200 Z_delta, Z_delta_bytes);
17203 /* Set the cursor. */
17204 row = row_containing_pos (w, PT, r0, NULL, 0);
17205 if (row)
17206 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17207 return 1;
17211 /* Handle the case that changes are all below what is displayed in
17212 the window, and that PT is in the window. This shortcut cannot
17213 be taken if ZV is visible in the window, and text has been added
17214 there that is visible in the window. */
17215 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17216 /* ZV is not visible in the window, or there are no
17217 changes at ZV, actually. */
17218 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17219 || first_changed_charpos == last_changed_charpos))
17221 struct glyph_row *r0;
17223 /* Give up if PT is not in the window. Note that it already has
17224 been checked at the start of try_window_id that PT is not in
17225 front of the window start. */
17226 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17227 GIVE_UP (14);
17229 /* If window start is unchanged, we can reuse the whole matrix
17230 as is, without changing glyph positions since no text has
17231 been added/removed in front of the window end. */
17232 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17233 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17234 /* PT must not be in a partially visible line. */
17235 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17236 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17238 /* We have to compute the window end anew since text
17239 could have been added/removed after it. */
17240 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17241 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17243 /* Set the cursor. */
17244 row = row_containing_pos (w, PT, r0, NULL, 0);
17245 if (row)
17246 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17247 return 2;
17251 /* Give up if window start is in the changed area.
17253 The condition used to read
17255 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17257 but why that was tested escapes me at the moment. */
17258 if (CHARPOS (start) >= first_changed_charpos
17259 && CHARPOS (start) <= last_changed_charpos)
17260 GIVE_UP (15);
17262 /* Check that window start agrees with the start of the first glyph
17263 row in its current matrix. Check this after we know the window
17264 start is not in changed text, otherwise positions would not be
17265 comparable. */
17266 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17267 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17268 GIVE_UP (16);
17270 /* Give up if the window ends in strings. Overlay strings
17271 at the end are difficult to handle, so don't try. */
17272 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17273 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17274 GIVE_UP (20);
17276 /* Compute the position at which we have to start displaying new
17277 lines. Some of the lines at the top of the window might be
17278 reusable because they are not displaying changed text. Find the
17279 last row in W's current matrix not affected by changes at the
17280 start of current_buffer. Value is null if changes start in the
17281 first line of window. */
17282 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17283 if (last_unchanged_at_beg_row)
17285 /* Avoid starting to display in the middle of a character, a TAB
17286 for instance. This is easier than to set up the iterator
17287 exactly, and it's not a frequent case, so the additional
17288 effort wouldn't really pay off. */
17289 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17290 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17291 && last_unchanged_at_beg_row > w->current_matrix->rows)
17292 --last_unchanged_at_beg_row;
17294 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17295 GIVE_UP (17);
17297 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17298 GIVE_UP (18);
17299 start_pos = it.current.pos;
17301 /* Start displaying new lines in the desired matrix at the same
17302 vpos we would use in the current matrix, i.e. below
17303 last_unchanged_at_beg_row. */
17304 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17305 current_matrix);
17306 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17307 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17309 eassert (it.hpos == 0 && it.current_x == 0);
17311 else
17313 /* There are no reusable lines at the start of the window.
17314 Start displaying in the first text line. */
17315 start_display (&it, w, start);
17316 it.vpos = it.first_vpos;
17317 start_pos = it.current.pos;
17320 /* Find the first row that is not affected by changes at the end of
17321 the buffer. Value will be null if there is no unchanged row, in
17322 which case we must redisplay to the end of the window. delta
17323 will be set to the value by which buffer positions beginning with
17324 first_unchanged_at_end_row have to be adjusted due to text
17325 changes. */
17326 first_unchanged_at_end_row
17327 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17328 IF_DEBUG (debug_delta = delta);
17329 IF_DEBUG (debug_delta_bytes = delta_bytes);
17331 /* Set stop_pos to the buffer position up to which we will have to
17332 display new lines. If first_unchanged_at_end_row != NULL, this
17333 is the buffer position of the start of the line displayed in that
17334 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17335 that we don't stop at a buffer position. */
17336 stop_pos = 0;
17337 if (first_unchanged_at_end_row)
17339 eassert (last_unchanged_at_beg_row == NULL
17340 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17342 /* If this is a continuation line, move forward to the next one
17343 that isn't. Changes in lines above affect this line.
17344 Caution: this may move first_unchanged_at_end_row to a row
17345 not displaying text. */
17346 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17347 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17348 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17349 < it.last_visible_y))
17350 ++first_unchanged_at_end_row;
17352 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17353 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17354 >= it.last_visible_y))
17355 first_unchanged_at_end_row = NULL;
17356 else
17358 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17359 + delta);
17360 first_unchanged_at_end_vpos
17361 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17362 eassert (stop_pos >= Z - END_UNCHANGED);
17365 else if (last_unchanged_at_beg_row == NULL)
17366 GIVE_UP (19);
17369 #ifdef GLYPH_DEBUG
17371 /* Either there is no unchanged row at the end, or the one we have
17372 now displays text. This is a necessary condition for the window
17373 end pos calculation at the end of this function. */
17374 eassert (first_unchanged_at_end_row == NULL
17375 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17377 debug_last_unchanged_at_beg_vpos
17378 = (last_unchanged_at_beg_row
17379 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17380 : -1);
17381 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17383 #endif /* GLYPH_DEBUG */
17386 /* Display new lines. Set last_text_row to the last new line
17387 displayed which has text on it, i.e. might end up as being the
17388 line where the window_end_vpos is. */
17389 w->cursor.vpos = -1;
17390 last_text_row = NULL;
17391 overlay_arrow_seen = 0;
17392 while (it.current_y < it.last_visible_y
17393 && !f->fonts_changed
17394 && (first_unchanged_at_end_row == NULL
17395 || IT_CHARPOS (it) < stop_pos))
17397 if (display_line (&it))
17398 last_text_row = it.glyph_row - 1;
17401 if (f->fonts_changed)
17402 return -1;
17405 /* Compute differences in buffer positions, y-positions etc. for
17406 lines reused at the bottom of the window. Compute what we can
17407 scroll. */
17408 if (first_unchanged_at_end_row
17409 /* No lines reused because we displayed everything up to the
17410 bottom of the window. */
17411 && it.current_y < it.last_visible_y)
17413 dvpos = (it.vpos
17414 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17415 current_matrix));
17416 dy = it.current_y - first_unchanged_at_end_row->y;
17417 run.current_y = first_unchanged_at_end_row->y;
17418 run.desired_y = run.current_y + dy;
17419 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17421 else
17423 delta = delta_bytes = dvpos = dy
17424 = run.current_y = run.desired_y = run.height = 0;
17425 first_unchanged_at_end_row = NULL;
17427 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17430 /* Find the cursor if not already found. We have to decide whether
17431 PT will appear on this window (it sometimes doesn't, but this is
17432 not a very frequent case.) This decision has to be made before
17433 the current matrix is altered. A value of cursor.vpos < 0 means
17434 that PT is either in one of the lines beginning at
17435 first_unchanged_at_end_row or below the window. Don't care for
17436 lines that might be displayed later at the window end; as
17437 mentioned, this is not a frequent case. */
17438 if (w->cursor.vpos < 0)
17440 /* Cursor in unchanged rows at the top? */
17441 if (PT < CHARPOS (start_pos)
17442 && last_unchanged_at_beg_row)
17444 row = row_containing_pos (w, PT,
17445 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17446 last_unchanged_at_beg_row + 1, 0);
17447 if (row)
17448 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17451 /* Start from first_unchanged_at_end_row looking for PT. */
17452 else if (first_unchanged_at_end_row)
17454 row = row_containing_pos (w, PT - delta,
17455 first_unchanged_at_end_row, NULL, 0);
17456 if (row)
17457 set_cursor_from_row (w, row, w->current_matrix, delta,
17458 delta_bytes, dy, dvpos);
17461 /* Give up if cursor was not found. */
17462 if (w->cursor.vpos < 0)
17464 clear_glyph_matrix (w->desired_matrix);
17465 return -1;
17469 /* Don't let the cursor end in the scroll margins. */
17471 int this_scroll_margin, cursor_height;
17472 int frame_line_height = default_line_pixel_height (w);
17473 int window_total_lines
17474 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17476 this_scroll_margin =
17477 max (0, min (scroll_margin, window_total_lines / 4));
17478 this_scroll_margin *= frame_line_height;
17479 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17481 if ((w->cursor.y < this_scroll_margin
17482 && CHARPOS (start) > BEGV)
17483 /* Old redisplay didn't take scroll margin into account at the bottom,
17484 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17485 || (w->cursor.y + (make_cursor_line_fully_visible_p
17486 ? cursor_height + this_scroll_margin
17487 : 1)) > it.last_visible_y)
17489 w->cursor.vpos = -1;
17490 clear_glyph_matrix (w->desired_matrix);
17491 return -1;
17495 /* Scroll the display. Do it before changing the current matrix so
17496 that xterm.c doesn't get confused about where the cursor glyph is
17497 found. */
17498 if (dy && run.height)
17500 update_begin (f);
17502 if (FRAME_WINDOW_P (f))
17504 FRAME_RIF (f)->update_window_begin_hook (w);
17505 FRAME_RIF (f)->clear_window_mouse_face (w);
17506 FRAME_RIF (f)->scroll_run_hook (w, &run);
17507 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17509 else
17511 /* Terminal frame. In this case, dvpos gives the number of
17512 lines to scroll by; dvpos < 0 means scroll up. */
17513 int from_vpos
17514 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17515 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17516 int end = (WINDOW_TOP_EDGE_LINE (w)
17517 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17518 + window_internal_height (w));
17520 #if defined (HAVE_GPM) || defined (MSDOS)
17521 x_clear_window_mouse_face (w);
17522 #endif
17523 /* Perform the operation on the screen. */
17524 if (dvpos > 0)
17526 /* Scroll last_unchanged_at_beg_row to the end of the
17527 window down dvpos lines. */
17528 set_terminal_window (f, end);
17530 /* On dumb terminals delete dvpos lines at the end
17531 before inserting dvpos empty lines. */
17532 if (!FRAME_SCROLL_REGION_OK (f))
17533 ins_del_lines (f, end - dvpos, -dvpos);
17535 /* Insert dvpos empty lines in front of
17536 last_unchanged_at_beg_row. */
17537 ins_del_lines (f, from, dvpos);
17539 else if (dvpos < 0)
17541 /* Scroll up last_unchanged_at_beg_vpos to the end of
17542 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17543 set_terminal_window (f, end);
17545 /* Delete dvpos lines in front of
17546 last_unchanged_at_beg_vpos. ins_del_lines will set
17547 the cursor to the given vpos and emit |dvpos| delete
17548 line sequences. */
17549 ins_del_lines (f, from + dvpos, dvpos);
17551 /* On a dumb terminal insert dvpos empty lines at the
17552 end. */
17553 if (!FRAME_SCROLL_REGION_OK (f))
17554 ins_del_lines (f, end + dvpos, -dvpos);
17557 set_terminal_window (f, 0);
17560 update_end (f);
17563 /* Shift reused rows of the current matrix to the right position.
17564 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17565 text. */
17566 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17567 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17568 if (dvpos < 0)
17570 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17571 bottom_vpos, dvpos);
17572 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17573 bottom_vpos);
17575 else if (dvpos > 0)
17577 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17578 bottom_vpos, dvpos);
17579 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17580 first_unchanged_at_end_vpos + dvpos);
17583 /* For frame-based redisplay, make sure that current frame and window
17584 matrix are in sync with respect to glyph memory. */
17585 if (!FRAME_WINDOW_P (f))
17586 sync_frame_with_window_matrix_rows (w);
17588 /* Adjust buffer positions in reused rows. */
17589 if (delta || delta_bytes)
17590 increment_matrix_positions (current_matrix,
17591 first_unchanged_at_end_vpos + dvpos,
17592 bottom_vpos, delta, delta_bytes);
17594 /* Adjust Y positions. */
17595 if (dy)
17596 shift_glyph_matrix (w, current_matrix,
17597 first_unchanged_at_end_vpos + dvpos,
17598 bottom_vpos, dy);
17600 if (first_unchanged_at_end_row)
17602 first_unchanged_at_end_row += dvpos;
17603 if (first_unchanged_at_end_row->y >= it.last_visible_y
17604 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17605 first_unchanged_at_end_row = NULL;
17608 /* If scrolling up, there may be some lines to display at the end of
17609 the window. */
17610 last_text_row_at_end = NULL;
17611 if (dy < 0)
17613 /* Scrolling up can leave for example a partially visible line
17614 at the end of the window to be redisplayed. */
17615 /* Set last_row to the glyph row in the current matrix where the
17616 window end line is found. It has been moved up or down in
17617 the matrix by dvpos. */
17618 int last_vpos = w->window_end_vpos + dvpos;
17619 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17621 /* If last_row is the window end line, it should display text. */
17622 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17624 /* If window end line was partially visible before, begin
17625 displaying at that line. Otherwise begin displaying with the
17626 line following it. */
17627 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17629 init_to_row_start (&it, w, last_row);
17630 it.vpos = last_vpos;
17631 it.current_y = last_row->y;
17633 else
17635 init_to_row_end (&it, w, last_row);
17636 it.vpos = 1 + last_vpos;
17637 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17638 ++last_row;
17641 /* We may start in a continuation line. If so, we have to
17642 get the right continuation_lines_width and current_x. */
17643 it.continuation_lines_width = last_row->continuation_lines_width;
17644 it.hpos = it.current_x = 0;
17646 /* Display the rest of the lines at the window end. */
17647 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17648 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17650 /* Is it always sure that the display agrees with lines in
17651 the current matrix? I don't think so, so we mark rows
17652 displayed invalid in the current matrix by setting their
17653 enabled_p flag to zero. */
17654 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17655 if (display_line (&it))
17656 last_text_row_at_end = it.glyph_row - 1;
17660 /* Update window_end_pos and window_end_vpos. */
17661 if (first_unchanged_at_end_row && !last_text_row_at_end)
17663 /* Window end line if one of the preserved rows from the current
17664 matrix. Set row to the last row displaying text in current
17665 matrix starting at first_unchanged_at_end_row, after
17666 scrolling. */
17667 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17668 row = find_last_row_displaying_text (w->current_matrix, &it,
17669 first_unchanged_at_end_row);
17670 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17671 adjust_window_ends (w, row, 1);
17672 eassert (w->window_end_bytepos >= 0);
17673 IF_DEBUG (debug_method_add (w, "A"));
17675 else if (last_text_row_at_end)
17677 adjust_window_ends (w, last_text_row_at_end, 0);
17678 eassert (w->window_end_bytepos >= 0);
17679 IF_DEBUG (debug_method_add (w, "B"));
17681 else if (last_text_row)
17683 /* We have displayed either to the end of the window or at the
17684 end of the window, i.e. the last row with text is to be found
17685 in the desired matrix. */
17686 adjust_window_ends (w, last_text_row, 0);
17687 eassert (w->window_end_bytepos >= 0);
17689 else if (first_unchanged_at_end_row == NULL
17690 && last_text_row == NULL
17691 && last_text_row_at_end == NULL)
17693 /* Displayed to end of window, but no line containing text was
17694 displayed. Lines were deleted at the end of the window. */
17695 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17696 int vpos = w->window_end_vpos;
17697 struct glyph_row *current_row = current_matrix->rows + vpos;
17698 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17700 for (row = NULL;
17701 row == NULL && vpos >= first_vpos;
17702 --vpos, --current_row, --desired_row)
17704 if (desired_row->enabled_p)
17706 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17707 row = desired_row;
17709 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17710 row = current_row;
17713 eassert (row != NULL);
17714 w->window_end_vpos = vpos + 1;
17715 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17716 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17717 eassert (w->window_end_bytepos >= 0);
17718 IF_DEBUG (debug_method_add (w, "C"));
17720 else
17721 emacs_abort ();
17723 IF_DEBUG (debug_end_pos = w->window_end_pos;
17724 debug_end_vpos = w->window_end_vpos);
17726 /* Record that display has not been completed. */
17727 w->window_end_valid = 0;
17728 w->desired_matrix->no_scrolling_p = 1;
17729 return 3;
17731 #undef GIVE_UP
17736 /***********************************************************************
17737 More debugging support
17738 ***********************************************************************/
17740 #ifdef GLYPH_DEBUG
17742 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17743 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17744 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17747 /* Dump the contents of glyph matrix MATRIX on stderr.
17749 GLYPHS 0 means don't show glyph contents.
17750 GLYPHS 1 means show glyphs in short form
17751 GLYPHS > 1 means show glyphs in long form. */
17753 void
17754 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17756 int i;
17757 for (i = 0; i < matrix->nrows; ++i)
17758 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17762 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17763 the glyph row and area where the glyph comes from. */
17765 void
17766 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17768 if (glyph->type == CHAR_GLYPH
17769 || glyph->type == GLYPHLESS_GLYPH)
17771 fprintf (stderr,
17772 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17773 glyph - row->glyphs[TEXT_AREA],
17774 (glyph->type == CHAR_GLYPH
17775 ? 'C'
17776 : 'G'),
17777 glyph->charpos,
17778 (BUFFERP (glyph->object)
17779 ? 'B'
17780 : (STRINGP (glyph->object)
17781 ? 'S'
17782 : (INTEGERP (glyph->object)
17783 ? '0'
17784 : '-'))),
17785 glyph->pixel_width,
17786 glyph->u.ch,
17787 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17788 ? glyph->u.ch
17789 : '.'),
17790 glyph->face_id,
17791 glyph->left_box_line_p,
17792 glyph->right_box_line_p);
17794 else if (glyph->type == STRETCH_GLYPH)
17796 fprintf (stderr,
17797 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17798 glyph - row->glyphs[TEXT_AREA],
17799 'S',
17800 glyph->charpos,
17801 (BUFFERP (glyph->object)
17802 ? 'B'
17803 : (STRINGP (glyph->object)
17804 ? 'S'
17805 : (INTEGERP (glyph->object)
17806 ? '0'
17807 : '-'))),
17808 glyph->pixel_width,
17810 ' ',
17811 glyph->face_id,
17812 glyph->left_box_line_p,
17813 glyph->right_box_line_p);
17815 else if (glyph->type == IMAGE_GLYPH)
17817 fprintf (stderr,
17818 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17819 glyph - row->glyphs[TEXT_AREA],
17820 'I',
17821 glyph->charpos,
17822 (BUFFERP (glyph->object)
17823 ? 'B'
17824 : (STRINGP (glyph->object)
17825 ? 'S'
17826 : (INTEGERP (glyph->object)
17827 ? '0'
17828 : '-'))),
17829 glyph->pixel_width,
17830 glyph->u.img_id,
17831 '.',
17832 glyph->face_id,
17833 glyph->left_box_line_p,
17834 glyph->right_box_line_p);
17836 else if (glyph->type == COMPOSITE_GLYPH)
17838 fprintf (stderr,
17839 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17840 glyph - row->glyphs[TEXT_AREA],
17841 '+',
17842 glyph->charpos,
17843 (BUFFERP (glyph->object)
17844 ? 'B'
17845 : (STRINGP (glyph->object)
17846 ? 'S'
17847 : (INTEGERP (glyph->object)
17848 ? '0'
17849 : '-'))),
17850 glyph->pixel_width,
17851 glyph->u.cmp.id);
17852 if (glyph->u.cmp.automatic)
17853 fprintf (stderr,
17854 "[%d-%d]",
17855 glyph->slice.cmp.from, glyph->slice.cmp.to);
17856 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17857 glyph->face_id,
17858 glyph->left_box_line_p,
17859 glyph->right_box_line_p);
17864 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17865 GLYPHS 0 means don't show glyph contents.
17866 GLYPHS 1 means show glyphs in short form
17867 GLYPHS > 1 means show glyphs in long form. */
17869 void
17870 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17872 if (glyphs != 1)
17874 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17875 fprintf (stderr, "==============================================================================\n");
17877 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17878 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17879 vpos,
17880 MATRIX_ROW_START_CHARPOS (row),
17881 MATRIX_ROW_END_CHARPOS (row),
17882 row->used[TEXT_AREA],
17883 row->contains_overlapping_glyphs_p,
17884 row->enabled_p,
17885 row->truncated_on_left_p,
17886 row->truncated_on_right_p,
17887 row->continued_p,
17888 MATRIX_ROW_CONTINUATION_LINE_P (row),
17889 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17890 row->ends_at_zv_p,
17891 row->fill_line_p,
17892 row->ends_in_middle_of_char_p,
17893 row->starts_in_middle_of_char_p,
17894 row->mouse_face_p,
17895 row->x,
17896 row->y,
17897 row->pixel_width,
17898 row->height,
17899 row->visible_height,
17900 row->ascent,
17901 row->phys_ascent);
17902 /* The next 3 lines should align to "Start" in the header. */
17903 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17904 row->end.overlay_string_index,
17905 row->continuation_lines_width);
17906 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17907 CHARPOS (row->start.string_pos),
17908 CHARPOS (row->end.string_pos));
17909 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17910 row->end.dpvec_index);
17913 if (glyphs > 1)
17915 int area;
17917 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17919 struct glyph *glyph = row->glyphs[area];
17920 struct glyph *glyph_end = glyph + row->used[area];
17922 /* Glyph for a line end in text. */
17923 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17924 ++glyph_end;
17926 if (glyph < glyph_end)
17927 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17929 for (; glyph < glyph_end; ++glyph)
17930 dump_glyph (row, glyph, area);
17933 else if (glyphs == 1)
17935 int area;
17937 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17939 char *s = alloca (row->used[area] + 4);
17940 int i;
17942 for (i = 0; i < row->used[area]; ++i)
17944 struct glyph *glyph = row->glyphs[area] + i;
17945 if (i == row->used[area] - 1
17946 && area == TEXT_AREA
17947 && INTEGERP (glyph->object)
17948 && glyph->type == CHAR_GLYPH
17949 && glyph->u.ch == ' ')
17951 strcpy (&s[i], "[\\n]");
17952 i += 4;
17954 else if (glyph->type == CHAR_GLYPH
17955 && glyph->u.ch < 0x80
17956 && glyph->u.ch >= ' ')
17957 s[i] = glyph->u.ch;
17958 else
17959 s[i] = '.';
17962 s[i] = '\0';
17963 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17969 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17970 Sdump_glyph_matrix, 0, 1, "p",
17971 doc: /* Dump the current matrix of the selected window to stderr.
17972 Shows contents of glyph row structures. With non-nil
17973 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17974 glyphs in short form, otherwise show glyphs in long form. */)
17975 (Lisp_Object glyphs)
17977 struct window *w = XWINDOW (selected_window);
17978 struct buffer *buffer = XBUFFER (w->contents);
17980 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17981 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17982 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17983 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17984 fprintf (stderr, "=============================================\n");
17985 dump_glyph_matrix (w->current_matrix,
17986 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17987 return Qnil;
17991 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17992 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17993 (void)
17995 struct frame *f = XFRAME (selected_frame);
17996 dump_glyph_matrix (f->current_matrix, 1);
17997 return Qnil;
18001 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18002 doc: /* Dump glyph row ROW to stderr.
18003 GLYPH 0 means don't dump glyphs.
18004 GLYPH 1 means dump glyphs in short form.
18005 GLYPH > 1 or omitted means dump glyphs in long form. */)
18006 (Lisp_Object row, Lisp_Object glyphs)
18008 struct glyph_matrix *matrix;
18009 EMACS_INT vpos;
18011 CHECK_NUMBER (row);
18012 matrix = XWINDOW (selected_window)->current_matrix;
18013 vpos = XINT (row);
18014 if (vpos >= 0 && vpos < matrix->nrows)
18015 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18016 vpos,
18017 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18018 return Qnil;
18022 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18023 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18024 GLYPH 0 means don't dump glyphs.
18025 GLYPH 1 means dump glyphs in short form.
18026 GLYPH > 1 or omitted means dump glyphs in long form.
18028 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18029 do nothing. */)
18030 (Lisp_Object row, Lisp_Object glyphs)
18032 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18033 struct frame *sf = SELECTED_FRAME ();
18034 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18035 EMACS_INT vpos;
18037 CHECK_NUMBER (row);
18038 vpos = XINT (row);
18039 if (vpos >= 0 && vpos < m->nrows)
18040 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18041 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18042 #endif
18043 return Qnil;
18047 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18048 doc: /* Toggle tracing of redisplay.
18049 With ARG, turn tracing on if and only if ARG is positive. */)
18050 (Lisp_Object arg)
18052 if (NILP (arg))
18053 trace_redisplay_p = !trace_redisplay_p;
18054 else
18056 arg = Fprefix_numeric_value (arg);
18057 trace_redisplay_p = XINT (arg) > 0;
18060 return Qnil;
18064 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18065 doc: /* Like `format', but print result to stderr.
18066 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18067 (ptrdiff_t nargs, Lisp_Object *args)
18069 Lisp_Object s = Fformat (nargs, args);
18070 fprintf (stderr, "%s", SDATA (s));
18071 return Qnil;
18074 #endif /* GLYPH_DEBUG */
18078 /***********************************************************************
18079 Building Desired Matrix Rows
18080 ***********************************************************************/
18082 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18083 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18085 static struct glyph_row *
18086 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18088 struct frame *f = XFRAME (WINDOW_FRAME (w));
18089 struct buffer *buffer = XBUFFER (w->contents);
18090 struct buffer *old = current_buffer;
18091 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18092 int arrow_len = SCHARS (overlay_arrow_string);
18093 const unsigned char *arrow_end = arrow_string + arrow_len;
18094 const unsigned char *p;
18095 struct it it;
18096 bool multibyte_p;
18097 int n_glyphs_before;
18099 set_buffer_temp (buffer);
18100 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18101 it.glyph_row->used[TEXT_AREA] = 0;
18102 SET_TEXT_POS (it.position, 0, 0);
18104 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18105 p = arrow_string;
18106 while (p < arrow_end)
18108 Lisp_Object face, ilisp;
18110 /* Get the next character. */
18111 if (multibyte_p)
18112 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18113 else
18115 it.c = it.char_to_display = *p, it.len = 1;
18116 if (! ASCII_CHAR_P (it.c))
18117 it.char_to_display = BYTE8_TO_CHAR (it.c);
18119 p += it.len;
18121 /* Get its face. */
18122 ilisp = make_number (p - arrow_string);
18123 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18124 it.face_id = compute_char_face (f, it.char_to_display, face);
18126 /* Compute its width, get its glyphs. */
18127 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18128 SET_TEXT_POS (it.position, -1, -1);
18129 PRODUCE_GLYPHS (&it);
18131 /* If this character doesn't fit any more in the line, we have
18132 to remove some glyphs. */
18133 if (it.current_x > it.last_visible_x)
18135 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18136 break;
18140 set_buffer_temp (old);
18141 return it.glyph_row;
18145 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18146 glyphs to insert is determined by produce_special_glyphs. */
18148 static void
18149 insert_left_trunc_glyphs (struct it *it)
18151 struct it truncate_it;
18152 struct glyph *from, *end, *to, *toend;
18154 eassert (!FRAME_WINDOW_P (it->f)
18155 || (!it->glyph_row->reversed_p
18156 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18157 || (it->glyph_row->reversed_p
18158 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18160 /* Get the truncation glyphs. */
18161 truncate_it = *it;
18162 truncate_it.current_x = 0;
18163 truncate_it.face_id = DEFAULT_FACE_ID;
18164 truncate_it.glyph_row = &scratch_glyph_row;
18165 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18166 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18167 truncate_it.object = make_number (0);
18168 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18170 /* Overwrite glyphs from IT with truncation glyphs. */
18171 if (!it->glyph_row->reversed_p)
18173 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18175 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18176 end = from + tused;
18177 to = it->glyph_row->glyphs[TEXT_AREA];
18178 toend = to + it->glyph_row->used[TEXT_AREA];
18179 if (FRAME_WINDOW_P (it->f))
18181 /* On GUI frames, when variable-size fonts are displayed,
18182 the truncation glyphs may need more pixels than the row's
18183 glyphs they overwrite. We overwrite more glyphs to free
18184 enough screen real estate, and enlarge the stretch glyph
18185 on the right (see display_line), if there is one, to
18186 preserve the screen position of the truncation glyphs on
18187 the right. */
18188 int w = 0;
18189 struct glyph *g = to;
18190 short used;
18192 /* The first glyph could be partially visible, in which case
18193 it->glyph_row->x will be negative. But we want the left
18194 truncation glyphs to be aligned at the left margin of the
18195 window, so we override the x coordinate at which the row
18196 will begin. */
18197 it->glyph_row->x = 0;
18198 while (g < toend && w < it->truncation_pixel_width)
18200 w += g->pixel_width;
18201 ++g;
18203 if (g - to - tused > 0)
18205 memmove (to + tused, g, (toend - g) * sizeof(*g));
18206 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18208 used = it->glyph_row->used[TEXT_AREA];
18209 if (it->glyph_row->truncated_on_right_p
18210 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18211 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18212 == STRETCH_GLYPH)
18214 int extra = w - it->truncation_pixel_width;
18216 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18220 while (from < end)
18221 *to++ = *from++;
18223 /* There may be padding glyphs left over. Overwrite them too. */
18224 if (!FRAME_WINDOW_P (it->f))
18226 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18228 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18229 while (from < end)
18230 *to++ = *from++;
18234 if (to > toend)
18235 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18237 else
18239 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18241 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18242 that back to front. */
18243 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18244 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18245 toend = it->glyph_row->glyphs[TEXT_AREA];
18246 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18247 if (FRAME_WINDOW_P (it->f))
18249 int w = 0;
18250 struct glyph *g = to;
18252 while (g >= toend && w < it->truncation_pixel_width)
18254 w += g->pixel_width;
18255 --g;
18257 if (to - g - tused > 0)
18258 to = g + tused;
18259 if (it->glyph_row->truncated_on_right_p
18260 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18261 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18263 int extra = w - it->truncation_pixel_width;
18265 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18269 while (from >= end && to >= toend)
18270 *to-- = *from--;
18271 if (!FRAME_WINDOW_P (it->f))
18273 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18275 from =
18276 truncate_it.glyph_row->glyphs[TEXT_AREA]
18277 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18278 while (from >= end && to >= toend)
18279 *to-- = *from--;
18282 if (from >= end)
18284 /* Need to free some room before prepending additional
18285 glyphs. */
18286 int move_by = from - end + 1;
18287 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18288 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18290 for ( ; g >= g0; g--)
18291 g[move_by] = *g;
18292 while (from >= end)
18293 *to-- = *from--;
18294 it->glyph_row->used[TEXT_AREA] += move_by;
18299 /* Compute the hash code for ROW. */
18300 unsigned
18301 row_hash (struct glyph_row *row)
18303 int area, k;
18304 unsigned hashval = 0;
18306 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18307 for (k = 0; k < row->used[area]; ++k)
18308 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18309 + row->glyphs[area][k].u.val
18310 + row->glyphs[area][k].face_id
18311 + row->glyphs[area][k].padding_p
18312 + (row->glyphs[area][k].type << 2));
18314 return hashval;
18317 /* Compute the pixel height and width of IT->glyph_row.
18319 Most of the time, ascent and height of a display line will be equal
18320 to the max_ascent and max_height values of the display iterator
18321 structure. This is not the case if
18323 1. We hit ZV without displaying anything. In this case, max_ascent
18324 and max_height will be zero.
18326 2. We have some glyphs that don't contribute to the line height.
18327 (The glyph row flag contributes_to_line_height_p is for future
18328 pixmap extensions).
18330 The first case is easily covered by using default values because in
18331 these cases, the line height does not really matter, except that it
18332 must not be zero. */
18334 static void
18335 compute_line_metrics (struct it *it)
18337 struct glyph_row *row = it->glyph_row;
18339 if (FRAME_WINDOW_P (it->f))
18341 int i, min_y, max_y;
18343 /* The line may consist of one space only, that was added to
18344 place the cursor on it. If so, the row's height hasn't been
18345 computed yet. */
18346 if (row->height == 0)
18348 if (it->max_ascent + it->max_descent == 0)
18349 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18350 row->ascent = it->max_ascent;
18351 row->height = it->max_ascent + it->max_descent;
18352 row->phys_ascent = it->max_phys_ascent;
18353 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18354 row->extra_line_spacing = it->max_extra_line_spacing;
18357 /* Compute the width of this line. */
18358 row->pixel_width = row->x;
18359 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18360 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18362 eassert (row->pixel_width >= 0);
18363 eassert (row->ascent >= 0 && row->height > 0);
18365 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18366 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18368 /* If first line's physical ascent is larger than its logical
18369 ascent, use the physical ascent, and make the row taller.
18370 This makes accented characters fully visible. */
18371 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18372 && row->phys_ascent > row->ascent)
18374 row->height += row->phys_ascent - row->ascent;
18375 row->ascent = row->phys_ascent;
18378 /* Compute how much of the line is visible. */
18379 row->visible_height = row->height;
18381 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18382 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18384 if (row->y < min_y)
18385 row->visible_height -= min_y - row->y;
18386 if (row->y + row->height > max_y)
18387 row->visible_height -= row->y + row->height - max_y;
18389 else
18391 row->pixel_width = row->used[TEXT_AREA];
18392 if (row->continued_p)
18393 row->pixel_width -= it->continuation_pixel_width;
18394 else if (row->truncated_on_right_p)
18395 row->pixel_width -= it->truncation_pixel_width;
18396 row->ascent = row->phys_ascent = 0;
18397 row->height = row->phys_height = row->visible_height = 1;
18398 row->extra_line_spacing = 0;
18401 /* Compute a hash code for this row. */
18402 row->hash = row_hash (row);
18404 it->max_ascent = it->max_descent = 0;
18405 it->max_phys_ascent = it->max_phys_descent = 0;
18409 /* Append one space to the glyph row of iterator IT if doing a
18410 window-based redisplay. The space has the same face as
18411 IT->face_id. Value is non-zero if a space was added.
18413 This function is called to make sure that there is always one glyph
18414 at the end of a glyph row that the cursor can be set on under
18415 window-systems. (If there weren't such a glyph we would not know
18416 how wide and tall a box cursor should be displayed).
18418 At the same time this space let's a nicely handle clearing to the
18419 end of the line if the row ends in italic text. */
18421 static int
18422 append_space_for_newline (struct it *it, int default_face_p)
18424 if (FRAME_WINDOW_P (it->f))
18426 int n = it->glyph_row->used[TEXT_AREA];
18428 if (it->glyph_row->glyphs[TEXT_AREA] + n
18429 < it->glyph_row->glyphs[1 + TEXT_AREA])
18431 /* Save some values that must not be changed.
18432 Must save IT->c and IT->len because otherwise
18433 ITERATOR_AT_END_P wouldn't work anymore after
18434 append_space_for_newline has been called. */
18435 enum display_element_type saved_what = it->what;
18436 int saved_c = it->c, saved_len = it->len;
18437 int saved_char_to_display = it->char_to_display;
18438 int saved_x = it->current_x;
18439 int saved_face_id = it->face_id;
18440 int saved_box_end = it->end_of_box_run_p;
18441 struct text_pos saved_pos;
18442 Lisp_Object saved_object;
18443 struct face *face;
18445 saved_object = it->object;
18446 saved_pos = it->position;
18448 it->what = IT_CHARACTER;
18449 memset (&it->position, 0, sizeof it->position);
18450 it->object = make_number (0);
18451 it->c = it->char_to_display = ' ';
18452 it->len = 1;
18454 /* If the default face was remapped, be sure to use the
18455 remapped face for the appended newline. */
18456 if (default_face_p)
18457 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18458 else if (it->face_before_selective_p)
18459 it->face_id = it->saved_face_id;
18460 face = FACE_FROM_ID (it->f, it->face_id);
18461 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18462 /* In R2L rows, we will prepend a stretch glyph that will
18463 have the end_of_box_run_p flag set for it, so there's no
18464 need for the appended newline glyph to have that flag
18465 set. */
18466 if (it->glyph_row->reversed_p
18467 /* But if the appended newline glyph goes all the way to
18468 the end of the row, there will be no stretch glyph,
18469 so leave the box flag set. */
18470 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18471 it->end_of_box_run_p = 0;
18473 PRODUCE_GLYPHS (it);
18475 it->override_ascent = -1;
18476 it->constrain_row_ascent_descent_p = 0;
18477 it->current_x = saved_x;
18478 it->object = saved_object;
18479 it->position = saved_pos;
18480 it->what = saved_what;
18481 it->face_id = saved_face_id;
18482 it->len = saved_len;
18483 it->c = saved_c;
18484 it->char_to_display = saved_char_to_display;
18485 it->end_of_box_run_p = saved_box_end;
18486 return 1;
18490 return 0;
18494 /* Extend the face of the last glyph in the text area of IT->glyph_row
18495 to the end of the display line. Called from display_line. If the
18496 glyph row is empty, add a space glyph to it so that we know the
18497 face to draw. Set the glyph row flag fill_line_p. If the glyph
18498 row is R2L, prepend a stretch glyph to cover the empty space to the
18499 left of the leftmost glyph. */
18501 static void
18502 extend_face_to_end_of_line (struct it *it)
18504 struct face *face, *default_face;
18505 struct frame *f = it->f;
18507 /* If line is already filled, do nothing. Non window-system frames
18508 get a grace of one more ``pixel'' because their characters are
18509 1-``pixel'' wide, so they hit the equality too early. This grace
18510 is needed only for R2L rows that are not continued, to produce
18511 one extra blank where we could display the cursor. */
18512 if (it->current_x >= it->last_visible_x
18513 + (!FRAME_WINDOW_P (f)
18514 && it->glyph_row->reversed_p
18515 && !it->glyph_row->continued_p))
18516 return;
18518 /* The default face, possibly remapped. */
18519 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18521 /* Face extension extends the background and box of IT->face_id
18522 to the end of the line. If the background equals the background
18523 of the frame, we don't have to do anything. */
18524 if (it->face_before_selective_p)
18525 face = FACE_FROM_ID (f, it->saved_face_id);
18526 else
18527 face = FACE_FROM_ID (f, it->face_id);
18529 if (FRAME_WINDOW_P (f)
18530 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18531 && face->box == FACE_NO_BOX
18532 && face->background == FRAME_BACKGROUND_PIXEL (f)
18533 #ifdef HAVE_WINDOW_SYSTEM
18534 && !face->stipple
18535 #endif
18536 && !it->glyph_row->reversed_p)
18537 return;
18539 /* Set the glyph row flag indicating that the face of the last glyph
18540 in the text area has to be drawn to the end of the text area. */
18541 it->glyph_row->fill_line_p = 1;
18543 /* If current character of IT is not ASCII, make sure we have the
18544 ASCII face. This will be automatically undone the next time
18545 get_next_display_element returns a multibyte character. Note
18546 that the character will always be single byte in unibyte
18547 text. */
18548 if (!ASCII_CHAR_P (it->c))
18550 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18553 if (FRAME_WINDOW_P (f))
18555 /* If the row is empty, add a space with the current face of IT,
18556 so that we know which face to draw. */
18557 if (it->glyph_row->used[TEXT_AREA] == 0)
18559 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18560 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18561 it->glyph_row->used[TEXT_AREA] = 1;
18563 #ifdef HAVE_WINDOW_SYSTEM
18564 if (it->glyph_row->reversed_p)
18566 /* Prepend a stretch glyph to the row, such that the
18567 rightmost glyph will be drawn flushed all the way to the
18568 right margin of the window. The stretch glyph that will
18569 occupy the empty space, if any, to the left of the
18570 glyphs. */
18571 struct font *font = face->font ? face->font : FRAME_FONT (f);
18572 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18573 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18574 struct glyph *g;
18575 int row_width, stretch_ascent, stretch_width;
18576 struct text_pos saved_pos;
18577 int saved_face_id, saved_avoid_cursor, saved_box_start;
18579 for (row_width = 0, g = row_start; g < row_end; g++)
18580 row_width += g->pixel_width;
18581 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18582 if (stretch_width > 0)
18584 stretch_ascent =
18585 (((it->ascent + it->descent)
18586 * FONT_BASE (font)) / FONT_HEIGHT (font));
18587 saved_pos = it->position;
18588 memset (&it->position, 0, sizeof it->position);
18589 saved_avoid_cursor = it->avoid_cursor_p;
18590 it->avoid_cursor_p = 1;
18591 saved_face_id = it->face_id;
18592 saved_box_start = it->start_of_box_run_p;
18593 /* The last row's stretch glyph should get the default
18594 face, to avoid painting the rest of the window with
18595 the region face, if the region ends at ZV. */
18596 if (it->glyph_row->ends_at_zv_p)
18597 it->face_id = default_face->id;
18598 else
18599 it->face_id = face->id;
18600 it->start_of_box_run_p = 0;
18601 append_stretch_glyph (it, make_number (0), stretch_width,
18602 it->ascent + it->descent, stretch_ascent);
18603 it->position = saved_pos;
18604 it->avoid_cursor_p = saved_avoid_cursor;
18605 it->face_id = saved_face_id;
18606 it->start_of_box_run_p = saved_box_start;
18609 #endif /* HAVE_WINDOW_SYSTEM */
18611 else
18613 /* Save some values that must not be changed. */
18614 int saved_x = it->current_x;
18615 struct text_pos saved_pos;
18616 Lisp_Object saved_object;
18617 enum display_element_type saved_what = it->what;
18618 int saved_face_id = it->face_id;
18620 saved_object = it->object;
18621 saved_pos = it->position;
18623 it->what = IT_CHARACTER;
18624 memset (&it->position, 0, sizeof it->position);
18625 it->object = make_number (0);
18626 it->c = it->char_to_display = ' ';
18627 it->len = 1;
18628 /* The last row's blank glyphs should get the default face, to
18629 avoid painting the rest of the window with the region face,
18630 if the region ends at ZV. */
18631 if (it->glyph_row->ends_at_zv_p)
18632 it->face_id = default_face->id;
18633 else
18634 it->face_id = face->id;
18636 PRODUCE_GLYPHS (it);
18638 while (it->current_x <= it->last_visible_x)
18639 PRODUCE_GLYPHS (it);
18641 /* Don't count these blanks really. It would let us insert a left
18642 truncation glyph below and make us set the cursor on them, maybe. */
18643 it->current_x = saved_x;
18644 it->object = saved_object;
18645 it->position = saved_pos;
18646 it->what = saved_what;
18647 it->face_id = saved_face_id;
18652 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18653 trailing whitespace. */
18655 static int
18656 trailing_whitespace_p (ptrdiff_t charpos)
18658 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18659 int c = 0;
18661 while (bytepos < ZV_BYTE
18662 && (c = FETCH_CHAR (bytepos),
18663 c == ' ' || c == '\t'))
18664 ++bytepos;
18666 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18668 if (bytepos != PT_BYTE)
18669 return 1;
18671 return 0;
18675 /* Highlight trailing whitespace, if any, in ROW. */
18677 static void
18678 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18680 int used = row->used[TEXT_AREA];
18682 if (used)
18684 struct glyph *start = row->glyphs[TEXT_AREA];
18685 struct glyph *glyph = start + used - 1;
18687 if (row->reversed_p)
18689 /* Right-to-left rows need to be processed in the opposite
18690 direction, so swap the edge pointers. */
18691 glyph = start;
18692 start = row->glyphs[TEXT_AREA] + used - 1;
18695 /* Skip over glyphs inserted to display the cursor at the
18696 end of a line, for extending the face of the last glyph
18697 to the end of the line on terminals, and for truncation
18698 and continuation glyphs. */
18699 if (!row->reversed_p)
18701 while (glyph >= start
18702 && glyph->type == CHAR_GLYPH
18703 && INTEGERP (glyph->object))
18704 --glyph;
18706 else
18708 while (glyph <= start
18709 && glyph->type == CHAR_GLYPH
18710 && INTEGERP (glyph->object))
18711 ++glyph;
18714 /* If last glyph is a space or stretch, and it's trailing
18715 whitespace, set the face of all trailing whitespace glyphs in
18716 IT->glyph_row to `trailing-whitespace'. */
18717 if ((row->reversed_p ? glyph <= start : glyph >= start)
18718 && BUFFERP (glyph->object)
18719 && (glyph->type == STRETCH_GLYPH
18720 || (glyph->type == CHAR_GLYPH
18721 && glyph->u.ch == ' '))
18722 && trailing_whitespace_p (glyph->charpos))
18724 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18725 if (face_id < 0)
18726 return;
18728 if (!row->reversed_p)
18730 while (glyph >= start
18731 && BUFFERP (glyph->object)
18732 && (glyph->type == STRETCH_GLYPH
18733 || (glyph->type == CHAR_GLYPH
18734 && glyph->u.ch == ' ')))
18735 (glyph--)->face_id = face_id;
18737 else
18739 while (glyph <= start
18740 && BUFFERP (glyph->object)
18741 && (glyph->type == STRETCH_GLYPH
18742 || (glyph->type == CHAR_GLYPH
18743 && glyph->u.ch == ' ')))
18744 (glyph++)->face_id = face_id;
18751 /* Value is non-zero if glyph row ROW should be
18752 considered to hold the buffer position CHARPOS. */
18754 static int
18755 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18757 int result = 1;
18759 if (charpos == CHARPOS (row->end.pos)
18760 || charpos == MATRIX_ROW_END_CHARPOS (row))
18762 /* Suppose the row ends on a string.
18763 Unless the row is continued, that means it ends on a newline
18764 in the string. If it's anything other than a display string
18765 (e.g., a before-string from an overlay), we don't want the
18766 cursor there. (This heuristic seems to give the optimal
18767 behavior for the various types of multi-line strings.)
18768 One exception: if the string has `cursor' property on one of
18769 its characters, we _do_ want the cursor there. */
18770 if (CHARPOS (row->end.string_pos) >= 0)
18772 if (row->continued_p)
18773 result = 1;
18774 else
18776 /* Check for `display' property. */
18777 struct glyph *beg = row->glyphs[TEXT_AREA];
18778 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18779 struct glyph *glyph;
18781 result = 0;
18782 for (glyph = end; glyph >= beg; --glyph)
18783 if (STRINGP (glyph->object))
18785 Lisp_Object prop
18786 = Fget_char_property (make_number (charpos),
18787 Qdisplay, Qnil);
18788 result =
18789 (!NILP (prop)
18790 && display_prop_string_p (prop, glyph->object));
18791 /* If there's a `cursor' property on one of the
18792 string's characters, this row is a cursor row,
18793 even though this is not a display string. */
18794 if (!result)
18796 Lisp_Object s = glyph->object;
18798 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18800 ptrdiff_t gpos = glyph->charpos;
18802 if (!NILP (Fget_char_property (make_number (gpos),
18803 Qcursor, s)))
18805 result = 1;
18806 break;
18810 break;
18814 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18816 /* If the row ends in middle of a real character,
18817 and the line is continued, we want the cursor here.
18818 That's because CHARPOS (ROW->end.pos) would equal
18819 PT if PT is before the character. */
18820 if (!row->ends_in_ellipsis_p)
18821 result = row->continued_p;
18822 else
18823 /* If the row ends in an ellipsis, then
18824 CHARPOS (ROW->end.pos) will equal point after the
18825 invisible text. We want that position to be displayed
18826 after the ellipsis. */
18827 result = 0;
18829 /* If the row ends at ZV, display the cursor at the end of that
18830 row instead of at the start of the row below. */
18831 else if (row->ends_at_zv_p)
18832 result = 1;
18833 else
18834 result = 0;
18837 return result;
18840 /* Value is non-zero if glyph row ROW should be
18841 used to hold the cursor. */
18843 static int
18844 cursor_row_p (struct glyph_row *row)
18846 return row_for_charpos_p (row, PT);
18851 /* Push the property PROP so that it will be rendered at the current
18852 position in IT. Return 1 if PROP was successfully pushed, 0
18853 otherwise. Called from handle_line_prefix to handle the
18854 `line-prefix' and `wrap-prefix' properties. */
18856 static int
18857 push_prefix_prop (struct it *it, Lisp_Object prop)
18859 struct text_pos pos =
18860 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18862 eassert (it->method == GET_FROM_BUFFER
18863 || it->method == GET_FROM_DISPLAY_VECTOR
18864 || it->method == GET_FROM_STRING);
18866 /* We need to save the current buffer/string position, so it will be
18867 restored by pop_it, because iterate_out_of_display_property
18868 depends on that being set correctly, but some situations leave
18869 it->position not yet set when this function is called. */
18870 push_it (it, &pos);
18872 if (STRINGP (prop))
18874 if (SCHARS (prop) == 0)
18876 pop_it (it);
18877 return 0;
18880 it->string = prop;
18881 it->string_from_prefix_prop_p = 1;
18882 it->multibyte_p = STRING_MULTIBYTE (it->string);
18883 it->current.overlay_string_index = -1;
18884 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18885 it->end_charpos = it->string_nchars = SCHARS (it->string);
18886 it->method = GET_FROM_STRING;
18887 it->stop_charpos = 0;
18888 it->prev_stop = 0;
18889 it->base_level_stop = 0;
18891 /* Force paragraph direction to be that of the parent
18892 buffer/string. */
18893 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18894 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18895 else
18896 it->paragraph_embedding = L2R;
18898 /* Set up the bidi iterator for this display string. */
18899 if (it->bidi_p)
18901 it->bidi_it.string.lstring = it->string;
18902 it->bidi_it.string.s = NULL;
18903 it->bidi_it.string.schars = it->end_charpos;
18904 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18905 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18906 it->bidi_it.string.unibyte = !it->multibyte_p;
18907 it->bidi_it.w = it->w;
18908 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18911 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18913 it->method = GET_FROM_STRETCH;
18914 it->object = prop;
18916 #ifdef HAVE_WINDOW_SYSTEM
18917 else if (IMAGEP (prop))
18919 it->what = IT_IMAGE;
18920 it->image_id = lookup_image (it->f, prop);
18921 it->method = GET_FROM_IMAGE;
18923 #endif /* HAVE_WINDOW_SYSTEM */
18924 else
18926 pop_it (it); /* bogus display property, give up */
18927 return 0;
18930 return 1;
18933 /* Return the character-property PROP at the current position in IT. */
18935 static Lisp_Object
18936 get_it_property (struct it *it, Lisp_Object prop)
18938 Lisp_Object position, object = it->object;
18940 if (STRINGP (object))
18941 position = make_number (IT_STRING_CHARPOS (*it));
18942 else if (BUFFERP (object))
18944 position = make_number (IT_CHARPOS (*it));
18945 object = it->window;
18947 else
18948 return Qnil;
18950 return Fget_char_property (position, prop, object);
18953 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18955 static void
18956 handle_line_prefix (struct it *it)
18958 Lisp_Object prefix;
18960 if (it->continuation_lines_width > 0)
18962 prefix = get_it_property (it, Qwrap_prefix);
18963 if (NILP (prefix))
18964 prefix = Vwrap_prefix;
18966 else
18968 prefix = get_it_property (it, Qline_prefix);
18969 if (NILP (prefix))
18970 prefix = Vline_prefix;
18972 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18974 /* If the prefix is wider than the window, and we try to wrap
18975 it, it would acquire its own wrap prefix, and so on till the
18976 iterator stack overflows. So, don't wrap the prefix. */
18977 it->line_wrap = TRUNCATE;
18978 it->avoid_cursor_p = 1;
18984 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18985 only for R2L lines from display_line and display_string, when they
18986 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18987 the line/string needs to be continued on the next glyph row. */
18988 static void
18989 unproduce_glyphs (struct it *it, int n)
18991 struct glyph *glyph, *end;
18993 eassert (it->glyph_row);
18994 eassert (it->glyph_row->reversed_p);
18995 eassert (it->area == TEXT_AREA);
18996 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18998 if (n > it->glyph_row->used[TEXT_AREA])
18999 n = it->glyph_row->used[TEXT_AREA];
19000 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19001 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19002 for ( ; glyph < end; glyph++)
19003 glyph[-n] = *glyph;
19006 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19007 and ROW->maxpos. */
19008 static void
19009 find_row_edges (struct it *it, struct glyph_row *row,
19010 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19011 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19013 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19014 lines' rows is implemented for bidi-reordered rows. */
19016 /* ROW->minpos is the value of min_pos, the minimal buffer position
19017 we have in ROW, or ROW->start.pos if that is smaller. */
19018 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19019 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19020 else
19021 /* We didn't find buffer positions smaller than ROW->start, or
19022 didn't find _any_ valid buffer positions in any of the glyphs,
19023 so we must trust the iterator's computed positions. */
19024 row->minpos = row->start.pos;
19025 if (max_pos <= 0)
19027 max_pos = CHARPOS (it->current.pos);
19028 max_bpos = BYTEPOS (it->current.pos);
19031 /* Here are the various use-cases for ending the row, and the
19032 corresponding values for ROW->maxpos:
19034 Line ends in a newline from buffer eol_pos + 1
19035 Line is continued from buffer max_pos + 1
19036 Line is truncated on right it->current.pos
19037 Line ends in a newline from string max_pos + 1(*)
19038 (*) + 1 only when line ends in a forward scan
19039 Line is continued from string max_pos
19040 Line is continued from display vector max_pos
19041 Line is entirely from a string min_pos == max_pos
19042 Line is entirely from a display vector min_pos == max_pos
19043 Line that ends at ZV ZV
19045 If you discover other use-cases, please add them here as
19046 appropriate. */
19047 if (row->ends_at_zv_p)
19048 row->maxpos = it->current.pos;
19049 else if (row->used[TEXT_AREA])
19051 int seen_this_string = 0;
19052 struct glyph_row *r1 = row - 1;
19054 /* Did we see the same display string on the previous row? */
19055 if (STRINGP (it->object)
19056 /* this is not the first row */
19057 && row > it->w->desired_matrix->rows
19058 /* previous row is not the header line */
19059 && !r1->mode_line_p
19060 /* previous row also ends in a newline from a string */
19061 && r1->ends_in_newline_from_string_p)
19063 struct glyph *start, *end;
19065 /* Search for the last glyph of the previous row that came
19066 from buffer or string. Depending on whether the row is
19067 L2R or R2L, we need to process it front to back or the
19068 other way round. */
19069 if (!r1->reversed_p)
19071 start = r1->glyphs[TEXT_AREA];
19072 end = start + r1->used[TEXT_AREA];
19073 /* Glyphs inserted by redisplay have an integer (zero)
19074 as their object. */
19075 while (end > start
19076 && INTEGERP ((end - 1)->object)
19077 && (end - 1)->charpos <= 0)
19078 --end;
19079 if (end > start)
19081 if (EQ ((end - 1)->object, it->object))
19082 seen_this_string = 1;
19084 else
19085 /* If all the glyphs of the previous row were inserted
19086 by redisplay, it means the previous row was
19087 produced from a single newline, which is only
19088 possible if that newline came from the same string
19089 as the one which produced this ROW. */
19090 seen_this_string = 1;
19092 else
19094 end = r1->glyphs[TEXT_AREA] - 1;
19095 start = end + r1->used[TEXT_AREA];
19096 while (end < start
19097 && INTEGERP ((end + 1)->object)
19098 && (end + 1)->charpos <= 0)
19099 ++end;
19100 if (end < start)
19102 if (EQ ((end + 1)->object, it->object))
19103 seen_this_string = 1;
19105 else
19106 seen_this_string = 1;
19109 /* Take note of each display string that covers a newline only
19110 once, the first time we see it. This is for when a display
19111 string includes more than one newline in it. */
19112 if (row->ends_in_newline_from_string_p && !seen_this_string)
19114 /* If we were scanning the buffer forward when we displayed
19115 the string, we want to account for at least one buffer
19116 position that belongs to this row (position covered by
19117 the display string), so that cursor positioning will
19118 consider this row as a candidate when point is at the end
19119 of the visual line represented by this row. This is not
19120 required when scanning back, because max_pos will already
19121 have a much larger value. */
19122 if (CHARPOS (row->end.pos) > max_pos)
19123 INC_BOTH (max_pos, max_bpos);
19124 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19126 else if (CHARPOS (it->eol_pos) > 0)
19127 SET_TEXT_POS (row->maxpos,
19128 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19129 else if (row->continued_p)
19131 /* If max_pos is different from IT's current position, it
19132 means IT->method does not belong to the display element
19133 at max_pos. However, it also means that the display
19134 element at max_pos was displayed in its entirety on this
19135 line, which is equivalent to saying that the next line
19136 starts at the next buffer position. */
19137 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19138 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19139 else
19141 INC_BOTH (max_pos, max_bpos);
19142 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19145 else if (row->truncated_on_right_p)
19146 /* display_line already called reseat_at_next_visible_line_start,
19147 which puts the iterator at the beginning of the next line, in
19148 the logical order. */
19149 row->maxpos = it->current.pos;
19150 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19151 /* A line that is entirely from a string/image/stretch... */
19152 row->maxpos = row->minpos;
19153 else
19154 emacs_abort ();
19156 else
19157 row->maxpos = it->current.pos;
19160 /* Construct the glyph row IT->glyph_row in the desired matrix of
19161 IT->w from text at the current position of IT. See dispextern.h
19162 for an overview of struct it. Value is non-zero if
19163 IT->glyph_row displays text, as opposed to a line displaying ZV
19164 only. */
19166 static int
19167 display_line (struct it *it)
19169 struct glyph_row *row = it->glyph_row;
19170 Lisp_Object overlay_arrow_string;
19171 struct it wrap_it;
19172 void *wrap_data = NULL;
19173 int may_wrap = 0, wrap_x IF_LINT (= 0);
19174 int wrap_row_used = -1;
19175 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19176 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19177 int wrap_row_extra_line_spacing IF_LINT (= 0);
19178 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19179 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19180 int cvpos;
19181 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19182 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19184 /* We always start displaying at hpos zero even if hscrolled. */
19185 eassert (it->hpos == 0 && it->current_x == 0);
19187 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19188 >= it->w->desired_matrix->nrows)
19190 it->w->nrows_scale_factor++;
19191 it->f->fonts_changed = 1;
19192 return 0;
19195 /* Clear the result glyph row and enable it. */
19196 prepare_desired_row (row);
19198 row->y = it->current_y;
19199 row->start = it->start;
19200 row->continuation_lines_width = it->continuation_lines_width;
19201 row->displays_text_p = 1;
19202 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19203 it->starts_in_middle_of_char_p = 0;
19205 /* Arrange the overlays nicely for our purposes. Usually, we call
19206 display_line on only one line at a time, in which case this
19207 can't really hurt too much, or we call it on lines which appear
19208 one after another in the buffer, in which case all calls to
19209 recenter_overlay_lists but the first will be pretty cheap. */
19210 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19212 /* Move over display elements that are not visible because we are
19213 hscrolled. This may stop at an x-position < IT->first_visible_x
19214 if the first glyph is partially visible or if we hit a line end. */
19215 if (it->current_x < it->first_visible_x)
19217 enum move_it_result move_result;
19219 this_line_min_pos = row->start.pos;
19220 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19221 MOVE_TO_POS | MOVE_TO_X);
19222 /* If we are under a large hscroll, move_it_in_display_line_to
19223 could hit the end of the line without reaching
19224 it->first_visible_x. Pretend that we did reach it. This is
19225 especially important on a TTY, where we will call
19226 extend_face_to_end_of_line, which needs to know how many
19227 blank glyphs to produce. */
19228 if (it->current_x < it->first_visible_x
19229 && (move_result == MOVE_NEWLINE_OR_CR
19230 || move_result == MOVE_POS_MATCH_OR_ZV))
19231 it->current_x = it->first_visible_x;
19233 /* Record the smallest positions seen while we moved over
19234 display elements that are not visible. This is needed by
19235 redisplay_internal for optimizing the case where the cursor
19236 stays inside the same line. The rest of this function only
19237 considers positions that are actually displayed, so
19238 RECORD_MAX_MIN_POS will not otherwise record positions that
19239 are hscrolled to the left of the left edge of the window. */
19240 min_pos = CHARPOS (this_line_min_pos);
19241 min_bpos = BYTEPOS (this_line_min_pos);
19243 else
19245 /* We only do this when not calling `move_it_in_display_line_to'
19246 above, because move_it_in_display_line_to calls
19247 handle_line_prefix itself. */
19248 handle_line_prefix (it);
19251 /* Get the initial row height. This is either the height of the
19252 text hscrolled, if there is any, or zero. */
19253 row->ascent = it->max_ascent;
19254 row->height = it->max_ascent + it->max_descent;
19255 row->phys_ascent = it->max_phys_ascent;
19256 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19257 row->extra_line_spacing = it->max_extra_line_spacing;
19259 /* Utility macro to record max and min buffer positions seen until now. */
19260 #define RECORD_MAX_MIN_POS(IT) \
19261 do \
19263 int composition_p = !STRINGP ((IT)->string) \
19264 && ((IT)->what == IT_COMPOSITION); \
19265 ptrdiff_t current_pos = \
19266 composition_p ? (IT)->cmp_it.charpos \
19267 : IT_CHARPOS (*(IT)); \
19268 ptrdiff_t current_bpos = \
19269 composition_p ? CHAR_TO_BYTE (current_pos) \
19270 : IT_BYTEPOS (*(IT)); \
19271 if (current_pos < min_pos) \
19273 min_pos = current_pos; \
19274 min_bpos = current_bpos; \
19276 if (IT_CHARPOS (*it) > max_pos) \
19278 max_pos = IT_CHARPOS (*it); \
19279 max_bpos = IT_BYTEPOS (*it); \
19282 while (0)
19284 /* Loop generating characters. The loop is left with IT on the next
19285 character to display. */
19286 while (1)
19288 int n_glyphs_before, hpos_before, x_before;
19289 int x, nglyphs;
19290 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19292 /* Retrieve the next thing to display. Value is zero if end of
19293 buffer reached. */
19294 if (!get_next_display_element (it))
19296 /* Maybe add a space at the end of this line that is used to
19297 display the cursor there under X. Set the charpos of the
19298 first glyph of blank lines not corresponding to any text
19299 to -1. */
19300 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19301 row->exact_window_width_line_p = 1;
19302 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19303 || row->used[TEXT_AREA] == 0)
19305 row->glyphs[TEXT_AREA]->charpos = -1;
19306 row->displays_text_p = 0;
19308 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19309 && (!MINI_WINDOW_P (it->w)
19310 || (minibuf_level && EQ (it->window, minibuf_window))))
19311 row->indicate_empty_line_p = 1;
19314 it->continuation_lines_width = 0;
19315 row->ends_at_zv_p = 1;
19316 /* A row that displays right-to-left text must always have
19317 its last face extended all the way to the end of line,
19318 even if this row ends in ZV, because we still write to
19319 the screen left to right. We also need to extend the
19320 last face if the default face is remapped to some
19321 different face, otherwise the functions that clear
19322 portions of the screen will clear with the default face's
19323 background color. */
19324 if (row->reversed_p
19325 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19326 extend_face_to_end_of_line (it);
19327 break;
19330 /* Now, get the metrics of what we want to display. This also
19331 generates glyphs in `row' (which is IT->glyph_row). */
19332 n_glyphs_before = row->used[TEXT_AREA];
19333 x = it->current_x;
19335 /* Remember the line height so far in case the next element doesn't
19336 fit on the line. */
19337 if (it->line_wrap != TRUNCATE)
19339 ascent = it->max_ascent;
19340 descent = it->max_descent;
19341 phys_ascent = it->max_phys_ascent;
19342 phys_descent = it->max_phys_descent;
19344 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19346 if (IT_DISPLAYING_WHITESPACE (it))
19347 may_wrap = 1;
19348 else if (may_wrap)
19350 SAVE_IT (wrap_it, *it, wrap_data);
19351 wrap_x = x;
19352 wrap_row_used = row->used[TEXT_AREA];
19353 wrap_row_ascent = row->ascent;
19354 wrap_row_height = row->height;
19355 wrap_row_phys_ascent = row->phys_ascent;
19356 wrap_row_phys_height = row->phys_height;
19357 wrap_row_extra_line_spacing = row->extra_line_spacing;
19358 wrap_row_min_pos = min_pos;
19359 wrap_row_min_bpos = min_bpos;
19360 wrap_row_max_pos = max_pos;
19361 wrap_row_max_bpos = max_bpos;
19362 may_wrap = 0;
19367 PRODUCE_GLYPHS (it);
19369 /* If this display element was in marginal areas, continue with
19370 the next one. */
19371 if (it->area != TEXT_AREA)
19373 row->ascent = max (row->ascent, it->max_ascent);
19374 row->height = max (row->height, it->max_ascent + it->max_descent);
19375 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19376 row->phys_height = max (row->phys_height,
19377 it->max_phys_ascent + it->max_phys_descent);
19378 row->extra_line_spacing = max (row->extra_line_spacing,
19379 it->max_extra_line_spacing);
19380 set_iterator_to_next (it, 1);
19381 continue;
19384 /* Does the display element fit on the line? If we truncate
19385 lines, we should draw past the right edge of the window. If
19386 we don't truncate, we want to stop so that we can display the
19387 continuation glyph before the right margin. If lines are
19388 continued, there are two possible strategies for characters
19389 resulting in more than 1 glyph (e.g. tabs): Display as many
19390 glyphs as possible in this line and leave the rest for the
19391 continuation line, or display the whole element in the next
19392 line. Original redisplay did the former, so we do it also. */
19393 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19394 hpos_before = it->hpos;
19395 x_before = x;
19397 if (/* Not a newline. */
19398 nglyphs > 0
19399 /* Glyphs produced fit entirely in the line. */
19400 && it->current_x < it->last_visible_x)
19402 it->hpos += nglyphs;
19403 row->ascent = max (row->ascent, it->max_ascent);
19404 row->height = max (row->height, it->max_ascent + it->max_descent);
19405 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19406 row->phys_height = max (row->phys_height,
19407 it->max_phys_ascent + it->max_phys_descent);
19408 row->extra_line_spacing = max (row->extra_line_spacing,
19409 it->max_extra_line_spacing);
19410 if (it->current_x - it->pixel_width < it->first_visible_x)
19411 row->x = x - it->first_visible_x;
19412 /* Record the maximum and minimum buffer positions seen so
19413 far in glyphs that will be displayed by this row. */
19414 if (it->bidi_p)
19415 RECORD_MAX_MIN_POS (it);
19417 else
19419 int i, new_x;
19420 struct glyph *glyph;
19422 for (i = 0; i < nglyphs; ++i, x = new_x)
19424 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19425 new_x = x + glyph->pixel_width;
19427 if (/* Lines are continued. */
19428 it->line_wrap != TRUNCATE
19429 && (/* Glyph doesn't fit on the line. */
19430 new_x > it->last_visible_x
19431 /* Or it fits exactly on a window system frame. */
19432 || (new_x == it->last_visible_x
19433 && FRAME_WINDOW_P (it->f)
19434 && (row->reversed_p
19435 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19436 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19438 /* End of a continued line. */
19440 if (it->hpos == 0
19441 || (new_x == it->last_visible_x
19442 && FRAME_WINDOW_P (it->f)
19443 && (row->reversed_p
19444 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19445 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19447 /* Current glyph is the only one on the line or
19448 fits exactly on the line. We must continue
19449 the line because we can't draw the cursor
19450 after the glyph. */
19451 row->continued_p = 1;
19452 it->current_x = new_x;
19453 it->continuation_lines_width += new_x;
19454 ++it->hpos;
19455 if (i == nglyphs - 1)
19457 /* If line-wrap is on, check if a previous
19458 wrap point was found. */
19459 if (wrap_row_used > 0
19460 /* Even if there is a previous wrap
19461 point, continue the line here as
19462 usual, if (i) the previous character
19463 was a space or tab AND (ii) the
19464 current character is not. */
19465 && (!may_wrap
19466 || IT_DISPLAYING_WHITESPACE (it)))
19467 goto back_to_wrap;
19469 /* Record the maximum and minimum buffer
19470 positions seen so far in glyphs that will be
19471 displayed by this row. */
19472 if (it->bidi_p)
19473 RECORD_MAX_MIN_POS (it);
19474 set_iterator_to_next (it, 1);
19475 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19477 if (!get_next_display_element (it))
19479 row->exact_window_width_line_p = 1;
19480 it->continuation_lines_width = 0;
19481 row->continued_p = 0;
19482 row->ends_at_zv_p = 1;
19484 else if (ITERATOR_AT_END_OF_LINE_P (it))
19486 row->continued_p = 0;
19487 row->exact_window_width_line_p = 1;
19491 else if (it->bidi_p)
19492 RECORD_MAX_MIN_POS (it);
19494 else if (CHAR_GLYPH_PADDING_P (*glyph)
19495 && !FRAME_WINDOW_P (it->f))
19497 /* A padding glyph that doesn't fit on this line.
19498 This means the whole character doesn't fit
19499 on the line. */
19500 if (row->reversed_p)
19501 unproduce_glyphs (it, row->used[TEXT_AREA]
19502 - n_glyphs_before);
19503 row->used[TEXT_AREA] = n_glyphs_before;
19505 /* Fill the rest of the row with continuation
19506 glyphs like in 20.x. */
19507 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19508 < row->glyphs[1 + TEXT_AREA])
19509 produce_special_glyphs (it, IT_CONTINUATION);
19511 row->continued_p = 1;
19512 it->current_x = x_before;
19513 it->continuation_lines_width += x_before;
19515 /* Restore the height to what it was before the
19516 element not fitting on the line. */
19517 it->max_ascent = ascent;
19518 it->max_descent = descent;
19519 it->max_phys_ascent = phys_ascent;
19520 it->max_phys_descent = phys_descent;
19522 else if (wrap_row_used > 0)
19524 back_to_wrap:
19525 if (row->reversed_p)
19526 unproduce_glyphs (it,
19527 row->used[TEXT_AREA] - wrap_row_used);
19528 RESTORE_IT (it, &wrap_it, wrap_data);
19529 it->continuation_lines_width += wrap_x;
19530 row->used[TEXT_AREA] = wrap_row_used;
19531 row->ascent = wrap_row_ascent;
19532 row->height = wrap_row_height;
19533 row->phys_ascent = wrap_row_phys_ascent;
19534 row->phys_height = wrap_row_phys_height;
19535 row->extra_line_spacing = wrap_row_extra_line_spacing;
19536 min_pos = wrap_row_min_pos;
19537 min_bpos = wrap_row_min_bpos;
19538 max_pos = wrap_row_max_pos;
19539 max_bpos = wrap_row_max_bpos;
19540 row->continued_p = 1;
19541 row->ends_at_zv_p = 0;
19542 row->exact_window_width_line_p = 0;
19543 it->continuation_lines_width += x;
19545 /* Make sure that a non-default face is extended
19546 up to the right margin of the window. */
19547 extend_face_to_end_of_line (it);
19549 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19551 /* A TAB that extends past the right edge of the
19552 window. This produces a single glyph on
19553 window system frames. We leave the glyph in
19554 this row and let it fill the row, but don't
19555 consume the TAB. */
19556 if ((row->reversed_p
19557 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19558 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19559 produce_special_glyphs (it, IT_CONTINUATION);
19560 it->continuation_lines_width += it->last_visible_x;
19561 row->ends_in_middle_of_char_p = 1;
19562 row->continued_p = 1;
19563 glyph->pixel_width = it->last_visible_x - x;
19564 it->starts_in_middle_of_char_p = 1;
19566 else
19568 /* Something other than a TAB that draws past
19569 the right edge of the window. Restore
19570 positions to values before the element. */
19571 if (row->reversed_p)
19572 unproduce_glyphs (it, row->used[TEXT_AREA]
19573 - (n_glyphs_before + i));
19574 row->used[TEXT_AREA] = n_glyphs_before + i;
19576 /* Display continuation glyphs. */
19577 it->current_x = x_before;
19578 it->continuation_lines_width += x;
19579 if (!FRAME_WINDOW_P (it->f)
19580 || (row->reversed_p
19581 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19582 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19583 produce_special_glyphs (it, IT_CONTINUATION);
19584 row->continued_p = 1;
19586 extend_face_to_end_of_line (it);
19588 if (nglyphs > 1 && i > 0)
19590 row->ends_in_middle_of_char_p = 1;
19591 it->starts_in_middle_of_char_p = 1;
19594 /* Restore the height to what it was before the
19595 element not fitting on the line. */
19596 it->max_ascent = ascent;
19597 it->max_descent = descent;
19598 it->max_phys_ascent = phys_ascent;
19599 it->max_phys_descent = phys_descent;
19602 break;
19604 else if (new_x > it->first_visible_x)
19606 /* Increment number of glyphs actually displayed. */
19607 ++it->hpos;
19609 /* Record the maximum and minimum buffer positions
19610 seen so far in glyphs that will be displayed by
19611 this row. */
19612 if (it->bidi_p)
19613 RECORD_MAX_MIN_POS (it);
19615 if (x < it->first_visible_x)
19616 /* Glyph is partially visible, i.e. row starts at
19617 negative X position. */
19618 row->x = x - it->first_visible_x;
19620 else
19622 /* Glyph is completely off the left margin of the
19623 window. This should not happen because of the
19624 move_it_in_display_line at the start of this
19625 function, unless the text display area of the
19626 window is empty. */
19627 eassert (it->first_visible_x <= it->last_visible_x);
19630 /* Even if this display element produced no glyphs at all,
19631 we want to record its position. */
19632 if (it->bidi_p && nglyphs == 0)
19633 RECORD_MAX_MIN_POS (it);
19635 row->ascent = max (row->ascent, it->max_ascent);
19636 row->height = max (row->height, it->max_ascent + it->max_descent);
19637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19638 row->phys_height = max (row->phys_height,
19639 it->max_phys_ascent + it->max_phys_descent);
19640 row->extra_line_spacing = max (row->extra_line_spacing,
19641 it->max_extra_line_spacing);
19643 /* End of this display line if row is continued. */
19644 if (row->continued_p || row->ends_at_zv_p)
19645 break;
19648 at_end_of_line:
19649 /* Is this a line end? If yes, we're also done, after making
19650 sure that a non-default face is extended up to the right
19651 margin of the window. */
19652 if (ITERATOR_AT_END_OF_LINE_P (it))
19654 int used_before = row->used[TEXT_AREA];
19656 row->ends_in_newline_from_string_p = STRINGP (it->object);
19658 /* Add a space at the end of the line that is used to
19659 display the cursor there. */
19660 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19661 append_space_for_newline (it, 0);
19663 /* Extend the face to the end of the line. */
19664 extend_face_to_end_of_line (it);
19666 /* Make sure we have the position. */
19667 if (used_before == 0)
19668 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19670 /* Record the position of the newline, for use in
19671 find_row_edges. */
19672 it->eol_pos = it->current.pos;
19674 /* Consume the line end. This skips over invisible lines. */
19675 set_iterator_to_next (it, 1);
19676 it->continuation_lines_width = 0;
19677 break;
19680 /* Proceed with next display element. Note that this skips
19681 over lines invisible because of selective display. */
19682 set_iterator_to_next (it, 1);
19684 /* If we truncate lines, we are done when the last displayed
19685 glyphs reach past the right margin of the window. */
19686 if (it->line_wrap == TRUNCATE
19687 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19688 ? (it->current_x >= it->last_visible_x)
19689 : (it->current_x > it->last_visible_x)))
19691 /* Maybe add truncation glyphs. */
19692 if (!FRAME_WINDOW_P (it->f)
19693 || (row->reversed_p
19694 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19695 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19697 int i, n;
19699 if (!row->reversed_p)
19701 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19702 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19703 break;
19705 else
19707 for (i = 0; i < row->used[TEXT_AREA]; i++)
19708 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19709 break;
19710 /* Remove any padding glyphs at the front of ROW, to
19711 make room for the truncation glyphs we will be
19712 adding below. The loop below always inserts at
19713 least one truncation glyph, so also remove the
19714 last glyph added to ROW. */
19715 unproduce_glyphs (it, i + 1);
19716 /* Adjust i for the loop below. */
19717 i = row->used[TEXT_AREA] - (i + 1);
19720 it->current_x = x_before;
19721 if (!FRAME_WINDOW_P (it->f))
19723 for (n = row->used[TEXT_AREA]; i < n; ++i)
19725 row->used[TEXT_AREA] = i;
19726 produce_special_glyphs (it, IT_TRUNCATION);
19729 else
19731 row->used[TEXT_AREA] = i;
19732 produce_special_glyphs (it, IT_TRUNCATION);
19735 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19737 /* Don't truncate if we can overflow newline into fringe. */
19738 if (!get_next_display_element (it))
19740 it->continuation_lines_width = 0;
19741 row->ends_at_zv_p = 1;
19742 row->exact_window_width_line_p = 1;
19743 break;
19745 if (ITERATOR_AT_END_OF_LINE_P (it))
19747 row->exact_window_width_line_p = 1;
19748 goto at_end_of_line;
19750 it->current_x = x_before;
19753 row->truncated_on_right_p = 1;
19754 it->continuation_lines_width = 0;
19755 reseat_at_next_visible_line_start (it, 0);
19756 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19757 it->hpos = hpos_before;
19758 break;
19762 if (wrap_data)
19763 bidi_unshelve_cache (wrap_data, 1);
19765 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19766 at the left window margin. */
19767 if (it->first_visible_x
19768 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19770 if (!FRAME_WINDOW_P (it->f)
19771 || (row->reversed_p
19772 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19773 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19774 insert_left_trunc_glyphs (it);
19775 row->truncated_on_left_p = 1;
19778 /* Remember the position at which this line ends.
19780 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19781 cannot be before the call to find_row_edges below, since that is
19782 where these positions are determined. */
19783 row->end = it->current;
19784 if (!it->bidi_p)
19786 row->minpos = row->start.pos;
19787 row->maxpos = row->end.pos;
19789 else
19791 /* ROW->minpos and ROW->maxpos must be the smallest and
19792 `1 + the largest' buffer positions in ROW. But if ROW was
19793 bidi-reordered, these two positions can be anywhere in the
19794 row, so we must determine them now. */
19795 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19798 /* If the start of this line is the overlay arrow-position, then
19799 mark this glyph row as the one containing the overlay arrow.
19800 This is clearly a mess with variable size fonts. It would be
19801 better to let it be displayed like cursors under X. */
19802 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19803 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19804 !NILP (overlay_arrow_string)))
19806 /* Overlay arrow in window redisplay is a fringe bitmap. */
19807 if (STRINGP (overlay_arrow_string))
19809 struct glyph_row *arrow_row
19810 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19811 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19812 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19813 struct glyph *p = row->glyphs[TEXT_AREA];
19814 struct glyph *p2, *end;
19816 /* Copy the arrow glyphs. */
19817 while (glyph < arrow_end)
19818 *p++ = *glyph++;
19820 /* Throw away padding glyphs. */
19821 p2 = p;
19822 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19823 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19824 ++p2;
19825 if (p2 > p)
19827 while (p2 < end)
19828 *p++ = *p2++;
19829 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19832 else
19834 eassert (INTEGERP (overlay_arrow_string));
19835 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19837 overlay_arrow_seen = 1;
19840 /* Highlight trailing whitespace. */
19841 if (!NILP (Vshow_trailing_whitespace))
19842 highlight_trailing_whitespace (it->f, it->glyph_row);
19844 /* Compute pixel dimensions of this line. */
19845 compute_line_metrics (it);
19847 /* Implementation note: No changes in the glyphs of ROW or in their
19848 faces can be done past this point, because compute_line_metrics
19849 computes ROW's hash value and stores it within the glyph_row
19850 structure. */
19852 /* Record whether this row ends inside an ellipsis. */
19853 row->ends_in_ellipsis_p
19854 = (it->method == GET_FROM_DISPLAY_VECTOR
19855 && it->ellipsis_p);
19857 /* Save fringe bitmaps in this row. */
19858 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19859 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19860 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19861 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19863 it->left_user_fringe_bitmap = 0;
19864 it->left_user_fringe_face_id = 0;
19865 it->right_user_fringe_bitmap = 0;
19866 it->right_user_fringe_face_id = 0;
19868 /* Maybe set the cursor. */
19869 cvpos = it->w->cursor.vpos;
19870 if ((cvpos < 0
19871 /* In bidi-reordered rows, keep checking for proper cursor
19872 position even if one has been found already, because buffer
19873 positions in such rows change non-linearly with ROW->VPOS,
19874 when a line is continued. One exception: when we are at ZV,
19875 display cursor on the first suitable glyph row, since all
19876 the empty rows after that also have their position set to ZV. */
19877 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19878 lines' rows is implemented for bidi-reordered rows. */
19879 || (it->bidi_p
19880 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19881 && PT >= MATRIX_ROW_START_CHARPOS (row)
19882 && PT <= MATRIX_ROW_END_CHARPOS (row)
19883 && cursor_row_p (row))
19884 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19886 /* Prepare for the next line. This line starts horizontally at (X
19887 HPOS) = (0 0). Vertical positions are incremented. As a
19888 convenience for the caller, IT->glyph_row is set to the next
19889 row to be used. */
19890 it->current_x = it->hpos = 0;
19891 it->current_y += row->height;
19892 SET_TEXT_POS (it->eol_pos, 0, 0);
19893 ++it->vpos;
19894 ++it->glyph_row;
19895 /* The next row should by default use the same value of the
19896 reversed_p flag as this one. set_iterator_to_next decides when
19897 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19898 the flag accordingly. */
19899 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19900 it->glyph_row->reversed_p = row->reversed_p;
19901 it->start = row->end;
19902 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19904 #undef RECORD_MAX_MIN_POS
19907 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19908 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19909 doc: /* Return paragraph direction at point in BUFFER.
19910 Value is either `left-to-right' or `right-to-left'.
19911 If BUFFER is omitted or nil, it defaults to the current buffer.
19913 Paragraph direction determines how the text in the paragraph is displayed.
19914 In left-to-right paragraphs, text begins at the left margin of the window
19915 and the reading direction is generally left to right. In right-to-left
19916 paragraphs, text begins at the right margin and is read from right to left.
19918 See also `bidi-paragraph-direction'. */)
19919 (Lisp_Object buffer)
19921 struct buffer *buf = current_buffer;
19922 struct buffer *old = buf;
19924 if (! NILP (buffer))
19926 CHECK_BUFFER (buffer);
19927 buf = XBUFFER (buffer);
19930 if (NILP (BVAR (buf, bidi_display_reordering))
19931 || NILP (BVAR (buf, enable_multibyte_characters))
19932 /* When we are loading loadup.el, the character property tables
19933 needed for bidi iteration are not yet available. */
19934 || !NILP (Vpurify_flag))
19935 return Qleft_to_right;
19936 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19937 return BVAR (buf, bidi_paragraph_direction);
19938 else
19940 /* Determine the direction from buffer text. We could try to
19941 use current_matrix if it is up to date, but this seems fast
19942 enough as it is. */
19943 struct bidi_it itb;
19944 ptrdiff_t pos = BUF_PT (buf);
19945 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19946 int c;
19947 void *itb_data = bidi_shelve_cache ();
19949 set_buffer_temp (buf);
19950 /* bidi_paragraph_init finds the base direction of the paragraph
19951 by searching forward from paragraph start. We need the base
19952 direction of the current or _previous_ paragraph, so we need
19953 to make sure we are within that paragraph. To that end, find
19954 the previous non-empty line. */
19955 if (pos >= ZV && pos > BEGV)
19956 DEC_BOTH (pos, bytepos);
19957 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19958 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19960 while ((c = FETCH_BYTE (bytepos)) == '\n'
19961 || c == ' ' || c == '\t' || c == '\f')
19963 if (bytepos <= BEGV_BYTE)
19964 break;
19965 bytepos--;
19966 pos--;
19968 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19969 bytepos--;
19971 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19972 itb.paragraph_dir = NEUTRAL_DIR;
19973 itb.string.s = NULL;
19974 itb.string.lstring = Qnil;
19975 itb.string.bufpos = 0;
19976 itb.string.unibyte = 0;
19977 /* We have no window to use here for ignoring window-specific
19978 overlays. Using NULL for window pointer will cause
19979 compute_display_string_pos to use the current buffer. */
19980 itb.w = NULL;
19981 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19982 bidi_unshelve_cache (itb_data, 0);
19983 set_buffer_temp (old);
19984 switch (itb.paragraph_dir)
19986 case L2R:
19987 return Qleft_to_right;
19988 break;
19989 case R2L:
19990 return Qright_to_left;
19991 break;
19992 default:
19993 emacs_abort ();
19998 DEFUN ("move-point-visually", Fmove_point_visually,
19999 Smove_point_visually, 1, 1, 0,
20000 doc: /* Move point in the visual order in the specified DIRECTION.
20001 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20002 left.
20004 Value is the new character position of point. */)
20005 (Lisp_Object direction)
20007 struct window *w = XWINDOW (selected_window);
20008 struct buffer *b = XBUFFER (w->contents);
20009 struct glyph_row *row;
20010 int dir;
20011 Lisp_Object paragraph_dir;
20013 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20014 (!(ROW)->continued_p \
20015 && INTEGERP ((GLYPH)->object) \
20016 && (GLYPH)->type == CHAR_GLYPH \
20017 && (GLYPH)->u.ch == ' ' \
20018 && (GLYPH)->charpos >= 0 \
20019 && !(GLYPH)->avoid_cursor_p)
20021 CHECK_NUMBER (direction);
20022 dir = XINT (direction);
20023 if (dir > 0)
20024 dir = 1;
20025 else
20026 dir = -1;
20028 /* If current matrix is up-to-date, we can use the information
20029 recorded in the glyphs, at least as long as the goal is on the
20030 screen. */
20031 if (w->window_end_valid
20032 && !windows_or_buffers_changed
20033 && b
20034 && !b->clip_changed
20035 && !b->prevent_redisplay_optimizations_p
20036 && !window_outdated (w)
20037 && w->cursor.vpos >= 0
20038 && w->cursor.vpos < w->current_matrix->nrows
20039 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20041 struct glyph *g = row->glyphs[TEXT_AREA];
20042 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20043 struct glyph *gpt = g + w->cursor.hpos;
20045 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20047 if (BUFFERP (g->object) && g->charpos != PT)
20049 SET_PT (g->charpos);
20050 w->cursor.vpos = -1;
20051 return make_number (PT);
20053 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20055 ptrdiff_t new_pos;
20057 if (BUFFERP (gpt->object))
20059 new_pos = PT;
20060 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20061 new_pos += (row->reversed_p ? -dir : dir);
20062 else
20063 new_pos -= (row->reversed_p ? -dir : dir);;
20065 else if (BUFFERP (g->object))
20066 new_pos = g->charpos;
20067 else
20068 break;
20069 SET_PT (new_pos);
20070 w->cursor.vpos = -1;
20071 return make_number (PT);
20073 else if (ROW_GLYPH_NEWLINE_P (row, g))
20075 /* Glyphs inserted at the end of a non-empty line for
20076 positioning the cursor have zero charpos, so we must
20077 deduce the value of point by other means. */
20078 if (g->charpos > 0)
20079 SET_PT (g->charpos);
20080 else if (row->ends_at_zv_p && PT != ZV)
20081 SET_PT (ZV);
20082 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20083 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20084 else
20085 break;
20086 w->cursor.vpos = -1;
20087 return make_number (PT);
20090 if (g == e || INTEGERP (g->object))
20092 if (row->truncated_on_left_p || row->truncated_on_right_p)
20093 goto simulate_display;
20094 if (!row->reversed_p)
20095 row += dir;
20096 else
20097 row -= dir;
20098 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20099 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20100 goto simulate_display;
20102 if (dir > 0)
20104 if (row->reversed_p && !row->continued_p)
20106 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20107 w->cursor.vpos = -1;
20108 return make_number (PT);
20110 g = row->glyphs[TEXT_AREA];
20111 e = g + row->used[TEXT_AREA];
20112 for ( ; g < e; g++)
20114 if (BUFFERP (g->object)
20115 /* Empty lines have only one glyph, which stands
20116 for the newline, and whose charpos is the
20117 buffer position of the newline. */
20118 || ROW_GLYPH_NEWLINE_P (row, g)
20119 /* When the buffer ends in a newline, the line at
20120 EOB also has one glyph, but its charpos is -1. */
20121 || (row->ends_at_zv_p
20122 && !row->reversed_p
20123 && INTEGERP (g->object)
20124 && g->type == CHAR_GLYPH
20125 && g->u.ch == ' '))
20127 if (g->charpos > 0)
20128 SET_PT (g->charpos);
20129 else if (!row->reversed_p
20130 && row->ends_at_zv_p
20131 && PT != ZV)
20132 SET_PT (ZV);
20133 else
20134 continue;
20135 w->cursor.vpos = -1;
20136 return make_number (PT);
20140 else
20142 if (!row->reversed_p && !row->continued_p)
20144 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20145 w->cursor.vpos = -1;
20146 return make_number (PT);
20148 e = row->glyphs[TEXT_AREA];
20149 g = e + row->used[TEXT_AREA] - 1;
20150 for ( ; g >= e; g--)
20152 if (BUFFERP (g->object)
20153 || (ROW_GLYPH_NEWLINE_P (row, g)
20154 && g->charpos > 0)
20155 /* Empty R2L lines on GUI frames have the buffer
20156 position of the newline stored in the stretch
20157 glyph. */
20158 || g->type == STRETCH_GLYPH
20159 || (row->ends_at_zv_p
20160 && row->reversed_p
20161 && INTEGERP (g->object)
20162 && g->type == CHAR_GLYPH
20163 && g->u.ch == ' '))
20165 if (g->charpos > 0)
20166 SET_PT (g->charpos);
20167 else if (row->reversed_p
20168 && row->ends_at_zv_p
20169 && PT != ZV)
20170 SET_PT (ZV);
20171 else
20172 continue;
20173 w->cursor.vpos = -1;
20174 return make_number (PT);
20181 simulate_display:
20183 /* If we wind up here, we failed to move by using the glyphs, so we
20184 need to simulate display instead. */
20186 if (b)
20187 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20188 else
20189 paragraph_dir = Qleft_to_right;
20190 if (EQ (paragraph_dir, Qright_to_left))
20191 dir = -dir;
20192 if (PT <= BEGV && dir < 0)
20193 xsignal0 (Qbeginning_of_buffer);
20194 else if (PT >= ZV && dir > 0)
20195 xsignal0 (Qend_of_buffer);
20196 else
20198 struct text_pos pt;
20199 struct it it;
20200 int pt_x, target_x, pixel_width, pt_vpos;
20201 bool at_eol_p;
20202 bool overshoot_expected = false;
20203 bool target_is_eol_p = false;
20205 /* Setup the arena. */
20206 SET_TEXT_POS (pt, PT, PT_BYTE);
20207 start_display (&it, w, pt);
20209 if (it.cmp_it.id < 0
20210 && it.method == GET_FROM_STRING
20211 && it.area == TEXT_AREA
20212 && it.string_from_display_prop_p
20213 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20214 overshoot_expected = true;
20216 /* Find the X coordinate of point. We start from the beginning
20217 of this or previous line to make sure we are before point in
20218 the logical order (since the move_it_* functions can only
20219 move forward). */
20220 reseat_at_previous_visible_line_start (&it);
20221 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20222 if (IT_CHARPOS (it) != PT)
20223 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20224 -1, -1, -1, MOVE_TO_POS);
20225 pt_x = it.current_x;
20226 pt_vpos = it.vpos;
20227 if (dir > 0 || overshoot_expected)
20229 struct glyph_row *row = it.glyph_row;
20231 /* When point is at beginning of line, we don't have
20232 information about the glyph there loaded into struct
20233 it. Calling get_next_display_element fixes that. */
20234 if (pt_x == 0)
20235 get_next_display_element (&it);
20236 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20237 it.glyph_row = NULL;
20238 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20239 it.glyph_row = row;
20240 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20241 it, lest it will become out of sync with it's buffer
20242 position. */
20243 it.current_x = pt_x;
20245 else
20246 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20247 pixel_width = it.pixel_width;
20248 if (overshoot_expected && at_eol_p)
20249 pixel_width = 0;
20250 else if (pixel_width <= 0)
20251 pixel_width = 1;
20253 /* If there's a display string at point, we are actually at the
20254 glyph to the left of point, so we need to correct the X
20255 coordinate. */
20256 if (overshoot_expected)
20257 pt_x += pixel_width;
20259 /* Compute target X coordinate, either to the left or to the
20260 right of point. On TTY frames, all characters have the same
20261 pixel width of 1, so we can use that. On GUI frames we don't
20262 have an easy way of getting at the pixel width of the
20263 character to the left of point, so we use a different method
20264 of getting to that place. */
20265 if (dir > 0)
20266 target_x = pt_x + pixel_width;
20267 else
20268 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20270 /* Target X coordinate could be one line above or below the line
20271 of point, in which case we need to adjust the target X
20272 coordinate. Also, if moving to the left, we need to begin at
20273 the left edge of the point's screen line. */
20274 if (dir < 0)
20276 if (pt_x > 0)
20278 start_display (&it, w, pt);
20279 reseat_at_previous_visible_line_start (&it);
20280 it.current_x = it.current_y = it.hpos = 0;
20281 if (pt_vpos != 0)
20282 move_it_by_lines (&it, pt_vpos);
20284 else
20286 move_it_by_lines (&it, -1);
20287 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20288 target_is_eol_p = true;
20291 else
20293 if (at_eol_p
20294 || (target_x >= it.last_visible_x
20295 && it.line_wrap != TRUNCATE))
20297 if (pt_x > 0)
20298 move_it_by_lines (&it, 0);
20299 move_it_by_lines (&it, 1);
20300 target_x = 0;
20304 /* Move to the target X coordinate. */
20305 #ifdef HAVE_WINDOW_SYSTEM
20306 /* On GUI frames, as we don't know the X coordinate of the
20307 character to the left of point, moving point to the left
20308 requires walking, one grapheme cluster at a time, until we
20309 find ourself at a place immediately to the left of the
20310 character at point. */
20311 if (FRAME_WINDOW_P (it.f) && dir < 0)
20313 struct text_pos new_pos = it.current.pos;
20314 enum move_it_result rc = MOVE_X_REACHED;
20316 while (it.current_x + it.pixel_width <= target_x
20317 && rc == MOVE_X_REACHED)
20319 int new_x = it.current_x + it.pixel_width;
20321 new_pos = it.current.pos;
20322 if (new_x == it.current_x)
20323 new_x++;
20324 rc = move_it_in_display_line_to (&it, ZV, new_x,
20325 MOVE_TO_POS | MOVE_TO_X);
20326 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20327 break;
20329 /* If we ended up on a composed character inside
20330 bidi-reordered text (e.g., Hebrew text with diacritics),
20331 the iterator gives us the buffer position of the last (in
20332 logical order) character of the composed grapheme cluster,
20333 which is not what we want. So we cheat: we compute the
20334 character position of the character that follows (in the
20335 logical order) the one where the above loop stopped. That
20336 character will appear on display to the left of point. */
20337 if (it.bidi_p
20338 && it.bidi_it.scan_dir == -1
20339 && new_pos.charpos - IT_CHARPOS (it) > 1)
20341 new_pos.charpos = IT_CHARPOS (it) + 1;
20342 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20344 it.current.pos = new_pos;
20346 else
20347 #endif
20348 if (it.current_x != target_x)
20349 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20351 /* When lines are truncated, the above loop will stop at the
20352 window edge. But we want to get to the end of line, even if
20353 it is beyond the window edge; automatic hscroll will then
20354 scroll the window to show point as appropriate. */
20355 if (target_is_eol_p && it.line_wrap == TRUNCATE
20356 && get_next_display_element (&it))
20358 struct text_pos new_pos = it.current.pos;
20360 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20362 set_iterator_to_next (&it, 0);
20363 if (it.method == GET_FROM_BUFFER)
20364 new_pos = it.current.pos;
20365 if (!get_next_display_element (&it))
20366 break;
20369 it.current.pos = new_pos;
20372 /* If we ended up in a display string that covers point, move to
20373 buffer position to the right in the visual order. */
20374 if (dir > 0)
20376 while (IT_CHARPOS (it) == PT)
20378 set_iterator_to_next (&it, 0);
20379 if (!get_next_display_element (&it))
20380 break;
20384 /* Move point to that position. */
20385 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20388 return make_number (PT);
20390 #undef ROW_GLYPH_NEWLINE_P
20394 /***********************************************************************
20395 Menu Bar
20396 ***********************************************************************/
20398 /* Redisplay the menu bar in the frame for window W.
20400 The menu bar of X frames that don't have X toolkit support is
20401 displayed in a special window W->frame->menu_bar_window.
20403 The menu bar of terminal frames is treated specially as far as
20404 glyph matrices are concerned. Menu bar lines are not part of
20405 windows, so the update is done directly on the frame matrix rows
20406 for the menu bar. */
20408 static void
20409 display_menu_bar (struct window *w)
20411 struct frame *f = XFRAME (WINDOW_FRAME (w));
20412 struct it it;
20413 Lisp_Object items;
20414 int i;
20416 /* Don't do all this for graphical frames. */
20417 #ifdef HAVE_NTGUI
20418 if (FRAME_W32_P (f))
20419 return;
20420 #endif
20421 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20422 if (FRAME_X_P (f))
20423 return;
20424 #endif
20426 #ifdef HAVE_NS
20427 if (FRAME_NS_P (f))
20428 return;
20429 #endif /* HAVE_NS */
20431 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20432 eassert (!FRAME_WINDOW_P (f));
20433 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20434 it.first_visible_x = 0;
20435 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20436 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20437 if (FRAME_WINDOW_P (f))
20439 /* Menu bar lines are displayed in the desired matrix of the
20440 dummy window menu_bar_window. */
20441 struct window *menu_w;
20442 menu_w = XWINDOW (f->menu_bar_window);
20443 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20444 MENU_FACE_ID);
20445 it.first_visible_x = 0;
20446 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20448 else
20449 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20451 /* This is a TTY frame, i.e. character hpos/vpos are used as
20452 pixel x/y. */
20453 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20454 MENU_FACE_ID);
20455 it.first_visible_x = 0;
20456 it.last_visible_x = FRAME_COLS (f);
20459 /* FIXME: This should be controlled by a user option. See the
20460 comments in redisplay_tool_bar and display_mode_line about
20461 this. */
20462 it.paragraph_embedding = L2R;
20464 /* Clear all rows of the menu bar. */
20465 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20467 struct glyph_row *row = it.glyph_row + i;
20468 clear_glyph_row (row);
20469 row->enabled_p = 1;
20470 row->full_width_p = 1;
20473 /* Display all items of the menu bar. */
20474 items = FRAME_MENU_BAR_ITEMS (it.f);
20475 for (i = 0; i < ASIZE (items); i += 4)
20477 Lisp_Object string;
20479 /* Stop at nil string. */
20480 string = AREF (items, i + 1);
20481 if (NILP (string))
20482 break;
20484 /* Remember where item was displayed. */
20485 ASET (items, i + 3, make_number (it.hpos));
20487 /* Display the item, pad with one space. */
20488 if (it.current_x < it.last_visible_x)
20489 display_string (NULL, string, Qnil, 0, 0, &it,
20490 SCHARS (string) + 1, 0, 0, -1);
20493 /* Fill out the line with spaces. */
20494 if (it.current_x < it.last_visible_x)
20495 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20497 /* Compute the total height of the lines. */
20498 compute_line_metrics (&it);
20501 #ifdef HAVE_MENUS
20502 /* Deep copy of a glyph row, including the glyphs. */
20503 static void
20504 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20506 struct glyph *pointers[1 + LAST_AREA];
20507 int to_used = to->used[TEXT_AREA];
20509 /* Save glyph pointers of TO. */
20510 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20512 /* Do a structure assignment. */
20513 *to = *from;
20515 /* Restore original glyph pointers of TO. */
20516 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20518 /* Copy the glyphs. */
20519 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20520 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20522 /* If we filled only part of the TO row, fill the rest with
20523 space_glyph (which will display as empty space). */
20524 if (to_used > from->used[TEXT_AREA])
20525 fill_up_frame_row_with_spaces (to, to_used);
20528 /* Display one menu item on a TTY, by overwriting the glyphs in the
20529 frame F's desired glyph matrix with glyphs produced from the menu
20530 item text. Called from term.c to display TTY drop-down menus one
20531 item at a time.
20533 ITEM_TEXT is the menu item text as a C string.
20535 FACE_ID is the face ID to be used for this menu item. FACE_ID
20536 could specify one of 3 faces: a face for an enabled item, a face
20537 for a disabled item, or a face for a selected item.
20539 X and Y are coordinates of the first glyph in the frame's desired
20540 matrix to be overwritten by the menu item. Since this is a TTY, Y
20541 is the zero-based number of the glyph row and X is the zero-based
20542 glyph number in the row, starting from left, where to start
20543 displaying the item.
20545 SUBMENU non-zero means this menu item drops down a submenu, which
20546 should be indicated by displaying a proper visual cue after the
20547 item text. */
20549 void
20550 display_tty_menu_item (const char *item_text, int width, int face_id,
20551 int x, int y, int submenu)
20553 struct it it;
20554 struct frame *f = SELECTED_FRAME ();
20555 struct window *w = XWINDOW (f->selected_window);
20556 int saved_used, saved_truncated, saved_width, saved_reversed;
20557 struct glyph_row *row;
20558 size_t item_len = strlen (item_text);
20560 eassert (FRAME_TERMCAP_P (f));
20562 /* Don't write beyond the matrix's last row. This can happen for
20563 TTY screens that are not high enough to show the entire menu.
20564 (This is actually a bit of defensive programming, as
20565 tty_menu_display already limits the number of menu items to one
20566 less than the number of screen lines.) */
20567 if (y >= f->desired_matrix->nrows)
20568 return;
20570 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20571 it.first_visible_x = 0;
20572 it.last_visible_x = FRAME_COLS (f) - 1;
20573 row = it.glyph_row;
20574 /* Start with the row contents from the current matrix. */
20575 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20576 saved_width = row->full_width_p;
20577 row->full_width_p = 1;
20578 saved_reversed = row->reversed_p;
20579 row->reversed_p = 0;
20580 row->enabled_p = 1;
20582 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20583 desired face. */
20584 eassert (x < f->desired_matrix->matrix_w);
20585 it.current_x = it.hpos = x;
20586 it.current_y = it.vpos = y;
20587 saved_used = row->used[TEXT_AREA];
20588 saved_truncated = row->truncated_on_right_p;
20589 row->used[TEXT_AREA] = x;
20590 it.face_id = face_id;
20591 it.line_wrap = TRUNCATE;
20593 /* FIXME: This should be controlled by a user option. See the
20594 comments in redisplay_tool_bar and display_mode_line about this.
20595 Also, if paragraph_embedding could ever be R2L, changes will be
20596 needed to avoid shifting to the right the row characters in
20597 term.c:append_glyph. */
20598 it.paragraph_embedding = L2R;
20600 /* Pad with a space on the left. */
20601 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20602 width--;
20603 /* Display the menu item, pad with spaces to WIDTH. */
20604 if (submenu)
20606 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20607 item_len, 0, FRAME_COLS (f) - 1, -1);
20608 width -= item_len;
20609 /* Indicate with " >" that there's a submenu. */
20610 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20611 FRAME_COLS (f) - 1, -1);
20613 else
20614 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20615 width, 0, FRAME_COLS (f) - 1, -1);
20617 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20618 row->truncated_on_right_p = saved_truncated;
20619 row->hash = row_hash (row);
20620 row->full_width_p = saved_width;
20621 row->reversed_p = saved_reversed;
20623 #endif /* HAVE_MENUS */
20625 /***********************************************************************
20626 Mode Line
20627 ***********************************************************************/
20629 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20630 FORCE is non-zero, redisplay mode lines unconditionally.
20631 Otherwise, redisplay only mode lines that are garbaged. Value is
20632 the number of windows whose mode lines were redisplayed. */
20634 static int
20635 redisplay_mode_lines (Lisp_Object window, int force)
20637 int nwindows = 0;
20639 while (!NILP (window))
20641 struct window *w = XWINDOW (window);
20643 if (WINDOWP (w->contents))
20644 nwindows += redisplay_mode_lines (w->contents, force);
20645 else if (force
20646 || FRAME_GARBAGED_P (XFRAME (w->frame))
20647 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20649 struct text_pos lpoint;
20650 struct buffer *old = current_buffer;
20652 /* Set the window's buffer for the mode line display. */
20653 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20654 set_buffer_internal_1 (XBUFFER (w->contents));
20656 /* Point refers normally to the selected window. For any
20657 other window, set up appropriate value. */
20658 if (!EQ (window, selected_window))
20660 struct text_pos pt;
20662 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20663 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20666 /* Display mode lines. */
20667 clear_glyph_matrix (w->desired_matrix);
20668 if (display_mode_lines (w))
20670 ++nwindows;
20671 w->must_be_updated_p = 1;
20674 /* Restore old settings. */
20675 set_buffer_internal_1 (old);
20676 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20679 window = w->next;
20682 return nwindows;
20686 /* Display the mode and/or header line of window W. Value is the
20687 sum number of mode lines and header lines displayed. */
20689 static int
20690 display_mode_lines (struct window *w)
20692 Lisp_Object old_selected_window = selected_window;
20693 Lisp_Object old_selected_frame = selected_frame;
20694 Lisp_Object new_frame = w->frame;
20695 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20696 int n = 0;
20698 selected_frame = new_frame;
20699 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20700 or window's point, then we'd need select_window_1 here as well. */
20701 XSETWINDOW (selected_window, w);
20702 XFRAME (new_frame)->selected_window = selected_window;
20704 /* These will be set while the mode line specs are processed. */
20705 line_number_displayed = 0;
20706 w->column_number_displayed = -1;
20708 if (WINDOW_WANTS_MODELINE_P (w))
20710 struct window *sel_w = XWINDOW (old_selected_window);
20712 /* Select mode line face based on the real selected window. */
20713 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20714 BVAR (current_buffer, mode_line_format));
20715 ++n;
20718 if (WINDOW_WANTS_HEADER_LINE_P (w))
20720 display_mode_line (w, HEADER_LINE_FACE_ID,
20721 BVAR (current_buffer, header_line_format));
20722 ++n;
20725 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20726 selected_frame = old_selected_frame;
20727 selected_window = old_selected_window;
20728 return n;
20732 /* Display mode or header line of window W. FACE_ID specifies which
20733 line to display; it is either MODE_LINE_FACE_ID or
20734 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20735 display. Value is the pixel height of the mode/header line
20736 displayed. */
20738 static int
20739 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20741 struct it it;
20742 struct face *face;
20743 ptrdiff_t count = SPECPDL_INDEX ();
20745 init_iterator (&it, w, -1, -1, NULL, face_id);
20746 /* Don't extend on a previously drawn mode-line.
20747 This may happen if called from pos_visible_p. */
20748 it.glyph_row->enabled_p = 0;
20749 prepare_desired_row (it.glyph_row);
20751 it.glyph_row->mode_line_p = 1;
20753 /* FIXME: This should be controlled by a user option. But
20754 supporting such an option is not trivial, since the mode line is
20755 made up of many separate strings. */
20756 it.paragraph_embedding = L2R;
20758 record_unwind_protect (unwind_format_mode_line,
20759 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20761 mode_line_target = MODE_LINE_DISPLAY;
20763 /* Temporarily make frame's keyboard the current kboard so that
20764 kboard-local variables in the mode_line_format will get the right
20765 values. */
20766 push_kboard (FRAME_KBOARD (it.f));
20767 record_unwind_save_match_data ();
20768 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20769 pop_kboard ();
20771 unbind_to (count, Qnil);
20773 /* Fill up with spaces. */
20774 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20776 compute_line_metrics (&it);
20777 it.glyph_row->full_width_p = 1;
20778 it.glyph_row->continued_p = 0;
20779 it.glyph_row->truncated_on_left_p = 0;
20780 it.glyph_row->truncated_on_right_p = 0;
20782 /* Make a 3D mode-line have a shadow at its right end. */
20783 face = FACE_FROM_ID (it.f, face_id);
20784 extend_face_to_end_of_line (&it);
20785 if (face->box != FACE_NO_BOX)
20787 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20788 + it.glyph_row->used[TEXT_AREA] - 1);
20789 last->right_box_line_p = 1;
20792 return it.glyph_row->height;
20795 /* Move element ELT in LIST to the front of LIST.
20796 Return the updated list. */
20798 static Lisp_Object
20799 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20801 register Lisp_Object tail, prev;
20802 register Lisp_Object tem;
20804 tail = list;
20805 prev = Qnil;
20806 while (CONSP (tail))
20808 tem = XCAR (tail);
20810 if (EQ (elt, tem))
20812 /* Splice out the link TAIL. */
20813 if (NILP (prev))
20814 list = XCDR (tail);
20815 else
20816 Fsetcdr (prev, XCDR (tail));
20818 /* Now make it the first. */
20819 Fsetcdr (tail, list);
20820 return tail;
20822 else
20823 prev = tail;
20824 tail = XCDR (tail);
20825 QUIT;
20828 /* Not found--return unchanged LIST. */
20829 return list;
20832 /* Contribute ELT to the mode line for window IT->w. How it
20833 translates into text depends on its data type.
20835 IT describes the display environment in which we display, as usual.
20837 DEPTH is the depth in recursion. It is used to prevent
20838 infinite recursion here.
20840 FIELD_WIDTH is the number of characters the display of ELT should
20841 occupy in the mode line, and PRECISION is the maximum number of
20842 characters to display from ELT's representation. See
20843 display_string for details.
20845 Returns the hpos of the end of the text generated by ELT.
20847 PROPS is a property list to add to any string we encounter.
20849 If RISKY is nonzero, remove (disregard) any properties in any string
20850 we encounter, and ignore :eval and :propertize.
20852 The global variable `mode_line_target' determines whether the
20853 output is passed to `store_mode_line_noprop',
20854 `store_mode_line_string', or `display_string'. */
20856 static int
20857 display_mode_element (struct it *it, int depth, int field_width, int precision,
20858 Lisp_Object elt, Lisp_Object props, int risky)
20860 int n = 0, field, prec;
20861 int literal = 0;
20863 tail_recurse:
20864 if (depth > 100)
20865 elt = build_string ("*too-deep*");
20867 depth++;
20869 switch (XTYPE (elt))
20871 case Lisp_String:
20873 /* A string: output it and check for %-constructs within it. */
20874 unsigned char c;
20875 ptrdiff_t offset = 0;
20877 if (SCHARS (elt) > 0
20878 && (!NILP (props) || risky))
20880 Lisp_Object oprops, aelt;
20881 oprops = Ftext_properties_at (make_number (0), elt);
20883 /* If the starting string's properties are not what
20884 we want, translate the string. Also, if the string
20885 is risky, do that anyway. */
20887 if (NILP (Fequal (props, oprops)) || risky)
20889 /* If the starting string has properties,
20890 merge the specified ones onto the existing ones. */
20891 if (! NILP (oprops) && !risky)
20893 Lisp_Object tem;
20895 oprops = Fcopy_sequence (oprops);
20896 tem = props;
20897 while (CONSP (tem))
20899 oprops = Fplist_put (oprops, XCAR (tem),
20900 XCAR (XCDR (tem)));
20901 tem = XCDR (XCDR (tem));
20903 props = oprops;
20906 aelt = Fassoc (elt, mode_line_proptrans_alist);
20907 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20909 /* AELT is what we want. Move it to the front
20910 without consing. */
20911 elt = XCAR (aelt);
20912 mode_line_proptrans_alist
20913 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20915 else
20917 Lisp_Object tem;
20919 /* If AELT has the wrong props, it is useless.
20920 so get rid of it. */
20921 if (! NILP (aelt))
20922 mode_line_proptrans_alist
20923 = Fdelq (aelt, mode_line_proptrans_alist);
20925 elt = Fcopy_sequence (elt);
20926 Fset_text_properties (make_number (0), Flength (elt),
20927 props, elt);
20928 /* Add this item to mode_line_proptrans_alist. */
20929 mode_line_proptrans_alist
20930 = Fcons (Fcons (elt, props),
20931 mode_line_proptrans_alist);
20932 /* Truncate mode_line_proptrans_alist
20933 to at most 50 elements. */
20934 tem = Fnthcdr (make_number (50),
20935 mode_line_proptrans_alist);
20936 if (! NILP (tem))
20937 XSETCDR (tem, Qnil);
20942 offset = 0;
20944 if (literal)
20946 prec = precision - n;
20947 switch (mode_line_target)
20949 case MODE_LINE_NOPROP:
20950 case MODE_LINE_TITLE:
20951 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20952 break;
20953 case MODE_LINE_STRING:
20954 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20955 break;
20956 case MODE_LINE_DISPLAY:
20957 n += display_string (NULL, elt, Qnil, 0, 0, it,
20958 0, prec, 0, STRING_MULTIBYTE (elt));
20959 break;
20962 break;
20965 /* Handle the non-literal case. */
20967 while ((precision <= 0 || n < precision)
20968 && SREF (elt, offset) != 0
20969 && (mode_line_target != MODE_LINE_DISPLAY
20970 || it->current_x < it->last_visible_x))
20972 ptrdiff_t last_offset = offset;
20974 /* Advance to end of string or next format specifier. */
20975 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20978 if (offset - 1 != last_offset)
20980 ptrdiff_t nchars, nbytes;
20982 /* Output to end of string or up to '%'. Field width
20983 is length of string. Don't output more than
20984 PRECISION allows us. */
20985 offset--;
20987 prec = c_string_width (SDATA (elt) + last_offset,
20988 offset - last_offset, precision - n,
20989 &nchars, &nbytes);
20991 switch (mode_line_target)
20993 case MODE_LINE_NOPROP:
20994 case MODE_LINE_TITLE:
20995 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20996 break;
20997 case MODE_LINE_STRING:
20999 ptrdiff_t bytepos = last_offset;
21000 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21001 ptrdiff_t endpos = (precision <= 0
21002 ? string_byte_to_char (elt, offset)
21003 : charpos + nchars);
21005 n += store_mode_line_string (NULL,
21006 Fsubstring (elt, make_number (charpos),
21007 make_number (endpos)),
21008 0, 0, 0, Qnil);
21010 break;
21011 case MODE_LINE_DISPLAY:
21013 ptrdiff_t bytepos = last_offset;
21014 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21016 if (precision <= 0)
21017 nchars = string_byte_to_char (elt, offset) - charpos;
21018 n += display_string (NULL, elt, Qnil, 0, charpos,
21019 it, 0, nchars, 0,
21020 STRING_MULTIBYTE (elt));
21022 break;
21025 else /* c == '%' */
21027 ptrdiff_t percent_position = offset;
21029 /* Get the specified minimum width. Zero means
21030 don't pad. */
21031 field = 0;
21032 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21033 field = field * 10 + c - '0';
21035 /* Don't pad beyond the total padding allowed. */
21036 if (field_width - n > 0 && field > field_width - n)
21037 field = field_width - n;
21039 /* Note that either PRECISION <= 0 or N < PRECISION. */
21040 prec = precision - n;
21042 if (c == 'M')
21043 n += display_mode_element (it, depth, field, prec,
21044 Vglobal_mode_string, props,
21045 risky);
21046 else if (c != 0)
21048 bool multibyte;
21049 ptrdiff_t bytepos, charpos;
21050 const char *spec;
21051 Lisp_Object string;
21053 bytepos = percent_position;
21054 charpos = (STRING_MULTIBYTE (elt)
21055 ? string_byte_to_char (elt, bytepos)
21056 : bytepos);
21057 spec = decode_mode_spec (it->w, c, field, &string);
21058 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21060 switch (mode_line_target)
21062 case MODE_LINE_NOPROP:
21063 case MODE_LINE_TITLE:
21064 n += store_mode_line_noprop (spec, field, prec);
21065 break;
21066 case MODE_LINE_STRING:
21068 Lisp_Object tem = build_string (spec);
21069 props = Ftext_properties_at (make_number (charpos), elt);
21070 /* Should only keep face property in props */
21071 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21073 break;
21074 case MODE_LINE_DISPLAY:
21076 int nglyphs_before, nwritten;
21078 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21079 nwritten = display_string (spec, string, elt,
21080 charpos, 0, it,
21081 field, prec, 0,
21082 multibyte);
21084 /* Assign to the glyphs written above the
21085 string where the `%x' came from, position
21086 of the `%'. */
21087 if (nwritten > 0)
21089 struct glyph *glyph
21090 = (it->glyph_row->glyphs[TEXT_AREA]
21091 + nglyphs_before);
21092 int i;
21094 for (i = 0; i < nwritten; ++i)
21096 glyph[i].object = elt;
21097 glyph[i].charpos = charpos;
21100 n += nwritten;
21103 break;
21106 else /* c == 0 */
21107 break;
21111 break;
21113 case Lisp_Symbol:
21114 /* A symbol: process the value of the symbol recursively
21115 as if it appeared here directly. Avoid error if symbol void.
21116 Special case: if value of symbol is a string, output the string
21117 literally. */
21119 register Lisp_Object tem;
21121 /* If the variable is not marked as risky to set
21122 then its contents are risky to use. */
21123 if (NILP (Fget (elt, Qrisky_local_variable)))
21124 risky = 1;
21126 tem = Fboundp (elt);
21127 if (!NILP (tem))
21129 tem = Fsymbol_value (elt);
21130 /* If value is a string, output that string literally:
21131 don't check for % within it. */
21132 if (STRINGP (tem))
21133 literal = 1;
21135 if (!EQ (tem, elt))
21137 /* Give up right away for nil or t. */
21138 elt = tem;
21139 goto tail_recurse;
21143 break;
21145 case Lisp_Cons:
21147 register Lisp_Object car, tem;
21149 /* A cons cell: five distinct cases.
21150 If first element is :eval or :propertize, do something special.
21151 If first element is a string or a cons, process all the elements
21152 and effectively concatenate them.
21153 If first element is a negative number, truncate displaying cdr to
21154 at most that many characters. If positive, pad (with spaces)
21155 to at least that many characters.
21156 If first element is a symbol, process the cadr or caddr recursively
21157 according to whether the symbol's value is non-nil or nil. */
21158 car = XCAR (elt);
21159 if (EQ (car, QCeval))
21161 /* An element of the form (:eval FORM) means evaluate FORM
21162 and use the result as mode line elements. */
21164 if (risky)
21165 break;
21167 if (CONSP (XCDR (elt)))
21169 Lisp_Object spec;
21170 spec = safe_eval (XCAR (XCDR (elt)));
21171 n += display_mode_element (it, depth, field_width - n,
21172 precision - n, spec, props,
21173 risky);
21176 else if (EQ (car, QCpropertize))
21178 /* An element of the form (:propertize ELT PROPS...)
21179 means display ELT but applying properties PROPS. */
21181 if (risky)
21182 break;
21184 if (CONSP (XCDR (elt)))
21185 n += display_mode_element (it, depth, field_width - n,
21186 precision - n, XCAR (XCDR (elt)),
21187 XCDR (XCDR (elt)), risky);
21189 else if (SYMBOLP (car))
21191 tem = Fboundp (car);
21192 elt = XCDR (elt);
21193 if (!CONSP (elt))
21194 goto invalid;
21195 /* elt is now the cdr, and we know it is a cons cell.
21196 Use its car if CAR has a non-nil value. */
21197 if (!NILP (tem))
21199 tem = Fsymbol_value (car);
21200 if (!NILP (tem))
21202 elt = XCAR (elt);
21203 goto tail_recurse;
21206 /* Symbol's value is nil (or symbol is unbound)
21207 Get the cddr of the original list
21208 and if possible find the caddr and use that. */
21209 elt = XCDR (elt);
21210 if (NILP (elt))
21211 break;
21212 else if (!CONSP (elt))
21213 goto invalid;
21214 elt = XCAR (elt);
21215 goto tail_recurse;
21217 else if (INTEGERP (car))
21219 register int lim = XINT (car);
21220 elt = XCDR (elt);
21221 if (lim < 0)
21223 /* Negative int means reduce maximum width. */
21224 if (precision <= 0)
21225 precision = -lim;
21226 else
21227 precision = min (precision, -lim);
21229 else if (lim > 0)
21231 /* Padding specified. Don't let it be more than
21232 current maximum. */
21233 if (precision > 0)
21234 lim = min (precision, lim);
21236 /* If that's more padding than already wanted, queue it.
21237 But don't reduce padding already specified even if
21238 that is beyond the current truncation point. */
21239 field_width = max (lim, field_width);
21241 goto tail_recurse;
21243 else if (STRINGP (car) || CONSP (car))
21245 Lisp_Object halftail = elt;
21246 int len = 0;
21248 while (CONSP (elt)
21249 && (precision <= 0 || n < precision))
21251 n += display_mode_element (it, depth,
21252 /* Do padding only after the last
21253 element in the list. */
21254 (! CONSP (XCDR (elt))
21255 ? field_width - n
21256 : 0),
21257 precision - n, XCAR (elt),
21258 props, risky);
21259 elt = XCDR (elt);
21260 len++;
21261 if ((len & 1) == 0)
21262 halftail = XCDR (halftail);
21263 /* Check for cycle. */
21264 if (EQ (halftail, elt))
21265 break;
21269 break;
21271 default:
21272 invalid:
21273 elt = build_string ("*invalid*");
21274 goto tail_recurse;
21277 /* Pad to FIELD_WIDTH. */
21278 if (field_width > 0 && n < field_width)
21280 switch (mode_line_target)
21282 case MODE_LINE_NOPROP:
21283 case MODE_LINE_TITLE:
21284 n += store_mode_line_noprop ("", field_width - n, 0);
21285 break;
21286 case MODE_LINE_STRING:
21287 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21288 break;
21289 case MODE_LINE_DISPLAY:
21290 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21291 0, 0, 0);
21292 break;
21296 return n;
21299 /* Store a mode-line string element in mode_line_string_list.
21301 If STRING is non-null, display that C string. Otherwise, the Lisp
21302 string LISP_STRING is displayed.
21304 FIELD_WIDTH is the minimum number of output glyphs to produce.
21305 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21306 with spaces. FIELD_WIDTH <= 0 means don't pad.
21308 PRECISION is the maximum number of characters to output from
21309 STRING. PRECISION <= 0 means don't truncate the string.
21311 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21312 properties to the string.
21314 PROPS are the properties to add to the string.
21315 The mode_line_string_face face property is always added to the string.
21318 static int
21319 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21320 int field_width, int precision, Lisp_Object props)
21322 ptrdiff_t len;
21323 int n = 0;
21325 if (string != NULL)
21327 len = strlen (string);
21328 if (precision > 0 && len > precision)
21329 len = precision;
21330 lisp_string = make_string (string, len);
21331 if (NILP (props))
21332 props = mode_line_string_face_prop;
21333 else if (!NILP (mode_line_string_face))
21335 Lisp_Object face = Fplist_get (props, Qface);
21336 props = Fcopy_sequence (props);
21337 if (NILP (face))
21338 face = mode_line_string_face;
21339 else
21340 face = list2 (face, mode_line_string_face);
21341 props = Fplist_put (props, Qface, face);
21343 Fadd_text_properties (make_number (0), make_number (len),
21344 props, lisp_string);
21346 else
21348 len = XFASTINT (Flength (lisp_string));
21349 if (precision > 0 && len > precision)
21351 len = precision;
21352 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21353 precision = -1;
21355 if (!NILP (mode_line_string_face))
21357 Lisp_Object face;
21358 if (NILP (props))
21359 props = Ftext_properties_at (make_number (0), lisp_string);
21360 face = Fplist_get (props, Qface);
21361 if (NILP (face))
21362 face = mode_line_string_face;
21363 else
21364 face = list2 (face, mode_line_string_face);
21365 props = list2 (Qface, face);
21366 if (copy_string)
21367 lisp_string = Fcopy_sequence (lisp_string);
21369 if (!NILP (props))
21370 Fadd_text_properties (make_number (0), make_number (len),
21371 props, lisp_string);
21374 if (len > 0)
21376 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21377 n += len;
21380 if (field_width > len)
21382 field_width -= len;
21383 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21384 if (!NILP (props))
21385 Fadd_text_properties (make_number (0), make_number (field_width),
21386 props, lisp_string);
21387 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21388 n += field_width;
21391 return n;
21395 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21396 1, 4, 0,
21397 doc: /* Format a string out of a mode line format specification.
21398 First arg FORMAT specifies the mode line format (see `mode-line-format'
21399 for details) to use.
21401 By default, the format is evaluated for the currently selected window.
21403 Optional second arg FACE specifies the face property to put on all
21404 characters for which no face is specified. The value nil means the
21405 default face. The value t means whatever face the window's mode line
21406 currently uses (either `mode-line' or `mode-line-inactive',
21407 depending on whether the window is the selected window or not).
21408 An integer value means the value string has no text
21409 properties.
21411 Optional third and fourth args WINDOW and BUFFER specify the window
21412 and buffer to use as the context for the formatting (defaults
21413 are the selected window and the WINDOW's buffer). */)
21414 (Lisp_Object format, Lisp_Object face,
21415 Lisp_Object window, Lisp_Object buffer)
21417 struct it it;
21418 int len;
21419 struct window *w;
21420 struct buffer *old_buffer = NULL;
21421 int face_id;
21422 int no_props = INTEGERP (face);
21423 ptrdiff_t count = SPECPDL_INDEX ();
21424 Lisp_Object str;
21425 int string_start = 0;
21427 w = decode_any_window (window);
21428 XSETWINDOW (window, w);
21430 if (NILP (buffer))
21431 buffer = w->contents;
21432 CHECK_BUFFER (buffer);
21434 /* Make formatting the modeline a non-op when noninteractive, otherwise
21435 there will be problems later caused by a partially initialized frame. */
21436 if (NILP (format) || noninteractive)
21437 return empty_unibyte_string;
21439 if (no_props)
21440 face = Qnil;
21442 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21443 : EQ (face, Qt) ? (EQ (window, selected_window)
21444 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21445 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21446 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21447 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21448 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21449 : DEFAULT_FACE_ID;
21451 old_buffer = current_buffer;
21453 /* Save things including mode_line_proptrans_alist,
21454 and set that to nil so that we don't alter the outer value. */
21455 record_unwind_protect (unwind_format_mode_line,
21456 format_mode_line_unwind_data
21457 (XFRAME (WINDOW_FRAME (w)),
21458 old_buffer, selected_window, 1));
21459 mode_line_proptrans_alist = Qnil;
21461 Fselect_window (window, Qt);
21462 set_buffer_internal_1 (XBUFFER (buffer));
21464 init_iterator (&it, w, -1, -1, NULL, face_id);
21466 if (no_props)
21468 mode_line_target = MODE_LINE_NOPROP;
21469 mode_line_string_face_prop = Qnil;
21470 mode_line_string_list = Qnil;
21471 string_start = MODE_LINE_NOPROP_LEN (0);
21473 else
21475 mode_line_target = MODE_LINE_STRING;
21476 mode_line_string_list = Qnil;
21477 mode_line_string_face = face;
21478 mode_line_string_face_prop
21479 = NILP (face) ? Qnil : list2 (Qface, face);
21482 push_kboard (FRAME_KBOARD (it.f));
21483 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21484 pop_kboard ();
21486 if (no_props)
21488 len = MODE_LINE_NOPROP_LEN (string_start);
21489 str = make_string (mode_line_noprop_buf + string_start, len);
21491 else
21493 mode_line_string_list = Fnreverse (mode_line_string_list);
21494 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21495 empty_unibyte_string);
21498 unbind_to (count, Qnil);
21499 return str;
21502 /* Write a null-terminated, right justified decimal representation of
21503 the positive integer D to BUF using a minimal field width WIDTH. */
21505 static void
21506 pint2str (register char *buf, register int width, register ptrdiff_t d)
21508 register char *p = buf;
21510 if (d <= 0)
21511 *p++ = '0';
21512 else
21514 while (d > 0)
21516 *p++ = d % 10 + '0';
21517 d /= 10;
21521 for (width -= (int) (p - buf); width > 0; --width)
21522 *p++ = ' ';
21523 *p-- = '\0';
21524 while (p > buf)
21526 d = *buf;
21527 *buf++ = *p;
21528 *p-- = d;
21532 /* Write a null-terminated, right justified decimal and "human
21533 readable" representation of the nonnegative integer D to BUF using
21534 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21536 static const char power_letter[] =
21538 0, /* no letter */
21539 'k', /* kilo */
21540 'M', /* mega */
21541 'G', /* giga */
21542 'T', /* tera */
21543 'P', /* peta */
21544 'E', /* exa */
21545 'Z', /* zetta */
21546 'Y' /* yotta */
21549 static void
21550 pint2hrstr (char *buf, int width, ptrdiff_t d)
21552 /* We aim to represent the nonnegative integer D as
21553 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21554 ptrdiff_t quotient = d;
21555 int remainder = 0;
21556 /* -1 means: do not use TENTHS. */
21557 int tenths = -1;
21558 int exponent = 0;
21560 /* Length of QUOTIENT.TENTHS as a string. */
21561 int length;
21563 char * psuffix;
21564 char * p;
21566 if (quotient >= 1000)
21568 /* Scale to the appropriate EXPONENT. */
21571 remainder = quotient % 1000;
21572 quotient /= 1000;
21573 exponent++;
21575 while (quotient >= 1000);
21577 /* Round to nearest and decide whether to use TENTHS or not. */
21578 if (quotient <= 9)
21580 tenths = remainder / 100;
21581 if (remainder % 100 >= 50)
21583 if (tenths < 9)
21584 tenths++;
21585 else
21587 quotient++;
21588 if (quotient == 10)
21589 tenths = -1;
21590 else
21591 tenths = 0;
21595 else
21596 if (remainder >= 500)
21598 if (quotient < 999)
21599 quotient++;
21600 else
21602 quotient = 1;
21603 exponent++;
21604 tenths = 0;
21609 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21610 if (tenths == -1 && quotient <= 99)
21611 if (quotient <= 9)
21612 length = 1;
21613 else
21614 length = 2;
21615 else
21616 length = 3;
21617 p = psuffix = buf + max (width, length);
21619 /* Print EXPONENT. */
21620 *psuffix++ = power_letter[exponent];
21621 *psuffix = '\0';
21623 /* Print TENTHS. */
21624 if (tenths >= 0)
21626 *--p = '0' + tenths;
21627 *--p = '.';
21630 /* Print QUOTIENT. */
21633 int digit = quotient % 10;
21634 *--p = '0' + digit;
21636 while ((quotient /= 10) != 0);
21638 /* Print leading spaces. */
21639 while (buf < p)
21640 *--p = ' ';
21643 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21644 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21645 type of CODING_SYSTEM. Return updated pointer into BUF. */
21647 static unsigned char invalid_eol_type[] = "(*invalid*)";
21649 static char *
21650 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21652 Lisp_Object val;
21653 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21654 const unsigned char *eol_str;
21655 int eol_str_len;
21656 /* The EOL conversion we are using. */
21657 Lisp_Object eoltype;
21659 val = CODING_SYSTEM_SPEC (coding_system);
21660 eoltype = Qnil;
21662 if (!VECTORP (val)) /* Not yet decided. */
21664 *buf++ = multibyte ? '-' : ' ';
21665 if (eol_flag)
21666 eoltype = eol_mnemonic_undecided;
21667 /* Don't mention EOL conversion if it isn't decided. */
21669 else
21671 Lisp_Object attrs;
21672 Lisp_Object eolvalue;
21674 attrs = AREF (val, 0);
21675 eolvalue = AREF (val, 2);
21677 *buf++ = multibyte
21678 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21679 : ' ';
21681 if (eol_flag)
21683 /* The EOL conversion that is normal on this system. */
21685 if (NILP (eolvalue)) /* Not yet decided. */
21686 eoltype = eol_mnemonic_undecided;
21687 else if (VECTORP (eolvalue)) /* Not yet decided. */
21688 eoltype = eol_mnemonic_undecided;
21689 else /* eolvalue is Qunix, Qdos, or Qmac. */
21690 eoltype = (EQ (eolvalue, Qunix)
21691 ? eol_mnemonic_unix
21692 : (EQ (eolvalue, Qdos) == 1
21693 ? eol_mnemonic_dos : eol_mnemonic_mac));
21697 if (eol_flag)
21699 /* Mention the EOL conversion if it is not the usual one. */
21700 if (STRINGP (eoltype))
21702 eol_str = SDATA (eoltype);
21703 eol_str_len = SBYTES (eoltype);
21705 else if (CHARACTERP (eoltype))
21707 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21708 int c = XFASTINT (eoltype);
21709 eol_str_len = CHAR_STRING (c, tmp);
21710 eol_str = tmp;
21712 else
21714 eol_str = invalid_eol_type;
21715 eol_str_len = sizeof (invalid_eol_type) - 1;
21717 memcpy (buf, eol_str, eol_str_len);
21718 buf += eol_str_len;
21721 return buf;
21724 /* Return a string for the output of a mode line %-spec for window W,
21725 generated by character C. FIELD_WIDTH > 0 means pad the string
21726 returned with spaces to that value. Return a Lisp string in
21727 *STRING if the resulting string is taken from that Lisp string.
21729 Note we operate on the current buffer for most purposes. */
21731 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21733 static const char *
21734 decode_mode_spec (struct window *w, register int c, int field_width,
21735 Lisp_Object *string)
21737 Lisp_Object obj;
21738 struct frame *f = XFRAME (WINDOW_FRAME (w));
21739 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21740 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21741 produce strings from numerical values, so limit preposterously
21742 large values of FIELD_WIDTH to avoid overrunning the buffer's
21743 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21744 bytes plus the terminating null. */
21745 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21746 struct buffer *b = current_buffer;
21748 obj = Qnil;
21749 *string = Qnil;
21751 switch (c)
21753 case '*':
21754 if (!NILP (BVAR (b, read_only)))
21755 return "%";
21756 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21757 return "*";
21758 return "-";
21760 case '+':
21761 /* This differs from %* only for a modified read-only buffer. */
21762 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21763 return "*";
21764 if (!NILP (BVAR (b, read_only)))
21765 return "%";
21766 return "-";
21768 case '&':
21769 /* This differs from %* in ignoring read-only-ness. */
21770 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21771 return "*";
21772 return "-";
21774 case '%':
21775 return "%";
21777 case '[':
21779 int i;
21780 char *p;
21782 if (command_loop_level > 5)
21783 return "[[[... ";
21784 p = decode_mode_spec_buf;
21785 for (i = 0; i < command_loop_level; i++)
21786 *p++ = '[';
21787 *p = 0;
21788 return decode_mode_spec_buf;
21791 case ']':
21793 int i;
21794 char *p;
21796 if (command_loop_level > 5)
21797 return " ...]]]";
21798 p = decode_mode_spec_buf;
21799 for (i = 0; i < command_loop_level; i++)
21800 *p++ = ']';
21801 *p = 0;
21802 return decode_mode_spec_buf;
21805 case '-':
21807 register int i;
21809 /* Let lots_of_dashes be a string of infinite length. */
21810 if (mode_line_target == MODE_LINE_NOPROP
21811 || mode_line_target == MODE_LINE_STRING)
21812 return "--";
21813 if (field_width <= 0
21814 || field_width > sizeof (lots_of_dashes))
21816 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21817 decode_mode_spec_buf[i] = '-';
21818 decode_mode_spec_buf[i] = '\0';
21819 return decode_mode_spec_buf;
21821 else
21822 return lots_of_dashes;
21825 case 'b':
21826 obj = BVAR (b, name);
21827 break;
21829 case 'c':
21830 /* %c and %l are ignored in `frame-title-format'.
21831 (In redisplay_internal, the frame title is drawn _before_ the
21832 windows are updated, so the stuff which depends on actual
21833 window contents (such as %l) may fail to render properly, or
21834 even crash emacs.) */
21835 if (mode_line_target == MODE_LINE_TITLE)
21836 return "";
21837 else
21839 ptrdiff_t col = current_column ();
21840 w->column_number_displayed = col;
21841 pint2str (decode_mode_spec_buf, width, col);
21842 return decode_mode_spec_buf;
21845 case 'e':
21846 #ifndef SYSTEM_MALLOC
21848 if (NILP (Vmemory_full))
21849 return "";
21850 else
21851 return "!MEM FULL! ";
21853 #else
21854 return "";
21855 #endif
21857 case 'F':
21858 /* %F displays the frame name. */
21859 if (!NILP (f->title))
21860 return SSDATA (f->title);
21861 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21862 return SSDATA (f->name);
21863 return "Emacs";
21865 case 'f':
21866 obj = BVAR (b, filename);
21867 break;
21869 case 'i':
21871 ptrdiff_t size = ZV - BEGV;
21872 pint2str (decode_mode_spec_buf, width, size);
21873 return decode_mode_spec_buf;
21876 case 'I':
21878 ptrdiff_t size = ZV - BEGV;
21879 pint2hrstr (decode_mode_spec_buf, width, size);
21880 return decode_mode_spec_buf;
21883 case 'l':
21885 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21886 ptrdiff_t topline, nlines, height;
21887 ptrdiff_t junk;
21889 /* %c and %l are ignored in `frame-title-format'. */
21890 if (mode_line_target == MODE_LINE_TITLE)
21891 return "";
21893 startpos = marker_position (w->start);
21894 startpos_byte = marker_byte_position (w->start);
21895 height = WINDOW_TOTAL_LINES (w);
21897 /* If we decided that this buffer isn't suitable for line numbers,
21898 don't forget that too fast. */
21899 if (w->base_line_pos == -1)
21900 goto no_value;
21902 /* If the buffer is very big, don't waste time. */
21903 if (INTEGERP (Vline_number_display_limit)
21904 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21906 w->base_line_pos = 0;
21907 w->base_line_number = 0;
21908 goto no_value;
21911 if (w->base_line_number > 0
21912 && w->base_line_pos > 0
21913 && w->base_line_pos <= startpos)
21915 line = w->base_line_number;
21916 linepos = w->base_line_pos;
21917 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21919 else
21921 line = 1;
21922 linepos = BUF_BEGV (b);
21923 linepos_byte = BUF_BEGV_BYTE (b);
21926 /* Count lines from base line to window start position. */
21927 nlines = display_count_lines (linepos_byte,
21928 startpos_byte,
21929 startpos, &junk);
21931 topline = nlines + line;
21933 /* Determine a new base line, if the old one is too close
21934 or too far away, or if we did not have one.
21935 "Too close" means it's plausible a scroll-down would
21936 go back past it. */
21937 if (startpos == BUF_BEGV (b))
21939 w->base_line_number = topline;
21940 w->base_line_pos = BUF_BEGV (b);
21942 else if (nlines < height + 25 || nlines > height * 3 + 50
21943 || linepos == BUF_BEGV (b))
21945 ptrdiff_t limit = BUF_BEGV (b);
21946 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21947 ptrdiff_t position;
21948 ptrdiff_t distance =
21949 (height * 2 + 30) * line_number_display_limit_width;
21951 if (startpos - distance > limit)
21953 limit = startpos - distance;
21954 limit_byte = CHAR_TO_BYTE (limit);
21957 nlines = display_count_lines (startpos_byte,
21958 limit_byte,
21959 - (height * 2 + 30),
21960 &position);
21961 /* If we couldn't find the lines we wanted within
21962 line_number_display_limit_width chars per line,
21963 give up on line numbers for this window. */
21964 if (position == limit_byte && limit == startpos - distance)
21966 w->base_line_pos = -1;
21967 w->base_line_number = 0;
21968 goto no_value;
21971 w->base_line_number = topline - nlines;
21972 w->base_line_pos = BYTE_TO_CHAR (position);
21975 /* Now count lines from the start pos to point. */
21976 nlines = display_count_lines (startpos_byte,
21977 PT_BYTE, PT, &junk);
21979 /* Record that we did display the line number. */
21980 line_number_displayed = 1;
21982 /* Make the string to show. */
21983 pint2str (decode_mode_spec_buf, width, topline + nlines);
21984 return decode_mode_spec_buf;
21985 no_value:
21987 char* p = decode_mode_spec_buf;
21988 int pad = width - 2;
21989 while (pad-- > 0)
21990 *p++ = ' ';
21991 *p++ = '?';
21992 *p++ = '?';
21993 *p = '\0';
21994 return decode_mode_spec_buf;
21997 break;
21999 case 'm':
22000 obj = BVAR (b, mode_name);
22001 break;
22003 case 'n':
22004 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22005 return " Narrow";
22006 break;
22008 case 'p':
22010 ptrdiff_t pos = marker_position (w->start);
22011 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22013 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22015 if (pos <= BUF_BEGV (b))
22016 return "All";
22017 else
22018 return "Bottom";
22020 else if (pos <= BUF_BEGV (b))
22021 return "Top";
22022 else
22024 if (total > 1000000)
22025 /* Do it differently for a large value, to avoid overflow. */
22026 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22027 else
22028 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22029 /* We can't normally display a 3-digit number,
22030 so get us a 2-digit number that is close. */
22031 if (total == 100)
22032 total = 99;
22033 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22034 return decode_mode_spec_buf;
22038 /* Display percentage of size above the bottom of the screen. */
22039 case 'P':
22041 ptrdiff_t toppos = marker_position (w->start);
22042 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22043 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22045 if (botpos >= BUF_ZV (b))
22047 if (toppos <= BUF_BEGV (b))
22048 return "All";
22049 else
22050 return "Bottom";
22052 else
22054 if (total > 1000000)
22055 /* Do it differently for a large value, to avoid overflow. */
22056 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22057 else
22058 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22059 /* We can't normally display a 3-digit number,
22060 so get us a 2-digit number that is close. */
22061 if (total == 100)
22062 total = 99;
22063 if (toppos <= BUF_BEGV (b))
22064 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22065 else
22066 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22067 return decode_mode_spec_buf;
22071 case 's':
22072 /* status of process */
22073 obj = Fget_buffer_process (Fcurrent_buffer ());
22074 if (NILP (obj))
22075 return "no process";
22076 #ifndef MSDOS
22077 obj = Fsymbol_name (Fprocess_status (obj));
22078 #endif
22079 break;
22081 case '@':
22083 ptrdiff_t count = inhibit_garbage_collection ();
22084 Lisp_Object val = call1 (intern ("file-remote-p"),
22085 BVAR (current_buffer, directory));
22086 unbind_to (count, Qnil);
22088 if (NILP (val))
22089 return "-";
22090 else
22091 return "@";
22094 case 'z':
22095 /* coding-system (not including end-of-line format) */
22096 case 'Z':
22097 /* coding-system (including end-of-line type) */
22099 int eol_flag = (c == 'Z');
22100 char *p = decode_mode_spec_buf;
22102 if (! FRAME_WINDOW_P (f))
22104 /* No need to mention EOL here--the terminal never needs
22105 to do EOL conversion. */
22106 p = decode_mode_spec_coding (CODING_ID_NAME
22107 (FRAME_KEYBOARD_CODING (f)->id),
22108 p, 0);
22109 p = decode_mode_spec_coding (CODING_ID_NAME
22110 (FRAME_TERMINAL_CODING (f)->id),
22111 p, 0);
22113 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22114 p, eol_flag);
22116 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22117 #ifdef subprocesses
22118 obj = Fget_buffer_process (Fcurrent_buffer ());
22119 if (PROCESSP (obj))
22121 p = decode_mode_spec_coding
22122 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22123 p = decode_mode_spec_coding
22124 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22126 #endif /* subprocesses */
22127 #endif /* 0 */
22128 *p = 0;
22129 return decode_mode_spec_buf;
22133 if (STRINGP (obj))
22135 *string = obj;
22136 return SSDATA (obj);
22138 else
22139 return "";
22143 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22144 means count lines back from START_BYTE. But don't go beyond
22145 LIMIT_BYTE. Return the number of lines thus found (always
22146 nonnegative).
22148 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22149 either the position COUNT lines after/before START_BYTE, if we
22150 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22151 COUNT lines. */
22153 static ptrdiff_t
22154 display_count_lines (ptrdiff_t start_byte,
22155 ptrdiff_t limit_byte, ptrdiff_t count,
22156 ptrdiff_t *byte_pos_ptr)
22158 register unsigned char *cursor;
22159 unsigned char *base;
22161 register ptrdiff_t ceiling;
22162 register unsigned char *ceiling_addr;
22163 ptrdiff_t orig_count = count;
22165 /* If we are not in selective display mode,
22166 check only for newlines. */
22167 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22168 && !INTEGERP (BVAR (current_buffer, selective_display)));
22170 if (count > 0)
22172 while (start_byte < limit_byte)
22174 ceiling = BUFFER_CEILING_OF (start_byte);
22175 ceiling = min (limit_byte - 1, ceiling);
22176 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22177 base = (cursor = BYTE_POS_ADDR (start_byte));
22181 if (selective_display)
22183 while (*cursor != '\n' && *cursor != 015
22184 && ++cursor != ceiling_addr)
22185 continue;
22186 if (cursor == ceiling_addr)
22187 break;
22189 else
22191 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22192 if (! cursor)
22193 break;
22196 cursor++;
22198 if (--count == 0)
22200 start_byte += cursor - base;
22201 *byte_pos_ptr = start_byte;
22202 return orig_count;
22205 while (cursor < ceiling_addr);
22207 start_byte += ceiling_addr - base;
22210 else
22212 while (start_byte > limit_byte)
22214 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22215 ceiling = max (limit_byte, ceiling);
22216 ceiling_addr = BYTE_POS_ADDR (ceiling);
22217 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22218 while (1)
22220 if (selective_display)
22222 while (--cursor >= ceiling_addr
22223 && *cursor != '\n' && *cursor != 015)
22224 continue;
22225 if (cursor < ceiling_addr)
22226 break;
22228 else
22230 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22231 if (! cursor)
22232 break;
22235 if (++count == 0)
22237 start_byte += cursor - base + 1;
22238 *byte_pos_ptr = start_byte;
22239 /* When scanning backwards, we should
22240 not count the newline posterior to which we stop. */
22241 return - orig_count - 1;
22244 start_byte += ceiling_addr - base;
22248 *byte_pos_ptr = limit_byte;
22250 if (count < 0)
22251 return - orig_count + count;
22252 return orig_count - count;
22258 /***********************************************************************
22259 Displaying strings
22260 ***********************************************************************/
22262 /* Display a NUL-terminated string, starting with index START.
22264 If STRING is non-null, display that C string. Otherwise, the Lisp
22265 string LISP_STRING is displayed. There's a case that STRING is
22266 non-null and LISP_STRING is not nil. It means STRING is a string
22267 data of LISP_STRING. In that case, we display LISP_STRING while
22268 ignoring its text properties.
22270 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22271 FACE_STRING. Display STRING or LISP_STRING with the face at
22272 FACE_STRING_POS in FACE_STRING:
22274 Display the string in the environment given by IT, but use the
22275 standard display table, temporarily.
22277 FIELD_WIDTH is the minimum number of output glyphs to produce.
22278 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22279 with spaces. If STRING has more characters, more than FIELD_WIDTH
22280 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22282 PRECISION is the maximum number of characters to output from
22283 STRING. PRECISION < 0 means don't truncate the string.
22285 This is roughly equivalent to printf format specifiers:
22287 FIELD_WIDTH PRECISION PRINTF
22288 ----------------------------------------
22289 -1 -1 %s
22290 -1 10 %.10s
22291 10 -1 %10s
22292 20 10 %20.10s
22294 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22295 display them, and < 0 means obey the current buffer's value of
22296 enable_multibyte_characters.
22298 Value is the number of columns displayed. */
22300 static int
22301 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22302 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22303 int field_width, int precision, int max_x, int multibyte)
22305 int hpos_at_start = it->hpos;
22306 int saved_face_id = it->face_id;
22307 struct glyph_row *row = it->glyph_row;
22308 ptrdiff_t it_charpos;
22310 /* Initialize the iterator IT for iteration over STRING beginning
22311 with index START. */
22312 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22313 precision, field_width, multibyte);
22314 if (string && STRINGP (lisp_string))
22315 /* LISP_STRING is the one returned by decode_mode_spec. We should
22316 ignore its text properties. */
22317 it->stop_charpos = it->end_charpos;
22319 /* If displaying STRING, set up the face of the iterator from
22320 FACE_STRING, if that's given. */
22321 if (STRINGP (face_string))
22323 ptrdiff_t endptr;
22324 struct face *face;
22326 it->face_id
22327 = face_at_string_position (it->w, face_string, face_string_pos,
22328 0, &endptr, it->base_face_id, 0);
22329 face = FACE_FROM_ID (it->f, it->face_id);
22330 it->face_box_p = face->box != FACE_NO_BOX;
22333 /* Set max_x to the maximum allowed X position. Don't let it go
22334 beyond the right edge of the window. */
22335 if (max_x <= 0)
22336 max_x = it->last_visible_x;
22337 else
22338 max_x = min (max_x, it->last_visible_x);
22340 /* Skip over display elements that are not visible. because IT->w is
22341 hscrolled. */
22342 if (it->current_x < it->first_visible_x)
22343 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22344 MOVE_TO_POS | MOVE_TO_X);
22346 row->ascent = it->max_ascent;
22347 row->height = it->max_ascent + it->max_descent;
22348 row->phys_ascent = it->max_phys_ascent;
22349 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22350 row->extra_line_spacing = it->max_extra_line_spacing;
22352 if (STRINGP (it->string))
22353 it_charpos = IT_STRING_CHARPOS (*it);
22354 else
22355 it_charpos = IT_CHARPOS (*it);
22357 /* This condition is for the case that we are called with current_x
22358 past last_visible_x. */
22359 while (it->current_x < max_x)
22361 int x_before, x, n_glyphs_before, i, nglyphs;
22363 /* Get the next display element. */
22364 if (!get_next_display_element (it))
22365 break;
22367 /* Produce glyphs. */
22368 x_before = it->current_x;
22369 n_glyphs_before = row->used[TEXT_AREA];
22370 PRODUCE_GLYPHS (it);
22372 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22373 i = 0;
22374 x = x_before;
22375 while (i < nglyphs)
22377 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22379 if (it->line_wrap != TRUNCATE
22380 && x + glyph->pixel_width > max_x)
22382 /* End of continued line or max_x reached. */
22383 if (CHAR_GLYPH_PADDING_P (*glyph))
22385 /* A wide character is unbreakable. */
22386 if (row->reversed_p)
22387 unproduce_glyphs (it, row->used[TEXT_AREA]
22388 - n_glyphs_before);
22389 row->used[TEXT_AREA] = n_glyphs_before;
22390 it->current_x = x_before;
22392 else
22394 if (row->reversed_p)
22395 unproduce_glyphs (it, row->used[TEXT_AREA]
22396 - (n_glyphs_before + i));
22397 row->used[TEXT_AREA] = n_glyphs_before + i;
22398 it->current_x = x;
22400 break;
22402 else if (x + glyph->pixel_width >= it->first_visible_x)
22404 /* Glyph is at least partially visible. */
22405 ++it->hpos;
22406 if (x < it->first_visible_x)
22407 row->x = x - it->first_visible_x;
22409 else
22411 /* Glyph is off the left margin of the display area.
22412 Should not happen. */
22413 emacs_abort ();
22416 row->ascent = max (row->ascent, it->max_ascent);
22417 row->height = max (row->height, it->max_ascent + it->max_descent);
22418 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22419 row->phys_height = max (row->phys_height,
22420 it->max_phys_ascent + it->max_phys_descent);
22421 row->extra_line_spacing = max (row->extra_line_spacing,
22422 it->max_extra_line_spacing);
22423 x += glyph->pixel_width;
22424 ++i;
22427 /* Stop if max_x reached. */
22428 if (i < nglyphs)
22429 break;
22431 /* Stop at line ends. */
22432 if (ITERATOR_AT_END_OF_LINE_P (it))
22434 it->continuation_lines_width = 0;
22435 break;
22438 set_iterator_to_next (it, 1);
22439 if (STRINGP (it->string))
22440 it_charpos = IT_STRING_CHARPOS (*it);
22441 else
22442 it_charpos = IT_CHARPOS (*it);
22444 /* Stop if truncating at the right edge. */
22445 if (it->line_wrap == TRUNCATE
22446 && it->current_x >= it->last_visible_x)
22448 /* Add truncation mark, but don't do it if the line is
22449 truncated at a padding space. */
22450 if (it_charpos < it->string_nchars)
22452 if (!FRAME_WINDOW_P (it->f))
22454 int ii, n;
22456 if (it->current_x > it->last_visible_x)
22458 if (!row->reversed_p)
22460 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22461 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22462 break;
22464 else
22466 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22467 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22468 break;
22469 unproduce_glyphs (it, ii + 1);
22470 ii = row->used[TEXT_AREA] - (ii + 1);
22472 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22474 row->used[TEXT_AREA] = ii;
22475 produce_special_glyphs (it, IT_TRUNCATION);
22478 produce_special_glyphs (it, IT_TRUNCATION);
22480 row->truncated_on_right_p = 1;
22482 break;
22486 /* Maybe insert a truncation at the left. */
22487 if (it->first_visible_x
22488 && it_charpos > 0)
22490 if (!FRAME_WINDOW_P (it->f)
22491 || (row->reversed_p
22492 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22493 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22494 insert_left_trunc_glyphs (it);
22495 row->truncated_on_left_p = 1;
22498 it->face_id = saved_face_id;
22500 /* Value is number of columns displayed. */
22501 return it->hpos - hpos_at_start;
22506 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22507 appears as an element of LIST or as the car of an element of LIST.
22508 If PROPVAL is a list, compare each element against LIST in that
22509 way, and return 1/2 if any element of PROPVAL is found in LIST.
22510 Otherwise return 0. This function cannot quit.
22511 The return value is 2 if the text is invisible but with an ellipsis
22512 and 1 if it's invisible and without an ellipsis. */
22515 invisible_p (register Lisp_Object propval, Lisp_Object list)
22517 register Lisp_Object tail, proptail;
22519 for (tail = list; CONSP (tail); tail = XCDR (tail))
22521 register Lisp_Object tem;
22522 tem = XCAR (tail);
22523 if (EQ (propval, tem))
22524 return 1;
22525 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22526 return NILP (XCDR (tem)) ? 1 : 2;
22529 if (CONSP (propval))
22531 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22533 Lisp_Object propelt;
22534 propelt = XCAR (proptail);
22535 for (tail = list; CONSP (tail); tail = XCDR (tail))
22537 register Lisp_Object tem;
22538 tem = XCAR (tail);
22539 if (EQ (propelt, tem))
22540 return 1;
22541 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22542 return NILP (XCDR (tem)) ? 1 : 2;
22547 return 0;
22550 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22551 doc: /* Non-nil if the property makes the text invisible.
22552 POS-OR-PROP can be a marker or number, in which case it is taken to be
22553 a position in the current buffer and the value of the `invisible' property
22554 is checked; or it can be some other value, which is then presumed to be the
22555 value of the `invisible' property of the text of interest.
22556 The non-nil value returned can be t for truly invisible text or something
22557 else if the text is replaced by an ellipsis. */)
22558 (Lisp_Object pos_or_prop)
22560 Lisp_Object prop
22561 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22562 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22563 : pos_or_prop);
22564 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22565 return (invis == 0 ? Qnil
22566 : invis == 1 ? Qt
22567 : make_number (invis));
22570 /* Calculate a width or height in pixels from a specification using
22571 the following elements:
22573 SPEC ::=
22574 NUM - a (fractional) multiple of the default font width/height
22575 (NUM) - specifies exactly NUM pixels
22576 UNIT - a fixed number of pixels, see below.
22577 ELEMENT - size of a display element in pixels, see below.
22578 (NUM . SPEC) - equals NUM * SPEC
22579 (+ SPEC SPEC ...) - add pixel values
22580 (- SPEC SPEC ...) - subtract pixel values
22581 (- SPEC) - negate pixel value
22583 NUM ::=
22584 INT or FLOAT - a number constant
22585 SYMBOL - use symbol's (buffer local) variable binding.
22587 UNIT ::=
22588 in - pixels per inch *)
22589 mm - pixels per 1/1000 meter *)
22590 cm - pixels per 1/100 meter *)
22591 width - width of current font in pixels.
22592 height - height of current font in pixels.
22594 *) using the ratio(s) defined in display-pixels-per-inch.
22596 ELEMENT ::=
22598 left-fringe - left fringe width in pixels
22599 right-fringe - right fringe width in pixels
22601 left-margin - left margin width in pixels
22602 right-margin - right margin width in pixels
22604 scroll-bar - scroll-bar area width in pixels
22606 Examples:
22608 Pixels corresponding to 5 inches:
22609 (5 . in)
22611 Total width of non-text areas on left side of window (if scroll-bar is on left):
22612 '(space :width (+ left-fringe left-margin scroll-bar))
22614 Align to first text column (in header line):
22615 '(space :align-to 0)
22617 Align to middle of text area minus half the width of variable `my-image'
22618 containing a loaded image:
22619 '(space :align-to (0.5 . (- text my-image)))
22621 Width of left margin minus width of 1 character in the default font:
22622 '(space :width (- left-margin 1))
22624 Width of left margin minus width of 2 characters in the current font:
22625 '(space :width (- left-margin (2 . width)))
22627 Center 1 character over left-margin (in header line):
22628 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22630 Different ways to express width of left fringe plus left margin minus one pixel:
22631 '(space :width (- (+ left-fringe left-margin) (1)))
22632 '(space :width (+ left-fringe left-margin (- (1))))
22633 '(space :width (+ left-fringe left-margin (-1)))
22637 static int
22638 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22639 struct font *font, int width_p, int *align_to)
22641 double pixels;
22643 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22644 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22646 if (NILP (prop))
22647 return OK_PIXELS (0);
22649 eassert (FRAME_LIVE_P (it->f));
22651 if (SYMBOLP (prop))
22653 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22655 char *unit = SSDATA (SYMBOL_NAME (prop));
22657 if (unit[0] == 'i' && unit[1] == 'n')
22658 pixels = 1.0;
22659 else if (unit[0] == 'm' && unit[1] == 'm')
22660 pixels = 25.4;
22661 else if (unit[0] == 'c' && unit[1] == 'm')
22662 pixels = 2.54;
22663 else
22664 pixels = 0;
22665 if (pixels > 0)
22667 double ppi = (width_p ? FRAME_RES_X (it->f)
22668 : FRAME_RES_Y (it->f));
22670 if (ppi > 0)
22671 return OK_PIXELS (ppi / pixels);
22672 return 0;
22676 #ifdef HAVE_WINDOW_SYSTEM
22677 if (EQ (prop, Qheight))
22678 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22679 if (EQ (prop, Qwidth))
22680 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22681 #else
22682 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22683 return OK_PIXELS (1);
22684 #endif
22686 if (EQ (prop, Qtext))
22687 return OK_PIXELS (width_p
22688 ? window_box_width (it->w, TEXT_AREA)
22689 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22691 if (align_to && *align_to < 0)
22693 *res = 0;
22694 if (EQ (prop, Qleft))
22695 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22696 if (EQ (prop, Qright))
22697 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22698 if (EQ (prop, Qcenter))
22699 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22700 + window_box_width (it->w, TEXT_AREA) / 2);
22701 if (EQ (prop, Qleft_fringe))
22702 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22703 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22704 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22705 if (EQ (prop, Qright_fringe))
22706 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22707 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22708 : window_box_right_offset (it->w, TEXT_AREA));
22709 if (EQ (prop, Qleft_margin))
22710 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22711 if (EQ (prop, Qright_margin))
22712 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22713 if (EQ (prop, Qscroll_bar))
22714 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22716 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22717 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22718 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22719 : 0)));
22721 else
22723 if (EQ (prop, Qleft_fringe))
22724 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22725 if (EQ (prop, Qright_fringe))
22726 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22727 if (EQ (prop, Qleft_margin))
22728 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22729 if (EQ (prop, Qright_margin))
22730 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22731 if (EQ (prop, Qscroll_bar))
22732 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22735 prop = buffer_local_value_1 (prop, it->w->contents);
22736 if (EQ (prop, Qunbound))
22737 prop = Qnil;
22740 if (INTEGERP (prop) || FLOATP (prop))
22742 int base_unit = (width_p
22743 ? FRAME_COLUMN_WIDTH (it->f)
22744 : FRAME_LINE_HEIGHT (it->f));
22745 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22748 if (CONSP (prop))
22750 Lisp_Object car = XCAR (prop);
22751 Lisp_Object cdr = XCDR (prop);
22753 if (SYMBOLP (car))
22755 #ifdef HAVE_WINDOW_SYSTEM
22756 if (FRAME_WINDOW_P (it->f)
22757 && valid_image_p (prop))
22759 ptrdiff_t id = lookup_image (it->f, prop);
22760 struct image *img = IMAGE_FROM_ID (it->f, id);
22762 return OK_PIXELS (width_p ? img->width : img->height);
22764 #endif
22765 if (EQ (car, Qplus) || EQ (car, Qminus))
22767 int first = 1;
22768 double px;
22770 pixels = 0;
22771 while (CONSP (cdr))
22773 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22774 font, width_p, align_to))
22775 return 0;
22776 if (first)
22777 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22778 else
22779 pixels += px;
22780 cdr = XCDR (cdr);
22782 if (EQ (car, Qminus))
22783 pixels = -pixels;
22784 return OK_PIXELS (pixels);
22787 car = buffer_local_value_1 (car, it->w->contents);
22788 if (EQ (car, Qunbound))
22789 car = Qnil;
22792 if (INTEGERP (car) || FLOATP (car))
22794 double fact;
22795 pixels = XFLOATINT (car);
22796 if (NILP (cdr))
22797 return OK_PIXELS (pixels);
22798 if (calc_pixel_width_or_height (&fact, it, cdr,
22799 font, width_p, align_to))
22800 return OK_PIXELS (pixels * fact);
22801 return 0;
22804 return 0;
22807 return 0;
22811 /***********************************************************************
22812 Glyph Display
22813 ***********************************************************************/
22815 #ifdef HAVE_WINDOW_SYSTEM
22817 #ifdef GLYPH_DEBUG
22819 void
22820 dump_glyph_string (struct glyph_string *s)
22822 fprintf (stderr, "glyph string\n");
22823 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22824 s->x, s->y, s->width, s->height);
22825 fprintf (stderr, " ybase = %d\n", s->ybase);
22826 fprintf (stderr, " hl = %d\n", s->hl);
22827 fprintf (stderr, " left overhang = %d, right = %d\n",
22828 s->left_overhang, s->right_overhang);
22829 fprintf (stderr, " nchars = %d\n", s->nchars);
22830 fprintf (stderr, " extends to end of line = %d\n",
22831 s->extends_to_end_of_line_p);
22832 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22833 fprintf (stderr, " bg width = %d\n", s->background_width);
22836 #endif /* GLYPH_DEBUG */
22838 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22839 of XChar2b structures for S; it can't be allocated in
22840 init_glyph_string because it must be allocated via `alloca'. W
22841 is the window on which S is drawn. ROW and AREA are the glyph row
22842 and area within the row from which S is constructed. START is the
22843 index of the first glyph structure covered by S. HL is a
22844 face-override for drawing S. */
22846 #ifdef HAVE_NTGUI
22847 #define OPTIONAL_HDC(hdc) HDC hdc,
22848 #define DECLARE_HDC(hdc) HDC hdc;
22849 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22850 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22851 #endif
22853 #ifndef OPTIONAL_HDC
22854 #define OPTIONAL_HDC(hdc)
22855 #define DECLARE_HDC(hdc)
22856 #define ALLOCATE_HDC(hdc, f)
22857 #define RELEASE_HDC(hdc, f)
22858 #endif
22860 static void
22861 init_glyph_string (struct glyph_string *s,
22862 OPTIONAL_HDC (hdc)
22863 XChar2b *char2b, struct window *w, struct glyph_row *row,
22864 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22866 memset (s, 0, sizeof *s);
22867 s->w = w;
22868 s->f = XFRAME (w->frame);
22869 #ifdef HAVE_NTGUI
22870 s->hdc = hdc;
22871 #endif
22872 s->display = FRAME_X_DISPLAY (s->f);
22873 s->window = FRAME_X_WINDOW (s->f);
22874 s->char2b = char2b;
22875 s->hl = hl;
22876 s->row = row;
22877 s->area = area;
22878 s->first_glyph = row->glyphs[area] + start;
22879 s->height = row->height;
22880 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22881 s->ybase = s->y + row->ascent;
22885 /* Append the list of glyph strings with head H and tail T to the list
22886 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22888 static void
22889 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22890 struct glyph_string *h, struct glyph_string *t)
22892 if (h)
22894 if (*head)
22895 (*tail)->next = h;
22896 else
22897 *head = h;
22898 h->prev = *tail;
22899 *tail = t;
22904 /* Prepend the list of glyph strings with head H and tail T to the
22905 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22906 result. */
22908 static void
22909 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22910 struct glyph_string *h, struct glyph_string *t)
22912 if (h)
22914 if (*head)
22915 (*head)->prev = t;
22916 else
22917 *tail = t;
22918 t->next = *head;
22919 *head = h;
22924 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22925 Set *HEAD and *TAIL to the resulting list. */
22927 static void
22928 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22929 struct glyph_string *s)
22931 s->next = s->prev = NULL;
22932 append_glyph_string_lists (head, tail, s, s);
22936 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22937 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22938 make sure that X resources for the face returned are allocated.
22939 Value is a pointer to a realized face that is ready for display if
22940 DISPLAY_P is non-zero. */
22942 static struct face *
22943 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22944 XChar2b *char2b, int display_p)
22946 struct face *face = FACE_FROM_ID (f, face_id);
22947 unsigned code = 0;
22949 if (face->font)
22951 code = face->font->driver->encode_char (face->font, c);
22953 if (code == FONT_INVALID_CODE)
22954 code = 0;
22956 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22958 /* Make sure X resources of the face are allocated. */
22959 #ifdef HAVE_X_WINDOWS
22960 if (display_p)
22961 #endif
22963 eassert (face != NULL);
22964 PREPARE_FACE_FOR_DISPLAY (f, face);
22967 return face;
22971 /* Get face and two-byte form of character glyph GLYPH on frame F.
22972 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22973 a pointer to a realized face that is ready for display. */
22975 static struct face *
22976 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22977 XChar2b *char2b, int *two_byte_p)
22979 struct face *face;
22980 unsigned code = 0;
22982 eassert (glyph->type == CHAR_GLYPH);
22983 face = FACE_FROM_ID (f, glyph->face_id);
22985 /* Make sure X resources of the face are allocated. */
22986 eassert (face != NULL);
22987 PREPARE_FACE_FOR_DISPLAY (f, face);
22989 if (two_byte_p)
22990 *two_byte_p = 0;
22992 if (face->font)
22994 if (CHAR_BYTE8_P (glyph->u.ch))
22995 code = CHAR_TO_BYTE8 (glyph->u.ch);
22996 else
22997 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22999 if (code == FONT_INVALID_CODE)
23000 code = 0;
23003 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23004 return face;
23008 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23009 Return 1 if FONT has a glyph for C, otherwise return 0. */
23011 static int
23012 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23014 unsigned code;
23016 if (CHAR_BYTE8_P (c))
23017 code = CHAR_TO_BYTE8 (c);
23018 else
23019 code = font->driver->encode_char (font, c);
23021 if (code == FONT_INVALID_CODE)
23022 return 0;
23023 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23024 return 1;
23028 /* Fill glyph string S with composition components specified by S->cmp.
23030 BASE_FACE is the base face of the composition.
23031 S->cmp_from is the index of the first component for S.
23033 OVERLAPS non-zero means S should draw the foreground only, and use
23034 its physical height for clipping. See also draw_glyphs.
23036 Value is the index of a component not in S. */
23038 static int
23039 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23040 int overlaps)
23042 int i;
23043 /* For all glyphs of this composition, starting at the offset
23044 S->cmp_from, until we reach the end of the definition or encounter a
23045 glyph that requires the different face, add it to S. */
23046 struct face *face;
23048 eassert (s);
23050 s->for_overlaps = overlaps;
23051 s->face = NULL;
23052 s->font = NULL;
23053 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23055 int c = COMPOSITION_GLYPH (s->cmp, i);
23057 /* TAB in a composition means display glyphs with padding space
23058 on the left or right. */
23059 if (c != '\t')
23061 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23062 -1, Qnil);
23064 face = get_char_face_and_encoding (s->f, c, face_id,
23065 s->char2b + i, 1);
23066 if (face)
23068 if (! s->face)
23070 s->face = face;
23071 s->font = s->face->font;
23073 else if (s->face != face)
23074 break;
23077 ++s->nchars;
23079 s->cmp_to = i;
23081 if (s->face == NULL)
23083 s->face = base_face->ascii_face;
23084 s->font = s->face->font;
23087 /* All glyph strings for the same composition has the same width,
23088 i.e. the width set for the first component of the composition. */
23089 s->width = s->first_glyph->pixel_width;
23091 /* If the specified font could not be loaded, use the frame's
23092 default font, but record the fact that we couldn't load it in
23093 the glyph string so that we can draw rectangles for the
23094 characters of the glyph string. */
23095 if (s->font == NULL)
23097 s->font_not_found_p = 1;
23098 s->font = FRAME_FONT (s->f);
23101 /* Adjust base line for subscript/superscript text. */
23102 s->ybase += s->first_glyph->voffset;
23104 /* This glyph string must always be drawn with 16-bit functions. */
23105 s->two_byte_p = 1;
23107 return s->cmp_to;
23110 static int
23111 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23112 int start, int end, int overlaps)
23114 struct glyph *glyph, *last;
23115 Lisp_Object lgstring;
23116 int i;
23118 s->for_overlaps = overlaps;
23119 glyph = s->row->glyphs[s->area] + start;
23120 last = s->row->glyphs[s->area] + end;
23121 s->cmp_id = glyph->u.cmp.id;
23122 s->cmp_from = glyph->slice.cmp.from;
23123 s->cmp_to = glyph->slice.cmp.to + 1;
23124 s->face = FACE_FROM_ID (s->f, face_id);
23125 lgstring = composition_gstring_from_id (s->cmp_id);
23126 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23127 glyph++;
23128 while (glyph < last
23129 && glyph->u.cmp.automatic
23130 && glyph->u.cmp.id == s->cmp_id
23131 && s->cmp_to == glyph->slice.cmp.from)
23132 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23134 for (i = s->cmp_from; i < s->cmp_to; i++)
23136 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23137 unsigned code = LGLYPH_CODE (lglyph);
23139 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23141 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23142 return glyph - s->row->glyphs[s->area];
23146 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23147 See the comment of fill_glyph_string for arguments.
23148 Value is the index of the first glyph not in S. */
23151 static int
23152 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23153 int start, int end, int overlaps)
23155 struct glyph *glyph, *last;
23156 int voffset;
23158 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23159 s->for_overlaps = overlaps;
23160 glyph = s->row->glyphs[s->area] + start;
23161 last = s->row->glyphs[s->area] + end;
23162 voffset = glyph->voffset;
23163 s->face = FACE_FROM_ID (s->f, face_id);
23164 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23165 s->nchars = 1;
23166 s->width = glyph->pixel_width;
23167 glyph++;
23168 while (glyph < last
23169 && glyph->type == GLYPHLESS_GLYPH
23170 && glyph->voffset == voffset
23171 && glyph->face_id == face_id)
23173 s->nchars++;
23174 s->width += glyph->pixel_width;
23175 glyph++;
23177 s->ybase += voffset;
23178 return glyph - s->row->glyphs[s->area];
23182 /* Fill glyph string S from a sequence of character glyphs.
23184 FACE_ID is the face id of the string. START is the index of the
23185 first glyph to consider, END is the index of the last + 1.
23186 OVERLAPS non-zero means S should draw the foreground only, and use
23187 its physical height for clipping. See also draw_glyphs.
23189 Value is the index of the first glyph not in S. */
23191 static int
23192 fill_glyph_string (struct glyph_string *s, int face_id,
23193 int start, int end, int overlaps)
23195 struct glyph *glyph, *last;
23196 int voffset;
23197 int glyph_not_available_p;
23199 eassert (s->f == XFRAME (s->w->frame));
23200 eassert (s->nchars == 0);
23201 eassert (start >= 0 && end > start);
23203 s->for_overlaps = overlaps;
23204 glyph = s->row->glyphs[s->area] + start;
23205 last = s->row->glyphs[s->area] + end;
23206 voffset = glyph->voffset;
23207 s->padding_p = glyph->padding_p;
23208 glyph_not_available_p = glyph->glyph_not_available_p;
23210 while (glyph < last
23211 && glyph->type == CHAR_GLYPH
23212 && glyph->voffset == voffset
23213 /* Same face id implies same font, nowadays. */
23214 && glyph->face_id == face_id
23215 && glyph->glyph_not_available_p == glyph_not_available_p)
23217 int two_byte_p;
23219 s->face = get_glyph_face_and_encoding (s->f, glyph,
23220 s->char2b + s->nchars,
23221 &two_byte_p);
23222 s->two_byte_p = two_byte_p;
23223 ++s->nchars;
23224 eassert (s->nchars <= end - start);
23225 s->width += glyph->pixel_width;
23226 if (glyph++->padding_p != s->padding_p)
23227 break;
23230 s->font = s->face->font;
23232 /* If the specified font could not be loaded, use the frame's font,
23233 but record the fact that we couldn't load it in
23234 S->font_not_found_p so that we can draw rectangles for the
23235 characters of the glyph string. */
23236 if (s->font == NULL || glyph_not_available_p)
23238 s->font_not_found_p = 1;
23239 s->font = FRAME_FONT (s->f);
23242 /* Adjust base line for subscript/superscript text. */
23243 s->ybase += voffset;
23245 eassert (s->face && s->face->gc);
23246 return glyph - s->row->glyphs[s->area];
23250 /* Fill glyph string S from image glyph S->first_glyph. */
23252 static void
23253 fill_image_glyph_string (struct glyph_string *s)
23255 eassert (s->first_glyph->type == IMAGE_GLYPH);
23256 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23257 eassert (s->img);
23258 s->slice = s->first_glyph->slice.img;
23259 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23260 s->font = s->face->font;
23261 s->width = s->first_glyph->pixel_width;
23263 /* Adjust base line for subscript/superscript text. */
23264 s->ybase += s->first_glyph->voffset;
23268 /* Fill glyph string S from a sequence of stretch glyphs.
23270 START is the index of the first glyph to consider,
23271 END is the index of the last + 1.
23273 Value is the index of the first glyph not in S. */
23275 static int
23276 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23278 struct glyph *glyph, *last;
23279 int voffset, face_id;
23281 eassert (s->first_glyph->type == STRETCH_GLYPH);
23283 glyph = s->row->glyphs[s->area] + start;
23284 last = s->row->glyphs[s->area] + end;
23285 face_id = glyph->face_id;
23286 s->face = FACE_FROM_ID (s->f, face_id);
23287 s->font = s->face->font;
23288 s->width = glyph->pixel_width;
23289 s->nchars = 1;
23290 voffset = glyph->voffset;
23292 for (++glyph;
23293 (glyph < last
23294 && glyph->type == STRETCH_GLYPH
23295 && glyph->voffset == voffset
23296 && glyph->face_id == face_id);
23297 ++glyph)
23298 s->width += glyph->pixel_width;
23300 /* Adjust base line for subscript/superscript text. */
23301 s->ybase += voffset;
23303 /* The case that face->gc == 0 is handled when drawing the glyph
23304 string by calling PREPARE_FACE_FOR_DISPLAY. */
23305 eassert (s->face);
23306 return glyph - s->row->glyphs[s->area];
23309 static struct font_metrics *
23310 get_per_char_metric (struct font *font, XChar2b *char2b)
23312 static struct font_metrics metrics;
23313 unsigned code;
23315 if (! font)
23316 return NULL;
23317 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23318 if (code == FONT_INVALID_CODE)
23319 return NULL;
23320 font->driver->text_extents (font, &code, 1, &metrics);
23321 return &metrics;
23324 /* EXPORT for RIF:
23325 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23326 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23327 assumed to be zero. */
23329 void
23330 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23332 *left = *right = 0;
23334 if (glyph->type == CHAR_GLYPH)
23336 struct face *face;
23337 XChar2b char2b;
23338 struct font_metrics *pcm;
23340 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23341 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23343 if (pcm->rbearing > pcm->width)
23344 *right = pcm->rbearing - pcm->width;
23345 if (pcm->lbearing < 0)
23346 *left = -pcm->lbearing;
23349 else if (glyph->type == COMPOSITE_GLYPH)
23351 if (! glyph->u.cmp.automatic)
23353 struct composition *cmp = composition_table[glyph->u.cmp.id];
23355 if (cmp->rbearing > cmp->pixel_width)
23356 *right = cmp->rbearing - cmp->pixel_width;
23357 if (cmp->lbearing < 0)
23358 *left = - cmp->lbearing;
23360 else
23362 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23363 struct font_metrics metrics;
23365 composition_gstring_width (gstring, glyph->slice.cmp.from,
23366 glyph->slice.cmp.to + 1, &metrics);
23367 if (metrics.rbearing > metrics.width)
23368 *right = metrics.rbearing - metrics.width;
23369 if (metrics.lbearing < 0)
23370 *left = - metrics.lbearing;
23376 /* Return the index of the first glyph preceding glyph string S that
23377 is overwritten by S because of S's left overhang. Value is -1
23378 if no glyphs are overwritten. */
23380 static int
23381 left_overwritten (struct glyph_string *s)
23383 int k;
23385 if (s->left_overhang)
23387 int x = 0, i;
23388 struct glyph *glyphs = s->row->glyphs[s->area];
23389 int first = s->first_glyph - glyphs;
23391 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23392 x -= glyphs[i].pixel_width;
23394 k = i + 1;
23396 else
23397 k = -1;
23399 return k;
23403 /* Return the index of the first glyph preceding glyph string S that
23404 is overwriting S because of its right overhang. Value is -1 if no
23405 glyph in front of S overwrites S. */
23407 static int
23408 left_overwriting (struct glyph_string *s)
23410 int i, k, x;
23411 struct glyph *glyphs = s->row->glyphs[s->area];
23412 int first = s->first_glyph - glyphs;
23414 k = -1;
23415 x = 0;
23416 for (i = first - 1; i >= 0; --i)
23418 int left, right;
23419 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23420 if (x + right > 0)
23421 k = i;
23422 x -= glyphs[i].pixel_width;
23425 return k;
23429 /* Return the index of the last glyph following glyph string S that is
23430 overwritten by S because of S's right overhang. Value is -1 if
23431 no such glyph is found. */
23433 static int
23434 right_overwritten (struct glyph_string *s)
23436 int k = -1;
23438 if (s->right_overhang)
23440 int x = 0, i;
23441 struct glyph *glyphs = s->row->glyphs[s->area];
23442 int first = (s->first_glyph - glyphs
23443 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23444 int end = s->row->used[s->area];
23446 for (i = first; i < end && s->right_overhang > x; ++i)
23447 x += glyphs[i].pixel_width;
23449 k = i;
23452 return k;
23456 /* Return the index of the last glyph following glyph string S that
23457 overwrites S because of its left overhang. Value is negative
23458 if no such glyph is found. */
23460 static int
23461 right_overwriting (struct glyph_string *s)
23463 int i, k, x;
23464 int end = s->row->used[s->area];
23465 struct glyph *glyphs = s->row->glyphs[s->area];
23466 int first = (s->first_glyph - glyphs
23467 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23469 k = -1;
23470 x = 0;
23471 for (i = first; i < end; ++i)
23473 int left, right;
23474 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23475 if (x - left < 0)
23476 k = i;
23477 x += glyphs[i].pixel_width;
23480 return k;
23484 /* Set background width of glyph string S. START is the index of the
23485 first glyph following S. LAST_X is the right-most x-position + 1
23486 in the drawing area. */
23488 static void
23489 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23491 /* If the face of this glyph string has to be drawn to the end of
23492 the drawing area, set S->extends_to_end_of_line_p. */
23494 if (start == s->row->used[s->area]
23495 && s->area == TEXT_AREA
23496 && ((s->row->fill_line_p
23497 && (s->hl == DRAW_NORMAL_TEXT
23498 || s->hl == DRAW_IMAGE_RAISED
23499 || s->hl == DRAW_IMAGE_SUNKEN))
23500 || s->hl == DRAW_MOUSE_FACE))
23501 s->extends_to_end_of_line_p = 1;
23503 /* If S extends its face to the end of the line, set its
23504 background_width to the distance to the right edge of the drawing
23505 area. */
23506 if (s->extends_to_end_of_line_p)
23507 s->background_width = last_x - s->x + 1;
23508 else
23509 s->background_width = s->width;
23513 /* Compute overhangs and x-positions for glyph string S and its
23514 predecessors, or successors. X is the starting x-position for S.
23515 BACKWARD_P non-zero means process predecessors. */
23517 static void
23518 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23520 if (backward_p)
23522 while (s)
23524 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23525 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23526 x -= s->width;
23527 s->x = x;
23528 s = s->prev;
23531 else
23533 while (s)
23535 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23536 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23537 s->x = x;
23538 x += s->width;
23539 s = s->next;
23546 /* The following macros are only called from draw_glyphs below.
23547 They reference the following parameters of that function directly:
23548 `w', `row', `area', and `overlap_p'
23549 as well as the following local variables:
23550 `s', `f', and `hdc' (in W32) */
23552 #ifdef HAVE_NTGUI
23553 /* On W32, silently add local `hdc' variable to argument list of
23554 init_glyph_string. */
23555 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23556 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23557 #else
23558 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23559 init_glyph_string (s, char2b, w, row, area, start, hl)
23560 #endif
23562 /* Add a glyph string for a stretch glyph to the list of strings
23563 between HEAD and TAIL. START is the index of the stretch glyph in
23564 row area AREA of glyph row ROW. END is the index of the last glyph
23565 in that glyph row area. X is the current output position assigned
23566 to the new glyph string constructed. HL overrides that face of the
23567 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23568 is the right-most x-position of the drawing area. */
23570 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23571 and below -- keep them on one line. */
23572 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23573 do \
23575 s = alloca (sizeof *s); \
23576 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23577 START = fill_stretch_glyph_string (s, START, END); \
23578 append_glyph_string (&HEAD, &TAIL, s); \
23579 s->x = (X); \
23581 while (0)
23584 /* Add a glyph string for an image glyph to the list of strings
23585 between HEAD and TAIL. START is the index of the image glyph in
23586 row area AREA of glyph row ROW. END is the index of the last glyph
23587 in that glyph row area. X is the current output position assigned
23588 to the new glyph string constructed. HL overrides that face of the
23589 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23590 is the right-most x-position of the drawing area. */
23592 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23593 do \
23595 s = alloca (sizeof *s); \
23596 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23597 fill_image_glyph_string (s); \
23598 append_glyph_string (&HEAD, &TAIL, s); \
23599 ++START; \
23600 s->x = (X); \
23602 while (0)
23605 /* Add a glyph string for a sequence of character glyphs to the list
23606 of strings between HEAD and TAIL. START is the index of the first
23607 glyph in row area AREA of glyph row ROW that is part of the new
23608 glyph string. END is the index of the last glyph in that glyph row
23609 area. X is the current output position assigned to the new glyph
23610 string constructed. HL overrides that face of the glyph; e.g. it
23611 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23612 right-most x-position of the drawing area. */
23614 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23615 do \
23617 int face_id; \
23618 XChar2b *char2b; \
23620 face_id = (row)->glyphs[area][START].face_id; \
23622 s = alloca (sizeof *s); \
23623 char2b = alloca ((END - START) * sizeof *char2b); \
23624 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23625 append_glyph_string (&HEAD, &TAIL, s); \
23626 s->x = (X); \
23627 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23629 while (0)
23632 /* Add a glyph string for a composite sequence to the list of strings
23633 between HEAD and TAIL. START is the index of the first glyph in
23634 row area AREA of glyph row ROW that is part of the new glyph
23635 string. END is the index of the last glyph in that glyph row area.
23636 X is the current output position assigned to the new glyph string
23637 constructed. HL overrides that face of the glyph; e.g. it is
23638 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23639 x-position of the drawing area. */
23641 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23642 do { \
23643 int face_id = (row)->glyphs[area][START].face_id; \
23644 struct face *base_face = FACE_FROM_ID (f, face_id); \
23645 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23646 struct composition *cmp = composition_table[cmp_id]; \
23647 XChar2b *char2b; \
23648 struct glyph_string *first_s = NULL; \
23649 int n; \
23651 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23653 /* Make glyph_strings for each glyph sequence that is drawable by \
23654 the same face, and append them to HEAD/TAIL. */ \
23655 for (n = 0; n < cmp->glyph_len;) \
23657 s = alloca (sizeof *s); \
23658 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23659 append_glyph_string (&(HEAD), &(TAIL), s); \
23660 s->cmp = cmp; \
23661 s->cmp_from = n; \
23662 s->x = (X); \
23663 if (n == 0) \
23664 first_s = s; \
23665 n = fill_composite_glyph_string (s, base_face, overlaps); \
23668 ++START; \
23669 s = first_s; \
23670 } while (0)
23673 /* Add a glyph string for a glyph-string sequence to the list of strings
23674 between HEAD and TAIL. */
23676 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23677 do { \
23678 int face_id; \
23679 XChar2b *char2b; \
23680 Lisp_Object gstring; \
23682 face_id = (row)->glyphs[area][START].face_id; \
23683 gstring = (composition_gstring_from_id \
23684 ((row)->glyphs[area][START].u.cmp.id)); \
23685 s = alloca (sizeof *s); \
23686 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23687 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23688 append_glyph_string (&(HEAD), &(TAIL), s); \
23689 s->x = (X); \
23690 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23691 } while (0)
23694 /* Add a glyph string for a sequence of glyphless character's glyphs
23695 to the list of strings between HEAD and TAIL. The meanings of
23696 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23698 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23699 do \
23701 int face_id; \
23703 face_id = (row)->glyphs[area][START].face_id; \
23705 s = alloca (sizeof *s); \
23706 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23707 append_glyph_string (&HEAD, &TAIL, s); \
23708 s->x = (X); \
23709 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23710 overlaps); \
23712 while (0)
23715 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23716 of AREA of glyph row ROW on window W between indices START and END.
23717 HL overrides the face for drawing glyph strings, e.g. it is
23718 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23719 x-positions of the drawing area.
23721 This is an ugly monster macro construct because we must use alloca
23722 to allocate glyph strings (because draw_glyphs can be called
23723 asynchronously). */
23725 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23726 do \
23728 HEAD = TAIL = NULL; \
23729 while (START < END) \
23731 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23732 switch (first_glyph->type) \
23734 case CHAR_GLYPH: \
23735 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23736 HL, X, LAST_X); \
23737 break; \
23739 case COMPOSITE_GLYPH: \
23740 if (first_glyph->u.cmp.automatic) \
23741 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23742 HL, X, LAST_X); \
23743 else \
23744 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23745 HL, X, LAST_X); \
23746 break; \
23748 case STRETCH_GLYPH: \
23749 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23750 HL, X, LAST_X); \
23751 break; \
23753 case IMAGE_GLYPH: \
23754 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23755 HL, X, LAST_X); \
23756 break; \
23758 case GLYPHLESS_GLYPH: \
23759 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23760 HL, X, LAST_X); \
23761 break; \
23763 default: \
23764 emacs_abort (); \
23767 if (s) \
23769 set_glyph_string_background_width (s, START, LAST_X); \
23770 (X) += s->width; \
23773 } while (0)
23776 /* Draw glyphs between START and END in AREA of ROW on window W,
23777 starting at x-position X. X is relative to AREA in W. HL is a
23778 face-override with the following meaning:
23780 DRAW_NORMAL_TEXT draw normally
23781 DRAW_CURSOR draw in cursor face
23782 DRAW_MOUSE_FACE draw in mouse face.
23783 DRAW_INVERSE_VIDEO draw in mode line face
23784 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23785 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23787 If OVERLAPS is non-zero, draw only the foreground of characters and
23788 clip to the physical height of ROW. Non-zero value also defines
23789 the overlapping part to be drawn:
23791 OVERLAPS_PRED overlap with preceding rows
23792 OVERLAPS_SUCC overlap with succeeding rows
23793 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23794 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23796 Value is the x-position reached, relative to AREA of W. */
23798 static int
23799 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23800 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23801 enum draw_glyphs_face hl, int overlaps)
23803 struct glyph_string *head, *tail;
23804 struct glyph_string *s;
23805 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23806 int i, j, x_reached, last_x, area_left = 0;
23807 struct frame *f = XFRAME (WINDOW_FRAME (w));
23808 DECLARE_HDC (hdc);
23810 ALLOCATE_HDC (hdc, f);
23812 /* Let's rather be paranoid than getting a SEGV. */
23813 end = min (end, row->used[area]);
23814 start = clip_to_bounds (0, start, end);
23816 /* Translate X to frame coordinates. Set last_x to the right
23817 end of the drawing area. */
23818 if (row->full_width_p)
23820 /* X is relative to the left edge of W, without scroll bars
23821 or fringes. */
23822 area_left = WINDOW_LEFT_EDGE_X (w);
23823 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23825 else
23827 area_left = window_box_left (w, area);
23828 last_x = area_left + window_box_width (w, area);
23830 x += area_left;
23832 /* Build a doubly-linked list of glyph_string structures between
23833 head and tail from what we have to draw. Note that the macro
23834 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23835 the reason we use a separate variable `i'. */
23836 i = start;
23837 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23838 if (tail)
23839 x_reached = tail->x + tail->background_width;
23840 else
23841 x_reached = x;
23843 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23844 the row, redraw some glyphs in front or following the glyph
23845 strings built above. */
23846 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23848 struct glyph_string *h, *t;
23849 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23850 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23851 int check_mouse_face = 0;
23852 int dummy_x = 0;
23854 /* If mouse highlighting is on, we may need to draw adjacent
23855 glyphs using mouse-face highlighting. */
23856 if (area == TEXT_AREA && row->mouse_face_p
23857 && hlinfo->mouse_face_beg_row >= 0
23858 && hlinfo->mouse_face_end_row >= 0)
23860 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23862 if (row_vpos >= hlinfo->mouse_face_beg_row
23863 && row_vpos <= hlinfo->mouse_face_end_row)
23865 check_mouse_face = 1;
23866 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23867 ? hlinfo->mouse_face_beg_col : 0;
23868 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23869 ? hlinfo->mouse_face_end_col
23870 : row->used[TEXT_AREA];
23874 /* Compute overhangs for all glyph strings. */
23875 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23876 for (s = head; s; s = s->next)
23877 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23879 /* Prepend glyph strings for glyphs in front of the first glyph
23880 string that are overwritten because of the first glyph
23881 string's left overhang. The background of all strings
23882 prepended must be drawn because the first glyph string
23883 draws over it. */
23884 i = left_overwritten (head);
23885 if (i >= 0)
23887 enum draw_glyphs_face overlap_hl;
23889 /* If this row contains mouse highlighting, attempt to draw
23890 the overlapped glyphs with the correct highlight. This
23891 code fails if the overlap encompasses more than one glyph
23892 and mouse-highlight spans only some of these glyphs.
23893 However, making it work perfectly involves a lot more
23894 code, and I don't know if the pathological case occurs in
23895 practice, so we'll stick to this for now. --- cyd */
23896 if (check_mouse_face
23897 && mouse_beg_col < start && mouse_end_col > i)
23898 overlap_hl = DRAW_MOUSE_FACE;
23899 else
23900 overlap_hl = DRAW_NORMAL_TEXT;
23902 j = i;
23903 BUILD_GLYPH_STRINGS (j, start, h, t,
23904 overlap_hl, dummy_x, last_x);
23905 start = i;
23906 compute_overhangs_and_x (t, head->x, 1);
23907 prepend_glyph_string_lists (&head, &tail, h, t);
23908 clip_head = head;
23911 /* Prepend glyph strings for glyphs in front of the first glyph
23912 string that overwrite that glyph string because of their
23913 right overhang. For these strings, only the foreground must
23914 be drawn, because it draws over the glyph string at `head'.
23915 The background must not be drawn because this would overwrite
23916 right overhangs of preceding glyphs for which no glyph
23917 strings exist. */
23918 i = left_overwriting (head);
23919 if (i >= 0)
23921 enum draw_glyphs_face overlap_hl;
23923 if (check_mouse_face
23924 && mouse_beg_col < start && mouse_end_col > i)
23925 overlap_hl = DRAW_MOUSE_FACE;
23926 else
23927 overlap_hl = DRAW_NORMAL_TEXT;
23929 clip_head = head;
23930 BUILD_GLYPH_STRINGS (i, start, h, t,
23931 overlap_hl, dummy_x, last_x);
23932 for (s = h; s; s = s->next)
23933 s->background_filled_p = 1;
23934 compute_overhangs_and_x (t, head->x, 1);
23935 prepend_glyph_string_lists (&head, &tail, h, t);
23938 /* Append glyphs strings for glyphs following the last glyph
23939 string tail that are overwritten by tail. The background of
23940 these strings has to be drawn because tail's foreground draws
23941 over it. */
23942 i = right_overwritten (tail);
23943 if (i >= 0)
23945 enum draw_glyphs_face overlap_hl;
23947 if (check_mouse_face
23948 && mouse_beg_col < i && mouse_end_col > end)
23949 overlap_hl = DRAW_MOUSE_FACE;
23950 else
23951 overlap_hl = DRAW_NORMAL_TEXT;
23953 BUILD_GLYPH_STRINGS (end, i, h, t,
23954 overlap_hl, x, last_x);
23955 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23956 we don't have `end = i;' here. */
23957 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23958 append_glyph_string_lists (&head, &tail, h, t);
23959 clip_tail = tail;
23962 /* Append glyph strings for glyphs following the last glyph
23963 string tail that overwrite tail. The foreground of such
23964 glyphs has to be drawn because it writes into the background
23965 of tail. The background must not be drawn because it could
23966 paint over the foreground of following glyphs. */
23967 i = right_overwriting (tail);
23968 if (i >= 0)
23970 enum draw_glyphs_face overlap_hl;
23971 if (check_mouse_face
23972 && mouse_beg_col < i && mouse_end_col > end)
23973 overlap_hl = DRAW_MOUSE_FACE;
23974 else
23975 overlap_hl = DRAW_NORMAL_TEXT;
23977 clip_tail = tail;
23978 i++; /* We must include the Ith glyph. */
23979 BUILD_GLYPH_STRINGS (end, i, h, t,
23980 overlap_hl, x, last_x);
23981 for (s = h; s; s = s->next)
23982 s->background_filled_p = 1;
23983 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23984 append_glyph_string_lists (&head, &tail, h, t);
23986 if (clip_head || clip_tail)
23987 for (s = head; s; s = s->next)
23989 s->clip_head = clip_head;
23990 s->clip_tail = clip_tail;
23994 /* Draw all strings. */
23995 for (s = head; s; s = s->next)
23996 FRAME_RIF (f)->draw_glyph_string (s);
23998 #ifndef HAVE_NS
23999 /* When focus a sole frame and move horizontally, this sets on_p to 0
24000 causing a failure to erase prev cursor position. */
24001 if (area == TEXT_AREA
24002 && !row->full_width_p
24003 /* When drawing overlapping rows, only the glyph strings'
24004 foreground is drawn, which doesn't erase a cursor
24005 completely. */
24006 && !overlaps)
24008 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24009 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24010 : (tail ? tail->x + tail->background_width : x));
24011 x0 -= area_left;
24012 x1 -= area_left;
24014 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24015 row->y, MATRIX_ROW_BOTTOM_Y (row));
24017 #endif
24019 /* Value is the x-position up to which drawn, relative to AREA of W.
24020 This doesn't include parts drawn because of overhangs. */
24021 if (row->full_width_p)
24022 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24023 else
24024 x_reached -= area_left;
24026 RELEASE_HDC (hdc, f);
24028 return x_reached;
24031 /* Expand row matrix if too narrow. Don't expand if area
24032 is not present. */
24034 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24036 if (!it->f->fonts_changed \
24037 && (it->glyph_row->glyphs[area] \
24038 < it->glyph_row->glyphs[area + 1])) \
24040 it->w->ncols_scale_factor++; \
24041 it->f->fonts_changed = 1; \
24045 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24046 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24048 static void
24049 append_glyph (struct it *it)
24051 struct glyph *glyph;
24052 enum glyph_row_area area = it->area;
24054 eassert (it->glyph_row);
24055 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24057 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24058 if (glyph < it->glyph_row->glyphs[area + 1])
24060 /* If the glyph row is reversed, we need to prepend the glyph
24061 rather than append it. */
24062 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24064 struct glyph *g;
24066 /* Make room for the additional glyph. */
24067 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24068 g[1] = *g;
24069 glyph = it->glyph_row->glyphs[area];
24071 glyph->charpos = CHARPOS (it->position);
24072 glyph->object = it->object;
24073 if (it->pixel_width > 0)
24075 glyph->pixel_width = it->pixel_width;
24076 glyph->padding_p = 0;
24078 else
24080 /* Assure at least 1-pixel width. Otherwise, cursor can't
24081 be displayed correctly. */
24082 glyph->pixel_width = 1;
24083 glyph->padding_p = 1;
24085 glyph->ascent = it->ascent;
24086 glyph->descent = it->descent;
24087 glyph->voffset = it->voffset;
24088 glyph->type = CHAR_GLYPH;
24089 glyph->avoid_cursor_p = it->avoid_cursor_p;
24090 glyph->multibyte_p = it->multibyte_p;
24091 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24093 /* In R2L rows, the left and the right box edges need to be
24094 drawn in reverse direction. */
24095 glyph->right_box_line_p = it->start_of_box_run_p;
24096 glyph->left_box_line_p = it->end_of_box_run_p;
24098 else
24100 glyph->left_box_line_p = it->start_of_box_run_p;
24101 glyph->right_box_line_p = it->end_of_box_run_p;
24103 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24104 || it->phys_descent > it->descent);
24105 glyph->glyph_not_available_p = it->glyph_not_available_p;
24106 glyph->face_id = it->face_id;
24107 glyph->u.ch = it->char_to_display;
24108 glyph->slice.img = null_glyph_slice;
24109 glyph->font_type = FONT_TYPE_UNKNOWN;
24110 if (it->bidi_p)
24112 glyph->resolved_level = it->bidi_it.resolved_level;
24113 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24114 emacs_abort ();
24115 glyph->bidi_type = it->bidi_it.type;
24117 else
24119 glyph->resolved_level = 0;
24120 glyph->bidi_type = UNKNOWN_BT;
24122 ++it->glyph_row->used[area];
24124 else
24125 IT_EXPAND_MATRIX_WIDTH (it, area);
24128 /* Store one glyph for the composition IT->cmp_it.id in
24129 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24130 non-null. */
24132 static void
24133 append_composite_glyph (struct it *it)
24135 struct glyph *glyph;
24136 enum glyph_row_area area = it->area;
24138 eassert (it->glyph_row);
24140 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24141 if (glyph < it->glyph_row->glyphs[area + 1])
24143 /* If the glyph row is reversed, we need to prepend the glyph
24144 rather than append it. */
24145 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24147 struct glyph *g;
24149 /* Make room for the new glyph. */
24150 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24151 g[1] = *g;
24152 glyph = it->glyph_row->glyphs[it->area];
24154 glyph->charpos = it->cmp_it.charpos;
24155 glyph->object = it->object;
24156 glyph->pixel_width = it->pixel_width;
24157 glyph->ascent = it->ascent;
24158 glyph->descent = it->descent;
24159 glyph->voffset = it->voffset;
24160 glyph->type = COMPOSITE_GLYPH;
24161 if (it->cmp_it.ch < 0)
24163 glyph->u.cmp.automatic = 0;
24164 glyph->u.cmp.id = it->cmp_it.id;
24165 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24167 else
24169 glyph->u.cmp.automatic = 1;
24170 glyph->u.cmp.id = it->cmp_it.id;
24171 glyph->slice.cmp.from = it->cmp_it.from;
24172 glyph->slice.cmp.to = it->cmp_it.to - 1;
24174 glyph->avoid_cursor_p = it->avoid_cursor_p;
24175 glyph->multibyte_p = it->multibyte_p;
24176 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24178 /* In R2L rows, the left and the right box edges need to be
24179 drawn in reverse direction. */
24180 glyph->right_box_line_p = it->start_of_box_run_p;
24181 glyph->left_box_line_p = it->end_of_box_run_p;
24183 else
24185 glyph->left_box_line_p = it->start_of_box_run_p;
24186 glyph->right_box_line_p = it->end_of_box_run_p;
24188 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24189 || it->phys_descent > it->descent);
24190 glyph->padding_p = 0;
24191 glyph->glyph_not_available_p = 0;
24192 glyph->face_id = it->face_id;
24193 glyph->font_type = FONT_TYPE_UNKNOWN;
24194 if (it->bidi_p)
24196 glyph->resolved_level = it->bidi_it.resolved_level;
24197 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24198 emacs_abort ();
24199 glyph->bidi_type = it->bidi_it.type;
24201 ++it->glyph_row->used[area];
24203 else
24204 IT_EXPAND_MATRIX_WIDTH (it, area);
24208 /* Change IT->ascent and IT->height according to the setting of
24209 IT->voffset. */
24211 static void
24212 take_vertical_position_into_account (struct it *it)
24214 if (it->voffset)
24216 if (it->voffset < 0)
24217 /* Increase the ascent so that we can display the text higher
24218 in the line. */
24219 it->ascent -= it->voffset;
24220 else
24221 /* Increase the descent so that we can display the text lower
24222 in the line. */
24223 it->descent += it->voffset;
24228 /* Produce glyphs/get display metrics for the image IT is loaded with.
24229 See the description of struct display_iterator in dispextern.h for
24230 an overview of struct display_iterator. */
24232 static void
24233 produce_image_glyph (struct it *it)
24235 struct image *img;
24236 struct face *face;
24237 int glyph_ascent, crop;
24238 struct glyph_slice slice;
24240 eassert (it->what == IT_IMAGE);
24242 face = FACE_FROM_ID (it->f, it->face_id);
24243 eassert (face);
24244 /* Make sure X resources of the face is loaded. */
24245 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24247 if (it->image_id < 0)
24249 /* Fringe bitmap. */
24250 it->ascent = it->phys_ascent = 0;
24251 it->descent = it->phys_descent = 0;
24252 it->pixel_width = 0;
24253 it->nglyphs = 0;
24254 return;
24257 img = IMAGE_FROM_ID (it->f, it->image_id);
24258 eassert (img);
24259 /* Make sure X resources of the image is loaded. */
24260 prepare_image_for_display (it->f, img);
24262 slice.x = slice.y = 0;
24263 slice.width = img->width;
24264 slice.height = img->height;
24266 if (INTEGERP (it->slice.x))
24267 slice.x = XINT (it->slice.x);
24268 else if (FLOATP (it->slice.x))
24269 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24271 if (INTEGERP (it->slice.y))
24272 slice.y = XINT (it->slice.y);
24273 else if (FLOATP (it->slice.y))
24274 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24276 if (INTEGERP (it->slice.width))
24277 slice.width = XINT (it->slice.width);
24278 else if (FLOATP (it->slice.width))
24279 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24281 if (INTEGERP (it->slice.height))
24282 slice.height = XINT (it->slice.height);
24283 else if (FLOATP (it->slice.height))
24284 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24286 if (slice.x >= img->width)
24287 slice.x = img->width;
24288 if (slice.y >= img->height)
24289 slice.y = img->height;
24290 if (slice.x + slice.width >= img->width)
24291 slice.width = img->width - slice.x;
24292 if (slice.y + slice.height > img->height)
24293 slice.height = img->height - slice.y;
24295 if (slice.width == 0 || slice.height == 0)
24296 return;
24298 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24300 it->descent = slice.height - glyph_ascent;
24301 if (slice.y == 0)
24302 it->descent += img->vmargin;
24303 if (slice.y + slice.height == img->height)
24304 it->descent += img->vmargin;
24305 it->phys_descent = it->descent;
24307 it->pixel_width = slice.width;
24308 if (slice.x == 0)
24309 it->pixel_width += img->hmargin;
24310 if (slice.x + slice.width == img->width)
24311 it->pixel_width += img->hmargin;
24313 /* It's quite possible for images to have an ascent greater than
24314 their height, so don't get confused in that case. */
24315 if (it->descent < 0)
24316 it->descent = 0;
24318 it->nglyphs = 1;
24320 if (face->box != FACE_NO_BOX)
24322 if (face->box_line_width > 0)
24324 if (slice.y == 0)
24325 it->ascent += face->box_line_width;
24326 if (slice.y + slice.height == img->height)
24327 it->descent += face->box_line_width;
24330 if (it->start_of_box_run_p && slice.x == 0)
24331 it->pixel_width += eabs (face->box_line_width);
24332 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24333 it->pixel_width += eabs (face->box_line_width);
24336 take_vertical_position_into_account (it);
24338 /* Automatically crop wide image glyphs at right edge so we can
24339 draw the cursor on same display row. */
24340 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24341 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24343 it->pixel_width -= crop;
24344 slice.width -= crop;
24347 if (it->glyph_row)
24349 struct glyph *glyph;
24350 enum glyph_row_area area = it->area;
24352 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24353 if (glyph < it->glyph_row->glyphs[area + 1])
24355 glyph->charpos = CHARPOS (it->position);
24356 glyph->object = it->object;
24357 glyph->pixel_width = it->pixel_width;
24358 glyph->ascent = glyph_ascent;
24359 glyph->descent = it->descent;
24360 glyph->voffset = it->voffset;
24361 glyph->type = IMAGE_GLYPH;
24362 glyph->avoid_cursor_p = it->avoid_cursor_p;
24363 glyph->multibyte_p = it->multibyte_p;
24364 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24366 /* In R2L rows, the left and the right box edges need to be
24367 drawn in reverse direction. */
24368 glyph->right_box_line_p = it->start_of_box_run_p;
24369 glyph->left_box_line_p = it->end_of_box_run_p;
24371 else
24373 glyph->left_box_line_p = it->start_of_box_run_p;
24374 glyph->right_box_line_p = it->end_of_box_run_p;
24376 glyph->overlaps_vertically_p = 0;
24377 glyph->padding_p = 0;
24378 glyph->glyph_not_available_p = 0;
24379 glyph->face_id = it->face_id;
24380 glyph->u.img_id = img->id;
24381 glyph->slice.img = slice;
24382 glyph->font_type = FONT_TYPE_UNKNOWN;
24383 if (it->bidi_p)
24385 glyph->resolved_level = it->bidi_it.resolved_level;
24386 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24387 emacs_abort ();
24388 glyph->bidi_type = it->bidi_it.type;
24390 ++it->glyph_row->used[area];
24392 else
24393 IT_EXPAND_MATRIX_WIDTH (it, area);
24398 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24399 of the glyph, WIDTH and HEIGHT are the width and height of the
24400 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24402 static void
24403 append_stretch_glyph (struct it *it, Lisp_Object object,
24404 int width, int height, int ascent)
24406 struct glyph *glyph;
24407 enum glyph_row_area area = it->area;
24409 eassert (ascent >= 0 && ascent <= height);
24411 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24412 if (glyph < it->glyph_row->glyphs[area + 1])
24414 /* If the glyph row is reversed, we need to prepend the glyph
24415 rather than append it. */
24416 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24418 struct glyph *g;
24420 /* Make room for the additional glyph. */
24421 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24422 g[1] = *g;
24423 glyph = it->glyph_row->glyphs[area];
24425 glyph->charpos = CHARPOS (it->position);
24426 glyph->object = object;
24427 glyph->pixel_width = width;
24428 glyph->ascent = ascent;
24429 glyph->descent = height - ascent;
24430 glyph->voffset = it->voffset;
24431 glyph->type = STRETCH_GLYPH;
24432 glyph->avoid_cursor_p = it->avoid_cursor_p;
24433 glyph->multibyte_p = it->multibyte_p;
24434 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24436 /* In R2L rows, the left and the right box edges need to be
24437 drawn in reverse direction. */
24438 glyph->right_box_line_p = it->start_of_box_run_p;
24439 glyph->left_box_line_p = it->end_of_box_run_p;
24441 else
24443 glyph->left_box_line_p = it->start_of_box_run_p;
24444 glyph->right_box_line_p = it->end_of_box_run_p;
24446 glyph->overlaps_vertically_p = 0;
24447 glyph->padding_p = 0;
24448 glyph->glyph_not_available_p = 0;
24449 glyph->face_id = it->face_id;
24450 glyph->u.stretch.ascent = ascent;
24451 glyph->u.stretch.height = height;
24452 glyph->slice.img = null_glyph_slice;
24453 glyph->font_type = FONT_TYPE_UNKNOWN;
24454 if (it->bidi_p)
24456 glyph->resolved_level = it->bidi_it.resolved_level;
24457 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24458 emacs_abort ();
24459 glyph->bidi_type = it->bidi_it.type;
24461 else
24463 glyph->resolved_level = 0;
24464 glyph->bidi_type = UNKNOWN_BT;
24466 ++it->glyph_row->used[area];
24468 else
24469 IT_EXPAND_MATRIX_WIDTH (it, area);
24472 #endif /* HAVE_WINDOW_SYSTEM */
24474 /* Produce a stretch glyph for iterator IT. IT->object is the value
24475 of the glyph property displayed. The value must be a list
24476 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24477 being recognized:
24479 1. `:width WIDTH' specifies that the space should be WIDTH *
24480 canonical char width wide. WIDTH may be an integer or floating
24481 point number.
24483 2. `:relative-width FACTOR' specifies that the width of the stretch
24484 should be computed from the width of the first character having the
24485 `glyph' property, and should be FACTOR times that width.
24487 3. `:align-to HPOS' specifies that the space should be wide enough
24488 to reach HPOS, a value in canonical character units.
24490 Exactly one of the above pairs must be present.
24492 4. `:height HEIGHT' specifies that the height of the stretch produced
24493 should be HEIGHT, measured in canonical character units.
24495 5. `:relative-height FACTOR' specifies that the height of the
24496 stretch should be FACTOR times the height of the characters having
24497 the glyph property.
24499 Either none or exactly one of 4 or 5 must be present.
24501 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24502 of the stretch should be used for the ascent of the stretch.
24503 ASCENT must be in the range 0 <= ASCENT <= 100. */
24505 void
24506 produce_stretch_glyph (struct it *it)
24508 /* (space :width WIDTH :height HEIGHT ...) */
24509 Lisp_Object prop, plist;
24510 int width = 0, height = 0, align_to = -1;
24511 int zero_width_ok_p = 0;
24512 double tem;
24513 struct font *font = NULL;
24515 #ifdef HAVE_WINDOW_SYSTEM
24516 int ascent = 0;
24517 int zero_height_ok_p = 0;
24519 if (FRAME_WINDOW_P (it->f))
24521 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24522 font = face->font ? face->font : FRAME_FONT (it->f);
24523 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24525 #endif
24527 /* List should start with `space'. */
24528 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24529 plist = XCDR (it->object);
24531 /* Compute the width of the stretch. */
24532 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24533 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24535 /* Absolute width `:width WIDTH' specified and valid. */
24536 zero_width_ok_p = 1;
24537 width = (int)tem;
24539 #ifdef HAVE_WINDOW_SYSTEM
24540 else if (FRAME_WINDOW_P (it->f)
24541 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24543 /* Relative width `:relative-width FACTOR' specified and valid.
24544 Compute the width of the characters having the `glyph'
24545 property. */
24546 struct it it2;
24547 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24549 it2 = *it;
24550 if (it->multibyte_p)
24551 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24552 else
24554 it2.c = it2.char_to_display = *p, it2.len = 1;
24555 if (! ASCII_CHAR_P (it2.c))
24556 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24559 it2.glyph_row = NULL;
24560 it2.what = IT_CHARACTER;
24561 x_produce_glyphs (&it2);
24562 width = NUMVAL (prop) * it2.pixel_width;
24564 #endif /* HAVE_WINDOW_SYSTEM */
24565 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24566 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24568 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24569 align_to = (align_to < 0
24571 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24572 else if (align_to < 0)
24573 align_to = window_box_left_offset (it->w, TEXT_AREA);
24574 width = max (0, (int)tem + align_to - it->current_x);
24575 zero_width_ok_p = 1;
24577 else
24578 /* Nothing specified -> width defaults to canonical char width. */
24579 width = FRAME_COLUMN_WIDTH (it->f);
24581 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24582 width = 1;
24584 #ifdef HAVE_WINDOW_SYSTEM
24585 /* Compute height. */
24586 if (FRAME_WINDOW_P (it->f))
24588 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24589 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24591 height = (int)tem;
24592 zero_height_ok_p = 1;
24594 else if (prop = Fplist_get (plist, QCrelative_height),
24595 NUMVAL (prop) > 0)
24596 height = FONT_HEIGHT (font) * NUMVAL (prop);
24597 else
24598 height = FONT_HEIGHT (font);
24600 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24601 height = 1;
24603 /* Compute percentage of height used for ascent. If
24604 `:ascent ASCENT' is present and valid, use that. Otherwise,
24605 derive the ascent from the font in use. */
24606 if (prop = Fplist_get (plist, QCascent),
24607 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24608 ascent = height * NUMVAL (prop) / 100.0;
24609 else if (!NILP (prop)
24610 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24611 ascent = min (max (0, (int)tem), height);
24612 else
24613 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24615 else
24616 #endif /* HAVE_WINDOW_SYSTEM */
24617 height = 1;
24619 if (width > 0 && it->line_wrap != TRUNCATE
24620 && it->current_x + width > it->last_visible_x)
24622 width = it->last_visible_x - it->current_x;
24623 #ifdef HAVE_WINDOW_SYSTEM
24624 /* Subtract one more pixel from the stretch width, but only on
24625 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24626 width -= FRAME_WINDOW_P (it->f);
24627 #endif
24630 if (width > 0 && height > 0 && it->glyph_row)
24632 Lisp_Object o_object = it->object;
24633 Lisp_Object object = it->stack[it->sp - 1].string;
24634 int n = width;
24636 if (!STRINGP (object))
24637 object = it->w->contents;
24638 #ifdef HAVE_WINDOW_SYSTEM
24639 if (FRAME_WINDOW_P (it->f))
24640 append_stretch_glyph (it, object, width, height, ascent);
24641 else
24642 #endif
24644 it->object = object;
24645 it->char_to_display = ' ';
24646 it->pixel_width = it->len = 1;
24647 while (n--)
24648 tty_append_glyph (it);
24649 it->object = o_object;
24653 it->pixel_width = width;
24654 #ifdef HAVE_WINDOW_SYSTEM
24655 if (FRAME_WINDOW_P (it->f))
24657 it->ascent = it->phys_ascent = ascent;
24658 it->descent = it->phys_descent = height - it->ascent;
24659 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24660 take_vertical_position_into_account (it);
24662 else
24663 #endif
24664 it->nglyphs = width;
24667 /* Get information about special display element WHAT in an
24668 environment described by IT. WHAT is one of IT_TRUNCATION or
24669 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24670 non-null glyph_row member. This function ensures that fields like
24671 face_id, c, len of IT are left untouched. */
24673 static void
24674 produce_special_glyphs (struct it *it, enum display_element_type what)
24676 struct it temp_it;
24677 Lisp_Object gc;
24678 GLYPH glyph;
24680 temp_it = *it;
24681 temp_it.object = make_number (0);
24682 memset (&temp_it.current, 0, sizeof temp_it.current);
24684 if (what == IT_CONTINUATION)
24686 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24687 if (it->bidi_it.paragraph_dir == R2L)
24688 SET_GLYPH_FROM_CHAR (glyph, '/');
24689 else
24690 SET_GLYPH_FROM_CHAR (glyph, '\\');
24691 if (it->dp
24692 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24694 /* FIXME: Should we mirror GC for R2L lines? */
24695 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24696 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24699 else if (what == IT_TRUNCATION)
24701 /* Truncation glyph. */
24702 SET_GLYPH_FROM_CHAR (glyph, '$');
24703 if (it->dp
24704 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24706 /* FIXME: Should we mirror GC for R2L lines? */
24707 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24708 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24711 else
24712 emacs_abort ();
24714 #ifdef HAVE_WINDOW_SYSTEM
24715 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24716 is turned off, we precede the truncation/continuation glyphs by a
24717 stretch glyph whose width is computed such that these special
24718 glyphs are aligned at the window margin, even when very different
24719 fonts are used in different glyph rows. */
24720 if (FRAME_WINDOW_P (temp_it.f)
24721 /* init_iterator calls this with it->glyph_row == NULL, and it
24722 wants only the pixel width of the truncation/continuation
24723 glyphs. */
24724 && temp_it.glyph_row
24725 /* insert_left_trunc_glyphs calls us at the beginning of the
24726 row, and it has its own calculation of the stretch glyph
24727 width. */
24728 && temp_it.glyph_row->used[TEXT_AREA] > 0
24729 && (temp_it.glyph_row->reversed_p
24730 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24731 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24733 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24735 if (stretch_width > 0)
24737 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24738 struct font *font =
24739 face->font ? face->font : FRAME_FONT (temp_it.f);
24740 int stretch_ascent =
24741 (((temp_it.ascent + temp_it.descent)
24742 * FONT_BASE (font)) / FONT_HEIGHT (font));
24744 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24745 temp_it.ascent + temp_it.descent,
24746 stretch_ascent);
24749 #endif
24751 temp_it.dp = NULL;
24752 temp_it.what = IT_CHARACTER;
24753 temp_it.len = 1;
24754 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24755 temp_it.face_id = GLYPH_FACE (glyph);
24756 temp_it.len = CHAR_BYTES (temp_it.c);
24758 PRODUCE_GLYPHS (&temp_it);
24759 it->pixel_width = temp_it.pixel_width;
24760 it->nglyphs = temp_it.pixel_width;
24763 #ifdef HAVE_WINDOW_SYSTEM
24765 /* Calculate line-height and line-spacing properties.
24766 An integer value specifies explicit pixel value.
24767 A float value specifies relative value to current face height.
24768 A cons (float . face-name) specifies relative value to
24769 height of specified face font.
24771 Returns height in pixels, or nil. */
24774 static Lisp_Object
24775 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24776 int boff, int override)
24778 Lisp_Object face_name = Qnil;
24779 int ascent, descent, height;
24781 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24782 return val;
24784 if (CONSP (val))
24786 face_name = XCAR (val);
24787 val = XCDR (val);
24788 if (!NUMBERP (val))
24789 val = make_number (1);
24790 if (NILP (face_name))
24792 height = it->ascent + it->descent;
24793 goto scale;
24797 if (NILP (face_name))
24799 font = FRAME_FONT (it->f);
24800 boff = FRAME_BASELINE_OFFSET (it->f);
24802 else if (EQ (face_name, Qt))
24804 override = 0;
24806 else
24808 int face_id;
24809 struct face *face;
24811 face_id = lookup_named_face (it->f, face_name, 0);
24812 if (face_id < 0)
24813 return make_number (-1);
24815 face = FACE_FROM_ID (it->f, face_id);
24816 font = face->font;
24817 if (font == NULL)
24818 return make_number (-1);
24819 boff = font->baseline_offset;
24820 if (font->vertical_centering)
24821 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24824 ascent = FONT_BASE (font) + boff;
24825 descent = FONT_DESCENT (font) - boff;
24827 if (override)
24829 it->override_ascent = ascent;
24830 it->override_descent = descent;
24831 it->override_boff = boff;
24834 height = ascent + descent;
24836 scale:
24837 if (FLOATP (val))
24838 height = (int)(XFLOAT_DATA (val) * height);
24839 else if (INTEGERP (val))
24840 height *= XINT (val);
24842 return make_number (height);
24846 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24847 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24848 and only if this is for a character for which no font was found.
24850 If the display method (it->glyphless_method) is
24851 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24852 length of the acronym or the hexadecimal string, UPPER_XOFF and
24853 UPPER_YOFF are pixel offsets for the upper part of the string,
24854 LOWER_XOFF and LOWER_YOFF are for the lower part.
24856 For the other display methods, LEN through LOWER_YOFF are zero. */
24858 static void
24859 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24860 short upper_xoff, short upper_yoff,
24861 short lower_xoff, short lower_yoff)
24863 struct glyph *glyph;
24864 enum glyph_row_area area = it->area;
24866 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24867 if (glyph < it->glyph_row->glyphs[area + 1])
24869 /* If the glyph row is reversed, we need to prepend the glyph
24870 rather than append it. */
24871 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24873 struct glyph *g;
24875 /* Make room for the additional glyph. */
24876 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24877 g[1] = *g;
24878 glyph = it->glyph_row->glyphs[area];
24880 glyph->charpos = CHARPOS (it->position);
24881 glyph->object = it->object;
24882 glyph->pixel_width = it->pixel_width;
24883 glyph->ascent = it->ascent;
24884 glyph->descent = it->descent;
24885 glyph->voffset = it->voffset;
24886 glyph->type = GLYPHLESS_GLYPH;
24887 glyph->u.glyphless.method = it->glyphless_method;
24888 glyph->u.glyphless.for_no_font = for_no_font;
24889 glyph->u.glyphless.len = len;
24890 glyph->u.glyphless.ch = it->c;
24891 glyph->slice.glyphless.upper_xoff = upper_xoff;
24892 glyph->slice.glyphless.upper_yoff = upper_yoff;
24893 glyph->slice.glyphless.lower_xoff = lower_xoff;
24894 glyph->slice.glyphless.lower_yoff = lower_yoff;
24895 glyph->avoid_cursor_p = it->avoid_cursor_p;
24896 glyph->multibyte_p = it->multibyte_p;
24897 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24899 /* In R2L rows, the left and the right box edges need to be
24900 drawn in reverse direction. */
24901 glyph->right_box_line_p = it->start_of_box_run_p;
24902 glyph->left_box_line_p = it->end_of_box_run_p;
24904 else
24906 glyph->left_box_line_p = it->start_of_box_run_p;
24907 glyph->right_box_line_p = it->end_of_box_run_p;
24909 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24910 || it->phys_descent > it->descent);
24911 glyph->padding_p = 0;
24912 glyph->glyph_not_available_p = 0;
24913 glyph->face_id = face_id;
24914 glyph->font_type = FONT_TYPE_UNKNOWN;
24915 if (it->bidi_p)
24917 glyph->resolved_level = it->bidi_it.resolved_level;
24918 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24919 emacs_abort ();
24920 glyph->bidi_type = it->bidi_it.type;
24922 ++it->glyph_row->used[area];
24924 else
24925 IT_EXPAND_MATRIX_WIDTH (it, area);
24929 /* Produce a glyph for a glyphless character for iterator IT.
24930 IT->glyphless_method specifies which method to use for displaying
24931 the character. See the description of enum
24932 glyphless_display_method in dispextern.h for the detail.
24934 FOR_NO_FONT is nonzero if and only if this is for a character for
24935 which no font was found. ACRONYM, if non-nil, is an acronym string
24936 for the character. */
24938 static void
24939 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24941 int face_id;
24942 struct face *face;
24943 struct font *font;
24944 int base_width, base_height, width, height;
24945 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24946 int len;
24948 /* Get the metrics of the base font. We always refer to the current
24949 ASCII face. */
24950 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24951 font = face->font ? face->font : FRAME_FONT (it->f);
24952 it->ascent = FONT_BASE (font) + font->baseline_offset;
24953 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24954 base_height = it->ascent + it->descent;
24955 base_width = font->average_width;
24957 face_id = merge_glyphless_glyph_face (it);
24959 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24961 it->pixel_width = THIN_SPACE_WIDTH;
24962 len = 0;
24963 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24965 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24967 width = CHAR_WIDTH (it->c);
24968 if (width == 0)
24969 width = 1;
24970 else if (width > 4)
24971 width = 4;
24972 it->pixel_width = base_width * width;
24973 len = 0;
24974 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24976 else
24978 char buf[7];
24979 const char *str;
24980 unsigned int code[6];
24981 int upper_len;
24982 int ascent, descent;
24983 struct font_metrics metrics_upper, metrics_lower;
24985 face = FACE_FROM_ID (it->f, face_id);
24986 font = face->font ? face->font : FRAME_FONT (it->f);
24987 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24989 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24991 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24992 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24993 if (CONSP (acronym))
24994 acronym = XCAR (acronym);
24995 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24997 else
24999 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25000 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25001 str = buf;
25003 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25004 code[len] = font->driver->encode_char (font, str[len]);
25005 upper_len = (len + 1) / 2;
25006 font->driver->text_extents (font, code, upper_len,
25007 &metrics_upper);
25008 font->driver->text_extents (font, code + upper_len, len - upper_len,
25009 &metrics_lower);
25013 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25014 width = max (metrics_upper.width, metrics_lower.width) + 4;
25015 upper_xoff = upper_yoff = 2; /* the typical case */
25016 if (base_width >= width)
25018 /* Align the upper to the left, the lower to the right. */
25019 it->pixel_width = base_width;
25020 lower_xoff = base_width - 2 - metrics_lower.width;
25022 else
25024 /* Center the shorter one. */
25025 it->pixel_width = width;
25026 if (metrics_upper.width >= metrics_lower.width)
25027 lower_xoff = (width - metrics_lower.width) / 2;
25028 else
25030 /* FIXME: This code doesn't look right. It formerly was
25031 missing the "lower_xoff = 0;", which couldn't have
25032 been right since it left lower_xoff uninitialized. */
25033 lower_xoff = 0;
25034 upper_xoff = (width - metrics_upper.width) / 2;
25038 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25039 top, bottom, and between upper and lower strings. */
25040 height = (metrics_upper.ascent + metrics_upper.descent
25041 + metrics_lower.ascent + metrics_lower.descent) + 5;
25042 /* Center vertically.
25043 H:base_height, D:base_descent
25044 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25046 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25047 descent = D - H/2 + h/2;
25048 lower_yoff = descent - 2 - ld;
25049 upper_yoff = lower_yoff - la - 1 - ud; */
25050 ascent = - (it->descent - (base_height + height + 1) / 2);
25051 descent = it->descent - (base_height - height) / 2;
25052 lower_yoff = descent - 2 - metrics_lower.descent;
25053 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25054 - metrics_upper.descent);
25055 /* Don't make the height shorter than the base height. */
25056 if (height > base_height)
25058 it->ascent = ascent;
25059 it->descent = descent;
25063 it->phys_ascent = it->ascent;
25064 it->phys_descent = it->descent;
25065 if (it->glyph_row)
25066 append_glyphless_glyph (it, face_id, for_no_font, len,
25067 upper_xoff, upper_yoff,
25068 lower_xoff, lower_yoff);
25069 it->nglyphs = 1;
25070 take_vertical_position_into_account (it);
25074 /* RIF:
25075 Produce glyphs/get display metrics for the display element IT is
25076 loaded with. See the description of struct it in dispextern.h
25077 for an overview of struct it. */
25079 void
25080 x_produce_glyphs (struct it *it)
25082 int extra_line_spacing = it->extra_line_spacing;
25084 it->glyph_not_available_p = 0;
25086 if (it->what == IT_CHARACTER)
25088 XChar2b char2b;
25089 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25090 struct font *font = face->font;
25091 struct font_metrics *pcm = NULL;
25092 int boff; /* baseline offset */
25094 if (font == NULL)
25096 /* When no suitable font is found, display this character by
25097 the method specified in the first extra slot of
25098 Vglyphless_char_display. */
25099 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25101 eassert (it->what == IT_GLYPHLESS);
25102 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25103 goto done;
25106 boff = font->baseline_offset;
25107 if (font->vertical_centering)
25108 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25110 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25112 int stretched_p;
25114 it->nglyphs = 1;
25116 if (it->override_ascent >= 0)
25118 it->ascent = it->override_ascent;
25119 it->descent = it->override_descent;
25120 boff = it->override_boff;
25122 else
25124 it->ascent = FONT_BASE (font) + boff;
25125 it->descent = FONT_DESCENT (font) - boff;
25128 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25130 pcm = get_per_char_metric (font, &char2b);
25131 if (pcm->width == 0
25132 && pcm->rbearing == 0 && pcm->lbearing == 0)
25133 pcm = NULL;
25136 if (pcm)
25138 it->phys_ascent = pcm->ascent + boff;
25139 it->phys_descent = pcm->descent - boff;
25140 it->pixel_width = pcm->width;
25142 else
25144 it->glyph_not_available_p = 1;
25145 it->phys_ascent = it->ascent;
25146 it->phys_descent = it->descent;
25147 it->pixel_width = font->space_width;
25150 if (it->constrain_row_ascent_descent_p)
25152 if (it->descent > it->max_descent)
25154 it->ascent += it->descent - it->max_descent;
25155 it->descent = it->max_descent;
25157 if (it->ascent > it->max_ascent)
25159 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25160 it->ascent = it->max_ascent;
25162 it->phys_ascent = min (it->phys_ascent, it->ascent);
25163 it->phys_descent = min (it->phys_descent, it->descent);
25164 extra_line_spacing = 0;
25167 /* If this is a space inside a region of text with
25168 `space-width' property, change its width. */
25169 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25170 if (stretched_p)
25171 it->pixel_width *= XFLOATINT (it->space_width);
25173 /* If face has a box, add the box thickness to the character
25174 height. If character has a box line to the left and/or
25175 right, add the box line width to the character's width. */
25176 if (face->box != FACE_NO_BOX)
25178 int thick = face->box_line_width;
25180 if (thick > 0)
25182 it->ascent += thick;
25183 it->descent += thick;
25185 else
25186 thick = -thick;
25188 if (it->start_of_box_run_p)
25189 it->pixel_width += thick;
25190 if (it->end_of_box_run_p)
25191 it->pixel_width += thick;
25194 /* If face has an overline, add the height of the overline
25195 (1 pixel) and a 1 pixel margin to the character height. */
25196 if (face->overline_p)
25197 it->ascent += overline_margin;
25199 if (it->constrain_row_ascent_descent_p)
25201 if (it->ascent > it->max_ascent)
25202 it->ascent = it->max_ascent;
25203 if (it->descent > it->max_descent)
25204 it->descent = it->max_descent;
25207 take_vertical_position_into_account (it);
25209 /* If we have to actually produce glyphs, do it. */
25210 if (it->glyph_row)
25212 if (stretched_p)
25214 /* Translate a space with a `space-width' property
25215 into a stretch glyph. */
25216 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25217 / FONT_HEIGHT (font));
25218 append_stretch_glyph (it, it->object, it->pixel_width,
25219 it->ascent + it->descent, ascent);
25221 else
25222 append_glyph (it);
25224 /* If characters with lbearing or rbearing are displayed
25225 in this line, record that fact in a flag of the
25226 glyph row. This is used to optimize X output code. */
25227 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25228 it->glyph_row->contains_overlapping_glyphs_p = 1;
25230 if (! stretched_p && it->pixel_width == 0)
25231 /* We assure that all visible glyphs have at least 1-pixel
25232 width. */
25233 it->pixel_width = 1;
25235 else if (it->char_to_display == '\n')
25237 /* A newline has no width, but we need the height of the
25238 line. But if previous part of the line sets a height,
25239 don't increase that height */
25241 Lisp_Object height;
25242 Lisp_Object total_height = Qnil;
25244 it->override_ascent = -1;
25245 it->pixel_width = 0;
25246 it->nglyphs = 0;
25248 height = get_it_property (it, Qline_height);
25249 /* Split (line-height total-height) list */
25250 if (CONSP (height)
25251 && CONSP (XCDR (height))
25252 && NILP (XCDR (XCDR (height))))
25254 total_height = XCAR (XCDR (height));
25255 height = XCAR (height);
25257 height = calc_line_height_property (it, height, font, boff, 1);
25259 if (it->override_ascent >= 0)
25261 it->ascent = it->override_ascent;
25262 it->descent = it->override_descent;
25263 boff = it->override_boff;
25265 else
25267 it->ascent = FONT_BASE (font) + boff;
25268 it->descent = FONT_DESCENT (font) - boff;
25271 if (EQ (height, Qt))
25273 if (it->descent > it->max_descent)
25275 it->ascent += it->descent - it->max_descent;
25276 it->descent = it->max_descent;
25278 if (it->ascent > it->max_ascent)
25280 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25281 it->ascent = it->max_ascent;
25283 it->phys_ascent = min (it->phys_ascent, it->ascent);
25284 it->phys_descent = min (it->phys_descent, it->descent);
25285 it->constrain_row_ascent_descent_p = 1;
25286 extra_line_spacing = 0;
25288 else
25290 Lisp_Object spacing;
25292 it->phys_ascent = it->ascent;
25293 it->phys_descent = it->descent;
25295 if ((it->max_ascent > 0 || it->max_descent > 0)
25296 && face->box != FACE_NO_BOX
25297 && face->box_line_width > 0)
25299 it->ascent += face->box_line_width;
25300 it->descent += face->box_line_width;
25302 if (!NILP (height)
25303 && XINT (height) > it->ascent + it->descent)
25304 it->ascent = XINT (height) - it->descent;
25306 if (!NILP (total_height))
25307 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25308 else
25310 spacing = get_it_property (it, Qline_spacing);
25311 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25313 if (INTEGERP (spacing))
25315 extra_line_spacing = XINT (spacing);
25316 if (!NILP (total_height))
25317 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25321 else /* i.e. (it->char_to_display == '\t') */
25323 if (font->space_width > 0)
25325 int tab_width = it->tab_width * font->space_width;
25326 int x = it->current_x + it->continuation_lines_width;
25327 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25329 /* If the distance from the current position to the next tab
25330 stop is less than a space character width, use the
25331 tab stop after that. */
25332 if (next_tab_x - x < font->space_width)
25333 next_tab_x += tab_width;
25335 it->pixel_width = next_tab_x - x;
25336 it->nglyphs = 1;
25337 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25338 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25340 if (it->glyph_row)
25342 append_stretch_glyph (it, it->object, it->pixel_width,
25343 it->ascent + it->descent, it->ascent);
25346 else
25348 it->pixel_width = 0;
25349 it->nglyphs = 1;
25353 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25355 /* A static composition.
25357 Note: A composition is represented as one glyph in the
25358 glyph matrix. There are no padding glyphs.
25360 Important note: pixel_width, ascent, and descent are the
25361 values of what is drawn by draw_glyphs (i.e. the values of
25362 the overall glyphs composed). */
25363 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25364 int boff; /* baseline offset */
25365 struct composition *cmp = composition_table[it->cmp_it.id];
25366 int glyph_len = cmp->glyph_len;
25367 struct font *font = face->font;
25369 it->nglyphs = 1;
25371 /* If we have not yet calculated pixel size data of glyphs of
25372 the composition for the current face font, calculate them
25373 now. Theoretically, we have to check all fonts for the
25374 glyphs, but that requires much time and memory space. So,
25375 here we check only the font of the first glyph. This may
25376 lead to incorrect display, but it's very rare, and C-l
25377 (recenter-top-bottom) can correct the display anyway. */
25378 if (! cmp->font || cmp->font != font)
25380 /* Ascent and descent of the font of the first character
25381 of this composition (adjusted by baseline offset).
25382 Ascent and descent of overall glyphs should not be less
25383 than these, respectively. */
25384 int font_ascent, font_descent, font_height;
25385 /* Bounding box of the overall glyphs. */
25386 int leftmost, rightmost, lowest, highest;
25387 int lbearing, rbearing;
25388 int i, width, ascent, descent;
25389 int left_padded = 0, right_padded = 0;
25390 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25391 XChar2b char2b;
25392 struct font_metrics *pcm;
25393 int font_not_found_p;
25394 ptrdiff_t pos;
25396 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25397 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25398 break;
25399 if (glyph_len < cmp->glyph_len)
25400 right_padded = 1;
25401 for (i = 0; i < glyph_len; i++)
25403 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25404 break;
25405 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25407 if (i > 0)
25408 left_padded = 1;
25410 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25411 : IT_CHARPOS (*it));
25412 /* If no suitable font is found, use the default font. */
25413 font_not_found_p = font == NULL;
25414 if (font_not_found_p)
25416 face = face->ascii_face;
25417 font = face->font;
25419 boff = font->baseline_offset;
25420 if (font->vertical_centering)
25421 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25422 font_ascent = FONT_BASE (font) + boff;
25423 font_descent = FONT_DESCENT (font) - boff;
25424 font_height = FONT_HEIGHT (font);
25426 cmp->font = font;
25428 pcm = NULL;
25429 if (! font_not_found_p)
25431 get_char_face_and_encoding (it->f, c, it->face_id,
25432 &char2b, 0);
25433 pcm = get_per_char_metric (font, &char2b);
25436 /* Initialize the bounding box. */
25437 if (pcm)
25439 width = cmp->glyph_len > 0 ? pcm->width : 0;
25440 ascent = pcm->ascent;
25441 descent = pcm->descent;
25442 lbearing = pcm->lbearing;
25443 rbearing = pcm->rbearing;
25445 else
25447 width = cmp->glyph_len > 0 ? font->space_width : 0;
25448 ascent = FONT_BASE (font);
25449 descent = FONT_DESCENT (font);
25450 lbearing = 0;
25451 rbearing = width;
25454 rightmost = width;
25455 leftmost = 0;
25456 lowest = - descent + boff;
25457 highest = ascent + boff;
25459 if (! font_not_found_p
25460 && font->default_ascent
25461 && CHAR_TABLE_P (Vuse_default_ascent)
25462 && !NILP (Faref (Vuse_default_ascent,
25463 make_number (it->char_to_display))))
25464 highest = font->default_ascent + boff;
25466 /* Draw the first glyph at the normal position. It may be
25467 shifted to right later if some other glyphs are drawn
25468 at the left. */
25469 cmp->offsets[i * 2] = 0;
25470 cmp->offsets[i * 2 + 1] = boff;
25471 cmp->lbearing = lbearing;
25472 cmp->rbearing = rbearing;
25474 /* Set cmp->offsets for the remaining glyphs. */
25475 for (i++; i < glyph_len; i++)
25477 int left, right, btm, top;
25478 int ch = COMPOSITION_GLYPH (cmp, i);
25479 int face_id;
25480 struct face *this_face;
25482 if (ch == '\t')
25483 ch = ' ';
25484 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25485 this_face = FACE_FROM_ID (it->f, face_id);
25486 font = this_face->font;
25488 if (font == NULL)
25489 pcm = NULL;
25490 else
25492 get_char_face_and_encoding (it->f, ch, face_id,
25493 &char2b, 0);
25494 pcm = get_per_char_metric (font, &char2b);
25496 if (! pcm)
25497 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25498 else
25500 width = pcm->width;
25501 ascent = pcm->ascent;
25502 descent = pcm->descent;
25503 lbearing = pcm->lbearing;
25504 rbearing = pcm->rbearing;
25505 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25507 /* Relative composition with or without
25508 alternate chars. */
25509 left = (leftmost + rightmost - width) / 2;
25510 btm = - descent + boff;
25511 if (font->relative_compose
25512 && (! CHAR_TABLE_P (Vignore_relative_composition)
25513 || NILP (Faref (Vignore_relative_composition,
25514 make_number (ch)))))
25517 if (- descent >= font->relative_compose)
25518 /* One extra pixel between two glyphs. */
25519 btm = highest + 1;
25520 else if (ascent <= 0)
25521 /* One extra pixel between two glyphs. */
25522 btm = lowest - 1 - ascent - descent;
25525 else
25527 /* A composition rule is specified by an integer
25528 value that encodes global and new reference
25529 points (GREF and NREF). GREF and NREF are
25530 specified by numbers as below:
25532 0---1---2 -- ascent
25536 9--10--11 -- center
25538 ---3---4---5--- baseline
25540 6---7---8 -- descent
25542 int rule = COMPOSITION_RULE (cmp, i);
25543 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25545 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25546 grefx = gref % 3, nrefx = nref % 3;
25547 grefy = gref / 3, nrefy = nref / 3;
25548 if (xoff)
25549 xoff = font_height * (xoff - 128) / 256;
25550 if (yoff)
25551 yoff = font_height * (yoff - 128) / 256;
25553 left = (leftmost
25554 + grefx * (rightmost - leftmost) / 2
25555 - nrefx * width / 2
25556 + xoff);
25558 btm = ((grefy == 0 ? highest
25559 : grefy == 1 ? 0
25560 : grefy == 2 ? lowest
25561 : (highest + lowest) / 2)
25562 - (nrefy == 0 ? ascent + descent
25563 : nrefy == 1 ? descent - boff
25564 : nrefy == 2 ? 0
25565 : (ascent + descent) / 2)
25566 + yoff);
25569 cmp->offsets[i * 2] = left;
25570 cmp->offsets[i * 2 + 1] = btm + descent;
25572 /* Update the bounding box of the overall glyphs. */
25573 if (width > 0)
25575 right = left + width;
25576 if (left < leftmost)
25577 leftmost = left;
25578 if (right > rightmost)
25579 rightmost = right;
25581 top = btm + descent + ascent;
25582 if (top > highest)
25583 highest = top;
25584 if (btm < lowest)
25585 lowest = btm;
25587 if (cmp->lbearing > left + lbearing)
25588 cmp->lbearing = left + lbearing;
25589 if (cmp->rbearing < left + rbearing)
25590 cmp->rbearing = left + rbearing;
25594 /* If there are glyphs whose x-offsets are negative,
25595 shift all glyphs to the right and make all x-offsets
25596 non-negative. */
25597 if (leftmost < 0)
25599 for (i = 0; i < cmp->glyph_len; i++)
25600 cmp->offsets[i * 2] -= leftmost;
25601 rightmost -= leftmost;
25602 cmp->lbearing -= leftmost;
25603 cmp->rbearing -= leftmost;
25606 if (left_padded && cmp->lbearing < 0)
25608 for (i = 0; i < cmp->glyph_len; i++)
25609 cmp->offsets[i * 2] -= cmp->lbearing;
25610 rightmost -= cmp->lbearing;
25611 cmp->rbearing -= cmp->lbearing;
25612 cmp->lbearing = 0;
25614 if (right_padded && rightmost < cmp->rbearing)
25616 rightmost = cmp->rbearing;
25619 cmp->pixel_width = rightmost;
25620 cmp->ascent = highest;
25621 cmp->descent = - lowest;
25622 if (cmp->ascent < font_ascent)
25623 cmp->ascent = font_ascent;
25624 if (cmp->descent < font_descent)
25625 cmp->descent = font_descent;
25628 if (it->glyph_row
25629 && (cmp->lbearing < 0
25630 || cmp->rbearing > cmp->pixel_width))
25631 it->glyph_row->contains_overlapping_glyphs_p = 1;
25633 it->pixel_width = cmp->pixel_width;
25634 it->ascent = it->phys_ascent = cmp->ascent;
25635 it->descent = it->phys_descent = cmp->descent;
25636 if (face->box != FACE_NO_BOX)
25638 int thick = face->box_line_width;
25640 if (thick > 0)
25642 it->ascent += thick;
25643 it->descent += thick;
25645 else
25646 thick = - thick;
25648 if (it->start_of_box_run_p)
25649 it->pixel_width += thick;
25650 if (it->end_of_box_run_p)
25651 it->pixel_width += thick;
25654 /* If face has an overline, add the height of the overline
25655 (1 pixel) and a 1 pixel margin to the character height. */
25656 if (face->overline_p)
25657 it->ascent += overline_margin;
25659 take_vertical_position_into_account (it);
25660 if (it->ascent < 0)
25661 it->ascent = 0;
25662 if (it->descent < 0)
25663 it->descent = 0;
25665 if (it->glyph_row && cmp->glyph_len > 0)
25666 append_composite_glyph (it);
25668 else if (it->what == IT_COMPOSITION)
25670 /* A dynamic (automatic) composition. */
25671 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25672 Lisp_Object gstring;
25673 struct font_metrics metrics;
25675 it->nglyphs = 1;
25677 gstring = composition_gstring_from_id (it->cmp_it.id);
25678 it->pixel_width
25679 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25680 &metrics);
25681 if (it->glyph_row
25682 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25683 it->glyph_row->contains_overlapping_glyphs_p = 1;
25684 it->ascent = it->phys_ascent = metrics.ascent;
25685 it->descent = it->phys_descent = metrics.descent;
25686 if (face->box != FACE_NO_BOX)
25688 int thick = face->box_line_width;
25690 if (thick > 0)
25692 it->ascent += thick;
25693 it->descent += thick;
25695 else
25696 thick = - thick;
25698 if (it->start_of_box_run_p)
25699 it->pixel_width += thick;
25700 if (it->end_of_box_run_p)
25701 it->pixel_width += thick;
25703 /* If face has an overline, add the height of the overline
25704 (1 pixel) and a 1 pixel margin to the character height. */
25705 if (face->overline_p)
25706 it->ascent += overline_margin;
25707 take_vertical_position_into_account (it);
25708 if (it->ascent < 0)
25709 it->ascent = 0;
25710 if (it->descent < 0)
25711 it->descent = 0;
25713 if (it->glyph_row)
25714 append_composite_glyph (it);
25716 else if (it->what == IT_GLYPHLESS)
25717 produce_glyphless_glyph (it, 0, Qnil);
25718 else if (it->what == IT_IMAGE)
25719 produce_image_glyph (it);
25720 else if (it->what == IT_STRETCH)
25721 produce_stretch_glyph (it);
25723 done:
25724 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25725 because this isn't true for images with `:ascent 100'. */
25726 eassert (it->ascent >= 0 && it->descent >= 0);
25727 if (it->area == TEXT_AREA)
25728 it->current_x += it->pixel_width;
25730 if (extra_line_spacing > 0)
25732 it->descent += extra_line_spacing;
25733 if (extra_line_spacing > it->max_extra_line_spacing)
25734 it->max_extra_line_spacing = extra_line_spacing;
25737 it->max_ascent = max (it->max_ascent, it->ascent);
25738 it->max_descent = max (it->max_descent, it->descent);
25739 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25740 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25743 /* EXPORT for RIF:
25744 Output LEN glyphs starting at START at the nominal cursor position.
25745 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25746 being updated, and UPDATED_AREA is the area of that row being updated. */
25748 void
25749 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25750 struct glyph *start, enum glyph_row_area updated_area, int len)
25752 int x, hpos, chpos = w->phys_cursor.hpos;
25754 eassert (updated_row);
25755 /* When the window is hscrolled, cursor hpos can legitimately be out
25756 of bounds, but we draw the cursor at the corresponding window
25757 margin in that case. */
25758 if (!updated_row->reversed_p && chpos < 0)
25759 chpos = 0;
25760 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25761 chpos = updated_row->used[TEXT_AREA] - 1;
25763 block_input ();
25765 /* Write glyphs. */
25767 hpos = start - updated_row->glyphs[updated_area];
25768 x = draw_glyphs (w, w->output_cursor.x,
25769 updated_row, updated_area,
25770 hpos, hpos + len,
25771 DRAW_NORMAL_TEXT, 0);
25773 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25774 if (updated_area == TEXT_AREA
25775 && w->phys_cursor_on_p
25776 && w->phys_cursor.vpos == w->output_cursor.vpos
25777 && chpos >= hpos
25778 && chpos < hpos + len)
25779 w->phys_cursor_on_p = 0;
25781 unblock_input ();
25783 /* Advance the output cursor. */
25784 w->output_cursor.hpos += len;
25785 w->output_cursor.x = x;
25789 /* EXPORT for RIF:
25790 Insert LEN glyphs from START at the nominal cursor position. */
25792 void
25793 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25794 struct glyph *start, enum glyph_row_area updated_area, int len)
25796 struct frame *f;
25797 int line_height, shift_by_width, shifted_region_width;
25798 struct glyph_row *row;
25799 struct glyph *glyph;
25800 int frame_x, frame_y;
25801 ptrdiff_t hpos;
25803 eassert (updated_row);
25804 block_input ();
25805 f = XFRAME (WINDOW_FRAME (w));
25807 /* Get the height of the line we are in. */
25808 row = updated_row;
25809 line_height = row->height;
25811 /* Get the width of the glyphs to insert. */
25812 shift_by_width = 0;
25813 for (glyph = start; glyph < start + len; ++glyph)
25814 shift_by_width += glyph->pixel_width;
25816 /* Get the width of the region to shift right. */
25817 shifted_region_width = (window_box_width (w, updated_area)
25818 - w->output_cursor.x
25819 - shift_by_width);
25821 /* Shift right. */
25822 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25823 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25825 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25826 line_height, shift_by_width);
25828 /* Write the glyphs. */
25829 hpos = start - row->glyphs[updated_area];
25830 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25831 hpos, hpos + len,
25832 DRAW_NORMAL_TEXT, 0);
25834 /* Advance the output cursor. */
25835 w->output_cursor.hpos += len;
25836 w->output_cursor.x += shift_by_width;
25837 unblock_input ();
25841 /* EXPORT for RIF:
25842 Erase the current text line from the nominal cursor position
25843 (inclusive) to pixel column TO_X (exclusive). The idea is that
25844 everything from TO_X onward is already erased.
25846 TO_X is a pixel position relative to UPDATED_AREA of currently
25847 updated window W. TO_X == -1 means clear to the end of this area. */
25849 void
25850 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25851 enum glyph_row_area updated_area, int to_x)
25853 struct frame *f;
25854 int max_x, min_y, max_y;
25855 int from_x, from_y, to_y;
25857 eassert (updated_row);
25858 f = XFRAME (w->frame);
25860 if (updated_row->full_width_p)
25861 max_x = WINDOW_TOTAL_WIDTH (w);
25862 else
25863 max_x = window_box_width (w, updated_area);
25864 max_y = window_text_bottom_y (w);
25866 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25867 of window. For TO_X > 0, truncate to end of drawing area. */
25868 if (to_x == 0)
25869 return;
25870 else if (to_x < 0)
25871 to_x = max_x;
25872 else
25873 to_x = min (to_x, max_x);
25875 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25877 /* Notice if the cursor will be cleared by this operation. */
25878 if (!updated_row->full_width_p)
25879 notice_overwritten_cursor (w, updated_area,
25880 w->output_cursor.x, -1,
25881 updated_row->y,
25882 MATRIX_ROW_BOTTOM_Y (updated_row));
25884 from_x = w->output_cursor.x;
25886 /* Translate to frame coordinates. */
25887 if (updated_row->full_width_p)
25889 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25890 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25892 else
25894 int area_left = window_box_left (w, updated_area);
25895 from_x += area_left;
25896 to_x += area_left;
25899 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25900 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25901 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25903 /* Prevent inadvertently clearing to end of the X window. */
25904 if (to_x > from_x && to_y > from_y)
25906 block_input ();
25907 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25908 to_x - from_x, to_y - from_y);
25909 unblock_input ();
25913 #endif /* HAVE_WINDOW_SYSTEM */
25917 /***********************************************************************
25918 Cursor types
25919 ***********************************************************************/
25921 /* Value is the internal representation of the specified cursor type
25922 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25923 of the bar cursor. */
25925 static enum text_cursor_kinds
25926 get_specified_cursor_type (Lisp_Object arg, int *width)
25928 enum text_cursor_kinds type;
25930 if (NILP (arg))
25931 return NO_CURSOR;
25933 if (EQ (arg, Qbox))
25934 return FILLED_BOX_CURSOR;
25936 if (EQ (arg, Qhollow))
25937 return HOLLOW_BOX_CURSOR;
25939 if (EQ (arg, Qbar))
25941 *width = 2;
25942 return BAR_CURSOR;
25945 if (CONSP (arg)
25946 && EQ (XCAR (arg), Qbar)
25947 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25949 *width = XINT (XCDR (arg));
25950 return BAR_CURSOR;
25953 if (EQ (arg, Qhbar))
25955 *width = 2;
25956 return HBAR_CURSOR;
25959 if (CONSP (arg)
25960 && EQ (XCAR (arg), Qhbar)
25961 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25963 *width = XINT (XCDR (arg));
25964 return HBAR_CURSOR;
25967 /* Treat anything unknown as "hollow box cursor".
25968 It was bad to signal an error; people have trouble fixing
25969 .Xdefaults with Emacs, when it has something bad in it. */
25970 type = HOLLOW_BOX_CURSOR;
25972 return type;
25975 /* Set the default cursor types for specified frame. */
25976 void
25977 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25979 int width = 1;
25980 Lisp_Object tem;
25982 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25983 FRAME_CURSOR_WIDTH (f) = width;
25985 /* By default, set up the blink-off state depending on the on-state. */
25987 tem = Fassoc (arg, Vblink_cursor_alist);
25988 if (!NILP (tem))
25990 FRAME_BLINK_OFF_CURSOR (f)
25991 = get_specified_cursor_type (XCDR (tem), &width);
25992 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25994 else
25995 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25997 /* Make sure the cursor gets redrawn. */
25998 f->cursor_type_changed = 1;
26002 #ifdef HAVE_WINDOW_SYSTEM
26004 /* Return the cursor we want to be displayed in window W. Return
26005 width of bar/hbar cursor through WIDTH arg. Return with
26006 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26007 (i.e. if the `system caret' should track this cursor).
26009 In a mini-buffer window, we want the cursor only to appear if we
26010 are reading input from this window. For the selected window, we
26011 want the cursor type given by the frame parameter or buffer local
26012 setting of cursor-type. If explicitly marked off, draw no cursor.
26013 In all other cases, we want a hollow box cursor. */
26015 static enum text_cursor_kinds
26016 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26017 int *active_cursor)
26019 struct frame *f = XFRAME (w->frame);
26020 struct buffer *b = XBUFFER (w->contents);
26021 int cursor_type = DEFAULT_CURSOR;
26022 Lisp_Object alt_cursor;
26023 int non_selected = 0;
26025 *active_cursor = 1;
26027 /* Echo area */
26028 if (cursor_in_echo_area
26029 && FRAME_HAS_MINIBUF_P (f)
26030 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26032 if (w == XWINDOW (echo_area_window))
26034 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26036 *width = FRAME_CURSOR_WIDTH (f);
26037 return FRAME_DESIRED_CURSOR (f);
26039 else
26040 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26043 *active_cursor = 0;
26044 non_selected = 1;
26047 /* Detect a nonselected window or nonselected frame. */
26048 else if (w != XWINDOW (f->selected_window)
26049 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26051 *active_cursor = 0;
26053 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26054 return NO_CURSOR;
26056 non_selected = 1;
26059 /* Never display a cursor in a window in which cursor-type is nil. */
26060 if (NILP (BVAR (b, cursor_type)))
26061 return NO_CURSOR;
26063 /* Get the normal cursor type for this window. */
26064 if (EQ (BVAR (b, cursor_type), Qt))
26066 cursor_type = FRAME_DESIRED_CURSOR (f);
26067 *width = FRAME_CURSOR_WIDTH (f);
26069 else
26070 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26072 /* Use cursor-in-non-selected-windows instead
26073 for non-selected window or frame. */
26074 if (non_selected)
26076 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26077 if (!EQ (Qt, alt_cursor))
26078 return get_specified_cursor_type (alt_cursor, width);
26079 /* t means modify the normal cursor type. */
26080 if (cursor_type == FILLED_BOX_CURSOR)
26081 cursor_type = HOLLOW_BOX_CURSOR;
26082 else if (cursor_type == BAR_CURSOR && *width > 1)
26083 --*width;
26084 return cursor_type;
26087 /* Use normal cursor if not blinked off. */
26088 if (!w->cursor_off_p)
26090 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26092 if (cursor_type == FILLED_BOX_CURSOR)
26094 /* Using a block cursor on large images can be very annoying.
26095 So use a hollow cursor for "large" images.
26096 If image is not transparent (no mask), also use hollow cursor. */
26097 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26098 if (img != NULL && IMAGEP (img->spec))
26100 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26101 where N = size of default frame font size.
26102 This should cover most of the "tiny" icons people may use. */
26103 if (!img->mask
26104 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26105 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26106 cursor_type = HOLLOW_BOX_CURSOR;
26109 else if (cursor_type != NO_CURSOR)
26111 /* Display current only supports BOX and HOLLOW cursors for images.
26112 So for now, unconditionally use a HOLLOW cursor when cursor is
26113 not a solid box cursor. */
26114 cursor_type = HOLLOW_BOX_CURSOR;
26117 return cursor_type;
26120 /* Cursor is blinked off, so determine how to "toggle" it. */
26122 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26123 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26124 return get_specified_cursor_type (XCDR (alt_cursor), width);
26126 /* Then see if frame has specified a specific blink off cursor type. */
26127 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26129 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26130 return FRAME_BLINK_OFF_CURSOR (f);
26133 #if 0
26134 /* Some people liked having a permanently visible blinking cursor,
26135 while others had very strong opinions against it. So it was
26136 decided to remove it. KFS 2003-09-03 */
26138 /* Finally perform built-in cursor blinking:
26139 filled box <-> hollow box
26140 wide [h]bar <-> narrow [h]bar
26141 narrow [h]bar <-> no cursor
26142 other type <-> no cursor */
26144 if (cursor_type == FILLED_BOX_CURSOR)
26145 return HOLLOW_BOX_CURSOR;
26147 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26149 *width = 1;
26150 return cursor_type;
26152 #endif
26154 return NO_CURSOR;
26158 /* Notice when the text cursor of window W has been completely
26159 overwritten by a drawing operation that outputs glyphs in AREA
26160 starting at X0 and ending at X1 in the line starting at Y0 and
26161 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26162 the rest of the line after X0 has been written. Y coordinates
26163 are window-relative. */
26165 static void
26166 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26167 int x0, int x1, int y0, int y1)
26169 int cx0, cx1, cy0, cy1;
26170 struct glyph_row *row;
26172 if (!w->phys_cursor_on_p)
26173 return;
26174 if (area != TEXT_AREA)
26175 return;
26177 if (w->phys_cursor.vpos < 0
26178 || w->phys_cursor.vpos >= w->current_matrix->nrows
26179 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26180 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26181 return;
26183 if (row->cursor_in_fringe_p)
26185 row->cursor_in_fringe_p = 0;
26186 draw_fringe_bitmap (w, row, row->reversed_p);
26187 w->phys_cursor_on_p = 0;
26188 return;
26191 cx0 = w->phys_cursor.x;
26192 cx1 = cx0 + w->phys_cursor_width;
26193 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26194 return;
26196 /* The cursor image will be completely removed from the
26197 screen if the output area intersects the cursor area in
26198 y-direction. When we draw in [y0 y1[, and some part of
26199 the cursor is at y < y0, that part must have been drawn
26200 before. When scrolling, the cursor is erased before
26201 actually scrolling, so we don't come here. When not
26202 scrolling, the rows above the old cursor row must have
26203 changed, and in this case these rows must have written
26204 over the cursor image.
26206 Likewise if part of the cursor is below y1, with the
26207 exception of the cursor being in the first blank row at
26208 the buffer and window end because update_text_area
26209 doesn't draw that row. (Except when it does, but
26210 that's handled in update_text_area.) */
26212 cy0 = w->phys_cursor.y;
26213 cy1 = cy0 + w->phys_cursor_height;
26214 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26215 return;
26217 w->phys_cursor_on_p = 0;
26220 #endif /* HAVE_WINDOW_SYSTEM */
26223 /************************************************************************
26224 Mouse Face
26225 ************************************************************************/
26227 #ifdef HAVE_WINDOW_SYSTEM
26229 /* EXPORT for RIF:
26230 Fix the display of area AREA of overlapping row ROW in window W
26231 with respect to the overlapping part OVERLAPS. */
26233 void
26234 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26235 enum glyph_row_area area, int overlaps)
26237 int i, x;
26239 block_input ();
26241 x = 0;
26242 for (i = 0; i < row->used[area];)
26244 if (row->glyphs[area][i].overlaps_vertically_p)
26246 int start = i, start_x = x;
26250 x += row->glyphs[area][i].pixel_width;
26251 ++i;
26253 while (i < row->used[area]
26254 && row->glyphs[area][i].overlaps_vertically_p);
26256 draw_glyphs (w, start_x, row, area,
26257 start, i,
26258 DRAW_NORMAL_TEXT, overlaps);
26260 else
26262 x += row->glyphs[area][i].pixel_width;
26263 ++i;
26267 unblock_input ();
26271 /* EXPORT:
26272 Draw the cursor glyph of window W in glyph row ROW. See the
26273 comment of draw_glyphs for the meaning of HL. */
26275 void
26276 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26277 enum draw_glyphs_face hl)
26279 /* If cursor hpos is out of bounds, don't draw garbage. This can
26280 happen in mini-buffer windows when switching between echo area
26281 glyphs and mini-buffer. */
26282 if ((row->reversed_p
26283 ? (w->phys_cursor.hpos >= 0)
26284 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26286 int on_p = w->phys_cursor_on_p;
26287 int x1;
26288 int hpos = w->phys_cursor.hpos;
26290 /* When the window is hscrolled, cursor hpos can legitimately be
26291 out of bounds, but we draw the cursor at the corresponding
26292 window margin in that case. */
26293 if (!row->reversed_p && hpos < 0)
26294 hpos = 0;
26295 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26296 hpos = row->used[TEXT_AREA] - 1;
26298 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26299 hl, 0);
26300 w->phys_cursor_on_p = on_p;
26302 if (hl == DRAW_CURSOR)
26303 w->phys_cursor_width = x1 - w->phys_cursor.x;
26304 /* When we erase the cursor, and ROW is overlapped by other
26305 rows, make sure that these overlapping parts of other rows
26306 are redrawn. */
26307 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26309 w->phys_cursor_width = x1 - w->phys_cursor.x;
26311 if (row > w->current_matrix->rows
26312 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26313 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26314 OVERLAPS_ERASED_CURSOR);
26316 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26317 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26318 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26319 OVERLAPS_ERASED_CURSOR);
26325 /* Erase the image of a cursor of window W from the screen. */
26327 #ifndef WINDOWSNT
26328 static
26329 #endif
26330 void
26331 erase_phys_cursor (struct window *w)
26333 struct frame *f = XFRAME (w->frame);
26334 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26335 int hpos = w->phys_cursor.hpos;
26336 int vpos = w->phys_cursor.vpos;
26337 int mouse_face_here_p = 0;
26338 struct glyph_matrix *active_glyphs = w->current_matrix;
26339 struct glyph_row *cursor_row;
26340 struct glyph *cursor_glyph;
26341 enum draw_glyphs_face hl;
26343 /* No cursor displayed or row invalidated => nothing to do on the
26344 screen. */
26345 if (w->phys_cursor_type == NO_CURSOR)
26346 goto mark_cursor_off;
26348 /* VPOS >= active_glyphs->nrows means that window has been resized.
26349 Don't bother to erase the cursor. */
26350 if (vpos >= active_glyphs->nrows)
26351 goto mark_cursor_off;
26353 /* If row containing cursor is marked invalid, there is nothing we
26354 can do. */
26355 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26356 if (!cursor_row->enabled_p)
26357 goto mark_cursor_off;
26359 /* If line spacing is > 0, old cursor may only be partially visible in
26360 window after split-window. So adjust visible height. */
26361 cursor_row->visible_height = min (cursor_row->visible_height,
26362 window_text_bottom_y (w) - cursor_row->y);
26364 /* If row is completely invisible, don't attempt to delete a cursor which
26365 isn't there. This can happen if cursor is at top of a window, and
26366 we switch to a buffer with a header line in that window. */
26367 if (cursor_row->visible_height <= 0)
26368 goto mark_cursor_off;
26370 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26371 if (cursor_row->cursor_in_fringe_p)
26373 cursor_row->cursor_in_fringe_p = 0;
26374 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26375 goto mark_cursor_off;
26378 /* This can happen when the new row is shorter than the old one.
26379 In this case, either draw_glyphs or clear_end_of_line
26380 should have cleared the cursor. Note that we wouldn't be
26381 able to erase the cursor in this case because we don't have a
26382 cursor glyph at hand. */
26383 if ((cursor_row->reversed_p
26384 ? (w->phys_cursor.hpos < 0)
26385 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26386 goto mark_cursor_off;
26388 /* When the window is hscrolled, cursor hpos can legitimately be out
26389 of bounds, but we draw the cursor at the corresponding window
26390 margin in that case. */
26391 if (!cursor_row->reversed_p && hpos < 0)
26392 hpos = 0;
26393 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26394 hpos = cursor_row->used[TEXT_AREA] - 1;
26396 /* If the cursor is in the mouse face area, redisplay that when
26397 we clear the cursor. */
26398 if (! NILP (hlinfo->mouse_face_window)
26399 && coords_in_mouse_face_p (w, hpos, vpos)
26400 /* Don't redraw the cursor's spot in mouse face if it is at the
26401 end of a line (on a newline). The cursor appears there, but
26402 mouse highlighting does not. */
26403 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26404 mouse_face_here_p = 1;
26406 /* Maybe clear the display under the cursor. */
26407 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26409 int x, y, left_x;
26410 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26411 int width;
26413 cursor_glyph = get_phys_cursor_glyph (w);
26414 if (cursor_glyph == NULL)
26415 goto mark_cursor_off;
26417 width = cursor_glyph->pixel_width;
26418 left_x = window_box_left_offset (w, TEXT_AREA);
26419 x = w->phys_cursor.x;
26420 if (x < left_x)
26421 width -= left_x - x;
26422 width = min (width, window_box_width (w, TEXT_AREA) - x);
26423 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26424 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26426 if (width > 0)
26427 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26430 /* Erase the cursor by redrawing the character underneath it. */
26431 if (mouse_face_here_p)
26432 hl = DRAW_MOUSE_FACE;
26433 else
26434 hl = DRAW_NORMAL_TEXT;
26435 draw_phys_cursor_glyph (w, cursor_row, hl);
26437 mark_cursor_off:
26438 w->phys_cursor_on_p = 0;
26439 w->phys_cursor_type = NO_CURSOR;
26443 /* EXPORT:
26444 Display or clear cursor of window W. If ON is zero, clear the
26445 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26446 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26448 void
26449 display_and_set_cursor (struct window *w, bool on,
26450 int hpos, int vpos, int x, int y)
26452 struct frame *f = XFRAME (w->frame);
26453 int new_cursor_type;
26454 int new_cursor_width;
26455 int active_cursor;
26456 struct glyph_row *glyph_row;
26457 struct glyph *glyph;
26459 /* This is pointless on invisible frames, and dangerous on garbaged
26460 windows and frames; in the latter case, the frame or window may
26461 be in the midst of changing its size, and x and y may be off the
26462 window. */
26463 if (! FRAME_VISIBLE_P (f)
26464 || FRAME_GARBAGED_P (f)
26465 || vpos >= w->current_matrix->nrows
26466 || hpos >= w->current_matrix->matrix_w)
26467 return;
26469 /* If cursor is off and we want it off, return quickly. */
26470 if (!on && !w->phys_cursor_on_p)
26471 return;
26473 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26474 /* If cursor row is not enabled, we don't really know where to
26475 display the cursor. */
26476 if (!glyph_row->enabled_p)
26478 w->phys_cursor_on_p = 0;
26479 return;
26482 glyph = NULL;
26483 if (!glyph_row->exact_window_width_line_p
26484 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26485 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26487 eassert (input_blocked_p ());
26489 /* Set new_cursor_type to the cursor we want to be displayed. */
26490 new_cursor_type = get_window_cursor_type (w, glyph,
26491 &new_cursor_width, &active_cursor);
26493 /* If cursor is currently being shown and we don't want it to be or
26494 it is in the wrong place, or the cursor type is not what we want,
26495 erase it. */
26496 if (w->phys_cursor_on_p
26497 && (!on
26498 || w->phys_cursor.x != x
26499 || w->phys_cursor.y != y
26500 || new_cursor_type != w->phys_cursor_type
26501 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26502 && new_cursor_width != w->phys_cursor_width)))
26503 erase_phys_cursor (w);
26505 /* Don't check phys_cursor_on_p here because that flag is only set
26506 to zero in some cases where we know that the cursor has been
26507 completely erased, to avoid the extra work of erasing the cursor
26508 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26509 still not be visible, or it has only been partly erased. */
26510 if (on)
26512 w->phys_cursor_ascent = glyph_row->ascent;
26513 w->phys_cursor_height = glyph_row->height;
26515 /* Set phys_cursor_.* before x_draw_.* is called because some
26516 of them may need the information. */
26517 w->phys_cursor.x = x;
26518 w->phys_cursor.y = glyph_row->y;
26519 w->phys_cursor.hpos = hpos;
26520 w->phys_cursor.vpos = vpos;
26523 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26524 new_cursor_type, new_cursor_width,
26525 on, active_cursor);
26529 /* Switch the display of W's cursor on or off, according to the value
26530 of ON. */
26532 static void
26533 update_window_cursor (struct window *w, bool on)
26535 /* Don't update cursor in windows whose frame is in the process
26536 of being deleted. */
26537 if (w->current_matrix)
26539 int hpos = w->phys_cursor.hpos;
26540 int vpos = w->phys_cursor.vpos;
26541 struct glyph_row *row;
26543 if (vpos >= w->current_matrix->nrows
26544 || hpos >= w->current_matrix->matrix_w)
26545 return;
26547 row = MATRIX_ROW (w->current_matrix, vpos);
26549 /* When the window is hscrolled, cursor hpos can legitimately be
26550 out of bounds, but we draw the cursor at the corresponding
26551 window margin in that case. */
26552 if (!row->reversed_p && hpos < 0)
26553 hpos = 0;
26554 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26555 hpos = row->used[TEXT_AREA] - 1;
26557 block_input ();
26558 display_and_set_cursor (w, on, hpos, vpos,
26559 w->phys_cursor.x, w->phys_cursor.y);
26560 unblock_input ();
26565 /* Call update_window_cursor with parameter ON_P on all leaf windows
26566 in the window tree rooted at W. */
26568 static void
26569 update_cursor_in_window_tree (struct window *w, bool on_p)
26571 while (w)
26573 if (WINDOWP (w->contents))
26574 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26575 else
26576 update_window_cursor (w, on_p);
26578 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26583 /* EXPORT:
26584 Display the cursor on window W, or clear it, according to ON_P.
26585 Don't change the cursor's position. */
26587 void
26588 x_update_cursor (struct frame *f, bool on_p)
26590 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26594 /* EXPORT:
26595 Clear the cursor of window W to background color, and mark the
26596 cursor as not shown. This is used when the text where the cursor
26597 is about to be rewritten. */
26599 void
26600 x_clear_cursor (struct window *w)
26602 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26603 update_window_cursor (w, 0);
26606 #endif /* HAVE_WINDOW_SYSTEM */
26608 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26609 and MSDOS. */
26610 static void
26611 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26612 int start_hpos, int end_hpos,
26613 enum draw_glyphs_face draw)
26615 #ifdef HAVE_WINDOW_SYSTEM
26616 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26618 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26619 return;
26621 #endif
26622 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26623 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26624 #endif
26627 /* Display the active region described by mouse_face_* according to DRAW. */
26629 static void
26630 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26632 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26633 struct frame *f = XFRAME (WINDOW_FRAME (w));
26635 if (/* If window is in the process of being destroyed, don't bother
26636 to do anything. */
26637 w->current_matrix != NULL
26638 /* Don't update mouse highlight if hidden */
26639 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26640 /* Recognize when we are called to operate on rows that don't exist
26641 anymore. This can happen when a window is split. */
26642 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26644 int phys_cursor_on_p = w->phys_cursor_on_p;
26645 struct glyph_row *row, *first, *last;
26647 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26648 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26650 for (row = first; row <= last && row->enabled_p; ++row)
26652 int start_hpos, end_hpos, start_x;
26654 /* For all but the first row, the highlight starts at column 0. */
26655 if (row == first)
26657 /* R2L rows have BEG and END in reversed order, but the
26658 screen drawing geometry is always left to right. So
26659 we need to mirror the beginning and end of the
26660 highlighted area in R2L rows. */
26661 if (!row->reversed_p)
26663 start_hpos = hlinfo->mouse_face_beg_col;
26664 start_x = hlinfo->mouse_face_beg_x;
26666 else if (row == last)
26668 start_hpos = hlinfo->mouse_face_end_col;
26669 start_x = hlinfo->mouse_face_end_x;
26671 else
26673 start_hpos = 0;
26674 start_x = 0;
26677 else if (row->reversed_p && row == last)
26679 start_hpos = hlinfo->mouse_face_end_col;
26680 start_x = hlinfo->mouse_face_end_x;
26682 else
26684 start_hpos = 0;
26685 start_x = 0;
26688 if (row == last)
26690 if (!row->reversed_p)
26691 end_hpos = hlinfo->mouse_face_end_col;
26692 else if (row == first)
26693 end_hpos = hlinfo->mouse_face_beg_col;
26694 else
26696 end_hpos = row->used[TEXT_AREA];
26697 if (draw == DRAW_NORMAL_TEXT)
26698 row->fill_line_p = 1; /* Clear to end of line */
26701 else if (row->reversed_p && row == first)
26702 end_hpos = hlinfo->mouse_face_beg_col;
26703 else
26705 end_hpos = row->used[TEXT_AREA];
26706 if (draw == DRAW_NORMAL_TEXT)
26707 row->fill_line_p = 1; /* Clear to end of line */
26710 if (end_hpos > start_hpos)
26712 draw_row_with_mouse_face (w, start_x, row,
26713 start_hpos, end_hpos, draw);
26715 row->mouse_face_p
26716 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26720 #ifdef HAVE_WINDOW_SYSTEM
26721 /* When we've written over the cursor, arrange for it to
26722 be displayed again. */
26723 if (FRAME_WINDOW_P (f)
26724 && phys_cursor_on_p && !w->phys_cursor_on_p)
26726 int hpos = w->phys_cursor.hpos;
26728 /* When the window is hscrolled, cursor hpos can legitimately be
26729 out of bounds, but we draw the cursor at the corresponding
26730 window margin in that case. */
26731 if (!row->reversed_p && hpos < 0)
26732 hpos = 0;
26733 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26734 hpos = row->used[TEXT_AREA] - 1;
26736 block_input ();
26737 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26738 w->phys_cursor.x, w->phys_cursor.y);
26739 unblock_input ();
26741 #endif /* HAVE_WINDOW_SYSTEM */
26744 #ifdef HAVE_WINDOW_SYSTEM
26745 /* Change the mouse cursor. */
26746 if (FRAME_WINDOW_P (f))
26748 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26749 if (draw == DRAW_NORMAL_TEXT
26750 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26751 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26752 else
26753 #endif
26754 if (draw == DRAW_MOUSE_FACE)
26755 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26756 else
26757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26759 #endif /* HAVE_WINDOW_SYSTEM */
26762 /* EXPORT:
26763 Clear out the mouse-highlighted active region.
26764 Redraw it un-highlighted first. Value is non-zero if mouse
26765 face was actually drawn unhighlighted. */
26768 clear_mouse_face (Mouse_HLInfo *hlinfo)
26770 int cleared = 0;
26772 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26774 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26775 cleared = 1;
26778 reset_mouse_highlight (hlinfo);
26779 return cleared;
26782 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26783 within the mouse face on that window. */
26784 static int
26785 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26787 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26789 /* Quickly resolve the easy cases. */
26790 if (!(WINDOWP (hlinfo->mouse_face_window)
26791 && XWINDOW (hlinfo->mouse_face_window) == w))
26792 return 0;
26793 if (vpos < hlinfo->mouse_face_beg_row
26794 || vpos > hlinfo->mouse_face_end_row)
26795 return 0;
26796 if (vpos > hlinfo->mouse_face_beg_row
26797 && vpos < hlinfo->mouse_face_end_row)
26798 return 1;
26800 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26802 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26804 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26805 return 1;
26807 else if ((vpos == hlinfo->mouse_face_beg_row
26808 && hpos >= hlinfo->mouse_face_beg_col)
26809 || (vpos == hlinfo->mouse_face_end_row
26810 && hpos < hlinfo->mouse_face_end_col))
26811 return 1;
26813 else
26815 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26817 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26818 return 1;
26820 else if ((vpos == hlinfo->mouse_face_beg_row
26821 && hpos <= hlinfo->mouse_face_beg_col)
26822 || (vpos == hlinfo->mouse_face_end_row
26823 && hpos > hlinfo->mouse_face_end_col))
26824 return 1;
26826 return 0;
26830 /* EXPORT:
26831 Non-zero if physical cursor of window W is within mouse face. */
26834 cursor_in_mouse_face_p (struct window *w)
26836 int hpos = w->phys_cursor.hpos;
26837 int vpos = w->phys_cursor.vpos;
26838 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26840 /* When the window is hscrolled, cursor hpos can legitimately be out
26841 of bounds, but we draw the cursor at the corresponding window
26842 margin in that case. */
26843 if (!row->reversed_p && hpos < 0)
26844 hpos = 0;
26845 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26846 hpos = row->used[TEXT_AREA] - 1;
26848 return coords_in_mouse_face_p (w, hpos, vpos);
26853 /* Find the glyph rows START_ROW and END_ROW of window W that display
26854 characters between buffer positions START_CHARPOS and END_CHARPOS
26855 (excluding END_CHARPOS). DISP_STRING is a display string that
26856 covers these buffer positions. This is similar to
26857 row_containing_pos, but is more accurate when bidi reordering makes
26858 buffer positions change non-linearly with glyph rows. */
26859 static void
26860 rows_from_pos_range (struct window *w,
26861 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26862 Lisp_Object disp_string,
26863 struct glyph_row **start, struct glyph_row **end)
26865 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26866 int last_y = window_text_bottom_y (w);
26867 struct glyph_row *row;
26869 *start = NULL;
26870 *end = NULL;
26872 while (!first->enabled_p
26873 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26874 first++;
26876 /* Find the START row. */
26877 for (row = first;
26878 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26879 row++)
26881 /* A row can potentially be the START row if the range of the
26882 characters it displays intersects the range
26883 [START_CHARPOS..END_CHARPOS). */
26884 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26885 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26886 /* See the commentary in row_containing_pos, for the
26887 explanation of the complicated way to check whether
26888 some position is beyond the end of the characters
26889 displayed by a row. */
26890 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26891 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26892 && !row->ends_at_zv_p
26893 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26894 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26895 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26896 && !row->ends_at_zv_p
26897 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26899 /* Found a candidate row. Now make sure at least one of the
26900 glyphs it displays has a charpos from the range
26901 [START_CHARPOS..END_CHARPOS).
26903 This is not obvious because bidi reordering could make
26904 buffer positions of a row be 1,2,3,102,101,100, and if we
26905 want to highlight characters in [50..60), we don't want
26906 this row, even though [50..60) does intersect [1..103),
26907 the range of character positions given by the row's start
26908 and end positions. */
26909 struct glyph *g = row->glyphs[TEXT_AREA];
26910 struct glyph *e = g + row->used[TEXT_AREA];
26912 while (g < e)
26914 if (((BUFFERP (g->object) || INTEGERP (g->object))
26915 && start_charpos <= g->charpos && g->charpos < end_charpos)
26916 /* A glyph that comes from DISP_STRING is by
26917 definition to be highlighted. */
26918 || EQ (g->object, disp_string))
26919 *start = row;
26920 g++;
26922 if (*start)
26923 break;
26927 /* Find the END row. */
26928 if (!*start
26929 /* If the last row is partially visible, start looking for END
26930 from that row, instead of starting from FIRST. */
26931 && !(row->enabled_p
26932 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26933 row = first;
26934 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26936 struct glyph_row *next = row + 1;
26937 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26939 if (!next->enabled_p
26940 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26941 /* The first row >= START whose range of displayed characters
26942 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26943 is the row END + 1. */
26944 || (start_charpos < next_start
26945 && end_charpos < next_start)
26946 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26947 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26948 && !next->ends_at_zv_p
26949 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26950 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26951 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26952 && !next->ends_at_zv_p
26953 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26955 *end = row;
26956 break;
26958 else
26960 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26961 but none of the characters it displays are in the range, it is
26962 also END + 1. */
26963 struct glyph *g = next->glyphs[TEXT_AREA];
26964 struct glyph *s = g;
26965 struct glyph *e = g + next->used[TEXT_AREA];
26967 while (g < e)
26969 if (((BUFFERP (g->object) || INTEGERP (g->object))
26970 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26971 /* If the buffer position of the first glyph in
26972 the row is equal to END_CHARPOS, it means
26973 the last character to be highlighted is the
26974 newline of ROW, and we must consider NEXT as
26975 END, not END+1. */
26976 || (((!next->reversed_p && g == s)
26977 || (next->reversed_p && g == e - 1))
26978 && (g->charpos == end_charpos
26979 /* Special case for when NEXT is an
26980 empty line at ZV. */
26981 || (g->charpos == -1
26982 && !row->ends_at_zv_p
26983 && next_start == end_charpos)))))
26984 /* A glyph that comes from DISP_STRING is by
26985 definition to be highlighted. */
26986 || EQ (g->object, disp_string))
26987 break;
26988 g++;
26990 if (g == e)
26992 *end = row;
26993 break;
26995 /* The first row that ends at ZV must be the last to be
26996 highlighted. */
26997 else if (next->ends_at_zv_p)
26999 *end = next;
27000 break;
27006 /* This function sets the mouse_face_* elements of HLINFO, assuming
27007 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27008 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27009 for the overlay or run of text properties specifying the mouse
27010 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27011 before-string and after-string that must also be highlighted.
27012 DISP_STRING, if non-nil, is a display string that may cover some
27013 or all of the highlighted text. */
27015 static void
27016 mouse_face_from_buffer_pos (Lisp_Object window,
27017 Mouse_HLInfo *hlinfo,
27018 ptrdiff_t mouse_charpos,
27019 ptrdiff_t start_charpos,
27020 ptrdiff_t end_charpos,
27021 Lisp_Object before_string,
27022 Lisp_Object after_string,
27023 Lisp_Object disp_string)
27025 struct window *w = XWINDOW (window);
27026 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27027 struct glyph_row *r1, *r2;
27028 struct glyph *glyph, *end;
27029 ptrdiff_t ignore, pos;
27030 int x;
27032 eassert (NILP (disp_string) || STRINGP (disp_string));
27033 eassert (NILP (before_string) || STRINGP (before_string));
27034 eassert (NILP (after_string) || STRINGP (after_string));
27036 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27037 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27038 if (r1 == NULL)
27039 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27040 /* If the before-string or display-string contains newlines,
27041 rows_from_pos_range skips to its last row. Move back. */
27042 if (!NILP (before_string) || !NILP (disp_string))
27044 struct glyph_row *prev;
27045 while ((prev = r1 - 1, prev >= first)
27046 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27047 && prev->used[TEXT_AREA] > 0)
27049 struct glyph *beg = prev->glyphs[TEXT_AREA];
27050 glyph = beg + prev->used[TEXT_AREA];
27051 while (--glyph >= beg && INTEGERP (glyph->object));
27052 if (glyph < beg
27053 || !(EQ (glyph->object, before_string)
27054 || EQ (glyph->object, disp_string)))
27055 break;
27056 r1 = prev;
27059 if (r2 == NULL)
27061 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27062 hlinfo->mouse_face_past_end = 1;
27064 else if (!NILP (after_string))
27066 /* If the after-string has newlines, advance to its last row. */
27067 struct glyph_row *next;
27068 struct glyph_row *last
27069 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27071 for (next = r2 + 1;
27072 next <= last
27073 && next->used[TEXT_AREA] > 0
27074 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27075 ++next)
27076 r2 = next;
27078 /* The rest of the display engine assumes that mouse_face_beg_row is
27079 either above mouse_face_end_row or identical to it. But with
27080 bidi-reordered continued lines, the row for START_CHARPOS could
27081 be below the row for END_CHARPOS. If so, swap the rows and store
27082 them in correct order. */
27083 if (r1->y > r2->y)
27085 struct glyph_row *tem = r2;
27087 r2 = r1;
27088 r1 = tem;
27091 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27092 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27094 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27095 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27096 could be anywhere in the row and in any order. The strategy
27097 below is to find the leftmost and the rightmost glyph that
27098 belongs to either of these 3 strings, or whose position is
27099 between START_CHARPOS and END_CHARPOS, and highlight all the
27100 glyphs between those two. This may cover more than just the text
27101 between START_CHARPOS and END_CHARPOS if the range of characters
27102 strides the bidi level boundary, e.g. if the beginning is in R2L
27103 text while the end is in L2R text or vice versa. */
27104 if (!r1->reversed_p)
27106 /* This row is in a left to right paragraph. Scan it left to
27107 right. */
27108 glyph = r1->glyphs[TEXT_AREA];
27109 end = glyph + r1->used[TEXT_AREA];
27110 x = r1->x;
27112 /* Skip truncation glyphs at the start of the glyph row. */
27113 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27114 for (; glyph < end
27115 && INTEGERP (glyph->object)
27116 && glyph->charpos < 0;
27117 ++glyph)
27118 x += glyph->pixel_width;
27120 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27121 or DISP_STRING, and the first glyph from buffer whose
27122 position is between START_CHARPOS and END_CHARPOS. */
27123 for (; glyph < end
27124 && !INTEGERP (glyph->object)
27125 && !EQ (glyph->object, disp_string)
27126 && !(BUFFERP (glyph->object)
27127 && (glyph->charpos >= start_charpos
27128 && glyph->charpos < end_charpos));
27129 ++glyph)
27131 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27132 are present at buffer positions between START_CHARPOS and
27133 END_CHARPOS, or if they come from an overlay. */
27134 if (EQ (glyph->object, before_string))
27136 pos = string_buffer_position (before_string,
27137 start_charpos);
27138 /* If pos == 0, it means before_string came from an
27139 overlay, not from a buffer position. */
27140 if (!pos || (pos >= start_charpos && pos < end_charpos))
27141 break;
27143 else if (EQ (glyph->object, after_string))
27145 pos = string_buffer_position (after_string, end_charpos);
27146 if (!pos || (pos >= start_charpos && pos < end_charpos))
27147 break;
27149 x += glyph->pixel_width;
27151 hlinfo->mouse_face_beg_x = x;
27152 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27154 else
27156 /* This row is in a right to left paragraph. Scan it right to
27157 left. */
27158 struct glyph *g;
27160 end = r1->glyphs[TEXT_AREA] - 1;
27161 glyph = end + r1->used[TEXT_AREA];
27163 /* Skip truncation glyphs at the start of the glyph row. */
27164 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27165 for (; glyph > end
27166 && INTEGERP (glyph->object)
27167 && glyph->charpos < 0;
27168 --glyph)
27171 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27172 or DISP_STRING, and the first glyph from buffer whose
27173 position is between START_CHARPOS and END_CHARPOS. */
27174 for (; glyph > end
27175 && !INTEGERP (glyph->object)
27176 && !EQ (glyph->object, disp_string)
27177 && !(BUFFERP (glyph->object)
27178 && (glyph->charpos >= start_charpos
27179 && glyph->charpos < end_charpos));
27180 --glyph)
27182 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27183 are present at buffer positions between START_CHARPOS and
27184 END_CHARPOS, or if they come from an overlay. */
27185 if (EQ (glyph->object, before_string))
27187 pos = string_buffer_position (before_string, start_charpos);
27188 /* If pos == 0, it means before_string came from an
27189 overlay, not from a buffer position. */
27190 if (!pos || (pos >= start_charpos && pos < end_charpos))
27191 break;
27193 else if (EQ (glyph->object, after_string))
27195 pos = string_buffer_position (after_string, end_charpos);
27196 if (!pos || (pos >= start_charpos && pos < end_charpos))
27197 break;
27201 glyph++; /* first glyph to the right of the highlighted area */
27202 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27203 x += g->pixel_width;
27204 hlinfo->mouse_face_beg_x = x;
27205 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27208 /* If the highlight ends in a different row, compute GLYPH and END
27209 for the end row. Otherwise, reuse the values computed above for
27210 the row where the highlight begins. */
27211 if (r2 != r1)
27213 if (!r2->reversed_p)
27215 glyph = r2->glyphs[TEXT_AREA];
27216 end = glyph + r2->used[TEXT_AREA];
27217 x = r2->x;
27219 else
27221 end = r2->glyphs[TEXT_AREA] - 1;
27222 glyph = end + r2->used[TEXT_AREA];
27226 if (!r2->reversed_p)
27228 /* Skip truncation and continuation glyphs near the end of the
27229 row, and also blanks and stretch glyphs inserted by
27230 extend_face_to_end_of_line. */
27231 while (end > glyph
27232 && INTEGERP ((end - 1)->object))
27233 --end;
27234 /* Scan the rest of the glyph row from the end, looking for the
27235 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27236 DISP_STRING, or whose position is between START_CHARPOS
27237 and END_CHARPOS */
27238 for (--end;
27239 end > glyph
27240 && !INTEGERP (end->object)
27241 && !EQ (end->object, disp_string)
27242 && !(BUFFERP (end->object)
27243 && (end->charpos >= start_charpos
27244 && end->charpos < end_charpos));
27245 --end)
27247 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27248 are present at buffer positions between START_CHARPOS and
27249 END_CHARPOS, or if they come from an overlay. */
27250 if (EQ (end->object, before_string))
27252 pos = string_buffer_position (before_string, start_charpos);
27253 if (!pos || (pos >= start_charpos && pos < end_charpos))
27254 break;
27256 else if (EQ (end->object, after_string))
27258 pos = string_buffer_position (after_string, end_charpos);
27259 if (!pos || (pos >= start_charpos && pos < end_charpos))
27260 break;
27263 /* Find the X coordinate of the last glyph to be highlighted. */
27264 for (; glyph <= end; ++glyph)
27265 x += glyph->pixel_width;
27267 hlinfo->mouse_face_end_x = x;
27268 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27270 else
27272 /* Skip truncation and continuation glyphs near the end of the
27273 row, and also blanks and stretch glyphs inserted by
27274 extend_face_to_end_of_line. */
27275 x = r2->x;
27276 end++;
27277 while (end < glyph
27278 && INTEGERP (end->object))
27280 x += end->pixel_width;
27281 ++end;
27283 /* Scan the rest of the glyph row from the end, looking for the
27284 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27285 DISP_STRING, or whose position is between START_CHARPOS
27286 and END_CHARPOS */
27287 for ( ;
27288 end < glyph
27289 && !INTEGERP (end->object)
27290 && !EQ (end->object, disp_string)
27291 && !(BUFFERP (end->object)
27292 && (end->charpos >= start_charpos
27293 && end->charpos < end_charpos));
27294 ++end)
27296 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27297 are present at buffer positions between START_CHARPOS and
27298 END_CHARPOS, or if they come from an overlay. */
27299 if (EQ (end->object, before_string))
27301 pos = string_buffer_position (before_string, start_charpos);
27302 if (!pos || (pos >= start_charpos && pos < end_charpos))
27303 break;
27305 else if (EQ (end->object, after_string))
27307 pos = string_buffer_position (after_string, end_charpos);
27308 if (!pos || (pos >= start_charpos && pos < end_charpos))
27309 break;
27311 x += end->pixel_width;
27313 /* If we exited the above loop because we arrived at the last
27314 glyph of the row, and its buffer position is still not in
27315 range, it means the last character in range is the preceding
27316 newline. Bump the end column and x values to get past the
27317 last glyph. */
27318 if (end == glyph
27319 && BUFFERP (end->object)
27320 && (end->charpos < start_charpos
27321 || end->charpos >= end_charpos))
27323 x += end->pixel_width;
27324 ++end;
27326 hlinfo->mouse_face_end_x = x;
27327 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27330 hlinfo->mouse_face_window = window;
27331 hlinfo->mouse_face_face_id
27332 = face_at_buffer_position (w, mouse_charpos, &ignore,
27333 mouse_charpos + 1,
27334 !hlinfo->mouse_face_hidden, -1);
27335 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27338 /* The following function is not used anymore (replaced with
27339 mouse_face_from_string_pos), but I leave it here for the time
27340 being, in case someone would. */
27342 #if 0 /* not used */
27344 /* Find the position of the glyph for position POS in OBJECT in
27345 window W's current matrix, and return in *X, *Y the pixel
27346 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27348 RIGHT_P non-zero means return the position of the right edge of the
27349 glyph, RIGHT_P zero means return the left edge position.
27351 If no glyph for POS exists in the matrix, return the position of
27352 the glyph with the next smaller position that is in the matrix, if
27353 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27354 exists in the matrix, return the position of the glyph with the
27355 next larger position in OBJECT.
27357 Value is non-zero if a glyph was found. */
27359 static int
27360 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27361 int *hpos, int *vpos, int *x, int *y, int right_p)
27363 int yb = window_text_bottom_y (w);
27364 struct glyph_row *r;
27365 struct glyph *best_glyph = NULL;
27366 struct glyph_row *best_row = NULL;
27367 int best_x = 0;
27369 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27370 r->enabled_p && r->y < yb;
27371 ++r)
27373 struct glyph *g = r->glyphs[TEXT_AREA];
27374 struct glyph *e = g + r->used[TEXT_AREA];
27375 int gx;
27377 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27378 if (EQ (g->object, object))
27380 if (g->charpos == pos)
27382 best_glyph = g;
27383 best_x = gx;
27384 best_row = r;
27385 goto found;
27387 else if (best_glyph == NULL
27388 || ((eabs (g->charpos - pos)
27389 < eabs (best_glyph->charpos - pos))
27390 && (right_p
27391 ? g->charpos < pos
27392 : g->charpos > pos)))
27394 best_glyph = g;
27395 best_x = gx;
27396 best_row = r;
27401 found:
27403 if (best_glyph)
27405 *x = best_x;
27406 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27408 if (right_p)
27410 *x += best_glyph->pixel_width;
27411 ++*hpos;
27414 *y = best_row->y;
27415 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27418 return best_glyph != NULL;
27420 #endif /* not used */
27422 /* Find the positions of the first and the last glyphs in window W's
27423 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27424 (assumed to be a string), and return in HLINFO's mouse_face_*
27425 members the pixel and column/row coordinates of those glyphs. */
27427 static void
27428 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27429 Lisp_Object object,
27430 ptrdiff_t startpos, ptrdiff_t endpos)
27432 int yb = window_text_bottom_y (w);
27433 struct glyph_row *r;
27434 struct glyph *g, *e;
27435 int gx;
27436 int found = 0;
27438 /* Find the glyph row with at least one position in the range
27439 [STARTPOS..ENDPOS), and the first glyph in that row whose
27440 position belongs to that range. */
27441 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27442 r->enabled_p && r->y < yb;
27443 ++r)
27445 if (!r->reversed_p)
27447 g = r->glyphs[TEXT_AREA];
27448 e = g + r->used[TEXT_AREA];
27449 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27450 if (EQ (g->object, object)
27451 && startpos <= g->charpos && g->charpos < endpos)
27453 hlinfo->mouse_face_beg_row
27454 = MATRIX_ROW_VPOS (r, w->current_matrix);
27455 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27456 hlinfo->mouse_face_beg_x = gx;
27457 found = 1;
27458 break;
27461 else
27463 struct glyph *g1;
27465 e = r->glyphs[TEXT_AREA];
27466 g = e + r->used[TEXT_AREA];
27467 for ( ; g > e; --g)
27468 if (EQ ((g-1)->object, object)
27469 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27471 hlinfo->mouse_face_beg_row
27472 = MATRIX_ROW_VPOS (r, w->current_matrix);
27473 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27474 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27475 gx += g1->pixel_width;
27476 hlinfo->mouse_face_beg_x = gx;
27477 found = 1;
27478 break;
27481 if (found)
27482 break;
27485 if (!found)
27486 return;
27488 /* Starting with the next row, look for the first row which does NOT
27489 include any glyphs whose positions are in the range. */
27490 for (++r; r->enabled_p && r->y < yb; ++r)
27492 g = r->glyphs[TEXT_AREA];
27493 e = g + r->used[TEXT_AREA];
27494 found = 0;
27495 for ( ; g < e; ++g)
27496 if (EQ (g->object, object)
27497 && startpos <= g->charpos && g->charpos < endpos)
27499 found = 1;
27500 break;
27502 if (!found)
27503 break;
27506 /* The highlighted region ends on the previous row. */
27507 r--;
27509 /* Set the end row. */
27510 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27512 /* Compute and set the end column and the end column's horizontal
27513 pixel coordinate. */
27514 if (!r->reversed_p)
27516 g = r->glyphs[TEXT_AREA];
27517 e = g + r->used[TEXT_AREA];
27518 for ( ; e > g; --e)
27519 if (EQ ((e-1)->object, object)
27520 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27521 break;
27522 hlinfo->mouse_face_end_col = e - g;
27524 for (gx = r->x; g < e; ++g)
27525 gx += g->pixel_width;
27526 hlinfo->mouse_face_end_x = gx;
27528 else
27530 e = r->glyphs[TEXT_AREA];
27531 g = e + r->used[TEXT_AREA];
27532 for (gx = r->x ; e < g; ++e)
27534 if (EQ (e->object, object)
27535 && startpos <= e->charpos && e->charpos < endpos)
27536 break;
27537 gx += e->pixel_width;
27539 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27540 hlinfo->mouse_face_end_x = gx;
27544 #ifdef HAVE_WINDOW_SYSTEM
27546 /* See if position X, Y is within a hot-spot of an image. */
27548 static int
27549 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27551 if (!CONSP (hot_spot))
27552 return 0;
27554 if (EQ (XCAR (hot_spot), Qrect))
27556 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27557 Lisp_Object rect = XCDR (hot_spot);
27558 Lisp_Object tem;
27559 if (!CONSP (rect))
27560 return 0;
27561 if (!CONSP (XCAR (rect)))
27562 return 0;
27563 if (!CONSP (XCDR (rect)))
27564 return 0;
27565 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27566 return 0;
27567 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27568 return 0;
27569 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27570 return 0;
27571 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27572 return 0;
27573 return 1;
27575 else if (EQ (XCAR (hot_spot), Qcircle))
27577 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27578 Lisp_Object circ = XCDR (hot_spot);
27579 Lisp_Object lr, lx0, ly0;
27580 if (CONSP (circ)
27581 && CONSP (XCAR (circ))
27582 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27583 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27584 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27586 double r = XFLOATINT (lr);
27587 double dx = XINT (lx0) - x;
27588 double dy = XINT (ly0) - y;
27589 return (dx * dx + dy * dy <= r * r);
27592 else if (EQ (XCAR (hot_spot), Qpoly))
27594 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27595 if (VECTORP (XCDR (hot_spot)))
27597 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27598 Lisp_Object *poly = v->contents;
27599 ptrdiff_t n = v->header.size;
27600 ptrdiff_t i;
27601 int inside = 0;
27602 Lisp_Object lx, ly;
27603 int x0, y0;
27605 /* Need an even number of coordinates, and at least 3 edges. */
27606 if (n < 6 || n & 1)
27607 return 0;
27609 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27610 If count is odd, we are inside polygon. Pixels on edges
27611 may or may not be included depending on actual geometry of the
27612 polygon. */
27613 if ((lx = poly[n-2], !INTEGERP (lx))
27614 || (ly = poly[n-1], !INTEGERP (lx)))
27615 return 0;
27616 x0 = XINT (lx), y0 = XINT (ly);
27617 for (i = 0; i < n; i += 2)
27619 int x1 = x0, y1 = y0;
27620 if ((lx = poly[i], !INTEGERP (lx))
27621 || (ly = poly[i+1], !INTEGERP (ly)))
27622 return 0;
27623 x0 = XINT (lx), y0 = XINT (ly);
27625 /* Does this segment cross the X line? */
27626 if (x0 >= x)
27628 if (x1 >= x)
27629 continue;
27631 else if (x1 < x)
27632 continue;
27633 if (y > y0 && y > y1)
27634 continue;
27635 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27636 inside = !inside;
27638 return inside;
27641 return 0;
27644 Lisp_Object
27645 find_hot_spot (Lisp_Object map, int x, int y)
27647 while (CONSP (map))
27649 if (CONSP (XCAR (map))
27650 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27651 return XCAR (map);
27652 map = XCDR (map);
27655 return Qnil;
27658 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27659 3, 3, 0,
27660 doc: /* Lookup in image map MAP coordinates X and Y.
27661 An image map is an alist where each element has the format (AREA ID PLIST).
27662 An AREA is specified as either a rectangle, a circle, or a polygon:
27663 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27664 pixel coordinates of the upper left and bottom right corners.
27665 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27666 and the radius of the circle; r may be a float or integer.
27667 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27668 vector describes one corner in the polygon.
27669 Returns the alist element for the first matching AREA in MAP. */)
27670 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27672 if (NILP (map))
27673 return Qnil;
27675 CHECK_NUMBER (x);
27676 CHECK_NUMBER (y);
27678 return find_hot_spot (map,
27679 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27680 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27684 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27685 static void
27686 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27688 /* Do not change cursor shape while dragging mouse. */
27689 if (!NILP (do_mouse_tracking))
27690 return;
27692 if (!NILP (pointer))
27694 if (EQ (pointer, Qarrow))
27695 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27696 else if (EQ (pointer, Qhand))
27697 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27698 else if (EQ (pointer, Qtext))
27699 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27700 else if (EQ (pointer, intern ("hdrag")))
27701 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27702 #ifdef HAVE_X_WINDOWS
27703 else if (EQ (pointer, intern ("vdrag")))
27704 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27705 #endif
27706 else if (EQ (pointer, intern ("hourglass")))
27707 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27708 else if (EQ (pointer, Qmodeline))
27709 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27710 else
27711 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27714 if (cursor != No_Cursor)
27715 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27718 #endif /* HAVE_WINDOW_SYSTEM */
27720 /* Take proper action when mouse has moved to the mode or header line
27721 or marginal area AREA of window W, x-position X and y-position Y.
27722 X is relative to the start of the text display area of W, so the
27723 width of bitmap areas and scroll bars must be subtracted to get a
27724 position relative to the start of the mode line. */
27726 static void
27727 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27728 enum window_part area)
27730 struct window *w = XWINDOW (window);
27731 struct frame *f = XFRAME (w->frame);
27732 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27733 #ifdef HAVE_WINDOW_SYSTEM
27734 Display_Info *dpyinfo;
27735 #endif
27736 Cursor cursor = No_Cursor;
27737 Lisp_Object pointer = Qnil;
27738 int dx, dy, width, height;
27739 ptrdiff_t charpos;
27740 Lisp_Object string, object = Qnil;
27741 Lisp_Object pos IF_LINT (= Qnil), help;
27743 Lisp_Object mouse_face;
27744 int original_x_pixel = x;
27745 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27746 struct glyph_row *row IF_LINT (= 0);
27748 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27750 int x0;
27751 struct glyph *end;
27753 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27754 returns them in row/column units! */
27755 string = mode_line_string (w, area, &x, &y, &charpos,
27756 &object, &dx, &dy, &width, &height);
27758 row = (area == ON_MODE_LINE
27759 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27760 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27762 /* Find the glyph under the mouse pointer. */
27763 if (row->mode_line_p && row->enabled_p)
27765 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27766 end = glyph + row->used[TEXT_AREA];
27768 for (x0 = original_x_pixel;
27769 glyph < end && x0 >= glyph->pixel_width;
27770 ++glyph)
27771 x0 -= glyph->pixel_width;
27773 if (glyph >= end)
27774 glyph = NULL;
27777 else
27779 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27780 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27781 returns them in row/column units! */
27782 string = marginal_area_string (w, area, &x, &y, &charpos,
27783 &object, &dx, &dy, &width, &height);
27786 help = Qnil;
27788 #ifdef HAVE_WINDOW_SYSTEM
27789 if (IMAGEP (object))
27791 Lisp_Object image_map, hotspot;
27792 if ((image_map = Fplist_get (XCDR (object), QCmap),
27793 !NILP (image_map))
27794 && (hotspot = find_hot_spot (image_map, dx, dy),
27795 CONSP (hotspot))
27796 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27798 Lisp_Object plist;
27800 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27801 If so, we could look for mouse-enter, mouse-leave
27802 properties in PLIST (and do something...). */
27803 hotspot = XCDR (hotspot);
27804 if (CONSP (hotspot)
27805 && (plist = XCAR (hotspot), CONSP (plist)))
27807 pointer = Fplist_get (plist, Qpointer);
27808 if (NILP (pointer))
27809 pointer = Qhand;
27810 help = Fplist_get (plist, Qhelp_echo);
27811 if (!NILP (help))
27813 help_echo_string = help;
27814 XSETWINDOW (help_echo_window, w);
27815 help_echo_object = w->contents;
27816 help_echo_pos = charpos;
27820 if (NILP (pointer))
27821 pointer = Fplist_get (XCDR (object), QCpointer);
27823 #endif /* HAVE_WINDOW_SYSTEM */
27825 if (STRINGP (string))
27826 pos = make_number (charpos);
27828 /* Set the help text and mouse pointer. If the mouse is on a part
27829 of the mode line without any text (e.g. past the right edge of
27830 the mode line text), use the default help text and pointer. */
27831 if (STRINGP (string) || area == ON_MODE_LINE)
27833 /* Arrange to display the help by setting the global variables
27834 help_echo_string, help_echo_object, and help_echo_pos. */
27835 if (NILP (help))
27837 if (STRINGP (string))
27838 help = Fget_text_property (pos, Qhelp_echo, string);
27840 if (!NILP (help))
27842 help_echo_string = help;
27843 XSETWINDOW (help_echo_window, w);
27844 help_echo_object = string;
27845 help_echo_pos = charpos;
27847 else if (area == ON_MODE_LINE)
27849 Lisp_Object default_help
27850 = buffer_local_value_1 (Qmode_line_default_help_echo,
27851 w->contents);
27853 if (STRINGP (default_help))
27855 help_echo_string = default_help;
27856 XSETWINDOW (help_echo_window, w);
27857 help_echo_object = Qnil;
27858 help_echo_pos = -1;
27863 #ifdef HAVE_WINDOW_SYSTEM
27864 /* Change the mouse pointer according to what is under it. */
27865 if (FRAME_WINDOW_P (f))
27867 dpyinfo = FRAME_DISPLAY_INFO (f);
27868 if (STRINGP (string))
27870 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27872 if (NILP (pointer))
27873 pointer = Fget_text_property (pos, Qpointer, string);
27875 /* Change the mouse pointer according to what is under X/Y. */
27876 if (NILP (pointer)
27877 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27879 Lisp_Object map;
27880 map = Fget_text_property (pos, Qlocal_map, string);
27881 if (!KEYMAPP (map))
27882 map = Fget_text_property (pos, Qkeymap, string);
27883 if (!KEYMAPP (map))
27884 cursor = dpyinfo->vertical_scroll_bar_cursor;
27887 else
27888 /* Default mode-line pointer. */
27889 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27891 #endif
27894 /* Change the mouse face according to what is under X/Y. */
27895 if (STRINGP (string))
27897 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27898 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27899 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27900 && glyph)
27902 Lisp_Object b, e;
27904 struct glyph * tmp_glyph;
27906 int gpos;
27907 int gseq_length;
27908 int total_pixel_width;
27909 ptrdiff_t begpos, endpos, ignore;
27911 int vpos, hpos;
27913 b = Fprevious_single_property_change (make_number (charpos + 1),
27914 Qmouse_face, string, Qnil);
27915 if (NILP (b))
27916 begpos = 0;
27917 else
27918 begpos = XINT (b);
27920 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27921 if (NILP (e))
27922 endpos = SCHARS (string);
27923 else
27924 endpos = XINT (e);
27926 /* Calculate the glyph position GPOS of GLYPH in the
27927 displayed string, relative to the beginning of the
27928 highlighted part of the string.
27930 Note: GPOS is different from CHARPOS. CHARPOS is the
27931 position of GLYPH in the internal string object. A mode
27932 line string format has structures which are converted to
27933 a flattened string by the Emacs Lisp interpreter. The
27934 internal string is an element of those structures. The
27935 displayed string is the flattened string. */
27936 tmp_glyph = row_start_glyph;
27937 while (tmp_glyph < glyph
27938 && (!(EQ (tmp_glyph->object, glyph->object)
27939 && begpos <= tmp_glyph->charpos
27940 && tmp_glyph->charpos < endpos)))
27941 tmp_glyph++;
27942 gpos = glyph - tmp_glyph;
27944 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27945 the highlighted part of the displayed string to which
27946 GLYPH belongs. Note: GSEQ_LENGTH is different from
27947 SCHARS (STRING), because the latter returns the length of
27948 the internal string. */
27949 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27950 tmp_glyph > glyph
27951 && (!(EQ (tmp_glyph->object, glyph->object)
27952 && begpos <= tmp_glyph->charpos
27953 && tmp_glyph->charpos < endpos));
27954 tmp_glyph--)
27956 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27958 /* Calculate the total pixel width of all the glyphs between
27959 the beginning of the highlighted area and GLYPH. */
27960 total_pixel_width = 0;
27961 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27962 total_pixel_width += tmp_glyph->pixel_width;
27964 /* Pre calculation of re-rendering position. Note: X is in
27965 column units here, after the call to mode_line_string or
27966 marginal_area_string. */
27967 hpos = x - gpos;
27968 vpos = (area == ON_MODE_LINE
27969 ? (w->current_matrix)->nrows - 1
27970 : 0);
27972 /* If GLYPH's position is included in the region that is
27973 already drawn in mouse face, we have nothing to do. */
27974 if ( EQ (window, hlinfo->mouse_face_window)
27975 && (!row->reversed_p
27976 ? (hlinfo->mouse_face_beg_col <= hpos
27977 && hpos < hlinfo->mouse_face_end_col)
27978 /* In R2L rows we swap BEG and END, see below. */
27979 : (hlinfo->mouse_face_end_col <= hpos
27980 && hpos < hlinfo->mouse_face_beg_col))
27981 && hlinfo->mouse_face_beg_row == vpos )
27982 return;
27984 if (clear_mouse_face (hlinfo))
27985 cursor = No_Cursor;
27987 if (!row->reversed_p)
27989 hlinfo->mouse_face_beg_col = hpos;
27990 hlinfo->mouse_face_beg_x = original_x_pixel
27991 - (total_pixel_width + dx);
27992 hlinfo->mouse_face_end_col = hpos + gseq_length;
27993 hlinfo->mouse_face_end_x = 0;
27995 else
27997 /* In R2L rows, show_mouse_face expects BEG and END
27998 coordinates to be swapped. */
27999 hlinfo->mouse_face_end_col = hpos;
28000 hlinfo->mouse_face_end_x = original_x_pixel
28001 - (total_pixel_width + dx);
28002 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28003 hlinfo->mouse_face_beg_x = 0;
28006 hlinfo->mouse_face_beg_row = vpos;
28007 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28008 hlinfo->mouse_face_past_end = 0;
28009 hlinfo->mouse_face_window = window;
28011 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28012 charpos,
28013 0, &ignore,
28014 glyph->face_id,
28016 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28018 if (NILP (pointer))
28019 pointer = Qhand;
28021 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28022 clear_mouse_face (hlinfo);
28024 #ifdef HAVE_WINDOW_SYSTEM
28025 if (FRAME_WINDOW_P (f))
28026 define_frame_cursor1 (f, cursor, pointer);
28027 #endif
28031 /* EXPORT:
28032 Take proper action when the mouse has moved to position X, Y on
28033 frame F with regards to highlighting portions of display that have
28034 mouse-face properties. Also de-highlight portions of display where
28035 the mouse was before, set the mouse pointer shape as appropriate
28036 for the mouse coordinates, and activate help echo (tooltips).
28037 X and Y can be negative or out of range. */
28039 void
28040 note_mouse_highlight (struct frame *f, int x, int y)
28042 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28043 enum window_part part = ON_NOTHING;
28044 Lisp_Object window;
28045 struct window *w;
28046 Cursor cursor = No_Cursor;
28047 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28048 struct buffer *b;
28050 /* When a menu is active, don't highlight because this looks odd. */
28051 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28052 if (popup_activated ())
28053 return;
28054 #endif
28056 if (!f->glyphs_initialized_p
28057 || f->pointer_invisible)
28058 return;
28060 hlinfo->mouse_face_mouse_x = x;
28061 hlinfo->mouse_face_mouse_y = y;
28062 hlinfo->mouse_face_mouse_frame = f;
28064 if (hlinfo->mouse_face_defer)
28065 return;
28067 /* Which window is that in? */
28068 window = window_from_coordinates (f, x, y, &part, 1);
28070 /* If displaying active text in another window, clear that. */
28071 if (! EQ (window, hlinfo->mouse_face_window)
28072 /* Also clear if we move out of text area in same window. */
28073 || (!NILP (hlinfo->mouse_face_window)
28074 && !NILP (window)
28075 && part != ON_TEXT
28076 && part != ON_MODE_LINE
28077 && part != ON_HEADER_LINE))
28078 clear_mouse_face (hlinfo);
28080 /* Not on a window -> return. */
28081 if (!WINDOWP (window))
28082 return;
28084 /* Reset help_echo_string. It will get recomputed below. */
28085 help_echo_string = Qnil;
28087 /* Convert to window-relative pixel coordinates. */
28088 w = XWINDOW (window);
28089 frame_to_window_pixel_xy (w, &x, &y);
28091 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28092 /* Handle tool-bar window differently since it doesn't display a
28093 buffer. */
28094 if (EQ (window, f->tool_bar_window))
28096 note_tool_bar_highlight (f, x, y);
28097 return;
28099 #endif
28101 /* Mouse is on the mode, header line or margin? */
28102 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28103 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28105 note_mode_line_or_margin_highlight (window, x, y, part);
28106 return;
28109 #ifdef HAVE_WINDOW_SYSTEM
28110 if (part == ON_VERTICAL_BORDER)
28112 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28113 help_echo_string = build_string ("drag-mouse-1: resize");
28115 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28116 || part == ON_SCROLL_BAR)
28117 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28118 else
28119 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28120 #endif
28122 /* Are we in a window whose display is up to date?
28123 And verify the buffer's text has not changed. */
28124 b = XBUFFER (w->contents);
28125 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28127 int hpos, vpos, dx, dy, area = LAST_AREA;
28128 ptrdiff_t pos;
28129 struct glyph *glyph;
28130 Lisp_Object object;
28131 Lisp_Object mouse_face = Qnil, position;
28132 Lisp_Object *overlay_vec = NULL;
28133 ptrdiff_t i, noverlays;
28134 struct buffer *obuf;
28135 ptrdiff_t obegv, ozv;
28136 int same_region;
28138 /* Find the glyph under X/Y. */
28139 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28141 #ifdef HAVE_WINDOW_SYSTEM
28142 /* Look for :pointer property on image. */
28143 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28145 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28146 if (img != NULL && IMAGEP (img->spec))
28148 Lisp_Object image_map, hotspot;
28149 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28150 !NILP (image_map))
28151 && (hotspot = find_hot_spot (image_map,
28152 glyph->slice.img.x + dx,
28153 glyph->slice.img.y + dy),
28154 CONSP (hotspot))
28155 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28157 Lisp_Object plist;
28159 /* Could check XCAR (hotspot) to see if we enter/leave
28160 this hot-spot.
28161 If so, we could look for mouse-enter, mouse-leave
28162 properties in PLIST (and do something...). */
28163 hotspot = XCDR (hotspot);
28164 if (CONSP (hotspot)
28165 && (plist = XCAR (hotspot), CONSP (plist)))
28167 pointer = Fplist_get (plist, Qpointer);
28168 if (NILP (pointer))
28169 pointer = Qhand;
28170 help_echo_string = Fplist_get (plist, Qhelp_echo);
28171 if (!NILP (help_echo_string))
28173 help_echo_window = window;
28174 help_echo_object = glyph->object;
28175 help_echo_pos = glyph->charpos;
28179 if (NILP (pointer))
28180 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28183 #endif /* HAVE_WINDOW_SYSTEM */
28185 /* Clear mouse face if X/Y not over text. */
28186 if (glyph == NULL
28187 || area != TEXT_AREA
28188 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28189 /* Glyph's OBJECT is an integer for glyphs inserted by the
28190 display engine for its internal purposes, like truncation
28191 and continuation glyphs and blanks beyond the end of
28192 line's text on text terminals. If we are over such a
28193 glyph, we are not over any text. */
28194 || INTEGERP (glyph->object)
28195 /* R2L rows have a stretch glyph at their front, which
28196 stands for no text, whereas L2R rows have no glyphs at
28197 all beyond the end of text. Treat such stretch glyphs
28198 like we do with NULL glyphs in L2R rows. */
28199 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28200 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28201 && glyph->type == STRETCH_GLYPH
28202 && glyph->avoid_cursor_p))
28204 if (clear_mouse_face (hlinfo))
28205 cursor = No_Cursor;
28206 #ifdef HAVE_WINDOW_SYSTEM
28207 if (FRAME_WINDOW_P (f) && NILP (pointer))
28209 if (area != TEXT_AREA)
28210 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28211 else
28212 pointer = Vvoid_text_area_pointer;
28214 #endif
28215 goto set_cursor;
28218 pos = glyph->charpos;
28219 object = glyph->object;
28220 if (!STRINGP (object) && !BUFFERP (object))
28221 goto set_cursor;
28223 /* If we get an out-of-range value, return now; avoid an error. */
28224 if (BUFFERP (object) && pos > BUF_Z (b))
28225 goto set_cursor;
28227 /* Make the window's buffer temporarily current for
28228 overlays_at and compute_char_face. */
28229 obuf = current_buffer;
28230 current_buffer = b;
28231 obegv = BEGV;
28232 ozv = ZV;
28233 BEGV = BEG;
28234 ZV = Z;
28236 /* Is this char mouse-active or does it have help-echo? */
28237 position = make_number (pos);
28239 if (BUFFERP (object))
28241 /* Put all the overlays we want in a vector in overlay_vec. */
28242 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28243 /* Sort overlays into increasing priority order. */
28244 noverlays = sort_overlays (overlay_vec, noverlays, w);
28246 else
28247 noverlays = 0;
28249 if (NILP (Vmouse_highlight))
28251 clear_mouse_face (hlinfo);
28252 goto check_help_echo;
28255 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28257 if (same_region)
28258 cursor = No_Cursor;
28260 /* Check mouse-face highlighting. */
28261 if (! same_region
28262 /* If there exists an overlay with mouse-face overlapping
28263 the one we are currently highlighting, we have to
28264 check if we enter the overlapping overlay, and then
28265 highlight only that. */
28266 || (OVERLAYP (hlinfo->mouse_face_overlay)
28267 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28269 /* Find the highest priority overlay with a mouse-face. */
28270 Lisp_Object overlay = Qnil;
28271 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28273 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28274 if (!NILP (mouse_face))
28275 overlay = overlay_vec[i];
28278 /* If we're highlighting the same overlay as before, there's
28279 no need to do that again. */
28280 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28281 goto check_help_echo;
28282 hlinfo->mouse_face_overlay = overlay;
28284 /* Clear the display of the old active region, if any. */
28285 if (clear_mouse_face (hlinfo))
28286 cursor = No_Cursor;
28288 /* If no overlay applies, get a text property. */
28289 if (NILP (overlay))
28290 mouse_face = Fget_text_property (position, Qmouse_face, object);
28292 /* Next, compute the bounds of the mouse highlighting and
28293 display it. */
28294 if (!NILP (mouse_face) && STRINGP (object))
28296 /* The mouse-highlighting comes from a display string
28297 with a mouse-face. */
28298 Lisp_Object s, e;
28299 ptrdiff_t ignore;
28301 s = Fprevious_single_property_change
28302 (make_number (pos + 1), Qmouse_face, object, Qnil);
28303 e = Fnext_single_property_change
28304 (position, Qmouse_face, object, Qnil);
28305 if (NILP (s))
28306 s = make_number (0);
28307 if (NILP (e))
28308 e = make_number (SCHARS (object));
28309 mouse_face_from_string_pos (w, hlinfo, object,
28310 XINT (s), XINT (e));
28311 hlinfo->mouse_face_past_end = 0;
28312 hlinfo->mouse_face_window = window;
28313 hlinfo->mouse_face_face_id
28314 = face_at_string_position (w, object, pos, 0, &ignore,
28315 glyph->face_id, 1);
28316 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28317 cursor = No_Cursor;
28319 else
28321 /* The mouse-highlighting, if any, comes from an overlay
28322 or text property in the buffer. */
28323 Lisp_Object buffer IF_LINT (= Qnil);
28324 Lisp_Object disp_string IF_LINT (= Qnil);
28326 if (STRINGP (object))
28328 /* If we are on a display string with no mouse-face,
28329 check if the text under it has one. */
28330 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28331 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28332 pos = string_buffer_position (object, start);
28333 if (pos > 0)
28335 mouse_face = get_char_property_and_overlay
28336 (make_number (pos), Qmouse_face, w->contents, &overlay);
28337 buffer = w->contents;
28338 disp_string = object;
28341 else
28343 buffer = object;
28344 disp_string = Qnil;
28347 if (!NILP (mouse_face))
28349 Lisp_Object before, after;
28350 Lisp_Object before_string, after_string;
28351 /* To correctly find the limits of mouse highlight
28352 in a bidi-reordered buffer, we must not use the
28353 optimization of limiting the search in
28354 previous-single-property-change and
28355 next-single-property-change, because
28356 rows_from_pos_range needs the real start and end
28357 positions to DTRT in this case. That's because
28358 the first row visible in a window does not
28359 necessarily display the character whose position
28360 is the smallest. */
28361 Lisp_Object lim1
28362 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28363 ? Fmarker_position (w->start)
28364 : Qnil;
28365 Lisp_Object lim2
28366 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28367 ? make_number (BUF_Z (XBUFFER (buffer))
28368 - w->window_end_pos)
28369 : Qnil;
28371 if (NILP (overlay))
28373 /* Handle the text property case. */
28374 before = Fprevious_single_property_change
28375 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28376 after = Fnext_single_property_change
28377 (make_number (pos), Qmouse_face, buffer, lim2);
28378 before_string = after_string = Qnil;
28380 else
28382 /* Handle the overlay case. */
28383 before = Foverlay_start (overlay);
28384 after = Foverlay_end (overlay);
28385 before_string = Foverlay_get (overlay, Qbefore_string);
28386 after_string = Foverlay_get (overlay, Qafter_string);
28388 if (!STRINGP (before_string)) before_string = Qnil;
28389 if (!STRINGP (after_string)) after_string = Qnil;
28392 mouse_face_from_buffer_pos (window, hlinfo, pos,
28393 NILP (before)
28395 : XFASTINT (before),
28396 NILP (after)
28397 ? BUF_Z (XBUFFER (buffer))
28398 : XFASTINT (after),
28399 before_string, after_string,
28400 disp_string);
28401 cursor = No_Cursor;
28406 check_help_echo:
28408 /* Look for a `help-echo' property. */
28409 if (NILP (help_echo_string)) {
28410 Lisp_Object help, overlay;
28412 /* Check overlays first. */
28413 help = overlay = Qnil;
28414 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28416 overlay = overlay_vec[i];
28417 help = Foverlay_get (overlay, Qhelp_echo);
28420 if (!NILP (help))
28422 help_echo_string = help;
28423 help_echo_window = window;
28424 help_echo_object = overlay;
28425 help_echo_pos = pos;
28427 else
28429 Lisp_Object obj = glyph->object;
28430 ptrdiff_t charpos = glyph->charpos;
28432 /* Try text properties. */
28433 if (STRINGP (obj)
28434 && charpos >= 0
28435 && charpos < SCHARS (obj))
28437 help = Fget_text_property (make_number (charpos),
28438 Qhelp_echo, obj);
28439 if (NILP (help))
28441 /* If the string itself doesn't specify a help-echo,
28442 see if the buffer text ``under'' it does. */
28443 struct glyph_row *r
28444 = MATRIX_ROW (w->current_matrix, vpos);
28445 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28446 ptrdiff_t p = string_buffer_position (obj, start);
28447 if (p > 0)
28449 help = Fget_char_property (make_number (p),
28450 Qhelp_echo, w->contents);
28451 if (!NILP (help))
28453 charpos = p;
28454 obj = w->contents;
28459 else if (BUFFERP (obj)
28460 && charpos >= BEGV
28461 && charpos < ZV)
28462 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28463 obj);
28465 if (!NILP (help))
28467 help_echo_string = help;
28468 help_echo_window = window;
28469 help_echo_object = obj;
28470 help_echo_pos = charpos;
28475 #ifdef HAVE_WINDOW_SYSTEM
28476 /* Look for a `pointer' property. */
28477 if (FRAME_WINDOW_P (f) && NILP (pointer))
28479 /* Check overlays first. */
28480 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28481 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28483 if (NILP (pointer))
28485 Lisp_Object obj = glyph->object;
28486 ptrdiff_t charpos = glyph->charpos;
28488 /* Try text properties. */
28489 if (STRINGP (obj)
28490 && charpos >= 0
28491 && charpos < SCHARS (obj))
28493 pointer = Fget_text_property (make_number (charpos),
28494 Qpointer, obj);
28495 if (NILP (pointer))
28497 /* If the string itself doesn't specify a pointer,
28498 see if the buffer text ``under'' it does. */
28499 struct glyph_row *r
28500 = MATRIX_ROW (w->current_matrix, vpos);
28501 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28502 ptrdiff_t p = string_buffer_position (obj, start);
28503 if (p > 0)
28504 pointer = Fget_char_property (make_number (p),
28505 Qpointer, w->contents);
28508 else if (BUFFERP (obj)
28509 && charpos >= BEGV
28510 && charpos < ZV)
28511 pointer = Fget_text_property (make_number (charpos),
28512 Qpointer, obj);
28515 #endif /* HAVE_WINDOW_SYSTEM */
28517 BEGV = obegv;
28518 ZV = ozv;
28519 current_buffer = obuf;
28522 set_cursor:
28524 #ifdef HAVE_WINDOW_SYSTEM
28525 if (FRAME_WINDOW_P (f))
28526 define_frame_cursor1 (f, cursor, pointer);
28527 #else
28528 /* This is here to prevent a compiler error, about "label at end of
28529 compound statement". */
28530 return;
28531 #endif
28535 /* EXPORT for RIF:
28536 Clear any mouse-face on window W. This function is part of the
28537 redisplay interface, and is called from try_window_id and similar
28538 functions to ensure the mouse-highlight is off. */
28540 void
28541 x_clear_window_mouse_face (struct window *w)
28543 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28544 Lisp_Object window;
28546 block_input ();
28547 XSETWINDOW (window, w);
28548 if (EQ (window, hlinfo->mouse_face_window))
28549 clear_mouse_face (hlinfo);
28550 unblock_input ();
28554 /* EXPORT:
28555 Just discard the mouse face information for frame F, if any.
28556 This is used when the size of F is changed. */
28558 void
28559 cancel_mouse_face (struct frame *f)
28561 Lisp_Object window;
28562 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28564 window = hlinfo->mouse_face_window;
28565 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28566 reset_mouse_highlight (hlinfo);
28571 /***********************************************************************
28572 Exposure Events
28573 ***********************************************************************/
28575 #ifdef HAVE_WINDOW_SYSTEM
28577 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28578 which intersects rectangle R. R is in window-relative coordinates. */
28580 static void
28581 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28582 enum glyph_row_area area)
28584 struct glyph *first = row->glyphs[area];
28585 struct glyph *end = row->glyphs[area] + row->used[area];
28586 struct glyph *last;
28587 int first_x, start_x, x;
28589 if (area == TEXT_AREA && row->fill_line_p)
28590 /* If row extends face to end of line write the whole line. */
28591 draw_glyphs (w, 0, row, area,
28592 0, row->used[area],
28593 DRAW_NORMAL_TEXT, 0);
28594 else
28596 /* Set START_X to the window-relative start position for drawing glyphs of
28597 AREA. The first glyph of the text area can be partially visible.
28598 The first glyphs of other areas cannot. */
28599 start_x = window_box_left_offset (w, area);
28600 x = start_x;
28601 if (area == TEXT_AREA)
28602 x += row->x;
28604 /* Find the first glyph that must be redrawn. */
28605 while (first < end
28606 && x + first->pixel_width < r->x)
28608 x += first->pixel_width;
28609 ++first;
28612 /* Find the last one. */
28613 last = first;
28614 first_x = x;
28615 while (last < end
28616 && x < r->x + r->width)
28618 x += last->pixel_width;
28619 ++last;
28622 /* Repaint. */
28623 if (last > first)
28624 draw_glyphs (w, first_x - start_x, row, area,
28625 first - row->glyphs[area], last - row->glyphs[area],
28626 DRAW_NORMAL_TEXT, 0);
28631 /* Redraw the parts of the glyph row ROW on window W intersecting
28632 rectangle R. R is in window-relative coordinates. Value is
28633 non-zero if mouse-face was overwritten. */
28635 static int
28636 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28638 eassert (row->enabled_p);
28640 if (row->mode_line_p || w->pseudo_window_p)
28641 draw_glyphs (w, 0, row, TEXT_AREA,
28642 0, row->used[TEXT_AREA],
28643 DRAW_NORMAL_TEXT, 0);
28644 else
28646 if (row->used[LEFT_MARGIN_AREA])
28647 expose_area (w, row, r, LEFT_MARGIN_AREA);
28648 if (row->used[TEXT_AREA])
28649 expose_area (w, row, r, TEXT_AREA);
28650 if (row->used[RIGHT_MARGIN_AREA])
28651 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28652 draw_row_fringe_bitmaps (w, row);
28655 return row->mouse_face_p;
28659 /* Redraw those parts of glyphs rows during expose event handling that
28660 overlap other rows. Redrawing of an exposed line writes over parts
28661 of lines overlapping that exposed line; this function fixes that.
28663 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28664 row in W's current matrix that is exposed and overlaps other rows.
28665 LAST_OVERLAPPING_ROW is the last such row. */
28667 static void
28668 expose_overlaps (struct window *w,
28669 struct glyph_row *first_overlapping_row,
28670 struct glyph_row *last_overlapping_row,
28671 XRectangle *r)
28673 struct glyph_row *row;
28675 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28676 if (row->overlapping_p)
28678 eassert (row->enabled_p && !row->mode_line_p);
28680 row->clip = r;
28681 if (row->used[LEFT_MARGIN_AREA])
28682 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28684 if (row->used[TEXT_AREA])
28685 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28687 if (row->used[RIGHT_MARGIN_AREA])
28688 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28689 row->clip = NULL;
28694 /* Return non-zero if W's cursor intersects rectangle R. */
28696 static int
28697 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28699 XRectangle cr, result;
28700 struct glyph *cursor_glyph;
28701 struct glyph_row *row;
28703 if (w->phys_cursor.vpos >= 0
28704 && w->phys_cursor.vpos < w->current_matrix->nrows
28705 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28706 row->enabled_p)
28707 && row->cursor_in_fringe_p)
28709 /* Cursor is in the fringe. */
28710 cr.x = window_box_right_offset (w,
28711 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28712 ? RIGHT_MARGIN_AREA
28713 : TEXT_AREA));
28714 cr.y = row->y;
28715 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28716 cr.height = row->height;
28717 return x_intersect_rectangles (&cr, r, &result);
28720 cursor_glyph = get_phys_cursor_glyph (w);
28721 if (cursor_glyph)
28723 /* r is relative to W's box, but w->phys_cursor.x is relative
28724 to left edge of W's TEXT area. Adjust it. */
28725 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28726 cr.y = w->phys_cursor.y;
28727 cr.width = cursor_glyph->pixel_width;
28728 cr.height = w->phys_cursor_height;
28729 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28730 I assume the effect is the same -- and this is portable. */
28731 return x_intersect_rectangles (&cr, r, &result);
28733 /* If we don't understand the format, pretend we're not in the hot-spot. */
28734 return 0;
28738 /* EXPORT:
28739 Draw a vertical window border to the right of window W if W doesn't
28740 have vertical scroll bars. */
28742 void
28743 x_draw_vertical_border (struct window *w)
28745 struct frame *f = XFRAME (WINDOW_FRAME (w));
28747 /* We could do better, if we knew what type of scroll-bar the adjacent
28748 windows (on either side) have... But we don't :-(
28749 However, I think this works ok. ++KFS 2003-04-25 */
28751 /* Redraw borders between horizontally adjacent windows. Don't
28752 do it for frames with vertical scroll bars because either the
28753 right scroll bar of a window, or the left scroll bar of its
28754 neighbor will suffice as a border. */
28755 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28756 return;
28758 /* Note: It is necessary to redraw both the left and the right
28759 borders, for when only this single window W is being
28760 redisplayed. */
28761 if (!WINDOW_RIGHTMOST_P (w)
28762 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28764 int x0, x1, y0, y1;
28766 window_box_edges (w, &x0, &y0, &x1, &y1);
28767 y1 -= 1;
28769 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28770 x1 -= 1;
28772 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28774 if (!WINDOW_LEFTMOST_P (w)
28775 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28777 int x0, x1, y0, y1;
28779 window_box_edges (w, &x0, &y0, &x1, &y1);
28780 y1 -= 1;
28782 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28783 x0 -= 1;
28785 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28790 /* Redraw the part of window W intersection rectangle FR. Pixel
28791 coordinates in FR are frame-relative. Call this function with
28792 input blocked. Value is non-zero if the exposure overwrites
28793 mouse-face. */
28795 static int
28796 expose_window (struct window *w, XRectangle *fr)
28798 struct frame *f = XFRAME (w->frame);
28799 XRectangle wr, r;
28800 int mouse_face_overwritten_p = 0;
28802 /* If window is not yet fully initialized, do nothing. This can
28803 happen when toolkit scroll bars are used and a window is split.
28804 Reconfiguring the scroll bar will generate an expose for a newly
28805 created window. */
28806 if (w->current_matrix == NULL)
28807 return 0;
28809 /* When we're currently updating the window, display and current
28810 matrix usually don't agree. Arrange for a thorough display
28811 later. */
28812 if (w->must_be_updated_p)
28814 SET_FRAME_GARBAGED (f);
28815 return 0;
28818 /* Frame-relative pixel rectangle of W. */
28819 wr.x = WINDOW_LEFT_EDGE_X (w);
28820 wr.y = WINDOW_TOP_EDGE_Y (w);
28821 wr.width = WINDOW_TOTAL_WIDTH (w);
28822 wr.height = WINDOW_TOTAL_HEIGHT (w);
28824 if (x_intersect_rectangles (fr, &wr, &r))
28826 int yb = window_text_bottom_y (w);
28827 struct glyph_row *row;
28828 int cursor_cleared_p, phys_cursor_on_p;
28829 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28831 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28832 r.x, r.y, r.width, r.height));
28834 /* Convert to window coordinates. */
28835 r.x -= WINDOW_LEFT_EDGE_X (w);
28836 r.y -= WINDOW_TOP_EDGE_Y (w);
28838 /* Turn off the cursor. */
28839 if (!w->pseudo_window_p
28840 && phys_cursor_in_rect_p (w, &r))
28842 x_clear_cursor (w);
28843 cursor_cleared_p = 1;
28845 else
28846 cursor_cleared_p = 0;
28848 /* If the row containing the cursor extends face to end of line,
28849 then expose_area might overwrite the cursor outside the
28850 rectangle and thus notice_overwritten_cursor might clear
28851 w->phys_cursor_on_p. We remember the original value and
28852 check later if it is changed. */
28853 phys_cursor_on_p = w->phys_cursor_on_p;
28855 /* Update lines intersecting rectangle R. */
28856 first_overlapping_row = last_overlapping_row = NULL;
28857 for (row = w->current_matrix->rows;
28858 row->enabled_p;
28859 ++row)
28861 int y0 = row->y;
28862 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28864 if ((y0 >= r.y && y0 < r.y + r.height)
28865 || (y1 > r.y && y1 < r.y + r.height)
28866 || (r.y >= y0 && r.y < y1)
28867 || (r.y + r.height > y0 && r.y + r.height < y1))
28869 /* A header line may be overlapping, but there is no need
28870 to fix overlapping areas for them. KFS 2005-02-12 */
28871 if (row->overlapping_p && !row->mode_line_p)
28873 if (first_overlapping_row == NULL)
28874 first_overlapping_row = row;
28875 last_overlapping_row = row;
28878 row->clip = fr;
28879 if (expose_line (w, row, &r))
28880 mouse_face_overwritten_p = 1;
28881 row->clip = NULL;
28883 else if (row->overlapping_p)
28885 /* We must redraw a row overlapping the exposed area. */
28886 if (y0 < r.y
28887 ? y0 + row->phys_height > r.y
28888 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28890 if (first_overlapping_row == NULL)
28891 first_overlapping_row = row;
28892 last_overlapping_row = row;
28896 if (y1 >= yb)
28897 break;
28900 /* Display the mode line if there is one. */
28901 if (WINDOW_WANTS_MODELINE_P (w)
28902 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28903 row->enabled_p)
28904 && row->y < r.y + r.height)
28906 if (expose_line (w, row, &r))
28907 mouse_face_overwritten_p = 1;
28910 if (!w->pseudo_window_p)
28912 /* Fix the display of overlapping rows. */
28913 if (first_overlapping_row)
28914 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28915 fr);
28917 /* Draw border between windows. */
28918 x_draw_vertical_border (w);
28920 /* Turn the cursor on again. */
28921 if (cursor_cleared_p
28922 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28923 update_window_cursor (w, 1);
28927 return mouse_face_overwritten_p;
28932 /* Redraw (parts) of all windows in the window tree rooted at W that
28933 intersect R. R contains frame pixel coordinates. Value is
28934 non-zero if the exposure overwrites mouse-face. */
28936 static int
28937 expose_window_tree (struct window *w, XRectangle *r)
28939 struct frame *f = XFRAME (w->frame);
28940 int mouse_face_overwritten_p = 0;
28942 while (w && !FRAME_GARBAGED_P (f))
28944 if (WINDOWP (w->contents))
28945 mouse_face_overwritten_p
28946 |= expose_window_tree (XWINDOW (w->contents), r);
28947 else
28948 mouse_face_overwritten_p |= expose_window (w, r);
28950 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28953 return mouse_face_overwritten_p;
28957 /* EXPORT:
28958 Redisplay an exposed area of frame F. X and Y are the upper-left
28959 corner of the exposed rectangle. W and H are width and height of
28960 the exposed area. All are pixel values. W or H zero means redraw
28961 the entire frame. */
28963 void
28964 expose_frame (struct frame *f, int x, int y, int w, int h)
28966 XRectangle r;
28967 int mouse_face_overwritten_p = 0;
28969 TRACE ((stderr, "expose_frame "));
28971 /* No need to redraw if frame will be redrawn soon. */
28972 if (FRAME_GARBAGED_P (f))
28974 TRACE ((stderr, " garbaged\n"));
28975 return;
28978 /* If basic faces haven't been realized yet, there is no point in
28979 trying to redraw anything. This can happen when we get an expose
28980 event while Emacs is starting, e.g. by moving another window. */
28981 if (FRAME_FACE_CACHE (f) == NULL
28982 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28984 TRACE ((stderr, " no faces\n"));
28985 return;
28988 if (w == 0 || h == 0)
28990 r.x = r.y = 0;
28991 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28992 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28994 else
28996 r.x = x;
28997 r.y = y;
28998 r.width = w;
28999 r.height = h;
29002 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29003 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29005 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29006 if (WINDOWP (f->tool_bar_window))
29007 mouse_face_overwritten_p
29008 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29009 #endif
29011 #ifdef HAVE_X_WINDOWS
29012 #ifndef MSDOS
29013 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29014 if (WINDOWP (f->menu_bar_window))
29015 mouse_face_overwritten_p
29016 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29017 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29018 #endif
29019 #endif
29021 /* Some window managers support a focus-follows-mouse style with
29022 delayed raising of frames. Imagine a partially obscured frame,
29023 and moving the mouse into partially obscured mouse-face on that
29024 frame. The visible part of the mouse-face will be highlighted,
29025 then the WM raises the obscured frame. With at least one WM, KDE
29026 2.1, Emacs is not getting any event for the raising of the frame
29027 (even tried with SubstructureRedirectMask), only Expose events.
29028 These expose events will draw text normally, i.e. not
29029 highlighted. Which means we must redo the highlight here.
29030 Subsume it under ``we love X''. --gerd 2001-08-15 */
29031 /* Included in Windows version because Windows most likely does not
29032 do the right thing if any third party tool offers
29033 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29034 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29036 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29037 if (f == hlinfo->mouse_face_mouse_frame)
29039 int mouse_x = hlinfo->mouse_face_mouse_x;
29040 int mouse_y = hlinfo->mouse_face_mouse_y;
29041 clear_mouse_face (hlinfo);
29042 note_mouse_highlight (f, mouse_x, mouse_y);
29048 /* EXPORT:
29049 Determine the intersection of two rectangles R1 and R2. Return
29050 the intersection in *RESULT. Value is non-zero if RESULT is not
29051 empty. */
29054 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29056 XRectangle *left, *right;
29057 XRectangle *upper, *lower;
29058 int intersection_p = 0;
29060 /* Rearrange so that R1 is the left-most rectangle. */
29061 if (r1->x < r2->x)
29062 left = r1, right = r2;
29063 else
29064 left = r2, right = r1;
29066 /* X0 of the intersection is right.x0, if this is inside R1,
29067 otherwise there is no intersection. */
29068 if (right->x <= left->x + left->width)
29070 result->x = right->x;
29072 /* The right end of the intersection is the minimum of
29073 the right ends of left and right. */
29074 result->width = (min (left->x + left->width, right->x + right->width)
29075 - result->x);
29077 /* Same game for Y. */
29078 if (r1->y < r2->y)
29079 upper = r1, lower = r2;
29080 else
29081 upper = r2, lower = r1;
29083 /* The upper end of the intersection is lower.y0, if this is inside
29084 of upper. Otherwise, there is no intersection. */
29085 if (lower->y <= upper->y + upper->height)
29087 result->y = lower->y;
29089 /* The lower end of the intersection is the minimum of the lower
29090 ends of upper and lower. */
29091 result->height = (min (lower->y + lower->height,
29092 upper->y + upper->height)
29093 - result->y);
29094 intersection_p = 1;
29098 return intersection_p;
29101 #endif /* HAVE_WINDOW_SYSTEM */
29104 /***********************************************************************
29105 Initialization
29106 ***********************************************************************/
29108 void
29109 syms_of_xdisp (void)
29111 Vwith_echo_area_save_vector = Qnil;
29112 staticpro (&Vwith_echo_area_save_vector);
29114 Vmessage_stack = Qnil;
29115 staticpro (&Vmessage_stack);
29117 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29118 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29120 message_dolog_marker1 = Fmake_marker ();
29121 staticpro (&message_dolog_marker1);
29122 message_dolog_marker2 = Fmake_marker ();
29123 staticpro (&message_dolog_marker2);
29124 message_dolog_marker3 = Fmake_marker ();
29125 staticpro (&message_dolog_marker3);
29127 #ifdef GLYPH_DEBUG
29128 defsubr (&Sdump_frame_glyph_matrix);
29129 defsubr (&Sdump_glyph_matrix);
29130 defsubr (&Sdump_glyph_row);
29131 defsubr (&Sdump_tool_bar_row);
29132 defsubr (&Strace_redisplay);
29133 defsubr (&Strace_to_stderr);
29134 #endif
29135 #ifdef HAVE_WINDOW_SYSTEM
29136 defsubr (&Stool_bar_lines_needed);
29137 defsubr (&Slookup_image_map);
29138 #endif
29139 defsubr (&Sline_pixel_height);
29140 defsubr (&Sformat_mode_line);
29141 defsubr (&Sinvisible_p);
29142 defsubr (&Scurrent_bidi_paragraph_direction);
29143 defsubr (&Smove_point_visually);
29145 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29146 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29147 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29148 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29149 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29150 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29151 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29152 DEFSYM (Qeval, "eval");
29153 DEFSYM (QCdata, ":data");
29154 DEFSYM (Qdisplay, "display");
29155 DEFSYM (Qspace_width, "space-width");
29156 DEFSYM (Qraise, "raise");
29157 DEFSYM (Qslice, "slice");
29158 DEFSYM (Qspace, "space");
29159 DEFSYM (Qmargin, "margin");
29160 DEFSYM (Qpointer, "pointer");
29161 DEFSYM (Qleft_margin, "left-margin");
29162 DEFSYM (Qright_margin, "right-margin");
29163 DEFSYM (Qcenter, "center");
29164 DEFSYM (Qline_height, "line-height");
29165 DEFSYM (QCalign_to, ":align-to");
29166 DEFSYM (QCrelative_width, ":relative-width");
29167 DEFSYM (QCrelative_height, ":relative-height");
29168 DEFSYM (QCeval, ":eval");
29169 DEFSYM (QCpropertize, ":propertize");
29170 DEFSYM (QCfile, ":file");
29171 DEFSYM (Qfontified, "fontified");
29172 DEFSYM (Qfontification_functions, "fontification-functions");
29173 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29174 DEFSYM (Qescape_glyph, "escape-glyph");
29175 DEFSYM (Qnobreak_space, "nobreak-space");
29176 DEFSYM (Qimage, "image");
29177 DEFSYM (Qtext, "text");
29178 DEFSYM (Qboth, "both");
29179 DEFSYM (Qboth_horiz, "both-horiz");
29180 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29181 DEFSYM (QCmap, ":map");
29182 DEFSYM (QCpointer, ":pointer");
29183 DEFSYM (Qrect, "rect");
29184 DEFSYM (Qcircle, "circle");
29185 DEFSYM (Qpoly, "poly");
29186 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29187 DEFSYM (Qgrow_only, "grow-only");
29188 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29189 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29190 DEFSYM (Qposition, "position");
29191 DEFSYM (Qbuffer_position, "buffer-position");
29192 DEFSYM (Qobject, "object");
29193 DEFSYM (Qbar, "bar");
29194 DEFSYM (Qhbar, "hbar");
29195 DEFSYM (Qbox, "box");
29196 DEFSYM (Qhollow, "hollow");
29197 DEFSYM (Qhand, "hand");
29198 DEFSYM (Qarrow, "arrow");
29199 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29201 list_of_error = list1 (list2 (intern_c_string ("error"),
29202 intern_c_string ("void-variable")));
29203 staticpro (&list_of_error);
29205 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29206 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29207 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29208 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29210 echo_buffer[0] = echo_buffer[1] = Qnil;
29211 staticpro (&echo_buffer[0]);
29212 staticpro (&echo_buffer[1]);
29214 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29215 staticpro (&echo_area_buffer[0]);
29216 staticpro (&echo_area_buffer[1]);
29218 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29219 staticpro (&Vmessages_buffer_name);
29221 mode_line_proptrans_alist = Qnil;
29222 staticpro (&mode_line_proptrans_alist);
29223 mode_line_string_list = Qnil;
29224 staticpro (&mode_line_string_list);
29225 mode_line_string_face = Qnil;
29226 staticpro (&mode_line_string_face);
29227 mode_line_string_face_prop = Qnil;
29228 staticpro (&mode_line_string_face_prop);
29229 Vmode_line_unwind_vector = Qnil;
29230 staticpro (&Vmode_line_unwind_vector);
29232 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29234 help_echo_string = Qnil;
29235 staticpro (&help_echo_string);
29236 help_echo_object = Qnil;
29237 staticpro (&help_echo_object);
29238 help_echo_window = Qnil;
29239 staticpro (&help_echo_window);
29240 previous_help_echo_string = Qnil;
29241 staticpro (&previous_help_echo_string);
29242 help_echo_pos = -1;
29244 DEFSYM (Qright_to_left, "right-to-left");
29245 DEFSYM (Qleft_to_right, "left-to-right");
29247 #ifdef HAVE_WINDOW_SYSTEM
29248 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29249 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29250 For example, if a block cursor is over a tab, it will be drawn as
29251 wide as that tab on the display. */);
29252 x_stretch_cursor_p = 0;
29253 #endif
29255 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29256 doc: /* Non-nil means highlight trailing whitespace.
29257 The face used for trailing whitespace is `trailing-whitespace'. */);
29258 Vshow_trailing_whitespace = Qnil;
29260 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29261 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29262 If the value is t, Emacs highlights non-ASCII chars which have the
29263 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29264 or `escape-glyph' face respectively.
29266 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29267 U+2011 (non-breaking hyphen) are affected.
29269 Any other non-nil value means to display these characters as a escape
29270 glyph followed by an ordinary space or hyphen.
29272 A value of nil means no special handling of these characters. */);
29273 Vnobreak_char_display = Qt;
29275 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29276 doc: /* The pointer shape to show in void text areas.
29277 A value of nil means to show the text pointer. Other options are `arrow',
29278 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29279 Vvoid_text_area_pointer = Qarrow;
29281 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29282 doc: /* Non-nil means don't actually do any redisplay.
29283 This is used for internal purposes. */);
29284 Vinhibit_redisplay = Qnil;
29286 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29287 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29288 Vglobal_mode_string = Qnil;
29290 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29291 doc: /* Marker for where to display an arrow on top of the buffer text.
29292 This must be the beginning of a line in order to work.
29293 See also `overlay-arrow-string'. */);
29294 Voverlay_arrow_position = Qnil;
29296 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29297 doc: /* String to display as an arrow in non-window frames.
29298 See also `overlay-arrow-position'. */);
29299 Voverlay_arrow_string = build_pure_c_string ("=>");
29301 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29302 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29303 The symbols on this list are examined during redisplay to determine
29304 where to display overlay arrows. */);
29305 Voverlay_arrow_variable_list
29306 = list1 (intern_c_string ("overlay-arrow-position"));
29308 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29309 doc: /* The number of lines to try scrolling a window by when point moves out.
29310 If that fails to bring point back on frame, point is centered instead.
29311 If this is zero, point is always centered after it moves off frame.
29312 If you want scrolling to always be a line at a time, you should set
29313 `scroll-conservatively' to a large value rather than set this to 1. */);
29315 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29316 doc: /* Scroll up to this many lines, to bring point back on screen.
29317 If point moves off-screen, redisplay will scroll by up to
29318 `scroll-conservatively' lines in order to bring point just barely
29319 onto the screen again. If that cannot be done, then redisplay
29320 recenters point as usual.
29322 If the value is greater than 100, redisplay will never recenter point,
29323 but will always scroll just enough text to bring point into view, even
29324 if you move far away.
29326 A value of zero means always recenter point if it moves off screen. */);
29327 scroll_conservatively = 0;
29329 DEFVAR_INT ("scroll-margin", scroll_margin,
29330 doc: /* Number of lines of margin at the top and bottom of a window.
29331 Recenter the window whenever point gets within this many lines
29332 of the top or bottom of the window. */);
29333 scroll_margin = 0;
29335 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29336 doc: /* Pixels per inch value for non-window system displays.
29337 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29338 Vdisplay_pixels_per_inch = make_float (72.0);
29340 #ifdef GLYPH_DEBUG
29341 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29342 #endif
29344 DEFVAR_LISP ("truncate-partial-width-windows",
29345 Vtruncate_partial_width_windows,
29346 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29347 For an integer value, truncate lines in each window narrower than the
29348 full frame width, provided the window width is less than that integer;
29349 otherwise, respect the value of `truncate-lines'.
29351 For any other non-nil value, truncate lines in all windows that do
29352 not span the full frame width.
29354 A value of nil means to respect the value of `truncate-lines'.
29356 If `word-wrap' is enabled, you might want to reduce this. */);
29357 Vtruncate_partial_width_windows = make_number (50);
29359 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29360 doc: /* Maximum buffer size for which line number should be displayed.
29361 If the buffer is bigger than this, the line number does not appear
29362 in the mode line. A value of nil means no limit. */);
29363 Vline_number_display_limit = Qnil;
29365 DEFVAR_INT ("line-number-display-limit-width",
29366 line_number_display_limit_width,
29367 doc: /* Maximum line width (in characters) for line number display.
29368 If the average length of the lines near point is bigger than this, then the
29369 line number may be omitted from the mode line. */);
29370 line_number_display_limit_width = 200;
29372 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29373 doc: /* Non-nil means highlight region even in nonselected windows. */);
29374 highlight_nonselected_windows = 0;
29376 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29377 doc: /* Non-nil if more than one frame is visible on this display.
29378 Minibuffer-only frames don't count, but iconified frames do.
29379 This variable is not guaranteed to be accurate except while processing
29380 `frame-title-format' and `icon-title-format'. */);
29382 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29383 doc: /* Template for displaying the title bar of visible frames.
29384 \(Assuming the window manager supports this feature.)
29386 This variable has the same structure as `mode-line-format', except that
29387 the %c and %l constructs are ignored. It is used only on frames for
29388 which no explicit name has been set \(see `modify-frame-parameters'). */);
29390 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29391 doc: /* Template for displaying the title bar of an iconified frame.
29392 \(Assuming the window manager supports this feature.)
29393 This variable has the same structure as `mode-line-format' (which see),
29394 and is used only on frames for which no explicit name has been set
29395 \(see `modify-frame-parameters'). */);
29396 Vicon_title_format
29397 = Vframe_title_format
29398 = listn (CONSTYPE_PURE, 3,
29399 intern_c_string ("multiple-frames"),
29400 build_pure_c_string ("%b"),
29401 listn (CONSTYPE_PURE, 4,
29402 empty_unibyte_string,
29403 intern_c_string ("invocation-name"),
29404 build_pure_c_string ("@"),
29405 intern_c_string ("system-name")));
29407 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29408 doc: /* Maximum number of lines to keep in the message log buffer.
29409 If nil, disable message logging. If t, log messages but don't truncate
29410 the buffer when it becomes large. */);
29411 Vmessage_log_max = make_number (1000);
29413 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29414 doc: /* Functions called before redisplay, if window sizes have changed.
29415 The value should be a list of functions that take one argument.
29416 Just before redisplay, for each frame, if any of its windows have changed
29417 size since the last redisplay, or have been split or deleted,
29418 all the functions in the list are called, with the frame as argument. */);
29419 Vwindow_size_change_functions = Qnil;
29421 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29422 doc: /* List of functions to call before redisplaying a window with scrolling.
29423 Each function is called with two arguments, the window and its new
29424 display-start position. Note that these functions are also called by
29425 `set-window-buffer'. Also note that the value of `window-end' is not
29426 valid when these functions are called.
29428 Warning: Do not use this feature to alter the way the window
29429 is scrolled. It is not designed for that, and such use probably won't
29430 work. */);
29431 Vwindow_scroll_functions = Qnil;
29433 DEFVAR_LISP ("window-text-change-functions",
29434 Vwindow_text_change_functions,
29435 doc: /* Functions to call in redisplay when text in the window might change. */);
29436 Vwindow_text_change_functions = Qnil;
29438 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29439 doc: /* Functions called when redisplay of a window reaches the end trigger.
29440 Each function is called with two arguments, the window and the end trigger value.
29441 See `set-window-redisplay-end-trigger'. */);
29442 Vredisplay_end_trigger_functions = Qnil;
29444 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29445 doc: /* Non-nil means autoselect window with mouse pointer.
29446 If nil, do not autoselect windows.
29447 A positive number means delay autoselection by that many seconds: a
29448 window is autoselected only after the mouse has remained in that
29449 window for the duration of the delay.
29450 A negative number has a similar effect, but causes windows to be
29451 autoselected only after the mouse has stopped moving. \(Because of
29452 the way Emacs compares mouse events, you will occasionally wait twice
29453 that time before the window gets selected.\)
29454 Any other value means to autoselect window instantaneously when the
29455 mouse pointer enters it.
29457 Autoselection selects the minibuffer only if it is active, and never
29458 unselects the minibuffer if it is active.
29460 When customizing this variable make sure that the actual value of
29461 `focus-follows-mouse' matches the behavior of your window manager. */);
29462 Vmouse_autoselect_window = Qnil;
29464 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29465 doc: /* Non-nil means automatically resize tool-bars.
29466 This dynamically changes the tool-bar's height to the minimum height
29467 that is needed to make all tool-bar items visible.
29468 If value is `grow-only', the tool-bar's height is only increased
29469 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29470 Vauto_resize_tool_bars = Qt;
29472 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29473 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29474 auto_raise_tool_bar_buttons_p = 1;
29476 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29477 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29478 make_cursor_line_fully_visible_p = 1;
29480 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29481 doc: /* Border below tool-bar in pixels.
29482 If an integer, use it as the height of the border.
29483 If it is one of `internal-border-width' or `border-width', use the
29484 value of the corresponding frame parameter.
29485 Otherwise, no border is added below the tool-bar. */);
29486 Vtool_bar_border = Qinternal_border_width;
29488 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29489 doc: /* Margin around tool-bar buttons in pixels.
29490 If an integer, use that for both horizontal and vertical margins.
29491 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29492 HORZ specifying the horizontal margin, and VERT specifying the
29493 vertical margin. */);
29494 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29496 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29497 doc: /* Relief thickness of tool-bar buttons. */);
29498 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29500 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29501 doc: /* Tool bar style to use.
29502 It can be one of
29503 image - show images only
29504 text - show text only
29505 both - show both, text below image
29506 both-horiz - show text to the right of the image
29507 text-image-horiz - show text to the left of the image
29508 any other - use system default or image if no system default.
29510 This variable only affects the GTK+ toolkit version of Emacs. */);
29511 Vtool_bar_style = Qnil;
29513 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29514 doc: /* Maximum number of characters a label can have to be shown.
29515 The tool bar style must also show labels for this to have any effect, see
29516 `tool-bar-style'. */);
29517 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29519 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29520 doc: /* List of functions to call to fontify regions of text.
29521 Each function is called with one argument POS. Functions must
29522 fontify a region starting at POS in the current buffer, and give
29523 fontified regions the property `fontified'. */);
29524 Vfontification_functions = Qnil;
29525 Fmake_variable_buffer_local (Qfontification_functions);
29527 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29528 unibyte_display_via_language_environment,
29529 doc: /* Non-nil means display unibyte text according to language environment.
29530 Specifically, this means that raw bytes in the range 160-255 decimal
29531 are displayed by converting them to the equivalent multibyte characters
29532 according to the current language environment. As a result, they are
29533 displayed according to the current fontset.
29535 Note that this variable affects only how these bytes are displayed,
29536 but does not change the fact they are interpreted as raw bytes. */);
29537 unibyte_display_via_language_environment = 0;
29539 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29540 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29541 If a float, it specifies a fraction of the mini-window frame's height.
29542 If an integer, it specifies a number of lines. */);
29543 Vmax_mini_window_height = make_float (0.25);
29545 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29546 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29547 A value of nil means don't automatically resize mini-windows.
29548 A value of t means resize them to fit the text displayed in them.
29549 A value of `grow-only', the default, means let mini-windows grow only;
29550 they return to their normal size when the minibuffer is closed, or the
29551 echo area becomes empty. */);
29552 Vresize_mini_windows = Qgrow_only;
29554 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29555 doc: /* Alist specifying how to blink the cursor off.
29556 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29557 `cursor-type' frame-parameter or variable equals ON-STATE,
29558 comparing using `equal', Emacs uses OFF-STATE to specify
29559 how to blink it off. ON-STATE and OFF-STATE are values for
29560 the `cursor-type' frame parameter.
29562 If a frame's ON-STATE has no entry in this list,
29563 the frame's other specifications determine how to blink the cursor off. */);
29564 Vblink_cursor_alist = Qnil;
29566 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29567 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29568 If non-nil, windows are automatically scrolled horizontally to make
29569 point visible. */);
29570 automatic_hscrolling_p = 1;
29571 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29573 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29574 doc: /* How many columns away from the window edge point is allowed to get
29575 before automatic hscrolling will horizontally scroll the window. */);
29576 hscroll_margin = 5;
29578 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29579 doc: /* How many columns to scroll the window when point gets too close to the edge.
29580 When point is less than `hscroll-margin' columns from the window
29581 edge, automatic hscrolling will scroll the window by the amount of columns
29582 determined by this variable. If its value is a positive integer, scroll that
29583 many columns. If it's a positive floating-point number, it specifies the
29584 fraction of the window's width to scroll. If it's nil or zero, point will be
29585 centered horizontally after the scroll. Any other value, including negative
29586 numbers, are treated as if the value were zero.
29588 Automatic hscrolling always moves point outside the scroll margin, so if
29589 point was more than scroll step columns inside the margin, the window will
29590 scroll more than the value given by the scroll step.
29592 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29593 and `scroll-right' overrides this variable's effect. */);
29594 Vhscroll_step = make_number (0);
29596 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29597 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29598 Bind this around calls to `message' to let it take effect. */);
29599 message_truncate_lines = 0;
29601 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29602 doc: /* Normal hook run to update the menu bar definitions.
29603 Redisplay runs this hook before it redisplays the menu bar.
29604 This is used to update submenus such as Buffers,
29605 whose contents depend on various data. */);
29606 Vmenu_bar_update_hook = Qnil;
29608 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29609 doc: /* Frame for which we are updating a menu.
29610 The enable predicate for a menu binding should check this variable. */);
29611 Vmenu_updating_frame = Qnil;
29613 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29614 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29615 inhibit_menubar_update = 0;
29617 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29618 doc: /* Prefix prepended to all continuation lines at display time.
29619 The value may be a string, an image, or a stretch-glyph; it is
29620 interpreted in the same way as the value of a `display' text property.
29622 This variable is overridden by any `wrap-prefix' text or overlay
29623 property.
29625 To add a prefix to non-continuation lines, use `line-prefix'. */);
29626 Vwrap_prefix = Qnil;
29627 DEFSYM (Qwrap_prefix, "wrap-prefix");
29628 Fmake_variable_buffer_local (Qwrap_prefix);
29630 DEFVAR_LISP ("line-prefix", Vline_prefix,
29631 doc: /* Prefix prepended to all non-continuation lines at display time.
29632 The value may be a string, an image, or a stretch-glyph; it is
29633 interpreted in the same way as the value of a `display' text property.
29635 This variable is overridden by any `line-prefix' text or overlay
29636 property.
29638 To add a prefix to continuation lines, use `wrap-prefix'. */);
29639 Vline_prefix = Qnil;
29640 DEFSYM (Qline_prefix, "line-prefix");
29641 Fmake_variable_buffer_local (Qline_prefix);
29643 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29644 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29645 inhibit_eval_during_redisplay = 0;
29647 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29648 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29649 inhibit_free_realized_faces = 0;
29651 #ifdef GLYPH_DEBUG
29652 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29653 doc: /* Inhibit try_window_id display optimization. */);
29654 inhibit_try_window_id = 0;
29656 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29657 doc: /* Inhibit try_window_reusing display optimization. */);
29658 inhibit_try_window_reusing = 0;
29660 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29661 doc: /* Inhibit try_cursor_movement display optimization. */);
29662 inhibit_try_cursor_movement = 0;
29663 #endif /* GLYPH_DEBUG */
29665 DEFVAR_INT ("overline-margin", overline_margin,
29666 doc: /* Space between overline and text, in pixels.
29667 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29668 margin to the character height. */);
29669 overline_margin = 2;
29671 DEFVAR_INT ("underline-minimum-offset",
29672 underline_minimum_offset,
29673 doc: /* Minimum distance between baseline and underline.
29674 This can improve legibility of underlined text at small font sizes,
29675 particularly when using variable `x-use-underline-position-properties'
29676 with fonts that specify an UNDERLINE_POSITION relatively close to the
29677 baseline. The default value is 1. */);
29678 underline_minimum_offset = 1;
29680 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29681 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29682 This feature only works when on a window system that can change
29683 cursor shapes. */);
29684 display_hourglass_p = 1;
29686 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29687 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29688 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29690 #ifdef HAVE_WINDOW_SYSTEM
29691 hourglass_atimer = NULL;
29692 hourglass_shown_p = 0;
29693 #endif /* HAVE_WINDOW_SYSTEM */
29695 DEFSYM (Qglyphless_char, "glyphless-char");
29696 DEFSYM (Qhex_code, "hex-code");
29697 DEFSYM (Qempty_box, "empty-box");
29698 DEFSYM (Qthin_space, "thin-space");
29699 DEFSYM (Qzero_width, "zero-width");
29701 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29702 doc: /* Function run just before redisplay.
29703 It is called with one argument, which is the set of windows that are to
29704 be redisplayed. This set can be nil (meaning, only the selected window),
29705 or t (meaning all windows). */);
29706 Vpre_redisplay_function = intern ("ignore");
29708 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29709 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29711 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29712 doc: /* Char-table defining glyphless characters.
29713 Each element, if non-nil, should be one of the following:
29714 an ASCII acronym string: display this string in a box
29715 `hex-code': display the hexadecimal code of a character in a box
29716 `empty-box': display as an empty box
29717 `thin-space': display as 1-pixel width space
29718 `zero-width': don't display
29719 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29720 display method for graphical terminals and text terminals respectively.
29721 GRAPHICAL and TEXT should each have one of the values listed above.
29723 The char-table has one extra slot to control the display of a character for
29724 which no font is found. This slot only takes effect on graphical terminals.
29725 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29726 `thin-space'. The default is `empty-box'.
29728 If a character has a non-nil entry in an active display table, the
29729 display table takes effect; in this case, Emacs does not consult
29730 `glyphless-char-display' at all. */);
29731 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29732 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29733 Qempty_box);
29735 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29736 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29737 Vdebug_on_message = Qnil;
29741 /* Initialize this module when Emacs starts. */
29743 void
29744 init_xdisp (void)
29746 CHARPOS (this_line_start_pos) = 0;
29748 if (!noninteractive)
29750 struct window *m = XWINDOW (minibuf_window);
29751 Lisp_Object frame = m->frame;
29752 struct frame *f = XFRAME (frame);
29753 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29754 struct window *r = XWINDOW (root);
29755 int i;
29757 echo_area_window = minibuf_window;
29759 r->top_line = FRAME_TOP_MARGIN (f);
29760 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29761 r->total_cols = FRAME_COLS (f);
29763 m->top_line = FRAME_LINES (f) - 1;
29764 m->total_lines = 1;
29765 m->total_cols = FRAME_COLS (f);
29767 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29768 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29769 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29771 /* The default ellipsis glyphs `...'. */
29772 for (i = 0; i < 3; ++i)
29773 default_invis_vector[i] = make_number ('.');
29777 /* Allocate the buffer for frame titles.
29778 Also used for `format-mode-line'. */
29779 int size = 100;
29780 mode_line_noprop_buf = xmalloc (size);
29781 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29782 mode_line_noprop_ptr = mode_line_noprop_buf;
29783 mode_line_target = MODE_LINE_DISPLAY;
29786 help_echo_showing_p = 0;
29789 #ifdef HAVE_WINDOW_SYSTEM
29791 /* Platform-independent portion of hourglass implementation. */
29793 /* Cancel a currently active hourglass timer, and start a new one. */
29794 void
29795 start_hourglass (void)
29797 struct timespec delay;
29799 cancel_hourglass ();
29801 if (INTEGERP (Vhourglass_delay)
29802 && XINT (Vhourglass_delay) > 0)
29803 delay = make_timespec (min (XINT (Vhourglass_delay),
29804 TYPE_MAXIMUM (time_t)),
29806 else if (FLOATP (Vhourglass_delay)
29807 && XFLOAT_DATA (Vhourglass_delay) > 0)
29808 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29809 else
29810 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29812 #ifdef HAVE_NTGUI
29814 extern void w32_note_current_window (void);
29815 w32_note_current_window ();
29817 #endif /* HAVE_NTGUI */
29819 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29820 show_hourglass, NULL);
29824 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29825 shown. */
29826 void
29827 cancel_hourglass (void)
29829 if (hourglass_atimer)
29831 cancel_atimer (hourglass_atimer);
29832 hourglass_atimer = NULL;
29835 if (hourglass_shown_p)
29836 hide_hourglass ();
29839 #endif /* HAVE_WINDOW_SYSTEM */